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

Commit 39e248ca authored by Kweku Adams's avatar Kweku Adams
Browse files

Shift some bootup work to later phases.

Shift some of the work required at boot to later phases. For example,
some of the data isn't needed until we're able to start 3rd party apps,
so shift that data loading to the THIRD_PARTY_APPS_CAN_START phase.

Bug: 241553134
Test: Android boots up and TARE loads properly
Change-Id: I4ad7d912b3318ad5c18f3347ac74b1046e637eb0
parent 1a38800a
Loading
Loading
Loading
Loading
+57 −30
Original line number Diff line number Diff line
@@ -274,24 +274,19 @@ public class InternalResourceService extends SystemService {
    public void onBootPhase(int phase) {
        mBootPhase = phase;

        if (PHASE_SYSTEM_SERVICES_READY == phase) {
        switch (phase) {
            case PHASE_SYSTEM_SERVICES_READY:
                mConfigObserver.start();
                mDeviceIdleController = IDeviceIdleController.Stub.asInterface(
                        ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER));
            setupEverything();
        } else if (PHASE_BOOT_COMPLETED == phase) {
            if (!mExemptListLoaded) {
                synchronized (mLock) {
                    try {
                        mExemptedApps =
                                new ArraySet<>(mDeviceIdleController.getFullPowerWhitelist());
                    } catch (RemoteException e) {
                        // Shouldn't happen.
                        Slog.wtf(TAG, e);
                    }
                    mExemptListLoaded = true;
                }
            }
                onBootPhaseSystemServicesReady();
                break;
            case PHASE_THIRD_PARTY_APPS_CAN_START:
                onBootPhaseThirdPartyAppsCanStart();
                break;
            case PHASE_BOOT_COMPLETED:
                onBootPhaseBootCompleted();
                break;
        }
    }

@@ -403,10 +398,9 @@ public class InternalResourceService extends SystemService {
            final ArraySet<String> added = new ArraySet<>();
            try {
                mExemptedApps = new ArraySet<>(mDeviceIdleController.getFullPowerWhitelist());
                mExemptListLoaded = true;
            } catch (RemoteException e) {
                // Shouldn't happen.
                Slog.wtf(TAG, e);
                return;
            }

            for (int i = mExemptedApps.size() - 1; i >= 0; --i) {
@@ -695,17 +689,11 @@ public class InternalResourceService extends SystemService {

    /** Perform long-running and/or heavy setup work. This should be called off the main thread. */
    private void setupHeavyWork() {
        if (mBootPhase < PHASE_THIRD_PARTY_APPS_CAN_START || !mIsEnabled) {
            return;
        }
        synchronized (mLock) {
            loadInstalledPackageListLocked();
            if (mBootPhase >= PHASE_BOOT_COMPLETED && !mExemptListLoaded) {
                try {
                    mExemptedApps = new ArraySet<>(mDeviceIdleController.getFullPowerWhitelist());
                } catch (RemoteException e) {
                    // Shouldn't happen.
                    Slog.wtf(TAG, e);
                }
                mExemptListLoaded = true;
            }
            final boolean isFirstSetup = !mScribe.recordExists();
            if (isFirstSetup) {
                mAgent.grantBirthrightsLocked();
@@ -726,18 +714,57 @@ public class InternalResourceService extends SystemService {
        }
    }

    private void setupEverything() {
    private void onBootPhaseSystemServicesReady() {
        if (mBootPhase < PHASE_SYSTEM_SERVICES_READY || !mIsEnabled) {
            return;
        }
        synchronized (mLock) {
            registerListeners();
            mCurrentBatteryLevel = getCurrentBatteryLevel();
        }
    }

    private void onBootPhaseThirdPartyAppsCanStart() {
        if (mBootPhase < PHASE_THIRD_PARTY_APPS_CAN_START || !mIsEnabled) {
            return;
        }
        synchronized (mLock) {
            mHandler.post(this::setupHeavyWork);
            mCompleteEconomicPolicy.setup(mConfigObserver.getAllDeviceConfigProperties());
        }
    }

    private void onBootPhaseBootCompleted() {
        if (mBootPhase < PHASE_BOOT_COMPLETED || !mIsEnabled) {
            return;
        }
        synchronized (mLock) {
            if (!mExemptListLoaded) {
                try {
                    mExemptedApps = new ArraySet<>(mDeviceIdleController.getFullPowerWhitelist());
                    mExemptListLoaded = true;
                } catch (RemoteException e) {
                    // Shouldn't happen.
                }
            }
        }
    }

    private void setupEverything() {
        if (!mIsEnabled) {
            return;
        }
        if (mBootPhase >= PHASE_SYSTEM_SERVICES_READY) {
            onBootPhaseSystemServicesReady();
        }
        if (mBootPhase >= PHASE_THIRD_PARTY_APPS_CAN_START) {
            onBootPhaseThirdPartyAppsCanStart();
        }
        if (mBootPhase >= PHASE_BOOT_COMPLETED) {
            onBootPhaseBootCompleted();
        }
    }

    private void tearDownEverything() {
        if (mIsEnabled) {
            return;