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

Commit 85f51f25 authored by Achim Thesmann's avatar Achim Thesmann Committed by Android (Google) Code Review
Browse files

Merge "Revert^2 "Clear the BAL allowlist duration"" into main

parents 881fee41 949e7bb7
Loading
Loading
Loading
Loading
+7 −0
Original line number Original line Diff line number Diff line
@@ -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"
}
+15 −1
Original line number Original line Diff line number Diff line
@@ -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;
@@ -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) {
@@ -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) {
@@ -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)
+43 −0
Original line number Original line Diff line number Diff line
@@ -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;
@@ -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;
@@ -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;


@@ -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);