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

Commit 8617e4ec authored by Julia Reynolds's avatar Julia Reynolds
Browse files

Bump the importance of foreground service notis

Unless the user has chosen an importance.

Change-Id: Ibe82c91c6a98a75fc623784300f3f906879e8a80
Fixes: 65640487
Test: runtest systemui-notification
parent 3f1ba67f
Loading
Loading
Loading
Loading
+25 −2
Original line number Original line Diff line number Diff line
@@ -16,8 +16,10 @@


package com.android.server.notification;
package com.android.server.notification;


import static android.app.NotificationManager.IMPORTANCE_LOW;
import static android.app.NotificationManager.IMPORTANCE_MIN;
import static android.app.NotificationManager.IMPORTANCE_MIN;
import static android.app.NotificationManager.IMPORTANCE_NONE;
import static android.app.NotificationManager.IMPORTANCE_NONE;
import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
import static android.content.pm.PackageManager.FEATURE_LEANBACK;
import static android.content.pm.PackageManager.FEATURE_LEANBACK;
import static android.content.pm.PackageManager.FEATURE_TELEVISION;
import static android.content.pm.PackageManager.FEATURE_TELEVISION;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
@@ -1142,6 +1144,12 @@ public class NotificationManagerService extends SystemService {
        mEnqueuedNotifications.add(r);
        mEnqueuedNotifications.add(r);
    }
    }


    @VisibleForTesting
    NotificationRecord getNotificationRecord(String key) {
        return mNotificationsByKey.get(key);
    }


    @VisibleForTesting
    @VisibleForTesting
    void setSystemReady(boolean systemReady) {
    void setSystemReady(boolean systemReady) {
        mSystemReady = systemReady;
        mSystemReady = systemReady;
@@ -1217,7 +1225,7 @@ public class NotificationManagerService extends SystemService {
        mUsageStats = usageStats;
        mUsageStats = usageStats;
        mRankingHandler = new RankingHandlerWorker(mRankingThread.getLooper());
        mRankingHandler = new RankingHandlerWorker(mRankingThread.getLooper());
        mRankingHelper = new RankingHelper(getContext(),
        mRankingHelper = new RankingHelper(getContext(),
                getContext().getPackageManager(),
                mPackageManagerClient,
                mRankingHandler,
                mRankingHandler,
                mUsageStats,
                mUsageStats,
                extractorNames);
                extractorNames);
@@ -1477,7 +1485,7 @@ public class NotificationManagerService extends SystemService {
                }
                }
            }
            }
        }
        }
        mRankingHelper.updateNotificationChannel(pkg, uid, channel);
        mRankingHelper.updateNotificationChannel(pkg, uid, channel, true);


        if (!fromListener) {
        if (!fromListener) {
            final NotificationChannel modifiedChannel =
            final NotificationChannel modifiedChannel =
@@ -3485,6 +3493,21 @@ public class NotificationManagerService extends SystemService {
                user, null, System.currentTimeMillis());
                user, null, System.currentTimeMillis());
        final NotificationRecord r = new NotificationRecord(getContext(), n, channel);
        final NotificationRecord r = new NotificationRecord(getContext(), n, channel);


        if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0
                && (channel.getUserLockedFields() & NotificationChannel.USER_LOCKED_IMPORTANCE) == 0
                && (r.getImportance() == IMPORTANCE_MIN || r.getImportance() == IMPORTANCE_NONE)) {
            // Increase the importance of foreground service notifications unless the user had an
            // opinion otherwise
            if (TextUtils.isEmpty(channelId)
                    || NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) {
                r.setImportance(IMPORTANCE_LOW, "Bumped for foreground service");
            } else {
                channel.setImportance(IMPORTANCE_LOW);
                mRankingHelper.updateNotificationChannel(pkg, notificationUid, channel, false);
                r.updateNotificationChannel(channel);
            }
        }

        if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r,
        if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r,
                r.sbn.getOverrideGroupKey() != null)) {
                r.sbn.getOverrideGroupKey() != null)) {
            return;
            return;
+1 −1
Original line number Original line Diff line number Diff line
@@ -38,7 +38,7 @@ public interface RankingConfig {
            int uid, boolean includeDeleted);
            int uid, boolean includeDeleted);
    void createNotificationChannel(String pkg, int uid, NotificationChannel channel,
    void createNotificationChannel(String pkg, int uid, NotificationChannel channel,
            boolean fromTargetApp);
            boolean fromTargetApp);
    void updateNotificationChannel(String pkg, int uid, NotificationChannel channel);
    void updateNotificationChannel(String pkg, int uid, NotificationChannel channel, boolean fromUser);
    NotificationChannel getNotificationChannel(String pkg, int uid, String channelId, boolean includeDeleted);
    NotificationChannel getNotificationChannel(String pkg, int uid, String channelId, boolean includeDeleted);
    void deleteNotificationChannel(String pkg, int uid, String channelId);
    void deleteNotificationChannel(String pkg, int uid, String channelId);
    void permanentlyDeleteNotificationChannel(String pkg, int uid, String channelId);
    void permanentlyDeleteNotificationChannel(String pkg, int uid, String channelId);
