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

Commit c8f33a95 authored by Jay Thomas Sullivan's avatar Jay Thomas Sullivan Committed by Jay Sullivan
Browse files

[Role Logic Move] Make setNotificationListenerAccessGranted user-aware

This operation currently assumes that the calling user is the user for
which the operation should target.

Instead, pull the userId from NotificationManager's context, and pass
this to NotificationService.

Mark NotificationManager method as @UserHandleAware, which informs the
caller that the operation will instead target the user passed into the
context used to initialize NotificationManager.

This change is needed to support the Role Business Logic Move project, because the caller of this method will be moving from a per-user app into SystemServer.

Bug: 302563478
Test: atest CtsRoleTestCases NotificationListenerCheckTest NotificationManagerTest
Change-Id: Ic590a9ab40a87be4817cd32c13d7248fc15a154b
parent f33a3a29
Loading
Loading
Loading
Loading
+26 −3
Original line number Diff line number Diff line
@@ -25,8 +25,12 @@ import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.annotation.TestApi;
import android.annotation.UserHandleAware;
import android.annotation.WorkerThread;
import android.app.Notification.Builder;
import android.app.compat.CompatChanges;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledSince;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.ComponentName;
import android.content.Context;
@@ -1658,6 +1662,19 @@ public class NotificationManager {
        setNotificationListenerAccessGranted(listener, granted, true);
    }

    /**
     * For apps targeting {@link Build.VERSION_CODES#VANILLA_ICE_CREAM} and above, the
     * {@code setNotificationListenerAccessGranted} method will use the user contained within the
     * context.
     * For apps targeting an SDK version <em>below</em> this, the user of the calling process will
     * be used (Process.myUserHandle()).
     *
     * @hide
     */
    @ChangeId
    @EnabledSince(targetSdkVersion = Build.VERSION_CODES.VANILLA_ICE_CREAM)
    public static final long SET_LISTENER_ACCESS_GRANTED_IS_USER_AWARE = 302563478L;

    /**
     * Grants/revokes Notification Listener access to the given component for current user.
     * To grant access for a particular user, obtain this service by using the {@link Context}
@@ -1670,12 +1687,18 @@ public class NotificationManager {
     */
    @SystemApi
    @TestApi
    @UserHandleAware(enabledSinceTargetSdkVersion = Build.VERSION_CODES.VANILLA_ICE_CREAM)
    @RequiresPermission(android.Manifest.permission.MANAGE_NOTIFICATION_LISTENERS)
    public void setNotificationListenerAccessGranted(
            @NonNull ComponentName listener, boolean granted, boolean userSet) {
        INotificationManager service = getService();
        try {
            if (CompatChanges.isChangeEnabled(SET_LISTENER_ACCESS_GRANTED_IS_USER_AWARE)) {
                service.setNotificationListenerAccessGrantedForUser(listener, mContext.getUserId(),
                        granted, userSet);
            } else {
                service.setNotificationListenerAccessGranted(listener, granted, userSet);
            }
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
+8 −4
Original line number Diff line number Diff line
@@ -5730,13 +5730,18 @@ public class NotificationManagerService extends SystemService {
        public void setNotificationListenerAccessGrantedForUser(ComponentName listener, int userId,
                boolean granted, boolean userSet) {
            Objects.requireNonNull(listener);
            if (UserHandle.getCallingUserId() != userId) {
                getContext().enforceCallingOrSelfPermission(
                        android.Manifest.permission.INTERACT_ACROSS_USERS,
                        "setNotificationListenerAccessGrantedForUser for user " + userId);
            }
            checkNotificationListenerAccess();
            if (granted && listener.flattenToString().length()
                    > NotificationManager.MAX_SERVICE_COMPONENT_NAME_LENGTH) {
                throw new IllegalArgumentException(
                        "Component name too long: " + listener.flattenToString());
            }
            if (!userSet && isNotificationListenerAccessUserSet(listener)) {
            if (!userSet && isNotificationListenerAccessUserSet(listener, userId)) {
                // Don't override user's choice
                return;
            }
@@ -5762,9 +5767,8 @@ public class NotificationManagerService extends SystemService {
            }
        }
        private boolean isNotificationListenerAccessUserSet(ComponentName listener) {
            return mListeners.isPackageOrComponentUserSet(listener.flattenToString(),
                    getCallingUserHandle().getIdentifier());
        private boolean isNotificationListenerAccessUserSet(ComponentName listener, int userId) {
            return mListeners.isPackageOrComponentUserSet(listener.flattenToString(), userId);
        }
        @Override