Left: | ||
Right: |
OLD | NEW |
---|---|
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 | |
26 public class SharedPrefsUtils | 28 public class SharedPrefsUtils |
27 { | 29 { |
28 | 30 |
29 public static void putBoolean(Context context, int keyResId, boolean value) | 31 public static void putBoolean(Context context, int keyResId, boolean value) |
30 { | 32 { |
31 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit(); | 33 final Editor editor = getDefaultSharedPreferences(context).edit(); |
32 editor.putBoolean(context.getString(keyResId), value).apply(); | 34 editor.putBoolean(context.getString(keyResId), value).apply(); |
33 } | 35 } |
34 | 36 |
35 public static boolean getBoolean(Context context, int keyResId, boolean defVal ue) | 37 public static boolean getBoolean(Context context, int keyResId, boolean defVal ue) |
36 { | 38 { |
37 final SharedPreferences preferences = getDefaultSharedPreferences(context); | 39 final SharedPreferences preferences = getDefaultSharedPreferences(context); |
38 try | 40 try |
39 { | 41 { |
40 return preferences.getBoolean(context.getString(keyResId), defValue); | 42 return preferences.getBoolean(context.getString(keyResId), defValue); |
41 } | 43 } |
42 catch (ClassCastException e) | 44 catch (ClassCastException e) |
43 { | 45 { |
44 return defValue; | 46 return defValue; |
45 } | 47 } |
46 } | 48 } |
47 | 49 |
48 public static void putInt(Context context, int keyResId, int value) | 50 public static void putInt(Context context, int keyResId, int value) |
49 { | 51 { |
50 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit(); | 52 final Editor editor = getDefaultSharedPreferences(context).edit(); |
51 editor.putInt(context.getString(keyResId), value).apply(); | 53 editor.putInt(context.getString(keyResId), value).apply(); |
52 } | 54 } |
53 | 55 |
54 public static int getInt(Context context, int keyResId, int defValue) | 56 public static int getInt(Context context, int keyResId, int defValue) |
55 { | 57 { |
56 final SharedPreferences preferences = getDefaultSharedPreferences(context); | 58 final SharedPreferences preferences = getDefaultSharedPreferences(context); |
57 try | 59 try |
58 { | 60 { |
59 return preferences.getInt(context.getString(keyResId), defValue); | 61 return preferences.getInt(context.getString(keyResId), defValue); |
60 } | 62 } |
61 catch (ClassCastException e) | 63 catch (ClassCastException e) |
62 { | 64 { |
63 return defValue; | 65 return defValue; |
64 } | 66 } |
65 } | 67 } |
66 | 68 |
67 public static void putString(Context context, int keyResId, String value) | 69 public static void putString(Context context, int keyResId, String value) |
68 { | 70 { |
69 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit(); | 71 final Editor editor = getDefaultSharedPreferences(context).edit(); |
70 editor.putString(context.getString(keyResId), value).apply(); | 72 editor.putString(context.getString(keyResId), value).apply(); |
71 } | 73 } |
72 | 74 |
73 public static String getString(Context context, int keyResId, String defValue) | 75 public static String getString(Context context, int keyResId, String defValue) |
74 { | 76 { |
75 final SharedPreferences preferences = getDefaultSharedPreferences(context); | 77 final SharedPreferences preferences = getDefaultSharedPreferences(context); |
76 try | 78 try |
77 { | 79 { |
78 return preferences.getString(context.getString(keyResId), defValue); | 80 return preferences.getString(context.getString(keyResId), defValue); |
79 } | 81 } |
80 catch (ClassCastException e) | 82 catch (ClassCastException e) |
81 { | 83 { |
82 return defValue; | 84 return defValue; |
83 } | 85 } |
84 } | 86 } |
85 | 87 |
86 public static void putStringSet(Context context, int keyResId, Set<String> val ues) | 88 public static void putStringSet(Context context, int keyResId, Set<String> val ues) |
87 { | 89 { |
88 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit(); | 90 final Editor editor = getDefaultSharedPreferences(context).edit(); |
89 editor.putStringSet(context.getString(keyResId), values).apply(); | 91 editor.putStringSet(context.getString(keyResId), values).apply(); |
90 } | 92 } |
91 | 93 |
92 public static Set<String> getStringSet(Context context, int keyResId, Set<Stri ng> defValues) | 94 public static Set<String> getStringSet(Context context, int keyResId, Set<Stri ng> defValues) |
93 { | 95 { |
94 final SharedPreferences preferences = getDefaultSharedPreferences(context); | 96 final SharedPreferences preferences = getDefaultSharedPreferences(context); |
95 try | 97 try |
96 { | 98 { |
97 return preferences.getStringSet(context.getString(keyResId), defValues); | 99 return preferences.getStringSet(context.getString(keyResId), defValues); |
98 } | 100 } |
99 catch (ClassCastException e) | 101 catch (ClassCastException e) |
100 { | 102 { |
101 return defValues; | 103 return defValues; |
102 } | 104 } |
103 } | 105 } |
104 | 106 |
105 public static void registerOnSharedPreferenceChangeListener(Context context, | 107 public static void registerOnSharedPreferenceChangeListener(Context context, |
106 OnSharedPreferenceChangeListener listener) | 108 OnSharedPreferenceChangeListener listener) |
107 { | 109 { |
108 getDefaultSharedPreferences(context).registerOnSharedPreferenceChangeListene r( | 110 getDefaultSharedPreferences(context).registerOnSharedPreferenceChangeListene r(listener); |
109 new OnSharedPreferenceChangeListenerWrapper(listener) | |
110 ); | |
111 } | 111 } |
112 | 112 |
113 public static void unregisterOnSharedPreferenceChangeListener(Context context, | 113 public static void unregisterOnSharedPreferenceChangeListener(Context context, |
114 OnSharedPreferenceChangeListener listener) | 114 OnSharedPreferenceChangeListener listener) |
115 { | 115 { |
116 getDefaultSharedPreferences(context).unregisterOnSharedPreferenceChangeListe ner( | 116 getDefaultSharedPreferences(context).unregisterOnSharedPreferenceChangeListe ner(listener); |
117 new OnSharedPreferenceChangeListenerWrapper(listener) | |
118 ); | |
119 } | 117 } |
120 | 118 |
121 private static SharedPreferences getDefaultSharedPreferences(Context context) | 119 private static SharedPreferences getDefaultSharedPreferences(Context context) |
122 { | 120 { |
123 return PreferenceManager.getDefaultSharedPreferences(context.getApplicationC ontext()); | 121 return PreferenceManager.getDefaultSharedPreferences(context.getApplicationC ontext()); |
124 } | 122 } |
125 | 123 |
126 private static class OnSharedPreferenceChangeListenerWrapper | 124 public abstract static class OnSharedPreferenceChangeListener |
127 implements SharedPreferences.OnSharedPreferenceChangeListener | 125 implements SharedPreferences.OnSharedPreferenceChangeListener |
128 { | 126 { |
129 | 127 |
130 private final OnSharedPreferenceChangeListener listener; | 128 @Override |
131 | 129 public final void onSharedPreferenceChanged(SharedPreferences sharedPreferen ces, String key) |
132 OnSharedPreferenceChangeListenerWrapper(OnSharedPreferenceChangeListener lis tener) { | 130 { |
133 this.listener = listener; | 131 this.onSharedPreferenceChanged(key); |
134 } | 132 } |
135 | 133 |
136 @Override | 134 protected abstract void onSharedPreferenceChanged(String key); |
137 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, S tring key) | |
138 { | |
139 this.listener.onSharedPreferenceChanged(key); | |
140 } | |
141 | |
142 @Override | |
143 public boolean equals(Object obj) | |
144 { | |
145 if (this == obj) | |
146 { | |
147 return true; | |
148 } | |
149 else if (obj instanceof OnSharedPreferenceChangeListenerWrapper) | |
150 { | |
151 return this.listener.equals(((OnSharedPreferenceChangeListenerWrapper) o bj).listener); | |
152 } | |
153 return false; | |
154 } | |
155 | |
156 @Override | |
157 public int hashCode() | |
158 { | |
159 return this.listener.hashCode(); | |
160 } | |
161 } | |
162 | |
163 public interface OnSharedPreferenceChangeListener | |
164 { | |
165 void onSharedPreferenceChanged(String key); | |
166 } | 135 } |
167 } | 136 } |
OLD | NEW |