+7 −4
Original line number Original line Diff line number Diff line
@@ -589,7 +589,8 @@ public class RankingHelper implements RankingConfig {
    }
    }


    @Override
    @Override
    public void updateNotificationChannel(String pkg, int uid, NotificationChannel updatedChannel) {
    public void updateNotificationChannel(String pkg, int uid, NotificationChannel updatedChannel,
            boolean fromUser) {
        Preconditions.checkNotNull(updatedChannel);
        Preconditions.checkNotNull(updatedChannel);
        Preconditions.checkNotNull(updatedChannel.getId());
        Preconditions.checkNotNull(updatedChannel.getId());
        Record r = getOrCreateRecord(pkg, uid);
        Record r = getOrCreateRecord(pkg, uid);
@@ -603,7 +604,11 @@ public class RankingHelper implements RankingConfig {
        if (updatedChannel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) {
        if (updatedChannel.getLockscreenVisibility() == Notification.VISIBILITY_PUBLIC) {
            updatedChannel.setLockscreenVisibility(Ranking.VISIBILITY_NO_OVERRIDE);
            updatedChannel.setLockscreenVisibility(Ranking.VISIBILITY_NO_OVERRIDE);
        }
        }
        updatedChannel.unlockFields(updatedChannel.getUserLockedFields());
        updatedChannel.lockFields(channel.getUserLockedFields());
        if (fromUser) {
            lockFieldsForUpdate(channel, updatedChannel);
            lockFieldsForUpdate(channel, updatedChannel);
        }
        r.channels.put(updatedChannel.getId(), updatedChannel);
        r.channels.put(updatedChannel.getId(), updatedChannel);


        if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(updatedChannel.getId())) {
        if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(updatedChannel.getId())) {
@@ -828,8 +833,6 @@ public class RankingHelper implements RankingConfig {


    @VisibleForTesting
    @VisibleForTesting
    void lockFieldsForUpdate(NotificationChannel original, NotificationChannel update) {
    void lockFieldsForUpdate(NotificationChannel original, NotificationChannel update) {
        update.unlockFields(update.getUserLockedFields());
        update.lockFields(original.getUserLockedFields());
        if (original.canBypassDnd() != update.canBypassDnd()) {
        if (original.canBypassDnd() != update.canBypassDnd()) {
            update.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
            update.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
        }
        }
+148 −16
Original line number Original line Diff line number Diff line
@@ -16,12 +16,15 @@


package com.android.server.notification;
package com.android.server.notification;


import static android.app.NotificationManager.IMPORTANCE_HIGH;
import static android.app.NotificationManager.IMPORTANCE_LOW;
import static android.app.NotificationManager.IMPORTANCE_LOW;
import static android.app.NotificationManager.IMPORTANCE_NONE;
import static android.app.NotificationManager.IMPORTANCE_NONE;
import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
import static android.content.pm.PackageManager.PERMISSION_DENIED;
import static android.content.pm.PackageManager.PERMISSION_DENIED;


import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import static junit.framework.Assert.fail;


@@ -56,6 +59,7 @@ import android.content.pm.ParceledListSlice;
import android.graphics.Color;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.AudioManager;
import android.os.Binder;
import android.os.Binder;
import android.os.Build;
import android.os.Process;
import android.os.Process;
import android.os.UserHandle;
import android.os.UserHandle;
import android.provider.Settings.Secure;
import android.provider.Settings.Secure;
@@ -241,6 +245,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
                nb.build(), new UserHandle(mUid), null, 0);
                nb.build(), new UserHandle(mUid), null, 0);
        return new NotificationRecord(mContext, sbn, channel);
        return new NotificationRecord(mContext, sbn, channel);
    }
    }

    private NotificationRecord generateNotificationRecord(NotificationChannel channel) {
    private NotificationRecord generateNotificationRecord(NotificationChannel channel) {
        return generateNotificationRecord(channel, null);
        return generateNotificationRecord(channel, null);
    }
    }
@@ -342,7 +347,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {


        // Recreating the channel doesn't throw, but ignores importance.
        // Recreating the channel doesn't throw, but ignores importance.
        final NotificationChannel dupeChannel =
        final NotificationChannel dupeChannel =
                new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH);
                new NotificationChannel("id", "name", IMPORTANCE_HIGH);
        mBinderService.createNotificationChannels(PKG,
        mBinderService.createNotificationChannels(PKG,
                new ParceledListSlice(Arrays.asList(dupeChannel)));
                new ParceledListSlice(Arrays.asList(dupeChannel)));
        final NotificationChannel createdChannel =
        final NotificationChannel createdChannel =
@@ -378,7 +383,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {


        // The user modifies importance directly, can no longer be changed by the app.
        // The user modifies importance directly, can no longer be changed by the app.
        final NotificationChannel updatedChannel =
        final NotificationChannel updatedChannel =
                new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH);
                new NotificationChannel("id", "name", IMPORTANCE_HIGH);
        mBinderService.updateNotificationChannelForPackage(PKG, mUid, updatedChannel);
        mBinderService.updateNotificationChannelForPackage(PKG, mUid, updatedChannel);


        // Recreating with a lower importance leaves channel unchanged.
        // Recreating with a lower importance leaves channel unchanged.
@@ -388,7 +393,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
                new ParceledListSlice(Arrays.asList(dupeChannel)));
                new ParceledListSlice(Arrays.asList(dupeChannel)));
        final NotificationChannel createdChannel =
        final NotificationChannel createdChannel =
                mBinderService.getNotificationChannel(PKG, "id");
                mBinderService.getNotificationChannel(PKG, "id");
        assertEquals(NotificationManager.IMPORTANCE_HIGH, createdChannel.getImportance());
        assertEquals(IMPORTANCE_HIGH, createdChannel.getImportance());
    }
    }


    @Test
    @Test
