MD5 校验值:27cf44ec6e0720408f5ef017a90a3331
AppUtils.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。
package com.jb.ga0.commerce.util; import android.annotation.TargetApi; import android.app.Activity; import android.app.ActivityManager; import android.content.ActivityNotFoundException; import android.content.ComponentName; import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PermissionInfo; import android.content.pm.ProviderInfo; import android.content.pm.ResolveInfo; import android.content.pm.ServiceInfo; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.os.Process; import android.text.TextUtils; import com.jiubang.commerce.receiver.AppBroadcastReceiver; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; public class AppUtils { public static final String ACTION_SETTINGS = "android.settings.APPLICATION_DETAILS_SETTINGS"; private static final String TAG = "matt"; private static String sCurrProcessName = null; public static boolean isAppExist(Context context, String str) { if (context == null || TextUtils.isEmpty(str)) { return false; } try { context.getPackageManager().getPackageInfo(str, 1024); return true; } catch (PackageManager.NameNotFoundException e) { return false; } catch (Exception e2) { return false; } } public static int getAppVersionCode(Context context) { return getAppVersionCode(context, context.getPackageName()); } public static int getAppVersionCode(Context context, String str) { if (context == null || str == null) { return -1; } try { return context.getPackageManager().getPackageInfo(str, 1024).versionCode; } catch (PackageManager.NameNotFoundException e) { LogUtils.e(TAG, "Error :" + str + " is not exist."); return -1; } catch (Exception e2) { return -1; } } public static String getAppVersionName(Context context) { return context == null ? "" : getAppVersionName(context, context.getPackageName()); } public static String getAppVersionName(Context context, String str) { if (context == null || str == null) { return ""; } try { return context.getPackageManager().getPackageInfo(str, 1024).versionName; } catch (PackageManager.NameNotFoundException e) { LogUtils.e(TAG, "Error :" + str + " is not exist."); return ""; } catch (Exception e2) { return ""; } } public static String getAppLabel(Context context, String str) { if (str == null) { return null; } try { PackageManager packageManager = context.getPackageManager(); ApplicationInfo applicationInfo = packageManager.getApplicationInfo(str, 0); if (applicationInfo != null) { return packageManager.getApplicationLabel(applicationInfo).toString(); } return null; } catch (Exception e) { return null; } } public static String getPackageName(Intent intent) { ComponentName component; if (intent == null || (component = intent.getComponent()) == null) { return null; } return component.getPackageName(); } public static List<ResolveInfo> getLauncherApps(Context context) { PackageManager packageManager = context.getPackageManager(); Intent intent = new Intent("android.intent.action.MAIN"); intent.addCategory("android.intent.category.LAUNCHER"); return packageManager.queryIntentActivities(intent, 0); } public static List<PackageInfo> getLauncherableApps(Context context) { PackageManager packageManager = context.getPackageManager(); String packageName = context.getPackageName(); List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0); ArrayList arrayList = new ArrayList(); for (PackageInfo packageInfo : installedPackages) { if (packageManager.getLaunchIntentForPackage(packageInfo.applicationInfo.packageName) != null && !packageName.equals(packageInfo.packageName)) { arrayList.add(packageInfo); } } return arrayList; } public static int getPidByProcessName(Context context, String str) { for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : ((ActivityManager) context.getSystemService("activity")).getRunningAppProcesses()) { if (runningAppProcessInfo.processName.equals(str)) { return runningAppProcessInfo.pid; } } return 0; } public static void killProcess() { killProcess(Process.myPid()); } public static void killProcess(int i) { new Exception().printStackTrace(); Process.killProcess(i); } public static String getSelfTopActivityName(Context context) { try { List<ActivityManager.RunningTaskInfo> runningTasks = ((ActivityManager) context.getSystemService("activity")).getRunningTasks(1); if (runningTasks.size() > 0) { return runningTasks.get(0).topActivity.getClassName(); } } catch (Exception e) { } return null; } public static String getDefaultLauncher(Context context) { Intent intent = new Intent("android.intent.action.MAIN"); intent.addCategory("android.intent.category.HOME"); ResolveInfo resolveActivity = context.getPackageManager().resolveActivity(intent, 65536); if (resolveActivity.activityInfo == null || resolveActivity.activityInfo.packageName.equals("android")) { return null; } return resolveActivity.activityInfo.packageName; } public static Drawable getApkIcon(Context context, String str) { PackageManager packageManager = context.getPackageManager(); PackageInfo packageArchiveInfo = packageManager.getPackageArchiveInfo(str, 1); if (packageArchiveInfo != null) { ApplicationInfo applicationInfo = packageArchiveInfo.applicationInfo; applicationInfo.sourceDir = str; applicationInfo.publicSourceDir = str; try { return applicationInfo.loadIcon(packageManager); } catch (OutOfMemoryError e) { } } return null; } public static void installApk(Context context, File file) { if (file.exists()) { Intent intent = new Intent("android.intent.action.VIEW"); intent.setFlags(268435456); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); ((ContextWrapper) context).startActivity(intent); } } public static void uninstallApk(Context context, String str) { Intent intent = new Intent("android.intent.action.DELETE", Uri.parse("package:" + str)); if (!"Xiaomi".equals(Build.BRAND)) { intent.setFlags(1073741824); } context.startActivity(intent); } public static List<PackageInfo> getInstalledApps(Context context) { PackageManager packageManager = context.getPackageManager(); String packageName = context.getPackageName(); List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0); ArrayList arrayList = new ArrayList(); for (PackageInfo packageInfo : installedPackages) { if ((packageInfo.applicationInfo.flags & 1) == 0 && !packageName.equals(packageInfo.packageName)) { arrayList.add(packageInfo); } } return arrayList; } public static List<PackageInfo> getAllInstalledApps(Context context) { return context.getPackageManager().getInstalledPackages(0); } public static PackageInfo getAppPackageInfo(Context context, String str) { try { return context.getPackageManager().getPackageInfo(str, 0); } catch (Exception e) { return null; } } public static boolean safeStartActivity(Context context, String str) { PackageInfo appPackageInfo; if (context != null) { try { if (!TextUtils.isEmpty(str)) { Intent launchIntentForPackage = context.getPackageManager().getLaunchIntentForPackage(str); if (launchIntentForPackage == null && (appPackageInfo = getAppPackageInfo(context, str)) != null && !TextUtils.isEmpty(appPackageInfo.applicationInfo.className)) { launchIntentForPackage = new Intent(); launchIntentForPackage.addFlags(268435456); launchIntentForPackage.setClassName(str, appPackageInfo.applicationInfo.className); } if (launchIntentForPackage != null) { context.startActivity(launchIntentForPackage); return true; } } } catch (ActivityNotFoundException e) { } catch (Exception e2) { } } return false; } public static void safeStartActivity(Context context, Intent intent) { if (context != null) { try { context.startActivity(intent); } catch (ActivityNotFoundException e) { LogUtils.e(TAG, "saveStartActivity err " + e.getMessage()); } catch (SecurityException e2) { LogUtils.e(TAG, "saveStartActivity err " + e2.getMessage()); } catch (Exception e3) { LogUtils.e(TAG, "saveStartActivity err " + e3.getMessage()); } } } public static void safeStartActivityForResult(Activity activity, Intent intent, int i) { if (activity != null) { try { activity.startActivityForResult(intent, i); } catch (ActivityNotFoundException e) { LogUtils.e(TAG, "saveStartActivityForResult err " + e.getMessage()); } catch (SecurityException e2) { LogUtils.e(TAG, "saveStartActivityForResult err " + e2.getMessage()); } } } public static String getCurrProcessName(Context context) { if (sCurrProcessName == null) { sCurrProcessName = getCurrProcessNameSystem(context); } return sCurrProcessName; } private static String getCurrProcessNameSystem(Context context) { BufferedReader bufferedReader; Process process; String readLine; BufferedReader bufferedReader2 = null; int myPid = Process.myPid(); List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = ((ActivityManager) context.getSystemService("activity")).getRunningAppProcesses(); if (runningAppProcesses != null) { for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) { if (runningAppProcessInfo.pid == myPid) { return runningAppProcessInfo.processName; } } } try { process = Runtime.getRuntime().exec("ps " + myPid); try { bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); try { try { } catch (Throwable th) { th = th; th.printStackTrace(); if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (process != null) { process.destroy(); } return null; } } catch (Throwable th2) { th = th2; bufferedReader2 = bufferedReader; if (bufferedReader2 != null) { try { bufferedReader2.close(); } catch (IOException e2) { e2.printStackTrace(); } } if (process != null) { process.destroy(); } throw th; } } catch (Throwable th3) { th = th3; if (bufferedReader2 != null) { } if (process != null) { } throw th; } } catch (Throwable th4) { th = th4; bufferedReader = null; process = null; } if (bufferedReader.readLine() == null || (readLine = bufferedReader.readLine()) == null) { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e3) { e3.printStackTrace(); } } if (process != null) { process.destroy(); } return null; } String str = readLine.split("\\s+", Integer.MAX_VALUE)[r0.length - 1]; if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e4) { e4.printStackTrace(); } } if (process == null) { return str; } process.destroy(); return str; } @Deprecated public static boolean isAppRunningInForground(Context context, String str) { return Build.VERSION.SDK_INT >= 21 ? isForgroundApp(context, str) : isTopActivity(context, str); } @Deprecated public static String getTopAppPackageName(Context context) { try { List<ActivityManager.RunningTaskInfo> runningTasks = ((ActivityManager) context.getSystemService("activity")).getRunningTasks(1); if (runningTasks.size() > 0) { return runningTasks.get(0).topActivity.getPackageName(); } } catch (Exception e) { } return null; } @Deprecated private static boolean isTopActivity(Context context, String str) { try { List<ActivityManager.RunningTaskInfo> runningTasks = ((ActivityManager) context.getSystemService("activity")).getRunningTasks(1); if (runningTasks.size() > 0) { if (str.equals(runningTasks.get(0).topActivity.getPackageName())) { return true; } } } catch (Exception e) { } return false; } @Deprecated private static boolean isForgroundApp(Context context, String str) { List<ActivityManager.RunningAppProcessInfo> runningAppProcesses; try { runningAppProcesses = ((ActivityManager) context.getSystemService("activity")).getRunningAppProcesses(); } catch (Exception e) { } if (runningAppProcesses == null) { return false; } for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) { if (runningAppProcessInfo.importance == 100 && (runningAppProcessInfo.processName.equals(str) || Arrays.asList(runningAppProcessInfo.pkgList).contains(str))) { return true; } } return false; } @Deprecated public static String getTopAppPkgName(Context context) { if (Machine.IS_SDK_ABOVE_L) { return null; } try { List<ActivityManager.RunningTaskInfo> runningTasks = ((ActivityManager) context.getSystemService("activity")).getRunningTasks(1); if (runningTasks.size() > 0) { return runningTasks.get(0).topActivity.getPackageName(); } } catch (Exception e) { } return null; } @TargetApi(12) public static boolean isAppStop(Context context, String str) { try { return (context.getPackageManager().getApplicationInfo(str, 0).flags & 2097152) != 0; } catch (Exception e) { e.printStackTrace(); return true; } } public static boolean isAppRunning(Context context, String str) { Set<String> runningApps = getRunningApps(context); return runningApps != null && runningApps.contains(str); } public static Set<String> getRunningApps(Context context) { return Build.VERSION.SDK_INT >= 21 ? ProcessHelperUtil.getRunningAppProcesses(context).keySet() : getRunningAppsBelowApiLevel20(context); } private static Set<String> getRunningAppsBelowApiLevel20(Context context) { HashSet hashSet = new HashSet(); for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : ((ActivityManager) context.getSystemService("activity")).getRunningAppProcesses()) { if (runningAppProcessInfo.pkgList != null) { String[] strArr = runningAppProcessInfo.pkgList; for (String str : strArr) { hashSet.add(str); } } } return hashSet; } public static int getUidByPkgName(Context context, String str) { try { return context.getPackageManager().getApplicationInfo(str, 1).uid; } catch (Exception e) { e.printStackTrace(); return 0; } } public static boolean isServiceRunning(Context context, String str, String str2) { return isServiceRunning((ActivityManager) context.getSystemService("activity"), str, str2); } public static boolean isServiceRunning(ActivityManager activityManager, String str, String str2) { List<ActivityManager.RunningServiceInfo> runningServices = activityManager.getRunningServices(Integer.MAX_VALUE); int size = runningServices == null ? 0 : runningServices.size(); for (int i = 0; i < size; i++) { ActivityManager.RunningServiceInfo runningServiceInfo = runningServices.get(i); if (runningServiceInfo != null && runningServiceInfo.service != null) { String packageName = runningServiceInfo.service.getPackageName(); String className = runningServiceInfo.service.getClassName(); if (packageName != null && packageName.contains(str) && className != null && className.contains(str2)) { LogUtils.i("Notification", "package = " + runningServiceInfo.service.getPackageName() + " class = " + runningServiceInfo.service.getClassName()); return true; } } } return false; } public static boolean gotoBrowser(Context context, String str) { Uri parse; if (str == null || (parse = Uri.parse(str)) == null) { return false; } Intent intent = new Intent("android.intent.action.VIEW", parse); intent.setFlags(268435456); try { context.startActivity(intent); return true; } catch (ActivityNotFoundException e) { LogUtils.i(TAG, "gotoBrowser error, uri = " + str); return false; } catch (Exception e2) { LogUtils.i(TAG, "gotoBrowser error, uri = " + str); return false; } } public static List<ResolveInfo> getLauncherApps(Context context, String str) { PackageManager packageManager = context.getPackageManager(); Intent intent = new Intent(str); intent.addCategory("android.intent.category.LAUNCHER"); try { return packageManager.queryIntentActivities(intent, 0); } catch (Exception e) { e.printStackTrace(); return null; } catch (OutOfMemoryError e2) { e2.printStackTrace(); return null; } } public static void uninstallApp(Activity activity, Uri uri) { activity.startActivity(new Intent("android.intent.action.DELETE", uri)); } public static void uninstallPackage(Activity activity, String str) { uninstallApp(activity, Uri.parse("package:" + str)); } public static boolean isDefaultLauncher(Context context) { return context.getPackageName().equals(getDefaultLauncher(context)); } public static void showAppDetails(Context context, String str) { Intent intent = new Intent(); int i = Build.VERSION.SDK_INT; if (i >= 9) { intent.setAction(ACTION_SETTINGS); intent.setData(Uri.fromParts(AppBroadcastReceiver.DATA_SCHEME, str, null)); } else { String str2 = i == 8 ? "pkg" : "com.android.settings.ApplicationPkgName"; intent.setAction("android.intent.action.VIEW"); intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails"); intent.putExtra(str2, str); } try { context.startActivity(intent); } catch (Exception e) { e.printStackTrace(); } } public static boolean isInternalApp(Context context, Intent intent) { if (context == null) { return false; } PackageManager packageManager = context.getPackageManager(); try { String absolutePath = Environment.getDataDirectory().getAbsolutePath(); String str = packageManager.getActivityInfo(intent.getComponent(), 0).applicationInfo.publicSourceDir; if (str == null || str.length() <= 0) { return false; } return str.startsWith(absolutePath); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return false; } } public static boolean isSystemApp(Context context, Intent intent) { if (context == null) { return false; } try { ApplicationInfo applicationInfo = context.getPackageManager().getActivityInfo(intent.getComponent(), 0).applicationInfo; if (applicationInfo == null) { return false; } if ((applicationInfo.flags & 1) == 0) { if ((applicationInfo.flags & 128) == 0) { return false; } } return true; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return false; } } public static boolean isSystemApp(Context context, String str) { ApplicationInfo applicationInfo = getApplicationInfo(context, str); if (applicationInfo == null) { return false; } return ((applicationInfo.flags & 1) == 0 && (applicationInfo.flags & 128) == 0) ? false : true; } public static Context getAppContext(Context context, String str) { try { return context.createPackageContext(str, 2); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return null; } } public static boolean canReadGmailLabels(Context context) { boolean z; boolean z2 = false; try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo("com.google.android.gm", 4104); if (packageInfo.permissions != null) { int length = packageInfo.permissions.length; for (int i = 0; i < length; i++) { PermissionInfo permissionInfo = packageInfo.permissions[i]; if ("com.google.android.gm.permission.READ_CONTENT_PROVIDER".equals(permissionInfo.name) && permissionInfo.protectionLevel < 2) { z = true; break; } } } z = false; if (z && packageInfo.providers != null) { int length2 = packageInfo.providers.length; for (int i2 = 0; i2 < length2; i2++) { ProviderInfo providerInfo = packageInfo.providers[i2]; if ("com.google.android.gm".equals(providerInfo.authority) && TextUtils.equals("com.google.android.gm.permission.READ_CONTENT_PROVIDER", providerInfo.readPermission)) { z2 = true; } } } } catch (PackageManager.NameNotFoundException e) { } return z2; } public static boolean isBrowser(Context context, String str) { if (str == null) { return false; } Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addCategory("android.intent.category.BROWSABLE"); intent.setData(Uri.parse("http://3g.cn")); intent.setPackage(str); List<ResolveInfo> queryIntentActivities = context.getPackageManager().queryIntentActivities(intent, 0); return queryIntentActivities != null && queryIntentActivities.size() > 0; } public static List<ResolveInfo> filterPkgs(List<ResolveInfo> list, String[] strArr) { if (list != null && list.size() != 0 && strArr != null && strArr.length != 0) { ArrayList arrayList = new ArrayList(list); for (ResolveInfo resolveInfo : list) { if (resolveInfo != null && resolveInfo.activityInfo != null && resolveInfo.activityInfo.packageName != null) { int length = strArr.length; int i = 0; while (true) { if (i < length) { if (!resolveInfo.activityInfo.packageName.equals(strArr[i])) { i++; } else { arrayList.remove(resolveInfo); break; } } } } } return arrayList; } return list; } public static ServiceInfo getServiceInfo(Context context, ComponentName componentName) { try { return context.getPackageManager().getServiceInfo(componentName, 4); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return null; } } public static ApplicationInfo getApplicationInfo(Context context, String str) { if (str == null) { return null; } try { return context.getPackageManager().getApplicationInfo(str, 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return null; } } public static Drawable getAppIcon(Context context, String str) { if (str == null) { return null; } try { PackageManager packageManager = context.getPackageManager(); ApplicationInfo applicationInfo = packageManager.getApplicationInfo(str, 0); if (applicationInfo != null) { return applicationInfo.loadIcon(packageManager); } return null; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return null; } catch (OutOfMemoryError e2) { e2.printStackTrace(); return null; } } public static String getAppLable(Context context, String str) { if (str == null) { return null; } try { PackageManager packageManager = context.getPackageManager(); ApplicationInfo applicationInfo = packageManager.getApplicationInfo(str, 0); if (applicationInfo != null) { return packageManager.getApplicationLabel(applicationInfo).toString(); } return null; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return null; } } public static List<ApplicationInfo> getInstallAppInfo(Context context) { if (context == null) { return null; } return context.getPackageManager().getInstalledApplications(8192); } public static long getApkSize(PackageManager packageManager, String str) { if (TextUtils.isEmpty(str) || packageManager == null) { return 0L; } try { ApplicationInfo applicationInfo = packageManager.getApplicationInfo(str, 0); if (applicationInfo == null || applicationInfo.sourceDir == null) { return 0L; } return new File(applicationInfo.sourceDir).length(); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return 0L; } } public static boolean isInstallOnSDCard(PackageManager packageManager, String str) { if (TextUtils.isEmpty(str) || packageManager == null) { return false; } try { return (packageManager.getApplicationInfo(str, 0).flags & 262144) != 0; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return false; } } }