Search in sources :

Example 1 with SwitchPreferenceCompat

use of androidx.preference.SwitchPreferenceCompat in project IITB-App by wncc.

the class SettingsFragment method onCreatePreferences.

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    setPreferencesFromResource(R.xml.preferences, rootKey);
    // Get preferences and editor
    SharedPreferences sharedPref = getActivity().getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE);
    editor = sharedPref.edit();
    // Show contact number
    showContactPref = (SwitchPreferenceCompat) findPreference("show_contact");
    showContactPref.setOnPreferenceChangeListener((preference, option) -> {
        toggleShowContact((SwitchPreferenceCompat) preference, option);
        return false;
    });
    showContactPref.setEnabled(false);
    // Dark Theme
    SwitchPreferenceCompat darkThemePref = (SwitchPreferenceCompat) findPreference("dark_theme");
    darkThemePref.setOnPreferenceChangeListener((preference, option) -> {
        toggleDarkTheme((boolean) option);
        return true;
    });
    darkThemePref.setChecked(sharedPref.getBoolean(Constants.DARK_THEME, false));
    // Add to Calendar
    ListPreference calendarPref = (ListPreference) findPreference("add_to_calendar");
    calendarPref.setOnPreferenceChangeListener((preference, option) -> {
        toggleCalendarDialog((String) option);
        return true;
    });
    calendarPref.setValue(sharedPref.getString(Constants.CALENDAR_DIALOG, Constants.CALENDAR_DIALOG_ALWAYS_ASK));
    // Update Profile
    Preference profilePref = findPreference("profile");
    profilePref.setOnPreferenceClickListener(preference -> {
        openWebURL("https://gymkhana.iitb.ac.in/sso/user");
        return false;
    });
    // Feedback
    Preference feedbackPref = findPreference("feedback");
    feedbackPref.setOnPreferenceClickListener(preference -> {
        openWebURL("https://www.insti.app/feedback");
        return false;
    });
    // About
    Preference aboutPref = findPreference("about");
    aboutPref.setOnPreferenceClickListener(preference -> {
        openAbout();
        return false;
    });
    // Logout
    Preference logoutPref = findPreference("logout");
    logoutPref.setOnPreferenceClickListener(preference -> {
        logout();
        return false;
    });
    // Disable buttons if not logged in
    final SessionManager sessionManager = new SessionManager(getContext());
    if (!sessionManager.isLoggedIn()) {
        showContactPref.setVisible(false);
        logoutPref.setVisible(false);
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) SwitchPreferenceCompat(androidx.preference.SwitchPreferenceCompat) Preference(androidx.preference.Preference) ListPreference(androidx.preference.ListPreference) SessionManager(app.insti.SessionManager) ListPreference(androidx.preference.ListPreference)

Example 2 with SwitchPreferenceCompat

use of androidx.preference.SwitchPreferenceCompat in project kdeconnect-android by KDE.

