Example usage for android.content SharedPreferences contains

List of usage examples for android.content SharedPreferences contains

Introduction

In this page you can find the example usage for android.content SharedPreferences contains.

Prototype

boolean contains(String key);

Source Link

Document

Checks whether the preferences contains a preference.

Usage

From source file:im.neon.util.PhoneNumberUtils.java

/**
 * Provide the selected country code//from  w  ww  .  j  a v  a  2  s  .  co  m
 *
 * @param context the application context
 * @return the ISO country code or "" if it does not exist
 */
public static String getCountryCode(final Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

    if (!preferences.contains(COUNTRY_CODE_PREF_KEY)
            || TextUtils.isEmpty(preferences.getString(COUNTRY_CODE_PREF_KEY, ""))) {
        try {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            String countryCode = tm.getNetworkCountryIso().toUpperCase();
            if (TextUtils.isEmpty(countryCode) && !TextUtils.isEmpty(Locale.getDefault().getCountry())
                    && PhoneNumberUtil.getInstance()
                            .getCountryCodeForRegion(Locale.getDefault().getCountry()) != 0) {
                // Use Locale as a last resort
                setCountryCode(context, Locale.getDefault().getCountry());
            } else {
                setCountryCode(context, countryCode);
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "## getCountryCode failed " + e.getMessage());
        }
    }

    return preferences.getString(COUNTRY_CODE_PREF_KEY, "");
}

From source file:im.vector.util.ThemeUtils.java

/**
 * Provides the selected application theme
 *
 * @param context the context/*from   w  w  w .j  a  v  a2s . c  o m*/
 * @return the selected application theme
 */
public static String getApplicationTheme(Context context) {
    String appTheme = THEME_LIGHT_VALUE;
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);

    // defines a default value if not defined
    if (!sp.contains(APPLICATION_THEME_KEY)) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(APPLICATION_THEME_KEY, THEME_LIGHT_VALUE);
        editor.commit();
    } else {
        appTheme = sp.getString(APPLICATION_THEME_KEY, THEME_LIGHT_VALUE);
    }

    return appTheme;
}

From source file:io.v.android.apps.syncslides.SignInActivity.java

/**
 * Returns the best-effort full name of the signed-in user.
 *//*w  w  w. j  a  v a2  s.c om*/
public static String getUserName(Context ctx) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    // First try to read the user name we obtained from profile, as it's most accurate.
    if (prefs.contains(PREF_USER_NAME_FROM_PROFILE)) {
        return prefs.getString(PREF_USER_NAME_FROM_PROFILE, "Anonymous User");
    }
    return prefs.getString(PREF_USER_NAME_FROM_CONTACTS, "Anonymous User");
}

From source file:im.vector.contacts.ContactsManager.java

/**
 * Tells if the contacts book access has been requested.
 * For android > M devices, it only tells if the permission has been granted.
 * @param activity the calling activity/*from w w w.ja v  a  2  s. c  o  m*/
 * @return true it was requested once
 */
public static boolean isContactBookAccessRequested(Activity activity) {
    if (Build.VERSION.SDK_INT >= 23) {
        return (PackageManager.PERMISSION_GRANTED == ContextCompat
                .checkSelfPermission(activity.getApplicationContext(), Manifest.permission.READ_CONTACTS));
    } else {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
        return preferences.contains(CONTACTS_BOOK_ACCESS_KEY);
    }
}

From source file:de.schaeuffelhut.android.openvpn.Preferences.java

public static boolean hasPassphrase(Context context, File configFile) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    return sharedPreferences.contains(Preferences.KEY_CONFIG_PASSPHRASE(configFile));
}

From source file:de.schaeuffelhut.android.openvpn.Preferences.java

public static boolean hasCredentials(Context context, File configFile) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    return sharedPreferences.contains(Preferences.KEY_CONFIG_USERNAME(configFile))
            && sharedPreferences.contains(Preferences.KEY_CONFIG_PASSWORD(configFile));
}

From source file:fr.simon.marquis.preferencesmanager.util.Utils.java

private static void initFavorites(Context ctx) {
    if (favorites == null) {
        favorites = new HashSet<String>();

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ctx);

        if (sp.contains(FAVORITES_KEY)) {
            try {
                JSONArray array = new JSONArray(sp.getString(FAVORITES_KEY, "[]"));
                for (int i = 0; i < array.length(); i++) {
                    favorites.add(array.optString(i));
                }/*from   w w w. j a  v  a 2 s  . co  m*/
            } catch (JSONException e) {
                Log.e(TAG, "error parsing JSON", e);
            }
        }
    }
}

