| 1 | /* |
| 2 | * Copyright (C) 2020 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.wm.shell.bubbles; |
| 18 | |
| 19 | import static java.lang.annotation.ElementType.FIELD; |
| 20 | import static java.lang.annotation.ElementType.LOCAL_VARIABLE; |
| 21 | import static java.lang.annotation.ElementType.PARAMETER; |
| 22 | import static java.lang.annotation.RetentionPolicy.SOURCE; |
| 23 | |
| 24 | import android.app.NotificationChannel; |
| 25 | import android.content.Intent; |
| 26 | import android.content.pm.UserInfo; |
| 27 | import android.os.UserHandle; |
| 28 | import android.service.notification.NotificationListenerService; |
| 29 | import android.service.notification.NotificationListenerService.RankingMap; |
| 30 | import android.util.Pair; |
| 31 | import android.util.SparseArray; |
| 32 | |
| 33 | import androidx.annotation.IntDef; |
| 34 | import androidx.annotation.Nullable; |
| 35 | |
| 36 | import com.android.wm.shell.common.annotations.ExternalThread; |
| 37 | |
| 38 | import java.lang.annotation.Retention; |
| 39 | import java.lang.annotation.Target; |
| 40 | import java.util.HashMap; |
| 41 | import java.util.List; |
| 42 | import java.util.Set; |
| 43 | import java.util.concurrent.Executor; |
| 44 | import java.util.function.Consumer; |
| 45 | import java.util.function.IntConsumer; |
| 46 | |
| 47 | /** |
| 48 | * Interface to engage bubbles feature. |
| 49 | */ |
| 50 | @ExternalThread |
| 51 | public interface Bubbles { |
| 52 | |
| 53 | @Retention(SOURCE) |
| 54 | @IntDef({DISMISS_USER_GESTURE, DISMISS_AGED, DISMISS_TASK_FINISHED, DISMISS_BLOCKED, |
| 55 | DISMISS_NOTIF_CANCEL, DISMISS_ACCESSIBILITY_ACTION, DISMISS_NO_LONGER_BUBBLE, |
| 56 | DISMISS_USER_CHANGED, DISMISS_GROUP_CANCELLED, DISMISS_INVALID_INTENT, |
| 57 | DISMISS_OVERFLOW_MAX_REACHED, DISMISS_SHORTCUT_REMOVED, DISMISS_PACKAGE_REMOVED, |
| 58 | DISMISS_NO_BUBBLE_UP, DISMISS_RELOAD_FROM_DISK, DISMISS_USER_REMOVED}) |
| 59 | @Target({FIELD, LOCAL_VARIABLE, PARAMETER}) |
| 60 | @interface DismissReason {} |
| 61 | |
| 62 | int DISMISS_USER_GESTURE = 1; |
| 63 | int DISMISS_AGED = 2; |
| 64 | int DISMISS_TASK_FINISHED = 3; |
| 65 | int DISMISS_BLOCKED = 4; |
| 66 | int DISMISS_NOTIF_CANCEL = 5; |
| 67 | int DISMISS_ACCESSIBILITY_ACTION = 6; |
| 68 | int DISMISS_NO_LONGER_BUBBLE = 7; |
| 69 | int DISMISS_USER_CHANGED = 8; |
| 70 | int DISMISS_GROUP_CANCELLED = 9; |
| 71 | int DISMISS_INVALID_INTENT = 10; |
| 72 | int DISMISS_OVERFLOW_MAX_REACHED = 11; |
| 73 | int DISMISS_SHORTCUT_REMOVED = 12; |
| 74 | int DISMISS_PACKAGE_REMOVED = 13; |
| 75 | int DISMISS_NO_BUBBLE_UP = 14; |
| 76 | int DISMISS_RELOAD_FROM_DISK = 15; |
| 77 | int DISMISS_USER_REMOVED = 16; |
| 78 | |
| 79 | /** |
| 80 | * @return {@code true} if there is a bubble associated with the provided key and if its |
| 81 | * notification is hidden from the shade or there is a group summary associated with the |
| 82 | * provided key that is hidden from the shade because it has been dismissed but still has child |
| 83 | * bubbles active. |
| 84 | */ |
| 85 | boolean isBubbleNotificationSuppressedFromShade(String key, String groupKey); |
| 86 | |
| 87 | /** |
| 88 | * @return {@code true} if the current notification entry same as selected bubble |
| 89 | * notification entry and the stack is currently expanded. |
| 90 | */ |
| 91 | boolean isBubbleExpanded(String key); |
| 92 | |
| 93 | /** Tell the stack of bubbles to collapse. */ |
| 94 | void collapseStack(); |
| 95 | |
| 96 | /** |
| 97 | * Request the stack expand if needed, then select the specified Bubble as current. |
| 98 | * If no bubble exists for this entry, one is created. |
| 99 | * |
| 100 | * @param entry the notification for the bubble to be selected |
| 101 | */ |
| 102 | void expandStackAndSelectBubble(BubbleEntry entry); |
| 103 | |
| 104 | /** |
| 105 | * Request the stack expand if needed, then select the specified Bubble as current. |
| 106 | * |
| 107 | * @param bubble the bubble to be selected |
| 108 | */ |
| 109 | void expandStackAndSelectBubble(Bubble bubble); |
| 110 | |
| 111 | /** |
| 112 | * Adds and expands bubble that is not notification based, but instead based on an intent from |
| 113 | * the app. The intent must be explicit (i.e. include a package name or fully qualified |
| 114 | * component class name) and the activity for it should be resizable. |
| 115 | * |
| 116 | * @param intent the intent to populate the bubble. |
| 117 | */ |
| 118 | void showAppBubble(Intent intent); |
| 119 | |
| 120 | /** |
| 121 | * @return a bubble that matches the provided shortcutId, if one exists. |
| 122 | */ |
| 123 | @Nullable |
| 124 | Bubble getBubbleWithShortcutId(String shortcutId); |
| 125 | |
| 126 | /** |
| 127 | * We intercept notification entries (including group summaries) dismissed by the user when |
| 128 | * there is an active bubble associated with it. We do this so that developers can still |
| 129 | * cancel it (and hence the bubbles associated with it). However, these intercepted |
| 130 | * notifications should then be hidden from the shade since the user has cancelled them, so we |
| 131 | * {@link Bubble#setSuppressNotification}. For the case of suppressed summaries, we also add |
| 132 | * {@link BubbleData#addSummaryToSuppress}. |
| 133 | * |
| 134 | * @param entry the notification of the BubbleEntry should be removed. |
| 135 | * @param children the list of child notification of the BubbleEntry from 1st param entry, |
| 136 | * this will be null if entry does have no children. |
| 137 | * @param removeCallback the remove callback for SystemUI side to remove notification, the int |
| 138 | * number should be list position of children list and use -1 for |
| 139 | * removing the parent notification. |
| 140 | * |
| 141 | * @return true if we want to intercept the dismissal of the entry, else false. |
| 142 | */ |
| 143 | boolean handleDismissalInterception(BubbleEntry entry, @Nullable List<BubbleEntry> children, |
| 144 | IntConsumer removeCallback, Executor callbackExecutor); |
| 145 | |
| 146 | /** Set the proxy to commnuicate with SysUi side components. */ |
| 147 | void setSysuiProxy(SysuiProxy proxy); |
| 148 | |
| 149 | /** Set a listener to be notified of bubble expand events. */ |
| 150 | void setExpandListener(BubbleExpandListener listener); |
| 151 | |
| 152 | /** |
| 153 | * Called when new notification entry added. |
| 154 | * |
| 155 | * @param entry the {@link BubbleEntry} by the notification. |
| 156 | */ |
| 157 | void onEntryAdded(BubbleEntry entry); |
| 158 | |
| 159 | /** |
| 160 | * Called when new notification entry updated. |
| 161 | * |
| 162 | * @param entry the {@link BubbleEntry} by the notification. |
| 163 | * @param shouldBubbleUp {@code true} if this notification should bubble up. |
| 164 | * @param fromSystem {@code true} if this update is from NotificationManagerService. |
| 165 | */ |
| 166 | void onEntryUpdated(BubbleEntry entry, boolean shouldBubbleUp, boolean fromSystem); |
| 167 | |
| 168 | /** |
| 169 | * Called when new notification entry removed. |
| 170 | * |
| 171 | * @param entry the {@link BubbleEntry} by the notification. |
| 172 | */ |
| 173 | void onEntryRemoved(BubbleEntry entry); |
| 174 | |
| 175 | /** |
| 176 | * Called when NotificationListener has received adjusted notification rank and reapplied |
| 177 | * filtering and sorting. This is used to dismiss or create bubbles based on changes in |
| 178 | * permissions on the notification channel or the global setting. |
| 179 | * |
| 180 | * @param rankingMap the updated ranking map from NotificationListenerService |
| 181 | * @param entryDataByKey a map of ranking key to bubble entry and whether the entry should |
| 182 | * bubble up |
| 183 | */ |
| 184 | void onRankingUpdated( |
| 185 | RankingMap rankingMap, |
| 186 | HashMap<String, Pair<BubbleEntry, Boolean>> entryDataByKey); |
| 187 | |
| 188 | /** |
| 189 | * Called when a notification channel is modified, in response to |
| 190 | * {@link NotificationListenerService#onNotificationChannelModified}. |
| 191 | * |
| 192 | * @param pkg the package the notification channel belongs to. |
| 193 | * @param user the user the notification channel belongs to. |
| 194 | * @param channel the channel being modified. |
| 195 | * @param modificationType the type of modification that occurred to the channel. |
| 196 | */ |
| 197 | void onNotificationChannelModified( |
| 198 | String pkg, |
| 199 | UserHandle user, |
| 200 | NotificationChannel channel, |
| 201 | int modificationType); |
| 202 | |
| 203 | /** |
| 204 | * Called when notification panel is expanded or collapsed |
| 205 | */ |
| 206 | void onNotificationPanelExpandedChanged(boolean expanded); |
| 207 | |
| 208 | /** |
| 209 | * Called when the status bar has become visible or invisible (either permanently or |
| 210 | * temporarily). |
| 211 | */ |
| 212 | void onStatusBarVisibilityChanged(boolean visible); |
| 213 | |
| 214 | /** Called when system zen mode state changed. */ |
| 215 | void onZenStateChanged(); |
| 216 | |
| 217 | /** |
| 218 | * Called when statusBar state changed. |
| 219 | * |
| 220 | * @param isShade {@code true} is state is SHADE. |
| 221 | */ |
| 222 | void onStatusBarStateChanged(boolean isShade); |
| 223 | |
| 224 | /** |
| 225 | * Called when the current user changed. |
| 226 | * |
| 227 | * @param newUserId the new user's id. |
| 228 | */ |
| 229 | void onUserChanged(int newUserId); |
| 230 | |
| 231 | /** |
| 232 | * Called when the current user profiles change. |
| 233 | * |
| 234 | * @param currentProfiles the user infos for the current profile. |
| 235 | */ |
| 236 | void onCurrentProfilesChanged(SparseArray<UserInfo> currentProfiles); |
| 237 | |
| 238 | /** |
| 239 | * Called when a user is removed. |
| 240 | * |
| 241 | * @param removedUserId the id of the removed user. |
| 242 | */ |
| 243 | void onUserRemoved(int removedUserId); |
| 244 | |
| 245 | /** |
| 246 | * Sets whether bubble bar should be enabled or not. |
| 247 | */ |
| 248 | void setBubbleBarEnabled(boolean enabled); |
| 249 | |
| 250 | /** Listener to find out about stack expansion / collapse events. */ |
| 251 | interface BubbleExpandListener { |
| 252 | /** |
| 253 | * Called when the expansion state of the bubble stack changes. |
| 254 | * |
| 255 | * @param isExpanding whether it's expanding or collapsing |
| 256 | * @param key the notification key associated with bubble being expanded |
| 257 | */ |
| 258 | void onBubbleExpandChanged(boolean isExpanding, String key); |
| 259 | } |
| 260 | |
| 261 | /** Listener to be notified when the flags on BubbleMetadata have changed. */ |
| 262 | interface BubbleMetadataFlagListener { |
| 263 | /** Called when the flags on BubbleMetadata have changed for the provided bubble. */ |
| 264 | void onBubbleMetadataFlagChanged(Bubble bubble); |
| 265 | } |
| 266 | |
| 267 | /** Listener to be notified when a pending intent has been canceled for a bubble. */ |
| 268 | interface PendingIntentCanceledListener { |
| 269 | /** Called when the pending intent for a bubble has been canceled. */ |
| 270 | void onPendingIntentCanceled(Bubble bubble); |
| 271 | } |
| 272 | |
| 273 | /** Callback to tell SysUi components execute some methods. */ |
| 274 | interface SysuiProxy { |
| 275 | void isNotificationPanelExpand(Consumer<Boolean> callback); |
| 276 | |
| 277 | void getPendingOrActiveEntry(String key, Consumer<BubbleEntry> callback); |
| 278 | |
| 279 | void getShouldRestoredEntries(Set<String> savedBubbleKeys, |
| 280 | Consumer<List<BubbleEntry>> callback); |
| 281 | |
| 282 | void setNotificationInterruption(String key); |
| 283 | |
| 284 | void requestNotificationShadeTopUi(boolean requestTopUi, String componentTag); |
| 285 | |
| 286 | void notifyRemoveNotification(String key, int reason); |
| 287 | |
| 288 | void notifyInvalidateNotifications(String reason); |
| 289 | |
| 290 | void updateNotificationBubbleButton(String key); |
| 291 | |
| 292 | void onStackExpandChanged(boolean shouldExpand); |
| 293 | |
| 294 | void onManageMenuExpandChanged(boolean menuExpanded); |
| 295 | |
| 296 | void onUnbubbleConversation(String key); |
| 297 | } |
| 298 | } |