Example usage for android.content SharedPreferences getAll

List of usage examples for android.content SharedPreferences getAll

Introduction

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

Prototype

Map<String, ?> getAll();

Source Link

Document

Retrieve all values from the preferences.

Usage

From source file:com.master.metehan.filtereagle.ActivityMain.java

@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String name) {
    Log.i(TAG, "Preference " + name + "=" + prefs.getAll().get(name));
    if ("enabled".equals(name)) {
        // Get enabled
        boolean enabled = prefs.getBoolean(name, false);

        // Display disabled warning
        TextView tvDisabled = (TextView) findViewById(R.id.tvDisabled);
        tvDisabled.setVisibility(enabled ? View.GONE : View.VISIBLE);

        // Check switch state
        SwitchCompat swEnabled = (SwitchCompat) getSupportActionBar().getCustomView()
                .findViewById(R.id.swEnabled);
        if (swEnabled.isChecked() != enabled)
            swEnabled.setChecked(enabled);

    } else if ("whitelist_wifi".equals(name) || "screen_wifi".equals(name) || "whitelist_other".equals(name)
            || "screen_other".equals(name) || "whitelist_roaming".equals(name) || "show_user".equals(name)
            || "show_system".equals(name) || "show_nointernet".equals(name) || "show_disabled".equals(name)
            || "sort".equals(name) || "imported".equals(name))
        updateApplicationList(null);/*from   w  w w  . j av  a  2 s .  c om*/

    else if ("manage_system".equals(name)) {
        invalidateOptionsMenu();
        updateApplicationList(null);
        LinearLayout llSystem = (LinearLayout) findViewById(R.id.llSystem);
        boolean system = prefs.getBoolean("manage_system", false);
        boolean hint = prefs.getBoolean("hint_system", true);
        llSystem.setVisibility(!system && hint ? View.VISIBLE : View.GONE);

    } else if ("theme".equals(name) || "dark_theme".equals(name))
        recreate();
}

From source file:nf.frex.android.FrexActivity.java

private Registry<ColorScheme> getColorSchemes() {
    if (colorSchemes == null) {
        colorSchemes = Registries.colorSchemes;
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        Map<String, ?> all = preferences.getAll();
        for (String key : all.keySet()) {
            if (key.startsWith("$")) {
                String value = preferences.getString(key, null);
                if (value != null) {
                    try {
                        ColorScheme colorScheme = ColorScheme.parse(value);
                        colorSchemes.add(key, colorScheme);
                    } catch (IllegalArgumentException e) {
                        // What to do  here?
                    }//from  www. j a v a2  s . c o  m
                }
            }
        }
    }
    return colorSchemes;
}

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  w w w .j  a  v  a 2  s. c o m*/
 *         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.zhengde163.netguard.ActivityMain.java

@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String name) {
    Log.i(TAG, "Preference " + name + "=" + prefs.getAll().get(name));
    if ("enabled".equals(name)) {
        // Get enabled
        boolean enabled = prefs.getBoolean(name, false);

        // Display disabled warning
        //            TextView tvDisabled = (TextView) findViewById(R.id.tvDisabled);
        //            tvDisabled.setVisibility(enabled ? View.GONE : View.VISIBLE);
        LinearLayout ly = (LinearLayout) findViewById(R.id.lldisable);
        ly.setVisibility(enabled ? View.GONE : View.VISIBLE);
        // Check switch state
        ImageView ivEnabled = (ImageView) findViewById(R.id.ivEnabled);
        if (ivEnabled != null) {
            if (enabled) {
                ivEnabled.setImageResource(R.drawable.on);
            } else {
                ivEnabled.setImageResource(R.drawable.off);
            }/*from w w  w . j av  a  2  s.  c  o m*/
        }

    } else if ("whitelist_wifi".equals(name) || "screen_wifi".equals(name) || "whitelist_other".equals(name)
            || "screen_other".equals(name) || "whitelist_roaming".equals(name) || "show_user".equals(name)
            || "show_system".equals(name) || "show_nointernet".equals(name) || "show_disabled".equals(name)
            || "sort".equals(name) || "imported".equals(name))
        updateApplicationList(null);
    else if ("theme".equals(name) || "dark_theme".equals(name))
        recreate();
}

From source file:com.aware.Aware.java

