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

Commit 7e13fc66 authored by Presubmit Automerger Backend's avatar Presubmit Automerger Backend
Browse files

[automerge] Use the reason code from standby bucketing in bg restriction if needed 2p: 32bef9c5

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/17383576

Bug: 225951652
Bug: 203105544
Change-Id: Id4c3c21a5b5de477cee6d7553f7360c992f07569
parents de88b04b 32bef9c5
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
package com.android.server.usage;

import android.annotation.CurrentTimeMillisLong;
import android.annotation.ElapsedRealtimeLong;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
@@ -149,6 +150,12 @@ public interface AppStandbyInternal {
    int getAppMinStandbyBucket(String packageName, int appId, int userId,
            boolean shouldObfuscateInstantApps);

    /**
     * Return the bucketing reason code of the given app.
     */
    int getAppStandbyBucketReason(@NonNull String packageName, @UserIdInt int userId,
            @ElapsedRealtimeLong long elapsedRealtime);

    /**
     * Put the specified app in the
     * {@link android.app.usage.UsageStatsManager#STANDBY_BUCKET_RESTRICTED}
+2 −2
Original line number Diff line number Diff line
@@ -1436,8 +1436,8 @@ public class AppStandbyController
        }
    }

    @VisibleForTesting
    int getAppStandbyBucketReason(String packageName, int userId, long elapsedRealtime) {
    @Override
    public int getAppStandbyBucketReason(String packageName, int userId, long elapsedRealtime) {
        synchronized (mAppIdleLock) {
            return mAppIdleHistory.getAppStandbyReason(packageName, userId, elapsedRealtime);
        }
+41 −10
Original line number Diff line number Diff line
@@ -1086,6 +1086,12 @@ public final class AppRestrictionController {
            }
            final @RestrictionLevel int level = calcAppRestrictionLevel(
                    userId, uid, info.mPackageName, info.mStandbyBucket, false, false);
            if (DEBUG_BG_RESTRICTION_CONTROLLER) {
                Slog.i(TAG, "Proposed restriction level of " + info.mPackageName + "/"
                        + UserHandle.formatUid(uid) + ": "
                        + ActivityManager.restrictionLevelToName(level)
                        + " " + info.mStandbyBucket);
            }
            applyRestrictionLevel(info.mPackageName, uid, level,
                    info.mStandbyBucket, true, reason, subReason);
        }
@@ -1185,6 +1191,25 @@ public final class AppRestrictionController {
        return level;
    }

    private static @RestrictionLevel int standbyBucketToRestrictionLevel(
            @UsageStatsManager.StandbyBuckets int standbyBucket) {
        switch (standbyBucket) {
            case STANDBY_BUCKET_EXEMPTED:
                return RESTRICTION_LEVEL_EXEMPTED;
            case STANDBY_BUCKET_NEVER:
                return RESTRICTION_LEVEL_BACKGROUND_RESTRICTED;
            case STANDBY_BUCKET_ACTIVE:
            case STANDBY_BUCKET_WORKING_SET:
            case STANDBY_BUCKET_FREQUENT:
            case STANDBY_BUCKET_RARE:
                return RESTRICTION_LEVEL_ADAPTIVE_BUCKET;
            case STANDBY_BUCKET_RESTRICTED:
                return RESTRICTION_LEVEL_RESTRICTED_BUCKET;
            default:
                return RESTRICTION_LEVEL_UNKNOWN;
        }
    }

    /**
     * Get the restriction level of the given UID, if it hosts multiple packages,
     * return least restricted one (or if any of them is exempted).
@@ -1380,12 +1405,23 @@ public final class AppRestrictionController {
            int curBucket, boolean allowUpdateBucket, int reason, int subReason) {
        int curLevel;
        int prevReason;
        final AppStandbyInternal appStandbyInternal = mInjector.getAppStandbyInternal();
        synchronized (mSettingsLock) {
            curLevel = getRestrictionLevel(uid, pkgName);
            if (curLevel == level) {
                // Nothing to do.
                return;
            }
            final int levelOfBucket = standbyBucketToRestrictionLevel(curBucket);
            if (levelOfBucket == level) {
                // If the standby bucket yield the same level, use the reason from standby bucket.
                final int bucketReason = appStandbyInternal.getAppStandbyBucketReason(
                        pkgName, UserHandle.getUserId(uid), SystemClock.elapsedRealtime());
                if (bucketReason != 0) {
                    reason = bucketReason & REASON_MAIN_MASK;
                    subReason = bucketReason & REASON_SUB_MASK;
                }
            }
            if (DEBUG_BG_RESTRICTION_CONTROLLER) {
                Slog.i(TAG, "Updating the restriction level of " + pkgName + "/"
                        + UserHandle.formatUid(uid) + " from "
@@ -1393,7 +1429,6 @@ public final class AppRestrictionController {
                        + ActivityManager.restrictionLevelToName(level)
                        + " reason=" + reason + ", subReason=" + subReason);
            }

            prevReason = mRestrictionSettings.getReason(pkgName, uid);
            mRestrictionSettings.update(pkgName, uid, level, reason, subReason);
        }
@@ -1401,7 +1436,6 @@ public final class AppRestrictionController {
        if (!allowUpdateBucket || curBucket == STANDBY_BUCKET_EXEMPTED) {
            return;
        }
        final AppStandbyInternal appStandbyInternal = mInjector.getAppStandbyInternal();
        if (level >= RESTRICTION_LEVEL_RESTRICTED_BUCKET
                && curLevel < RESTRICTION_LEVEL_RESTRICTED_BUCKET) {
            if (!mConstantsObserver.mRestrictedBucketEnabled) {
@@ -1422,8 +1456,10 @@ public final class AppRestrictionController {
                    final int index = mActiveUids.indexOfKey(uid, pkgName);
                    if (index >= 0) {
                        // It's currently active, enqueue it.
                        final int localReason = reason;
                        final int localSubReason = subReason;
                        mActiveUids.add(uid, pkgName, () -> appStandbyInternal.restrictApp(
                                pkgName, UserHandle.getUserId(uid), reason, subReason));
                                pkgName, UserHandle.getUserId(uid), localReason, localSubReason));
                        doIt = false;
                    }
                }
@@ -1841,9 +1877,6 @@ public final class AppRestrictionController {
    }

    void handleUidInactive(int uid, boolean disabled) {
        if (!mConstantsObserver.mBgAutoRestrictedBucket) {
            return;
        }
        final ArrayList<Runnable> pendingTasks = mTmpRunnables;
        synchronized (mSettingsLock) {
            final int index = mActiveUids.indexOfKey(uid);
@@ -1866,14 +1899,12 @@ public final class AppRestrictionController {
    }

    void handleUidActive(int uid) {
        if (!mConstantsObserver.mBgAutoRestrictedBucket) {
            return;
        }
        synchronized (mSettingsLock) {
            final AppStandbyInternal appStandbyInternal = mInjector.getAppStandbyInternal();
            final int userId = UserHandle.getUserId(uid);
            mRestrictionSettings.forEachPackageInUidLocked(uid, (pkgName, level, reason) -> {
                if (level == RESTRICTION_LEVEL_BACKGROUND_RESTRICTED) {
                if (mConstantsObserver.mBgAutoRestrictedBucket
                        && level == RESTRICTION_LEVEL_BACKGROUND_RESTRICTED) {
                    mActiveUids.add(uid, pkgName, () -> appStandbyInternal.restrictApp(pkgName,
                            userId, reason & REASON_MAIN_MASK, reason & REASON_SUB_MASK));
                } else {