From source file:org.mozilla.gecko.home.HomeConfigPrefsBackend.java

/**
 * Migrates JSON config data storage./*  w ww  .jav a2  s.  com*/
 *
 * @param context Context used to get shared preferences and create built-in panel.
 * @param jsonString String currently stored in preferences.
 *
 * @return JSONArray array representing new set of panel configs.
 */
private static synchronized JSONArray maybePerformMigration(Context context, String jsonString)
        throws JSONException {
    // If the migration is already done, we're at the current version.
    if (sMigrationDone) {
        final JSONObject json = new JSONObject(jsonString);
        return json.getJSONArray(JSON_KEY_PANELS);
    }

    // Make sure we only do this version check once.
    sMigrationDone = true;

    final JSONArray jsonPanels;
    final int version;

    final SharedPreferences prefs = GeckoSharedPrefs.forProfile(context);
    if (prefs.contains(PREFS_CONFIG_KEY_OLD)) {
        // Our original implementation did not contain versioning, so this is implicitly version 0.
        jsonPanels = new JSONArray(jsonString);
        version = 0;
    } else {
        final JSONObject json = new JSONObject(jsonString);
        jsonPanels = json.getJSONArray(JSON_KEY_PANELS);
        version = json.getInt(JSON_KEY_VERSION);
    }

    if (version == VERSION) {
        return jsonPanels;
    }

    Log.d(LOGTAG, "Performing migration");

    final SharedPreferences.Editor prefsEditor = prefs.edit();

    for (int v = version + 1; v <= VERSION; v++) {
        Log.d(LOGTAG, "Migrating to version = " + v);

        switch (v) {
        case 1:
            // Add "Recent Tabs" panel.
            addBuiltinPanelConfig(context, jsonPanels, PanelType.RECENT_TABS, Position.FRONT, Position.BACK);

            // Remove the old pref key.
            prefsEditor.remove(PREFS_CONFIG_KEY_OLD);
            break;

        case 2:
            // Add "Remote Tabs"/"Synced Tabs" panel.
            addBuiltinPanelConfig(context, jsonPanels, PanelType.REMOTE_TABS, Position.FRONT, Position.BACK);
            break;

        case 3:
            // Add the "Reading List" panel if it does not exist. At one time,
            // the Reading List panel was shown only to devices that were not
            // considered "low memory". Now, we expose the panel to all devices.
            // This migration should only occur for "low memory" devices.
            // Note: This will not agree with the default configuration, which
            // has REMOTE_TABS after READING_LIST on some devices.
            if (!readingListPanelExists(jsonPanels)) {
                addBuiltinPanelConfig(context, jsonPanels, PanelType.READING_LIST, Position.BACK,
                        Position.BACK);
            }
            break;
        }
    }

    // Save the new panel config and the new version number.
    final JSONObject newJson = new JSONObject();
    newJson.put(JSON_KEY_PANELS, jsonPanels);
    newJson.put(JSON_KEY_VERSION, VERSION);

    prefsEditor.putString(PREFS_CONFIG_KEY, newJson.toString());
    prefsEditor.apply();

    return jsonPanels;
}

From source file:org.ohmage.triggers.notif.NotifSurveyAdaptor.java

private static boolean IsSurveyTaken(Context context, String campaignUrn, String survey, long since) {

    SharedPreferences pref = context
            .getSharedPreferences(NotifSurveyAdaptor.class.getName() + "_" + campaignUrn, Context.MODE_PRIVATE);

    if (!pref.contains(survey)) {
        return false;
    }/*from w ww . j a  v a 2  s  .  c om*/

    if (pref.getLong(survey, 0) <= since) {
        return false;
    }

    return true;
}

From source file:org.ohmage.reminders.notif.NotifSurveyAdaptor.java

private static boolean IsSurveyTakenOrIgnored(Context context, String survey, long since) {

    SharedPreferences pref = context.getSharedPreferences(NotifSurveyAdaptor.class.getName(),
            Context.MODE_PRIVATE);

    return (pref.contains(survey) && pref.getLong(survey, 0) > since)
            || (pref.contains(survey + "_ignored") && pref.getLong(survey + "_ignored", 0) > since);
}