|
@@ -0,0 +1,100 @@
|
|
|
+package me.yoqi.android.appmanager.model;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.content.pm.ApplicationInfo;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.content.pm.ResolveInfo;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author liuyuqi.gov@msn.cn
|
|
|
+ * @createTime 2020-10-01
|
|
|
+ */
|
|
|
+public class AppModel{
|
|
|
+
|
|
|
+ public static final int FILTER_ALL_APP = 0; // 所有应用程序
|
|
|
+ public static final int FILTER_SYSTEM_APP = 1; // 系统程序
|
|
|
+ public static final int FILTER_THIRD_APP = 2; // 第三方应用程序
|
|
|
+ public static final int FILTER_SDCARD_APP = 3; // 安装在SDCard的应用程序
|
|
|
+
|
|
|
+ private static PackageManager pm;
|
|
|
+
|
|
|
+ // 根据查询条件,查询特定的ApplicationInfo
|
|
|
+ public static List<AppInfo> queryFilterAppInfo(Context context, int filter) {
|
|
|
+ pm = context.getPackageManager();
|
|
|
+ // 查询所有已经安装的应用程序
|
|
|
+ List<ApplicationInfo> listAppcations = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
|
|
|
+ Collections.sort(listAppcations, new ApplicationInfo.DisplayNameComparator(pm));// 排序
|
|
|
+ List<AppInfo> appInfos = new ArrayList<AppInfo>(); // 保存过滤查到的AppInfo
|
|
|
+ // 根据条件来过滤
|
|
|
+ switch (filter) {
|
|
|
+ case FILTER_ALL_APP: // 所有应用程序
|
|
|
+ appInfos.clear();
|
|
|
+ for (ApplicationInfo app : listAppcations) {
|
|
|
+ appInfos.add(getAppInfo(context,app));
|
|
|
+ }
|
|
|
+ return appInfos;
|
|
|
+ case FILTER_SYSTEM_APP: // 系统程序
|
|
|
+ appInfos.clear();
|
|
|
+ for (ApplicationInfo app : listAppcations) {
|
|
|
+ if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
|
|
|
+ appInfos.add(getAppInfo(context,app));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return appInfos;
|
|
|
+ case FILTER_THIRD_APP: // 第三方应用程序
|
|
|
+ appInfos.clear();
|
|
|
+ for (ApplicationInfo app : listAppcations) {
|
|
|
+ //非系统程序
|
|
|
+ if ((app.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
|
|
|
+ appInfos.add(getAppInfo(context,app));
|
|
|
+ }
|
|
|
+ //本来是系统程序,被用户手动更新后,该系统程序也成为第三方应用程序了
|
|
|
+ else if ((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0){
|
|
|
+ appInfos.add(getAppInfo(context,app));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case FILTER_SDCARD_APP: // 安装在SDCard的应用程序
|
|
|
+ appInfos.clear();
|
|
|
+ for (ApplicationInfo app : listAppcations) {
|
|
|
+ if ((app.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
|
|
|
+ appInfos.add(getAppInfo(context,app));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return appInfos;
|
|
|
+ default:
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return appInfos;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构造一个AppInfo对象 ,并赋值
|
|
|
+ private static AppInfo getAppInfo(Context context,ApplicationInfo app) {
|
|
|
+ AppInfo appInfo = new AppInfo();
|
|
|
+ appInfo.setAppName((String) app.loadLabel(pm));
|
|
|
+ appInfo.setAppIcon(app.loadIcon(pm));
|
|
|
+ appInfo.setPackageName(app.packageName);
|
|
|
+ appInfo.setLuancherActivity(getLauncherActivityNameByPackageName(context,appInfo.getPackageName()));
|
|
|
+ return appInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getLauncherActivityNameByPackageName(Context context, String packageName) {
|
|
|
+ String className = null;
|
|
|
+ Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);//android.intent.action.MAIN
|
|
|
+ resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);//android.intent.category.LAUNCHER
|
|
|
+ resolveIntent.setPackage(packageName);
|
|
|
+ List<ResolveInfo> resolveinfoList = context.getPackageManager().queryIntentActivities(resolveIntent, 0);
|
|
|
+ if (resolveinfoList != null && resolveinfoList.size() > 0) {
|
|
|
+ ResolveInfo resolveinfo = resolveinfoList.iterator().next();
|
|
|
+ if (resolveinfo != null) {
|
|
|
+ className = resolveinfo.activityInfo.name;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return className;
|
|
|
+ }
|
|
|
+}
|