@@ -397,7 +402,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
        final NotificationChannel channel1 =
        final NotificationChannel channel1 =
                new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
                new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
        final NotificationChannel channel2 =
        final NotificationChannel channel2 =
                new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH);
                new NotificationChannel("id", "name", IMPORTANCE_HIGH);
        mBinderService.createNotificationChannels(PKG,
        mBinderService.createNotificationChannels(PKG,
                new ParceledListSlice(Arrays.asList(channel1, channel2)));
                new ParceledListSlice(Arrays.asList(channel1, channel2)));
        final NotificationChannel createdChannel =
        final NotificationChannel createdChannel =
@@ -410,7 +415,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
        when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(true);
        when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(true);


        NotificationChannel channel = new NotificationChannel("id", "name",
        NotificationChannel channel = new NotificationChannel("id", "name",
                NotificationManager.IMPORTANCE_HIGH);
                IMPORTANCE_HIGH);
        NotificationRecord r = generateNotificationRecord(channel);
        NotificationRecord r = generateNotificationRecord(channel);
        assertTrue(mNotificationManagerService.isBlocked(r, mUsageStats));
        assertTrue(mNotificationManagerService.isBlocked(r, mUsageStats));
        verify(mUsageStats, times(1)).registerSuspendedByAdmin(eq(r));
        verify(mUsageStats, times(1)).registerSuspendedByAdmin(eq(r));
