From a45036d4003974f8e78eb819a932895291d677d6 Mon Sep 17 00:00:00 2001 From: 陈奇 <1650699704@qq.com> Date: Tue, 27 Nov 2018 17:14:41 +0800 Subject: [PATCH] [*] --- app/src/main/java/com/moral/yunfushao/utils/SharedPreferencesUtil.java | 384 +++++++++++++++++++++++++++++++----------------------- 1 files changed, 222 insertions(+), 162 deletions(-) diff --git a/app/src/main/java/com/moral/yunfushao/utils/SharedPreferencesUtil.java b/app/src/main/java/com/moral/yunfushao/utils/SharedPreferencesUtil.java index 5f936b3..cb44f12 100644 --- a/app/src/main/java/com/moral/yunfushao/utils/SharedPreferencesUtil.java +++ b/app/src/main/java/com/moral/yunfushao/utils/SharedPreferencesUtil.java @@ -4,190 +4,250 @@ 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 Context mContext; - public final static String SP_LOGIN_USER_KEY = "login_user"; - public final static String SP_BLE = "ble"; - public SharedPreferencesUtil(Context context) { - this.mContext = context; - } + private static Context mContext = null; + public final static String SP_LOGIN_USER_KEY = "login_user"; + public final static String SP_BLE = "ble"; - /** - * ������������������������ - * @param mac + public static SharedPreferencesUtil getUtil(Context context) { + mContext = context; + return Instants.sharedPreferencesUtil; + } + + public static class Instants { + public static SharedPreferencesUtil sharedPreferencesUtil = new SharedPreferencesUtil(); + } + + /** + * ������������������������������������������������ + * + * @return */ - 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 static boolean getLoginModel() { + SharedPreferences preferences = mContext.getSharedPreferences(SP_LOGIN_USER_KEY, Context.MODE_PRIVATE); + return preferences.getBoolean("wx", false); + } - public String getBLE(){ - SharedPreferences preferences = mContext.getSharedPreferences(SP_BLE, Context.MODE_PRIVATE); - String mac = preferences.getString("mac",""); - return mac; - } + /** + * ������������������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 AccessTokenBean getWXToken() { + SharedPreferences preferences = mContext.getSharedPreferences(SP_LOGIN_USER_KEY, Context.MODE_PRIVATE); + String s = preferences.getString("token_wx", null); + if (s == null) { + return null; + } else + return GsonUtil.toObj(s, AccessTokenBean.class); + } + + /** + * ������������������Token + */ + public void removeWXToken() { + SharedPreferences preferences = mContext.getSharedPreferences(SP_LOGIN_USER_KEY, Context.MODE_PRIVATE); + Editor edit = preferences.edit(); + edit.putString("token_wx", null); + } - public void saveLoginInfo(User loginUser){ - 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.commit(); - } + /** + * ������������������������ + * + * @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 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)); - return loginUserInfo; - } + public String getBLE() { + SharedPreferences preferences = mContext.getSharedPreferences(SP_BLE, Context.MODE_PRIVATE); + String mac = preferences.getString("mac", ""); + return mac; + } - public String getUserid(){ - SharedPreferences preferences = mContext.getSharedPreferences(SP_LOGIN_USER_KEY, Context.MODE_PRIVATE); - String userid = preferences.getString("id",""); - return userid; - } + /** + * ������������ + * ������������������������������ + * + * @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.commit(); + } - 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 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)); + return loginUserInfo; + } - 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.commit(); - MainApp.theApp.userId=""; - saveShowAd(""); - } + public String getUserid() { + SharedPreferences preferences = mContext.getSharedPreferences(SP_LOGIN_USER_KEY, Context.MODE_PRIVATE); + String userid = preferences.getString("id", ""); + return userid; + } - /** - * ��������������������������� - * @param flag - */ - public void saveFirstUse(int flag) { - SharedPreferences preferences = mContext.getSharedPreferences("firstInfo", - Context.MODE_PRIVATE); - Editor editor = preferences.edit(); - editor.putInt("firstUse", flag); - editor.commit(); - } - - public int getFirstUse() { - SharedPreferences preferences = mContext.getSharedPreferences("firstInfo", - Context.MODE_PRIVATE); - int firstUse = preferences.getInt("firstUse", 0); - return firstUse; - } + 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.commit(); + MainApp.theApp.userId = ""; + saveShowAd(""); + } + + /** + * ��������������������������� + * + * @param flag + */ + public void saveFirstUse(int flag) { + SharedPreferences preferences = mContext.getSharedPreferences("firstInfo", + Context.MODE_PRIVATE); + Editor editor = preferences.edit(); + editor.putInt("firstUse", flag); + editor.commit(); + } + + public int getFirstUse() { + SharedPreferences preferences = mContext.getSharedPreferences("firstInfo", + Context.MODE_PRIVATE); + int firstUse = preferences.getInt("firstUse", 0); + return firstUse; + } - public void saveShowAd(String time) { - SharedPreferences preferences = mContext.getSharedPreferences("firstInfo", - Context.MODE_PRIVATE); - Editor editor = preferences.edit(); - editor.putString("time", time); - editor.commit(); - } + 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; - } + 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(); - } + /** + * ������ + * + * @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 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 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 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 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; - } + public int getVoiceType() { + SharedPreferences preferences = mContext.getSharedPreferences("sex", + Context.MODE_PRIVATE); + int test = preferences.getInt("sex", 0); + return test; + } } -- Gitblit v1.8.0