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 mClassifyTimeControlHashMap = new HashMap<>(); /*分类的所有包名列表*/ private HashMap 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>() { }.getType(); try { List 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 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() { }.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>() { }.getType(); List appTimeControlList = gson.fromJson(jsonString, Type); if (appTimeControlList == null) { return null; } HashMap 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 appTimeControlList) { if (appTimeControlList == null || appTimeControlList.size() == 0) { return; } HashMap classifyTimeControlHashMap = new HashMap<>(); List 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 appTimeControlMap = new HashMap<>(); List appTimeControls = new ArrayList<>(); for (TimeManageApp appTimeControl : appTimeControlList) { appTimeControlMap.put(appTimeControl.getApp_package(), appTimeControl); appTimeControls.add(appTimeControl); } RunningAppManager.getInstance().syncAppRemainingTime(appTimeControls); this.mAppTimeControlMap = appTimeControlMap; } }