@@ -421,11 +426,68 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
        when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);
        when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);


        NotificationChannel channel = new NotificationChannel("id", "name",
        NotificationChannel channel = new NotificationChannel("id", "name",
                NotificationManager.IMPORTANCE_HIGH);
                NotificationManager.IMPORTANCE_NONE);
        channel.setImportance(IMPORTANCE_NONE);
        NotificationRecord r = generateNotificationRecord(channel);
        NotificationRecord r = generateNotificationRecord(channel);
        assertTrue(mNotificationManagerService.isBlocked(r, mUsageStats));
        assertTrue(mNotificationManagerService.isBlocked(r, mUsageStats));
        verify(mUsageStats, times(1)).registerBlocked(eq(r));
        verify(mUsageStats, times(1)).registerBlocked(eq(r));

        mBinderService.createNotificationChannels(
                PKG, new ParceledListSlice(Arrays.asList(channel)));
        final StatusBarNotification sbn = generateNotificationRecord(channel).sbn;
        mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
                sbn.getId(), sbn.getNotification(), sbn.getUserId());
        waitForIdle();
        assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
    }

    @Test
    public void testEnqueuedBlockedNotifications_appBlockedChannelForegroundService()
            throws Exception {
        when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);

        NotificationChannel channel = new NotificationChannel("blocked", "name",
                NotificationManager.IMPORTANCE_NONE);
        mBinderService.createNotificationChannels(
                PKG, new ParceledListSlice(Arrays.asList(channel)));

        final StatusBarNotification sbn = generateNotificationRecord(channel).sbn;
        sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
        mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
                sbn.getId(), sbn.getNotification(), sbn.getUserId());
        waitForIdle();
        assertEquals(1, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
        assertEquals(IMPORTANCE_LOW,
                mNotificationManagerService.getNotificationRecord(sbn.getKey()).getImportance());
        assertEquals(IMPORTANCE_LOW,
                mBinderService.getNotificationChannel(PKG, channel.getId()).getImportance());
    }

    @Test
    public void testEnqueuedBlockedNotifications_userBlockedChannelForegroundService()
            throws Exception {
        when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);

        NotificationChannel channel =
                new NotificationChannel("blockedbyuser", "name", IMPORTANCE_HIGH);
        mBinderService.createNotificationChannels(
                PKG, new ParceledListSlice(Arrays.asList(channel)));

        NotificationChannel update =
                new NotificationChannel("blockedbyuser", "name", IMPORTANCE_NONE);
        mBinderService.updateNotificationChannelForPackage(PKG, mUid, update);
        waitForIdle();
        assertEquals(IMPORTANCE_NONE,
                mBinderService.getNotificationChannel(PKG, channel.getId()).getImportance());

        final StatusBarNotification sbn = generateNotificationRecord(channel).sbn;
        sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
        mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
                sbn.getId(), sbn.getNotification(), sbn.getUserId());
        waitForIdle();
        assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
        assertNull(mNotificationManagerService.getNotificationRecord(sbn.getKey()));
        assertEquals(IMPORTANCE_NONE,
                mBinderService.getNotificationChannel(PKG, channel.getId()).getImportance());
    }
    }


    @Test
    @Test
@@ -441,6 +503,21 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
        assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
        assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
    }
    }


    @Test
    public void testEnqueuedBlockedNotifications_blockedAppForegroundService() throws Exception {
        when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);

        mBinderService.setNotificationsEnabledForPackage(PKG, mUid, false);

        final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
        sbn.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;
        mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
                sbn.getId(), sbn.getNotification(), sbn.getUserId());
        waitForIdle();
        assertEquals(0, mBinderService.getActiveNotifications(sbn.getPackageName()).length);
        assertNull(mNotificationManagerService.getNotificationRecord(sbn.getKey()));
    }

    @Test
    @Test
    public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception {
    public void testEnqueueNotificationWithTag_PopulatesGetActiveNotifications() throws Exception {
        mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
        mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
@@ -798,7 +875,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
        mNotificationManagerService.setRankingHelper(mRankingHelper);
        mNotificationManagerService.setRankingHelper(mRankingHelper);
        when(mRankingHelper.getNotificationChannel(
        when(mRankingHelper.getNotificationChannel(
                anyString(), anyInt(), eq("foo"), anyBoolean())).thenReturn(
                anyString(), anyInt(), eq("foo"), anyBoolean())).thenReturn(
                        new NotificationChannel("foo", "foo", NotificationManager.IMPORTANCE_HIGH));
                        new NotificationChannel("foo", "foo", IMPORTANCE_HIGH));


        Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo");
        Notification.TvExtender tv = new Notification.TvExtender().setChannelId("foo");
        mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
        mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", 0,
@@ -927,7 +1004,8 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
        mBinderService.updateNotificationChannelFromPrivilegedListener(
        mBinderService.updateNotificationChannelFromPrivilegedListener(
                null, PKG, Process.myUserHandle(), mTestNotificationChannel);
                null, PKG, Process.myUserHandle(), mTestNotificationChannel);


        verify(mRankingHelper, times(1)).updateNotificationChannel(anyString(), anyInt(), any());
        verify(mRankingHelper, times(1)).updateNotificationChannel(
                anyString(), anyInt(), any(), anyBoolean());


        verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG),
        verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG),
                eq(Process.myUserHandle()), eq(mTestNotificationChannel),
                eq(Process.myUserHandle()), eq(mTestNotificationChannel),
