Files
yusheng-android/moduleUtil/src/main/java/com/xscm/moduleutil/utils/SpUtil.java

571 lines
19 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.xscm.moduleutil.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.SPUtils;
import com.xscm.moduleutil.base.CommonAppContext;
import com.xscm.moduleutil.bean.UserBean;
import com.xscm.moduleutil.bean.UserInfo;
import java.util.Map;
/**
* Created by cxf on 2018/9/17.
* SharedPreferences 封装
*/
public class SpUtil {
private static SpUtil sInstance;
private static SharedPreferences mSharedPreferences;
public static final String UID = "uid";
public static final String TOKEN = "token";
public static final String USER_INFO = "userInfo";
public static final String CONFIG = "config";
public static final String SYSTEM_MSG_COUNT = "systemMsgCount";
public static final String LOCATION_LNG = "locationLng";
public static final String LOCATION_LAT = "locationLat";
public static final String LOCATION_PROVINCE = "locationProvince";
public static final String LOCATION_CITY = "locationCity";
public static final String LOCATION_DISTRICT = "locationDistrict";
public static final String MH_BEAUTY_ENABLE = "mhBeautyEnable";
public static final String BRIGHTNESS = "brightness";//亮度
public static final String IM_MSG_RING = "imMsgRing";//私信音效
public static final String DEVICE_ID = "deviceId";
public static final String TEENAGER = "teenager";
public static final String TEENAGER_SHOW = "teenagerShow";
public static final String TX_IM_USER_SIGN = "txImUserSign";//腾讯IM用户签名登录腾讯IM用
public static final String TPNS_NOTIFY_EXT = "tpnsNotifyExt";//tpns通知携带参数
public static final String BASE_FUNCTION_MODE = "baseFunctionMode";//基本功能模式
public static final String LANGUAGE = "language";
private SpUtil() {
mSharedPreferences = CommonAppContext.getInstance().getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE);
}
public static SpUtil getInstance() {
if (sInstance == null) {
synchronized (SpUtil.class) {
if (sInstance == null) {
sInstance = new SpUtil();
}
}
}
return sInstance;
}
public int getIntValue(String key, int defaultValue) {
return mSharedPreferences.getInt(key, defaultValue);
}
public void setIntValue(String key, int value) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putInt(key, value);
editor.apply();
}
/**
* 保存一个字符串
*/
public void setStringValue(String key, String value) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString(key, value);
editor.apply();
}
/**
* 获取一个字符串
*/
public String getStringValue(String key) {
return mSharedPreferences.getString(key, "");
}
/**
* 保存多个字符串
*/
public void setMultiStringValue(Map<String, String> pairs) {
if (pairs == null || pairs.size() == 0) {
return;
}
SharedPreferences.Editor editor = mSharedPreferences.edit();
for (Map.Entry<String, String> entry : pairs.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
editor.putString(key, value);
}
}
editor.apply();
}
/**
* 获取多个字符串
*/
public String[] getMultiStringValue(String... keys) {
if (keys == null || keys.length == 0) {
return null;
}
int length = keys.length;
String[] result = new String[length];
for (int i = 0; i < length; i++) {
String temp = "";
if (!TextUtils.isEmpty(keys[i])) {
temp = mSharedPreferences.getString(keys[i], "");
}
result[i] = temp;
}
return result;
}
/**
* 保存一个布尔值
*/
public static void setBooleanValue(String key, boolean value) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(key, value);
editor.apply();
}
/**
* 获取一个布尔值
*/
public boolean getBooleanValue(String key) {
return mSharedPreferences.getBoolean(key, false);
}
/**
* 获取一个布尔值
*/
public static boolean getBooleanValue(String key, boolean defaultValue) {
return mSharedPreferences.getBoolean(key, defaultValue);
}
/**
* 保存多个布尔值
*/
public void setMultiBooleanValue(Map<String, Boolean> pairs) {
if (pairs == null || pairs.size() == 0) {
return;
}
SharedPreferences.Editor editor = mSharedPreferences.edit();
for (Map.Entry<String, Boolean> entry : pairs.entrySet()) {
String key = entry.getKey();
Boolean value = entry.getValue();
if (!TextUtils.isEmpty(key)) {
editor.putBoolean(key, value);
}
}
editor.apply();
}
/**
* 获取多个布尔值
*/
public boolean[] getMultiBooleanValue(String[] keys) {
if (keys == null || keys.length == 0) {
return null;
}
int length = keys.length;
boolean[] result = new boolean[length];
for (int i = 0; i < length; i++) {
boolean temp = false;
if (!TextUtils.isEmpty(keys[i])) {
temp = mSharedPreferences.getBoolean(keys[i], false);
}
result[i] = temp;
}
return result;
}
public void removeValue(String... keys) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
for (String key : keys) {
editor.remove(key);
}
editor.apply();
}
/**
* 亮度
*/
public void setBrightness(float brightness) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putFloat(BRIGHTNESS, brightness);
editor.apply();
}
/**
* 亮度
*/
public float getBrightness() {
return mSharedPreferences.getFloat(BRIGHTNESS, -1.0f);
}
/**
* 私信音效
*/
public void setImMsgRingOpen(boolean imMsgRingOpen) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putBoolean(IM_MSG_RING, imMsgRingOpen);
editor.apply();
}
/**
* 私信音效
*/
public boolean isImMsgRingOpen() {
return mSharedPreferences.getBoolean(IM_MSG_RING, true);
}
public static String getToken() {
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString(SPConstants.TOKEN);
}
public static void setBoolean(String key, boolean value) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(key, value, true);
}
public static boolean getBoolean(String key, boolean defaultValue) {
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getBoolean(key, defaultValue);
}
public static int getUserId() {
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getInt(SPConstants.USER_ID);
}
public static void saveUserId(int userId) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(SPConstants.USER_ID, userId, true);
}
public static void setMusicVolume(int musicVolume) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("musicVolume", musicVolume, true);
}
public static void setUnderagePassword(String password){
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("underagePassword", password, true);
}
public static String getUnderagePassword(){
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString("underagePassword");
}
public static Integer getMusicVolume() {
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getInt("musicVolume", 50);
}
public static void settPlayoutVolume(int tPlayoutVolume){
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("tPlayoutVolume", tPlayoutVolume, true);
}
public static Integer gettPlayoutVolume(){
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getInt("tPlayoutVolume", 50);
}
public static String getMyRoomId() {
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString("MyRoomId");
}
public static void saveUserInfo(UserInfo userInfo) {
String s = JSON.toJSONString(userInfo);
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(SPConstants.USER_INFO, s, true);
}
public static void saveUserBean(UserBean userBean) {
String s = JSON.toJSONString(userBean);
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(SPConstants.USER_BEAN, s, true);
}
public static void saveMyRoomId(String roomId) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("MyRoomId", roomId, true);
}
public static void saveSearchHistory(String searchHistory) {
SPUtils.getInstance(SPConstants.PREFERENCE_SEARCH_HISTORY).put(SPConstants.SEARCH_HISTORY, searchHistory);
}
public static String getSearchHistory() {
return SPUtils.getInstance(SPConstants.PREFERENCE_SEARCH_HISTORY).getString(SPConstants.SEARCH_HISTORY);
}
public static void putToken(String token) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(SPConstants.TOKEN, token, true);
}
public static void saveTzbl(String roomId, float bl) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(getUserId() + "-" + roomId + "_Tzbl", bl, true);
}
public static float getTzbl(String roomId) {
float bl = SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getFloat(getUserId() + "-" + roomId + "_Tzbl");
if (bl < 0.0f) {
if (roomId.equals(getMyRoomId())) {
return 0.8f;
} else {
return 0.0f;
}
} else {
return bl;
}
}
public static void saveEmqttId(String clientId) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(SPConstants.EMQTT_CLIENT_ID, clientId);
}
public static void setShelf(int shelf){
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(SPConstants.SHELF, shelf);
}
public static int getShelf(){
int shelf=SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getInt(SPConstants.SHELF);
return shelf;
}
public static String getEmqttId() {
String s = SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString(SPConstants.EMQTT_CLIENT_ID);
return s;
}
//获取SharedPreferences音乐轮播方式
public static int getPlayPattern() {
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getInt(SPConstants.PLAY_MODE, 1);
}
//通过SharedPreferences设置音乐轮播方式
public static void setPlayPattern(int i) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(SPConstants.PLAY_MODE, i);
}
//获取SharedPreferences音乐播放音量
public static int getChannelVolume() {
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getInt(SPConstants.VOLUME, 20);
}
//设置SharedPreferences音乐播放音量
public static void setChannelVolume(int i) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(SPConstants.VOLUME, i);
}
//获取播放的音乐
public static int getPlayCurrentMusic() {
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getInt(SPConstants.CURRENT_MUSIC, 0);
}
//设置播放的音乐
public static void setPlayCurrentMusic(int i) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(SPConstants.CURRENT_MUSIC, i);
}
//设置开启特效
public static void setOpenEffect(int i) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(SPConstants.OPEN_EFFECT, i);
}
//获取开启特效
public static int getOpenEffect() {
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getInt(SPConstants.OPEN_EFFECT, 1);
}
public static void setFloatingScreen(int i){
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(SPConstants.FLOATING_SCREEN, i);
}
public static int getFloatingScreen(){
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getInt(SPConstants.FLOATING_SCREEN, 1);
}
//设置耳返
public static void setAuricularBack(int i) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(SPConstants.OPEN_AU_BACK, i);
}
//获取耳返
public static int getAuricularBack() {
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getInt(SPConstants.OPEN_AU_BACK, 0);
}
//获取新的消息数量
public static int getOrderNewCounts() {
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getInt(SPConstants.ORDER_NEWS_COUNT, 0);
}
//设置新的消息数量
public static void setOrderNewCounts(int i) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(SPConstants.ORDER_NEWS_COUNT, i);
}
//获取装载的消息
public static String getLastOrderMsg() {
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString(SPConstants.ORDER_LAST_MSG, " ");
}
//设置装载的消息
public static void setLastOrderMsg(String s) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put(SPConstants.ORDER_LAST_MSG, s);
}
//新的token
public static void putNewToken(String token) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("newToken", token, true);
}
//新的token
public static String getNewToken() {
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString("newToken", "");
}
public static void setAgreeSkillProtocol() {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("agreeSkillProtocol", true, true);
}
public static boolean getAgreeSkillProtocol() {
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getBoolean("agreeSkillProtocol", false);
}
public static void setInReview(boolean inReview) {
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("inReview", inReview, true);
}
public static boolean inReview() {
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getBoolean("inReview", false);
}
public static void setHomeBanner(String banner){
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("homeBanner", banner, true);
}
public static String getHomeBanner(){
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString("homeBanner", "");
}
public static void setMusicName(String name){
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("musicName", name, true);
}
public static String getMusicName(){
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString("musicName", "");
}
public static void setTopRoom(String topRoom){
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("topRoom", topRoom, true);
}
public static void setUserOnline(String online){
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("userOnline", online, true);
}
public static String getUserOnline(){
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString("userOnline", "");
}
public static void setTopRoomTop(String topRoom){
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("topRoomTop", topRoom, true);
}
public static String getTopRoom(){
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString("topRoom", "");
}
public static String getTopRoomTop(){
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString("topRoomTop", "");
}
public static void setRoomTypeModel(String roomTypeModel){
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("roomTypeModel", roomTypeModel, true);
}
public static String getRoomTypeModel(){
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString("roomTypeModel", "");
}
public static void setRoomInfoResp(String roomInfoResp){
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("RoomInfoResp", roomInfoResp, true);
}
public static String getRoomInfoResp(){
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString("RoomInfoResp", "");
}
public static void setHomeBean(String homeBean){
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("homeBean", homeBean, true);
}
public static String getHomeBean(){
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString("homeBean", "");
}
public static void setRoomModel(String roomModel){
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("roomModel", roomModel, true);
}
public static String getRoomModel(){
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString("roomModel", "");
}
public static void setRtmToken(String rtm_token){
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("rtm_token", rtm_token, true);
}
public static String getRtmToken(){
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString("rtm_token", "");
}
public static void setAuctionId(String auctionId){
SPUtils.getInstance(SPConstants.PREFERENCE_NAME).put("auctionId", auctionId, true);
}
public static String getauctionId(){
return SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString("auctionId", "");
}
// TODO: 2025/5/16 设置实名认证
public static void setRealName(boolean realName){
SPUtils.getInstance(SPConstants.REAL_NAME).put("realName", realName, true);
}
// TODO: 2025/5/16 是否实名认证
public static boolean getRealName(){
return SPUtils.getInstance(SPConstants.REAL_NAME).getBoolean("realName", false);
}
public static UserBean getUserBean() {
String s = SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString(SPConstants.USER_BEAN);
if (TextUtils.isEmpty(s)) {
return new UserBean();
}
UserBean userBean = JSON.parseObject(s, UserBean.class);
if (userBean == null) {
return new UserBean();
}
return userBean;
}
public static UserInfo getUserInfo() {
String s = SPUtils.getInstance(SPConstants.PREFERENCE_NAME).getString(SPConstants.USER_INFO);
if (TextUtils.isEmpty(s)) {
return new UserInfo();
}
UserInfo userInfo = JSON.parseObject(s, UserInfo.class);
if (userInfo == null) {
return new UserInfo();
}
return userInfo;
}
/**
* 是否同意协议
*
* @return
*/
public static boolean isAgreePolicy() {
return !SPUtils.getInstance("Constant").getBoolean("launchRequestPermission", true);
}
/**
* 同意协议
*/
public static void completeAgreePolicy() {
SPUtils.getInstance("Constant").put("launchRequestPermission",
false, true);
}
}