陈奇
2018-12-07 0e7dd8919ae2811063057f18a4db4156c771448a
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
package com.moral.yunfushao.utils;
 
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
 
import com.moral.andbrickslib.utils.GsonUtil;
import com.moral.yunfushao.wxapi.data.AccessTokenBean;
import com.moral.yunfushao.MainApp;
import com.moral.yunfushao.model.User;
 
public class SharedPreferencesUtil {
    private static Context mContext = null;
    public final static String SP_LOGIN_USER_KEY = "login_user";
    public final static String SP_BLE = "ble";
 
    public static SharedPreferencesUtil getUtil(Context context) {
        mContext = context;
        return Instants.sharedPreferencesUtil;
    }
 
    public static class Instants {
        public static SharedPreferencesUtil sharedPreferencesUtil = new SharedPreferencesUtil();
    }
 
    /**
     * 获取登陆模式,手机登陆和微信登陆
     *
     * @return
     */
    public static boolean getLoginModel() {
        SharedPreferences preferences = mContext.getSharedPreferences(SP_LOGIN_USER_KEY, Context.MODE_PRIVATE);
        return preferences.getBoolean("wx", false);
    }
 
    /**
     * 保存微信登陆Token
     */
    public void saveWXToken(AccessTokenBean bean) {
        SharedPreferences preferences = mContext.getSharedPreferences(SP_LOGIN_USER_KEY, Context.MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.putString("token_wx", GsonUtil.toStr(bean));
        edit.commit();
    }
 
    /**
     * 保存微信登陆Token
     */
    public void removeWXToken() {
        SharedPreferences preferences = mContext.getSharedPreferences(SP_LOGIN_USER_KEY, Context.MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.putString("token_wx", null);
    }
 
 
    /**
     * 保存蓝牙设备信息
     *
     * @param mac
     */
    public void saveBLE(String mac) {
        SharedPreferences preferences = mContext.getSharedPreferences(SP_BLE, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putString("mac", mac);
        editor.commit();
    }
 
    public String getBLE() {
        SharedPreferences preferences = mContext.getSharedPreferences(SP_BLE, Context.MODE_PRIVATE);
        String mac = preferences.getString("mac", "");
        return mac;
    }
 
    /**
     * 账户登陆
     * 账户登陆清空微信登陆
     *
     * @param loginUser
     */
    public void saveLoginInfo(User loginUser) {
        removeWXToken();
        SharedPreferences preferences = mContext.getSharedPreferences(SP_LOGIN_USER_KEY, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putString("id", loginUser.get_id());
        editor.putString("phone", loginUser.getPhone());
        editor.putString("encypt", loginUser.getEncypt());
        editor.putString("password", loginUser.getPassword());
        editor.putString("nickname", loginUser.getNickname());
        editor.putInt("refresh_frequency", loginUser.getRefresh_frequency());
        editor.putInt("is_open_upload", loginUser.getIs_open_upload());
        editor.putInt("is_lock", loginUser.getIs_lock());
        editor.putInt("video", loginUser.getVideo());
        editor.putInt("login_type", loginUser.getLogin_type());
        editor.commit();
    }
 
    public User getLoginInfo() {
        SharedPreferences sp = mContext.getSharedPreferences(SP_LOGIN_USER_KEY, Context.MODE_PRIVATE);
        User loginUserInfo = new User();
        loginUserInfo.set_id(sp.getString("id", ""));
        loginUserInfo.setEncypt(sp.getString("encypt", ""));
        loginUserInfo.setPhone(sp.getString("phone", ""));
        loginUserInfo.setPassword(sp.getString("password", ""));
        loginUserInfo.setNickname(sp.getString("nickname", ""));
        loginUserInfo.setRefresh_frequency(sp.getInt("refresh_frequency", 10));
        loginUserInfo.setIs_open_upload(sp.getInt("is_open_upload", 1));
        loginUserInfo.setIs_lock(sp.getInt("is_lock", 0));
        loginUserInfo.setVideo(sp.getInt("video", 0));
        loginUserInfo.setLogin_type(sp.getInt("login_type", -1));
        return loginUserInfo;
    }
 
    public String getUserid() {
        SharedPreferences preferences = mContext.getSharedPreferences(SP_LOGIN_USER_KEY, Context.MODE_PRIVATE);
        String userid = preferences.getString("id", "");
        return userid;
    }
 
    public void setUpload(int is_open_upload) {
        SharedPreferences preferences = mContext.getSharedPreferences(SP_LOGIN_USER_KEY, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putInt("is_open_upload", is_open_upload);
        editor.commit();
    }
 
    public void setVideo(int video) {
        SharedPreferences preferences = mContext.getSharedPreferences(SP_LOGIN_USER_KEY, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putInt("video", video);
        editor.commit();
    }
 
    public void setRefreh(int refresh) {
        SharedPreferences preferences = mContext.getSharedPreferences(SP_LOGIN_USER_KEY, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putInt("refresh_frequency", refresh);
        editor.commit();
    }
 
    public void exitLogin() {
        SharedPreferences preferences = mContext.getSharedPreferences(SP_LOGIN_USER_KEY, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putString("id", "");
        editor.putString("phone", "");
        editor.putString("encypt", "");
        editor.putString("password", "");
        editor.putString("nickname", "");
        editor.putInt("refresh_frequency", 0);
        editor.putInt("is_open_upload", 0);
        editor.putInt("is_lock", 0);
        editor.putInt("video", 0);
        editor.putInt("login_type", -1);
        editor.commit();
        MainApp.theApp.userId = "";
        saveShowAd("");
    }
 
    public void saveShowAd(String time) {
        SharedPreferences preferences = mContext.getSharedPreferences("firstInfo",
                Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putString("time", time);
        editor.commit();
    }
 
    public String getShowAd() {
        SharedPreferences preferences = mContext.getSharedPreferences("firstInfo",
                Context.MODE_PRIVATE);
        String time = preferences.getString("time", "");
        return time;
    }
 
    /**
     * 铃声
     *
     * @param video
     */
    public void saveVideoList(String video) {
        SharedPreferences preferences = mContext.getSharedPreferences("videoInfo",
                Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putString("video", video);
        editor.commit();
    }
 
    public String getVideoList() {
        SharedPreferences preferences = mContext.getSharedPreferences("videoInfo",
                Context.MODE_PRIVATE);
        String video = preferences.getString("video", "");
        return video;
    }
 
 
    public void saveTest(String test) {
        SharedPreferences preferences = mContext.getSharedPreferences("test",
                Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putString("test", test);
        editor.commit();
    }
 
    public String getTest() {
        SharedPreferences preferences = mContext.getSharedPreferences("test",
                Context.MODE_PRIVATE);
        String test = preferences.getString("test", "");
        return test;
    }
 
    public void saveVoiceType(int sex) {
        SharedPreferences preferences = mContext.getSharedPreferences("sex",
                Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.putInt("sex", sex);
        editor.commit();
    }
 
    public int getVoiceType() {
        SharedPreferences preferences = mContext.getSharedPreferences("sex",
                Context.MODE_PRIVATE);
        int test = preferences.getInt("sex", 0);
        return test;
    }
}