Files
FLYSN/app/src/main/java/com/fuying/sn/desktop/TimeControlManager.java
tongtongstudio 4217eb7f8b feat: 基本功能逻辑没有问题
在自动返回到桌面时,后端说有接口没有传上去,待修复。
准备升级Android Studio版本
2026-06-02 15:52:29 +08:00

195 lines
6.9 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.fuying.sn.BuildConfig;
import com.fuying.sn.disklrucache.CacheHelper;
import com.fuying.sn.network.UrlAddress;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
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";
@SuppressLint("StaticFieldLeak")
private static TimeControlManager sInstance;
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 (sInstance == null) {
sInstance = new TimeControlManager(context);
}
}
public static TimeControlManager getInstance() {
if (sInstance == null) {
throw new IllegalStateException("You must be init TimeControlManager first");
}
return sInstance;
}
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) {
if (BuildConfig.VERSION_CODE < 88) {
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);
Log.e(TAG, "getAppTimeControl: jsonString = " + jsonString);
//为 "" 是已经请求成功的
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) {
Log.e(TAG, "setAppTimeControlMap: appTimeControlList is empty");
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);
// }
}
if (BuildConfig.VERSION_CODE < 88) {
RunningAppManager.getInstance().syncAppRemainingTime(appTimeControls);
}
this.mAppTimeControlMap = appTimeControlMap;
}
}