188 lines
6.6 KiB
Java
188 lines
6.6 KiB
Java
package com.fuying.sn.desktop;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.content.Context;
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.reflect.TypeToken;
|
|
import com.fuying.sn.disklrucache.CacheHelper;
|
|
import com.fuying.sn.network.UrlAddress;
|
|
|
|
import java.lang.reflect.Type;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class TimeControlManager {
|
|
private static final String TAG = TimeControlManager.class.getSimpleName();
|
|
|
|
@SuppressLint("StaticFieldLeak")
|
|
private static TimeControlManager mTimeControlManager;
|
|
private Context mContext;
|
|
private CacheHelper mCacheHelper;
|
|
|
|
private HashMap<Integer, AppTimeControl> mClassifyTimeControlHashMap = new HashMap<>();
|
|
/*分类的所有包名列表*/
|
|
private HashMap<String, AppTimeControl> mAppTimeControlMap = new HashMap<>();
|
|
/*整机分类*/
|
|
private MachineControl mGlobalMachineControl;
|
|
|
|
private TimeControlManager(Context context) {
|
|
if (context == null) {
|
|
throw new RuntimeException("Context is NULL");
|
|
}
|
|
this.mContext = context;
|
|
this.mCacheHelper = new CacheHelper(context);
|
|
String appTimeControlJson = mCacheHelper.getAsString(UrlAddress.GET_APP_TIME_CONTROL);
|
|
if (!TextUtils.isEmpty(appTimeControlJson)) {
|
|
Gson gson = new Gson();
|
|
Type type = new TypeToken<List<AppTimeControl>>() {
|
|
}.getType();
|
|
List<AppTimeControl> appTimeControls = gson.fromJson(appTimeControlJson, type);
|
|
setAppTimeControlMap(appTimeControls);
|
|
}
|
|
}
|
|
|
|
public static void init(Context context) {
|
|
if (mTimeControlManager == null) {
|
|
mTimeControlManager = new TimeControlManager(context);
|
|
}
|
|
}
|
|
|
|
public static TimeControlManager getInstance() {
|
|
if (mTimeControlManager == null) {
|
|
throw new IllegalStateException("You must be init TimeControlManager first");
|
|
}
|
|
return mTimeControlManager;
|
|
}
|
|
|
|
public HashMap<String, AppTimeControl> getAppTimeControlMap() {
|
|
return mAppTimeControlMap;
|
|
}
|
|
|
|
/**
|
|
* 判断是否有配置存在
|
|
*
|
|
* @param pkg
|
|
* @return
|
|
*/
|
|
public boolean havaConfigure(String pkg) {
|
|
AppTimeControl appTimeControl = mAppTimeControlMap.get(pkg);
|
|
if (appTimeControl == null) {
|
|
Log.e(TAG, "havaConfigure: not");
|
|
return false;
|
|
} else {
|
|
if (appTimeControl.getTc_use_type() == 1) {
|
|
//根据分类
|
|
Log.e(TAG, "havaConfigure: classify");
|
|
return mClassifyTimeControlHashMap.get(appTimeControl.getTc_class_id()) != null;
|
|
}
|
|
Log.e(TAG, "havaConfigure: hava");
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取整机配置
|
|
*
|
|
* @return
|
|
*/
|
|
public MachineControl getGlobalMachineControl() {
|
|
if (mGlobalMachineControl == null) {
|
|
String jsonString = mCacheHelper.getAsString(UrlAddress.GET_SN_TIME_CONTROL);
|
|
//为 "" 是已经请求成功的
|
|
if (jsonString == null) {
|
|
return null;
|
|
} else {
|
|
Gson gson = new Gson();
|
|
Type type = new TypeToken<MachineControl>() {
|
|
}.getType();
|
|
MachineControl machineControl = gson.fromJson(jsonString, type);
|
|
return machineControl;
|
|
}
|
|
} else {
|
|
return mGlobalMachineControl;
|
|
}
|
|
}
|
|
|
|
public void setGlobalMachineControl(MachineControl machineControl) {
|
|
this.mGlobalMachineControl = machineControl;
|
|
if (machineControl != null) {
|
|
RunningAppManager.getInstance().setGlobalUsageTime(machineControl.getToday_time());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取app的配置
|
|
*
|
|
* @param pkg
|
|
* @return
|
|
*/
|
|
public AppTimeControl getAppTimeControl(String pkg) {
|
|
AppTimeControl temp = mAppTimeControlMap.get(pkg);
|
|
if (temp == null) {
|
|
String jsonString = mCacheHelper.getAsString(UrlAddress.GET_APP_TIME_CONTROL);
|
|
//为 "" 是已经请求成功的
|
|
if (jsonString == null) {
|
|
return null;
|
|
} else {
|
|
Gson gson = new Gson();
|
|
Type Type = new TypeToken<List<AppTimeControl>>() {
|
|
}.getType();
|
|
List<AppTimeControl> appTimeControlList = gson.fromJson(jsonString, Type);
|
|
if (appTimeControlList == null) {
|
|
return null;
|
|
}
|
|
HashMap<String, AppTimeControl> appTimeControlMap = new HashMap<>();
|
|
for (AppTimeControl appTimeControl : appTimeControlList) {
|
|
appTimeControlMap.put(appTimeControl.getApp_package(), appTimeControl);
|
|
}
|
|
setAppTimeControlMap(appTimeControlList);
|
|
return appTimeControlMap.get(pkg);
|
|
}
|
|
} else {
|
|
return temp;
|
|
}
|
|
}
|
|
|
|
public void setAppTimeControl(String pkg, AppTimeControl appTimeControl) {
|
|
this.mAppTimeControlMap.put(pkg, appTimeControl);
|
|
}
|
|
|
|
|
|
/**
|
|
* 设置app的配置
|
|
*
|
|
* @param appTimeControlList
|
|
*/
|
|
public void setAppTimeControlMap(List<AppTimeControl> appTimeControlList) {
|
|
if (appTimeControlList == null || appTimeControlList.size() == 0) {
|
|
return;
|
|
}
|
|
HashMap<Integer, AppTimeControl> classifyTimeControlHashMap = new HashMap<>();
|
|
List<AppTimeControl> filterAppTimeControlList = appTimeControlList.stream().filter(appTimeControl -> {
|
|
//tc_use_type == 1 时用的是分类配置,配置都是一样的
|
|
return appTimeControl.getTc_use_type() == 1;
|
|
}).collect(Collectors.toList());
|
|
for (AppTimeControl appTimeControl : filterAppTimeControlList) {
|
|
classifyTimeControlHashMap.put(appTimeControl.getTc_class_id(), appTimeControl);
|
|
}
|
|
this.mClassifyTimeControlHashMap = classifyTimeControlHashMap;
|
|
|
|
HashMap<String, AppTimeControl> appTimeControlMap = new HashMap<>();
|
|
List<AppTimeControl> appTimeControls = new ArrayList<>();
|
|
for (AppTimeControl appTimeControl : appTimeControlList) {
|
|
// TODO: 2022/6/30 修改为全部配置
|
|
// if (appTimeControl.getTc_use_type() != 1) {
|
|
appTimeControlMap.put(appTimeControl.getApp_package(), appTimeControl);
|
|
appTimeControls.add(appTimeControl);
|
|
// }
|
|
}
|
|
RunningAppManager.getInstance().syncAppRemainingTime(appTimeControls);
|
|
this.mAppTimeControlMap = appTimeControlMap;
|
|
}
|
|
}
|