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

Commit 9534c6e5 authored by Yi-an Chen's avatar Yi-an Chen Committed by Android (Google) Code Review
Browse files

Merge "[Role Logic Move] Make setNotificationListenerAccessGranted user-aware" into main

parents 80a037fd c8f33a95
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
@@ -5734,13 +5734,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;
            }
@@ -5766,9 +5771,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