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

Commit 95176bb1 authored by Calin Juravle's avatar Calin Juravle
Browse files

Some refactoring in BackgroundDexOptService.

Extract postOta/idle optimizations in their own method.
In preparation for adding the logic to handle secondary dex files.

Test: device boots, pacakges get compiled
Bug: 32871170

(cherry picked from commit be6a71a0)

Change-Id: Ie6cdd8461e7214f5de68bc9172f4171ebf72aa39
parent b96dba9b
Loading
Loading
Loading
Loading
+99 −87
Original line number Diff line number Diff line
@@ -69,7 +69,7 @@ public class BackgroundDexOptService extends JobService {
     */
    final AtomicBoolean mExitPostBootUpdate = new AtomicBoolean(false);

    private final File dataDir = Environment.getDataDirectory();
    private final File mDataDir = Environment.getDataDirectory();

    public static void schedule(Context context) {
        JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
@@ -120,7 +120,7 @@ public class BackgroundDexOptService extends JobService {

    private long getLowStorageThreshold() {
        @SuppressWarnings("deprecation")
        final long lowThreshold = StorageManager.from(this).getStorageLowBytes(dataDir);
        final long lowThreshold = StorageManager.from(this).getStorageLowBytes(mDataDir);
        if (lowThreshold == 0) {
            Log.e(TAG, "Invalid low storage threshold");
        }
@@ -134,17 +134,25 @@ public class BackgroundDexOptService extends JobService {
            // This job has already been superseded. Do not start it.
            return false;
        }
        new Thread("BackgroundDexOptService_PostBootUpdate") {
            @Override
            public void run() {
                postBootUpdate(jobParams, pm, pkgs);
            }

        }.start();
        return true;
    }

    private void postBootUpdate(JobParameters jobParams, PackageManagerService pm,
            ArraySet<String> pkgs) {
        // Load low battery threshold from the system config. This is a 0-100 integer.
        final int lowBatteryThreshold = getResources().getInteger(
                com.android.internal.R.integer.config_lowBatteryWarningLevel);

        final long lowThreshold = getLowStorageThreshold();

        mAbortPostBootUpdate.set(false);
        new Thread("BackgroundDexOptService_PostBootUpdate") {
            @Override
            public void run() {

        for (String pkg : pkgs) {
            if (mAbortPostBootUpdate.get()) {
                // JobScheduler requested an early abort.
@@ -158,7 +166,7 @@ public class BackgroundDexOptService extends JobService {
                // Rather bail than completely drain the battery.
                break;
            }
                    long usableSpace = dataDir.getUsableSpace();
            long usableSpace = mDataDir.getUsableSpace();
            if (usableSpace < lowThreshold) {
                // Rather bail than completely fill up the disk.
                Log.w(TAG, "Aborting background dex opt job due to low storage: " +
@@ -186,33 +194,40 @@ public class BackgroundDexOptService extends JobService {
        // Ran to completion, so we abandon our timeslice and do not reschedule.
        jobFinished(jobParams, /* reschedule */ false);
    }

    private boolean runIdleOptimization(final JobParameters jobParams,
            final PackageManagerService pm, final ArraySet<String> pkgs) {
        new Thread("BackgroundDexOptService_IdleOptimization") {
            @Override
            public void run() {
                idleOptimization(jobParams, pm, pkgs);
            }
        }.start();
        return true;
    }

    private boolean runIdleOptimization(final JobParameters jobParams,
            final PackageManagerService pm, final ArraySet<String> pkgs) {
    private void idleOptimization(JobParameters jobParams, PackageManagerService pm,
            ArraySet<String> pkgs) {
        // If post-boot update is still running, request that it exits early.
        mExitPostBootUpdate.set(true);

        mAbortIdleOptimization.set(false);

        final long lowThreshold = getLowStorageThreshold();

        new Thread("BackgroundDexOptService_IdleOptimization") {
            @Override
            public void run() {
        for (String pkg : pkgs) {
            if (mAbortIdleOptimization.get()) {
                // JobScheduler requested an early abort.
                return;
            }

            synchronized (sFailedPackageNames) {
                if (sFailedPackageNames.contains(pkg)) {
                    // Skip previously failing package
                    continue;
                }
            }

                    long usableSpace = dataDir.getUsableSpace();
            long usableSpace = mDataDir.getUsableSpace();
            if (usableSpace < lowThreshold) {
                // Rather bail than completely fill up the disk.
                Log.w(TAG, "Aborting background dex opt job due to low storage: " +
@@ -240,9 +255,6 @@ public class BackgroundDexOptService extends JobService {
        // Ran to completion, so we abandon our timeslice and do not reschedule.
        jobFinished(jobParams, /* reschedule */ false);
    }
        }.start();
        return true;
    }

    @Override
    public boolean onStartJob(JobParameters params) {