Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 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.systemui.statusbar; |
| 18 | |
| 19 | import static com.android.systemui.statusbar.RemoteInputController.processForRemoteInput; |
Mady Mellor | 1a4e86f | 2019-05-03 16:07:23 -0700 | [diff] [blame] | 20 | import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON; |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 21 | import static com.android.systemui.statusbar.phone.StatusBar.DEBUG; |
| 22 | import static com.android.systemui.statusbar.phone.StatusBar.ENABLE_CHILD_NOTIFICATIONS; |
| 23 | |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 24 | import android.annotation.SuppressLint; |
| 25 | import android.app.NotificationManager; |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 26 | import android.content.ComponentName; |
| 27 | import android.content.Context; |
| 28 | import android.os.RemoteException; |
| 29 | import android.os.UserHandle; |
| 30 | import android.service.notification.StatusBarNotification; |
| 31 | import android.util.Log; |
| 32 | |
Eliot Courtney | 6c313d3 | 2017-12-14 19:57:51 +0900 | [diff] [blame] | 33 | import com.android.systemui.Dependency; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 34 | import com.android.systemui.statusbar.notification.NotificationEntryManager; |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 35 | import com.android.systemui.statusbar.phone.NotificationGroupManager; |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 36 | import com.android.systemui.statusbar.phone.NotificationListenerWithPlugins; |
| 37 | |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 38 | import java.util.ArrayList; |
| 39 | |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 40 | /** |
| 41 | * This class handles listening to notification updates and passing them along to |
| 42 | * NotificationPresenter to be displayed to the user. |
| 43 | */ |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 44 | @SuppressLint("OverrideAbstract") |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 45 | public class NotificationListener extends NotificationListenerWithPlugins { |
| 46 | private static final String TAG = "NotificationListener"; |
| 47 | |
Eliot Courtney | 6c313d3 | 2017-12-14 19:57:51 +0900 | [diff] [blame] | 48 | // Dependencies: |
| 49 | private final NotificationRemoteInputManager mRemoteInputManager = |
| 50 | Dependency.get(NotificationRemoteInputManager.class); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 51 | private final NotificationEntryManager mEntryManager = |
| 52 | Dependency.get(NotificationEntryManager.class); |
| 53 | private final NotificationGroupManager mGroupManager = |
| 54 | Dependency.get(NotificationGroupManager.class); |
Eliot Courtney | 6c313d3 | 2017-12-14 19:57:51 +0900 | [diff] [blame] | 55 | |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 56 | private final ArrayList<NotificationSettingsListener> mSettingsListeners = new ArrayList<>(); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 57 | private final Context mContext; |
| 58 | |
Eliot Courtney | 6c313d3 | 2017-12-14 19:57:51 +0900 | [diff] [blame] | 59 | public NotificationListener(Context context) { |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 60 | mContext = context; |
| 61 | } |
| 62 | |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 63 | public void addNotificationSettingsListener(NotificationSettingsListener listener) { |
| 64 | mSettingsListeners.add(listener); |
| 65 | } |
| 66 | |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 67 | @Override |
| 68 | public void onListenerConnected() { |
| 69 | if (DEBUG) Log.d(TAG, "onListenerConnected"); |
| 70 | onPluginConnected(); |
| 71 | final StatusBarNotification[] notifications = getActiveNotifications(); |
| 72 | if (notifications == null) { |
| 73 | Log.w(TAG, "onListenerConnected unable to get active notifications."); |
| 74 | return; |
| 75 | } |
| 76 | final RankingMap currentRanking = getCurrentRanking(); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 77 | Dependency.get(Dependency.MAIN_HANDLER).post(() -> { |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 78 | for (StatusBarNotification sbn : notifications) { |
Eliot Courtney | 4a96b36 | 2017-12-14 19:38:52 +0900 | [diff] [blame] | 79 | mEntryManager.addNotification(sbn, currentRanking); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 80 | } |
| 81 | }); |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 82 | NotificationManager noMan = mContext.getSystemService(NotificationManager.class); |
Julia Reynolds | 6d2b7e6 | 2019-03-22 15:15:38 -0400 | [diff] [blame] | 83 | onSilentStatusBarIconsVisibilityChanged(noMan.shouldHideSilentStatusBarIcons()); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void onNotificationPosted(final StatusBarNotification sbn, |
| 88 | final RankingMap rankingMap) { |
| 89 | if (DEBUG) Log.d(TAG, "onNotificationPosted: " + sbn); |
| 90 | if (sbn != null && !onPluginNotificationPosted(sbn, rankingMap)) { |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 91 | Dependency.get(Dependency.MAIN_HANDLER).post(() -> { |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 92 | processForRemoteInput(sbn.getNotification(), mContext); |
| 93 | String key = sbn.getKey(); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 94 | boolean isUpdate = |
Eliot Courtney | 4a96b36 | 2017-12-14 19:38:52 +0900 | [diff] [blame] | 95 | mEntryManager.getNotificationData().get(key) != null; |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 96 | // In case we don't allow child notifications, we ignore children of |
| 97 | // notifications that have a summary, since` we're not going to show them |
| 98 | // anyway. This is true also when the summary is canceled, |
| 99 | // because children are automatically canceled by NoMan in that case. |
| 100 | if (!ENABLE_CHILD_NOTIFICATIONS |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 101 | && mGroupManager.isChildInGroupWithSummary(sbn)) { |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 102 | if (DEBUG) { |
| 103 | Log.d(TAG, "Ignoring group child due to existing summary: " + sbn); |
| 104 | } |
| 105 | |
| 106 | // Remove existing notification to avoid stale data. |
| 107 | if (isUpdate) { |
Mady Mellor | 1a4e86f | 2019-05-03 16:07:23 -0700 | [diff] [blame] | 108 | mEntryManager.removeNotification(key, rankingMap, UNDEFINED_DISMISS_REASON); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 109 | } else { |
Eliot Courtney | 4a96b36 | 2017-12-14 19:38:52 +0900 | [diff] [blame] | 110 | mEntryManager.getNotificationData() |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 111 | .updateRanking(rankingMap); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 112 | } |
| 113 | return; |
| 114 | } |
| 115 | if (isUpdate) { |
Eliot Courtney | 4a96b36 | 2017-12-14 19:38:52 +0900 | [diff] [blame] | 116 | mEntryManager.updateNotification(sbn, rankingMap); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 117 | } else { |
Eliot Courtney | 4a96b36 | 2017-12-14 19:38:52 +0900 | [diff] [blame] | 118 | mEntryManager.addNotification(sbn, rankingMap); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 119 | } |
| 120 | }); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | @Override |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 125 | public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap, |
| 126 | int reason) { |
| 127 | if (DEBUG) Log.d(TAG, "onNotificationRemoved: " + sbn + " reason: " + reason); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 128 | if (sbn != null && !onPluginNotificationRemoved(sbn, rankingMap)) { |
| 129 | final String key = sbn.getKey(); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 130 | Dependency.get(Dependency.MAIN_HANDLER).post(() -> { |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 131 | mEntryManager.removeNotification(key, rankingMap, reason); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 132 | }); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | @Override |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 137 | public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) { |
Mady Mellor | 1a4e86f | 2019-05-03 16:07:23 -0700 | [diff] [blame] | 138 | onNotificationRemoved(sbn, rankingMap, UNDEFINED_DISMISS_REASON); |
Mady Mellor | c2ff011 | 2019-03-28 14:18:06 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 142 | public void onNotificationRankingUpdate(final RankingMap rankingMap) { |
| 143 | if (DEBUG) Log.d(TAG, "onRankingUpdate"); |
| 144 | if (rankingMap != null) { |
| 145 | RankingMap r = onPluginRankingUpdate(rankingMap); |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 146 | Dependency.get(Dependency.MAIN_HANDLER).post(() -> { |
Eliot Courtney | 4a96b36 | 2017-12-14 19:38:52 +0900 | [diff] [blame] | 147 | mEntryManager.updateNotificationRanking(r); |
Eliot Courtney | a6d8cf2 | 2017-10-20 13:26:58 +0900 | [diff] [blame] | 148 | }); |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 152 | @Override |
Julia Reynolds | 6d2b7e6 | 2019-03-22 15:15:38 -0400 | [diff] [blame] | 153 | public void onSilentStatusBarIconsVisibilityChanged(boolean hideSilentStatusIcons) { |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 154 | for (NotificationSettingsListener listener : mSettingsListeners) { |
| 155 | listener.onStatusBarIconsBehaviorChanged(hideSilentStatusIcons); |
| 156 | } |
| 157 | } |
| 158 | |
Julia Reynolds | 7c2367f | 2019-01-30 14:50:14 -0500 | [diff] [blame] | 159 | public void registerAsSystemService() { |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 160 | try { |
| 161 | registerAsSystemService(mContext, |
| 162 | new ComponentName(mContext.getPackageName(), getClass().getCanonicalName()), |
| 163 | UserHandle.USER_ALL); |
| 164 | } catch (RemoteException e) { |
| 165 | Log.e(TAG, "Unable to register notification listener", e); |
| 166 | } |
| 167 | } |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 168 | |
| 169 | public interface NotificationSettingsListener { |
| 170 | |
| 171 | default void onStatusBarIconsBehaviorChanged(boolean hideSilentStatusIcons) { } |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 172 | } |
Eliot Courtney | 21bc05f | 2017-10-19 17:03:34 +0900 | [diff] [blame] | 173 | } |