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

Commit b3ed6143 authored by Kweku Adams's avatar Kweku Adams
Browse files

Respond to force stops.

Reclaim all ARCs from an app when it's force stopped so it won't
immediately be able to do bg work. It'll regain ARCs over time in
response to various events.

Bug: 240726265
Test: manually force stop an app and check dumpsys
Change-Id: Ie8d768440f8366472a5bccbacf39fbc176f929c5
parent b218fd8f
Loading
Loading
Loading
Loading
+23 −17
Original line number Diff line number Diff line
@@ -554,6 +554,25 @@ class Agent {
        }
    }

    @GuardedBy("mLock")
    void reclaimAllAssetsLocked(final int userId, @NonNull final String pkgName, int regulationId) {
        final Ledger ledger = mScribe.getLedgerLocked(userId, pkgName);
        final long curBalance = ledger.getCurrentBalance();
        if (curBalance <= 0) {
            return;
        }
        if (DEBUG) {
            Slog.i(TAG, "Reclaiming " + cakeToString(curBalance)
                    + " from " + appToString(userId, pkgName)
                    + " because of " + eventToString(regulationId));
        }

        final long now = getCurrentTimeMillis();
        recordTransactionLocked(userId, pkgName, ledger,
                new Ledger.Transaction(now, now, regulationId, null, -curBalance, 0),
                true);
    }

    /**
     * Reclaim a percentage of unused ARCs from every app that hasn't been used recently. The
     * reclamation will not reduce an app's balance below its minimum balance as dictated by
@@ -564,7 +583,7 @@ class Agent {
     * @param minUnusedTimeMs The minimum amount of time (in milliseconds) that must have
     *                        transpired since the last user usage event before we will consider
     *                        reclaiming ARCs from the app.
     * @param scaleMinBalance Whether or not to used the scaled minimum app balance. If false,
     * @param scaleMinBalance Whether or not to use the scaled minimum app balance. If false,
     *                        this will use the constant min balance floor given by
     *                        {@link EconomicPolicy#getMinSatiatedBalance(int, String)}. If true,
     *                        this will use the scaled balance given by
@@ -603,7 +622,8 @@ class Agent {
                    }
                    if (toReclaim > 0) {
                        if (DEBUG) {
                            Slog.i(TAG, "Reclaiming unused wealth! Taking " + toReclaim
                            Slog.i(TAG, "Reclaiming unused wealth! Taking "
                                    + cakeToString(toReclaim)
                                    + " from " + appToString(userId, pkgName));
                        }

@@ -667,21 +687,7 @@ class Agent {
     */
    @GuardedBy("mLock")
    void onAppRestrictedLocked(final int userId, @NonNull final String pkgName) {
        final long curBalance = getBalanceLocked(userId, pkgName);
        final long minBalance = mIrs.getMinBalanceLocked(userId, pkgName);
        if (curBalance <= minBalance) {
            return;
        }
        if (DEBUG) {
            Slog.i(TAG, "App restricted! Taking " + curBalance
                    + " from " + appToString(userId, pkgName));
        }

        final long now = getCurrentTimeMillis();
        final Ledger ledger = mScribe.getLedgerLocked(userId, pkgName);
        recordTransactionLocked(userId, pkgName, ledger,
                new Ledger.Transaction(now, now, REGULATION_BG_RESTRICTED, null, -curBalance, 0),
                true);
        reclaimAllAssetsLocked(userId, pkgName, REGULATION_BG_RESTRICTED);
    }

    /**
+3 −0
Original line number Diff line number Diff line
@@ -70,6 +70,7 @@ public abstract class EconomicPolicy {
    /** App is fully restricted from running in the background. */
    static final int REGULATION_BG_RESTRICTED = TYPE_REGULATION | 5;
    static final int REGULATION_BG_UNRESTRICTED = TYPE_REGULATION | 6;
    static final int REGULATION_FORCE_STOP = TYPE_REGULATION | 8;

    static final int REWARD_NOTIFICATION_SEEN = TYPE_REWARD | 0;
    static final int REWARD_NOTIFICATION_INTERACTION = TYPE_REWARD | 1;
@@ -409,6 +410,8 @@ public abstract class EconomicPolicy {
                return "BG_RESTRICTED";
            case REGULATION_BG_UNRESTRICTED:
                return "BG_UNRESTRICTED";
            case REGULATION_FORCE_STOP:
                return "FORCE_STOP";
        }
        return "UNKNOWN_REGULATION:" + Integer.toHexString(eventId);
    }
+3 −1
Original line number Diff line number Diff line
@@ -533,7 +533,9 @@ public class InternalResourceService extends SystemService {

    void onPackageForceStopped(final int userId, @NonNull final String pkgName) {
        synchronized (mLock) {
            // TODO: reduce ARC count by some amount
            // Remove all credits if the user force stops the app. It will slowly regain them
            // in response to different events.
            mAgent.reclaimAllAssetsLocked(userId, pkgName, EconomicPolicy.REGULATION_FORCE_STOP);
        }
    }