Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: adblockplussbrowser/src/main/java/org/adblockplus/sbrowser/contentblocker/util/SharedPrefsUtils.java

Issue 29603697: Issue 5931 - Create tests for util package (Closed)
Patch Set: Created Nov. 10, 2017, 2:23 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 package org.adblockplus.sbrowser.contentblocker.util;
19
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 import android.preference.PreferenceManager;
23
24 import java.util.Set;
25
26 public class SharedPrefsUtils
27 {
28
29 public static void putBoolean(Context context, int keyResId, boolean value)
30 {
31 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit();
32 editor.putBoolean(context.getString(keyResId), value).apply();
33 }
34
35 public static boolean getBoolean(Context context, int keyResId, boolean defVal ue)
36 {
37 final SharedPreferences preferences = getDefaultSharedPreferences(context);
38 try
39 {
40 return preferences.getBoolean(context.getString(keyResId), defValue);
41 }
42 catch (ClassCastException e)
43 {
44 return defValue;
45 }
46 }
47
48 public static void putInt(Context context, int keyResId, int value)
49 {
50 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit();
51 editor.putInt(context.getString(keyResId), value).apply();
52 }
53
54 public static int getInt(Context context, int keyResId, int defValue)
55 {
56 final SharedPreferences preferences = getDefaultSharedPreferences(context);
57 try
58 {
59 return preferences.getInt(context.getString(keyResId), defValue);
60 }
61 catch (ClassCastException e)
62 {
63 return defValue;
64 }
65 }
66
67 public static void putString(Context context, int keyResId, String value)
68 {
69 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit();
70 editor.putString(context.getString(keyResId), value).apply();
71 }
72
73 public static String getString(Context context, int keyResId, String defValue)
74 {
75 final SharedPreferences preferences = getDefaultSharedPreferences(context);
76 try
77 {
78 return preferences.getString(context.getString(keyResId), defValue);
79 }
80 catch (ClassCastException e)
81 {
82 return defValue;
83 }
84 }
85
86 public static void putStringSet(Context context, int keyResId, Set<String> val ues)
87 {
88 final SharedPreferences.Editor editor = getDefaultSharedPreferences(context) .edit();
89 editor.putStringSet(context.getString(keyResId), values).apply();
90 }
91
92 public static Set<String> getStringSet(Context context, int keyResId, Set<Stri ng> defValues)
93 {
94 final SharedPreferences preferences = getDefaultSharedPreferences(context);
95 try
96 {
97 return preferences.getStringSet(context.getString(keyResId), defValues);
98 }
99 catch (ClassCastException e)
100 {
101 return defValues;
102 }
103 }
104
105 public static void registerOnSharedPreferenceChangeListener(Context context,
106 OnSharedPreferenceChangeListener listener)
107 {
108 getDefaultSharedPreferences(context).registerOnSharedPreferenceChangeListene r(
109 new OnSharedPreferenceChangeListenerWrapper(listener)
110 );
111 }
112
113 public static void unregisterOnSharedPreferenceChangeListener(Context context,
114 OnSharedPreferenceChangeListener listener)
115 {
116 getDefaultSharedPreferences(context).unregisterOnSharedPreferenceChangeListe ner(
117 new OnSharedPreferenceChangeListenerWrapper(listener)
118 );
119 }
120
121 private static SharedPreferences getDefaultSharedPreferences(Context context)
122 {
123 return PreferenceManager.getDefaultSharedPreferences(context.getApplicationC ontext());
124 }
125
126 private static class OnSharedPreferenceChangeListenerWrapper
127 implements SharedPreferences.OnSharedPreferenceChangeListener
128 {
129
130 private final OnSharedPreferenceChangeListener listener;
131
132 OnSharedPreferenceChangeListenerWrapper(OnSharedPreferenceChangeListener lis tener) {
133 this.listener = listener;
134 }
135
136 @Override
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 }
167 }
OLDNEW

Powered by Google App Engine
This is Rietveld