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

Commit 1404b89b authored by Andreas Gampe's avatar Andreas Gampe Committed by android-build-merger
Browse files

Merge "BGDexopt: Clean up ordering"

am: 33ac5052

Change-Id: Id09e7c5ec0c35daaa04f346e7966f28bcb3bc52a
parents 8ad549cd 33ac5052
Loading
Loading
Loading
Loading
+45 −30
Original line number Original line Diff line number Diff line
@@ -345,40 +345,55 @@ public class BackgroundDexOptService extends JobService {
    private int optimizePackages(PackageManagerService pm, ArraySet<String> pkgs,
    private int optimizePackages(PackageManagerService pm, ArraySet<String> pkgs,
            long lowStorageThreshold, boolean isForPrimaryDex) {
            long lowStorageThreshold, boolean isForPrimaryDex) {
        ArraySet<String> updatedPackages = new ArraySet<>();
        ArraySet<String> updatedPackages = new ArraySet<>();
        Set<String> unusedPackages = pm.getUnusedPackages(mDowngradeUnusedAppsThresholdInMillis);

        boolean hadSomeLowSpaceFailure = false;
        try {
        Log.d(TAG, "Unsused Packages " +  String.join(",", unusedPackages));
            // Only downgrade apps when space is low on device.
            // Only downgrade apps when space is low on device.
            // Threshold is selected above the lowStorageThreshold so that we can pro-actively clean
            // Threshold is selected above the lowStorageThreshold so that we can pro-actively clean
            // up disk before user hits the actual lowStorageThreshold.
            // up disk before user hits the actual lowStorageThreshold.
        final long lowStorageThresholdForDowngrade = LOW_THRESHOLD_MULTIPLIER_FOR_DOWNGRADE *
            final long lowStorageThresholdForDowngrade = LOW_THRESHOLD_MULTIPLIER_FOR_DOWNGRADE
                lowStorageThreshold;
                    * lowStorageThreshold;
            boolean shouldDowngrade = shouldDowngrade(lowStorageThresholdForDowngrade);
            boolean shouldDowngrade = shouldDowngrade(lowStorageThresholdForDowngrade);
            Log.d(TAG, "Should Downgrade " + shouldDowngrade);
            Log.d(TAG, "Should Downgrade " + shouldDowngrade);
        boolean dex_opt_performed = false;
            if (shouldDowngrade) {
        for (String pkg : pkgs) {
                Set<String> unusedPackages =
            int abort_code = abortIdleOptimizations(lowStorageThreshold);
                        pm.getUnusedPackages(mDowngradeUnusedAppsThresholdInMillis);
            if (abort_code == OPTIMIZE_ABORT_BY_JOB_SCHEDULER) {
                Log.d(TAG, "Unsused Packages " +  String.join(",", unusedPackages));
                return abort_code;

                for (String pkg : unusedPackages) {
                    int abortCode = abortIdleOptimizations(/*lowStorageThreshold*/ -1);
                    if (abortCode != OPTIMIZE_CONTINUE) {
                        // Should be aborted by the scheduler.
                        return abortCode;
                    }
                    }
            // Downgrade unused packages.
                    if (downgradePackage(pm, pkg, isForPrimaryDex)) {
            if (unusedPackages.contains(pkg) && shouldDowngrade) {
                        updatedPackages.add(pkg);
                dex_opt_performed = downgradePackage(pm, pkg, isForPrimaryDex);
            } else {
                if (abort_code == OPTIMIZE_ABORT_NO_SPACE_LEFT) {
                    // can't dexopt because of low space.
                    hadSomeLowSpaceFailure = true;
                    continue;
                    }
                    }
                dex_opt_performed = optimizePackage(pm, pkg, isForPrimaryDex);
                }
                }
            if (dex_opt_performed) {

                if (!unusedPackages.isEmpty()) {
                    pkgs = new ArraySet<>(pkgs);
                    pkgs.removeAll(unusedPackages);
                }
            }

            for (String pkg : pkgs) {
                int abortCode = abortIdleOptimizations(lowStorageThreshold);
                if (abortCode != OPTIMIZE_CONTINUE) {
                    // Either aborted by the scheduler or no space left.
                    return abortCode;
                }

                boolean dexOptPerformed = optimizePackage(pm, pkg, isForPrimaryDex);
                if (dexOptPerformed) {
                    updatedPackages.add(pkg);
                    updatedPackages.add(pkg);
                }
                }
            }
            }


            return OPTIMIZE_PROCESSED;
        } finally {
            // Always let the pinner service know about changes.
            notifyPinService(updatedPackages);
            notifyPinService(updatedPackages);
        return hadSomeLowSpaceFailure ? OPTIMIZE_ABORT_NO_SPACE_LEFT : OPTIMIZE_PROCESSED;
        }
    }
    }