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

Commit 401c536c authored by Matías Hernández's avatar Matías Hernández Committed by Automerger Merge Worker
Browse files

Merge "Disallow setting invalid importance when updating a...

Merge "Disallow setting invalid importance when updating a NotificationChannel" into udc-dev am: 8128beb1

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/21478864



Change-Id: Ia16622d2a6b7806d524a082175ec10a6db5a5cc7
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents a55b46eb 8128beb1
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import static android.app.NotificationChannel.USER_LOCKED_IMPORTANCE;
import static android.app.NotificationManager.BUBBLE_PREFERENCE_ALL;
import static android.app.NotificationManager.BUBBLE_PREFERENCE_NONE;
import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
import static android.app.NotificationManager.IMPORTANCE_MAX;
import static android.app.NotificationManager.IMPORTANCE_NONE;
import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
import static android.util.StatsLog.ANNOTATION_ID_IS_UID;
@@ -905,6 +906,9 @@ public class PreferencesHelper implements RankingConfig {
        Objects.requireNonNull(channel);
        Objects.requireNonNull(channel.getId());
        Preconditions.checkArgument(!TextUtils.isEmpty(channel.getName()));
        Preconditions.checkArgument(channel.getImportance() >= IMPORTANCE_NONE
                && channel.getImportance() <= IMPORTANCE_MAX, "Invalid importance level");

        boolean needsPolicyFileChange = false, wasUndeleted = false, needsDndChange = false;
        synchronized (mPackagePreferences) {
            PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
@@ -993,11 +997,6 @@ public class PreferencesHelper implements RankingConfig {

                needsPolicyFileChange = true;

                if (channel.getImportance() < IMPORTANCE_NONE
                        || channel.getImportance() > NotificationManager.IMPORTANCE_MAX) {
                    throw new IllegalArgumentException("Invalid importance level");
                }

                // Reset fields that apps aren't allowed to set.
                if (fromTargetApp && !hasDndAccess) {
                    channel.setBypassDnd(r.priority == Notification.PRIORITY_MAX);
+42 −0
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@ import static junit.framework.Assert.fail;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
@@ -1576,6 +1577,47 @@ public class PreferencesHelperTest extends UiServiceTestCase {
                new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX), true, false));
    }

    @Test
    public void testUpdateChannel_downgradeImportance() {
        mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
                new NotificationChannel("bananas", "bananas", IMPORTANCE_DEFAULT),
                true, false);

        assertTrue(mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
                new NotificationChannel("bananas", "bananas", IMPORTANCE_LOW), true, false));
    }

    @Test
    public void testUpdateChannel_upgradeImportance_ignored() {
        mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
                new NotificationChannel("bananas", "bananas", IMPORTANCE_DEFAULT),
                true, false);

        assertFalse(mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
                new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX), true, false));
    }

    @Test
    public void testUpdateChannel_badImportance() {
        mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
                new NotificationChannel("bananas", "bananas", IMPORTANCE_DEFAULT),
                true, false);

        assertThrows(IllegalArgumentException.class,
                () -> mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
                        new NotificationChannel("bananas", "bananas", IMPORTANCE_NONE - 1), true,
                        false));

        assertThrows(IllegalArgumentException.class,
                () -> mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
                        new NotificationChannel("bananas", "bananas", IMPORTANCE_UNSPECIFIED), true,
                        false));

        assertThrows(IllegalArgumentException.class,
                () -> mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1,
                        new NotificationChannel("bananas", "bananas", IMPORTANCE_MAX + 1), true,
                        false));
    }

    @Test
    public void testUpdate() throws Exception {