private static PackageInfo findPackageInfo(PackageManager pm, String pkg, int uid) { final String[] packages = pm.getPackagesForUid(uid); if (packages != null && pkg != null) { final int N = packages.length; for (int i = 0; i < N; i++) { final String p = packages[i]; if (pkg.equals(p)) { try { return pm.getPackageInfo(pkg, PackageManager.GET_SIGNATURES); } catch (NameNotFoundException e) { Log.w(TAG, "Failed to load package " + pkg, e); } } } } return null; }
/** * 获得APP的名称 * * @param context * @return */ public static String getAppName(Context context) { if (context == null) { return null; } try { PackageManager packageManager = context.getPackageManager(); PackageInfo packageInfo = packageManager.getPackageInfo( context.getPackageName(), 0); int labelRes = packageInfo.applicationInfo.labelRes; String appName = context.getResources().getString(labelRes); return appName; } catch (NameNotFoundException e) { e.printStackTrace(); } return null; }
@SuppressWarnings("deprecation") private void dumpPhoneInfo(PrintWriter pw) throws PackageManager.NameNotFoundException { PackageManager pm = mContext.getPackageManager(); PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES); pw.print("App Version: "); pw.print(pi.versionName); pw.print('_'); pw.println(pi.versionCode); /* Android 版本号 */ pw.print("OS Version: "); pw.print(Build.VERSION.RELEASE); pw.print('_'); pw.println(SDK_INT); /* 手机制造商 */ pw.print("Vendor: "); pw.println(Build.MODEL); /* CPU 架构 */ pw.print("CPU ABI: "); pw.println(Build.CPU_ABI); }
private String getConstants() { StringBuilder res = new StringBuilder(); res.append("Last Device: ").append(lastDeviceDebugString).append('\n'); res.append("Build.MANUFACTURER: ").append(Build.MANUFACTURER).append('\n'); res.append("Build.MODEL: ").append(Build.MODEL).append('\n'); res.append("Build.PRODUCT: ").append(Build.PRODUCT).append('\n'); res.append("Build.VERSION.SDK_INT: ").append(Build.VERSION.SDK_INT).append('\n'); res.append("Build.VERSION.RELEASE: ").append(Build.VERSION.RELEASE).append('\n'); try { PackageInfo packageInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0); res.append("Driver versionName: ").append(packageInfo.versionName).append('\n'); res.append("Driver versionCode: ").append(packageInfo.versionCode).append('\n'); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return res.toString(); }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) @NonNull static String[] neededPermissions(Context context) { PackageInfo info = null; try { info = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS); } catch (PackageManager.NameNotFoundException e) { /* */ } List<String> needed = new ArrayList<>(); if (info != null && info.requestedPermissions != null && info.requestedPermissionsFlags != null) { for (int i = 0; i < info.requestedPermissions.length; i++) { int flags = info.requestedPermissionsFlags[i]; if ((flags & PackageInfo.REQUESTED_PERMISSION_GRANTED) == 0) { needed.add(info.requestedPermissions[i]); } } } return needed.toArray(new String[needed.size()]); }
private boolean isOverlayApp(PackageInfo packageInfo) { int mask = FLAG_SYSTEM | FLAG_UPDATED_SYSTEM_APP; // Ignore system apps if ((packageInfo.applicationInfo.flags & mask) != 0) { return false; } // Ignore Play Services, it's effectively a system app if (isPlayServices(packageInfo.packageName)) { return false; } // Get permissions String[] requestedPermissions = packageInfo.requestedPermissions; if (requestedPermissions != null) { for (String requestedPermission : requestedPermissions) { if (requestedPermission.equals(SYSTEM_ALERT_WINDOW)) { return true; } } } return false; }
protected void setActionBarTitle(){ if (isRootPage()){ String pkgName = getPackageName(); try { PackageInfo pkgInfo = getPackageManager().getPackageInfo(pkgName,0); CharSequence appLabel = getPackageManager().getApplicationLabel(pkgInfo.applicationInfo); getSupportActionBar().setTitle(appLabel.toString()); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return; } Class<? extends DUnitGroupInterface> currentGroup = getCurrentGroup(); ArrayList<DUnitGroupModel> groupModels = DUnitManager.getInstance().getUnitGroupModels(); for (DUnitGroupModel groupModel: groupModels) { if (groupModel.getOriginal() == currentGroup){ getSupportActionBar().setTitle(groupModel.getName()); return; } } }
public static void startShareApp(Activity activity) { try { String packageName = activity.getPackageName(); PackageInfo packageInfo = activity.getPackageManager().getPackageInfo(packageName, 0); String dir = packageInfo.applicationInfo.sourceDir; File tempFile = new File(dir); Intent i = new Intent(); i.setAction(Intent.ACTION_SEND); i.setDataAndType(Uri.fromFile(tempFile), "application/vnd.android.package-archive"); i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tempFile)); // Testing... //Only use bluetooth to share the app i.setPackage("com.android.bluetooth"); activity.startActivity(i); } catch (Exception e) { showLog("SHAREAPP: ", e.getMessage()); } }
private AppTarget(Context context) { mContext = context; mSharedPreferences = context.getSharedPreferences("va_settings", Context.MODE_MULTI_PROCESS); // 从系统已安装的apk中安装插件,默认为false fromSystem = mSharedPreferences.getBoolean("fromSystem", false); firstInstall = mSharedPreferences.getBoolean("install", false); mApkFile = new File(Environment.getExternalStorageDirectory(), "debug.apk").getAbsolutePath(); mUpdateFile = mApkFile; if (!firstInstall) { PackageInfo packageInfo = PackageUtils.getPackageInfo(context, PKG); // 如果从系统中查找该包名存在,说明系统中已安装过此apk, 则可以从系统中加载此插件. if (packageInfo != null) { fromSystem = true; mApkFile = packageInfo.applicationInfo.publicSourceDir; } } }
@Override public Object call(Object who, Method method, Object... args) throws Throwable { int flags = (int) args[0]; int userId = VUserHandle.myUserId(); List<PackageInfo> packageInfos; if (isAppProcess()) { packageInfos = new ArrayList<PackageInfo>(VirtualCore.get().getAppCount()); } else { packageInfos = VirtualCore.get().getUnHookPackageManager().getInstalledPackages(flags); } packageInfos.addAll(VPackageManager.get().getInstalledPackages(flags, userId)); if (ParceledListSliceCompat.isReturnParceledListSlice(method)) { return ParceledListSliceCompat.create(packageInfos); } else { return packageInfos; } }
private boolean isPlayServices(String pkg) { if (!PLAY_SERVICES_PACKAGE.equals(pkg)) return false; try { PackageInfo sigs = pm.getPackageInfo(pkg, GET_SIGNATURES); // The genuine Play Services app should have a single signature Signature[] signatures = sigs.signatures; if (signatures == null || signatures.length != 1) return false; // Extract the public key from the signature CertificateFactory certFactory = CertificateFactory.getInstance("X509"); byte[] signatureBytes = signatures[0].toByteArray(); InputStream in = new ByteArrayInputStream(signatureBytes); X509Certificate cert = (X509Certificate) certFactory.generateCertificate(in); byte[] publicKeyBytes = cert.getPublicKey().getEncoded(); String publicKey = StringUtils.toHexString(publicKeyBytes); return PLAY_SERVICES_PUBLIC_KEY.equals(publicKey); } catch (NameNotFoundException | CertificateException e) { if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); return false; } }
/** * 根据包名打开第三方应用 * * @param context * @param packageName * @throws PackageManager.NameNotFoundException */ public static void openAppByPackageName(Context context, String packageName) throws PackageManager.NameNotFoundException { PackageInfo pi; try { pi = MyApplication.getContext().getPackageManager().getPackageInfo(packageName, 0); Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null); resolveIntent.setPackage(pi.packageName); PackageManager pManager = MyApplication.getContext().getPackageManager(); List<ResolveInfo> apps = pManager.queryIntentActivities(resolveIntent, 0); ResolveInfo ri = apps.iterator().next(); if (ri != null) { packageName = ri.activityInfo.packageName; String className = ri.activityInfo.name; Intent intent = new Intent(Intent.ACTION_MAIN); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//重点是加这个 ComponentName cn = new ComponentName(packageName, className); intent.setComponent(cn); context.startActivity(intent); } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } }
public String getApkInfo(String fileName) { try { String dexPath = null; if (getExternalFilesDir(null) != null) { dexPath = new File(getExternalFilesDir(null), fileName).getAbsolutePath(); } else if (getFilesDir() != null) { dexPath = new File(getFilesDir(), fileName).getAbsolutePath(); } PackageManager pm = getPackageManager(); PackageInfo info = pm.getPackageArchiveInfo(dexPath, 0); StringBuilder sb = new StringBuilder(); sb.append("\n*** Apk info ***\n"); sb.append("versionCode:" + info.versionCode); sb.append("\nversionName:" + info.versionName); sb.append("\n*** Apk info ***\n"); return sb.toString(); } catch (Exception e) { e.printStackTrace(); return e.toString(); } }
public static PackageInfo generatePackageInfo(Package p, int flags, long firstInstallTime, long lastUpdateTime) { if (API_LEVEL >= M) { return PackageParserMarshmallow.generatePackageInfo.call(p, GIDS, flags, firstInstallTime, lastUpdateTime, null, sUserState); } else if (API_LEVEL >= LOLLIPOP) { if (PackageParserLollipop22.generatePackageInfo != null) { return PackageParserLollipop22.generatePackageInfo.call(p, GIDS, flags, firstInstallTime, lastUpdateTime, null, sUserState); } else { return PackageParserLollipop.generatePackageInfo.call(p, GIDS, flags, firstInstallTime, lastUpdateTime, null, sUserState); } } else if (API_LEVEL >= JELLY_BEAN_MR1) { return PackageParserJellyBean17.generatePackageInfo.call(p, GIDS, flags, firstInstallTime, lastUpdateTime, null, sUserState); } else if (API_LEVEL >= JELLY_BEAN) { return PackageParserJellyBean.generatePackageInfo.call(p, GIDS, flags, firstInstallTime, lastUpdateTime, null); } else { return mirror.android.content.pm.PackageParser.generatePackageInfo.call(p, GIDS, flags, firstInstallTime, lastUpdateTime); } }
/** * Check if Font Provider app is available. * * @param context Context * @return code * * @see FontProviderAvailability#OK * @see FontProviderAvailability#NOT_INSTALLED * @see FontProviderAvailability#DISABLED * @see FontProviderAvailability#VERSION_TOO_LOW */ public static @FontProviderAvailability int checkAvailability(Context context) { PackageInfo pi = null; try { pi = context.getPackageManager().getPackageInfo(PACKAGE, 0); } catch (PackageManager.NameNotFoundException ignored) { } if (pi == null) { return NOT_INSTALLED; } if (!pi.applicationInfo.enabled) { return DISABLED; } if (pi.versionCode < MIN_VERSION) { return VERSION_TOO_LOW; } return OK; }
/** * Check if the app requests a specific permission in the manifest. * * @param permissionName the permission to check * @return true - the permission in requested in manifest, false - not. */ public static boolean hasPermissionInManifest(@NonNull Context context, @NonNull String permissionName) { String packageName = context.getPackageName(); try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_PERMISSIONS); final String[] declaredPermisisons = packageInfo.requestedPermissions; if (declaredPermisisons != null && declaredPermisisons.length > 0) { for (String p : declaredPermisisons) { if (p.equalsIgnoreCase(permissionName)) { return true; } } } } catch (PackageManager.NameNotFoundException e) { } return false; }
/** * 获取一些简单的信息,软件版本,手机版本,型号等信息存放在HashMap中 * * @param context * @return */ private HashMap<String, String> obtainSimpleInfo(Context context) { HashMap<String, String> map = new HashMap<String, String>(); PackageManager mPackageManager = context.getPackageManager(); PackageInfo mPackageInfo = null; try { mPackageInfo = mPackageManager.getPackageInfo( context.getPackageName(), PackageManager.GET_ACTIVITIES); } catch (NameNotFoundException e) { e.printStackTrace(); } map.put("versionName", mPackageInfo.versionName); map.put("versionCode", "" + mPackageInfo.versionCode); map.put("MODEL", "" + Build.MODEL); map.put("SDK_INT", "" + Build.VERSION.SDK_INT); map.put("PRODUCT", "" + Build.PRODUCT); return map; }
/** * @return true if the installed package signature matches the platform signature. */ private boolean isPlatformSigned(Context context, String pkgName) { PackageInfo platformPackageInfo = getPackageInfo(context, "android"); // Should never happen. if (platformPackageInfo == null || platformPackageInfo.signatures == null || platformPackageInfo.signatures.length == 0) { return false; } PackageInfo clientPackageInfo = getPackageInfo(context, pkgName); return (clientPackageInfo != null && clientPackageInfo.signatures != null && clientPackageInfo.signatures.length > 0 && platformPackageInfo.signatures[0].equals(clientPackageInfo.signatures[0])); }
public static int getAppVersionCode(Context context) { if (context != null) { PackageManager pm = context.getPackageManager(); if (pm != null) { PackageInfo pi; try { pi = pm.getPackageInfo(context.getPackageName(), 0); if (pi != null) { return pi.versionCode; } } catch (NameNotFoundException e) { e.printStackTrace(); } } } return -1; }
@Override public Object invoke(Object paramObject, Method paramMethod, Object[] paramArrayOfObject) { Object localObject = null; try { localObject = paramMethod.invoke(mPackageManager, paramArrayOfObject); } catch (Exception e) { e.printStackTrace(); } if ((paramMethod.getName().equals("getPackageInfo")) && (paramArrayOfObject[0].equals(mContext.getPackageName()))) { PackageInfo localPackageInfo = (PackageInfo) localObject; if (localPackageInfo != null) { localPackageInfo.versionCode = mVersionCode; localPackageInfo.versionName = mVersionName; if (localPackageInfo.applicationInfo != null) { localPackageInfo.applicationInfo.nativeLibraryDir = nativeLibraryDir; localPackageInfo.applicationInfo.publicSourceDir = publicSourceDir; localPackageInfo.applicationInfo.sourceDir = sourceDir; } } } return localObject; }
public static Drawable loadPackagePathIcon(Context context, String path, String mimeType){ int icon = sMimeIcons.get(mimeType); if (path != null) { final PackageManager pm = context.getPackageManager(); try { final PackageInfo packageInfo = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES); if (packageInfo != null) { packageInfo.applicationInfo.sourceDir = packageInfo.applicationInfo.publicSourceDir = path; // know issue with nine patch image instead of drawable return pm.getApplicationIcon(packageInfo.applicationInfo); } } catch (Exception e) { return ContextCompat.getDrawable(context, icon); } } else { return ContextCompat.getDrawable(context, icon); } return null; }
/** * 判断当前应用是否是顶栈 * @param context * @return */ public static boolean isAppOnForeground(Context context){ PackageInfo info = null; try{ info=context.getPackageManager().getPackageInfo(context.getPackageName(), 0); String curPackage=info.packageName; ActivityManager mActivityManager = ((ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE)); List<RunningTaskInfo> tasksInfo = mActivityManager.getRunningTasks(1); if(tasksInfo!=null && tasksInfo.size()>0){ if (!TextUtils.isEmpty(curPackage) && curPackage.equals(tasksInfo.get(0).topActivity.getPackageName())) { /**当前应用是顶栈*/ return true; } } }catch(Exception ex){ LogUtils.e(ex.toString()); } return false; }
public static PackageInfo getAPKVersionInfo(Context context, String packageName) { PackageManager packageManager = context.getPackageManager(); PackageInfo packInfo = null; try { packInfo = packageManager.getPackageInfo(packageName, 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return packInfo; }
/** * 获取系统中所有的应用 * * @param context 上下文 * @return 应用信息List */ public static List<PackageInfo> getAllApps(Context context) { List<PackageInfo> apps = new ArrayList<PackageInfo>(); PackageManager pManager = context.getPackageManager(); List<PackageInfo> paklist = pManager.getInstalledPackages(0); for (int i = 0; i < paklist.size(); i++) { PackageInfo pak = paklist.get(i); if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) { // customs applications apps.add(pak); } } return apps; }
private void saveSignatures(PackageInfo pkgInfo) { JLog.log("plugin", "保存签名信息: " + pkgInfo.signatures); if (pkgInfo != null && pkgInfo.signatures != null) { int i = 0; Signature[] signatureArr = pkgInfo.signatures; int length = signatureArr.length; int i2 = 0; while (i2 < length) { Signature signature = signatureArr[i2]; File file = new File(PluginDirHelper.getPluginSignatureFile(this.mContext, pkgInfo.packageName, i)); try { Utils.writeToFile(file, signature.toByteArray()); JLog.log("plugin", "保存签名信息 包名=" + pkgInfo.packageName + ",i=" + i + ", 签名md5=" + Utils.md5(signature.toByteArray())); i++; i2++; } catch (Exception e) { JLog.log("plugin", "保存签名信息失败 e=" + e.getMessage()); file.delete(); Utils.deleteDir(PluginDirHelper.getPluginSignatureDir(this.mContext, pkgInfo.packageName)); return; } } } }
/** * 获取应用程序版本名称 * @return */ public String getVersion() { String version = "0.0.0"; if(context == null) { return version; } try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo( getPackageName(), 0); version = packageInfo.versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return version; }
public boolean hasGms() { PackageManager packageManager = getPackageManager(); PackageInfo packageInfo; try { packageInfo = packageManager.getPackageInfo(GMS, 0); if (!packageInfo.applicationInfo.enabled) { return false; } } catch (PackageManager.NameNotFoundException e) { // NOSONAR // do nothing return false; } SharedPreferences preferences = getSharedPreferences("signature", Context.MODE_PRIVATE); long lastUpdate = preferences.getLong(GMS_LAST_UPDATE, 0); if (preferences.contains(GMS_VALID) && packageInfo.lastUpdateTime == lastUpdate) { return preferences.getBoolean(GMS_VALID, false); } boolean valid = checkGms(packageInfo.applicationInfo.sourceDir); preferences.edit() .putBoolean(GMS_VALID, valid) .putLong(GMS_LAST_UPDATE, packageInfo.lastUpdateTime) .apply(); return valid; }
private boolean resolveRemoteViews(Context appContext, String packageName, Notification notification) { if (notification == null) { return false; } String sourcePath = null; PackageInfo packageInfo = getPackageInfo(packageName); ApplicationInfo host = getHostContext().getApplicationInfo(); if (packageInfo != null) { sourcePath = packageInfo.applicationInfo.sourceDir; } if (TextUtils.isEmpty(sourcePath)) { sourcePath = getPackageResourcePath(packageName).getAbsolutePath(); } //Fix RemoteViews getNotificationFixer().fixNotificationRemoteViews(appContext, notification); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { getNotificationFixer().fixIcon(notification.getSmallIcon(), appContext, packageInfo != null); getNotificationFixer().fixIcon(notification.getLargeIcon(), appContext, packageInfo != null); } else { getNotificationFixer().fixIconImage(appContext.getResources(), notification.contentView, false, notification); } notification.icon = host.icon; ApplicationInfo proxyApplicationInfo = new ApplicationInfo(host); proxyApplicationInfo.packageName = packageName; proxyApplicationInfo.publicSourceDir = sourcePath; proxyApplicationInfo.sourceDir = sourcePath; fixApplicationInfo(notification.tickerView, proxyApplicationInfo); fixApplicationInfo(notification.contentView, proxyApplicationInfo); fixApplicationInfo(notification.bigContentView, proxyApplicationInfo); fixApplicationInfo(notification.headsUpContentView, proxyApplicationInfo); Bundle bundle = Reflect.on(notification).get("extras"); if (bundle != null) { bundle.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, proxyApplicationInfo); } return true; }
/** * 获取当前应用的版本号 * * @return 版本号 */ public String getVersionName() { PackageManager pm = getPackageManager(); // 第一个参数:应用程序的包名 // 第二个参数:指定信息的标签,0表示获取基础信息,比如包名、版本号。要想获取权限等信息必须要通过标签指定。 try { PackageInfo info = pm.getPackageInfo(getPackageName(), 0); return info.versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return ""; }
/** * A utility method to get the application's PackageInfo.versionName * @return the application's PackageInfo.versionName */ public static String getVersionName(final Context context) { try { if (context == null) { return ""; } final String packageName = context.getPackageName(); final PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); return info.versionName; } catch (final NameNotFoundException e) { Log.e(TAG, "Could not find version info.", e); } return ""; }
/** * 获取版本名称 * @param context 上下文 * @return 版本名称 */ public static String getVersionName(Context context){ //获取包管理器 PackageManager pm = context.getPackageManager(); //获取包信息 try { PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(),0); //返回版本号 return packageInfo.versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return null; }
public static String getCurrentVersionName(Context c) { try { PackageInfo pInfo = c.getPackageManager().getPackageInfo( c.getPackageName(), 0); return pInfo.versionName; } catch (PackageManager.NameNotFoundException e) { return "NA"; } }
/** * 获取支付宝客户端版本名称,作用不大 * @param context Context * @return 版本名称 */ public static String getAlipayClientVersion(Context context) { PackageManager pm = context.getPackageManager(); try { PackageInfo info = pm.getPackageInfo(ALIPAY_PACKAGE_NAME, 0); return info.versionName; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return null; } }
public static PackageInfo getPackageInfo(Context context, String packageName) { PackageManager packageManager = context.getPackageManager(); try { return packageManager.getPackageInfo(packageName, 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return null; }
private static a a(Context context, String str) { for (PackageInfo packageInfo : context.getPackageManager().getInstalledPackages(64)) { if (packageInfo.packageName.equals(str)) { a aVar = new a(); aVar.a = packageInfo.signatures[0].toByteArray(); aVar.b = packageInfo.versionCode; return aVar; } } return null; }
private Signature[] getSignature(String pkg, PackageManager pm) throws RemoteException, NameNotFoundException { PackageInfo info = getPackageInfo(pkg, 64); if (info == null) { info = pm.getPackageInfo(pkg, 64); } if (info != null) { return info.signatures; } throw new NameNotFoundException(); }
/** * @return Application's version code from the {@code PackageManager}. */ private static int getAppVersion(Context context) { try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); return packageInfo.versionCode; } catch (NameNotFoundException e) { // should never happen throw new RuntimeException("Could not get package name: " + e); } }
/** * Get current version number. * * @return String version */ private String getVersionNumber() { String version = "?"; try { PackageInfo pi = getPackageManager().getPackageInfo(getPackageName(), 0); version = pi.versionName; } catch (PackageManager.NameNotFoundException e) { //Log.e(TAG, "Package name not found", e); } return version; }