From f28754ebff4cf63312cfd5b424188e3c5fdafdfc Mon Sep 17 00:00:00 2001 From: Chaohui Wang Date: Fri, 18 Nov 2022 13:44:06 +0800 Subject: [PATCH] Split isEssentialPackage() from isSystemPackage() Please use ApplicationInfo#isSignedWithPlatformKey() to check whether an app is signed with platform key, instead of loading the signatures. This is faster, and we also no longer need to load the PackageInfos with the deprecated flag PackageManager.GET_SIGNATURES. Bug: 235727273 Test: Unit test Test: Manually with Settings App Change-Id: I4d3c103559736b725517acef9bbca6a2c9d62ed6 --- .../src/com/android/settingslib/Utils.java | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/packages/SettingsLib/src/com/android/settingslib/Utils.java b/packages/SettingsLib/src/com/android/settingslib/Utils.java index a822e185479a7..a78256a25e32f 100644 --- a/packages/SettingsLib/src/com/android/settingslib/Utils.java +++ b/packages/SettingsLib/src/com/android/settingslib/Utils.java @@ -376,27 +376,19 @@ public class Utils { /** * Determine whether a package is a "system package", in which case certain things (like * disabling notifications or disabling the package altogether) should be disallowed. + *

+ * Note: This function is just for UI treatment, and should not be used for security purposes. + * + * @deprecated Use {@link ApplicationInfo#isSignedWithPlatformKey()} and + * {@link #isEssentialPackage} instead. */ + @Deprecated public static boolean isSystemPackage(Resources resources, PackageManager pm, PackageInfo pkg) { if (sSystemSignature == null) { sSystemSignature = new Signature[]{getSystemSignature(pm)}; } - if (sPermissionControllerPackageName == null) { - sPermissionControllerPackageName = pm.getPermissionControllerPackageName(); - } - if (sServicesSystemSharedLibPackageName == null) { - sServicesSystemSharedLibPackageName = pm.getServicesSystemSharedLibraryPackageName(); - } - if (sSharedSystemSharedLibPackageName == null) { - sSharedSystemSharedLibPackageName = pm.getSharedSystemSharedLibraryPackageName(); - } - return (sSystemSignature[0] != null - && sSystemSignature[0].equals(getFirstSignature(pkg))) - || pkg.packageName.equals(sPermissionControllerPackageName) - || pkg.packageName.equals(sServicesSystemSharedLibPackageName) - || pkg.packageName.equals(sSharedSystemSharedLibPackageName) - || pkg.packageName.equals(PrintManager.PRINT_SPOOLER_PACKAGE_NAME) - || isDeviceProvisioningPackage(resources, pkg.packageName); + return (sSystemSignature[0] != null && sSystemSignature[0].equals(getFirstSignature(pkg))) + || isEssentialPackage(resources, pm, pkg.packageName); } private static Signature getFirstSignature(PackageInfo pkg) { @@ -415,6 +407,29 @@ public class Utils { return null; } + /** + * Determine whether a package is a "essential package". + *

+ * In which case certain things (like disabling the package) should be disallowed. + */ + public static boolean isEssentialPackage( + Resources resources, PackageManager pm, String packageName) { + if (sPermissionControllerPackageName == null) { + sPermissionControllerPackageName = pm.getPermissionControllerPackageName(); + } + if (sServicesSystemSharedLibPackageName == null) { + sServicesSystemSharedLibPackageName = pm.getServicesSystemSharedLibraryPackageName(); + } + if (sSharedSystemSharedLibPackageName == null) { + sSharedSystemSharedLibPackageName = pm.getSharedSystemSharedLibraryPackageName(); + } + return packageName.equals(sPermissionControllerPackageName) + || packageName.equals(sServicesSystemSharedLibPackageName) + || packageName.equals(sSharedSystemSharedLibPackageName) + || packageName.equals(PrintManager.PRINT_SPOOLER_PACKAGE_NAME) + || isDeviceProvisioningPackage(resources, packageName); + } + /** * Returns {@code true} if the supplied package is the device provisioning app. Otherwise, * returns {@code false}.