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

Commit d6f59082 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Prioritize which apps get credits first."

parents 48c8bfe8 642ab825
Loading
Loading
Loading
Loading
+54 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import static com.android.server.tare.TareUtils.narcToString;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.AlarmManager;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.os.Handler;
import android.os.Looper;
@@ -54,6 +55,7 @@ import com.android.server.usage.AppStandbyInternal;

import libcore.util.EmptyArray;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.PriorityQueue;
@@ -130,6 +132,54 @@ class Agent {
    private final BalanceThresholdAlarmListener mBalanceThresholdAlarmListener =
            new BalanceThresholdAlarmListener();

    /**
     * Comparator to use to sort apps before we distribute ARCs so that we try to give the most
     * important apps ARCs first.
     */
    @VisibleForTesting
    final Comparator<PackageInfo> mPackageDistributionComparator =
            new Comparator<PackageInfo>() {
                @Override
                public int compare(PackageInfo pi1, PackageInfo pi2) {
                    final ApplicationInfo appInfo1 = pi1.applicationInfo;
                    final ApplicationInfo appInfo2 = pi2.applicationInfo;
                    // Put any packages that don't declare an application at the end. A missing
                    // <application> tag likely means the app won't be doing any work anyway.
                    if (appInfo1 == null) {
                        if (appInfo2 == null) {
                            return 0;
                        }
                        return 1;
                    } else if (appInfo2 == null) {
                        return -1;
                    }
                    // Privileged apps eat first. They're likely required for the device to
                    // function properly.
                    // TODO: include headless system apps
                    if (appInfo1.isPrivilegedApp()) {
                        if (!appInfo2.isPrivilegedApp()) {
                            return -1;
                        }
                    } else if (appInfo2.isPrivilegedApp()) {
                        return 1;
                    }

                    // Sort by most recently used.
                    final long timeSinceLastUsedMs1 =
                            mAppStandbyInternal.getTimeSinceLastUsedByUser(
                                    pi1.packageName, UserHandle.getUserId(pi1.applicationInfo.uid));
                    final long timeSinceLastUsedMs2 =
                            mAppStandbyInternal.getTimeSinceLastUsedByUser(
                                    pi2.packageName, UserHandle.getUserId(pi2.applicationInfo.uid));
                    if (timeSinceLastUsedMs1 < timeSinceLastUsedMs2) {
                        return -1;
                    } else if (timeSinceLastUsedMs1 > timeSinceLastUsedMs2) {
                        return 1;
                    }
                    return 0;
                }
            };

    private static final int MSG_CHECK_BALANCE = 0;
    private static final int MSG_CLEAN_LEDGER = 1;
    private static final int MSG_SET_ALARMS = 2;
@@ -586,6 +636,8 @@ class Agent {
    @GuardedBy("mLock")
    void distributeBasicIncomeLocked(int batteryLevel) {
        List<PackageInfo> pkgs = mIrs.getInstalledPackages();
        pkgs.sort(mPackageDistributionComparator);

        final long now = getCurrentTimeMillis();
        for (int i = 0; i < pkgs.size(); ++i) {
            final PackageInfo pkgInfo = pkgs.get(i);
@@ -626,6 +678,8 @@ class Agent {
                mIrs.getMaxCirculationLocked() / mIrs.getInstalledPackages().size();
        final long now = getCurrentTimeMillis();

        pkgs.sort(mPackageDistributionComparator);

        for (int i = 0; i < pkgs.size(); ++i) {
            final PackageInfo packageInfo = pkgs.get(i);
            final String pkgName = packageInfo.packageName;