Skip to content

Commit 4465b0d

Browse files
Julia ReynoldsInVictusXV
authored andcommitted
Check group channels for FGSes
Before allowing the group to be deleted, by updating the current check to the method that populates the channel list Test: NotificationManagerServiceTest Bug: 209965481 Change-Id: I9db781c300e96e9c80bd5d21585b8be9b4db08c8 Merged-In: I9db781c300e96e9c80bd5d21585b8be9b4db08c8 (cherry picked from commit 6456b62) Merged-In:I9db781c300e96e9c80bd5d21585b8be9b4db08c8
1 parent ea4bfb8 commit 4465b0d

2 files changed

Lines changed: 62 additions & 6 deletions

File tree

services/core/java/com/android/server/notification/NotificationManagerService.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,7 +2504,7 @@ private void maybeNotifyChannelOwner(String pkg, int uid, NotificationChannel pr
25042504
}
25052505
}
25062506

2507-
private void createNotificationChannelGroup(String pkg, int uid, NotificationChannelGroup group,
2507+
void createNotificationChannelGroup(String pkg, int uid, NotificationChannelGroup group,
25082508
boolean fromApp, boolean fromListener) {
25092509
Objects.requireNonNull(group);
25102510
Objects.requireNonNull(pkg);
@@ -3537,7 +3537,8 @@ public void deleteNotificationChannelGroup(String pkg, String groupId) {
35373537

35383538
final int callingUid = Binder.getCallingUid();
35393539
NotificationChannelGroup groupToDelete =
3540-
mPreferencesHelper.getNotificationChannelGroup(groupId, pkg, callingUid);
3540+
mPreferencesHelper.getNotificationChannelGroupWithChannels(
3541+
pkg, callingUid, groupId, false);
35413542
if (groupToDelete != null) {
35423543
// Preflight for allowability
35433544
final int userId = UserHandle.getUserId(callingUid);

services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java

100755100644
Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@
179179
import com.android.server.wm.ActivityTaskManagerInternal;
180180
import com.android.server.wm.WindowManagerInternal;
181181

182+
import com.google.common.collect.ImmutableList;
183+
182184
import org.junit.After;
183185
import org.junit.Before;
184186
import org.junit.Test;
@@ -202,6 +204,7 @@
202204
import java.util.List;
203205
import java.util.Map;
204206
import java.util.Set;
207+
import java.util.concurrent.CountDownLatch;
205208
import java.util.function.Consumer;
206209

207210

@@ -245,6 +248,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
245248
@Mock
246249
Resources mResources;
247250
@Mock
251+
ActivityManagerInternal mAmi;
252+
@Mock
248253
RankingHandler mRankingHandler;
249254
@Mock
250255
protected PackageManagerInternal mPackageManagerInternal;
@@ -418,7 +423,6 @@ public void setUp() throws Exception {
418423

419424
DeviceIdleInternal deviceIdleInternal = mock(DeviceIdleInternal.class);
420425
when(deviceIdleInternal.getNotificationWhitelistDuration()).thenReturn(3000L);
421-
ActivityManagerInternal activityManagerInternal = mock(ActivityManagerInternal.class);
422426

423427
LocalServices.removeServiceForTest(UriGrantsManagerInternal.class);
424428
LocalServices.addService(UriGrantsManagerInternal.class, mUgmInternal);
@@ -429,7 +433,7 @@ public void setUp() throws Exception {
429433
LocalServices.removeServiceForTest(DeviceIdleInternal.class);
430434
LocalServices.addService(DeviceIdleInternal.class, deviceIdleInternal);
431435
LocalServices.removeServiceForTest(ActivityManagerInternal.class);
432-
LocalServices.addService(ActivityManagerInternal.class, activityManagerInternal);
436+
LocalServices.addService(ActivityManagerInternal.class, mAmi);
433437
mContext.addMockSystemService(Context.ALARM_SERVICE, mAlarmManager);
434438

435439

@@ -506,7 +510,7 @@ null, new ComponentName(PKG, "test_class"),
506510
mock(DevicePolicyManagerInternal.class), mUgm, mUgmInternal,
507511
mAppOpsManager, mUm, mHistoryManager, mStatsManager,
508512
mock(TelephonyManager.class),
509-
mock(ActivityManagerInternal.class));
513+
mAmi);
510514
mService.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY);
511515

512516
mService.setAudioManager(mAudioManager);
@@ -2473,7 +2477,8 @@ public void testDeleteChannelGroupNotifyListener() throws Exception {
24732477
.thenReturn(associations);
24742478
NotificationChannelGroup ncg = new NotificationChannelGroup("a", "b/c");
24752479
mService.setPreferencesHelper(mPreferencesHelper);
2476-
when(mPreferencesHelper.getNotificationChannelGroup(eq(ncg.getId()), eq(PKG), anyInt()))
2480+
when(mPreferencesHelper.getNotificationChannelGroupWithChannels(
2481+
eq(PKG), anyInt(), eq(ncg.getId()), anyBoolean()))
24772482
.thenReturn(ncg);
24782483
reset(mListeners);
24792484
mBinderService.deleteNotificationChannelGroup(PKG, ncg.getId());
@@ -2482,6 +2487,56 @@ public void testDeleteChannelGroupNotifyListener() throws Exception {
24822487
eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED));
24832488
}
24842489

2490+
@Test
2491+
public void testDeleteChannelGroupChecksForFgses() throws Exception {
2492+
List<String> associations = new ArrayList<>();
2493+
associations.add("a");
2494+
when(mCompanionMgr.getAssociations(PKG, UserHandle.getUserId(mUid)))
2495+
.thenReturn(associations);
2496+
CountDownLatch latch = new CountDownLatch(2);
2497+
mService.createNotificationChannelGroup(
2498+
PKG, mUid, new NotificationChannelGroup("group", "group"), true, false);
2499+
new Thread(() -> {
2500+
NotificationChannel notificationChannel = new NotificationChannel("id", "id",
2501+
NotificationManager.IMPORTANCE_HIGH);
2502+
notificationChannel.setGroup("group");
2503+
ParceledListSlice<NotificationChannel> pls =
2504+
new ParceledListSlice(ImmutableList.of(notificationChannel));
2505+
try {
2506+
mBinderService.createNotificationChannelsForPackage(PKG, mUid, pls);
2507+
} catch (RemoteException e) {
2508+
throw new RuntimeException(e);
2509+
}
2510+
latch.countDown();
2511+
}).start();
2512+
new Thread(() -> {
2513+
try {
2514+
synchronized (this) {
2515+
wait(5000);
2516+
}
2517+
mService.createNotificationChannelGroup(PKG, mUid,
2518+
new NotificationChannelGroup("new", "new group"), true, false);
2519+
NotificationChannel notificationChannel =
2520+
new NotificationChannel("id", "id", NotificationManager.IMPORTANCE_HIGH);
2521+
notificationChannel.setGroup("new");
2522+
ParceledListSlice<NotificationChannel> pls =
2523+
new ParceledListSlice(ImmutableList.of(notificationChannel));
2524+
try {
2525+
mBinderService.createNotificationChannelsForPackage(PKG, mUid, pls);
2526+
mBinderService.deleteNotificationChannelGroup(PKG, "group");
2527+
} catch (RemoteException e) {
2528+
throw new RuntimeException(e);
2529+
}
2530+
} catch (Exception e) {
2531+
e.printStackTrace();
2532+
}
2533+
latch.countDown();
2534+
}).start();
2535+
2536+
latch.await();
2537+
verify(mAmi).hasForegroundServiceNotification(anyString(), anyInt(), anyString());
2538+
}
2539+
24852540
@Test
24862541
public void testUpdateNotificationChannelFromPrivilegedListener_success() throws Exception {
24872542
mService.setPreferencesHelper(mPreferencesHelper);

0 commit comments

Comments
 (0)