diff options
4 files changed, 87 insertions, 2 deletions
diff --git a/core/java/android/app/BackgroundStartPrivileges.java b/core/java/android/app/BackgroundStartPrivileges.java index 76c0ccf98322..53e50e7ae6f6 100644 --- a/core/java/android/app/BackgroundStartPrivileges.java +++ b/core/java/android/app/BackgroundStartPrivileges.java @@ -23,12 +23,13 @@ import android.os.IBinder; import com.android.internal.util.Preconditions; import java.util.List; +import java.util.Objects; /** * Privileges granted to a Process that allows it to execute starts from the background. * @hide */ -public class BackgroundStartPrivileges { +public final class BackgroundStartPrivileges { /** No privileges. */ public static final BackgroundStartPrivileges NONE = new BackgroundStartPrivileges( false, false, null); @@ -181,4 +182,22 @@ public class BackgroundStartPrivileges { + ", originatingToken=" + mOriginatingToken + ']'; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + BackgroundStartPrivileges that = (BackgroundStartPrivileges) o; + return mAllowsBackgroundActivityStarts == that.mAllowsBackgroundActivityStarts + && mAllowsBackgroundForegroundServiceStarts + == that.mAllowsBackgroundForegroundServiceStarts + && Objects.equals(mOriginatingToken, that.mOriginatingToken); + } + + @Override + public int hashCode() { + return Objects.hash(mAllowsBackgroundActivityStarts, + mAllowsBackgroundForegroundServiceStarts, + mOriginatingToken); + } } diff --git a/core/tests/coretests/src/android/app/BackgroundStartPrivilegesTest.java b/core/tests/coretests/src/android/app/BackgroundStartPrivilegesTest.java index 982ad63d7e8d..d661e75d7256 100644 --- a/core/tests/coretests/src/android/app/BackgroundStartPrivilegesTest.java +++ b/core/tests/coretests/src/android/app/BackgroundStartPrivilegesTest.java @@ -119,4 +119,15 @@ public class BackgroundStartPrivilegesTest { Arrays.asList(BSP_ALLOW_A, BSP_ALLOW_A, BSP_ALLOW_A, BSP_ALLOW_A))) .isEqualTo(BSP_ALLOW_A); } + + @Test + public void backgroundStartPrivilege_equals_works() { + assertThat(NONE.equals(NONE)).isTrue(); + assertThat(ALLOW_BAL.equals(ALLOW_BAL)).isTrue(); + assertThat(ALLOW_FGS.equals(ALLOW_FGS)).isTrue(); + assertThat(BSP_ALLOW_A.equals(BSP_ALLOW_A)).isTrue(); + assertThat(NONE).isNotEqualTo(ALLOW_BAL); + assertThat(ALLOW_FGS).isNotEqualTo(ALLOW_BAL); + assertThat(BSP_ALLOW_A).isNotEqualTo(BSP_ALLOW_B); + } } diff --git a/services/core/java/com/android/server/am/PendingIntentRecord.java b/services/core/java/com/android/server/am/PendingIntentRecord.java index 202d407651ad..d8adfbe79f23 100644 --- a/services/core/java/com/android/server/am/PendingIntentRecord.java +++ b/services/core/java/com/android/server/am/PendingIntentRecord.java @@ -17,6 +17,8 @@ package com.android.server.am; import static android.app.ActivityManager.START_SUCCESS; +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 com.android.server.am.ActivityManagerDebugConfig.TAG_AM; import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; @@ -52,6 +54,7 @@ import android.util.ArraySet; import android.util.Slog; import android.util.TimeUtils; +import com.android.internal.annotations.VisibleForTesting; import com.android.internal.os.IResultReceiver; import com.android.internal.util.function.pooled.PooledLambda; import com.android.server.wm.SafeActivityOptions; @@ -270,6 +273,10 @@ public final class PendingIntentRecord extends IIntentSender.Stub { this.stringName = null; } + @VisibleForTesting TempAllowListDuration getAllowlistDurationLocked(IBinder allowlistToken) { + return mAllowlistDuration.get(allowlistToken); + } + void setAllowBgActivityStarts(IBinder token, int flags) { if (token == null) return; if ((flags & FLAG_ACTIVITY_SENDER) != 0) { @@ -288,6 +295,13 @@ public final class PendingIntentRecord extends IIntentSender.Stub { mAllowBgActivityStartsForActivitySender.remove(token); mAllowBgActivityStartsForBroadcastSender.remove(token); mAllowBgActivityStartsForServiceSender.remove(token); + if (mAllowlistDuration != null) { + 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) { @@ -634,7 +648,7 @@ public final class PendingIntentRecord extends IIntentSender.Stub { return res; } - private BackgroundStartPrivileges getBackgroundStartPrivilegesForActivitySender( + @VisibleForTesting BackgroundStartPrivileges getBackgroundStartPrivilegesForActivitySender( IBinder allowlistToken) { return mAllowBgActivityStartsForActivitySender.contains(allowlistToken) ? BackgroundStartPrivileges.allowBackgroundActivityStarts(allowlistToken) diff --git a/services/tests/mockingservicestests/src/com/android/server/am/PendingIntentControllerTest.java b/services/tests/mockingservicestests/src/com/android/server/am/PendingIntentControllerTest.java index 783971a1afe4..8b41b031fe43 100644 --- a/services/tests/mockingservicestests/src/com/android/server/am/PendingIntentControllerTest.java +++ b/services/tests/mockingservicestests/src/com/android/server/am/PendingIntentControllerTest.java @@ -16,11 +16,17 @@ 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 com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; +import static com.android.server.am.PendingIntentRecord.FLAG_ACTIVITY_SENDER; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.eq; @@ -30,9 +36,11 @@ import static org.mockito.Mockito.when; import android.app.ActivityManager; import android.app.ActivityManagerInternal; import android.app.AppGlobals; +import android.app.BackgroundStartPrivileges; import android.app.PendingIntent; import android.content.Intent; import android.content.pm.IPackageManager; +import android.os.Binder; import android.os.Looper; import androidx.test.runner.AndroidJUnit4; @@ -126,6 +134,39 @@ public class PendingIntentControllerTest { piCaptor.getValue().getTarget()); } + @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(TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED, + allowlistDurationLocked.type); + } + @After public void tearDown() { if (mMockingSession != null) { |