166 lines
6.3 KiB
Java
166 lines
6.3 KiB
Java
package com.xxpatx.os.manager;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.pm.PackageManager;
|
|
import android.content.pm.ResolveInfo;
|
|
import android.util.Log;
|
|
|
|
import com.tencent.mmkv.MMKV;
|
|
import com.xxpatx.os.activity.main.MainActivity;
|
|
import com.xxpatx.os.bean.DailyAppBean;
|
|
import com.xxpatx.os.config.CommonConfig;
|
|
import com.xxpatx.os.utils.ApkUtils;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.function.Predicate;
|
|
|
|
public class AppStatusManager {
|
|
private static final String TAG = "AppStatusManager";
|
|
|
|
@SuppressLint("StaticFieldLeak")
|
|
private static AppStatusManager sInstance;
|
|
|
|
private Context mContext;
|
|
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
|
|
private Set<String> hidedAppSet;
|
|
|
|
public static final String APP_STATUS_MANAGER_KEY = "AppStatusManagerSet";
|
|
|
|
private static final Set<String> defaultHiedApp = new HashSet<String>() {{
|
|
this.add("om.android.fmradio");//收音机
|
|
this.add("com.android.mms");//信息
|
|
this.add("com.android.gallery3d");
|
|
this.add("com.android.documentsui");
|
|
this.add("com.android.calculator2");
|
|
this.add("com.android.calendar");
|
|
this.add("com.android.settings");
|
|
this.add("com.android.stk");
|
|
this.add("com.tencent.mm");
|
|
this.add("com.ss.android.ugc.aweme");
|
|
this.add("com.mediatek.camera");
|
|
|
|
}};
|
|
|
|
private static final Set<String> mExcludeApp = new HashSet<String>() {{
|
|
this.add("com.android.contacts");
|
|
this.add("com.android.dialer");
|
|
// this.add("com.mediatek.camera");
|
|
this.add("cn.etouch.ecalendar");
|
|
}};
|
|
|
|
|
|
private AppStatusManager(Context context) {
|
|
if (context == null) {
|
|
throw new RuntimeException("Context is NULL");
|
|
}
|
|
this.mContext = context;
|
|
Set<String> stringSet = mMMKV.decodeStringSet(APP_STATUS_MANAGER_KEY, defaultHiedApp);
|
|
Log.e(TAG, "AppStatusManager: " + stringSet);
|
|
this.hidedAppSet = stringSet;
|
|
|
|
if (!mMMKV.decodeBool(CommonConfig.HIDE_CALENDAR_KEY, false)) {
|
|
removeHidedApp("cn.etouch.ecalendar");
|
|
addHidedApp("com.youloft.calendar");
|
|
addHidedApp("com.honghui.huangli");
|
|
ApkUtils.uninstall(mContext, "cn.etouch.ecalendar");
|
|
ApkUtils.disableApp(mContext, "cn.etouch.ecalendar");
|
|
mMMKV.encode(CommonConfig.HIDE_CALENDAR_KEY, true);
|
|
Log.e(TAG, "AppStatusManager: hide calendar");
|
|
}
|
|
long oldVersionCode = mMMKV.decodeLong(CommonConfig.APP_VERSION_CODE, 0);
|
|
if (oldVersionCode == 0) {
|
|
addHidedApp("com.android.stk");
|
|
addHidedApp("com.tencent.mm");
|
|
addHidedApp("com.ss.android.ugc.aweme");
|
|
addHidedApp("com.android.mms");
|
|
}
|
|
}
|
|
|
|
public static void init(Context context) {
|
|
if (sInstance == null) {
|
|
Log.e(TAG, "init: ");
|
|
sInstance = new AppStatusManager(context);
|
|
}
|
|
}
|
|
|
|
public static AppStatusManager getInstance() {
|
|
if (sInstance == null) {
|
|
throw new IllegalStateException("You must be init AppStatusManager first");
|
|
}
|
|
return sInstance;
|
|
}
|
|
|
|
public Set<String> getHidedAppSet() {
|
|
return this.hidedAppSet;
|
|
}
|
|
|
|
public void addHidedApp(String pkg) {
|
|
this.hidedAppSet.add(pkg);
|
|
mMMKV.encode(APP_STATUS_MANAGER_KEY, hidedAppSet);
|
|
mContext.sendBroadcast(new Intent(MainActivity.ACTION_PACKAGE_HIDE));
|
|
}
|
|
|
|
public void removeHidedApp(String pkg) {
|
|
this.hidedAppSet.remove(pkg);
|
|
mMMKV.encode(APP_STATUS_MANAGER_KEY, hidedAppSet);
|
|
mContext.sendBroadcast(new Intent(MainActivity.ACTION_PACKAGE_HIDE));
|
|
}
|
|
|
|
public List<DailyAppBean> getPackageList() {
|
|
PackageManager pm = mContext.getPackageManager();
|
|
List<DailyAppBean> dailyAppBeanList = new ArrayList<>();
|
|
// 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
|
|
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
|
|
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
|
|
// 通过getPackageManager()的queryIntentActivities方法遍历,得到所有能打开的app的packageName
|
|
List<ResolveInfo> resolveinfoList = pm.queryIntentActivities(resolveIntent, 0);
|
|
for (ResolveInfo packageInfo : resolveinfoList) {
|
|
String pkg = packageInfo.activityInfo.packageName;
|
|
if (mExcludeApp.contains(pkg)) {
|
|
continue;
|
|
}
|
|
if (hidedAppSet.contains(pkg)) {
|
|
Log.e(TAG, "getPackageList: " + pkg);
|
|
DailyAppBean appSelectBean = new DailyAppBean(packageInfo.activityInfo.loadLabel(pm).toString(),
|
|
packageInfo.activityInfo.packageName,
|
|
packageInfo.activityInfo.name
|
|
);
|
|
|
|
dailyAppBeanList.add(appSelectBean);
|
|
}
|
|
}
|
|
if (hidedAppSet.contains(AppManager.CONTACT_PACKAGE)) {
|
|
DailyAppBean contactIcon = new DailyAppBean("联系人", AppManager.CONTACT_PACKAGE);
|
|
dailyAppBeanList.add(0, contactIcon);
|
|
}
|
|
|
|
if (hidedAppSet.contains(AppManager.SERVICE_PACKAGE)) {
|
|
DailyAppBean serviceIcon = new DailyAppBean("客服中心", AppManager.SERVICE_PACKAGE);
|
|
dailyAppBeanList.add(0, serviceIcon);
|
|
}
|
|
|
|
return dailyAppBeanList;
|
|
}
|
|
|
|
public long getPackageListSize() {
|
|
PackageManager pm = mContext.getPackageManager();
|
|
List<DailyAppBean> dailyAppBeanList = new ArrayList<>();
|
|
// 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
|
|
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
|
|
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
|
|
// 通过getPackageManager()的queryIntentActivities方法遍历,得到所有能打开的app的packageName
|
|
List<ResolveInfo> resolveinfoList = pm.queryIntentActivities(resolveIntent, 0);
|
|
return resolveinfoList.stream().filter(new Predicate<ResolveInfo>() {
|
|
@Override
|
|
public boolean test(ResolveInfo resolveInfo) {
|
|
return hidedAppSet.contains(resolveInfo.activityInfo.packageName);
|
|
}
|
|
}).count();
|
|
}
|
|
}
|