wxr
2022-11-23 1e7b3abd15d37f6c6bc97ac14922457b9604c275
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package com.mm.android.deviceaddmodule.mobilecommon.utils;
 
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Base64;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
public class PreferencesHelper {
    private final static String PRENAME = "dh_data";
 
    private static Context mContext            = null;
    private SharedPreferences mConfig                = null;
 
    private PreferencesHelper()
    {
    }
 
    private static class Instance{
        private static PreferencesHelper ph=new PreferencesHelper();
    }
 
    public static PreferencesHelper getInstance(Context context){
        mContext = context.getApplicationContext();
        return Instance.ph;
    }
 
    /**
     * 若不存在Key,则返回 空字符串
     * @param Key
     * @return
     */
    public String getString(String Key){
        SharedPreferences config = getConfig();
        String result = config.getString(Key, "");
        return result;
    }
 
    /**
     * 若不存在Key,则返回 默认值
     *
     * @param Key
     * @return
     */
    public String getString(String Key, String defValue) {
        SharedPreferences config = getConfig();
        String result = config.getString(Key, defValue);
        return result;
    }
 
    /**
     * 若不存在Key,则返回0
     * @param Key
     * @return
     */
    public int getInt(String Key){
        SharedPreferences config = getConfig();
        int result = config.getInt(Key, 0);
        return result;
    }
 
    /**
     * 若不存在Key,则返回 默认值
     *
     * @param Key
     * @return
     */
    public int getInt(String Key, int defValue) {
        SharedPreferences config = getConfig();
        int result = config.getInt(Key, defValue);
        return result;
    }
 
    /**
     * 若不存在Key,则返回 false
     * @param Key
     * @return
     */
    public boolean getBoolean(String Key){
        SharedPreferences config = getConfig();
        boolean result = config.getBoolean(Key, false);
        return result;
    }
 
    /**
     * 若不存在Key,则返回 默认值
     *
     * @param Key
     * @return
     */
    public boolean getBoolean(String Key, boolean defValue) {
        SharedPreferences config = getConfig();
        boolean result = config.getBoolean(Key, defValue);
        return result;
    }
 
    /**
     * 若不存在Key,则返回 false
     * @param Key
     * @return
     */
    public float getFloat(String Key){
        SharedPreferences config = getConfig();
        float result = config.getFloat(Key, 0);
        return result;
    }
 
    /**
     * 若不存在Key,则返回 默认值
     *
     * @param Key
     * @return
     */
    public float getFloat(String Key, float defValue) {
        SharedPreferences config = getConfig();
        float result = config.getFloat(Key, defValue);
        return result;
    }
 
    /**
     * 新消息推送模块,专业默认返回true的
     * <p>
     * </p>
     * @param Key
     * @return
     */
    public boolean getMessagePushBoolean(String Key) {
        SharedPreferences config = getConfig();
        boolean result = config.getBoolean(Key, true);
        return result;
    }
 
    /**
     * 若不存在Key,则返回 0
     * @param Key
     * @return
     */
    public long getLong(String Key){
        SharedPreferences config = getConfig();
        long result = config.getLong(Key, 0);
        return result;
    }
 
    public long getLong(String Key, long defValue){
        SharedPreferences config = getConfig();
        long result = config.getLong(Key, defValue);
        return result;
    }
 
    public void set(String key, String value){
        SharedPreferences config = getConfig();
        SharedPreferences.Editor editor = config.edit();
        editor.putString(key, value);
        editor.commit();
    }
 
    public void set(String key, int value){
        SharedPreferences config = getConfig();
        SharedPreferences.Editor editor = config.edit();
        editor.putInt(key, value);
        editor.commit();
    }
 
    public void set(String key, boolean value){
        SharedPreferences config = getConfig();
        SharedPreferences.Editor editor = config.edit();
        editor.putBoolean(key, value);
        editor.commit();
        editor.apply();
    }
 
    public void set(String key, long value){
        SharedPreferences config = getConfig();
        SharedPreferences.Editor editor = config.edit();
        editor.putLong(key, value);
        editor.commit();
    }
 
    public void set(String key, float value){
        SharedPreferences config = getConfig();
        SharedPreferences.Editor editor = config.edit();
        editor.putFloat(key, value);
        editor.commit();
    }
 
    public void set(String key, List<String> values) {
        if (values == null) {
            return;
        }
 
        Set<String> set = new HashSet<>(values);
        SharedPreferences config = getConfig();
        SharedPreferences.Editor editor = config.edit();
        editor.putStringSet(key, set);
        editor.commit();
    }
 
    public List<String> getStrings(String key) {
        List<String> list = new ArrayList<>();
 
        if (!contains(key)) {
            return list;
        }
 
        SharedPreferences config = getConfig();
        Set<String> set = config.getStringSet(key, new HashSet<String>());
        list.addAll(set);
        return list;
    }
 