@@ -948,7 +1026,8 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
            // pass
            // pass
        }
        }


        verify(mRankingHelper, never()).updateNotificationChannel(anyString(), anyInt(), any());
        verify(mRankingHelper, never()).updateNotificationChannel(
                anyString(), anyInt(), any(), anyBoolean());


        verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG),
        verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG),
                eq(Process.myUserHandle()), eq(mTestNotificationChannel),
                eq(Process.myUserHandle()), eq(mTestNotificationChannel),
@@ -974,7 +1053,8 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
            // pass
            // pass
        }
        }


        verify(mRankingHelper, never()).updateNotificationChannel(anyString(), anyInt(), any());
        verify(mRankingHelper, never()).updateNotificationChannel(
                anyString(), anyInt(), any(), anyBoolean());


        verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG),
        verify(mListeners, never()).notifyNotificationChannelChanged(eq(PKG),
                eq(Process.myUserHandle()), eq(mTestNotificationChannel),
                eq(Process.myUserHandle()), eq(mTestNotificationChannel),
@@ -1345,7 +1425,8 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
    @Test
    @Test
    public void testOnlyAutogroupIfGroupChanged_groupChanged_autogroups()
    public void testOnlyAutogroupIfGroupChanged_groupChanged_autogroups()
            throws Exception {
            throws Exception {
        NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, 0, "group", false);
        NotificationRecord r =
                generateNotificationRecord(mTestNotificationChannel, 0, "group", false);
        mNotificationManagerService.addNotification(r);
        mNotificationManagerService.addNotification(r);


        r = generateNotificationRecord(mTestNotificationChannel, 0, null, false);
        r = generateNotificationRecord(mTestNotificationChannel, 0, null, false);
@@ -1425,12 +1506,16 @@ public class NotificationManagerServiceTest extends NotificationTestCase {


        // Same notifications are enqueued as posted, everything counts b/c id and tag don't match
        // Same notifications are enqueued as posted, everything counts b/c id and tag don't match
        int userId = new UserHandle(mUid).getIdentifier();
        int userId = new UserHandle(mUid).getIdentifier();
        assertEquals(40, mNotificationManagerService.getNotificationCountLocked(PKG, userId, 0, null));
        assertEquals(40,
        assertEquals(40, mNotificationManagerService.getNotificationCountLocked(PKG, userId, 0, "tag2"));
                mNotificationManagerService.getNotificationCountLocked(PKG, userId, 0, null));
        assertEquals(2, mNotificationManagerService.getNotificationCountLocked("a", userId, 0, "banana"));
        assertEquals(40,
                mNotificationManagerService.getNotificationCountLocked(PKG, userId, 0, "tag2"));
        assertEquals(2,
                mNotificationManagerService.getNotificationCountLocked("a", userId, 0, "banana"));


        // exclude a known notification - it's excluded from only the posted list, not enqueued
        // exclude a known notification - it's excluded from only the posted list, not enqueued
        assertEquals(39, mNotificationManagerService.getNotificationCountLocked(PKG, userId, 0, "tag"));
        assertEquals(39,
                mNotificationManagerService.getNotificationCountLocked(PKG, userId, 0, "tag"));
    }
    }


    @Test
    @Test
@@ -1560,4 +1645,51 @@ public class NotificationManagerServiceTest extends NotificationTestCase {


        verify(mZenModeHelper, times(1)).updateDefaultZenRules();
        verify(mZenModeHelper, times(1)).updateDefaultZenRules();
    }
    }

    @Test
    public void testBumpFGImportance_noChannelChangePreOApp() throws Exception {
        String preOPkg = "preO";
        int preOUid = 145;
        final ApplicationInfo legacy = new ApplicationInfo();
        legacy.targetSdkVersion = Build.VERSION_CODES.N_MR1;
        when(mPackageManagerClient.getApplicationInfoAsUser(eq(preOPkg), anyInt(), anyInt()))
                .thenReturn(legacy);
        when(mPackageManagerClient.getPackageUidAsUser(eq(preOPkg), anyInt())).thenReturn(preOUid);
        getContext().setMockPackageManager(mPackageManagerClient);

        Notification.Builder nb = new Notification.Builder(mContext,
                NotificationChannel.DEFAULT_CHANNEL_ID)
                .setContentTitle("foo")
                .setSmallIcon(android.R.drawable.sym_def_app_icon)
                .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
                .setPriority(Notification.PRIORITY_MIN);

        StatusBarNotification sbn = new StatusBarNotification(preOPkg, preOPkg, 9, "tag", preOUid,
                0, nb.build(), new UserHandle(preOUid), null, 0);

        mBinderService.enqueueNotificationWithTag(preOPkg, preOPkg, "tag",
                sbn.getId(), sbn.getNotification(), sbn.getUserId());
        waitForIdle();
        assertEquals(IMPORTANCE_LOW,
                mNotificationManagerService.getNotificationRecord(sbn.getKey()).getImportance());

        nb = new Notification.Builder(mContext)
                .setContentTitle("foo")
                .setSmallIcon(android.R.drawable.sym_def_app_icon)
                .setFlag(Notification.FLAG_FOREGROUND_SERVICE, true)
                .setPriority(Notification.PRIORITY_MIN);

        sbn = new StatusBarNotification(preOPkg, preOPkg, 9, "tag", preOUid,
                0, nb.build(), new UserHandle(preOUid), null, 0);

        mBinderService.enqueueNotificationWithTag(preOPkg, preOPkg, "tag",
                sbn.getId(), sbn.getNotification(), sbn.getUserId());
        waitForIdle();
        assertEquals(IMPORTANCE_LOW,
                mNotificationManagerService.getNotificationRecord(sbn.getKey()).getImportance());

        NotificationChannel defaultChannel = mBinderService.getNotificationChannel(
                preOPkg, NotificationChannel.DEFAULT_CHANNEL_ID);
        assertEquals(IMPORTANCE_UNSPECIFIED, defaultChannel.getImportance());
    }
}
}
+14 −14
Original line number Original line Diff line number Diff line
@@ -484,7 +484,7 @@ public class RankingHelperTest extends NotificationTestCase {
        final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG, UID,
        final NotificationChannel defaultChannel = mHelper.getNotificationChannel(PKG, UID,
                NotificationChannel.DEFAULT_CHANNEL_ID, false);
                NotificationChannel.DEFAULT_CHANNEL_ID, false);
        defaultChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
        defaultChannel.setImportance(NotificationManager.IMPORTANCE_LOW);
        mHelper.updateNotificationChannel(PKG, UID, defaultChannel);
        mHelper.updateNotificationChannel(PKG, UID, defaultChannel, true);


        ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false,
        ByteArrayOutputStream baos = writeXmlAndPurge(PKG, UID, false,
                NotificationChannel.DEFAULT_CHANNEL_ID);
                NotificationChannel.DEFAULT_CHANNEL_ID);
