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

Commit 5bf4981e authored by Suprabh Shukla's avatar Suprabh Shukla
Browse files

Only user whitelist gets allow_while_idle_unrestricted

Due to earlier refactorings, now allow-in-power-save-except-idle apps
are getting the flag ALLOW_WHILE_IDLE_UNRESTRICTED, which should not
happen. Restricting to user whitelisted app ids as was the case in O.

Test: atest com.android.server.AppStateTrackerTest
atest android.alarmmanager.cts.AppStandbyTests
Also, manually,
adb shell cmd deviceidle whitelist +<package-name>
Then verify the app id appears in App state tracker dump in
adb shell dumpsys alarm

Bug: 74773710
Change-Id: I6fdce33446e1374c6672ce98769aa8b5844effa9
parent 41a60f40
Loading
Loading
Loading
Loading
+4 −1
Original line number Original line Diff line number Diff line
@@ -25,7 +25,7 @@ option java_multiple_files = true;


// Dump from com.android.server.ForceAppStandbyTracker.
// Dump from com.android.server.ForceAppStandbyTracker.
//
//
// Next ID: 12
// Next ID: 13
message ForceAppStandbyTrackerProto {
message ForceAppStandbyTrackerProto {
    option (.android.msg_privacy).dest = DEST_AUTOMATIC;
    option (.android.msg_privacy).dest = DEST_AUTOMATIC;


@@ -41,6 +41,9 @@ message ForceAppStandbyTrackerProto {
    // App ids that are in power-save whitelist.
    // App ids that are in power-save whitelist.
    repeated int32 power_save_whitelist_app_ids = 3;
    repeated int32 power_save_whitelist_app_ids = 3;


    // App ids that are in power-save user whitelist.
    repeated int32 power_save_user_whitelist_app_ids = 12;

    // App ids that are in temporary power-save whitelist.
    // App ids that are in temporary power-save whitelist.
    repeated int32 temp_power_save_whitelist_app_ids = 4;
    repeated int32 temp_power_save_whitelist_app_ids = 4;


+1 −1
Original line number Original line Diff line number Diff line
@@ -1776,7 +1776,7 @@ class AlarmManagerService extends SystemService {
            } else if (workSource == null && (callingUid < Process.FIRST_APPLICATION_UID
            } else if (workSource == null && (callingUid < Process.FIRST_APPLICATION_UID
                    || UserHandle.isSameApp(callingUid, mSystemUiUid)
                    || UserHandle.isSameApp(callingUid, mSystemUiUid)
                    || ((mAppStateTracker != null)
                    || ((mAppStateTracker != null)
                        && mAppStateTracker.isUidPowerSaveWhitelisted(callingUid)))) {
                        && mAppStateTracker.isUidPowerSaveUserWhitelisted(callingUid)))) {
                flags |= AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED;
                flags |= AlarmManager.FLAG_ALLOW_WHILE_IDLE_UNRESTRICTED;
                flags &= ~AlarmManager.FLAG_ALLOW_WHILE_IDLE;
                flags &= ~AlarmManager.FLAG_ALLOW_WHILE_IDLE;
            }
            }
+29 −3
Original line number Original line Diff line number Diff line
@@ -117,6 +117,12 @@ public class AppStateTracker {
    @GuardedBy("mLock")
    @GuardedBy("mLock")
    private int[] mPowerWhitelistedAllAppIds = new int[0];
    private int[] mPowerWhitelistedAllAppIds = new int[0];


    /**
     * User whitelisted apps in the device idle controller.
     */
    @GuardedBy("mLock")
    private int[] mPowerWhitelistedUserAppIds = new int[0];

    @GuardedBy("mLock")
    @GuardedBy("mLock")
    private int[] mTempWhitelistedAppIds = mPowerWhitelistedAllAppIds;
    private int[] mTempWhitelistedAppIds = mPowerWhitelistedAllAppIds;


@@ -983,13 +989,16 @@ public class AppStateTracker {
     * Called by device idle controller to update the power save whitelists.
     * Called by device idle controller to update the power save whitelists.
     */
     */
    public void setPowerSaveWhitelistAppIds(
    public void setPowerSaveWhitelistAppIds(
            int[] powerSaveWhitelistAllAppIdArray, int[] tempWhitelistAppIdArray) {
            int[] powerSaveWhitelistExceptIdleAppIdArray,
            int[] powerSaveWhitelistUserAppIdArray,
            int[] tempWhitelistAppIdArray) {
        synchronized (mLock) {
        synchronized (mLock) {
            final int[] previousWhitelist = mPowerWhitelistedAllAppIds;
            final int[] previousWhitelist = mPowerWhitelistedAllAppIds;
            final int[] previousTempWhitelist = mTempWhitelistedAppIds;
            final int[] previousTempWhitelist = mTempWhitelistedAppIds;


            mPowerWhitelistedAllAppIds = powerSaveWhitelistAllAppIdArray;
            mPowerWhitelistedAllAppIds = powerSaveWhitelistExceptIdleAppIdArray;
            mTempWhitelistedAppIds = tempWhitelistAppIdArray;
            mTempWhitelistedAppIds = tempWhitelistAppIdArray;
            mPowerWhitelistedUserAppIds = powerSaveWhitelistUserAppIdArray;


            if (isAnyAppIdUnwhitelisted(previousWhitelist, mPowerWhitelistedAllAppIds)) {
            if (isAnyAppIdUnwhitelisted(previousWhitelist, mPowerWhitelistedAllAppIds)) {
                mHandler.notifyAllUnwhitelisted();
                mHandler.notifyAllUnwhitelisted();
@@ -1193,6 +1202,16 @@ public class AppStateTracker {
        }
        }
    }
    }


    /**
     * @param uid the uid to check for
     * @return whether a UID is in the user defined power-save whitelist or not.
     */
    public boolean isUidPowerSaveUserWhitelisted(int uid) {
        synchronized (mLock) {
            return ArrayUtils.contains(mPowerWhitelistedUserAppIds, UserHandle.getAppId(uid));
        }
    }

    /**
    /**
     * @return whether a UID is in the temp power-save whitelist or not.
     * @return whether a UID is in the temp power-save whitelist or not.
     *
     *
@@ -1231,9 +1250,12 @@ public class AppStateTracker {
            pw.print("Foreground uids: ");
            pw.print("Foreground uids: ");
            dumpUids(pw, mForegroundUids);
            dumpUids(pw, mForegroundUids);


            pw.print("Whitelist appids: ");
            pw.print("Except-idle + user whitelist appids: ");
            pw.println(Arrays.toString(mPowerWhitelistedAllAppIds));
            pw.println(Arrays.toString(mPowerWhitelistedAllAppIds));


            pw.print("User whitelist appids: ");
            pw.println(Arrays.toString(mPowerWhitelistedUserAppIds));

            pw.print("Temp whitelist appids: ");
            pw.print("Temp whitelist appids: ");
            pw.println(Arrays.toString(mTempWhitelistedAppIds));
            pw.println(Arrays.toString(mTempWhitelistedAppIds));


@@ -1311,6 +1333,10 @@ public class AppStateTracker {
                proto.write(ForceAppStandbyTrackerProto.POWER_SAVE_WHITELIST_APP_IDS, appId);
                proto.write(ForceAppStandbyTrackerProto.POWER_SAVE_WHITELIST_APP_IDS, appId);
            }
            }


            for (int appId : mPowerWhitelistedUserAppIds) {
                proto.write(ForceAppStandbyTrackerProto.POWER_SAVE_USER_WHITELIST_APP_IDS, appId);
            }

            for (int appId : mTempWhitelistedAppIds) {
            for (int appId : mTempWhitelistedAppIds) {
                proto.write(ForceAppStandbyTrackerProto.TEMP_POWER_SAVE_WHITELIST_APP_IDS, appId);
                proto.write(ForceAppStandbyTrackerProto.TEMP_POWER_SAVE_WHITELIST_APP_IDS, appId);
            }
            }
+7 −6
Original line number Original line Diff line number Diff line
@@ -1540,7 +1540,7 @@ public class DeviceIdleController extends SystemService


                mLocalActivityManager.registerScreenObserver(mScreenObserver);
                mLocalActivityManager.registerScreenObserver(mScreenObserver);


                passWhiteListToForceAppStandbyTrackerLocked();
                passWhiteListsToForceAppStandbyTrackerLocked();
                updateInteractivityLocked();
                updateInteractivityLocked();
            }
            }
            updateConnectivityState(null);
            updateConnectivityState(null);
@@ -1631,7 +1631,7 @@ public class DeviceIdleController extends SystemService
                            mPowerSaveWhitelistAppsExceptIdle, mPowerSaveWhitelistUserApps,
                            mPowerSaveWhitelistAppsExceptIdle, mPowerSaveWhitelistUserApps,
                            mPowerSaveWhitelistExceptIdleAppIds);
                            mPowerSaveWhitelistExceptIdleAppIds);


                    passWhiteListToForceAppStandbyTrackerLocked();
                    passWhiteListsToForceAppStandbyTrackerLocked();
                }
                }
                return true;
                return true;
            } catch (PackageManager.NameNotFoundException e) {
            } catch (PackageManager.NameNotFoundException e) {
@@ -1650,7 +1650,7 @@ public class DeviceIdleController extends SystemService
                        mPowerSaveWhitelistExceptIdleAppIds);
                        mPowerSaveWhitelistExceptIdleAppIds);
                mPowerSaveWhitelistUserAppsExceptIdle.clear();
                mPowerSaveWhitelistUserAppsExceptIdle.clear();


                passWhiteListToForceAppStandbyTrackerLocked();
                passWhiteListsToForceAppStandbyTrackerLocked();
            }
            }
        }
        }
    }
    }
@@ -2589,7 +2589,7 @@ public class DeviceIdleController extends SystemService
            }
            }
            mLocalPowerManager.setDeviceIdleWhitelist(mPowerSaveWhitelistAllAppIdArray);
            mLocalPowerManager.setDeviceIdleWhitelist(mPowerSaveWhitelistAllAppIdArray);
        }
        }
        passWhiteListToForceAppStandbyTrackerLocked();
        passWhiteListsToForceAppStandbyTrackerLocked();
    }
    }


    private void updateTempWhitelistAppIdsLocked(int appId, boolean adding) {
    private void updateTempWhitelistAppIdsLocked(int appId, boolean adding) {
@@ -2615,7 +2615,7 @@ public class DeviceIdleController extends SystemService
            }
            }
            mLocalPowerManager.setDeviceIdleTempWhitelist(mTempWhitelistAppIdArray);
            mLocalPowerManager.setDeviceIdleTempWhitelist(mTempWhitelistAppIdArray);
        }
        }
        passWhiteListToForceAppStandbyTrackerLocked();
        passWhiteListsToForceAppStandbyTrackerLocked();
    }
    }


    private void reportPowerSaveWhitelistChangedLocked() {
    private void reportPowerSaveWhitelistChangedLocked() {
@@ -2630,9 +2630,10 @@ public class DeviceIdleController extends SystemService
        getContext().sendBroadcastAsUser(intent, UserHandle.SYSTEM);
        getContext().sendBroadcastAsUser(intent, UserHandle.SYSTEM);
    }
    }


    private void passWhiteListToForceAppStandbyTrackerLocked() {
    private void passWhiteListsToForceAppStandbyTrackerLocked() {
        mAppStateTracker.setPowerSaveWhitelistAppIds(
        mAppStateTracker.setPowerSaveWhitelistAppIds(
                mPowerSaveWhitelistExceptIdleAppIdArray,
                mPowerSaveWhitelistExceptIdleAppIdArray,
                mPowerSaveWhitelistUserAppIdArray,
                mTempWhitelistAppIdArray);
                mTempWhitelistAppIdArray);
    }
    }


+20 −9
Original line number Original line Diff line number Diff line
@@ -445,7 +445,7 @@ public class AppStateTrackerTest {
        areRestricted(instance, UID_10_3, PACKAGE_3, JOBS_AND_ALARMS);
        areRestricted(instance, UID_10_3, PACKAGE_3, JOBS_AND_ALARMS);
        areRestricted(instance, Process.SYSTEM_UID, PACKAGE_SYSTEM, NONE);
        areRestricted(instance, Process.SYSTEM_UID, PACKAGE_SYSTEM, NONE);


        instance.setPowerSaveWhitelistAppIds(new int[] {UID_1}, new int[] {UID_2});
        instance.setPowerSaveWhitelistAppIds(new int[] {UID_1}, new int[] {}, new int[] {UID_2});


        areRestricted(instance, UID_1, PACKAGE_1, NONE);
        areRestricted(instance, UID_1, PACKAGE_1, NONE);
        areRestricted(instance, UID_10_1, PACKAGE_1, NONE);
        areRestricted(instance, UID_10_1, PACKAGE_1, NONE);
@@ -481,6 +481,15 @@ public class AppStateTrackerTest {
        assertTrue(instance.isUidTempPowerSaveWhitelisted(UID_10_2));
        assertTrue(instance.isUidTempPowerSaveWhitelisted(UID_10_2));
    }
    }


    @Test
    public void testPowerSaveUserWhitelist() throws Exception {
        final AppStateTrackerTestable instance = newInstance();
        instance.setPowerSaveWhitelistAppIds(new int[] {}, new int[] {UID_1, UID_2}, new int[] {});
        assertTrue(instance.isUidPowerSaveUserWhitelisted(UID_1));
        assertTrue(instance.isUidPowerSaveUserWhitelisted(UID_2));
        assertFalse(instance.isUidPowerSaveUserWhitelisted(UID_3));
    }

    @Test
    @Test
    public void testUidStateForeground() throws Exception {
    public void testUidStateForeground() throws Exception {
        final AppStateTrackerTestable instance = newInstance();
        final AppStateTrackerTestable instance = newInstance();
@@ -861,7 +870,7 @@ public class AppStateTrackerTest {
        // -------------------------------------------------------------------------
        // -------------------------------------------------------------------------
        // Tests with system/user/temp whitelist.
        // Tests with system/user/temp whitelist.


        instance.setPowerSaveWhitelistAppIds(new int[] {UID_1, UID_2}, new int[] {});
        instance.setPowerSaveWhitelistAppIds(new int[] {UID_1, UID_2}, new int[] {}, new int[] {});


        waitUntilMainHandlerDrain();
        waitUntilMainHandlerDrain();
        verify(l, times(1)).updateAllJobs();
        verify(l, times(1)).updateAllJobs();
@@ -873,7 +882,7 @@ public class AppStateTrackerTest {
        verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString());
        verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString());
        reset(l);
        reset(l);


        instance.setPowerSaveWhitelistAppIds(new int[] {UID_2}, new int[] {});
        instance.setPowerSaveWhitelistAppIds(new int[] {UID_2}, new int[] {}, new int[] {});


        waitUntilMainHandlerDrain();
        waitUntilMainHandlerDrain();
        verify(l, times(1)).updateAllJobs();
        verify(l, times(1)).updateAllJobs();
@@ -886,7 +895,8 @@ public class AppStateTrackerTest {
        reset(l);
        reset(l);


        // Update temp whitelist.
        // Update temp whitelist.
        instance.setPowerSaveWhitelistAppIds(new int[] {UID_2}, new int[] {UID_1, UID_3});
        instance.setPowerSaveWhitelistAppIds(new int[] {UID_2}, new int[] {},
                new int[] {UID_1, UID_3});


        waitUntilMainHandlerDrain();
        waitUntilMainHandlerDrain();
        verify(l, times(1)).updateAllJobs();
        verify(l, times(1)).updateAllJobs();
@@ -898,7 +908,7 @@ public class AppStateTrackerTest {
        verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString());
        verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString());
        reset(l);
        reset(l);


        instance.setPowerSaveWhitelistAppIds(new int[] {UID_2}, new int[] {UID_3});
        instance.setPowerSaveWhitelistAppIds(new int[] {UID_2}, new int[] {}, new int[] {UID_3});


        waitUntilMainHandlerDrain();
        waitUntilMainHandlerDrain();
        verify(l, times(1)).updateAllJobs();
        verify(l, times(1)).updateAllJobs();
@@ -924,7 +934,7 @@ public class AppStateTrackerTest {
        verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString());
        verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString());
        reset(l);
        reset(l);


        instance.setPowerSaveWhitelistAppIds(new int[] {UID_1, UID_2}, new int[] {});
        instance.setPowerSaveWhitelistAppIds(new int[] {UID_1, UID_2}, new int[] {}, new int[] {});


        waitUntilMainHandlerDrain();
        waitUntilMainHandlerDrain();
        // Called once for updating all whitelist and once for updating temp whitelist
        // Called once for updating all whitelist and once for updating temp whitelist
@@ -937,7 +947,7 @@ public class AppStateTrackerTest {
        verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString());
        verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString());
        reset(l);
        reset(l);


        instance.setPowerSaveWhitelistAppIds(new int[] {UID_2}, new int[] {});
        instance.setPowerSaveWhitelistAppIds(new int[] {UID_2}, new int[] {}, new int[] {});


        waitUntilMainHandlerDrain();
        waitUntilMainHandlerDrain();
        verify(l, times(1)).updateAllJobs();
        verify(l, times(1)).updateAllJobs();
@@ -950,7 +960,8 @@ public class AppStateTrackerTest {
        reset(l);
        reset(l);


        // Update temp whitelist.
        // Update temp whitelist.
        instance.setPowerSaveWhitelistAppIds(new int[] {UID_2}, new int[] {UID_1, UID_3});
        instance.setPowerSaveWhitelistAppIds(new int[] {UID_2}, new int[] {},
                new int[] {UID_1, UID_3});


        waitUntilMainHandlerDrain();
        waitUntilMainHandlerDrain();
        verify(l, times(1)).updateAllJobs();
        verify(l, times(1)).updateAllJobs();
@@ -962,7 +973,7 @@ public class AppStateTrackerTest {
        verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString());
        verify(l, times(0)).unblockAlarmsForUidPackage(anyInt(), anyString());
        reset(l);
        reset(l);


        instance.setPowerSaveWhitelistAppIds(new int[] {UID_2}, new int[] {UID_3});
        instance.setPowerSaveWhitelistAppIds(new int[] {UID_2}, new int[] {}, new int[] {UID_3});


        waitUntilMainHandlerDrain();
        waitUntilMainHandlerDrain();
        verify(l, times(1)).updateAllJobs();
        verify(l, times(1)).updateAllJobs();