    /**
     * 是否存在key值
     * @param key
     * @return
     */
    public boolean contains(String key){
        SharedPreferences config = getConfig();
        return config.contains(key);
    }
 
    private SharedPreferences getConfig()
    {
        if(mConfig == null)
        {
            mConfig = mContext.getSharedPreferences(PRENAME, Context.MODE_PRIVATE);
        }
        return mConfig;
    }
 
 
    public void setData(String tempName, List<?> tempList) {
        SharedPreferences config = getConfig();
        // 创建字节输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            // 创建对象输出流,并封装字节流
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            // 将对象写入字节流
            oos.writeObject(tempList);
            // 将字节流编码成base64的字符串
            String tempBase64 = new String(Base64.encode(baos.toByteArray(),Base64.DEFAULT));
            SharedPreferences.Editor editor = config.edit();
            editor.putString(tempName, tempBase64);
            editor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }
    }
 
 
    public List<?> getData(String tempName, List<?> tempList) {
        SharedPreferences config = getConfig();
        String tempBase64 = config.getString(tempName, "");// 初值空
        if (TextUtils.isEmpty(tempBase64)) {
            return tempList;
        }
        // 读取字节
        byte[] base64 = Base64.decode(tempBase64,Base64.DEFAULT);
        // 封装到字节流
        ByteArrayInputStream bais = new ByteArrayInputStream(base64);
        try {
            // 再次封装
            ObjectInputStream ois = new ObjectInputStream(bais);
            // 读取对象
            tempList = (List<?>) ois.readObject();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return tempList;
 
    }
 
    /**
     * 对象存储
     */
    public void setObject(String key, Object temp) {
        SharedPreferences config = getConfig();
        // 创建字节输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            // 创建对象输出流,并封装字节流
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            // 将对象写入字节流
            oos.writeObject(temp);
            // 将字节流编码成base64的字符串
            String tempBase64 = new String(Base64Help.encode(baos.toByteArray()));
            SharedPreferences.Editor editor = config.edit();
            editor.putString(key, tempBase64);
            editor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }
    }
 
    public Object getObject(String key, Object defaultValue) {
        SharedPreferences config = getConfig();
        String tempBase64 = config.getString(key, "");// 初值空
        if (TextUtils.isEmpty(tempBase64)) {
            return defaultValue;
        }
        // 读取字节
        byte[] base64 = Base64Help.decode(tempBase64);
        // 封装到字节流
        ByteArrayInputStream bais = new ByteArrayInputStream(base64);
        try {
            // 再次封装
            ObjectInputStream ois = new ObjectInputStream(bais);
            // 读取对象
            defaultValue = ois.readObject();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return defaultValue;
 
    }
 
    // key value
 
    public static void clearKeyValueCache(Context context,String totalKey) {
        PreferencesHelper preferencesHelper = PreferencesHelper.getInstance(context);
        ArrayList<String> urlKeyList = (ArrayList<String>)
                preferencesHelper.getData(totalKey, new ArrayList<String>());
        for (String urlKey : urlKeyList) {
            preferencesHelper.set(urlKey, "");
        }
        preferencesHelper.setData(totalKey, new ArrayList<String>());
    }
 
    public static void clearKeyValueCacheBySnCode(Context context,String totalKey,String key) {
        PreferencesHelper preferencesHelper = PreferencesHelper.getInstance(context);
        ArrayList<String> urlKeyList = (ArrayList<String>)
                preferencesHelper.getData(totalKey, new ArrayList<String>());
        ArrayList<String> newList = new ArrayList<>();
        for (String urlKey : urlKeyList) {
            if(urlKey!=null && urlKey.contains(key)){
                preferencesHelper.set(urlKey, "");
            }else{
                newList.add(urlKey);
            }
        }
        preferencesHelper.setData(totalKey, newList);
    }
    public static void saveKeyValueToCache(Context context,String totalKey, String singleKey,String value) {
        PreferencesHelper preferencesHelper = PreferencesHelper.getInstance(context);
        preferencesHelper.set(singleKey ,value);
 
        ArrayList<String> urlKeyList = (ArrayList<String>) preferencesHelper.getData(totalKey, new ArrayList<>());
        if(!urlKeyList.contains(singleKey)){
            urlKeyList.add(singleKey);
        }
        preferencesHelper.setData(totalKey ,urlKeyList);
    }
 
    public static void removeKey(Context context,String key) {
        PreferencesHelper preferencesHelper = PreferencesHelper.getInstance(context);
        SharedPreferences.Editor editor = preferencesHelper.getConfig().edit();
        editor.remove(key);
        editor.commit();
    }
 
}