103 lines
3.6 KiB
Java
103 lines
3.6 KiB
Java
package com.android;
|
|
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
import android.text.TextUtils;
|
|
|
|
import com.android.util.GsonUtil;
|
|
import com.android.util.LogUtils;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
public class SharePreUtils {
|
|
public static final String SHARERE_NAME="upload_ads_droid";
|
|
private static SharePreUtils mInstance;
|
|
private static List<Map<String, Object>> dataList;//处理一对多的关系 key为AdSourceId value 为任务类型值
|
|
private static SharedPreferences.Editor editor;
|
|
|
|
private static SharedPreferences sharedPreferences;
|
|
public SharePreUtils(Context context) {
|
|
sharedPreferences=context.getSharedPreferences(SHARERE_NAME,Context.MODE_PRIVATE);
|
|
editor=sharedPreferences.edit();
|
|
dataList=getDataList();
|
|
}
|
|
|
|
public synchronized static SharePreUtils getInstance(Context context){
|
|
if(mInstance==null){
|
|
mInstance=new SharePreUtils(context);
|
|
}
|
|
return mInstance;
|
|
}
|
|
public synchronized static List<Map<String, Object>> getDataList(){
|
|
if(dataList==null){
|
|
String adsHasUpload = sharedPreferences.getString("ads_has_upload", "");
|
|
if(!TextUtils.isEmpty(adsHasUpload)){
|
|
List<Map<String, Object>> mapList = GsonUtil.GsonToListMaps(adsHasUpload);
|
|
dataList=mapList;
|
|
}else{
|
|
dataList=new ArrayList<>();
|
|
}
|
|
}
|
|
return dataList;
|
|
}
|
|
|
|
public synchronized static void addRelation(String key, Object value) {
|
|
if(dataList!=null){
|
|
Map<String, Object> record = new HashMap<>();
|
|
record.put("relation_key", key);
|
|
record.put("relation_value", value);
|
|
dataList.add(record);
|
|
}
|
|
}
|
|
|
|
public static boolean filterByKeyValue(String key, Object value) {
|
|
List<Map<String, Object>> filtered = new ArrayList<>();
|
|
if(dataList!=null){
|
|
for (Map<String, Object> record : dataList) {
|
|
if (key.equals(record.get("relation_key")) &&(value == null ? record.get("relation_value") == null :value.equals(record.get("relation_value")))) {
|
|
filtered.add(record);
|
|
}
|
|
}
|
|
}
|
|
LogUtils.loge("filterByKeyValue() size"+filtered.size()+"dataList() size"+dataList.size());
|
|
return filtered.size()==1;
|
|
}
|
|
|
|
public synchronized static void savePreference(){
|
|
if(dataList!=null&&dataList.size()>0){
|
|
String commitJson = GsonUtil.GsonString(dataList);
|
|
editor.putString("ads_has_upload",commitJson).apply();
|
|
}
|
|
}
|
|
|
|
public static void removeKeyFromAllMaps(String keyToRemove) {
|
|
if (dataList == null || keyToRemove == null) {
|
|
return;
|
|
}
|
|
List<Map<String, Object>> current=new ArrayList<>();
|
|
// 遍历列表中的每个Map
|
|
for (Map<String, Object> map : dataList) {
|
|
if (map != null) {
|
|
// 使用remove方法删除指定键
|
|
if(!map.containsKey(keyToRemove)){
|
|
current.add(map);
|
|
}
|
|
}
|
|
}
|
|
dataList=current;
|
|
}
|
|
|
|
public static void resetDataList(){
|
|
String adsHasUpload = sharedPreferences.getString("ads_has_upload", "");
|
|
if(!TextUtils.isEmpty(adsHasUpload)&& GsonUtil.isJson(adsHasUpload)){
|
|
List<Map<String, Object>> mapList = GsonUtil.GsonToListMaps(adsHasUpload);
|
|
dataList=mapList;
|
|
}else{
|
|
dataList=new ArrayList<>();
|
|
}
|
|
}
|
|
}
|