@@ -633,7 +633,7 @@ public class RankingHelperTest extends NotificationTestCase {
        channel2.setBypassDnd(false);
        channel2.setBypassDnd(false);
        channel2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        channel2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);


        mHelper.updateNotificationChannel(PKG, UID, channel2);
        mHelper.updateNotificationChannel(PKG, UID, channel2, true);


        // all fields should be changed
        // all fields should be changed
        assertEquals(channel2, mHelper.getNotificationChannel(PKG, UID, channel.getId(), false));
        assertEquals(channel2, mHelper.getNotificationChannel(PKG, UID, channel.getId(), false));
@@ -657,7 +657,7 @@ public class RankingHelperTest extends NotificationTestCase {
        defaultChannel.setBypassDnd(true);
        defaultChannel.setBypassDnd(true);
        defaultChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
        defaultChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);


        mHelper.updateNotificationChannel(PKG, UID, defaultChannel);
        mHelper.updateNotificationChannel(PKG, UID, defaultChannel, true);


        // ensure app level fields are changed
        // ensure app level fields are changed
        assertFalse(mHelper.canShowBadge(PKG, UID));
        assertFalse(mHelper.canShowBadge(PKG, UID));
@@ -681,7 +681,7 @@ public class RankingHelperTest extends NotificationTestCase {
        channel.setBypassDnd(true);
        channel.setBypassDnd(true);
        channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
        channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);


        mHelper.updateNotificationChannel(PKG, UID, channel);
        mHelper.updateNotificationChannel(PKG, UID, channel, true);


        // ensure app level fields are not changed
        // ensure app level fields are not changed
        assertTrue(mHelper.canShowBadge(PKG, UID));
        assertTrue(mHelper.canShowBadge(PKG, UID));
