Example usage for android.content SharedPreferences getStringSet

List of usage examples for android.content SharedPreferences getStringSet

Introduction

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

Prototype

@Nullable
Set<String> getStringSet(String key, @Nullable Set<String> defValues);

Source Link

Document

Retrieve a set of String values from the preferences.

Usage

From source file:be.uclouvain.multipathcontrol.stats.JSONSender.java

/**
 * Get all sets of data that have to be send, prepare the JSON and send it.
 * If correctly sent, clear data from settings.
 */// www . ja v a  2s  .co m
public static void sendAll(Context context) {
    if (!trySending())
        return;

    SharedPreferences settings = context.getSharedPreferences(Config.PREFS_NAME, Context.MODE_PRIVATE);

    for (StatsCategories category : StatsCategories.values()) {
        String key = Config.PREFS_STATS_SET + '_' + category;
        Set<String> statsSet = settings.getStringSet(key, null);
        if (statsSet == null)
            continue;

        JSONSender jsonSenders[] = new JSONSender[statsSet.size()];
        int i = 0;
        // Get all data sets and send them
        for (String name : statsSet)
            jsonSenders[i++] = new JSONSender(context, name, category);

        JSONSenderTask jsonSenderTask = new JSONSenderTask(settings, category);
        jsonSenderTask.execute(jsonSenders);
    }
}

From source file:cc.flydev.launcher.InstallShortcutReceiver.java

public static void removeFromInstallQueue(SharedPreferences sharedPrefs, ArrayList<String> packageNames) {
    synchronized (sLock) {
        Set<String> strings = sharedPrefs.getStringSet(APPS_PENDING_INSTALL, null);
        if (DBG) {
            Log.d(TAG, "APPS_PENDING_INSTALL: " + strings + ", removing packages: " + packageNames);
        }/*from  w  ww.  j a v a 2  s.  c  o  m*/
        if (strings != null) {
            Set<String> newStrings = new HashSet<String>(strings);
            Iterator<String> newStringsIter = newStrings.iterator();
            while (newStringsIter.hasNext()) {
                String json = newStringsIter.next();
                try {
                    JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
                    Intent launchIntent = Intent.parseUri(object.getString(LAUNCH_INTENT_KEY), 0);
                    String pn = launchIntent.getPackage();
                    if (pn == null) {
                        pn = launchIntent.getComponent().getPackageName();
                    }
                    if (packageNames.contains(pn)) {
                        newStringsIter.remove();
                    }
                } catch (org.json.JSONException e) {
                    Log.d(TAG, "Exception reading shortcut to remove: " + e);
                } catch (java.net.URISyntaxException e) {
                    Log.d(TAG, "Exception reading shortcut to remove: " + e);
                }
            }
            sharedPrefs.edit().putStringSet(APPS_PENDING_INSTALL, new HashSet<String>(newStrings)).commit();
        }
    }
}

From source file:org.chromium.chrome.browser.media.MediaCaptureNotificationService.java

/**
 * Clear any previous media notifications.
 *///  w w w.  j  a  v a 2s. c  om
public static void clearMediaNotifications(Context context) {
    SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferences();
    Set<String> notificationIds = sharedPreferences.getStringSet(WEBRTC_NOTIFICATION_IDS, null);
    if (notificationIds == null || notificationIds.isEmpty())
        return;

    context.startService(new Intent(context, MediaCaptureNotificationService.class));
}

From source file:org.chromium.chrome.browser.media.MediaCaptureNotificationService.java

private static boolean shouldStartService(Context context, int mediaType, int tabId) {
    if (mediaType != MEDIATYPE_NO_MEDIA)
        return true;
    SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferences();
    Set<String> notificationIds = sharedPreferences.getStringSet(WEBRTC_NOTIFICATION_IDS, null);
    if (notificationIds != null && !notificationIds.isEmpty()
            && notificationIds.contains(String.valueOf(tabId))) {
        return true;
    }// ww w  .  j a  v  a 2s . c  o m
    return false;
}

From source file:org.chromium.chrome.browser.ntp.ContentSuggestionsNotificationHelper.java

/** Returns a mutable copy of the named pref. Never returns null. */
private static Set<String> getMutableStringSetPreference(SharedPreferences prefs, String prefName) {
    Set<String> prefValue = prefs.getStringSet(prefName, null);
    if (prefValue == null) {
        return new HashSet<String>();
    }/*w w  w.  ja v a 2s  .  c om*/
    return new HashSet<String>(prefValue);
}

From source file:com.andernity.launcher2.InstallShortcutReceiver.java

