From 07a1b2a63bad0bc2ed821bbe5e25ca8d9fb9db20 Mon Sep 17 00:00:00 2001 From: chenjuntao Date: Thu, 17 Aug 2023 16:29:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B7=B2=E5=AE=89=E8=A3=85=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=9D=83=E9=99=90-=E5=90=88=E8=A7=84=E6=95=B4?= =?UTF-8?q?=E6=94=B9=20https://jira.shanqu.cc/browse/GHZS-3249?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/gh/common/util/PackageUtils.java | 72 ++++++++++++++++++- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/gh/common/util/PackageUtils.java b/app/src/main/java/com/gh/common/util/PackageUtils.java index 1a93e43a51..350a6f0432 100644 --- a/app/src/main/java/com/gh/common/util/PackageUtils.java +++ b/app/src/main/java/com/gh/common/util/PackageUtils.java @@ -7,12 +7,14 @@ import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; +import android.content.pm.PermissionInfo; import android.content.pm.Signature; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.PowerManager; +import android.provider.Settings; import android.text.TextUtils; import androidx.annotation.NonNull; @@ -29,6 +31,7 @@ import com.gh.gamecenter.BuildConfig; import com.gh.gamecenter.common.constant.Constants; import com.gh.gamecenter.common.utils.ExtensionsKt; import com.gh.gamecenter.common.utils.PackageFlavorHelper; +import com.gh.gamecenter.common.utils.PermissionHelper; import com.gh.gamecenter.core.utils.MD5Utils; import com.gh.gamecenter.core.utils.SentryHelper; import com.gh.gamecenter.feature.entity.ApkEntity; @@ -74,6 +77,9 @@ public class PackageUtils { private static final String TAG = "PackageUtils"; + // 设备是否支持禁用获取已安装应用列表。-1 代表支持情况未知,0 代表不支持, 1 代表支持 + private static int mIsSupportGetInstalledListPermission = -1; + public static String getInstallPackageInfoSourceDir(String packageName) { try { return HaloApp.getInstance().getApplication().getPackageManager().getPackageInfo(packageName, @@ -966,18 +972,78 @@ public class PackageUtils { return new ArrayList<>(mInstalledPackageList); } - Utils.log(TAG, "调用系统 API 获取全新的已安装应用列表"); + // 是否需要调用系统 API 获取最新的已安装应用列表 + boolean shouldGetNewInstalledPackagedList = false; + + // 当前设备是否支持限制获取已安装应用列表的功能 + if (isSupportGetInstalledAppsPermission(context)) { + Utils.log(TAG, "当前设备支持限制获取已安装应用列表的功能"); + // 当前设备是否支持禁用了获取已安装应用列表 + if (!PermissionHelper.isGetInstalledListPermissionDisabled(context)) { + Utils.log(TAG, "当前设备没有限制获取已安装应用列表的功能"); + shouldGetNewInstalledPackagedList = true; + } else { + Utils.log(TAG, "当前设备已限制获取已安装应用列表的功能"); + } + } else { + Utils.log(TAG, "当前设备不支持限制获取已安装应用列表的功能"); + shouldGetNewInstalledPackagedList = true; + } + + if (shouldGetNewInstalledPackagedList) { + mLastInstalledPackageListTime = System.currentTimeMillis(); + mInstalledPackageList = getInstalledPackagesInternal(context, flags); + } + + if (mInstalledPackageList == null) { + mInstalledPackageList = new ArrayList<>(); + } - mLastInstalledPackageListTime = System.currentTimeMillis(); - mInstalledPackageList = getInstalledPackagesInternal(context, flags); return mInstalledPackageList; } + public static boolean isSupportGetInstalledAppsPermission(Context context) { + // 若存在缓存,直接返回缓存结果。为 0 代表不支持,为 1 代表支持 + if (mIsSupportGetInstalledListPermission != -1) { + return mIsSupportGetInstalledListPermission != 0; + } + + try { + // 根据官方提供的方法来判定是否支持限制获取已安装应用列表 + int flag = Settings.Secure.getInt(context.getContentResolver(), "oem_installed_apps_runtime_permission_enable", 0); + if (flag == 1) { + mIsSupportGetInstalledListPermission = 1; + return true; + } + + // 部分未升级的手机没有上面配置项,有定义下面危险权限也认为是支持设备软件列表管控 + PackageManager packageManager = context.getPackageManager(); + PermissionInfo permissionInfo = packageManager.getPermissionInfo("com.android.permission.GET_INSTALLED_APPS", 0); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + if (permissionInfo.getProtection() == PermissionInfo.PROTECTION_DANGEROUS) { + mIsSupportGetInstalledListPermission = 1; + return true; + } else { + mIsSupportGetInstalledListPermission = 0; + return false; + } + } else { + mIsSupportGetInstalledListPermission = 0; + return false; + } + } catch (NameNotFoundException e) { + mIsSupportGetInstalledListPermission = 0; + return false; + } + } + /** * 在5.1系统手机使用PackageManager获取已安装应用容易发生Package manager has died异常 * https://stackoverflow.com/questions/13235793/transactiontoolargeeception-when-trying-tÏo-get-a-list-of-applications-installed/30062632#30062632 */ private static List getInstalledPackagesInternal(Context context, int flags) { + Utils.log(TAG, "调用系统 API 获取已安装应用列表"); + final PackageManager pm = context.getPackageManager(); try { return pm.getInstalledPackages(flags);