121 lines
4.6 KiB
Java
121 lines
4.6 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.bean.DailyAppBean;
|
|
import com.xxpatx.os.config.CommonConfig;
|
|
|
|
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.mediatek.camera");
|
|
this.add("com.android.dialer");
|
|
this.add("com.android.settings");
|
|
}};
|
|
|
|
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);
|
|
this.hidedAppSet = stringSet;
|
|
}
|
|
|
|
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(OldMainActivity.ACTION_PACKAGE_HIDE));
|
|
}
|
|
|
|
public void removeHidedApp(String pkg) {
|
|
this.hidedAppSet.remove(pkg);
|
|
mMMKV.encode(APP_STATUS_MANAGER_KEY, hidedAppSet);
|
|
// mContext.sendBroadcast(new Intent(OldMainActivity.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 (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);
|
|
}
|
|
}
|
|
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();
|
|
}
|
|
}
|