summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/notification/DialerNotificationManager.java
blob: 0e34201690b12405aa10ae151e470b5800943f34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.dialer.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.service.notification.StatusBarNotification;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.os.BuildCompat;
import android.text.TextUtils;
import android.util.Pair;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;

/**
 * Wrapper around the notification manager APIs. The wrapper ensures that channels are set and that
 * notifications are limited to 10 per group.
 */
public final class DialerNotificationManager {
  public static void notify(@NonNull Context context, int id, @NonNull Notification notification) {
    Assert.isNotNull(context);
    Assert.isNotNull(notification);
    throw Assert.createUnsupportedOperationFailException("all notifications must have tags");
  }

  public static void notify(
      @NonNull Context context, @NonNull String tag, int id, @NonNull Notification notification) {
    Assert.isNotNull(context);
    Assert.isNotNull(notification);
    Assert.checkArgument(!TextUtils.isEmpty(tag));

    if (BuildCompat.isAtLeastO()) {
      Assert.checkArgument(!TextUtils.isEmpty(notification.getChannelId()));
    }

    getNotificationManager(context).notify(tag, id, notification);
    NotificationThrottler.throttle(context, notification);
  }

  public static void cancel(@NonNull Context context, int id) {
    Assert.isNotNull(context);
    throw Assert.createUnsupportedOperationFailException(
        "notification IDs are not unique across the app, a tag must be specified");
  }

  public static void cancel(@NonNull Context context, @NonNull String tag, int id) {
    Assert.isNotNull(context);
    Assert.checkArgument(!TextUtils.isEmpty(tag));

    NotificationManager notificationManager = getNotificationManager(context);
    StatusBarNotification[] notifications = notificationManager.getActiveNotifications();

    String groupKey = findGroupKey(notifications, tag, id);
    if (!TextUtils.isEmpty(groupKey)) {
      Pair<StatusBarNotification, Integer> groupSummaryAndCount =
          getGroupSummaryAndCount(notifications, groupKey);
      if (groupSummaryAndCount.first != null && groupSummaryAndCount.second <= 1) {
        LogUtil.i(
            "DialerNotificationManager.cancel",
            "last notification in group (%s) removed, also removing group summary",
            groupKey);
        notificationManager.cancel(
            groupSummaryAndCount.first.getTag(), groupSummaryAndCount.first.getId());
      }
    }

    notificationManager.cancel(tag, id);
  }

  public static StatusBarNotification[] getActiveNotifications(@NonNull Context context) {
    Assert.isNotNull(context);
    return getNotificationManager(context).getActiveNotifications();
  }

  @Nullable
  private static String findGroupKey(
      @NonNull StatusBarNotification[] notifications, @NonNull String tag, int id) {
    for (StatusBarNotification notification : notifications) {
      if (TextUtils.equals(tag, notification.getTag()) && id == notification.getId()) {
        return notification.getNotification().getGroup();
      }
    }
    return null;
  }

  @NonNull
  private static Pair<StatusBarNotification, Integer> getGroupSummaryAndCount(
      @NonNull StatusBarNotification[] notifications, @NonNull String groupKey) {
    StatusBarNotification groupSummaryNotification = null;
    int groupCount = 0;
    for (StatusBarNotification notification : notifications) {
      if (TextUtils.equals(groupKey, notification.getNotification().getGroup())) {
        if ((notification.getNotification().flags & Notification.FLAG_GROUP_SUMMARY) != 0) {
          groupSummaryNotification = notification;
        } else {
          groupCount++;
        }
      }
    }
    return new Pair<>(groupSummaryNotification, groupCount);
  }

  @NonNull
  private static NotificationManager getNotificationManager(@NonNull Context context) {
    return context.getSystemService(NotificationManager.class);
  }

  private DialerNotificationManager() {}
}