164 lines
6.5 KiB
Java
164 lines
6.5 KiB
Java
package com.android.boot.ad;
|
|
import static com.android.boot.ad.TouchTools.mainActivity;
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.TypeReference;
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
public class SharedPreferencesTools {
|
|
public static final String DATE = getToday();
|
|
public static final String IS_FIRST = "isFirst";
|
|
public static final String FIR_START = "first_start";
|
|
public static final String REWARD_CLICK_TIME = "REWARD_CLICK_TIME";
|
|
public static final String CLICK01 = "CLICK01";
|
|
public static final String CLICK02 = "CLICK02";
|
|
|
|
public static void putFirstStart() {
|
|
SharedPreferencesTools.putLong(FIR_START, System.currentTimeMillis(), mainActivity);
|
|
}
|
|
|
|
public static long getFirstStart() {
|
|
return SharedPreferencesTools.getLong(FIR_START, 0, mainActivity);
|
|
}
|
|
|
|
public static int getRewardTotalClickToday() {
|
|
return getIntegerDefault0(getToday() + CLICK01, TouchTools.mainActivity);
|
|
}
|
|
|
|
public static void setRewardTotalClickToday(int value, Activity activity) {
|
|
if (value >= 3) {
|
|
SharedPreferencesTools.putBoolean(SharedPreferencesTools.REWARD_CLICK_TIME, true, mainActivity);
|
|
}
|
|
SharedPreferencesTools.putInteger(getToday() + CLICK01, value, activity);
|
|
}
|
|
|
|
public static void addTotalClickToday(Activity activity) {
|
|
AdManager.click02++;
|
|
SharedPreferencesTools.putInteger(getToday() + CLICK02, AdManager.click02, activity);
|
|
}
|
|
|
|
public static void setTotalClickToday(int value, Activity activity) {
|
|
SharedPreferencesTools.putInteger(getToday() + CLICK02, value, activity);
|
|
}
|
|
|
|
public static int getTotalClickToday() {
|
|
return getIntegerDefault0(getToday() + CLICK02, TouchTools.mainActivity);
|
|
}
|
|
|
|
public static <K, V> void saveMapToPreferences(String mapName, Map<K, V> map, Activity activity) {
|
|
SharedPreferences sharedPreferences = activity.getPreferences(Context.MODE_PRIVATE);
|
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
|
|
|
// 使用 fastJson 将 Map 转换为 JSON 字符串
|
|
String json = JSON.toJSONString(map);
|
|
|
|
// 将 JSON 字符串存储到 SharedPreferences 中
|
|
editor.putString(mapName, json);
|
|
editor.apply(); // 或 editor.commit();
|
|
}
|
|
|
|
public static String getToday() {
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.set(Calendar.HOUR_OF_DAY, 23);
|
|
calendar.set(Calendar.MINUTE, 59);
|
|
calendar.set(Calendar.SECOND, 59);
|
|
calendar.set(Calendar.MILLISECOND, 999);
|
|
return calendar.getTime().toString();
|
|
}
|
|
|
|
public static <K, V> Map<K, V> getMapFromPreferences(String mapName, TypeReference<Map<K, V>> typeReference, Activity activity) {
|
|
SharedPreferences sharedPreferences = activity.getPreferences(Context.MODE_PRIVATE);
|
|
String json = sharedPreferences.getString(mapName, "");
|
|
|
|
// 如果没有保存数据,则返回一个空的 Map
|
|
assert json != null;
|
|
if (json.isEmpty()) {
|
|
return new HashMap<>();
|
|
}
|
|
|
|
// 使用 fastJson 将 JSON 字符串转换为 Map 对象
|
|
return JSON.parseObject(json, typeReference);
|
|
}
|
|
|
|
public static void putInteger(String name, String key, int value, Context context) {
|
|
SharedPreferences.Editor editor = context.getSharedPreferences(name, Context.MODE_PRIVATE).edit();
|
|
editor.putInt(key, value).apply();
|
|
}
|
|
|
|
public static void putInteger(String key, int value, Activity activity) {
|
|
SharedPreferences.Editor editor = activity.getPreferences(Context.MODE_PRIVATE).edit();
|
|
editor.putInt(key, value).apply();
|
|
}
|
|
|
|
|
|
public static void putString(String name, String key, String value, Context context) {
|
|
SharedPreferences.Editor editor = context.getSharedPreferences(name, Context.MODE_PRIVATE).edit();
|
|
editor.putString(key, value).apply();
|
|
}
|
|
|
|
public static void putLong(String name, String key, Long value, Context context) {
|
|
SharedPreferences.Editor editor = context.getSharedPreferences(name, Context.MODE_PRIVATE).edit();
|
|
editor.putLong(key, value).apply();
|
|
}
|
|
|
|
public static void putBoolean(String name, String key, boolean value, Context context) {
|
|
SharedPreferences.Editor editor = context.getSharedPreferences(name, Context.MODE_PRIVATE).edit();
|
|
editor.putBoolean(key, value).apply();
|
|
}
|
|
|
|
public static void putBoolean(String key, boolean value, Activity activity) {
|
|
SharedPreferences.Editor editor = activity.getPreferences(Context.MODE_PRIVATE).edit();
|
|
editor.putBoolean(key, value).apply();
|
|
}
|
|
|
|
|
|
public static String getString(String name, String key, Context context) {
|
|
String value = context.getSharedPreferences(name, Context.MODE_PRIVATE).getString(key, null);
|
|
if (value == null) {
|
|
throw new RuntimeException("数据不存在!");
|
|
} else {
|
|
return value;
|
|
}
|
|
}
|
|
|
|
public static long getLong(String name, String key, long defaultValue, Context context) {
|
|
return context.getSharedPreferences(name, Context.MODE_PRIVATE).getLong(key, defaultValue);
|
|
}
|
|
|
|
public static void putLong(String key, long value, Activity activity) {
|
|
SharedPreferences.Editor editor = activity.getPreferences(Context.MODE_PRIVATE).edit();
|
|
editor.putLong(key, value).apply();
|
|
}
|
|
|
|
public static long getLong(String key, long defaultValue, Activity activity) {
|
|
return activity.getPreferences(Context.MODE_PRIVATE).getLong(key, defaultValue);
|
|
}
|
|
|
|
|
|
public static boolean getBoolean(String name, String key, Context context) {
|
|
return context.getSharedPreferences(name, Context.MODE_PRIVATE).getBoolean(key, false);
|
|
}
|
|
|
|
|
|
public static boolean getBoolean(String key, Activity activity, boolean defaultValue) {
|
|
return activity.getPreferences(Context.MODE_PRIVATE).getBoolean(key, defaultValue);
|
|
}
|
|
|
|
public static boolean getBoolean(String key, Activity activity) {
|
|
return activity.getPreferences(Context.MODE_PRIVATE).getBoolean(key, false);
|
|
}
|
|
|
|
|
|
public static int getIntegerDefault0(String name, String key, Context context) {
|
|
return context.getSharedPreferences(name, Context.MODE_PRIVATE).getInt(key, 0);
|
|
}
|
|
|
|
public static int getIntegerDefault0(String key, Activity activity) {
|
|
return activity.getPreferences(Context.MODE_PRIVATE).getInt(key, 0);
|
|
}
|
|
}
|