-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathKV.java
275 lines (222 loc) · 8.41 KB
/
KV.java
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package me.shouheng.utils.store;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.preference.PreferenceManager;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import com.tencent.mmkv.MMKV;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import me.shouheng.utils.Platform;
import me.shouheng.utils.UtilsApp;
import me.shouheng.utils.data.StringUtils;
/**
* An Android Key-value typed data storage utils. This utils allow you to switch
* inner storage between {@link SharedPreferences} and {@link MMKV}.
*
* To learn how to use MMKV, see <a href="https://github.com/Tencent/MMKV">MMKV homepage</a>.
*
* @author Shouheng Wang 2019-05-08 21:30
*/
public final class KV {
@IntDef({Storage.SHARED_PREFERENCES, Storage.MMKV})
@Retention(RetentionPolicy.SOURCE)
public @interface Storage {
int SHARED_PREFERENCES = 1;
int MMKV = 2;
}
private static int DEFAULT_STORAGE_TYPE = Storage.SHARED_PREFERENCES;
private static final Map<String, KV> KV_MAP = new ConcurrentHashMap<>();
private SharedPreferences sp;
/** Set default storage type. */
public static void setDefaultStorageType(@Storage int storage) {
DEFAULT_STORAGE_TYPE = storage;
}
/*-------------------------------------get instance----------------------------------------*/
public static KV get() {
return get("", Context.MODE_PRIVATE, DEFAULT_STORAGE_TYPE);
}
public static KV get(final int mode) {
return get("", mode, DEFAULT_STORAGE_TYPE);
}
public static KV get(String spName) {
return get(spName, Context.MODE_PRIVATE, DEFAULT_STORAGE_TYPE);
}
public static KV of(final @Storage int storage) {
return get("", Context.MODE_PRIVATE, storage);
}
public static KV of(final int mode, final @Storage int storage) {
return get("", mode, storage);
}
public static KV of(String spName, final @Storage int storage) {
return get(spName, Context.MODE_PRIVATE, storage);
}
/** Get an {@link KV} instance from {@link #KV_MAP} by spName and storage type. */
public static KV get(String spName, final int mode, final @Storage int storage) {
if (StringUtils.isSpace(spName)) {
spName = getDefaultSharedPreferencesName();
}
String key = getMapKeyName(spName, storage);
KV kv = KV_MAP.get(key);
if (kv == null) {
kv = new KV(spName, mode, storage);
KV_MAP.put(key, kv);
}
return kv;
}
/*-------------------------------------instance methods----------------------------------------*/
public void put(@NonNull final String key, final String value) {
put(key, value, false);
}
public void put(@NonNull final String key, final String value, final boolean isCommit) {
if (isCommit) {
sp.edit().putString(key, value).commit();
} else {
sp.edit().putString(key, value).apply();
}
}
public String getString(@NonNull final String key) {
return getString(key, "");
}
public String getString(@NonNull final String key, final String defaultValue) {
return sp.getString(key, defaultValue);
}
public void put(@NonNull final String key, final int value) {
put(key, value, false);
}
public void put(@NonNull final String key, final int value, final boolean isCommit) {
if (isCommit) {
sp.edit().putInt(key, value).commit();
} else {
sp.edit().putInt(key, value).apply();
}
}
public int getInt(@NonNull final String key) {
return getInt(key, -1);
}
public int getInt(@NonNull final String key, final int defaultValue) {
return sp.getInt(key, defaultValue);
}
public void put(@NonNull final String key, final long value) {
put(key, value, false);
}
public void put(@NonNull final String key, final long value, final boolean isCommit) {
if (isCommit) {
sp.edit().putLong(key, value).commit();
} else {
sp.edit().putLong(key, value).apply();
}
}
public long getLong(@NonNull final String key) {
return getLong(key, -1L);
}
public long getLong(@NonNull final String key, final long defaultValue) {
return sp.getLong(key, defaultValue);
}
public void put(@NonNull final String key, final float value) {
put(key, value, false);
}
public void put(@NonNull final String key, final float value, final boolean isCommit) {
if (isCommit) {
sp.edit().putFloat(key, value).commit();
} else {
sp.edit().putFloat(key, value).apply();
}
}
public float getFloat(@NonNull final String key) {
return getFloat(key, -1f);
}
public float getFloat(@NonNull final String key, final float defaultValue) {
return sp.getFloat(key, defaultValue);
}
public void put(@NonNull final String key, final boolean value) {
put(key, value, false);
}
public void put(@NonNull final String key, final boolean value, final boolean isCommit) {
if (isCommit) {
sp.edit().putBoolean(key, value).commit();
} else {
sp.edit().putBoolean(key, value).apply();
}
}
public boolean getBoolean(@NonNull final String key) {
return getBoolean(key, false);
}
public boolean getBoolean(@NonNull final String key, final boolean defaultValue) {
return sp.getBoolean(key, defaultValue);
}
public void put(@NonNull final String key, final Set<String> value) {
put(key, value, false);
}
public void put(@NonNull final String key, final Set<String> value, final boolean isCommit) {
if (isCommit) {
sp.edit().putStringSet(key, value).commit();
} else {
sp.edit().putStringSet(key, value).apply();
}
}
public Set<String> getStringSet(@NonNull final String key) {
return getStringSet(key, Collections.<String>emptySet());
}
public Set<String> getStringSet(@NonNull final String key, final Set<String> defaultValue) {
return sp.getStringSet(key, defaultValue);
}
public Map<String, ?> getAll() {
return sp.getAll();
}
public boolean contains(@NonNull final String key) {
return sp.contains(key);
}
public void remove(@NonNull final String key) {
remove(key, false);
}
public void remove(@NonNull final String key, final boolean isCommit) {
if (isCommit) {
sp.edit().remove(key).commit();
} else {
sp.edit().remove(key).apply();
}
}
public void clear() {
clear(false);
}
public void clear(final boolean isCommit) {
if (isCommit) {
sp.edit().clear().commit();
} else {
sp.edit().clear().apply();
}
}
public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
sp.registerOnSharedPreferenceChangeListener(listener);
}
public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
sp.unregisterOnSharedPreferenceChangeListener(listener);
}
/*-------------------------------------inner methods----------------------------------------*/
private KV(final String spName, final int mode, final @Storage int storage) {
if (Platform.DEPENDENCY_MMKV_ANALYTICS && storage == Storage.MMKV) {
sp = MMKV.mmkvWithID(spName, mode);
} else {
sp = UtilsApp.getApp().getSharedPreferences(spName, mode);
}
}
private static String getDefaultSharedPreferencesName() {
if (VERSION.SDK_INT >= VERSION_CODES.N) {
return PreferenceManager.getDefaultSharedPreferencesName(UtilsApp.getApp());
} else {
return UtilsApp.getApp().getPackageName() + "_preferences";
}
}
/** Get key for {@link #KV_MAP}. */
private static String getMapKeyName(String spName, final @Storage int storage) {
return spName + "_" + storage;
}
}