@@ -772,14 +772,14 @@ public class RankingHelperTest extends NotificationTestCase {
        update1.setSound(new Uri.Builder().scheme("test").build(),
        update1.setSound(new Uri.Builder().scheme("test").build(),
                new AudioAttributes.Builder().build());
                new AudioAttributes.Builder().build());
        update1.lockFields(NotificationChannel.USER_LOCKED_PRIORITY); // should be ignored
        update1.lockFields(NotificationChannel.USER_LOCKED_PRIORITY); // should be ignored
        mHelper.updateNotificationChannel(PKG, UID, update1);
        mHelper.updateNotificationChannel(PKG, UID, update1, true);
        assertEquals(NotificationChannel.USER_LOCKED_SOUND,
        assertEquals(NotificationChannel.USER_LOCKED_SOUND,
                mHelper.getNotificationChannel(PKG, UID, update1.getId(), false)
                mHelper.getNotificationChannel(PKG, UID, update1.getId(), false)
                        .getUserLockedFields());
                        .getUserLockedFields());


        NotificationChannel update2 = getChannel();
        NotificationChannel update2 = getChannel();
        update2.enableVibration(true);
        update2.enableVibration(true);
        mHelper.updateNotificationChannel(PKG, UID, update2);
        mHelper.updateNotificationChannel(PKG, UID, update2, true);
        assertEquals(NotificationChannel.USER_LOCKED_SOUND
        assertEquals(NotificationChannel.USER_LOCKED_SOUND
                        | NotificationChannel.USER_LOCKED_VIBRATION,
                        | NotificationChannel.USER_LOCKED_VIBRATION,
                mHelper.getNotificationChannel(PKG, UID, update2.getId(), false)
                mHelper.getNotificationChannel(PKG, UID, update2.getId(), false)
@@ -792,14 +792,14 @@ public class RankingHelperTest extends NotificationTestCase {


        final NotificationChannel update1 = getChannel();
        final NotificationChannel update1 = getChannel();
        update1.setVibrationPattern(new long[]{7945, 46 ,246});
        update1.setVibrationPattern(new long[]{7945, 46 ,246});
        mHelper.updateNotificationChannel(PKG, UID, update1);
        mHelper.updateNotificationChannel(PKG, UID, update1, true);
        assertEquals(NotificationChannel.USER_LOCKED_VIBRATION,
        assertEquals(NotificationChannel.USER_LOCKED_VIBRATION,
                mHelper.getNotificationChannel(PKG, UID, update1.getId(), false)
                mHelper.getNotificationChannel(PKG, UID, update1.getId(), false)
                        .getUserLockedFields());
                        .getUserLockedFields());


        final NotificationChannel update2 = getChannel();
        final NotificationChannel update2 = getChannel();
        update2.enableLights(true);
        update2.enableLights(true);
        mHelper.updateNotificationChannel(PKG, UID, update2);
        mHelper.updateNotificationChannel(PKG, UID, update2, true);
        assertEquals(NotificationChannel.USER_LOCKED_VIBRATION
        assertEquals(NotificationChannel.USER_LOCKED_VIBRATION
                        | NotificationChannel.USER_LOCKED_LIGHTS,
                        | NotificationChannel.USER_LOCKED_LIGHTS,
                mHelper.getNotificationChannel(PKG, UID, update2.getId(), false)
                mHelper.getNotificationChannel(PKG, UID, update2.getId(), false)
@@ -812,14 +812,14 @@ public class RankingHelperTest extends NotificationTestCase {


        final NotificationChannel update1 = getChannel();
        final NotificationChannel update1 = getChannel();
        update1.setLightColor(Color.GREEN);
        update1.setLightColor(Color.GREEN);
        mHelper.updateNotificationChannel(PKG, UID, update1);
        mHelper.updateNotificationChannel(PKG, UID, update1, true);
        assertEquals(NotificationChannel.USER_LOCKED_LIGHTS,
        assertEquals(NotificationChannel.USER_LOCKED_LIGHTS,
                mHelper.getNotificationChannel(PKG, UID, update1.getId(), false)
                mHelper.getNotificationChannel(PKG, UID, update1.getId(), false)
                        .getUserLockedFields());
                        .getUserLockedFields());


        final NotificationChannel update2 = getChannel();
        final NotificationChannel update2 = getChannel();
        update2.setImportance(IMPORTANCE_DEFAULT);
        update2.setImportance(IMPORTANCE_DEFAULT);
        mHelper.updateNotificationChannel(PKG, UID, update2);
        mHelper.updateNotificationChannel(PKG, UID, update2, true);
        assertEquals(NotificationChannel.USER_LOCKED_LIGHTS
        assertEquals(NotificationChannel.USER_LOCKED_LIGHTS
                        | NotificationChannel.USER_LOCKED_IMPORTANCE,
                        | NotificationChannel.USER_LOCKED_IMPORTANCE,
                mHelper.getNotificationChannel(PKG, UID, update2.getId(), false)
                mHelper.getNotificationChannel(PKG, UID, update2.getId(), false)
@@ -835,14 +835,14 @@ public class RankingHelperTest extends NotificationTestCase {


        final NotificationChannel update1 = getChannel();
        final NotificationChannel update1 = getChannel();
        update1.setBypassDnd(true);
        update1.setBypassDnd(true);
        mHelper.updateNotificationChannel(PKG, UID, update1);
        mHelper.updateNotificationChannel(PKG, UID, update1, true);
        assertEquals(NotificationChannel.USER_LOCKED_PRIORITY,
        assertEquals(NotificationChannel.USER_LOCKED_PRIORITY,
                mHelper.getNotificationChannel(PKG, UID, update1.getId(), false)
                mHelper.getNotificationChannel(PKG, UID, update1.getId(), false)
                        .getUserLockedFields());
                        .getUserLockedFields());


        final NotificationChannel update2 = getChannel();
        final NotificationChannel update2 = getChannel();
        update2.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
        update2.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
        mHelper.updateNotificationChannel(PKG, UID, update2);
        mHelper.updateNotificationChannel(PKG, UID, update2, true);
        assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
        assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
                        | NotificationChannel.USER_LOCKED_VISIBILITY,
                        | NotificationChannel.USER_LOCKED_VISIBILITY,
                mHelper.getNotificationChannel(PKG, UID, update2.getId(), false)
                mHelper.getNotificationChannel(PKG, UID, update2.getId(), false)
@@ -850,7 +850,7 @@ public class RankingHelperTest extends NotificationTestCase {


        final NotificationChannel update3 = getChannel();
        final NotificationChannel update3 = getChannel();
        update3.setShowBadge(false);
        update3.setShowBadge(false);
        mHelper.updateNotificationChannel(PKG, UID, update3);
        mHelper.updateNotificationChannel(PKG, UID, update3, true);
        assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
        assertEquals(NotificationChannel.USER_LOCKED_PRIORITY
                        | NotificationChannel.USER_LOCKED_VISIBILITY
                        | NotificationChannel.USER_LOCKED_VISIBILITY
                        | NotificationChannel.USER_LOCKED_SHOW_BADGE,
                        | NotificationChannel.USER_LOCKED_SHOW_BADGE,
@@ -1263,7 +1263,7 @@ public class RankingHelperTest extends NotificationTestCase {
        mHelper.getNotificationChannelGroups(PKG, UID, true).getList();
        mHelper.getNotificationChannelGroups(PKG, UID, true).getList();


        channel1.setImportance(IMPORTANCE_LOW);
        channel1.setImportance(IMPORTANCE_LOW);
        mHelper.updateNotificationChannel(PKG, UID, channel1);
        mHelper.updateNotificationChannel(PKG, UID, channel1, true);


        List<NotificationChannelGroup> actual =
        List<NotificationChannelGroup> actual =
                mHelper.getNotificationChannelGroups(PKG, UID, true).getList();
                mHelper.getNotificationChannelGroups(PKG, UID, true).getList();