Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 package org.adblockplus.sbrowser.contentblocker.util; | 18 package org.adblockplus.sbrowser.contentblocker.util; |
19 | 19 |
20 import android.content.Context; | 20 import android.content.Context; |
21 import android.content.SharedPreferences; | 21 import android.content.SharedPreferences; |
22 import android.preference.PreferenceManager; | 22 import android.preference.PreferenceManager; |
23 | 23 |
24 import java.util.Set; | 24 import java.util.Set; |
25 | 25 |
26 import static android.content.SharedPreferences.*; | |
diegocarloslima
2018/01/19 14:26:11
We usually try to avoid the wildcard import
jens
2018/01/19 14:36:07
Acknowledged.
| |
27 | |
28 public class SharedPrefsUtils | 26 public class SharedPrefsUtils |
29 { | 27 { |
30 | 28 |
31 public static void putBoolean(Context context, int keyResId, boolean value) | 29 public static void putBoolean(Context context, int keyResId, boolean value) |
32 { | 30 { |
33 final Editor editor = getDefaultSharedPreferences(context).edit(); | 31 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit(); |
34 editor.putBoolean(context.getString(keyResId), value).apply(); | 32 editor.putBoolean(context.getString(keyResId), value).apply(); |
35 } | 33 } |
36 | 34 |
37 public static boolean getBoolean(Context context, int keyResId, boolean defVal ue) | 35 public static boolean getBoolean(Context context, int keyResId, boolean defVal ue) |
38 { | 36 { |
39 final SharedPreferences preferences = getDefaultSharedPreferences(context); | 37 final SharedPreferences preferences = getDefaultSharedPreferences(context); |
40 try | 38 try |
41 { | 39 { |
42 return preferences.getBoolean(context.getString(keyResId), defValue); | 40 return preferences.getBoolean(context.getString(keyResId), defValue); |
43 } | 41 } |
44 catch (ClassCastException e) | 42 catch (ClassCastException e) |
45 { | 43 { |
46 return defValue; | 44 return defValue; |
47 } | 45 } |
48 } | 46 } |
49 | 47 |
50 public static void putInt(Context context, int keyResId, int value) | 48 public static void putInt(Context context, int keyResId, int value) |
51 { | 49 { |
52 final Editor editor = getDefaultSharedPreferences(context).edit(); | 50 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit(); |
53 editor.putInt(context.getString(keyResId), value).apply(); | 51 editor.putInt(context.getString(keyResId), value).apply(); |
54 } | 52 } |
55 | 53 |
56 public static int getInt(Context context, int keyResId, int defValue) | 54 public static int getInt(Context context, int keyResId, int defValue) |
57 { | 55 { |
58 final SharedPreferences preferences = getDefaultSharedPreferences(context); | 56 final SharedPreferences preferences = getDefaultSharedPreferences(context); |
59 try | 57 try |
60 { | 58 { |
61 return preferences.getInt(context.getString(keyResId), defValue); | 59 return preferences.getInt(context.getString(keyResId), defValue); |
62 } | 60 } |
63 catch (ClassCastException e) | 61 catch (ClassCastException e) |
64 { | 62 { |
65 return defValue; | 63 return defValue; |
66 } | 64 } |
67 } | 65 } |
68 | 66 |
69 public static void putString(Context context, int keyResId, String value) | 67 public static void putString(Context context, int keyResId, String value) |
70 { | 68 { |
71 final Editor editor = getDefaultSharedPreferences(context).edit(); | 69 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit(); |
72 editor.putString(context.getString(keyResId), value).apply(); | 70 editor.putString(context.getString(keyResId), value).apply(); |
73 } | 71 } |
74 | 72 |
75 public static String getString(Context context, int keyResId, String defValue) | 73 public static String getString(Context context, int keyResId, String defValue) |
76 { | 74 { |
77 final SharedPreferences preferences = getDefaultSharedPreferences(context); | 75 final SharedPreferences preferences = getDefaultSharedPreferences(context); |
78 try | 76 try |
79 { | 77 { |
80 return preferences.getString(context.getString(keyResId), defValue); | 78 return preferences.getString(context.getString(keyResId), defValue); |
81 } | 79 } |
82 catch (ClassCastException e) | 80 catch (ClassCastException e) |
83 { | 81 { |
84 return defValue; | 82 return defValue; |
85 } | 83 } |
86 } | 84 } |
87 | 85 |
88 public static void putStringSet(Context context, int keyResId, Set<String> val ues) | 86 public static void putStringSet(Context context, int keyResId, Set<String> val ues) |
89 { | 87 { |
90 final Editor editor = getDefaultSharedPreferences(context).edit(); | 88 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit(); |
91 editor.putStringSet(context.getString(keyResId), values).apply(); | 89 editor.putStringSet(context.getString(keyResId), values).apply(); |
92 } | 90 } |
93 | 91 |
94 public static Set<String> getStringSet(Context context, int keyResId, Set<Stri ng> defValues) | 92 public static Set<String> getStringSet(Context context, int keyResId, Set<Stri ng> defValues) |
95 { | 93 { |
96 final SharedPreferences preferences = getDefaultSharedPreferences(context); | 94 final SharedPreferences preferences = getDefaultSharedPreferences(context); |
97 try | 95 try |
98 { | 96 { |
99 return preferences.getStringSet(context.getString(keyResId), defValues); | 97 return preferences.getStringSet(context.getString(keyResId), defValues); |
100 } | 98 } |
(...skipping 13 matching lines...) Expand all Loading... | |
114 OnSharedPreferenceChangeListener listener) | 112 OnSharedPreferenceChangeListener listener) |
115 { | 113 { |
116 getDefaultSharedPreferences(context).unregisterOnSharedPreferenceChangeListe ner(listener); | 114 getDefaultSharedPreferences(context).unregisterOnSharedPreferenceChangeListe ner(listener); |
117 } | 115 } |
118 | 116 |
119 private static SharedPreferences getDefaultSharedPreferences(Context context) | 117 private static SharedPreferences getDefaultSharedPreferences(Context context) |
120 { | 118 { |
121 return PreferenceManager.getDefaultSharedPreferences(context.getApplicationC ontext()); | 119 return PreferenceManager.getDefaultSharedPreferences(context.getApplicationC ontext()); |
122 } | 120 } |
123 | 121 |
124 public abstract static class OnSharedPreferenceChangeListener | 122 public abstract static class OnSharedPreferenceChangeListener |
anton
2018/01/22 05:41:13
are you sure that it's better to use class hierarc
diegocarloslima
2018/01/22 11:09:26
With an interface, we cannot make onSharedPreferen
| |
125 implements SharedPreferences.OnSharedPreferenceChangeListener | 123 implements SharedPreferences.OnSharedPreferenceChangeListener |
126 { | 124 { |
127 | 125 |
128 @Override | 126 @Override |
129 public final void onSharedPreferenceChanged(SharedPreferences sharedPreferen ces, String key) | 127 public final void onSharedPreferenceChanged(SharedPreferences sharedPreferen ces, String key) |
130 { | 128 { |
131 this.onSharedPreferenceChanged(key); | 129 this.onSharedPreferenceChanged(key); |
132 } | 130 } |
133 | 131 |
134 protected abstract void onSharedPreferenceChanged(String key); | 132 protected abstract void onSharedPreferenceChanged(String key); |
135 } | 133 } |
136 } | 134 } |
LEFT | RIGHT |