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