Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c695ead3 authored by Pawit Pornkitprasan's avatar Pawit Pornkitprasan Committed by LuK1337
Browse files

PackageManager: allow build-time disabling of components

Author: Pawit Pornkitprasan <p.pawit@gmail.com>
Date:   Sun Sep 1 16:24:14 2013 +0700

    PackageManager: allow build-time disabling of components

    Allow components to be specified as disabled at build time
    (applied on boot).

    This allows stock OTA components to be marked as disabled in
    CM builds.

    Change-Id: I6e4499cc40a779792a5ea97a10137399dad7d69f

Author: Gianmarco Reverberi <gianmarco.reverberi@gmail.com>
Date:   Mon Mar 16 20:02:15 2015 +0100

    SystemUpdateService: enable service but lock its receivers [1/2]

    Added a check for ensure that disabled components are not
    re-enabled at runtime

    Added code for forcing enable of previously disabled components

    Change-Id: Icfcfa26ccb85028d32edbb5cdb3dd7cdae85b720

Author: Michael W <baddaemon87@gmail.com>
Date:   Mon Oct 28 14:26:43 2019 +0100

    PM: Allow disabling components per-device

    * Introduce a new overlayable string-array
      "config_deviceDisabledComponents"
    * This is equally used to disable components per-device, e.g.
      NearbyMessagingService
    * At the same time, rename config_disabledComponents to
      config_globallyDisabledComponents to reflect the difference

    Change-Id: Ieeec0788b063a33e8a2830dd95b239c99a58466f

Author: Michael W <baddaemon87@gmail.com>
Date:   Fri Nov 1 11:11:29 2019 +0100

    PackageManager: Refactor en-/disabling of components

    * Don't use the same code twice, make it reusable

    Change-Id: I9fc388f56cea413654a390cb0ccd15a706fb3ad8

Change-Id: Ic26c9d2f080ab61e09b2d64ae48cd6f29147774a
parent c5542dc7
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -21,4 +21,17 @@
    <!-- Older rotation sensors are not setting event.timestamp correctly. Setting to
         true will use SystemClock.elapsedRealtimeNanos() to set timestamp. -->
    <bool name="config_useSystemClockforRotationSensor">false</bool>

    <!-- The list of components which should be automatically disabled for a specific device.
         Note: this MUST not be used to randomly disable components, ask for approval first! -->
    <string-array name="config_deviceDisabledComponents" translatable="false">
    </string-array>

    <!-- The list of components which should be automatically disabled for all devices. -->
    <string-array name="config_globallyDisabledComponents" translatable="false">
    </string-array>

    <!-- The list of components which should be forced to be enabled. -->
    <string-array name="config_forceEnabledComponents" translatable="false">
    </string-array>
</resources>
+5 −0
Original line number Diff line number Diff line
@@ -20,4 +20,9 @@

    <!-- Rotation sensor -->
    <java-symbol type="bool" name="config_useSystemClockforRotationSensor" />

    <!-- Package Manager -->
    <java-symbol type="array" name="config_deviceDisabledComponents" />
    <java-symbol type="array" name="config_globallyDisabledComponents" />
    <java-symbol type="array" name="config_forceEnabledComponents" />
</resources>
+42 −0
Original line number Diff line number Diff line
@@ -1475,6 +1475,8 @@ public class PackageManagerService extends IPackageManager.Stub
                        filter.hasDataScheme(IntentFilter.SCHEME_HTTPS));
    }
    ArrayList<ComponentName> mDisabledComponentsList;
    // Set of pending broadcasts for aggregating enable/disable of components.
    @VisibleForTesting(visibility = Visibility.PACKAGE)
    public static class PendingPackageBroadcasts {
@@ -3499,6 +3501,17 @@ public class PackageManagerService extends IPackageManager.Stub
                Slog.i(TAG, "Deferred reconcileAppsData finished " + count + " packages");
            }, "prepareAppData");
            // Disable components marked for disabling at build-time
            mDisabledComponentsList = new ArrayList<ComponentName>();
            enableComponents(mContext.getResources().getStringArray(
                    com.android.internal.R.array.config_deviceDisabledComponents), false);
            enableComponents(mContext.getResources().getStringArray(
                    com.android.internal.R.array.config_globallyDisabledComponents), false);
            // Enable components marked for forced-enable at build-time
            enableComponents(mContext.getResources().getStringArray(
                    com.android.internal.R.array.config_forceEnabledComponents), true);
            // If this is first boot after an OTA, and a normal boot, then
            // we need to clear code cache directories.
            // Note that we do *not* clear the application profiles. These remain valid
@@ -3663,6 +3676,29 @@ public class PackageManagerService extends IPackageManager.Stub
        mServiceStartWithDelay = SystemClock.uptimeMillis() + (60 * 1000L);
    }
    private void enableComponents(String[] components, boolean enable) {
        // Disable or enable components marked at build-time
        for (String name : components) {
            ComponentName cn = ComponentName.unflattenFromString(name);
            if (!enable) {
                mDisabledComponentsList.add(cn);
            }
            Slog.v(TAG, "Changing enabled state of " + name + " to " + enable);
            String className = cn.getClassName();
            PackageSetting pkgSetting = mSettings.mPackages.get(cn.getPackageName());
            if (pkgSetting == null || pkgSetting.pkg == null
                    || !AndroidPackageUtils.hasComponentClassName(pkgSetting.pkg, className)) {
                Slog.w(TAG, "Unable to change enabled state of " + name + " to " + enable);
                continue;
            }
            if (enable) {
                pkgSetting.enableComponentLPw(className, UserHandle.USER_OWNER);
            } else {
                pkgSetting.disableComponentLPw(className, UserHandle.USER_OWNER);
            }
        }
    }
    /**
     * Uncompress and install stub applications.
     * <p>In order to save space on the system partition, some applications are shipped in a
@@ -20933,6 +20969,12 @@ public class PackageManagerService extends IPackageManager.Stub
    public void setComponentEnabledSetting(ComponentName componentName,
            int newState, int flags, int userId) {
        if (!mUserManager.exists(userId)) return;
        // Don't allow to enable components marked for disabling at build-time
        if (mDisabledComponentsList.contains(componentName)) {
            Slog.d(TAG, "Ignoring attempt to set enabled state of disabled component "
                    + componentName.flattenToString());
            return;
        }
        setEnabledSetting(componentName.getPackageName(),
                componentName.getClassName(), newState, flags, userId, null);
    }