From 876ab66e3fd6795c6f1efcb693ed79f74d9dcb43 Mon Sep 17 00:00:00 2001 From: Martijn Coenen Date: Mon, 3 Apr 2023 14:06:23 +0000 Subject: [PATCH] Exclude headless packages from being scanned as stopped. With the enabling of putting all system apps in stopped state, we want to automatically exclude all packages that don't have a launcher entry, since those packages don't have a clean way for a user to "unstop" them. This is a rather conservative approach, that may be revisited in the future. Bug: 269129704 Test: Manual inspection no headless packages are stopped Change-Id: Ic2ea4b37d34ebcfea912e65e1bd0f2e1150a3daa --- .../server/pm/InstallPackageHelper.java | 19 +++++++++++++++++++ .../java/com/android/server/pm/Settings.java | 15 ++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/services/core/java/com/android/server/pm/InstallPackageHelper.java b/services/core/java/com/android/server/pm/InstallPackageHelper.java index 5f424edb15c4b..f0f091f16d473 100644 --- a/services/core/java/com/android/server/pm/InstallPackageHelper.java +++ b/services/core/java/com/android/server/pm/InstallPackageHelper.java @@ -117,6 +117,7 @@ import android.content.pm.PackageInstaller; import android.content.pm.PackageManager; import android.content.pm.PermissionGroupInfo; import android.content.pm.PermissionInfo; +import android.content.pm.ResolveInfo; import android.content.pm.SharedLibraryInfo; import android.content.pm.Signature; import android.content.pm.SigningDetails; @@ -3946,6 +3947,24 @@ final class InstallPackageHelper { mPm.mSettings.disableSystemPackageLPw(parsedPackage.getPackageName(), true); } } + + // If this is a system app we hadn't seen before, and this is a first boot or OTA, + // we need to unstop it if it doesn't have a launcher entry. + if (mPm.mShouldStopSystemPackagesByDefault && scanResult.mRequest.mPkgSetting == null + && ((scanFlags & SCAN_FIRST_BOOT_OR_UPGRADE) != 0) + && ((scanFlags & SCAN_AS_SYSTEM) != 0)) { + final Intent launcherIntent = new Intent(Intent.ACTION_MAIN); + launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER); + launcherIntent.setPackage(parsedPackage.getPackageName()); + final List launcherActivities = + mPm.snapshotComputer().queryIntentActivitiesInternal(launcherIntent, null, + PackageManager.MATCH_DIRECT_BOOT_AWARE + | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, 0); + if (launcherActivities.isEmpty()) { + scanResult.mPkgSetting.setStopped(false, 0); + } + } + if (mIncrementalManager != null && isIncrementalPath(parsedPackage.getPath())) { if (scanResult.mPkgSetting != null && scanResult.mPkgSetting.isLoading()) { // Continue monitoring loading progress of active incremental packages diff --git a/services/core/java/com/android/server/pm/Settings.java b/services/core/java/com/android/server/pm/Settings.java index 94a00d6e2e480..def8cb30f091a 100644 --- a/services/core/java/com/android/server/pm/Settings.java +++ b/services/core/java/com/android/server/pm/Settings.java @@ -4280,10 +4280,23 @@ public final class Settings implements Watchable, Snappable, ResilientAtomicFile // Non-Apex system apps, that are not included in the allowlist in // initialNonStoppedSystemPackages, should be marked as stopped by default. - final boolean shouldBeStopped = service.mShouldStopSystemPackagesByDefault + boolean shouldBeStopped = service.mShouldStopSystemPackagesByDefault && ps.isSystem() && !ps.isApex() && !service.mInitialNonStoppedSystemPackages.contains(ps.getPackageName()); + if (shouldBeStopped) { + final Intent launcherIntent = new Intent(Intent.ACTION_MAIN); + launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER); + launcherIntent.setPackage(ps.getPackageName()); + final List launcherActivities = + service.snapshotComputer().queryIntentActivitiesInternal(launcherIntent, + null, + PackageManager.MATCH_DIRECT_BOOT_AWARE + | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, 0); + if (launcherActivities.isEmpty()) { + shouldBeStopped = false; + } + } ps.setStopped(shouldBeStopped, userHandle); // If userTypeInstallablePackages is the *only* reason why we're not installing,