private static ArrayList<PendingInstallShortcutInfo> getAndClearInstallQueue(SharedPreferences sharedPrefs) {
    synchronized (sLock) {
        Set<String> strings = sharedPrefs.getStringSet(APPS_PENDING_INSTALL, null);
        if (strings == null) {
            return new ArrayList<PendingInstallShortcutInfo>();
        }/*www  .  ja  v a  2  s  . com*/
        ArrayList<PendingInstallShortcutInfo> infos = new ArrayList<PendingInstallShortcutInfo>();
        for (String json : strings) {
            try {
                JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
                Intent data = Intent.parseUri(object.getString(DATA_INTENT_KEY), 0);
                Intent launchIntent = Intent.parseUri(object.getString(LAUNCH_INTENT_KEY), 0);
                String name = object.getString(NAME_KEY);
                String iconBase64 = object.optString(ICON_KEY);
                String iconResourceName = object.optString(ICON_RESOURCE_NAME_KEY);
                String iconResourcePackageName = object.optString(ICON_RESOURCE_PACKAGE_NAME_KEY);
                if (iconBase64 != null && !iconBase64.isEmpty()) {
                    byte[] iconArray = Base64.decode(iconBase64, Base64.DEFAULT);
                    Bitmap b = BitmapFactory.decodeByteArray(iconArray, 0, iconArray.length);
                    data.putExtra(Intent.EXTRA_SHORTCUT_ICON, b);
                } else if (iconResourceName != null && !iconResourceName.isEmpty()) {
                    Intent.ShortcutIconResource iconResource = new Intent.ShortcutIconResource();
                    iconResource.resourceName = iconResourceName;
                    iconResource.packageName = iconResourcePackageName;
                    data.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
                }
                data.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
                PendingInstallShortcutInfo info = new PendingInstallShortcutInfo(data, name, launchIntent);
                infos.add(info);
            } catch (org.json.JSONException e) {
                Log.d("InstallShortcutReceiver", "Exception reading shortcut to add: " + e);
            } catch (java.net.URISyntaxException e) {
                Log.d("InstallShortcutReceiver", "Exception reading shortcut to add: " + e);
            }
        }
        sharedPrefs.edit().putStringSet(APPS_PENDING_INSTALL, new HashSet<String>()).commit();
        return infos;
    }
}

From source file:org.chromium.chrome.browser.ntp.ContentSuggestionsNotificationHelper.java

private static Collection<ActiveNotification> getActiveNotifications() {
    SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
    Set<String> activeNotifications = prefs.getStringSet(PREF_ACTIVE_NOTIFICATIONS, null);
    if (activeNotifications == null)
        return Collections.emptySet();

    Set<ActiveNotification> result = new HashSet<ActiveNotification>();
    for (String serialized : activeNotifications) {
        Uri notificationUri = Uri.parse(serialized);
        ActiveNotification activeNotification = ActiveNotification.fromUri(notificationUri);
        if (activeNotification != null)
            result.add(activeNotification);
    }//from  w w  w . ja v  a 2 s . c om
    return result;
}

From source file:org.chromium.chrome.browser.ntp.ContentSuggestionsNotificationHelper.java

/** Returns an ActiveNotification if a corresponding one is found, otherwise null. */
private static ActiveNotification findActiveNotification(int category, String idWithinCategory) {
    SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
    Set<String> activeNotifications = prefs.getStringSet(PREF_ACTIVE_NOTIFICATIONS, null);
    if (activeNotifications == null)
        return null;

    for (String serialized : activeNotifications) {
        Uri notificationUri = Uri.parse(serialized);
        ActiveNotification activeNotification = ActiveNotification.fromUri(notificationUri);
        if ((activeNotification != null) && (activeNotification.mCategory == category)
                && (activeNotification.mIdWithinCategory.equals(idWithinCategory))) {
            return activeNotification;
        }/*from ww w.ja va  2 s.c o m*/
    }
    return null;
}

From source file:org.chromium.chrome.browser.ntp.ContentSuggestionsNotificationHelper.java

/** Returns a non-negative integer greater than any active notification's notification ID. */
private static int nextNotificationId() {
    SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
    Set<String> activeNotifications = prefs.getStringSet(PREF_ACTIVE_NOTIFICATIONS, null);
    if (activeNotifications == null)
        return 0;

    int nextId = 0;
    for (String serialized : activeNotifications) {
        Uri notificationUri = Uri.parse(serialized);
        ActiveNotification activeNotification = ActiveNotification.fromUri(notificationUri);
        if ((activeNotification != null) && (activeNotification.mId >= nextId)) {
            nextId = activeNotification.mId + 1;
        }//from   w ww . j  a  v  a 2  s.c  o m
    }
    return nextId;
}

From source file:com.android.launcher3.InstallShortcutReceiver.java

private static ArrayList<PendingInstallShortcutInfo> getAndClearInstallQueue(SharedPreferences sharedPrefs,
        Context context) {/* w w  w  .jav a2  s .c  om*/
    synchronized (sLock) {
        Set<String> strings = sharedPrefs.getStringSet(APPS_PENDING_INSTALL, null);
        if (DBG)
            Log.d(TAG, "Getting and clearing APPS_PENDING_INSTALL: " + strings);
        if (strings == null) {
            return new ArrayList<PendingInstallShortcutInfo>();
        }
        ArrayList<PendingInstallShortcutInfo> infos = new ArrayList<PendingInstallShortcutInfo>();
        for (String encoded : strings) {
            PendingInstallShortcutInfo info = decode(encoded, context);
            if (info != null) {
                infos.add(info);
            }
        }
        sharedPrefs.edit().putStringSet(APPS_PENDING_INSTALL, new HashSet<String>()).commit();
        return infos;
    }
}