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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 public class SharedPrefsUtils | 26 public class SharedPrefsUtils |
27 { | 27 { |
28 | 28 |
29 public static void putBoolean(Context context, int keyResId, boolean value) | 29 public static void putBoolean(Context context, int keyResId, boolean value) |
anton
2017/05/29 05:33:04
general suggest for all similar methods.
I's assu
diegocarloslima
2017/05/31 14:32:09
Well for instance, the goal was to standardize the
| |
30 { | 30 { |
31 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit(); | 31 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit(); |
32 editor.putBoolean(context.getString(keyResId), value).apply(); | 32 editor.putBoolean(context.getString(keyResId), value).apply(); |
33 } | 33 } |
34 | 34 |
35 public static boolean getBoolean(Context context, int keyResId, boolean defVal ue) | 35 public static boolean getBoolean(Context context, int keyResId, boolean defVal ue) |
36 { | 36 { |
37 final SharedPreferences preferences = getDefaultSharedPreferences(context); | 37 final SharedPreferences preferences = getDefaultSharedPreferences(context); |
38 try | 38 try |
39 { | 39 { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 { | 107 { |
108 getDefaultSharedPreferences(context).registerOnSharedPreferenceChangeListene r( | 108 getDefaultSharedPreferences(context).registerOnSharedPreferenceChangeListene r( |
109 new OnSharedPreferenceChangeListenerWrapper(listener) | 109 new OnSharedPreferenceChangeListenerWrapper(listener) |
110 ); | 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( |
117 new OnSharedPreferenceChangeListenerWrapper(listener) | 117 new OnSharedPreferenceChangeListenerWrapper(listener) |
anton
2017/05/29 05:33:04
Creating new wrapper while unregistering looks str
diegocarloslima
2017/05/31 14:32:09
Really nice catch! I totally overlooked this. I cr
| |
118 ); | 118 ); |
119 } | 119 } |
120 | 120 |
121 private static SharedPreferences getDefaultSharedPreferences(Context context) | 121 private static SharedPreferences getDefaultSharedPreferences(Context context) |
122 { | 122 { |
123 return PreferenceManager.getDefaultSharedPreferences(context.getApplicationC ontext()); | 123 return PreferenceManager.getDefaultSharedPreferences(context.getApplicationC ontext()); |
124 } | 124 } |
125 | 125 |
126 private static class OnSharedPreferenceChangeListenerWrapper | 126 private static class OnSharedPreferenceChangeListenerWrapper |
127 implements SharedPreferences.OnSharedPreferenceChangeListener | 127 implements SharedPreferences.OnSharedPreferenceChangeListener |
128 { | 128 { |
129 | 129 |
130 private final OnSharedPreferenceChangeListener listener; | 130 private final OnSharedPreferenceChangeListener listener; |
131 | 131 |
132 OnSharedPreferenceChangeListenerWrapper(OnSharedPreferenceChangeListener lis tener) { | 132 OnSharedPreferenceChangeListenerWrapper(OnSharedPreferenceChangeListener lis tener) { |
133 this.listener = listener; | 133 this.listener = listener; |
134 } | 134 } |
135 | 135 |
136 @Override | 136 @Override |
137 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, S tring key) | 137 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, S tring key) |
138 { | 138 { |
139 this.listener.onSharedPreferenceChanged(key); | 139 this.listener.onSharedPreferenceChanged(key); |
140 } | 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 } | |
141 } | 161 } |
142 | 162 |
143 public interface OnSharedPreferenceChangeListener | 163 public interface OnSharedPreferenceChangeListener |
144 { | 164 { |
145 void onSharedPreferenceChanged(String key); | 165 void onSharedPreferenceChanged(String key); |
146 } | 166 } |
147 } | 167 } |
LEFT | RIGHT |