version:1.9
fix: update:修改包名,添加推送
This commit is contained in:
469
app/src/main/java/com/uiui/aios/utils/ApkUtils.java
Normal file
469
app/src/main/java/com/uiui/aios/utils/ApkUtils.java
Normal file
@@ -0,0 +1,469 @@
|
||||
package com.uiui.aios.utils;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageInstaller;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import com.uiui.aios.BuildConfig;
|
||||
import com.uiui.aios.bean.DesktopIcon;
|
||||
import com.uiui.aios.receiver.InstallResultReceiver;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.text.Collator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.rxjava3.core.Observable;
|
||||
import io.reactivex.rxjava3.core.ObservableEmitter;
|
||||
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
|
||||
import io.reactivex.rxjava3.core.Observer;
|
||||
import io.reactivex.rxjava3.disposables.Disposable;
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||
|
||||
public class ApkUtils {
|
||||
private static HashSet<String> excludePackageName = new HashSet<String>() {{
|
||||
this.add(BuildConfig.APPLICATION_ID);
|
||||
this.add("org.chromium.browser");
|
||||
this.add("com.sprd.sprdnote");
|
||||
this.add("com.android.deskclock");
|
||||
this.add("com.alldocube.store");
|
||||
this.add("com.android.email");
|
||||
this.add("com.android.calendar");
|
||||
}};
|
||||
private static HashSet<String> showPackageName = new HashSet<String>() {{
|
||||
this.add("com.android.dialer");
|
||||
this.add("com.android.gallery3d");
|
||||
this.add("com.android.settings");
|
||||
this.add("com.android.messaging");
|
||||
this.add("com.android.camera2");
|
||||
this.add("com.mediatek.camera");
|
||||
this.add("com.android.mms");
|
||||
}};
|
||||
private static HashSet<String> allHintPackage = new HashSet<String>() {{
|
||||
this.add("com.android.uiuios");
|
||||
}};
|
||||
private static String TAG = ApkUtils.class.getSimpleName();
|
||||
|
||||
public static ArrayList<ApplicationInfo> getSystemApp(Context context) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
// 查询所有已经安装的应用程序
|
||||
List<ApplicationInfo> appInfos = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);// GET_UNINSTALLED_PACKAGES代表已删除,但还有安装目录的
|
||||
ArrayList<ApplicationInfo> applicationInfos = 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 = new HashSet();
|
||||
for (ResolveInfo resolveInfo : resolveinfoList) {
|
||||
Log.i(TAG, "queryFilterAppInfo: " + resolveInfo.activityInfo.packageName);
|
||||
allowPackages.add(resolveInfo.activityInfo.packageName);
|
||||
}
|
||||
for (ApplicationInfo app : appInfos) {
|
||||
if (allHintPackage.contains(app.packageName)) continue;
|
||||
if ((app.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
|
||||
if (allowPackages.contains(app.packageName)
|
||||
&& !showPackageName.contains(app.packageName)) {
|
||||
applicationInfos.add(app);
|
||||
Log.e(TAG, "getSystemApp: " + app.packageName);
|
||||
}
|
||||
}
|
||||
}
|
||||
applicationInfos.sort(new Comparator<ApplicationInfo>() {
|
||||
@Override
|
||||
public int compare(ApplicationInfo o1, ApplicationInfo o2) {
|
||||
return Collator.getInstance(Locale.CHINESE).compare(o1.loadLabel(pm).toString(), o2.loadLabel(pm).toString());
|
||||
// return o1.loadLabel(pm).toString().compareTo(o2.loadLabel(pm).toString());
|
||||
}
|
||||
});
|
||||
applicationInfos.sort(new Comparator<ApplicationInfo>() {
|
||||
@Override
|
||||
public int compare(ApplicationInfo o1, ApplicationInfo o2) {
|
||||
if ((o1.flags & ApplicationInfo.FLAG_SYSTEM) <= (o2.flags & ApplicationInfo.FLAG_SYSTEM)) {
|
||||
return 1;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
});
|
||||
return applicationInfos;
|
||||
}
|
||||
|
||||
private static List<String> defaultSort = new ArrayList<String>() {{
|
||||
this.add("com.android.dialer");
|
||||
this.add("com.android.messaging");
|
||||
this.add("com.android.gallery3d");
|
||||
this.add("com.android.camera2");
|
||||
this.add("com.android.settings");
|
||||
this.add("com.uiui.sn");
|
||||
this.add("com.uiui.appstore");
|
||||
}};
|
||||
|
||||
/**
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
public static ArrayList<DesktopIcon> queryFilterAppInfo(Context context) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
// 查询所有已经安装的应用程序
|
||||
List<ApplicationInfo> appInfos = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);// GET_UNINSTALLED_PACKAGES代表已删除,但还有安装目录的
|
||||
ArrayList<ApplicationInfo> applicationInfos = 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 = new HashSet();
|
||||
for (ResolveInfo resolveInfo : resolveinfoList) {
|
||||
Log.i(TAG, "queryFilterAppInfo: " + resolveInfo.activityInfo.packageName);
|
||||
allowPackages.add(resolveInfo.activityInfo.packageName);
|
||||
}
|
||||
|
||||
for (ApplicationInfo app : appInfos) {
|
||||
if ((app.flags & ApplicationInfo.FLAG_SYSTEM) > 0)//通过flag排除系统应用,会将电话、短信也排除掉
|
||||
{
|
||||
if (showPackageName.contains(app.packageName)) {
|
||||
applicationInfos.add(app);
|
||||
}
|
||||
} else {
|
||||
// if(app.uid > 10000){//通过uid排除系统应用,在一些手机上效果不好
|
||||
// applicationInfos.add(app);
|
||||
// }
|
||||
if (allowPackages.contains(app.packageName) && !excludePackageName.contains(app.packageName)) {
|
||||
// if (allowPackages.contains(app.packageName)) {
|
||||
applicationInfos.add(app);
|
||||
}
|
||||
}
|
||||
}
|
||||
applicationInfos.sort(new Comparator<ApplicationInfo>() {
|
||||
@Override
|
||||
public int compare(ApplicationInfo o1, ApplicationInfo o2) {
|
||||
return Collator.getInstance(Locale.CHINESE).compare(o1.loadLabel(pm).toString(), o2.loadLabel(pm).toString());
|
||||
// return o1.loadLabel(pm).toString().compareTo(o2.loadLabel(pm).toString());
|
||||
}
|
||||
});
|
||||
applicationInfos.sort(new Comparator<ApplicationInfo>() {
|
||||
@Override
|
||||
public int compare(ApplicationInfo o1, ApplicationInfo o2) {
|
||||
if ((o1.flags & ApplicationInfo.FLAG_SYSTEM) <= (o2.flags & ApplicationInfo.FLAG_SYSTEM)) {
|
||||
return 1;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
});
|
||||
ArrayList<DesktopIcon> desktopIcons = new ArrayList<>();
|
||||
HashMap<String, ApplicationInfo> infoHashMap = new HashMap<>();
|
||||
for (ApplicationInfo applicationInfo : applicationInfos) {
|
||||
infoHashMap.put(applicationInfo.packageName, applicationInfo);
|
||||
}
|
||||
for (int i = 0; i < defaultSort.size(); i++) {
|
||||
ApplicationInfo info = infoHashMap.get(defaultSort.get(i));
|
||||
if (info != null) {
|
||||
desktopIcons.add(DesktopIcon.creatDesktopIcon(context, info, i));
|
||||
infoHashMap.remove(defaultSort.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
for (ApplicationInfo applicationInfo : infoHashMap.values()) {
|
||||
desktopIcons.add(DesktopIcon.creatDesktopIcon(context, applicationInfo));
|
||||
}
|
||||
return desktopIcons;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取第三方应用
|
||||
*
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
public static List<String> queryFilterAppList(Context context) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
// 查询所有已经安装的应用程序
|
||||
List<ApplicationInfo> appInfos = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);// GET_UNINSTALLED_PACKAGES代表已删除,但还有安装目录的
|
||||
List<String> applicationInfos = new ArrayList<>();
|
||||
for (ApplicationInfo app : appInfos) {
|
||||
// Log.e("queryFilterAppInfo", String.valueOf(app.flags));
|
||||
// Log.e("queryFilterAppInfo", String.valueOf((app.flags & mask)));
|
||||
if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
|
||||
//通过flag排除系统应用,会将电话、短信也排除掉
|
||||
} else {
|
||||
applicationInfos.add(app.packageName);
|
||||
Log.e("queryFilterAppInfo", app.packageName);
|
||||
}
|
||||
}
|
||||
return applicationInfos;
|
||||
}
|
||||
|
||||
public static PackageInfo getPackageInfo(Context context, String pkg) {
|
||||
PackageManager packageManager = context.getPackageManager();
|
||||
PackageInfo packageInfo = null;
|
||||
try {
|
||||
packageInfo = packageManager.getPackageInfo(pkg, 0);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return packageInfo;
|
||||
}
|
||||
|
||||
public static ApplicationInfo getApplicationInfo(Context context, String pkg) {
|
||||
PackageManager packageManager = context.getPackageManager();
|
||||
ApplicationInfo applicationInfo = null;
|
||||
try {
|
||||
applicationInfo = packageManager.getApplicationInfo(pkg, 0);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return applicationInfo;
|
||||
}
|
||||
|
||||
public static void openApp(Context context, String packageName) {
|
||||
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
|
||||
if (intent != null) {
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(intent);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean openPackage(Context context, String packageName) {
|
||||
Context pkgContext = getPackageContext(context, packageName);
|
||||
Intent intent = getAppOpenIntentByPackageName(context, packageName);
|
||||
if (pkgContext != null && intent != null) {
|
||||
pkgContext.startActivity(intent);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Context getPackageContext(Context context, String packageName) {
|
||||
Context pkgContext = null;
|
||||
if (context.getPackageName().equals(packageName)) {
|
||||
pkgContext = context;
|
||||
} else {
|
||||
// 创建第三方应用的上下文环境
|
||||
try {
|
||||
pkgContext = context.createPackageContext(packageName,
|
||||
Context.CONTEXT_IGNORE_SECURITY
|
||||
| Context.CONTEXT_INCLUDE_CODE);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return pkgContext;
|
||||
}
|
||||
|
||||
public static Intent getAppOpenIntentByPackageName(Context context, String packageName) {
|
||||
//Activity完整名
|
||||
String mainAct = null;
|
||||
//根据包名寻找
|
||||
PackageManager pkgMag = context.getPackageManager();
|
||||
Intent intent = new Intent(Intent.ACTION_MAIN);
|
||||
intent.addCategory(Intent.CATEGORY_LAUNCHER);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
|
||||
List<ResolveInfo> list = pkgMag.queryIntentActivities(intent,
|
||||
PackageManager.GET_ACTIVITIES);
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
ResolveInfo info = list.get(i);
|
||||
if (info.activityInfo.packageName.equals(packageName)) {
|
||||
mainAct = info.activityInfo.name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (TextUtils.isEmpty(mainAct)) {
|
||||
return null;
|
||||
}
|
||||
intent.setComponent(new ComponentName(packageName, mainAct));
|
||||
return intent;
|
||||
}
|
||||
|
||||
public static String getAPPVersionName(Context context, String packageName) {
|
||||
String versionName = "0";
|
||||
|
||||
if (TextUtils.isEmpty(packageName)) {
|
||||
return versionName;
|
||||
}
|
||||
PackageManager pm = context.getPackageManager();
|
||||
try {
|
||||
PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
|
||||
versionName = packageInfo.versionName;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return versionName;
|
||||
}
|
||||
|
||||
public static String getAppNameByPackage(Context context, String pkg) {
|
||||
PackageManager packageManager = context.getPackageManager();
|
||||
try {
|
||||
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(pkg, PackageManager.GET_META_DATA);
|
||||
String packageName = packageManager.getApplicationLabel(applicationInfo).toString();
|
||||
return packageName;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查手机上是否安装了指定的软件
|
||||
*/
|
||||
public static boolean isAvailable(Context context, String packageName) {
|
||||
PackageManager packageManager = context.getPackageManager();
|
||||
PackageInfo info = null;
|
||||
try {
|
||||
info = packageManager.getPackageInfo(packageName, 0);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return info != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查手机上是否安装了指定的软件
|
||||
*/
|
||||
public static boolean isAvailable(Context context, File file) {
|
||||
return isAvailable(context, getPackageName(context, file.getAbsolutePath()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件路径获取包名
|
||||
*/
|
||||
public static String getPackageName(Context context, String filePath) {
|
||||
PackageManager packageManager = context.getPackageManager();
|
||||
PackageInfo info = packageManager.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
|
||||
if (info != null) {
|
||||
ApplicationInfo appInfo = info.applicationInfo;
|
||||
return appInfo.packageName; //得到安装包名称
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 静默卸载应用
|
||||
*
|
||||
* @param context
|
||||
* @param pkg
|
||||
*/
|
||||
public static void UninstallAPP(Context context, String pkg) {
|
||||
Observable.create(new ObservableOnSubscribe<String>() {
|
||||
@Override
|
||||
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
|
||||
Log.e("UninstallAPP", "call: " + Thread.currentThread().getName());
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
ApkUtils.uninstall(context, pkg);
|
||||
} else {
|
||||
ApkUtils.deleteApkInSilence(pkg);
|
||||
}
|
||||
emitter.onNext(pkg);
|
||||
}
|
||||
}).subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<String>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
Log.e("UninstallAPP", "onSubscribe: ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(String s) {
|
||||
Log.e("UninstallAPP", "onNext: " + Thread.currentThread().getName());
|
||||
Log.e("UninstallAPP", "onNext: " + s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
Log.e("UninstallAPP", "onError: " + e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
Log.e("UninstallAPP", "onComplete: ");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据包名卸载应用
|
||||
*
|
||||
* @param packageName 包名
|
||||
*/
|
||||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
||||
public static void uninstall(Context context, String packageName) {
|
||||
Intent broadcastIntent = new Intent(context, InstallResultReceiver.class);
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1,
|
||||
broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
|
||||
packageInstaller.uninstall(packageName, pendingIntent.getIntentSender());
|
||||
}
|
||||
|
||||
public static void deleteApkInSilence(String packageName) {
|
||||
Class<?> pmService;
|
||||
Class<?> activityTherad;
|
||||
Method method;
|
||||
try {
|
||||
activityTherad = Class.forName("android.app.ActivityThread");
|
||||
Class<?> paramTypes[] = getParamTypes(activityTherad, "getPackageManager");
|
||||
method = activityTherad.getMethod("getPackageManager", paramTypes);
|
||||
Object PackageManagerService = method.invoke(activityTherad);
|
||||
pmService = PackageManagerService.getClass();
|
||||
Class<?> paramTypes1[] = getParamTypes(pmService, "deletePackageAsUser");
|
||||
method = pmService.getMethod("deletePackageAsUser", paramTypes1);
|
||||
//getUserId
|
||||
method.invoke(PackageManagerService, packageName, null, getUserId(Binder.getCallingUid()), 0x00000040);
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static Class<?>[] getParamTypes(Class<?> cls, String mName) {
|
||||
Class<?> cs[] = null;
|
||||
Method[] mtd = cls.getMethods();
|
||||
for (int i = 0; i < mtd.length; i++) {
|
||||
if (!mtd[i].getName().equals(mName)) {
|
||||
continue;
|
||||
}
|
||||
cs = mtd[i].getParameterTypes();
|
||||
}
|
||||
return cs;
|
||||
}
|
||||
|
||||
public static final int PER_USER_RANGE = 100000;
|
||||
|
||||
public static int getUserId(int uid) {
|
||||
return uid / PER_USER_RANGE;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user