Merge "Allow apps to downgrade importance in createNotificationChannel" into oc-dr1-dev

This commit is contained in:
Geoffrey Pitsch
2017-07-20 15:33:41 +00:00
committed by Android (Google) Code Review
3 changed files with 104 additions and 50 deletions

View File

@@ -18,14 +18,12 @@ package android.app;
import android.annotation.IntDef; import android.annotation.IntDef;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SdkConstant; import android.annotation.SdkConstant;
import android.annotation.SystemService; import android.annotation.SystemService;
import android.annotation.TestApi; import android.annotation.TestApi;
import android.app.Notification.Builder; import android.app.Notification.Builder;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.pm.ParceledListSlice; import android.content.pm.ParceledListSlice;
import android.graphics.drawable.Icon; import android.graphics.drawable.Icon;
import android.net.Uri; import android.net.Uri;
@@ -33,7 +31,6 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.Looper;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.os.RemoteException; import android.os.RemoteException;
@@ -41,7 +38,6 @@ import android.os.ServiceManager;
import android.os.StrictMode; import android.os.StrictMode;
import android.os.UserHandle; import android.os.UserHandle;
import android.provider.Settings.Global; import android.provider.Settings.Global;
import android.service.notification.NotificationListenerService.Ranking;
import android.service.notification.StatusBarNotification; import android.service.notification.StatusBarNotification;
import android.service.notification.ZenModeConfig; import android.service.notification.ZenModeConfig;
import android.util.ArraySet; import android.util.ArraySet;
@@ -419,12 +415,16 @@ public class NotificationManager {
* Creates a notification channel that notifications can be posted to. * Creates a notification channel that notifications can be posted to.
* *
* This can also be used to restore a deleted channel and to update an existing channel's * This can also be used to restore a deleted channel and to update an existing channel's
* name and description. * name, description, and/or importance.
* *
* <p>The name and description should only be changed if the locale changes * <p>The name and description should only be changed if the locale changes
* or in response to the user renaming this channel. For example, if a user has a channel * or in response to the user renaming this channel. For example, if a user has a channel
* named 'John Doe' that represents messages from a 'John Doe', and 'John Doe' changes his name * named 'John Doe' that represents messages from a 'John Doe', and 'John Doe' changes his name
* to 'John Smith,' the channel can be renamed to match. * to 'John Smith,' the channel can be renamed to match.
*
* <p>The importance of an existing channel will only be changed if the new importance is lower
* than the current value and the user has not altered any settings on this channel.
*
* All other fields are ignored for channels that already exist. * All other fields are ignored for channels that already exist.
* *
* @param channel the channel to create. Note that the created channel may differ from this * @param channel the channel to create. Note that the created channel may differ from this

View File

@@ -567,6 +567,13 @@ public class RankingHelper implements RankingConfig {
existing.setDescription(channel.getDescription()); existing.setDescription(channel.getDescription());
existing.setBlockableSystem(channel.isBlockableSystem()); existing.setBlockableSystem(channel.isBlockableSystem());
// Apps are allowed to downgrade channel importance if the user has not changed any
// fields on this channel yet.
if (existing.getUserLockedFields() == 0 &&
channel.getImportance() < existing.getImportance()) {
existing.setImportance(channel.getImportance());
}
updateConfig(); updateConfig();
return; return;
} }

View File

