• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.dialer.notification;
18 
19 import android.app.NotificationManager;
20 import android.service.notification.StatusBarNotification;
21 import android.support.annotation.NonNull;
22 import android.support.annotation.Nullable;
23 import java.util.Objects;
24 
25 /** Utilities for dealing with grouped notifications */
26 public final class GroupedNotificationUtil {
27 
28   /**
29    * Remove notification(s) that were added as part of a group. Will ensure that if this is the last
30    * notification in the group the summary will be removed.
31    *
32    * @param tag String tag as included in {@link NotificationManager#notify(String, int,
33    *     android.app.Notification)}. If null will remove all notifications under id
34    * @param id notification id as included with {@link NotificationManager#notify(String, int,
35    *     android.app.Notification)}.
36    * @param summaryTag String tag of the summary notification
37    */
removeNotification( @onNull NotificationManager notificationManager, @Nullable String tag, int id, @NonNull String summaryTag)38   public static void removeNotification(
39       @NonNull NotificationManager notificationManager,
40       @Nullable String tag,
41       int id,
42       @NonNull String summaryTag) {
43     if (tag == null) {
44       // Clear all grouped notifications
45       for (StatusBarNotification notification : notificationManager.getActiveNotifications()) {
46         if (notification.getId() == id) {
47           notificationManager.cancel(notification.getTag(), id);
48         }
49       }
50     } else {
51       notificationManager.cancel(tag, id);
52 
53       // See if other non-summary grouped notifications exist, and if not then clear the summary
54       boolean clearSummary = true;
55       for (StatusBarNotification notification : notificationManager.getActiveNotifications()) {
56         if (notification.getId() == id && !Objects.equals(summaryTag, notification.getTag())) {
57           clearSummary = false;
58           break;
59         }
60       }
61       if (clearSummary) {
62         notificationManager.cancel(summaryTag, id);
63       }
64     }
65   }
66 }
67