public static void reset(Context c) {
    if (!c.getPackageName().equals("com.aware"))
        return;// www.  ja v  a  2s .c  o  m

    String device_id = Aware.getSetting(c, Aware_Preferences.DEVICE_ID);

    //Remove all settings
    c.getContentResolver().delete(Aware_Settings.CONTENT_URI, null, null);

    //Read default client settings
    SharedPreferences prefs = c.getSharedPreferences(c.getPackageName(), Context.MODE_PRIVATE);
    PreferenceManager.setDefaultValues(c, c.getPackageName(), Context.MODE_PRIVATE, R.xml.aware_preferences,
            true);
    prefs.edit().commit();

    Map<String, ?> defaults = prefs.getAll();
    for (Map.Entry<String, ?> entry : defaults.entrySet()) {
        Aware.setSetting(c, entry.getKey(), entry.getValue());
    }

    //Restore old Device ID
    Aware.setSetting(c, Aware_Preferences.DEVICE_ID, device_id);

    //Turn off all active plugins
    Cursor active_plugins = c.getContentResolver().query(Aware_Plugins.CONTENT_URI, null,
            Aware_Plugins.PLUGIN_STATUS + "=" + Plugins_Manager.PLUGIN_ACTIVE, null, null);
    if (active_plugins != null && active_plugins.moveToFirst()) {
        do {
            Aware.stopPlugin(c,
                    active_plugins.getString(active_plugins.getColumnIndex(Aware_Plugins.PLUGIN_PACKAGE_NAME)));
        } while (active_plugins.moveToNext());
        active_plugins.close();
    }

    //Apply fresh state
    Intent aware_apply = new Intent(Aware.ACTION_AWARE_REFRESH);
    c.sendBroadcast(aware_apply);

    Intent preferences = new Intent(c, Aware_Preferences.class);
    preferences.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    c.startActivity(preferences);
}

From source file:de.mkrtchyan.recoverytools.RecoveryTools.java

/**
 * @return All Preferences as String/*from   w ww.  ja v a 2s.c o m*/
 */
