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

Commit 7ef319f4 authored by Andreas Gampe's avatar Andreas Gampe Committed by android-build-merger
Browse files

Merge "BGDexopt: Clean up ordering" am: 33ac5052

am: 1404b89b

Change-Id: Icbe9b69db03dc7c3bc39b576e138efdb5642516f
parents 9c1224cf 1404b89b
Loading
Loading
Loading
Loading
+45 −30
Original line number Diff line number Diff line
@@ -345,40 +345,55 @@ public class BackgroundDexOptService extends JobService {
    private int optimizePackages(PackageManagerService pm, ArraySet<String> pkgs,
            long lowStorageThreshold, boolean isForPrimaryDex) {
        ArraySet<String> updatedPackages = new ArraySet<>();
        Set<String> unusedPackages = pm.getUnusedPackages(mDowngradeUnusedAppsThresholdInMillis);
        boolean hadSomeLowSpaceFailure = false;
        Log.d(TAG, "Unsused Packages " +  String.join(",", unusedPackages));

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

                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 (unusedPackages.contains(pkg) && shouldDowngrade) {
                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;
                    if (downgradePackage(pm, pkg, isForPrimaryDex)) {
                        updatedPackages.add(pkg);
                    }
                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);
                }
            }

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