294 lines
13 KiB
Java
294 lines
13 KiB
Java
package com.uiui.zyos.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.uiui.zyos.R;
|
||
import com.uiui.zyos.bean.DesktopIcon;
|
||
import com.uiui.zyos.config.CommonConfig;
|
||
import com.uiui.zyos.utils.ApkUtils;
|
||
|
||
import java.text.Collator;
|
||
import java.util.ArrayList;
|
||
import java.util.Arrays;
|
||
import java.util.Comparator;
|
||
import java.util.HashSet;
|
||
import java.util.List;
|
||
import java.util.Locale;
|
||
import java.util.Set;
|
||
import java.util.function.Function;
|
||
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 APPSTORE_PACKAGE_NAME = "com.uiui.zyappstore";
|
||
public static final String APPSTORE_CLASS_NAME = "com.uiui.zyappstore.activity.MainActivity";
|
||
public static final String UPDATE_NAME = "com.zyos.update";
|
||
public static final String MANUAL_NAME = "com.zyos.manual";
|
||
public static final String SERVICE_NAME = "com.zyos.service";
|
||
private static final String SHOW_PACKAGE_KEY = "SHOW_PACKAGE_KEY";
|
||
public static final String BROWSER_PACKAGE_NAME = "com.uiui.zybrowser";
|
||
public static final String BROWSER_CLASS_NAME = "com.uiui.zybrowser.activity.main.MainActivity";
|
||
public static final String DESKTOP_EXIT = "com.zyos.exit";
|
||
|
||
|
||
@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;
|
||
|
||
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<String>(){{
|
||
this.add("com.jxw.yyhb");
|
||
this.add("com.jxw.souti");
|
||
}});
|
||
this.showPackages.removeIf(TextUtils::isEmpty);
|
||
}
|
||
|
||
public static AppManager getInstance() {
|
||
if (sInstance == null) {
|
||
throw new IllegalStateException("You must be init AppManager first");
|
||
}
|
||
return sInstance;
|
||
}
|
||
|
||
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, "queryFilterAppInfo: adminapp = " + adminApp);
|
||
for (ResolveInfo resolveInfo : resolveinfoList) {
|
||
String pkg = resolveInfo.activityInfo.packageName;
|
||
if (ApkUtils.appIsDisable(mContext, pkg)) {
|
||
Log.e(TAG, "queryFilterAppInfo: 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());
|
||
if (showPackages.contains("com.jxw.souti")) {
|
||
desktopIcons.add("com.jxw.souti");
|
||
}
|
||
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, "queryFilterAppInfo: adminapp = " + adminApp);
|
||
for (ResolveInfo resolveInfo : resolveinfoList) {
|
||
String pkg = resolveInfo.activityInfo.packageName;
|
||
if (ApkUtils.appIsDisable(mContext, pkg)) {
|
||
Log.e(TAG, "queryFilterAppInfo: 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<>();
|
||
if (showPackages.contains("com.jxw.souti")) {
|
||
if (ApkUtils.isAvailable(mContext, "com.jxw.souti")) {
|
||
DesktopIcon desktopIcon = new DesktopIcon();
|
||
desktopIcon.setLable("拍照搜题");
|
||
desktopIcon.setPackageName("com.jxw.souti");
|
||
desktopIcon.setClassName("com.jxw.souti.ui.activity.MainActivity");
|
||
desktopIcon.setIcon(mContext.getDrawable(R.drawable.icon_souti));
|
||
desktopIcons.add(desktopIcon);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
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, "queryFilterAppInfo: adminapp = " + adminApp);
|
||
for (ResolveInfo resolveInfo : resolveinfoList) {
|
||
String pkg = resolveInfo.activityInfo.packageName;
|
||
if (ApkUtils.appIsDisable(mContext, pkg)) {
|
||
Log.e(TAG, "queryFilterAppInfo: 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<>();
|
||
if (ApkUtils.isAvailable(mContext, "com.jxw.souti")) {
|
||
DesktopIcon desktopIcon = new DesktopIcon();
|
||
desktopIcon.setLable("拍照搜题");
|
||
desktopIcon.setPackageName("com.jxw.souti");
|
||
desktopIcon.setClassName("com.jxw.souti.ui.activity.MainActivity");
|
||
desktopIcon.setIcon(mContext.getDrawable(R.drawable.icon_souti));
|
||
desktopIcons.add(desktopIcon);
|
||
}
|
||
|
||
for (ResolveInfo resolveInfo : resolveInfos) {
|
||
desktopIcons.add(DesktopIcon.creatDesktopIcon(mContext, resolveInfo));
|
||
}
|
||
return desktopIcons;
|
||
}
|
||
|
||
}
|