@@ -81,7 +81,7 @@ import com.android.server.lights.LightsManager;
@RunWithLooper @RunWithLooper
public class NotificationManagerServiceTest extends NotificationTestCase { public class NotificationManagerServiceTest extends NotificationTestCase {
private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId"; private static final String TEST_CHANNEL_ID = "NotificationManagerServiceTestChannelId";
private final int uid = Binder.getCallingUid(); private final int mUid = Binder.getCallingUid();
private NotificationManagerService mNotificationManagerService; private NotificationManagerService mNotificationManagerService;
private INotificationManager mBinderService; private INotificationManager mBinderService;
private NotificationManagerInternal mInternalService; private NotificationManagerInternal mInternalService;
@@ -133,13 +133,13 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
// most tests assume badging is enabled // most tests assume badging is enabled
Secure.putIntForUser(getContext().getContentResolver(), Secure.putIntForUser(getContext().getContentResolver(),
Secure.NOTIFICATION_BADGING, 1, Secure.NOTIFICATION_BADGING, 1,
UserHandle.getUserHandleForUid(uid).getIdentifier()); UserHandle.getUserHandleForUid(mUid).getIdentifier());
mNotificationManagerService = new TestableNotificationManagerService(mContext); mNotificationManagerService = new TestableNotificationManagerService(mContext);
// MockPackageManager - default returns ApplicationInfo with matching calling UID // MockPackageManager - default returns ApplicationInfo with matching calling UID
final ApplicationInfo applicationInfo = new ApplicationInfo(); final ApplicationInfo applicationInfo = new ApplicationInfo();
applicationInfo.uid = uid; applicationInfo.uid = mUid;
when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt())) when(mPackageManager.getApplicationInfo(anyString(), anyInt(), anyInt()))
.thenReturn(applicationInfo); .thenReturn(applicationInfo);
when(mPackageManagerClient.getApplicationInfoAsUser(anyString(), anyInt(), anyInt())) when(mPackageManagerClient.getApplicationInfoAsUser(anyString(), anyInt(), anyInt()))
@@ -151,7 +151,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
mTestableLooper = TestableLooper.get(this); mTestableLooper = TestableLooper.get(this);
mListener = mNotificationListeners.new ManagedServiceInfo( mListener = mNotificationListeners.new ManagedServiceInfo(
null, new ComponentName(PKG, "test_class"), uid, true, null, 0); null, new ComponentName(PKG, "test_class"), mUid, true, null, 0);
when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener); when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener);
try { try {
mNotificationManagerService.init(mTestableLooper.getLooper(), mPackageManager, mNotificationManagerService.init(mTestableLooper.getLooper(), mPackageManager,
@@ -183,8 +183,8 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
.setGroup(groupKey) .setGroup(groupKey)
.setGroupSummary(isSummary); .setGroupSummary(isSummary);
StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, id, "tag", uid, 0, StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, id, "tag", mUid, 0,
nb.build(), new UserHandle(uid), 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) {
@@ -202,8 +202,8 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
if (extender != null) { if (extender != null) {
nb.extend(extender); nb.extend(extender);
} }
StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", uid, 0, StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", mUid, 0,
nb.build(), new UserHandle(uid), null, 0); nb.build(), new UserHandle(mUid), null, 0);
return new NotificationRecord(mContext, sbn, channel); return new NotificationRecord(mContext, sbn, channel);
} }
@@ -211,17 +211,17 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
public void testCreateNotificationChannels_SingleChannel() throws Exception { public void testCreateNotificationChannels_SingleChannel() throws Exception {
final NotificationChannel channel = final NotificationChannel channel =
new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT); new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
mBinderService.createNotificationChannels("test_pkg", mBinderService.createNotificationChannels(PKG,
new ParceledListSlice(Arrays.asList(channel))); new ParceledListSlice(Arrays.asList(channel)));
final NotificationChannel createdChannel = final NotificationChannel createdChannel =
mBinderService.getNotificationChannel("test_pkg", "id"); mBinderService.getNotificationChannel(PKG, "id");
assertTrue(createdChannel != null); assertTrue(createdChannel != null);
} }
@Test @Test
public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception { public void testCreateNotificationChannels_NullChannelThrowsException() throws Exception {
try { try {
mBinderService.createNotificationChannels("test_pkg", mBinderService.createNotificationChannels(PKG,
new ParceledListSlice(Arrays.asList(null))); new ParceledListSlice(Arrays.asList(null)));
fail("Exception should be thrown immediately."); fail("Exception should be thrown immediately.");
} catch (NullPointerException e) { } catch (NullPointerException e) {
@@ -235,10 +235,10 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
new NotificationChannel("id1", "name", NotificationManager.IMPORTANCE_DEFAULT); new NotificationChannel("id1", "name", NotificationManager.IMPORTANCE_DEFAULT);
final NotificationChannel channel2 = final NotificationChannel channel2 =
new NotificationChannel("id2", "name", NotificationManager.IMPORTANCE_DEFAULT); new NotificationChannel("id2", "name", NotificationManager.IMPORTANCE_DEFAULT);
mBinderService.createNotificationChannels("test_pkg", mBinderService.createNotificationChannels(PKG,
new ParceledListSlice(Arrays.asList(channel1, channel2))); new ParceledListSlice(Arrays.asList(channel1, channel2)));
assertTrue(mBinderService.getNotificationChannel("test_pkg", "id1") != null); assertTrue(mBinderService.getNotificationChannel(PKG, "id1") != null);
assertTrue(mBinderService.getNotificationChannel("test_pkg", "id2") != null); assertTrue(mBinderService.getNotificationChannel(PKG, "id2") != null);
} }
@Test @Test
@@ -246,19 +246,60 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
throws Exception { throws Exception {
final NotificationChannel channel = final NotificationChannel channel =
new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT); new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
mBinderService.createNotificationChannels("test_pkg", mBinderService.createNotificationChannels(PKG,
new ParceledListSlice(Arrays.asList(channel))); new ParceledListSlice(Arrays.asList(channel)));
// 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", NotificationManager.IMPORTANCE_HIGH);
mBinderService.createNotificationChannels("test_pkg", mBinderService.createNotificationChannels(PKG,
new ParceledListSlice(Arrays.asList(dupeChannel))); new ParceledListSlice(Arrays.asList(dupeChannel)));
final NotificationChannel createdChannel = final NotificationChannel createdChannel =
mBinderService.getNotificationChannel("test_pkg", "id"); mBinderService.getNotificationChannel(PKG, "id");
assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance()); assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance());
} }
@Test
public void testCreateNotificationChannels_SecondCreateAllowedToDowngradeImportance()
throws Exception {
final NotificationChannel channel =
new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
mBinderService.createNotificationChannels(PKG,
new ParceledListSlice(Arrays.asList(channel)));
// Recreating with a lower importance is allowed to modify the channel.
final NotificationChannel dupeChannel =
new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_LOW);
mBinderService.createNotificationChannels(PKG,
new ParceledListSlice(Arrays.asList(dupeChannel)));
final NotificationChannel createdChannel =
mBinderService.getNotificationChannel(PKG, "id");
assertEquals(NotificationManager.IMPORTANCE_LOW, createdChannel.getImportance());
}
@Test
public void testCreateNotificationChannels_CannotDowngradeImportanceIfAlreadyUpdated()
throws Exception {
final NotificationChannel channel =
new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT);
mBinderService.createNotificationChannels(PKG,
new ParceledListSlice(Arrays.asList(channel)));
// The user modifies importance directly, can no longer be changed by the app.
final NotificationChannel updatedChannel =
new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH);
mBinderService.updateNotificationChannelForPackage(PKG, mUid, updatedChannel);
// Recreating with a lower importance leaves channel unchanged.
final NotificationChannel dupeChannel =
new NotificationChannel("id", "name", NotificationManager.IMPORTANCE_LOW);
mBinderService.createNotificationChannels(PKG,
new ParceledListSlice(Arrays.asList(dupeChannel)));
final NotificationChannel createdChannel =
mBinderService.getNotificationChannel(PKG, "id");
assertEquals(NotificationManager.IMPORTANCE_HIGH, createdChannel.getImportance());
}
@Test @Test
public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond() public void testCreateNotificationChannels_IdenticalChannelsInListIgnoresSecond()
throws Exception { throws Exception {
@@ -266,10 +307,10 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
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", NotificationManager.IMPORTANCE_HIGH);
mBinderService.createNotificationChannels("test_pkg", mBinderService.createNotificationChannels(PKG,
new ParceledListSlice(Arrays.asList(channel1, channel2))); new ParceledListSlice(Arrays.asList(channel1, channel2)));
final NotificationChannel createdChannel = final NotificationChannel createdChannel =
mBinderService.getNotificationChannel("test_pkg", "id"); mBinderService.getNotificationChannel(PKG, "id");
assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance()); assertEquals(NotificationManager.IMPORTANCE_DEFAULT, createdChannel.getImportance());
} }
@@ -300,7 +341,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
public void testEnqueuedBlockedNotifications_blockedApp() throws Exception { public void testEnqueuedBlockedNotifications_blockedApp() throws Exception {
when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false); when(mPackageManager.isPackageSuspendedForUser(anyString(), anyInt())).thenReturn(false);
mBinderService.setNotificationsEnabledForPackage(PKG, uid, false); mBinderService.setNotificationsEnabledForPackage(PKG, mUid, false);
final StatusBarNotification sbn = generateNotificationRecord(null).sbn; final StatusBarNotification sbn = generateNotificationRecord(null).sbn;
mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag", mBinderService.enqueueNotificationWithTag(PKG, "opPkg", "tag",
@@ -381,7 +422,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
n.sbn.getId(), n.sbn.getNotification(), n.sbn.getUserId()); n.sbn.getId(), n.sbn.getNotification(), n.sbn.getUserId());
waitForIdle(); waitForIdle();
mNotificationManagerService.mNotificationDelegate.onClearAll(uid, Binder.getCallingPid(), mNotificationManagerService.mNotificationDelegate.onClearAll(mUid, Binder.getCallingPid(),
n.getUserId()); n.getUserId());
waitForIdle(); waitForIdle();
StatusBarNotification[] notifs = StatusBarNotification[] notifs =
@@ -607,7 +648,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
public void testCreateChannelNotifyListener() throws Exception { public void testCreateChannelNotifyListener() throws Exception {
List<String> associations = new ArrayList<>(); List<String> associations = new ArrayList<>();
associations.add("a"); associations.add("a");
when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
mNotificationManagerService.setRankingHelper(mRankingHelper); mNotificationManagerService.setRankingHelper(mRankingHelper);
when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
eq(mTestNotificationChannel.getId()), anyBoolean())) eq(mTestNotificationChannel.getId()), anyBoolean()))
@@ -632,7 +673,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
public void testCreateChannelGroupNotifyListener() throws Exception { public void testCreateChannelGroupNotifyListener() throws Exception {
List<String> associations = new ArrayList<>(); List<String> associations = new ArrayList<>();
associations.add("a"); associations.add("a");
when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
mNotificationManagerService.setRankingHelper(mRankingHelper); mNotificationManagerService.setRankingHelper(mRankingHelper);
NotificationChannelGroup group1 = new NotificationChannelGroup("a", "b"); NotificationChannelGroup group1 = new NotificationChannelGroup("a", "b");
NotificationChannelGroup group2 = new NotificationChannelGroup("n", "m"); NotificationChannelGroup group2 = new NotificationChannelGroup("n", "m");
@@ -652,7 +693,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
public void testUpdateChannelNotifyListener() throws Exception { public void testUpdateChannelNotifyListener() throws Exception {
List<String> associations = new ArrayList<>(); List<String> associations = new ArrayList<>();
associations.add("a"); associations.add("a");
when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
mNotificationManagerService.setRankingHelper(mRankingHelper); mNotificationManagerService.setRankingHelper(mRankingHelper);
mTestNotificationChannel.setLightColor(Color.CYAN); mTestNotificationChannel.setLightColor(Color.CYAN);
when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
@@ -670,7 +711,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
public void testDeleteChannelNotifyListener() throws Exception { public void testDeleteChannelNotifyListener() throws Exception {
List<String> associations = new ArrayList<>(); List<String> associations = new ArrayList<>();
associations.add("a"); associations.add("a");
when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
mNotificationManagerService.setRankingHelper(mRankingHelper); mNotificationManagerService.setRankingHelper(mRankingHelper);
when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(), when(mRankingHelper.getNotificationChannel(eq(PKG), anyInt(),
eq(mTestNotificationChannel.getId()), anyBoolean())) eq(mTestNotificationChannel.getId()), anyBoolean()))
@@ -686,7 +727,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
public void testDeleteChannelGroupNotifyListener() throws Exception { public void testDeleteChannelGroupNotifyListener() throws Exception {
List<String> associations = new ArrayList<>(); List<String> associations = new ArrayList<>();
associations.add("a"); associations.add("a");
when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
NotificationChannelGroup ncg = new NotificationChannelGroup("a", "b/c"); NotificationChannelGroup ncg = new NotificationChannelGroup("a", "b/c");
mNotificationManagerService.setRankingHelper(mRankingHelper); mNotificationManagerService.setRankingHelper(mRankingHelper);
when(mRankingHelper.getNotificationChannelGroup(eq(ncg.getId()), eq(PKG), anyInt())) when(mRankingHelper.getNotificationChannelGroup(eq(ncg.getId()), eq(PKG), anyInt()))
@@ -703,7 +744,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
mNotificationManagerService.setRankingHelper(mRankingHelper); mNotificationManagerService.setRankingHelper(mRankingHelper);
List<String> associations = new ArrayList<>(); List<String> associations = new ArrayList<>();
associations.add("a"); associations.add("a");
when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
mBinderService.updateNotificationChannelFromPrivilegedListener( mBinderService.updateNotificationChannelFromPrivilegedListener(
null, PKG, Process.myUserHandle(), mTestNotificationChannel); null, PKG, Process.myUserHandle(), mTestNotificationChannel);
@@ -719,7 +760,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
public void testUpdateNotificationChannelFromPrivilegedListener_noAccess() throws Exception { public void testUpdateNotificationChannelFromPrivilegedListener_noAccess() throws Exception {
mNotificationManagerService.setRankingHelper(mRankingHelper); mNotificationManagerService.setRankingHelper(mRankingHelper);
List<String> associations = new ArrayList<>(); List<String> associations = new ArrayList<>();
when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
try { try {
mBinderService.updateNotificationChannelFromPrivilegedListener( mBinderService.updateNotificationChannelFromPrivilegedListener(
@@ -741,7 +782,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
mNotificationManagerService.setRankingHelper(mRankingHelper); mNotificationManagerService.setRankingHelper(mRankingHelper);
List<String> associations = new ArrayList<>(); List<String> associations = new ArrayList<>();
associations.add("a"); associations.add("a");
when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
mListener = mock(ManagedServices.ManagedServiceInfo.class); mListener = mock(ManagedServices.ManagedServiceInfo.class);
mListener.component = new ComponentName(PKG, PKG); mListener.component = new ComponentName(PKG, PKG);
when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false);
@@ -767,7 +808,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
mNotificationManagerService.setRankingHelper(mRankingHelper); mNotificationManagerService.setRankingHelper(mRankingHelper);
List<String> associations = new ArrayList<>(); List<String> associations = new ArrayList<>();
associations.add("a"); associations.add("a");
when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
mBinderService.getNotificationChannelsFromPrivilegedListener( mBinderService.getNotificationChannelsFromPrivilegedListener(
null, PKG, Process.myUserHandle()); null, PKG, Process.myUserHandle());
@@ -780,7 +821,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
public void testGetNotificationChannelFromPrivilegedListener_noAccess() throws Exception { public void testGetNotificationChannelFromPrivilegedListener_noAccess() throws Exception {
mNotificationManagerService.setRankingHelper(mRankingHelper); mNotificationManagerService.setRankingHelper(mRankingHelper);
List<String> associations = new ArrayList<>(); List<String> associations = new ArrayList<>();
when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
try { try {
mBinderService.getNotificationChannelsFromPrivilegedListener( mBinderService.getNotificationChannelsFromPrivilegedListener(
@@ -799,7 +840,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
mNotificationManagerService.setRankingHelper(mRankingHelper); mNotificationManagerService.setRankingHelper(mRankingHelper);
List<String> associations = new ArrayList<>(); List<String> associations = new ArrayList<>();
associations.add("a"); associations.add("a");
when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
mListener = mock(ManagedServices.ManagedServiceInfo.class); mListener = mock(ManagedServices.ManagedServiceInfo.class);
when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false);
when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener); when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener);
@@ -821,7 +862,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
mNotificationManagerService.setRankingHelper(mRankingHelper); mNotificationManagerService.setRankingHelper(mRankingHelper);
List<String> associations = new ArrayList<>(); List<String> associations = new ArrayList<>();
associations.add("a"); associations.add("a");
when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
mBinderService.getNotificationChannelGroupsFromPrivilegedListener( mBinderService.getNotificationChannelGroupsFromPrivilegedListener(
null, PKG, Process.myUserHandle()); null, PKG, Process.myUserHandle());
@@ -833,7 +874,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
public void testGetNotificationChannelGroupsFromPrivilegedListener_noAccess() throws Exception { public void testGetNotificationChannelGroupsFromPrivilegedListener_noAccess() throws Exception {
mNotificationManagerService.setRankingHelper(mRankingHelper); mNotificationManagerService.setRankingHelper(mRankingHelper);
List<String> associations = new ArrayList<>(); List<String> associations = new ArrayList<>();
when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
try { try {
mBinderService.getNotificationChannelGroupsFromPrivilegedListener( mBinderService.getNotificationChannelGroupsFromPrivilegedListener(
@@ -850,7 +891,7 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
public void testGetNotificationChannelGroupsFromPrivilegedListener_badUser() throws Exception { public void testGetNotificationChannelGroupsFromPrivilegedListener_badUser() throws Exception {
mNotificationManagerService.setRankingHelper(mRankingHelper); mNotificationManagerService.setRankingHelper(mRankingHelper);
List<String> associations = new ArrayList<>(); List<String> associations = new ArrayList<>();
when(mCompanionMgr.getAssociations(PKG, uid)).thenReturn(associations); when(mCompanionMgr.getAssociations(PKG, mUid)).thenReturn(associations);
mListener = mock(ManagedServices.ManagedServiceInfo.class); mListener = mock(ManagedServices.ManagedServiceInfo.class);
when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false); when(mListener.enabledAndUserMatches(anyInt())).thenReturn(false);
when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener); when(mNotificationListeners.checkServiceTokenLocked(any())).thenReturn(mListener);
@@ -1020,8 +1061,8 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
.setColorized(true) .setColorized(true)
.setFlag(Notification.FLAG_CAN_COLORIZE, true) .setFlag(Notification.FLAG_CAN_COLORIZE, true)
.setSmallIcon(android.R.drawable.sym_def_app_icon); .setSmallIcon(android.R.drawable.sym_def_app_icon);
StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", uid, 0, StatusBarNotification sbn = new StatusBarNotification(PKG, PKG, 1, "tag", mUid, 0,
nb.build(), new UserHandle(uid), null, 0); nb.build(), new UserHandle(mUid), null, 0);
NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel); NotificationRecord nr = new NotificationRecord(mContext, sbn, mTestNotificationChannel);
mBinderService.enqueueNotificationWithTag(PKG, PKG, null, mBinderService.enqueueNotificationWithTag(PKG, PKG, null,
@@ -1037,11 +1078,13 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
@Test @Test
public void testGetNotificationCountLocked() throws Exception { public void testGetNotificationCountLocked() throws Exception {
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, i, null, false); NotificationRecord r =
generateNotificationRecord(mTestNotificationChannel, i, null, false);
mNotificationManagerService.addEnqueuedNotification(r); mNotificationManagerService.addEnqueuedNotification(r);
} }
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
NotificationRecord r = generateNotificationRecord(mTestNotificationChannel, i, null, false); NotificationRecord r =
generateNotificationRecord(mTestNotificationChannel, i, null, false);
mNotificationManagerService.addNotification(r); mNotificationManagerService.addNotification(r);
} }
@@ -1051,19 +1094,23 @@ public class NotificationManagerServiceTest extends NotificationTestCase {
.setSmallIcon(android.R.drawable.sym_def_app_icon) .setSmallIcon(android.R.drawable.sym_def_app_icon)
.build(); .build();
StatusBarNotification sbn = new StatusBarNotification("a", "a", 0, "tag", uid, 0, StatusBarNotification sbn = new StatusBarNotification("a", "a", 0, "tag", mUid, 0,
n, new UserHandle(uid), null, 0); n, new UserHandle(mUid), null, 0);
NotificationRecord otherPackage = NotificationRecord otherPackage =
new NotificationRecord(mContext, sbn, mTestNotificationChannel); new NotificationRecord(mContext, sbn, mTestNotificationChannel);
mNotificationManagerService.addEnqueuedNotification(otherPackage); mNotificationManagerService.addEnqueuedNotification(otherPackage);
mNotificationManagerService.addNotification(otherPackage); mNotificationManagerService.addNotification(otherPackage);
// 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
assertEquals(40, mNotificationManagerService.getNotificationCountLocked(PKG, new UserHandle(uid).getIdentifier(), 0, null)); assertEquals(40, mNotificationManagerService.getNotificationCountLocked(
assertEquals(40, mNotificationManagerService.getNotificationCountLocked(PKG, new UserHandle(uid).getIdentifier(), 0, "tag2")); PKG, new UserHandle(mUid).getIdentifier(), 0, null));
assertEquals(2, mNotificationManagerService.getNotificationCountLocked("a", new UserHandle(uid).getIdentifier(), 0, "banana")); assertEquals(40, mNotificationManagerService.getNotificationCountLocked(
PKG, new UserHandle(mUid).getIdentifier(), 0, "tag2"));
assertEquals(2, mNotificationManagerService.getNotificationCountLocked(
"a", new UserHandle(mUid).getIdentifier(), 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, new UserHandle(uid).getIdentifier(), 0, "tag")); assertEquals(39, mNotificationManagerService.getNotificationCountLocked(
PKG, new UserHandle(mUid).getIdentifier(), 0, "tag"));
} }
} }