package com.hdl.sdk.common.utils;
|
|
import android.content.Context;
|
import android.content.SharedPreferences;
|
import android.util.Base64;
|
|
|
import com.hdl.sdk.common.HDLSdk;
|
|
|
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
import java.io.ObjectInputStream;
|
import java.io.ObjectOutputStream;
|
import java.util.Collections;
|
import java.util.Map;
|
import java.util.Set;
|
|
/**
|
* Created by Tong on 2021/9/28.
|
*/
|
public class SPUtils {
|
private static final String APP_PREFERENCES_KEY = "profile";
|
private static final SharedPreferences PREFERENCES =
|
HDLSdk.getInstance().getContext().getApplicationContext().getSharedPreferences(APP_PREFERENCES_KEY, Context.MODE_PRIVATE);
|
|
private static SharedPreferences getAppPreference() {
|
return PREFERENCES;
|
}
|
|
|
//======通用存储========
|
public static void put(final String key, final String value) {
|
getAppPreference().edit().putString(key, value).apply();
|
}
|
|
|
public static String getString(final String key) {
|
return getString(key, "");
|
}
|
|
|
public static String getString(final String key, final String defaultValue) {
|
return getAppPreference().getString(key, defaultValue);
|
}
|
|
|
public static void put(final String key, final int value) {
|
put(key, value, false);
|
}
|
|
|
public static void put(final String key, final int value, final boolean isCommit) {
|
getAppPreference().edit().putInt(key, value).apply();
|
}
|
|
|
public static int getInt(final String key) {
|
return getInt(key, -1);
|
}
|
|
|
public static int getInt(final String key, final int defaultValue) {
|
return getAppPreference().getInt(key, defaultValue);
|
}
|
|
|
public static void put(final String key, final long value) {
|
getAppPreference().edit().putLong(key, value).apply();
|
}
|
|
|
public static long getLong(final String key) {
|
return getLong(key, -1L);
|
}
|
|
|
public static long getLong(final String key, final long defaultValue) {
|
return getAppPreference().getLong(key, defaultValue);
|
}
|
|
|
public static void put(final String key, final float value) {
|
getAppPreference().edit().putFloat(key, value).apply();
|
}
|
|
|
public static float getFloat(final String key) {
|
return getFloat(key, -1f);
|
}
|
|
|
public static float getFloat(final String key, final float defaultValue) {
|
return getAppPreference().getFloat(key, defaultValue);
|
}
|
|
|
public static void put(final String key, final boolean value) {
|
getAppPreference().edit().putBoolean(key, value).apply();
|
}
|
|
|
public static boolean getBoolean(final String key) {
|
return getBoolean(key, false);
|
}
|
|
|
public static boolean getBoolean(final String key, final boolean defaultValue) {
|
return getAppPreference().getBoolean(key, defaultValue);
|
}
|
|
|
public static void put(final String key,
|
final Set<String> value
|
) {
|
|
getAppPreference().edit().putStringSet(key, value).apply();
|
|
}
|
|
|
public static Set<String> getStringSet(final String key) {
|
return getStringSet(key, Collections.<String>emptySet());
|
}
|
|
|
public static Set<String> getStringSet(final String key,
|
final Set<String> defaultValue) {
|
return getAppPreference().getStringSet(key, defaultValue);
|
}
|
|
|
public static Map<String, ?> getAll() {
|
return getAppPreference().getAll();
|
}
|
|
|
public static boolean contains(final String key) {
|
return getAppPreference().contains(key);
|
}
|
|
|
public static void remove(final String key) {
|
getAppPreference().edit().remove(key).apply();
|
}
|
|
public static void clear() {
|
getAppPreference()
|
.edit()
|
.clear()
|
.apply();
|
}
|
|
private static String Object2String(Object object) {
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
ObjectOutputStream objectOutputStream = null;
|
try {
|
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
|
objectOutputStream.writeObject(object);
|
String string = new String(Base64.encode(byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
|
objectOutputStream.close();
|
return string;
|
} catch (IOException e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
private static Object String2Object(String objectString) {
|
byte[] mobileBytes = Base64.decode(objectString.getBytes(), Base64.DEFAULT);
|
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mobileBytes);
|
ObjectInputStream objectInputStream = null;
|
try {
|
objectInputStream = new ObjectInputStream(byteArrayInputStream);
|
Object object = objectInputStream.readObject();
|
objectInputStream.close();
|
return object;
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
public static void saveSerializableEntity(String key, Object saveObject) {
|
SharedPreferences.Editor editor = getAppPreference().edit();
|
String string = Object2String(saveObject);
|
editor.putString(key, string);
|
editor.commit();
|
}
|
|
public static Object getSerializableEntity(String key) {
|
String string = getAppPreference().getString(key, null);
|
if (string != null) {
|
Object object = String2Object(string);
|
return object;
|
} else {
|
return null;
|
}
|
}
|
|
|
}
|