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

Commit c572b278 authored by Matías Hernández's avatar Matías Hernández
Browse files

Allow auto companion apps to manage global Zen state

This extends the preexisting exemption for Watch companions to cover AUTOMOTIVE_PROJECTION.

Fixes: 317188796
Test: atest NotificationManagerServiceTest
Change-Id: I45f0c78f2878f7c9eaa27c9573744520d9bcb4dd
parent f1a4441b
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -5617,7 +5617,8 @@ public class NotificationManagerService extends SystemService {
            return !isCompatChangeEnabled
                    || isCallerSystemOrSystemUi()
                    || hasCompanionDevice(callingPkg, UserHandle.getUserId(callingUid),
                            AssociationRequest.DEVICE_PROFILE_WATCH);
                            Set.of(AssociationRequest.DEVICE_PROFILE_WATCH,
                                    AssociationRequest.DEVICE_PROFILE_AUTOMOTIVE_PROJECTION));
        }
        private void enforcePolicyAccess(String pkg, String method) {
@@ -10800,7 +10801,7 @@ public class NotificationManagerService extends SystemService {
    }
    private boolean hasCompanionDevice(String pkg, @UserIdInt int userId,
            @Nullable @AssociationRequest.DeviceProfile String withDeviceProfile) {
            @Nullable Set</* @AssociationRequest.DeviceProfile */ String> withDeviceProfiles) {
        if (mCompanionManager == null) {
            mCompanionManager = getCompanionManager();
        }
@@ -10812,7 +10813,7 @@ public class NotificationManagerService extends SystemService {
        try {
            List<AssociationInfo> associations = mCompanionManager.getAssociations(pkg, userId);
            for (AssociationInfo association : associations) {
                if (withDeviceProfile == null || withDeviceProfile.equals(
                if (withDeviceProfiles == null || withDeviceProfiles.contains(
                        association.getDeviceProfile())) {
                    return true;
                }
+65 −9
Original line number Diff line number Diff line
@@ -13616,7 +13616,31 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
    @Test
    @EnableCompatChanges(NotificationManagerService.MANAGE_GLOBAL_ZEN_VIA_IMPLICIT_RULES)
    public void setNotificationPolicy_watchCompanionApp_setsGlobalPolicy() throws RemoteException {
    public void setNotificationPolicy_watchCompanionApp_setsGlobalPolicy()
            throws RemoteException {
        setNotificationPolicy_dependingOnCompanionAppDevice_maySetGlobalPolicy(
                AssociationRequest.DEVICE_PROFILE_WATCH, true);
    }
    @Test
    @EnableCompatChanges(NotificationManagerService.MANAGE_GLOBAL_ZEN_VIA_IMPLICIT_RULES)
    public void setNotificationPolicy_autoCompanionApp_setsGlobalPolicy()
            throws RemoteException {
        setNotificationPolicy_dependingOnCompanionAppDevice_maySetGlobalPolicy(
                AssociationRequest.DEVICE_PROFILE_AUTOMOTIVE_PROJECTION, true);
    }
    @Test
    @EnableCompatChanges(NotificationManagerService.MANAGE_GLOBAL_ZEN_VIA_IMPLICIT_RULES)
    public void setNotificationPolicy_otherCompanionApp_doesNotSetGlobalPolicy()
            throws RemoteException {
        setNotificationPolicy_dependingOnCompanionAppDevice_maySetGlobalPolicy(
                AssociationRequest.DEVICE_PROFILE_NEARBY_DEVICE_STREAMING, false);
    }
    private void setNotificationPolicy_dependingOnCompanionAppDevice_maySetGlobalPolicy(
            @AssociationRequest.DeviceProfile String deviceProfile, boolean canSetGlobalPolicy)
            throws RemoteException {
        mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);
        mService.setCallerIsNormalPackage();
        ZenModeHelper zenModeHelper = mock(ZenModeHelper.class);
@@ -13626,14 +13650,19 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
        when(mCompanionMgr.getAssociations(anyString(), anyInt()))
                .thenReturn(ImmutableList.of(
                        new AssociationInfo.Builder(1, mUserId, "package")
                                .setDisplayName("My watch")
                                .setDeviceProfile(AssociationRequest.DEVICE_PROFILE_WATCH)
                                .setDisplayName("My connected device")
                                .setDeviceProfile(deviceProfile)
                                .build()));
        NotificationManager.Policy policy = new NotificationManager.Policy(0, 0, 0);
        mBinderService.setNotificationPolicy("package", policy, false);
        if (canSetGlobalPolicy) {
            verify(zenModeHelper).setNotificationPolicy(eq(policy), anyInt(), anyInt());
        } else {
            verify(zenModeHelper).applyGlobalPolicyAsImplicitZenRule(anyString(), anyInt(),
                    eq(policy), anyInt());
        }
    }
    @Test
@@ -13703,7 +13732,29 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
    @Test
    @EnableCompatChanges(NotificationManagerService.MANAGE_GLOBAL_ZEN_VIA_IMPLICIT_RULES)
    public void setInterruptionFilter_watchCompanionApp_setsGlobalPolicy() throws RemoteException {
    public void setInterruptionFilter_watchCompanionApp_setsGlobalZen() throws RemoteException {
        setInterruptionFilter_dependingOnCompanionAppDevice_maySetGlobalZen(
                AssociationRequest.DEVICE_PROFILE_WATCH, true);
    }
    @Test
    @EnableCompatChanges(NotificationManagerService.MANAGE_GLOBAL_ZEN_VIA_IMPLICIT_RULES)
    public void setInterruptionFilter_autoCompanionApp_setsGlobalZen() throws RemoteException {
        setInterruptionFilter_dependingOnCompanionAppDevice_maySetGlobalZen(
                AssociationRequest.DEVICE_PROFILE_AUTOMOTIVE_PROJECTION, true);
    }
    @Test
    @EnableCompatChanges(NotificationManagerService.MANAGE_GLOBAL_ZEN_VIA_IMPLICIT_RULES)
    public void setInterruptionFilter_otherCompanionApp_doesNotSetGlobalZen()
            throws RemoteException {
        setInterruptionFilter_dependingOnCompanionAppDevice_maySetGlobalZen(
                AssociationRequest.DEVICE_PROFILE_NEARBY_DEVICE_STREAMING, false);
    }
    private void setInterruptionFilter_dependingOnCompanionAppDevice_maySetGlobalZen(
            @AssociationRequest.DeviceProfile String deviceProfile, boolean canSetGlobalPolicy)
            throws RemoteException {
        mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);
        ZenModeHelper zenModeHelper = mock(ZenModeHelper.class);
        mService.mZenModeHelper = zenModeHelper;
@@ -13713,14 +13764,19 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
        when(mCompanionMgr.getAssociations(anyString(), anyInt()))
                .thenReturn(ImmutableList.of(
                        new AssociationInfo.Builder(1, mUserId, "package")
                                .setDisplayName("My watch")
                                .setDeviceProfile(AssociationRequest.DEVICE_PROFILE_WATCH)
                                .setDisplayName("My connected device")
                                .setDeviceProfile(deviceProfile)
                                .build()));
        mBinderService.setInterruptionFilter("package", INTERRUPTION_FILTER_PRIORITY, false);
        if (canSetGlobalPolicy) {
            verify(zenModeHelper).setManualZenMode(eq(ZEN_MODE_IMPORTANT_INTERRUPTIONS), eq(null),
                    eq(ZenModeConfig.UPDATE_ORIGIN_APP), anyString(), eq("package"), anyInt());
        } else {
            verify(zenModeHelper).applyGlobalZenModeAsImplicitZenRule(anyString(), anyInt(),
                    eq(ZEN_MODE_IMPORTANT_INTERRUPTIONS));
        }
    }
    @Test