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

Commit cd5c441f authored by Xin Guan's avatar Xin Guan Committed by Android (Google) Code Review
Browse files

Merge "Bypass screen time check for bucket evaluation" into main

parents 9da498e1 99e54e0f
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -12,3 +12,12 @@ flag {
    }
}

flag {
    name: "screen_time_bypass"
    namespace: "backstage_power"
    description: "Bypass the screen time check for bucket evaluation"
    bug: "374114769"
    metadata {
       purpose: PURPOSE_BUGFIX
    }
}
+7 −6
Original line number Diff line number Diff line
@@ -629,14 +629,15 @@ public class AppIdleHistory {
    }

    /**
     * Returns the index in the arrays of screenTimeThresholds and elapsedTimeThresholds
     * that corresponds to how long since the app was used.
     * Returns the index in the array of elapsedTimeThresholds that corresponds to
     * how long since the app was used.
     * @param packageName
     * @param userId
     * @param elapsedRealtime current time
     * @param screenTimeThresholds Array of screen times, in ascending order, first one is 0
     * @param screenTimeThresholds Array of screen times, in ascending order,
     *        first one is 0 (this will not be used any more)
     * @param elapsedTimeThresholds Array of elapsed time, in ascending order, first one is 0
     * @return The index whose values the app's used time exceeds (in both arrays) or {@code -1} to
     * @return The index whose values the app's used time exceeds or {@code -1} to
     *         indicate that the app has never been used.
     */
    int getThresholdIndex(String packageName, int userId, long elapsedRealtime,
@@ -646,7 +647,7 @@ public class AppIdleHistory {
                elapsedRealtime, false);
        // If we don't have any state for the app, assume never used
        if (appUsageHistory == null || appUsageHistory.lastUsedElapsedTime < 0
                || appUsageHistory.lastUsedScreenTime < 0) {
                || (!Flags.screenTimeBypass() && appUsageHistory.lastUsedScreenTime < 0)) {
            return -1;
        }

@@ -659,7 +660,7 @@ public class AppIdleHistory {
        if (DEBUG) Slog.d(TAG, packageName + " screenOn=" + screenOnDelta
                + ", elapsed=" + elapsedDelta);
        for (int i = screenTimeThresholds.length - 1; i >= 0; i--) {
            if (screenOnDelta >= screenTimeThresholds[i]
            if ((Flags.screenTimeBypass() || screenOnDelta >= screenTimeThresholds[i])
                && elapsedDelta >= elapsedTimeThresholds[i]) {
                return i;
            }
+34 −1
Original line number Diff line number Diff line
@@ -93,7 +93,10 @@ import android.os.Handler;
import android.os.Looper;
import android.os.RemoteException;
import android.os.UserHandle;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.annotations.Presubmit;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.DeviceConfig;
import android.util.ArraySet;
import android.util.Pair;
@@ -111,6 +114,7 @@ import com.android.server.usage.AppStandbyInternal.AppIdleStateChangeListener;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -185,6 +189,9 @@ public class AppStandbyControllerTests {

    private static final Random sRandom = new Random();

    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    private MyInjector mInjector;
    private AppStandbyController mController;

@@ -894,7 +901,8 @@ public class AppStandbyControllerTests {
    }

    @Test
    public void testScreenTimeAndBuckets() throws Exception {
    @DisableFlags(Flags.FLAG_SCREEN_TIME_BYPASS)
    public void testScreenTimeAndBuckets_Legacy() throws Exception {
        mInjector.setDisplayOn(false);

        assertTimeout(mController, 0, STANDBY_BUCKET_NEVER);
@@ -916,6 +924,31 @@ public class AppStandbyControllerTests {
        assertTimeout(mController, RARE_THRESHOLD + 2 * HOUR_MS + 1, STANDBY_BUCKET_RARE);
    }

    @Test
    @EnableFlags(Flags.FLAG_SCREEN_TIME_BYPASS)
    public void testScreenTimeAndBuckets_Bypass() throws Exception {
        mInjector.setDisplayOn(false);

        assertTimeout(mController, 0, STANDBY_BUCKET_NEVER);

        reportEvent(mController, USER_INTERACTION, 0, PACKAGE_1);

        // ACTIVE bucket
        assertTimeout(mController, WORKING_SET_THRESHOLD - 1, STANDBY_BUCKET_ACTIVE);

        // WORKING_SET bucket
        assertTimeout(mController, WORKING_SET_THRESHOLD + 1, STANDBY_BUCKET_WORKING_SET);

        // RARE bucket, should failed due to timeout hasn't reached yet.
        mInjector.mElapsedRealtime = RARE_THRESHOLD - 1;
        mController.checkIdleStates(USER_ID);
        waitAndAssertNotBucket(STANDBY_BUCKET_RARE, PACKAGE_1);

        mInjector.setDisplayOn(true);
        // screen on time doesn't count.
        assertTimeout(mController, RARE_THRESHOLD + 1, STANDBY_BUCKET_RARE);
    }

    @Test
    public void testForcedIdle() throws Exception {
        mController.forceIdleState(PACKAGE_1, USER_ID, true);