Example usage for android.preference Preference getSharedPreferences

List of usage examples for android.preference Preference getSharedPreferences

Introduction

In this page you can find the example usage for android.preference Preference getSharedPreferences.

Prototype

public SharedPreferences getSharedPreferences() 

Source Link

Document

Returns the SharedPreferences where this Preference can read its value(s).

Usage

From source file:com.android.inputmethod.latin.settings.CustomInputStyleSettingsFragment.java

static void updateCustomInputStylesSummary(final Preference pref) {
    // When we are called from the Settings application but we are not already running, some
    // singleton and utility classes may not have been initialized.  We have to call
    // initialization method of these classes here. See {@link LatinIME#onCreate()}.
    SubtypeLocaleUtils.init(pref.getContext());

    final Resources res = pref.getContext().getResources();
    final SharedPreferences prefs = pref.getSharedPreferences();
    final String prefSubtype = Settings.readPrefAdditionalSubtypes(prefs, res);
    final InputMethodSubtype[] subtypes = AdditionalSubtypeUtils.createAdditionalSubtypesArray(prefSubtype);
    final ArrayList<String> subtypeNames = new ArrayList<>();
    for (final InputMethodSubtype subtype : subtypes) {
        subtypeNames.add(SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(subtype));
    }//ww w  . jav  a  2 s .c o  m
    // TODO: A delimiter of custom input styles should be localized.
    pref.setSummary(TextUtils.join(", ", subtypeNames));
}

From source file:com.sbhstimetable.sbhs_timetable_android.SettingsFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.pref_notification);
    addPreferencesFromResource(R.xml.pref_widget);
    addPreferencesFromResource(R.xml.pref_appearance);
    String[] prefs = new String[] { PrefUtil.WIDGET_TRANSPARENCY_HS, PrefUtil.WIDGET_TRANSPARENCY_LS,
            PrefUtil.THEME, PrefUtil.COLOUR }; // settings to attach listeners to

    // don't offer lock screen widget options on platforms that don't support them
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1
            || Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
        mPreferenceScreen = getPreferenceScreen();
        mListPreference = (ListPreference) findPreference("widget_transparency_lockscreen");
        mPreferenceScreen.removePreference(mListPreference);
        prefs = new String[] { PrefUtil.WIDGET_TRANSPARENCY_HS, PrefUtil.THEME, PrefUtil.COLOUR };

    }// ww w  .j  av  a  2  s.c o m
    for (String pref : prefs) {
        Preference thePref = this.findPreference(pref);
        thePref.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
        SharedPreferences p = thePref.getSharedPreferences();
        String defaultVal = ((ListPreference) thePref).getValue();
        thePref.getOnPreferenceChangeListener().onPreferenceChange(thePref, p.getString(pref, defaultVal));
    }
}

From source file:com.geecko.QuickLyric.fragment.SettingsFragment.java

@Override
public boolean onPreferenceChange(Preference pref, Object newValue) {
    switch (pref.getKey()) {
    case "pref_theme":
        if (!newValue.equals(pref.getSharedPreferences().getString("pref_theme", "0"))) {
            getActivity().finish();//from w  ww.ja  v a2  s. com
            Intent intent = new Intent(getActivity(), MainActivity.class);
            intent.setAction("android.intent.action.MAIN");
            startActivity(intent);
        }
        return true;
    case "pref_opendyslexic":
        if (!newValue.equals(pref.getSharedPreferences().getBoolean("pref_opendyslexic", false))) {
            getActivity().finish();
            Intent intent = new Intent(getActivity(), MainActivity.class);
            intent.setAction("android.intent.action.MAIN");
            startActivity(intent);
        }
        return true;
    case "pref_notifications":
        if (newValue.equals("0")) {
            ((NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE)).cancelAll();
        } else {
            SharedPreferences current = getActivity().getSharedPreferences("current_music",
                    Context.MODE_PRIVATE);
            Intent intent = new Intent();
            intent.setAction("com.geecko.QuickLyric.SHOW_NOTIFICATION");
            intent.putExtra("artist", current.getString("artist", "Michael Jackson"));
            intent.putExtra("track", current.getString("track", "Bad"));
            intent.putExtra("playing", current.getBoolean("playing", false));
            getActivity().sendBroadcast(intent);
        }
        return true;
    case "pref_night_mode":
        if ((Boolean) newValue) {
            boolean twentyFourHourStyle = DateFormat.is24HourFormat(getActivity());
            TimePickerDialog tpd = TimePickerDialog.newInstance(this, 21, 0, twentyFourHourStyle);
            tpd.setCancelable(false);
            tpd.show(getFragmentManager(), NIGHT_START_TIME_DIALOG_TAG);
        } else if (NightTimeVerifier.check(getActivity())) {
            getActivity().finish();
            Intent intent = new Intent(getActivity(), MainActivity.class);
            intent.setAction("android.intent.action.MAIN");
            startActivity(intent);
        }
        return true;
    default:
        return true;
    }
}

From source file:com.jmstudios.redmoon.fragment.ShadesFragment.java

private boolean onAutomaticFilterPreferenceChange(Preference preference, Object newValue) {
    if (newValue.toString().equals("sun") && ContextCompat.checkSelfPermission(getActivity(),
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(getActivity(),
                new String[] { Manifest.permission.ACCESS_COARSE_LOCATION }, 0);
        return false;
    }//from  w  w w .  j a  v a2s  .  c o  m

    boolean custom = newValue.toString().equals("custom");
    automaticTurnOnPref.setEnabled(custom);
    automaticTurnOffPref.setEnabled(custom);

    boolean sun = newValue.toString().equals("sun");
    locationPref.setEnabled(sun);

    // From something to sun
    if (newValue.toString().equals("sun")) {
        // Update the FilterTimePreferences
        updateFilterTimesFromSun();

        // Attempt to get a new location
        locationPref.searchLocation(false);
    }

    // From sun to something
    String oldValue = preference.getSharedPreferences().getString(preference.getKey(), "never");
    if (oldValue.equals("sun") && !newValue.equals("sun")) {
        automaticTurnOnPref.setToCustomTime();
        automaticTurnOffPref.setToCustomTime();
    }

    ListPreference lp = (ListPreference) preference;
    String entry = lp.getEntries()[lp.findIndexOfValue(newValue.toString())].toString();
    lp.setSummary(entry);

    return true;
}

From source file:nz.Intelx.DroidNetLogin.DroidNetLoginActivity.java

@Override
public boolean onPreferenceChange(Preference pref, Object newValue) {

    //match summary for Login list preference to selected preference

    if (pref.getKey().equals("login_preference")) {
        ListPreference login_preference = (ListPreference) pref;
        login_preference.setValue((String) newValue);
        CharSequence Login_Summary = login_preference.getEntry();
        login_preference.setSummary(Login_Summary);
    }//from w  ww.ja v  a2  s.co m
    if (pref.getKey().equals("debug_preference")) {
        CheckBoxPreference debug_preference = (CheckBoxPreference) pref;
        debug_preference.setChecked((Boolean) newValue);
    }
    if (pref.getKey().equals("proxy_preference")) {
        CheckBoxPreference proxy_preference = (CheckBoxPreference) pref;
        proxy_preference.setChecked((Boolean) newValue);
    }
    updatePreference(pref.getSharedPreferences());
    return true;
}