public String getAllPrefs() {
    SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    String Prefs = "";
    Map<String, ?> prefsMap = prefs.getAll();
    for (Map.Entry<String, ?> entry : prefsMap.entrySet()) {
        /**
         * Skip following Prefs (PREF_KEY_KERNEL_HISTORY, PREF_KEY_RECOVERY_HISTORY ...)
         */
        try {
            if (!entry.getKey().contains(PREF_KEY_KERNEL_HISTORY)
                    && !entry.getKey().contains(PREF_KEY_RECOVERY_HISTORY)
                    && !entry.getKey().contains(FlashUtil.PREF_KEY_FLASH_KERNEL_COUNTER)
                    && !entry.getKey().contains(FlashUtil.PREF_KEY_FLASH_RECOVERY_COUNTER)) {
                Prefs += entry.getKey() + ": " + entry.getValue().toString() + "\n";
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }
    return Prefs;
}

From source file:dev.ukanth.ufirewall.Api.java

private static JSONArray getAllAppPreferences(Context ctx, SharedPreferences gPrefs) throws JSONException {
    Map<String, ?> keys = gPrefs.getAll();
    JSONArray arr = new JSONArray();
    for (Map.Entry<String, ?> entry : keys.entrySet()) {
        JSONObject obj = new JSONObject();
        obj.put(entry.getKey(), entry.getValue().toString());
        arr.put(obj);//from w w  w. ja  v a2  s .c  om
    }
    return arr;
}

From source file:android_network.hetnet.vpn_service.ActivitySettings.java

private void xmlExport(SharedPreferences prefs, XmlSerializer serializer) throws IOException {
    Map<String, ?> settings = prefs.getAll();
    for (String key : settings.keySet()) {
        Object value = settings.get(key);

        if ("imported".equals(key))
            continue;

        if (value instanceof Boolean) {
            serializer.startTag(null, "setting");
            serializer.attribute(null, "key", key);
            serializer.attribute(null, "type", "boolean");
            serializer.attribute(null, "value", value.toString());
            serializer.endTag(null, "setting");

        } else if (value instanceof Integer) {
            serializer.startTag(null, "setting");
            serializer.attribute(null, "key", key);
            serializer.attribute(null, "type", "integer");
            serializer.attribute(null, "value", value.toString());
            serializer.endTag(null, "setting");

        } else if (value instanceof String) {
            serializer.startTag(null, "setting");
            serializer.attribute(null, "key", key);
            serializer.attribute(null, "type", "string");
            serializer.attribute(null, "value", value.toString());
            serializer.endTag(null, "setting");

        } else if (value instanceof Set) {
            Set<String> set = (Set<String>) value;
            serializer.startTag(null, "setting");
            serializer.attribute(null, "key", key);
            serializer.attribute(null, "type", "set");
            serializer.attribute(null, "value", TextUtils.join("\n", set));
            serializer.endTag(null, "setting");

        } else//from  w  w  w.ja  va 2  s  . co m
            Log.e(TAG, "Unknown key=" + key);
    }
}

From source file:android_network.hetnet.vpn_service.ActivitySettings.java

private void xmlImport(Map<String, Object> settings, SharedPreferences prefs) {
    SharedPreferences.Editor editor = prefs.edit();

    // Clear existing setting
    for (String key : prefs.getAll().keySet())
        if (!"enabled".equals(key))
            editor.remove(key);// w w  w.j  a  va 2  s  . c  om

    // Apply new settings
    for (String key : settings.keySet()) {
        Object value = settings.get(key);
        if (value instanceof Boolean)
            editor.putBoolean(key, (Boolean) value);
        else if (value instanceof Integer)
            editor.putInt(key, (Integer) value);
        else if (value instanceof String)
            editor.putString(key, (String) value);
        else if (value instanceof Set)
            editor.putStringSet(key, (Set<String>) value);
        else
            Log.e(TAG, "Unknown type=" + value.getClass());
    }

    editor.apply();
}

From source file:com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPush.java

/**
 * Based on the PREFS_NOTIFICATION_COUNT value, fetch all the stored notifications in the same order from
 * the shared preferences and save it onto the list.
 * This method will ensure that the notifications are sent to the Application in the same order in which they arrived.
 *//*from  w  ww .ja v  a2s .  c  o m*/
public boolean getMessagesFromSharedPreferences(int notificationId) {
    boolean gotMessages = false;
    SharedPreferences sharedPreferences = appContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

    int countOfStoredMessages = sharedPreferences.getInt(MFPPush.PREFS_NOTIFICATION_COUNT, 0);

    if (countOfStoredMessages > 0) {
        String key = null;
        try {
            Map<String, ?> allEntriesFromSharedPreferences = sharedPreferences.getAll();
            Map<String, String> notificationEntries = new HashMap<String, String>();

            for (Map.Entry<String, ?> entry : allEntriesFromSharedPreferences.entrySet()) {
                String rKey = entry.getKey();
                if (entry.getKey().startsWith(PREFS_NOTIFICATION_MSG)) {
                    notificationEntries.put(rKey, entry.getValue().toString());
                }
            }

            for (Map.Entry<String, String> entry : notificationEntries.entrySet()) {
                String nKey = entry.getKey();
                key = nKey;
                String msg = sharedPreferences.getString(nKey, null);

                if (msg != null) {
                    gotMessages = true;
                    logger.debug(
                            "MFPPush:getMessagesFromSharedPreferences() - Messages retrieved from shared preferences.");
                    MFPInternalPushMessage pushMessage = new MFPInternalPushMessage(new JSONObject(msg));

                    if (notificationId != 0) {
                        if (notificationId == pushMessage.getNotificationId()) {
                            isFromNotificationBar = true;
                            messageFromBar = pushMessage;
                            MFPPushUtils.removeContentFromSharedPreferences(sharedPreferences, nKey);
                            MFPPushUtils.storeContentInSharedPreferences(sharedPreferences,
                                    MFPPush.PREFS_NOTIFICATION_COUNT, countOfStoredMessages - 1);
                            break;
                        }
                    } else {
                        synchronized (pending) {
                            pending.add(pushMessage);
                        }
                        MFPPushUtils.removeContentFromSharedPreferences(sharedPreferences, nKey);
                    }
                }
            }

        } catch (JSONException e) {
            MFPPushUtils.removeContentFromSharedPreferences(sharedPreferences, key);
        }
        if (notificationId == 0) {
            MFPPushUtils.storeContentInSharedPreferences(sharedPreferences, MFPPush.PREFS_NOTIFICATION_COUNT,
                    0);
        }
    }

    return gotMessages;
}