Files
UIUIPad-Launcher3-Q/src/com/uiuipad/os/manager/TimeControlManager.java
Fanhuitong 3d15a60ac9 version:1.1.1
fix:
update:修复报错,优化推送
2023-10-31 18:11:25 +08:00

197 lines
7.0 KiB
Java

package com.uiuipad.os.manager;
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.tencent.mmkv.MMKV;
import com.uiuipad.os.comm.CommonConfig;
import com.uiuipad.os.disklrucache.CacheHelper;
import com.uiuipad.os.network.UrlAddress;
import com.uiuipad.os.network.bean.TimeManageApp;
import com.uiuipad.os.network.bean.TimeManageSn;
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 MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
private HashMap<Integer, TimeManageApp> mClassifyTimeControlHashMap = new HashMap<>();
/*分类的所有包名列表*/
private HashMap<String, TimeManageApp> mAppTimeControlMap = new HashMap<>();
/*整机分类*/
private TimeManageSn mGlobalMachineControl;
private TimeControlManager(Context context) {
if (context == null) {
throw new RuntimeException("Context is NULL");
}
this.mContext = context;
String appTimeControlJson = mMMKV.decodeString(UrlAddress.GET_TIME_MANAGE_APP, "");
if (!TextUtils.isEmpty(appTimeControlJson)) {
Gson gson = new Gson();
Type type = new TypeToken<List<TimeManageApp>>() {
}.getType();
try {
List<TimeManageApp> appTimeControls = gson.fromJson(appTimeControlJson, type);
setAppTimeControlMap(appTimeControls);
} catch (Exception e) {
Log.e(TAG, "TimeControlManager: " + e.getMessage());
}
}
}
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, TimeManageApp> getAppTimeControlMap() {
return mAppTimeControlMap;
}
/**
* 判断是否有配置存在
*
* @param pkg
* @return
*/
public boolean havaConfigure(String pkg) {
TimeManageApp appTimeControl = mAppTimeControlMap.get(pkg);
if (appTimeControl == null) {
Log.e(TAG, "havaConfigure: not");
return false;
} else {
if (appTimeControl.getUse_type() == 1) {
//根据分类
Log.e(TAG, "havaConfigure: classify");
return mClassifyTimeControlHashMap.get(appTimeControl.getClass_id()) != null;
}
Log.e(TAG, "havaConfigure: hava");
return true;
}
}
/**
* 获取整机配置
*
* @return
*/
public TimeManageSn getGlobalMachineControl() {
if (mGlobalMachineControl == null) {
String jsonString = mMMKV.decodeString(UrlAddress.GET_TIME_MANAGE_SN, "");
//为 "" 是已经请求成功的
if (jsonString == null) {
return null;
} else {
Gson gson = new Gson();
Type Type = new TypeToken<TimeManageSn>() {
}.getType();
try {
TimeManageSn machineControl = gson.fromJson(jsonString, Type);
return machineControl;
} catch (Exception e) {
Log.e(TAG, "getGlobalMachineControl: " + e.getMessage());
return null;
}
}
} else {
return mGlobalMachineControl;
}
}
public void setGlobalMachineControl(TimeManageSn machineControl) {
this.mGlobalMachineControl = machineControl;
if (machineControl != null) {
RunningAppManager.getInstance().setGlobalUsageTime(machineControl.getToday_time());
}
}
/**
* 获取app的配置
*
* @param pkg
* @return
*/
public TimeManageApp getAppTimeControl(String pkg) {
TimeManageApp temp = mAppTimeControlMap.get(pkg);
if (temp == null) {
String jsonString = mMMKV.decodeString(UrlAddress.GET_TIME_MANAGE_APP, "");
//为 "" 是已经请求成功的
if (jsonString == null) {
return null;
} else {
Gson gson = new Gson();
Type Type = new TypeToken<List<TimeManageApp>>() {
}.getType();
List<TimeManageApp> appTimeControlList = gson.fromJson(jsonString, Type);
if (appTimeControlList == null) {
return null;
}
HashMap<String, TimeManageApp> appTimeControlMap = new HashMap<>();
for (TimeManageApp appTimeControl : appTimeControlList) {
appTimeControlMap.put(appTimeControl.getApp_package(), appTimeControl);
}
setAppTimeControlMap(appTimeControlList);
return appTimeControlMap.get(pkg);
}
} else {
return temp;
}
}
public void setAppTimeControl(String pkg, TimeManageApp appTimeControl) {
this.mAppTimeControlMap.put(pkg, appTimeControl);
}
/**
* 设置app的配置
*
* @param appTimeControlList
*/
public void setAppTimeControlMap(List<TimeManageApp> appTimeControlList) {
if (appTimeControlList == null || appTimeControlList.size() == 0) {
return;
}
HashMap<Integer, TimeManageApp> classifyTimeControlHashMap = new HashMap<>();
List<TimeManageApp> filterAppTimeControlList = appTimeControlList.stream().filter(appTimeControl -> {
//tc_use_type == 1 时用的是分类配置,配置都是一样的
return appTimeControl.getUse_type() == 1;
}).collect(Collectors.toList());
for (TimeManageApp appTimeControl : filterAppTimeControlList) {
classifyTimeControlHashMap.put(appTimeControl.getClass_id(), appTimeControl);
}
this.mClassifyTimeControlHashMap = classifyTimeControlHashMap;
HashMap<String, TimeManageApp> appTimeControlMap = new HashMap<>();
List<TimeManageApp> appTimeControls = new ArrayList<>();
for (TimeManageApp appTimeControl : appTimeControlList) {
appTimeControlMap.put(appTimeControl.getApp_package(), appTimeControl);
appTimeControls.add(appTimeControl);
}
RunningAppManager.getInstance().syncAppRemainingTime(appTimeControls);
this.mAppTimeControlMap = appTimeControlMap;
}
}