version:1.4.3
fix: update:增加添加应用,实时更新信息
This commit is contained in:
255
app/src/main/java/com/uiui/zyos/manager/AppManager.java
Normal file
255
app/src/main/java/com/uiui/zyos/manager/AppManager.java
Normal file
@@ -0,0 +1,255 @@
|
||||
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.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.class.getSimpleName();
|
||||
|
||||
public static final String ADD_NAME = "com.zyos.add";
|
||||
private static final String SHOW_PACKAGE_KEY = "SHOW_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;
|
||||
|
||||
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.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());
|
||||
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<>();
|
||||
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<>();
|
||||
for (ResolveInfo applicationInfo : resolveInfos) {
|
||||
desktopIcons.add(DesktopIcon.creatDesktopIcon(mContext, applicationInfo));
|
||||
}
|
||||
return desktopIcons;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user