/** * If set, this intent will not match any components in packages that * are currently * {@linkplain android.content.pm.ApplicationInfo#FLAG_STOPPED stopped}. * If this is not set, then the default behavior is to include such * applications in the result. */ publicstaticfinalintFLAG_EXCLUDE_STOPPED_PACKAGES=0x00000010; /** * If set, this intent will always match any components in packages that * are currently * {@linkplain android.content.pm.ApplicationInfo#FLAG_STOPPED stopped}. * This is the default behavior when * {@link #FLAG_EXCLUDE_STOPPED_PACKAGES} is not set. If both of these * flags are set, this one wins (it allows overriding of exclude for * places where the framework may automatically set the exclude flag, * such as broadcasts). */ publicstaticfinalintFLAG_INCLUDE_STOPPED_PACKAGES=0x00000020;
// System apps are never considered stopped for purposes of // filtering, because there may be no way for the user to // actually re-launch them. return !packageState.isSystem() && packageState.getUserStateOrDefault(userId).isStopped(); }
if (packageState.isSystem()) { // A system app can be considered in the stopped state only if it was originally // scanned in the stopped state. return packageState.isScannedAsStoppedSystemApp() && packageState.getUserStateOrDefault(userId).isStopped(); } return packageState.getUserStateOrDefault(userId).isStopped(); } }
private Pair<ScanResult, Boolean> scanSystemPackageLI(ParsedPackage parsedPackage, @ParsingPackageUtils.ParseFlags int parseFlags, @PackageManagerService.ScanFlags int scanFlags, @Nullable UserHandle user)throws PackageManagerException {
// A new application appeared on /system, and we are seeing it for the first time. // Its also not updated as we don't have a copy of it on /data. So, scan it in a // STOPPED state. // We'll skip this step under the following conditions: // - It's "android" // - It's an APEX or overlay package since stopped state does not affect them. // - It is enumerated with a <initial-package-state> tag having the stopped attribute // set to false // - It doesn't have an enabled and exported launcher activity, which means the user // wouldn't have a way to un-stop it finalbooleanisApexPkg= (scanFlags & SCAN_AS_APEX) != 0; if (mPm.mShouldStopSystemPackagesByDefault && scanSystemPartition && !pkgAlreadyExists && !isApexPkg && !parsedPackage.isOverlayIsStatic() ) { StringpackageName= parsedPackage.getPackageName(); if (!"android".contentEquals(packageName) && !mPm.mInitialNonStoppedSystemPackages.contains(packageName) && hasLauncherEntry(parsedPackage)) { scanFlags |= SCAN_AS_STOPPED_SYSTEM_APP; } } }
根据函数的注释,一个应用会被标记为“停止的系统应用”(Scanned As Stopped System App)需要满足以下所有条件:
<!-- Whether system apps should be scanned in the stopped state during initial boot. Packages can be added by OEMs in an allowlist, to prevent them from being scanned as "stopped" during initial boot of a device, or after an OTA update. Stopped state of an app is not changed during subsequent reboots. --> <boolname="config_stopSystemPackagesByDefault">true</bool>
<?xml version="1.0" encoding="utf-8"?> <!-- This XML defines an allowlist for packages that should not be scanned in a "stopped" state. When this feature is turned on (indicated by the config config_stopSystemPackagesByDefault in core/res/res/values/config.xml) packages on the system partition that are encountered by the PackageManagerService for the first time are scanned in the "stopped" state. This allowlist is also considered while creating new users on the device. Stopped state is not set during subsequent reboots.
Example usage 1. <initial-package-state package="com.example.app" stopped="false"/> Indicates that a system package - com.example.app's initial stopped state should not be set by the Package Manager. By default, system apps are marked as stopped. 2. <initial-package-state package="com.example.app" stopped="true"/> Indicates that a system package - com.example.app's initial state should be set by the Package Manager to "stopped=true". It will have the same effect on the package's stopped state even if this package was not included in the allow list. 3. <initial-package-state package="com.example.app"/> Invalid usage. -->