JLChen
2020-09-24 e91af284643d5e370b0d18c384fe8de65f59d9b3
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
package com.hdl.sdk.hdl_core.Util.SPUtil;
 
import android.content.Context;
import android.content.SharedPreferences;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.ZigbeeBean.ZigbeeDeviceSaveBean;
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.ZigbeeBean.ZigbeeSceneSaveBean;
import com.hdl.sdk.hdl_core.Util.TransformUtil.JsonUtil;
 
import java.lang.reflect.Type;
import java.util.List;
 
/**
 * Created by Tommy on 2018/1/19.
 * <p>
 * <p>
 * 保存
 * SPUtils.setParam(this, "String", "xiaanming");
 * SPUtils.setParam(this, "int", 10);
 * SPUtils.setParam(this, "boolean", true);
 * SPUtils.setParam(this, "long", 100L);
 * SPUtils.setParam(this, "float", 1.1f);
 * <p>
 * 获取
 * SPUtils.getParam(this, "String", "");                                                                                        SPUtils.getParam(TimerActivity.this, "int", 0);
 * SPUtils.getParam(this, "boolean", false);
 * SPUtils.getParam(this, "long", 0L);
 * SPUtils.getParam(this, "float", 0.0f);
 */
 
public class SPUtils {
    /**
     * 保存在手机里面的文件名
     */
    private static final String FILE_NAME = "share_rcu";
    public static final String KEY_HDL_RCU_IP = "hdlrcuip";
    public static final String KEY_IS_HDL_RCU = "ishdlrcu";
    public static final String KEY_LOCAL_REMARK = "localremark";
    public static final String DEFAULT_REMARK = "默认设备";
 
    public static final String KEY_ZIGBEE_SCENE_IP = "zigbeesceneip";
    public static final String KEY_ZIGBEE_IP = "zigbeeip";
    public static final String KEY_RCU_IP_ = "rcuip";
    public static final String KEY_SUB_ID_ = "subid";
    public static final String KEY_DEVICE_ID = "deviceid";
 
    public static final String KEY_DEVICE_DATA_LIST = "hdldevicedatalist";
 
    public static final String KEY_LOCAL_BIG_CLASS_ = "local_big_class";
    public static final String KEY_LOCAL_Small_CLASS_ = "local_Small_class";
    public static final int DEFAULT_SUB_ID = 254;
    public static final int DEFAULT_DEVICE_ID = 80;
 
    /**
     * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
     *
     * @param context
     * @param key
     * @param object
     */
    public static void setParam(Context context, String key, Object object) {
 
        String type = object.getClass().getSimpleName();
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
 
        if ("String".equals(type)) {
            editor.putString(key, (String) object);
        } else if ("Integer".equals(type)) {
            editor.putInt(key, (Integer) object);
        } else if ("Boolean".equals(type)) {
            editor.putBoolean(key, (Boolean) object);
        } else if ("Float".equals(type)) {
            editor.putFloat(key, (Float) object);
        } else if ("Long".equals(type)) {
            editor.putLong(key, (Long) object);
        }
        editor.commit();
    }
 
 
    /**
     * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
     *
     * @param context
     * @param key
     * @param defaultObject
     * @return
     */
    public static Object getParam(Context context, String key, Object defaultObject) {
        String type = defaultObject.getClass().getSimpleName();
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
 
        if ("String".equals(type)) {
            return sp.getString(key, (String) defaultObject);
        } else if ("Integer".equals(type)) {
            return sp.getInt(key, (Integer) defaultObject);
        } else if ("Boolean".equals(type)) {
            return sp.getBoolean(key, (Boolean) defaultObject);
        } else if ("Float".equals(type)) {
            return sp.getFloat(key, (Float) defaultObject);
        } else if ("Long".equals(type)) {
            return sp.getLong(key, (Long) defaultObject);
        }
        return null;
    }
 
    /**
     * 存储设备数据对象
     * @param ctx
     * @param key
     * @param bookList
     */
    public static void setZigbeeDeviceSaveBean(Context ctx,String key, List<ZigbeeDeviceSaveBean> bookList) {
        SharedPreferences sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        String json = JsonUtil.toJson(bookList);
        editor.putString(key, json);
        editor.commit();
    }
    public static List<ZigbeeDeviceSaveBean> getZigbeeDeviceSaveBean(Context ctx,String key) {
        SharedPreferences sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sp.getString(key, null);
        Type type = new TypeToken<List<ZigbeeDeviceSaveBean>>() {
        }.getType();
        List<ZigbeeDeviceSaveBean> arrayList = gson.fromJson(json, type);
        return arrayList;
    }
 
    /**
     * 存储场景数据对象
     * @param ctx
     * @param key
     * @param bookList
     */
    public static void setZigbeeSceneSaveBean(Context ctx,String key, List<ZigbeeSceneSaveBean> bookList) {
        SharedPreferences sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        String json = JsonUtil.toJson(bookList);
        editor.putString(key, json);
        editor.commit();
    }
    public static List<ZigbeeSceneSaveBean> getZigbeeSceneSaveBean(Context ctx,String key) {
        SharedPreferences sp = ctx.getSharedPreferences("config", Context.MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sp.getString(key, null);
        Type type = new TypeToken<List<ZigbeeSceneSaveBean>>() {
        }.getType();
        List<ZigbeeSceneSaveBean> arrayList = gson.fromJson(json, type);
        return arrayList;
    }
 
}