Example usage for android.preference PreferenceManager findPreference

List of usage examples for android.preference PreferenceManager findPreference

Introduction

In this page you can find the example usage for android.preference PreferenceManager findPreference.

Prototype

@Nullable
public Preference findPreference(CharSequence key) 

Source Link

Document

Finds a Preference based on its key.

Usage

From source file:Main.java

public static void enablePreference(PreferenceManager preferenceMgr, String prefKey, Boolean isEnabled) {
    Preference pref = preferenceMgr.findPreference(prefKey);
    if (pref != null) {
        preferenceMgr.findPreference(prefKey).setEnabled(isEnabled);
    }/* w  w w. j av  a  2s . c  om*/
}

From source file:im.vector.fragments.VectorSettingsPreferencesFragment.java

/**
 * Refresh the preferences.//from w  w w  .j av  a 2 s.  co  m
 */
private void refreshDisplay() {
    boolean isConnected = Matrix.getInstance(getActivity()).isConnected();
    Context appContext = getActivity().getApplicationContext();

    PreferenceManager preferenceManager = getPreferenceManager();

    // refresh the avatar
    UserAvatarPreference avatarPreference = (UserAvatarPreference) preferenceManager
            .findPreference(PreferencesManager.SETTINGS_PROFILE_PICTURE_PREFERENCE_KEY);
    avatarPreference.refreshAvatar();
    avatarPreference.setEnabled(isConnected);

    // refresh the display name
    final EditTextPreference displaynamePref = (EditTextPreference) preferenceManager
            .findPreference(PreferencesManager.SETTINGS_DISPLAY_NAME_PREFERENCE_KEY);
    displaynamePref.setSummary(mSession.getMyUser().displayname);
    displaynamePref.setText(mSession.getMyUser().displayname);
    displaynamePref.setEnabled(isConnected);

    // change password
    final EditTextPreference changePasswordPref = (EditTextPreference) preferenceManager
            .findPreference(PreferencesManager.SETTINGS_CHANGE_PASSWORD_PREFERENCE_KEY);
    changePasswordPref.setEnabled(isConnected);

    // update the push rules
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(appContext);

    BingRuleSet rules = mSession.getDataHandler().pushRules();

    GcmRegistrationManager gcmMgr = Matrix.getInstance(appContext).getSharedGCMRegistrationManager();

    for (String resourceText : mPushesRuleByResourceId.keySet()) {
        Preference preference = preferenceManager.findPreference(resourceText);

        if (null != preference) {
            if (preference instanceof BingRulePreference) {
                BingRulePreference bingRulePreference = (BingRulePreference) preference;
                bingRulePreference
                        .setEnabled((null != rules) && isConnected && gcmMgr.areDeviceNotificationsAllowed());
                bingRulePreference.setBingRule(mSession.getDataHandler().pushRules()
                        .findDefaultRule(mPushesRuleByResourceId.get(resourceText)));
            } else if (preference instanceof CheckBoxPreference) {
                CheckBoxPreference switchPreference = (CheckBoxPreference) preference;
                if (resourceText.equals(PreferencesManager.SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY)) {
                    switchPreference.setChecked(gcmMgr.areDeviceNotificationsAllowed());
                } else if (resourceText.equals(PreferencesManager.SETTINGS_TURN_SCREEN_ON_PREFERENCE_KEY)) {
                    switchPreference.setChecked(gcmMgr.isScreenTurnedOn());
                    switchPreference.setEnabled(gcmMgr.areDeviceNotificationsAllowed());
                } else {
                    switchPreference.setEnabled((null != rules) && isConnected);
                    switchPreference.setChecked(preferences.getBoolean(resourceText, false));
                }
            }
        }
    }

    // If notifications are disabled for the current user account or for the current user device
    // The others notifications settings have to be disable too
    boolean areNotifAllowed = rules != null && rules.findDefaultRule(BingRule.RULE_ID_DISABLE_ALL) != null
            && rules.findDefaultRule(BingRule.RULE_ID_DISABLE_ALL).isEnabled;

    Preference notificationSoundPreference = preferenceManager
            .findPreference(PreferencesManager.SETTINGS_NOTIFICATION_RINGTONE_SELECTION_PREFERENCE_KEY);
    notificationSoundPreference.setEnabled(!areNotifAllowed && gcmMgr.areDeviceNotificationsAllowed());

    Preference notificationPrivacyPreference = preferenceManager
            .findPreference(PreferencesManager.SETTINGS_NOTIFICATION_PRIVACY_PREFERENCE_KEY);
    if (notificationPrivacyPreference != null) {
        notificationPrivacyPreference
                .setEnabled(!areNotifAllowed && gcmMgr.areDeviceNotificationsAllowed() && gcmMgr.useGCM());
    }
}