blob: ce805aadec777709c6980c54a7550b2c2182d188 [file] [log] [blame]
Julia Reynolds8f488d32016-10-14 10:59:01 -04001/*
2 * Copyright (C) 2016 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 */
16package com.android.server.notification;
17
18import android.service.notification.StatusBarNotification;
19import android.util.Log;
20import android.util.Slog;
21
22import java.util.ArrayList;
23import java.util.HashMap;
24import java.util.LinkedHashSet;
25import java.util.List;
26import java.util.Map;
27
28/**
29 * NotificationManagerService helper for auto-grouping notifications.
30 */
31public class GroupHelper {
32 private static final String TAG = "GroupHelper";
33 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
34
35 protected static final int AUTOGROUP_AT_COUNT = 4;
36 protected static final String AUTOGROUP_KEY = "ranker_group";
37
38 private final Callback mCallback;
39
40 // Map of user : <Map of package : notification keys>. Only contains notifications that are not
Julia Reynoldsa13b3e22017-08-10 16:58:54 -040041 // grouped by the app (aka no group or sort key).
Julia Reynolds8f488d32016-10-14 10:59:01 -040042 Map<Integer, Map<String, LinkedHashSet<String>>> mUngroupedNotifications = new HashMap<>();
43
44 public GroupHelper(Callback callback) {;
45 mCallback = callback;
46 }
47
Julia Reynoldsa13b3e22017-08-10 16:58:54 -040048 public void onNotificationPosted(StatusBarNotification sbn, boolean autogroupSummaryExists) {
Julia Reynolds8f488d32016-10-14 10:59:01 -040049 if (DEBUG) Log.i(TAG, "POSTED " + sbn.getKey());
50 try {
51 List<String> notificationsToGroup = new ArrayList<>();
52 if (!sbn.isAppGroup()) {
53 // Not grouped by the app, add to the list of notifications for the app;
54 // send grouping update if app exceeds the autogrouping limit.
55 synchronized (mUngroupedNotifications) {
56 Map<String, LinkedHashSet<String>> ungroupedNotificationsByUser
57 = mUngroupedNotifications.get(sbn.getUserId());
58 if (ungroupedNotificationsByUser == null) {
59 ungroupedNotificationsByUser = new HashMap<>();
60 }
61 mUngroupedNotifications.put(sbn.getUserId(), ungroupedNotificationsByUser);
62 LinkedHashSet<String> notificationsForPackage
63 = ungroupedNotificationsByUser.get(sbn.getPackageName());
64 if (notificationsForPackage == null) {
65 notificationsForPackage = new LinkedHashSet<>();
66 }
67
68 notificationsForPackage.add(sbn.getKey());
69 ungroupedNotificationsByUser.put(sbn.getPackageName(), notificationsForPackage);
70
Julia Reynoldsa13b3e22017-08-10 16:58:54 -040071 if (notificationsForPackage.size() >= AUTOGROUP_AT_COUNT
72 || autogroupSummaryExists) {
Julia Reynolds8f488d32016-10-14 10:59:01 -040073 notificationsToGroup.addAll(notificationsForPackage);
74 }
75 }
76 if (notificationsToGroup.size() > 0) {
77 adjustAutogroupingSummary(sbn.getUserId(), sbn.getPackageName(),
78 notificationsToGroup.get(0), true);
79 adjustNotificationBundling(notificationsToGroup, true);
80 }
81 } else {
82 // Grouped, but not by us. Send updates to un-autogroup, if we grouped it.
83 maybeUngroup(sbn, false, sbn.getUserId());
84 }
85 } catch (Exception e) {
86 Slog.e(TAG, "Failure processing new notification", e);
87 }
88 }
89
90 public void onNotificationRemoved(StatusBarNotification sbn) {
91 try {
92 maybeUngroup(sbn, true, sbn.getUserId());
93 } catch (Exception e) {
94 Slog.e(TAG, "Error processing canceled notification", e);
95 }
96 }
97
98 /**
Julia Reynoldsf4af65b2016-12-20 17:05:12 -050099 * Un-autogroups notifications that are now grouped by the app.
Julia Reynolds8f488d32016-10-14 10:59:01 -0400100 */
101 private void maybeUngroup(StatusBarNotification sbn, boolean notificationGone, int userId) {
102 List<String> notificationsToUnAutogroup = new ArrayList<>();
103 boolean removeSummary = false;
104 synchronized (mUngroupedNotifications) {
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500105 Map<String, LinkedHashSet<String>> ungroupedNotificationsByUser
Julia Reynolds8f488d32016-10-14 10:59:01 -0400106 = mUngroupedNotifications.get(sbn.getUserId());
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500107 if (ungroupedNotificationsByUser == null || ungroupedNotificationsByUser.size() == 0) {
Julia Reynolds8f488d32016-10-14 10:59:01 -0400108 return;
109 }
110 LinkedHashSet<String> notificationsForPackage
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500111 = ungroupedNotificationsByUser.get(sbn.getPackageName());
Julia Reynolds8f488d32016-10-14 10:59:01 -0400112 if (notificationsForPackage == null || notificationsForPackage.size() == 0) {
113 return;
114 }
115 if (notificationsForPackage.remove(sbn.getKey())) {
116 if (!notificationGone) {
117 // Add the current notification to the ungrouping list if it still exists.
118 notificationsToUnAutogroup.add(sbn.getKey());
119 }
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500120 }
121 // If the status change of this notification has brought the number of loose
122 // notifications to zero, remove the summary and un-autogroup.
123 if (notificationsForPackage.size() == 0) {
Julia Reynoldsa13b3e22017-08-10 16:58:54 -0400124 ungroupedNotificationsByUser.remove(sbn.getPackageName());
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500125 removeSummary = true;
Julia Reynolds8f488d32016-10-14 10:59:01 -0400126 }
127 }
Julia Reynoldsf4af65b2016-12-20 17:05:12 -0500128 if (removeSummary) {
129 adjustAutogroupingSummary(userId, sbn.getPackageName(), null, false);
130 }
Julia Reynolds8f488d32016-10-14 10:59:01 -0400131 if (notificationsToUnAutogroup.size() > 0) {
Julia Reynolds8f488d32016-10-14 10:59:01 -0400132 adjustNotificationBundling(notificationsToUnAutogroup, false);
133 }
134 }
135
136 private void adjustAutogroupingSummary(int userId, String packageName, String triggeringKey,
137 boolean summaryNeeded) {
138 if (summaryNeeded) {
139 mCallback.addAutoGroupSummary(userId, packageName, triggeringKey);
140 } else {
141 mCallback.removeAutoGroupSummary(userId, packageName);
142 }
143 }
144
145 private void adjustNotificationBundling(List<String> keys, boolean group) {
146 for (String key : keys) {
147 if (DEBUG) Log.i(TAG, "Sending grouping adjustment for: " + key + " group? " + group);
148 if (group) {
149 mCallback.addAutoGroup(key);
150 } else {
151 mCallback.removeAutoGroup(key);
152 }
153 }
154 }
155
156 protected interface Callback {
157 void addAutoGroup(String key);
158 void removeAutoGroup(String key);
159 void addAutoGroupSummary(int userId, String pkg, String triggeringKey);
160 void removeAutoGroupSummary(int user, String pkg);
161 }
162}