summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/PrefUtils.java
blob: 217e40b9116fdc8e2cb7fdd9cb497ca3a70da105 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.util;

import java.util.HashSet;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.mozilla.gecko.AppConstants.Versions;

import android.content.SharedPreferences;
import android.util.Log;


public class PrefUtils {
    private static final String LOGTAG = "GeckoPrefUtils";

    // Cross version compatible way to get a string set from a pref
    public static Set<String> getStringSet(final SharedPreferences prefs,
                                           final String key,
                                           final Set<String> defaultVal) {
        if (!prefs.contains(key)) {
            return defaultVal;
        }

        // If this is Android version >= 11, try to use a Set<String>.
        try {
            return prefs.getStringSet(key, new HashSet<String>());
        } catch (ClassCastException ex) {
            // A ClassCastException means we've upgraded from a pre-v11 Android to a new one
            final Set<String> val = getFromJSON(prefs, key);
            SharedPreferences.Editor edit = prefs.edit();
            putStringSet(edit, key, val).apply();
            return val;
        }
    }

    private static Set<String> getFromJSON(SharedPreferences prefs, String key) {
        try {
            final String val = prefs.getString(key, "[]");
            return JSONUtils.parseStringSet(new JSONArray(val));
        } catch (JSONException ex) {
            Log.i(LOGTAG, "Unable to parse JSON", ex);
        }

        return new HashSet<String>();
    }

    /**
     * Cross version compatible way to save a set of strings.
     * <p>
     * This method <b>does not commit</b> any transaction. It is up to callers
     * to commit.
     *
     * @param editor to write to.
     * @param key to write.
     * @param vals comprising string set.
     * @return
     */
    public static SharedPreferences.Editor putStringSet(final SharedPreferences.Editor editor,
                                    final String key,
                                    final Set<String> vals) {
        editor.putStringSet(key, vals);
        return editor;
    }
}