Example usage for android.preference PreferenceGroup addPreference

List of usage examples for android.preference PreferenceGroup addPreference

Introduction

In this page you can find the example usage for android.preference PreferenceGroup addPreference.

Prototype

public boolean addPreference(Preference preference) 

Source Link

Document

Adds a Preference at the correct position based on the preference's order.

Usage

From source file:de.mrapp.android.preference.activity.PreferenceFragment.java

/**
 * Restores the default preferences, which are contained by a specific preference group.
 *
 * @param preferenceGroup/*from  www  . j ava  2s .c  om*/
 *         The preference group, whose preferences should be restored, as an instance of the
 *         class {@link PreferenceGroup}. The preference group may not be null
 * @param sharedPreferences
 *         The shared preferences, which should be used to restore the preferences, as an
 *         instance of the type {@link SharedPreferences}. The shared preferences may not be
 *         null
 */
private void restoreDefaults(@NonNull final PreferenceGroup preferenceGroup,
        @NonNull final SharedPreferences sharedPreferences) {
    for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
        Preference preference = preferenceGroup.getPreference(i);

        if (preference instanceof PreferenceGroup) {
            restoreDefaults((PreferenceGroup) preference, sharedPreferences);
        } else if (preference.getKey() != null && !preference.getKey().isEmpty()) {
            Object oldValue = sharedPreferences.getAll().get(preference.getKey());

            if (notifyOnRestoreDefaultValueRequested(preference, oldValue)) {
                sharedPreferences.edit().remove(preference.getKey()).apply();
                preferenceGroup.removePreference(preference);
                preferenceGroup.addPreference(preference);
                Object newValue = sharedPreferences.getAll().get(preference.getKey());
                notifyOnRestoredDefaultValue(preference, oldValue, newValue);
            } else {
                preferenceGroup.removePreference(preference);
                preferenceGroup.addPreference(preference);
            }

        }
    }
}

From source file:com.nttec.everychan.chans.allchan.AllchanModule.java

@Override
public void addPreferencesOnScreen(PreferenceGroup preferenceGroup) {
    Context context = preferenceGroup.getContext();
    addOnlyNewPostsPreference(preferenceGroup, true);
    final ListPreference captchaPreference = new LazyPreferences.ListPreference(context); //captcha_type
    captchaPreference.setTitle(R.string.pref_captcha_type);
    captchaPreference.setDialogTitle(R.string.pref_captcha_type);
    captchaPreference.setKey(getSharedKey(PREF_KEY_CAPTCHA_TYPE));
    captchaPreference.setEntryValues(CAPTCHA_TYPES_KEYS);
    captchaPreference.setEntries(CAPTCHA_TYPES);
    captchaPreference.setDefaultValue(CAPTCHA_TYPE_DEFAULT);
    int i = Arrays.asList(CAPTCHA_TYPES_KEYS)
            .indexOf(preferences.getString(getSharedKey(PREF_KEY_CAPTCHA_TYPE), CAPTCHA_TYPE_DEFAULT));
    if (i >= 0)
        captchaPreference.setSummary(CAPTCHA_TYPES[i]);
    preferenceGroup.addPreference(captchaPreference);
    addHttpsPreference(preferenceGroup, true);
    addCloudflareRecaptchaFallbackPreference(preferenceGroup);
    addProxyPreferences(preferenceGroup);

    final CheckBoxPreference proxyPreference = (CheckBoxPreference) preferenceGroup
            .findPreference(getSharedKey(PREF_KEY_USE_PROXY));
    proxyPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override/*from  w  w  w.  j  a v  a 2 s  .c o m*/
        public boolean onPreferenceClick(Preference preference) {
            if (proxyPreference.isChecked() && captchaPreference.getValue().equals("recaptcha")) {
                captchaPreference.setValue("recaptcha-fallback");
            }
            return false;
        }
    });
    captchaPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (proxyPreference.isChecked() && newValue.equals("recaptcha")) {
                captchaPreference.setValue("recaptcha-fallback");
                return false;
            }
            return true;
        }
    });
}

From source file:nya.miku.wishmaster.chans.tumbach.TumbachModule.java

@Override
public void addPreferencesOnScreen(PreferenceGroup preferenceGroup) {
    Context context = preferenceGroup.getContext();
    addOnlyNewPostsPreference(preferenceGroup, true);
    final ListPreference captchaPreference = new LazyPreferences.ListPreference(context); //captcha_type
    captchaPreference.setTitle(R.string.pref_captcha_type);
    captchaPreference.setDialogTitle(R.string.pref_captcha_type);
    captchaPreference.setKey(getSharedKey(PREF_KEY_CAPTCHA_TYPE));
    captchaPreference.setEntryValues(CAPTCHA_TYPES_KEYS);
    captchaPreference.setEntries(CAPTCHA_TYPES);
    captchaPreference.setDefaultValue(CAPTCHA_TYPE_DEFAULT);
    int i = Arrays.asList(CAPTCHA_TYPES_KEYS)
            .indexOf(preferences.getString(getSharedKey(PREF_KEY_CAPTCHA_TYPE), CAPTCHA_TYPE_DEFAULT));
    if (i >= 0)
        captchaPreference.setSummary(CAPTCHA_TYPES[i]);
    preferenceGroup.addPreference(captchaPreference);
    addPasswordPreference(preferenceGroup);
    addHttpsPreference(preferenceGroup, true);
    addCloudflareRecaptchaFallbackPreference(preferenceGroup);
    addProxyPreferences(preferenceGroup);

    final CheckBoxPreference proxyPreference = (CheckBoxPreference) preferenceGroup
            .findPreference(getSharedKey(PREF_KEY_USE_PROXY));
    proxyPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override/*w w w.  j a  va  2  s  .co m*/
        public boolean onPreferenceClick(Preference preference) {
            if (proxyPreference.isChecked() && captchaPreference.getValue().equals("recaptcha")) {
                captchaPreference.setValue("recaptcha-fallback");
            }
            return false;
        }
    });
    captchaPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (proxyPreference.isChecked() && newValue.equals("recaptcha")) {
                captchaPreference.setValue("recaptcha-fallback");
                return false;
            }
            return true;
        }
    });
}

