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

Commit 3fca7acc authored by Matías Hernández's avatar Matías Hernández Committed by Android (Google) Code Review
Browse files

Merge "Restrict TYPE_BEDTIME to rules added or updated by the Wellbeing package" into main

parents 88ab4b9f 4b522c37
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -5433,14 +5433,29 @@ public class NotificationManagerService extends SystemService {
            Objects.requireNonNull(rule.getConditionId(), "ConditionId is null");
            if (android.app.Flags.modesApi()) {
                if (rule.getType() == AutomaticZenRule.TYPE_MANAGED) {
                if (isCallerSystemOrSystemUi()) {
                    return; // System callers can use any type.
                }
                int uid = Binder.getCallingUid();
                int userId = UserHandle.getUserId(uid);
                if (rule.getType() == AutomaticZenRule.TYPE_MANAGED) {
                    boolean isDeviceOwner = Binder.withCleanCallingIdentity(
                            () -> mDpm.isActiveDeviceOwner(uid));
                    if (!isDeviceOwner) {
                        throw new IllegalArgumentException(
                                "Only Device Owners can use AutomaticZenRules with TYPE_MANAGED");
                    }
                } else if (rule.getType() == AutomaticZenRule.TYPE_BEDTIME) {
                    String wellbeingPackage = getContext().getResources().getString(
                            com.android.internal.R.string.config_systemWellbeing);
                    boolean isCallerWellbeing = !TextUtils.isEmpty(wellbeingPackage)
                            && mPackageManagerInternal.isSameApp(wellbeingPackage, uid, userId);
                    if (!isCallerWellbeing) {
                        throw new IllegalArgumentException(
                                "Only the 'Wellbeing' package can use AutomaticZenRules with "
                                        + "TYPE_BEDTIME");
                    }
                }
            }
        }
+76 −6
Original line number Diff line number Diff line
@@ -577,6 +577,9 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
        when(mPackageManagerClient.getPackageUidAsUser(any(), anyInt())).thenReturn(mUid);
        when(mPackageManagerInternal.isSameApp(anyString(), anyInt(), anyInt())).thenAnswer(
                (Answer<Boolean>) invocation -> {
                    // TODO: b/317957802 - This is overly broad and basically makes ANY 
                    //  isSameApp() check pass,  requiring Mockito.reset() for meaningful
                    //  tests! Make it more precise.
                    Object[] args = invocation.getArguments();
                    return (int) args[1] == mUid;
                });
@@ -9139,31 +9142,98 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
        AutomaticZenRule rule = new AutomaticZenRule.Builder("rule", Uri.parse("uri"))
                .setType(AutomaticZenRule.TYPE_MANAGED)
                .setOwner(new ComponentName("pkg", "cls"))
                .setOwner(new ComponentName(PKG, "cls"))
                .build();
        when(mDevicePolicyManager.isActiveDeviceOwner(anyInt())).thenReturn(true);
        mBinderService.addAutomaticZenRule(rule, "pkg", /* fromUser= */ false);
        mBinderService.addAutomaticZenRule(rule, PKG, /* fromUser= */ false);
        verify(zenModeHelper).addAutomaticZenRule(eq(PKG), eq(rule), anyInt(), any(), anyInt());
    }
        verify(zenModeHelper).addAutomaticZenRule(eq("pkg"), eq(rule), anyInt(), any(), anyInt());
    @Test
    @EnableFlags(android.app.Flags.FLAG_MODES_API)
    public void testAddAutomaticZenRule_typeManagedCanBeUsedBySystem() throws Exception {
        addAutomaticZenRule_restrictedRuleTypeCanBeUsedBySystem(AutomaticZenRule.TYPE_MANAGED);
    }
    @Test
    @EnableFlags(android.app.Flags.FLAG_MODES_API)
    public void testAddAutomaticZenRule_typeManagedCannotBeUsedByRegularApps() throws Exception {
        addAutomaticZenRule_restrictedRuleTypeCannotBeUsedByRegularApps(
                AutomaticZenRule.TYPE_MANAGED);
    }
    @Test
    @EnableFlags(android.app.Flags.FLAG_MODES_API)
    public void testAddAutomaticZenRule_typeBedtimeCanBeUsedByWellbeing() throws Exception {
        ZenModeHelper zenModeHelper = setUpMockZenTest();
        mService.setCallerIsNormalPackage();
        reset(mPackageManagerInternal);
        when(mPackageManagerInternal.isSameApp(eq(PKG), eq(mUid), anyInt())).thenReturn(true);
        when(mResources
                .getString(com.android.internal.R.string.config_systemWellbeing))
                .thenReturn(PKG);
        when(mContext.getResources()).thenReturn(mResources);
        AutomaticZenRule rule = new AutomaticZenRule.Builder("rule", Uri.parse("uri"))
                .setType(AutomaticZenRule.TYPE_BEDTIME)
                .setOwner(new ComponentName(PKG, "cls"))
                .build();
        mBinderService.addAutomaticZenRule(rule, PKG, /* fromUser= */ false);
        verify(zenModeHelper).addAutomaticZenRule(eq(PKG), eq(rule), anyInt(), any(), anyInt());
    }
    @Test
    @EnableFlags(android.app.Flags.FLAG_MODES_API)
    public void testAddAutomaticZenRule_typeBedtimeCanBeUsedBySystem() throws Exception {
        reset(mPackageManagerInternal);
        when(mPackageManagerInternal.isSameApp(eq(PKG), eq(mUid), anyInt())).thenReturn(true);
        addAutomaticZenRule_restrictedRuleTypeCanBeUsedBySystem(AutomaticZenRule.TYPE_BEDTIME);
    }
    @Test
    @EnableFlags(android.app.Flags.FLAG_MODES_API)
    public void testAddAutomaticZenRule_typeBedtimeCannotBeUsedByRegularApps() throws Exception {
        reset(mPackageManagerInternal);
        when(mPackageManagerInternal.isSameApp(eq(PKG), eq(mUid), anyInt())).thenReturn(true);
        addAutomaticZenRule_restrictedRuleTypeCannotBeUsedByRegularApps(
                AutomaticZenRule.TYPE_BEDTIME);
    }
    private void addAutomaticZenRule_restrictedRuleTypeCanBeUsedBySystem(
            @AutomaticZenRule.Type int ruleType) throws Exception {
        ZenModeHelper zenModeHelper = setUpMockZenTest();
        mService.isSystemUid = true;
        AutomaticZenRule rule = new AutomaticZenRule.Builder("rule", Uri.parse("uri"))
                .setType(ruleType)
                .setOwner(new ComponentName(PKG, "cls"))
                .build();
        when(mDevicePolicyManager.isActiveDeviceOwner(anyInt())).thenReturn(true);
        mBinderService.addAutomaticZenRule(rule, PKG, /* fromUser= */ false);
        verify(zenModeHelper).addAutomaticZenRule(eq(PKG), eq(rule), anyInt(), any(), anyInt());
    }
    private void addAutomaticZenRule_restrictedRuleTypeCannotBeUsedByRegularApps(
            @AutomaticZenRule.Type int ruleType) {
        mService.setCallerIsNormalPackage();
        mService.setZenHelper(mock(ZenModeHelper.class));
        when(mConditionProviders.isPackageOrComponentAllowed(anyString(), anyInt()))
                .thenReturn(true);
        AutomaticZenRule rule = new AutomaticZenRule.Builder("rule", Uri.parse("uri"))
                .setType(AutomaticZenRule.TYPE_MANAGED)
                .setOwner(new ComponentName("pkg", "cls"))
                .setType(ruleType)
                .setOwner(new ComponentName(PKG, "cls"))
                .build();
        when(mDevicePolicyManager.isActiveDeviceOwner(anyInt())).thenReturn(false);
        assertThrows(IllegalArgumentException.class,
                () -> mBinderService.addAutomaticZenRule(rule, "pkg", /* fromUser= */ false));
                () -> mBinderService.addAutomaticZenRule(rule, PKG, /* fromUser= */ false));
    }
    @Test