Loading core/java/android/window/flags/responsible_apis.aconfig +7 −0 Original line number Original line Diff line number Diff line Loading @@ -81,3 +81,10 @@ flag { description: "Strict mode violation triggered by grace period usage" description: "Strict mode violation triggered by grace period usage" bug: "384807495" bug: "384807495" } } flag { name: "bal_clear_allowlist_duration" namespace: "responsible_apis" description: "Clear the allowlist duration when clearAllowBgActivityStarts is called" bug: "322159724" } services/core/java/com/android/server/am/PendingIntentRecord.java +15 −1 Original line number Original line Diff line number Diff line Loading @@ -24,11 +24,14 @@ import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_COMPAT; import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_DENIED; import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_DENIED; import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED; import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED; import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOW_IF_VISIBLE; import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOW_IF_VISIBLE; import static android.os.PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED; import static android.os.PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED; import static android.os.Process.ROOT_UID; import static android.os.Process.ROOT_UID; import static android.os.Process.SYSTEM_UID; import static android.os.Process.SYSTEM_UID; import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; import static com.android.window.flags.Flags.balClearAllowlistDuration; import android.annotation.IntDef; import android.annotation.IntDef; import android.annotation.Nullable; import android.annotation.Nullable; Loading Loading @@ -305,6 +308,10 @@ public final class PendingIntentRecord extends IIntentSender.Stub { this.stringName = null; this.stringName = null; } } @VisibleForTesting TempAllowListDuration getAllowlistDurationLocked(IBinder allowlistToken) { return mAllowlistDuration.get(allowlistToken); } void setAllowBgActivityStarts(IBinder token, int flags) { void setAllowBgActivityStarts(IBinder token, int flags) { if (token == null) return; if (token == null) return; if ((flags & FLAG_ACTIVITY_SENDER) != 0) { if ((flags & FLAG_ACTIVITY_SENDER) != 0) { Loading @@ -323,6 +330,13 @@ public final class PendingIntentRecord extends IIntentSender.Stub { mAllowBgActivityStartsForActivitySender.remove(token); mAllowBgActivityStartsForActivitySender.remove(token); mAllowBgActivityStartsForBroadcastSender.remove(token); mAllowBgActivityStartsForBroadcastSender.remove(token); mAllowBgActivityStartsForServiceSender.remove(token); mAllowBgActivityStartsForServiceSender.remove(token); if (mAllowlistDuration != null && balClearAllowlistDuration()) { TempAllowListDuration duration = mAllowlistDuration.get(token); if (duration != null && duration.type == TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED) { duration.type = TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED; } } } } public void registerCancelListenerLocked(IResultReceiver receiver) { public void registerCancelListenerLocked(IResultReceiver receiver) { Loading Loading @@ -703,7 +717,7 @@ public final class PendingIntentRecord extends IIntentSender.Stub { return res; return res; } } private BackgroundStartPrivileges getBackgroundStartPrivilegesForActivitySender( @VisibleForTesting BackgroundStartPrivileges getBackgroundStartPrivilegesForActivitySender( IBinder allowlistToken) { IBinder allowlistToken) { return mAllowBgActivityStartsForActivitySender.contains(allowlistToken) return mAllowBgActivityStartsForActivitySender.contains(allowlistToken) ? BackgroundStartPrivileges.allowBackgroundActivityStarts(allowlistToken) ? BackgroundStartPrivileges.allowBackgroundActivityStarts(allowlistToken) Loading services/tests/mockingservicestests/src/com/android/server/am/PendingIntentControllerTest.java +43 −0 Original line number Original line Diff line number Diff line Loading @@ -16,6 +16,9 @@ package com.android.server.am; package com.android.server.am; import static android.os.PowerWhitelistManager.REASON_NOTIFICATION_SERVICE; import static android.os.PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED; import static android.os.PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED; import static android.os.Process.INVALID_UID; import static android.os.Process.INVALID_UID; import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; Loading @@ -27,9 +30,12 @@ import static com.android.server.am.PendingIntentRecord.CANCEL_REASON_OWNER_CANC import static com.android.server.am.PendingIntentRecord.CANCEL_REASON_OWNER_FORCE_STOPPED; import static com.android.server.am.PendingIntentRecord.CANCEL_REASON_OWNER_FORCE_STOPPED; import static com.android.server.am.PendingIntentRecord.CANCEL_REASON_SUPERSEDED; import static com.android.server.am.PendingIntentRecord.CANCEL_REASON_SUPERSEDED; import static com.android.server.am.PendingIntentRecord.CANCEL_REASON_USER_STOPPED; import static com.android.server.am.PendingIntentRecord.CANCEL_REASON_USER_STOPPED; import static com.android.server.am.PendingIntentRecord.FLAG_ACTIVITY_SENDER; import static com.android.server.am.PendingIntentRecord.cancelReasonToString; import static com.android.server.am.PendingIntentRecord.cancelReasonToString; import static com.android.window.flags.Flags.balClearAllowlistDuration; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq; Loading @@ -39,9 +45,11 @@ import static org.mockito.Mockito.when; import android.app.ActivityManager; import android.app.ActivityManager; import android.app.ActivityManagerInternal; import android.app.ActivityManagerInternal; import android.app.AppGlobals; import android.app.AppGlobals; import android.app.BackgroundStartPrivileges; import android.app.PendingIntent; import android.app.PendingIntent; import android.content.Intent; import android.content.Intent; import android.content.pm.IPackageManager; import android.content.pm.IPackageManager; import android.os.Binder; import android.os.Looper; import android.os.Looper; import android.os.UserHandle; import android.os.UserHandle; Loading Loading @@ -179,6 +187,41 @@ public class PendingIntentControllerTest { } } } } @Test public void testClearAllowBgActivityStartsClearsToken() { final PendingIntentRecord pir = createPendingIntentRecord(0); Binder token = new Binder(); pir.setAllowBgActivityStarts(token, FLAG_ACTIVITY_SENDER); assertEquals(BackgroundStartPrivileges.allowBackgroundActivityStarts(token), pir.getBackgroundStartPrivilegesForActivitySender(token)); pir.clearAllowBgActivityStarts(token); assertEquals(BackgroundStartPrivileges.NONE, pir.getBackgroundStartPrivilegesForActivitySender(token)); } @Test public void testClearAllowBgActivityStartsClearsDuration() { final PendingIntentRecord pir = createPendingIntentRecord(0); Binder token = new Binder(); pir.setAllowlistDurationLocked(token, 1000, TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED, REASON_NOTIFICATION_SERVICE, "NotificationManagerService"); PendingIntentRecord.TempAllowListDuration allowlistDurationLocked = pir.getAllowlistDurationLocked(token); assertEquals(1000, allowlistDurationLocked.duration); assertEquals(TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED, allowlistDurationLocked.type); pir.clearAllowBgActivityStarts(token); PendingIntentRecord.TempAllowListDuration allowlistDurationLockedAfterClear = pir.getAllowlistDurationLocked(token); assertNotNull(allowlistDurationLockedAfterClear); assertEquals(1000, allowlistDurationLockedAfterClear.duration); assertEquals(balClearAllowlistDuration() ? TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED : TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED, allowlistDurationLocked.type); } private void assertCancelReason(int expectedReason, int actualReason) { private void assertCancelReason(int expectedReason, int actualReason) { final String errMsg = "Expected: " + cancelReasonToString(expectedReason) final String errMsg = "Expected: " + cancelReasonToString(expectedReason) + "; Actual: " + cancelReasonToString(actualReason); + "; Actual: " + cancelReasonToString(actualReason); Loading Loading
core/java/android/window/flags/responsible_apis.aconfig +7 −0 Original line number Original line Diff line number Diff line Loading @@ -81,3 +81,10 @@ flag { description: "Strict mode violation triggered by grace period usage" description: "Strict mode violation triggered by grace period usage" bug: "384807495" bug: "384807495" } } flag { name: "bal_clear_allowlist_duration" namespace: "responsible_apis" description: "Clear the allowlist duration when clearAllowBgActivityStarts is called" bug: "322159724" }
services/core/java/com/android/server/am/PendingIntentRecord.java +15 −1 Original line number Original line Diff line number Diff line Loading @@ -24,11 +24,14 @@ import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_COMPAT; import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_DENIED; import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_DENIED; import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED; import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_SYSTEM_DEFINED; import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOW_IF_VISIBLE; import static android.app.ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOW_IF_VISIBLE; import static android.os.PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED; import static android.os.PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED; import static android.os.Process.ROOT_UID; import static android.os.Process.ROOT_UID; import static android.os.Process.SYSTEM_UID; import static android.os.Process.SYSTEM_UID; import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; import static com.android.window.flags.Flags.balClearAllowlistDuration; import android.annotation.IntDef; import android.annotation.IntDef; import android.annotation.Nullable; import android.annotation.Nullable; Loading Loading @@ -305,6 +308,10 @@ public final class PendingIntentRecord extends IIntentSender.Stub { this.stringName = null; this.stringName = null; } } @VisibleForTesting TempAllowListDuration getAllowlistDurationLocked(IBinder allowlistToken) { return mAllowlistDuration.get(allowlistToken); } void setAllowBgActivityStarts(IBinder token, int flags) { void setAllowBgActivityStarts(IBinder token, int flags) { if (token == null) return; if (token == null) return; if ((flags & FLAG_ACTIVITY_SENDER) != 0) { if ((flags & FLAG_ACTIVITY_SENDER) != 0) { Loading @@ -323,6 +330,13 @@ public final class PendingIntentRecord extends IIntentSender.Stub { mAllowBgActivityStartsForActivitySender.remove(token); mAllowBgActivityStartsForActivitySender.remove(token); mAllowBgActivityStartsForBroadcastSender.remove(token); mAllowBgActivityStartsForBroadcastSender.remove(token); mAllowBgActivityStartsForServiceSender.remove(token); mAllowBgActivityStartsForServiceSender.remove(token); if (mAllowlistDuration != null && balClearAllowlistDuration()) { TempAllowListDuration duration = mAllowlistDuration.get(token); if (duration != null && duration.type == TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED) { duration.type = TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED; } } } } public void registerCancelListenerLocked(IResultReceiver receiver) { public void registerCancelListenerLocked(IResultReceiver receiver) { Loading Loading @@ -703,7 +717,7 @@ public final class PendingIntentRecord extends IIntentSender.Stub { return res; return res; } } private BackgroundStartPrivileges getBackgroundStartPrivilegesForActivitySender( @VisibleForTesting BackgroundStartPrivileges getBackgroundStartPrivilegesForActivitySender( IBinder allowlistToken) { IBinder allowlistToken) { return mAllowBgActivityStartsForActivitySender.contains(allowlistToken) return mAllowBgActivityStartsForActivitySender.contains(allowlistToken) ? BackgroundStartPrivileges.allowBackgroundActivityStarts(allowlistToken) ? BackgroundStartPrivileges.allowBackgroundActivityStarts(allowlistToken) Loading
services/tests/mockingservicestests/src/com/android/server/am/PendingIntentControllerTest.java +43 −0 Original line number Original line Diff line number Diff line Loading @@ -16,6 +16,9 @@ package com.android.server.am; package com.android.server.am; import static android.os.PowerWhitelistManager.REASON_NOTIFICATION_SERVICE; import static android.os.PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED; import static android.os.PowerWhitelistManager.TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED; import static android.os.Process.INVALID_UID; import static android.os.Process.INVALID_UID; import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; Loading @@ -27,9 +30,12 @@ import static com.android.server.am.PendingIntentRecord.CANCEL_REASON_OWNER_CANC import static com.android.server.am.PendingIntentRecord.CANCEL_REASON_OWNER_FORCE_STOPPED; import static com.android.server.am.PendingIntentRecord.CANCEL_REASON_OWNER_FORCE_STOPPED; import static com.android.server.am.PendingIntentRecord.CANCEL_REASON_SUPERSEDED; import static com.android.server.am.PendingIntentRecord.CANCEL_REASON_SUPERSEDED; import static com.android.server.am.PendingIntentRecord.CANCEL_REASON_USER_STOPPED; import static com.android.server.am.PendingIntentRecord.CANCEL_REASON_USER_STOPPED; import static com.android.server.am.PendingIntentRecord.FLAG_ACTIVITY_SENDER; import static com.android.server.am.PendingIntentRecord.cancelReasonToString; import static com.android.server.am.PendingIntentRecord.cancelReasonToString; import static com.android.window.flags.Flags.balClearAllowlistDuration; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq; Loading @@ -39,9 +45,11 @@ import static org.mockito.Mockito.when; import android.app.ActivityManager; import android.app.ActivityManager; import android.app.ActivityManagerInternal; import android.app.ActivityManagerInternal; import android.app.AppGlobals; import android.app.AppGlobals; import android.app.BackgroundStartPrivileges; import android.app.PendingIntent; import android.app.PendingIntent; import android.content.Intent; import android.content.Intent; import android.content.pm.IPackageManager; import android.content.pm.IPackageManager; import android.os.Binder; import android.os.Looper; import android.os.Looper; import android.os.UserHandle; import android.os.UserHandle; Loading Loading @@ -179,6 +187,41 @@ public class PendingIntentControllerTest { } } } } @Test public void testClearAllowBgActivityStartsClearsToken() { final PendingIntentRecord pir = createPendingIntentRecord(0); Binder token = new Binder(); pir.setAllowBgActivityStarts(token, FLAG_ACTIVITY_SENDER); assertEquals(BackgroundStartPrivileges.allowBackgroundActivityStarts(token), pir.getBackgroundStartPrivilegesForActivitySender(token)); pir.clearAllowBgActivityStarts(token); assertEquals(BackgroundStartPrivileges.NONE, pir.getBackgroundStartPrivilegesForActivitySender(token)); } @Test public void testClearAllowBgActivityStartsClearsDuration() { final PendingIntentRecord pir = createPendingIntentRecord(0); Binder token = new Binder(); pir.setAllowlistDurationLocked(token, 1000, TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED, REASON_NOTIFICATION_SERVICE, "NotificationManagerService"); PendingIntentRecord.TempAllowListDuration allowlistDurationLocked = pir.getAllowlistDurationLocked(token); assertEquals(1000, allowlistDurationLocked.duration); assertEquals(TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED, allowlistDurationLocked.type); pir.clearAllowBgActivityStarts(token); PendingIntentRecord.TempAllowListDuration allowlistDurationLockedAfterClear = pir.getAllowlistDurationLocked(token); assertNotNull(allowlistDurationLockedAfterClear); assertEquals(1000, allowlistDurationLockedAfterClear.duration); assertEquals(balClearAllowlistDuration() ? TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED : TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED, allowlistDurationLocked.type); } private void assertCancelReason(int expectedReason, int actualReason) { private void assertCancelReason(int expectedReason, int actualReason) { final String errMsg = "Expected: " + cancelReasonToString(expectedReason) final String errMsg = "Expected: " + cancelReasonToString(expectedReason) + "; Actual: " + cancelReasonToString(actualReason); + "; Actual: " + cancelReasonToString(actualReason); Loading