the class SettingsFragment method onCreatePreferences.

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    Context context = getPreferenceManager().getContext();
    PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(context);
    // Rename device
    renameDevice = new EditTextPreference(context);
    renameDevice.setKey(DeviceHelper.KEY_DEVICE_NAME_PREFERENCE);
    renameDevice.setSelectable(true);
    String deviceName = DeviceHelper.getDeviceName(context);
    renameDevice.setTitle(R.string.settings_rename);
    renameDevice.setSummary(deviceName);
    renameDevice.setDialogTitle(R.string.device_rename_title);
    renameDevice.setText(deviceName);
    renameDevice.setPositiveButtonText(R.string.device_rename_confirm);
    renameDevice.setNegativeButtonText(R.string.cancel);
    renameDevice.setOnPreferenceChangeListener((preference, newValue) -> {
        String name = (String) newValue;
        if (TextUtils.isEmpty(name)) {
            if (getView() != null) {
                Snackbar snackbar = Snackbar.make(getView(), R.string.invalid_device_name, Snackbar.LENGTH_LONG);
                int currentTheme = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
                if (currentTheme != Configuration.UI_MODE_NIGHT_YES) {
                    // white color is set to the background of snackbar if dark mode is off
                    snackbar.getView().setBackgroundColor(Color.WHITE);
                }
                snackbar.show();
            }
            return false;
        }
        renameDevice.setSummary((String) newValue);
        return true;
    });
    screen.addPreference(renameDevice);
    // Theme Selector
    ListPreference themeSelector = new ListPreference(context);
    themeSelector.setKey("theme_pref");
    themeSelector.setTitle(R.string.theme_dialog_title);
    themeSelector.setDialogTitle(R.string.theme_dialog_title);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        themeSelector.setEntries(R.array.theme_list_v28);
    } else {
        themeSelector.setEntries(R.array.theme_list);
    }
    themeSelector.setEntryValues(R.array.theme_list_values);
    themeSelector.setDefaultValue(ThemeUtil.DEFAULT_MODE);
    themeSelector.setSummaryProvider(ListPreference.SimpleSummaryProvider.getInstance());
    themeSelector.setOnPreferenceChangeListener((preference, newValue) -> {
        String themeValue = (String) newValue;
        ThemeUtil.applyTheme(themeValue);
        return true;
    });
    screen.addPreference(themeSelector);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Preference persistentNotif = new Preference(context);
        persistentNotif.setTitle(R.string.setting_persistent_notification_oreo);
        persistentNotif.setSummary(R.string.setting_persistent_notification_description);
        persistentNotif.setOnPreferenceClickListener(preference -> {
            Intent intent = new Intent();
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName());
            context.startActivity(intent);
            return true;
        });
        screen.addPreference(persistentNotif);
    } else {
        // Persistent notification toggle for Android Versions below Oreo
        final TwoStatePreference notificationSwitch = new SwitchPreferenceCompat(context);
        notificationSwitch.setPersistent(false);
        notificationSwitch.setChecked(NotificationHelper.isPersistentNotificationEnabled(context));
        notificationSwitch.setTitle(R.string.setting_persistent_notification);
        notificationSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
            final boolean isChecked = (Boolean) newValue;
            NotificationHelper.setPersistentNotificationEnabled(context, isChecked);
            BackgroundService.RunCommand(context, service -> service.changePersistentNotificationVisibility(isChecked));
            NotificationHelper.setPersistentNotificationEnabled(context, isChecked);
            return true;
        });
        screen.addPreference(notificationSwitch);
    }
    // Trusted Networks
    Preference trustedNetworkPref = new Preference(context);
    trustedNetworkPref.setPersistent(false);
    trustedNetworkPref.setTitle(R.string.trusted_networks);
    trustedNetworkPref.setSummary(R.string.trusted_networks_desc);
    screen.addPreference(trustedNetworkPref);
    trustedNetworkPref.setOnPreferenceClickListener(preference -> {
        startActivity(new Intent(context, TrustedNetworksActivity.class));
        return true;
    });
    // Add device by IP
    Preference devicesByIpPreference = new Preference(context);
    devicesByIpPreference.setPersistent(false);
    devicesByIpPreference.setTitle(R.string.custom_device_list);
    screen.addPreference(devicesByIpPreference);
    devicesByIpPreference.setOnPreferenceClickListener(preference -> {
        startActivity(new Intent(context, CustomDevicesActivity.class));
        return true;
    });
    // More settings text
    Preference moreSettingsText = new Preference(context);
    moreSettingsText.setPersistent(false);
    moreSettingsText.setSelectable(false);
    moreSettingsText.setTitle(R.string.settings_more_settings_title);
    moreSettingsText.setSummary(R.string.settings_more_settings_text);
    screen.addPreference(moreSettingsText);
    setPreferenceScreen(screen);
}
Also used : Context(android.content.Context) TwoStatePreference(androidx.preference.TwoStatePreference) PreferenceScreen(androidx.preference.PreferenceScreen) SwitchPreferenceCompat(androidx.preference.SwitchPreferenceCompat) Intent(android.content.Intent) EditTextPreference(androidx.preference.EditTextPreference) ListPreference(androidx.preference.ListPreference) TwoStatePreference(androidx.preference.TwoStatePreference) Preference(androidx.preference.Preference) ListPreference(androidx.preference.ListPreference) EditTextPreference(androidx.preference.EditTextPreference) Snackbar(com.google.android.material.snackbar.Snackbar)

Example 3 with SwitchPreferenceCompat

use of androidx.preference.SwitchPreferenceCompat in project kdeconnect-android by KDE.

the class ShareSettingsFragment method onCreatePreferences.

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    super.onCreatePreferences(savedInstanceState, rootKey);
    PreferenceScreen preferenceScreen = getPreferenceScreen();
    final SwitchPreferenceCompat customDownloads = preferenceScreen.findPreference("share_destination_custom");
    filePicker = preferenceScreen.findPreference("share_destination_folder_preference");
    if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)) {
        customDownloads.setOnPreferenceChangeListener((preference, newValue) -> {
            updateFilePickerStatus((Boolean) newValue);
            return true;
        });
        filePicker.setOnPreferenceClickListener(preference -> {
            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
            startActivityForResult(intent, RESULT_PICKER);
            return true;
        });
    } else {
        customDownloads.setEnabled(false);
        filePicker.setEnabled(false);
    }
    boolean customized = PreferenceManager.getDefaultSharedPreferences(requireContext()).getBoolean(PREFERENCE_CUSTOMIZE_DESTINATION, false);
    updateFilePickerStatus(customized);
}
Also used : PreferenceScreen(androidx.preference.PreferenceScreen) SwitchPreferenceCompat(androidx.preference.SwitchPreferenceCompat) Intent(android.content.Intent)

Aggregations

SwitchPreferenceCompat (androidx.preference.SwitchPreferenceCompat)3 Intent (android.content.Intent)2 ListPreference (androidx.preference.ListPreference)2 Preference (androidx.preference.Preference)2 PreferenceScreen (androidx.preference.PreferenceScreen)2 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 EditTextPreference (androidx.preference.EditTextPreference)1 TwoStatePreference (androidx.preference.TwoStatePreference)1 SessionManager (app.insti.SessionManager)1 Snackbar (com.google.android.material.snackbar.Snackbar)1