Files
VscoolOS/app/src/main/java/com/xxpatx/os/manager/AppManager.java
Fanhuitong b4fc904734 version:1.0.1
update:
bugfixes:修复TAG
2024-07-13 15:35:13 +08:00

318 lines
14 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.xxpatx.os.manager;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import com.tencent.mmkv.MMKV;
import com.xxpatx.os.bean.DesktopIcon;
import com.xxpatx.os.config.CommonConfig;
import com.xxpatx.os.utils.ApkUtils;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;
public class AppManager {
private static final String TAG = "AppManager";
public static final String ADD_NAME = "com.zyos.add";
public static final String UPDATE_NAME = "com.zyos.update";
private static final String SHOW_PACKAGE_KEY = "SHOW_PACKAGE_KEY";
private static final String ADD_PACKAGE_KEY = "ADD_PACKAGE_KEY";
@SuppressLint("StaticFieldLeak")
private static AppManager sInstance;
private Context mContext;
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
private Set<String> showPackages;
private Set<String> addPackages;
public static void init(Context context) {
if (context == null) {
throw new RuntimeException("context is NULL");
}
if (sInstance == null) {
Log.e(TAG, "init: ");
sInstance = new AppManager(context);
}
}
public AppManager(Context context) {
if (context == null) {
throw new RuntimeException("Context is NULL");
}
this.mContext = context;
this.showPackages = mMMKV.decodeStringSet(SHOW_PACKAGE_KEY, new HashSet<>());
this.addPackages = mMMKV.decodeStringSet(ADD_PACKAGE_KEY, new HashSet<>());
this.showPackages.removeIf(TextUtils::isEmpty);
}
public static AppManager getInstance() {
if (sInstance == null) {
throw new IllegalStateException("You must be init AppManager first");
}
return sInstance;
}
public Set<String> getShowPackages() {
return showPackages;
}
public void addPakcage(String packageName) {
this.showPackages.add(packageName);
mMMKV.encode(SHOW_PACKAGE_KEY, showPackages);
}
public void removePakcage(String packageName) {
this.showPackages.remove(packageName);
mMMKV.encode(SHOW_PACKAGE_KEY, showPackages);
}
public Set<String> getFilterAppset() {
PackageManager pm = mContext.getPackageManager();
// 查询所有已经安装的应用程序
List<ResolveInfo> resolveInfos = 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);
Set<String> allowPackages = resolveinfoList.stream().map(resolveInfo -> resolveInfo.activityInfo.packageName).collect(Collectors.toSet());
// List<String> adminApp = RemoteManager.getInstance().getAdminApp();
// Log.i(TAG, "getFilterAppset: adminapp = " + adminApp);
for (ResolveInfo resolveInfo : resolveinfoList) {
String pkg = resolveInfo.activityInfo.packageName;
if (ApkUtils.appIsDisable(mContext, pkg)) {
Log.e(TAG, "getFilterAppset: disable = " + pkg);
continue;
}
if (showPackages.contains(pkg)) {
resolveInfos.add(resolveInfo);
} else {
//通过flag排除系统应用会将电话、短信也排除掉
if (ApkUtils.isSystemApp(mContext, pkg)) {
if (ApkUtils.showPackageName.contains(pkg)) {
resolveInfos.add(resolveInfo);
}
} else {
if (allowPackages.contains(pkg) && !ApkUtils.excludePackageName.contains(pkg)) {
// if (adminApp.contains(pkg)) {
// resolveInfos.add(resolveInfo);
// } else if (ApkUtils.showPackageName.contains(pkg)) {
resolveInfos.add(resolveInfo);
// }
}
}
}
}
if (Settings.Global.getInt(mContext.getContentResolver(), "is_activity", 0) == 0) {
resolveInfos.removeIf(resolveInfo -> "com.uiui.city".equals(resolveInfo.activityInfo.packageName));
}
Set<String> desktopIcons = resolveInfos.stream().map(resolveInfo -> resolveInfo.activityInfo.packageName).collect(Collectors.toSet());
return desktopIcons;
}
public ArrayList<DesktopIcon> getFilterAppList() {
PackageManager pm = mContext.getPackageManager();
// 查询所有已经安装的应用程序
List<ResolveInfo> resolveInfos = 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);
Set<String> allowPackages = resolveinfoList.stream().map(resolveInfo -> resolveInfo.activityInfo.packageName).collect(Collectors.toSet());
// List<String> adminApp = RemoteManager.getInstance().getAdminApp();
// Log.i(TAG, "getFilterAppset: adminapp = " + adminApp);
for (ResolveInfo resolveInfo : resolveinfoList) {
String pkg = resolveInfo.activityInfo.packageName;
if (ApkUtils.appIsDisable(mContext, pkg)) {
Log.e(TAG, "getFilterAppset: disable = " + pkg);
continue;
}
if (showPackages.contains(pkg)) {
resolveInfos.add(resolveInfo);
} else {
//通过flag排除系统应用会将电话、短信也排除掉
if (ApkUtils.isSystemApp(mContext, pkg)) {
if (ApkUtils.showPackageName.contains(pkg)) {
resolveInfos.add(resolveInfo);
}
} else {
if (allowPackages.contains(pkg) && !ApkUtils.excludePackageName.contains(pkg)) {
// if (adminApp.contains(pkg)) {
// resolveInfos.add(resolveInfo);
// } else if (ApkUtils.showPackageName.contains(pkg)) {
resolveInfos.add(resolveInfo);
// }
}
}
}
}
if (Settings.Global.getInt(mContext.getContentResolver(), "is_activity", 0) == 0) {
resolveInfos.removeIf(resolveInfo -> "com.uiui.city".equals(resolveInfo.activityInfo.packageName));
}
resolveInfos.sort(new Comparator<ResolveInfo>() {
@Override
public int compare(ResolveInfo o1, ResolveInfo o2) {
return Collator.getInstance(Locale.CHINESE).compare(o1.loadLabel(pm).toString(), o2.loadLabel(pm).toString());
}
});
resolveInfos.sort(new Comparator<ResolveInfo>() {
@Override
public int compare(ResolveInfo o1, ResolveInfo o2) {
try {
if ((pm.getApplicationInfo(o1.activityInfo.packageName, 0).flags & ApplicationInfo.FLAG_SYSTEM) <= (pm.getApplicationInfo(o2.activityInfo.packageName, 0).flags & ApplicationInfo.FLAG_SYSTEM)) {
return 1;
} else {
return -1;
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return 0;
}
}
});
ArrayList<DesktopIcon> desktopIcons = new ArrayList<>();
for (ResolveInfo applicationInfo : resolveInfos) {
if (!ApkUtils.excludeClassName.contains(applicationInfo.activityInfo.name)) {
desktopIcons.add(DesktopIcon.creatDesktopIcon(mContext, applicationInfo));
}
}
return desktopIcons;
}
public ArrayList<DesktopIcon> getAllAppList() {
PackageManager pm = mContext.getPackageManager();
// 查询所有已经安装的应用程序
List<ResolveInfo> resolveInfos = 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);
Set<String> allowPackages = resolveinfoList.stream().map(resolveInfo -> resolveInfo.activityInfo.packageName).collect(Collectors.toSet());
// List<String> adminApp = RemoteManager.getInstance().getAdminApp();
// Log.i(TAG, "getFilterAppset: adminapp = " + adminApp);
for (ResolveInfo resolveInfo : resolveinfoList) {
String pkg = resolveInfo.activityInfo.packageName;
if (ApkUtils.appIsDisable(mContext, pkg)) {
Log.e(TAG, "getFilterAppset: disable = " + pkg);
continue;
}
if (ApkUtils.excludePackageName.contains(pkg)) {
continue;
}
resolveInfos.add(resolveInfo);
}
if (Settings.Global.getInt(mContext.getContentResolver(), "is_activity", 0) == 0) {
resolveInfos.removeIf(resolveInfo -> "com.uiui.city".equals(resolveInfo.activityInfo.packageName));
}
resolveInfos.sort(new Comparator<ResolveInfo>() {
@Override
public int compare(ResolveInfo o1, ResolveInfo o2) {
return Collator.getInstance(Locale.CHINESE).compare(o1.loadLabel(pm).toString(), o2.loadLabel(pm).toString());
}
});
// resolveInfos.sort(new Comparator<ResolveInfo>() {
// @Override
// public int compare(ResolveInfo o1, ResolveInfo o2) {
// try {
// if ((pm.getApplicationInfo(o1.activityInfo.packageName, 0).flags & ApplicationInfo.FLAG_SYSTEM) <= (pm.getApplicationInfo(o2.activityInfo.packageName, 0).flags & ApplicationInfo.FLAG_SYSTEM)) {
// return 1;
// } else {
// return -1;
// }
// } catch (PackageManager.NameNotFoundException e) {
// e.printStackTrace();
// return 0;
// }
// }
// });
ArrayList<DesktopIcon> desktopIcons = new ArrayList<>();
for (ResolveInfo applicationInfo : resolveInfos) {
desktopIcons.add(DesktopIcon.creatDesktopIcon(mContext, applicationInfo));
}
return desktopIcons;
}
public ArrayList<DesktopIcon> getAddDesktopIcon() {
PackageManager pm = mContext.getPackageManager();
// 查询所有已经安装的应用程序
List<ResolveInfo> resolveInfos = 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 resolveInfo : resolveinfoList) {
if (addPackages.contains(resolveInfo.activityInfo.packageName)) {
resolveInfos.add(resolveInfo);
}
}
ArrayList<DesktopIcon> desktopIcons = new ArrayList<>();
for (ResolveInfo applicationInfo : resolveInfos) {
desktopIcons.add(DesktopIcon.creatDesktopIcon(mContext, applicationInfo));
}
return desktopIcons;
}
public Set<String> getAddDesktopIconSet() {
PackageManager pm = mContext.getPackageManager();
// 查询所有已经安装的应用程序
List<ResolveInfo> resolveInfos = 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 resolveInfo : resolveinfoList) {
if (addPackages.contains(resolveInfo.activityInfo.packageName)) {
resolveInfos.add(resolveInfo);
}
}
Set<String> pkgSet = resolveInfos.stream().map(resolveInfo -> resolveInfo.activityInfo.packageName).collect(Collectors.toSet());
return pkgSet;
}
public void addAddPakcage(String packageName) {
this.addPackages.add(packageName);
mMMKV.encode(ADD_PACKAGE_KEY, addPackages);
}
public void removeAddPakcage(String packageName) {
this.addPackages.remove(packageName);
mMMKV.encode(ADD_PACKAGE_KEY, addPackages);
}
public Set<String> getAddPackages() {
Set<String> stringSet = mMMKV.decodeStringSet(ADD_PACKAGE_KEY, new HashSet<>());
return stringSet;
}
}