From source file:com.nttec.everychan.chans.dobrochan.DobroModule.java

private void addRatingPreference(PreferenceGroup group) {
    Context context = group.getContext();
    Preference.OnPreferenceChangeListener updateRatingListener = new Preference.OnPreferenceChangeListener() {
        @Override//from ww  w  .ja v  a 2  s.c o  m
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (preference.getKey().equals(getSharedKey(PREF_KEY_MAX_RATING))) {
                setMaxRating((String) newValue);
                return true;
            }
            return false;
        }
    };
    ListPreference ratingPref = new LazyPreferences.ListPreference(context);
    ratingPref.setTitle(R.string.dobrochan_prefs_max_rating);
    ratingPref.setSummary(preferences.getString(getSharedKey(PREF_KEY_MAX_RATING), "R-15"));
    ratingPref.setEntries(RATINGS);
    ratingPref.setEntryValues(RATINGS);
    ratingPref.setDefaultValue("R-15");
    ratingPref.setKey(getSharedKey(PREF_KEY_MAX_RATING));
    ratingPref.setOnPreferenceChangeListener(updateRatingListener);
    group.addPreference(ratingPref);
}

From source file:com.nttec.everychan.chans.makaba.MakabaModule.java

/**   ?  ( .. https) */
private void addDomainPreferences(PreferenceGroup group) {
    Context context = group.getContext();
    Preference.OnPreferenceChangeListener updateDomainListener = new Preference.OnPreferenceChangeListener() {
        @Override/*from  w ww.j ava  2s. c o  m*/
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (preference.getKey().equals(getSharedKey(PREF_KEY_DOMAIN))) {
                updateDomain((String) newValue,
                        preferences.getBoolean(getSharedKey(PREF_KEY_USE_HTTPS_MAKABA), true));
                return true;
            } else if (preference.getKey().equals(getSharedKey(PREF_KEY_USE_HTTPS_MAKABA))) {
                updateDomain(preferences.getString(getSharedKey(PREF_KEY_DOMAIN), DEFAULT_DOMAIN),
                        (boolean) newValue);
                return true;
            }
            return false;
        }
    };
    PreferenceCategory domainCat = new PreferenceCategory(context);
    domainCat.setTitle(R.string.makaba_prefs_domain_category);
    group.addPreference(domainCat);
    EditTextPreference domainPref = new EditTextPreference(context); //  
    domainPref.setTitle(R.string.pref_domain);
    domainPref.setDialogTitle(R.string.pref_domain);
    domainPref.setSummary(resources.getString(R.string.pref_domain_summary, DOMAINS_HINT));
    domainPref.setKey(getSharedKey(PREF_KEY_DOMAIN));
    domainPref.getEditText().setHint(DEFAULT_DOMAIN);
    domainPref.getEditText().setSingleLine();
    domainPref.getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
    domainPref.setOnPreferenceChangeListener(updateDomainListener);
    domainCat.addPreference(domainPref);
    CheckBoxPreference httpsPref = new LazyPreferences.CheckBoxPreference(context); //? "? https"
    httpsPref.setTitle(R.string.pref_use_https);
    httpsPref.setSummary(R.string.pref_use_https_summary);
    httpsPref.setKey(getSharedKey(PREF_KEY_USE_HTTPS_MAKABA));
    httpsPref.setDefaultValue(true);
    httpsPref.setOnPreferenceChangeListener(updateDomainListener);
    domainCat.addPreference(httpsPref);
}

From source file:com.nttec.everychan.chans.dobrochan.DobroModule.java

private void addDomainPreferences(PreferenceGroup group) {
    Context context = group.getContext();
    Preference.OnPreferenceChangeListener updateDomainListener = new Preference.OnPreferenceChangeListener() {
        @Override//w w w .  j a  v  a  2  s .c o m
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (preference.getKey().equals(getSharedKey(PREF_KEY_DOMAIN))) {
                domain = (String) newValue;
                if (domain.length() == 0)
                    domain = DEFAULT_DOMAIN;
                loadHanabiraCookie();
                return true;
            }
            return false;
        }
    };
    EditTextPreference domainPref = new EditTextPreference(context);
    domainPref.setTitle(R.string.pref_domain);
    domainPref.setDialogTitle(R.string.pref_domain);
    domainPref.setSummary(resources.getString(R.string.pref_domain_summary, DOMAINS_HINT));
    domainPref.setKey(getSharedKey(PREF_KEY_DOMAIN));
    domainPref.getEditText().setHint(DEFAULT_DOMAIN);
    domainPref.getEditText().setSingleLine();
    domainPref.getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI);
    domainPref.setOnPreferenceChangeListener(updateDomainListener);
    group.addPreference(domainPref);
}