Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Ned Burns | c586467 | 2019-02-20 12:57:29 -0500 | [diff] [blame] | 17 | package com.android.systemui.statusbar.notification.collection; |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 18 | |
| 19 | import static com.android.internal.util.Preconditions.checkNotNull; |
| 20 | import static com.android.systemui.statusbar.NotificationRemoteInputManager.ENABLE_REMOTE_INPUT; |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 21 | import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_AMBIENT; |
| 22 | import static com.android.systemui.statusbar.notification.row.NotificationContentInflater.FLAG_CONTENT_VIEW_HEADS_UP; |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 23 | |
| 24 | import android.annotation.Nullable; |
| 25 | import android.content.Context; |
| 26 | import android.content.pm.ApplicationInfo; |
| 27 | import android.content.pm.PackageManager; |
| 28 | import android.os.Build; |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 29 | import android.service.notification.StatusBarNotification; |
| 30 | import android.util.Log; |
| 31 | import android.view.ViewGroup; |
| 32 | |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 33 | import com.android.internal.util.NotificationMessagingUtil; |
| 34 | import com.android.systemui.Dependency; |
| 35 | import com.android.systemui.R; |
| 36 | import com.android.systemui.UiOffloadThread; |
| 37 | import com.android.systemui.statusbar.NotificationLockscreenUserManager; |
| 38 | import com.android.systemui.statusbar.NotificationPresenter; |
| 39 | import com.android.systemui.statusbar.NotificationRemoteInputManager; |
| 40 | import com.android.systemui.statusbar.NotificationUiAdjustment; |
Ned Burns | c586467 | 2019-02-20 12:57:29 -0500 | [diff] [blame] | 41 | import com.android.systemui.statusbar.notification.InflationException; |
| 42 | import com.android.systemui.statusbar.notification.NotificationClicker; |
| 43 | import com.android.systemui.statusbar.notification.NotificationInterruptionStateProvider; |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 44 | import com.android.systemui.statusbar.notification.logging.NotificationLogger; |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 45 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 46 | import com.android.systemui.statusbar.notification.row.NotificationContentInflater; |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 47 | import com.android.systemui.statusbar.notification.row.NotificationGutsManager; |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 48 | import com.android.systemui.statusbar.notification.row.RowInflaterTask; |
| 49 | import com.android.systemui.statusbar.notification.stack.NotificationListContainer; |
| 50 | import com.android.systemui.statusbar.phone.NotificationGroupManager; |
| 51 | import com.android.systemui.statusbar.phone.StatusBar; |
| 52 | import com.android.systemui.statusbar.policy.HeadsUpManager; |
| 53 | |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 54 | /** Handles inflating and updating views for notifications. */ |
Ned Burns | c586467 | 2019-02-20 12:57:29 -0500 | [diff] [blame] | 55 | public class NotificationRowBinderImpl implements NotificationRowBinder { |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 56 | |
| 57 | private static final String TAG = "NotificationViewManager"; |
| 58 | |
| 59 | private final NotificationGroupManager mGroupManager = |
| 60 | Dependency.get(NotificationGroupManager.class); |
| 61 | private final NotificationGutsManager mGutsManager = |
| 62 | Dependency.get(NotificationGutsManager.class); |
| 63 | private final UiOffloadThread mUiOffloadThread = Dependency.get(UiOffloadThread.class); |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 64 | private final NotificationInterruptionStateProvider mNotificationInterruptionStateProvider = |
| 65 | Dependency.get(NotificationInterruptionStateProvider.class); |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 66 | |
| 67 | private final Context mContext; |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 68 | private final NotificationMessagingUtil mMessagingUtil; |
| 69 | private final ExpandableNotificationRow.ExpansionLogger mExpansionLogger = |
| 70 | this::logNotificationExpansion; |
Gus Prevas | 59ec2ff | 2018-12-28 16:20:28 -0500 | [diff] [blame] | 71 | private final boolean mAllowLongPress; |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 72 | |
| 73 | private NotificationRemoteInputManager mRemoteInputManager; |
| 74 | private NotificationPresenter mPresenter; |
| 75 | private NotificationListContainer mListContainer; |
| 76 | private HeadsUpManager mHeadsUpManager; |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 77 | private NotificationContentInflater.InflationCallback mInflationCallback; |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 78 | private ExpandableNotificationRow.OnAppOpsClickListener mOnAppOpsClickListener; |
| 79 | private BindRowCallback mBindRowCallback; |
| 80 | private NotificationClicker mNotificationClicker; |
Tony Mak | 202f25d | 2019-01-07 14:40:39 +0000 | [diff] [blame] | 81 | private final NotificationLogger mNotificationLogger = Dependency.get(NotificationLogger.class); |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 82 | |
Ned Burns | c586467 | 2019-02-20 12:57:29 -0500 | [diff] [blame] | 83 | public NotificationRowBinderImpl(Context context, boolean allowLongPress) { |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 84 | mContext = context; |
| 85 | mMessagingUtil = new NotificationMessagingUtil(context); |
Gus Prevas | 59ec2ff | 2018-12-28 16:20:28 -0500 | [diff] [blame] | 86 | mAllowLongPress = allowLongPress; |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | private NotificationRemoteInputManager getRemoteInputManager() { |
| 90 | if (mRemoteInputManager == null) { |
| 91 | mRemoteInputManager = Dependency.get(NotificationRemoteInputManager.class); |
| 92 | } |
| 93 | return mRemoteInputManager; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Sets up late-bound dependencies for this component. |
| 98 | */ |
| 99 | public void setUpWithPresenter(NotificationPresenter presenter, |
| 100 | NotificationListContainer listContainer, |
| 101 | HeadsUpManager headsUpManager, |
Ned Burns | 1a5e22f | 2019-02-14 15:11:52 -0500 | [diff] [blame] | 102 | NotificationContentInflater.InflationCallback inflationCallback, |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 103 | BindRowCallback bindRowCallback) { |
| 104 | mPresenter = presenter; |
| 105 | mListContainer = listContainer; |
| 106 | mHeadsUpManager = headsUpManager; |
| 107 | mInflationCallback = inflationCallback; |
| 108 | mBindRowCallback = bindRowCallback; |
| 109 | mOnAppOpsClickListener = mGutsManager::openGuts; |
| 110 | } |
| 111 | |
| 112 | public void setNotificationClicker(NotificationClicker clicker) { |
| 113 | mNotificationClicker = clicker; |
| 114 | } |
| 115 | |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 116 | /** |
| 117 | * Inflates the views for the given entry (possibly asynchronously). |
| 118 | */ |
Ned Burns | c586467 | 2019-02-20 12:57:29 -0500 | [diff] [blame] | 119 | @Override |
Gus Prevas | 86928bb | 2019-01-04 16:46:13 -0500 | [diff] [blame] | 120 | public void inflateViews( |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 121 | NotificationEntry entry, |
Ned Burns | f529ec2 | 2019-02-12 19:33:50 -0500 | [diff] [blame] | 122 | Runnable onDismissRunnable) |
Gus Prevas | 86928bb | 2019-01-04 16:46:13 -0500 | [diff] [blame] | 123 | throws InflationException { |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 124 | ViewGroup parent = mListContainer.getViewParentForNotification(entry); |
| 125 | PackageManager pmUser = StatusBar.getPackageManagerForUser(mContext, |
| 126 | entry.notification.getUser().getIdentifier()); |
| 127 | |
| 128 | final StatusBarNotification sbn = entry.notification; |
| 129 | if (entry.rowExists()) { |
Gus Prevas | 86928bb | 2019-01-04 16:46:13 -0500 | [diff] [blame] | 130 | entry.updateIcons(mContext, sbn); |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 131 | entry.reset(); |
Ned Burns | f529ec2 | 2019-02-12 19:33:50 -0500 | [diff] [blame] | 132 | updateNotification(entry, pmUser, sbn, entry.getRow()); |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 133 | } else { |
Gus Prevas | 86928bb | 2019-01-04 16:46:13 -0500 | [diff] [blame] | 134 | entry.createIcons(mContext, sbn); |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 135 | new RowInflaterTask().inflate(mContext, parent, entry, |
| 136 | row -> { |
| 137 | bindRow(entry, pmUser, sbn, row, onDismissRunnable); |
Ned Burns | f529ec2 | 2019-02-12 19:33:50 -0500 | [diff] [blame] | 138 | updateNotification(entry, pmUser, sbn, row); |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 139 | }); |
| 140 | } |
| 141 | } |
| 142 | |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 143 | private void bindRow(NotificationEntry entry, PackageManager pmUser, |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 144 | StatusBarNotification sbn, ExpandableNotificationRow row, |
| 145 | Runnable onDismissRunnable) { |
| 146 | row.setExpansionLogger(mExpansionLogger, entry.notification.getKey()); |
| 147 | row.setGroupManager(mGroupManager); |
| 148 | row.setHeadsUpManager(mHeadsUpManager); |
| 149 | row.setOnExpandClickListener(mPresenter); |
| 150 | row.setInflationCallback(mInflationCallback); |
Gus Prevas | 59ec2ff | 2018-12-28 16:20:28 -0500 | [diff] [blame] | 151 | if (mAllowLongPress) { |
| 152 | row.setLongPressListener(mGutsManager::openGuts); |
| 153 | } |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 154 | mListContainer.bindRow(row); |
| 155 | getRemoteInputManager().bindRow(row); |
| 156 | |
| 157 | // Get the app name. |
| 158 | // Note that Notification.Builder#bindHeaderAppName has similar logic |
| 159 | // but since this field is used in the guts, it must be accurate. |
| 160 | // Therefore we will only show the application label, or, failing that, the |
| 161 | // package name. No substitutions. |
| 162 | final String pkg = sbn.getPackageName(); |
| 163 | String appname = pkg; |
| 164 | try { |
| 165 | final ApplicationInfo info = pmUser.getApplicationInfo(pkg, |
| 166 | PackageManager.MATCH_UNINSTALLED_PACKAGES |
| 167 | | PackageManager.MATCH_DISABLED_COMPONENTS); |
| 168 | if (info != null) { |
| 169 | appname = String.valueOf(pmUser.getApplicationLabel(info)); |
| 170 | } |
| 171 | } catch (PackageManager.NameNotFoundException e) { |
| 172 | // Do nothing |
| 173 | } |
| 174 | row.setAppName(appname); |
| 175 | row.setOnDismissRunnable(onDismissRunnable); |
| 176 | row.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); |
| 177 | if (ENABLE_REMOTE_INPUT) { |
| 178 | row.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS); |
| 179 | } |
| 180 | |
| 181 | row.setAppOpsOnClickListener(mOnAppOpsClickListener); |
| 182 | |
| 183 | mBindRowCallback.onBindRow(entry, pmUser, sbn, row); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Updates the views bound to an entry when the entry's ranking changes, either in-place or by |
| 188 | * reinflating them. |
| 189 | */ |
Ned Burns | c586467 | 2019-02-20 12:57:29 -0500 | [diff] [blame] | 190 | @Override |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 191 | public void onNotificationRankingUpdated( |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 192 | NotificationEntry entry, |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 193 | @Nullable Integer oldImportance, |
| 194 | NotificationUiAdjustment oldAdjustment, |
Ned Burns | f529ec2 | 2019-02-12 19:33:50 -0500 | [diff] [blame] | 195 | NotificationUiAdjustment newAdjustment) { |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 196 | if (NotificationUiAdjustment.needReinflate(oldAdjustment, newAdjustment)) { |
| 197 | if (entry.rowExists()) { |
| 198 | entry.reset(); |
| 199 | PackageManager pmUser = StatusBar.getPackageManagerForUser( |
| 200 | mContext, |
| 201 | entry.notification.getUser().getIdentifier()); |
Ned Burns | f529ec2 | 2019-02-12 19:33:50 -0500 | [diff] [blame] | 202 | updateNotification(entry, pmUser, entry.notification, entry.getRow()); |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 203 | } else { |
| 204 | // Once the RowInflaterTask is done, it will pick up the updated entry, so |
| 205 | // no-op here. |
| 206 | } |
| 207 | } else { |
| 208 | if (oldImportance != null && entry.importance != oldImportance) { |
| 209 | if (entry.rowExists()) { |
| 210 | entry.getRow().onNotificationRankingUpdated(); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | //TODO: This method associates a row with an entry, but eventually needs to not do that |
| 217 | private void updateNotification( |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 218 | NotificationEntry entry, |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 219 | PackageManager pmUser, |
| 220 | StatusBarNotification sbn, |
Ned Burns | f529ec2 | 2019-02-12 19:33:50 -0500 | [diff] [blame] | 221 | ExpandableNotificationRow row) { |
Ned Burns | a13b6f1 | 2019-02-11 20:42:58 -0500 | [diff] [blame] | 222 | row.setIsLowPriority(entry.ambient); |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 223 | |
| 224 | // Extract target SDK version. |
| 225 | try { |
| 226 | ApplicationInfo info = pmUser.getApplicationInfo(sbn.getPackageName(), 0); |
| 227 | entry.targetSdk = info.targetSdkVersion; |
| 228 | } catch (PackageManager.NameNotFoundException ex) { |
| 229 | Log.e(TAG, "Failed looking up ApplicationInfo for " + sbn.getPackageName(), ex); |
| 230 | } |
| 231 | row.setLegacy(entry.targetSdk >= Build.VERSION_CODES.GINGERBREAD |
| 232 | && entry.targetSdk < Build.VERSION_CODES.LOLLIPOP); |
| 233 | |
| 234 | // TODO: should updates to the entry be happening somewhere else? |
| 235 | entry.setIconTag(R.id.icon_is_pre_L, entry.targetSdk < Build.VERSION_CODES.LOLLIPOP); |
| 236 | entry.autoRedacted = entry.notification.getNotification().publicVersion == null; |
| 237 | |
| 238 | entry.setRow(row); |
| 239 | row.setOnActivatedListener(mPresenter); |
| 240 | |
| 241 | boolean useIncreasedCollapsedHeight = |
| 242 | mMessagingUtil.isImportantMessaging(sbn, entry.importance); |
| 243 | boolean useIncreasedHeadsUp = useIncreasedCollapsedHeight |
| 244 | && !mPresenter.isPresenterFullyCollapsed(); |
| 245 | row.setUseIncreasedCollapsedHeight(useIncreasedCollapsedHeight); |
| 246 | row.setUseIncreasedHeadsUpHeight(useIncreasedHeadsUp); |
| 247 | row.setEntry(entry); |
| 248 | |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 249 | if (mNotificationInterruptionStateProvider.shouldHeadsUp(entry)) { |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 250 | row.updateInflationFlag(FLAG_CONTENT_VIEW_HEADS_UP, true /* shouldInflate */); |
| 251 | } |
Gus Prevas | ec9e1f0 | 2018-12-18 15:28:12 -0500 | [diff] [blame] | 252 | if (mNotificationInterruptionStateProvider.shouldPulse(entry)) { |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 253 | row.updateInflationFlag(FLAG_CONTENT_VIEW_AMBIENT, true /* shouldInflate */); |
| 254 | } |
| 255 | row.setNeedsRedaction( |
| 256 | Dependency.get(NotificationLockscreenUserManager.class).needsRedaction(entry)); |
| 257 | row.inflateViews(); |
Mark Renouf | fec45da | 2019-03-13 13:24:27 -0400 | [diff] [blame] | 258 | |
| 259 | // bind the click event to the content area |
| 260 | checkNotNull(mNotificationClicker).register(row, sbn); |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 261 | } |
| 262 | |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 263 | private void logNotificationExpansion(String key, boolean userAction, boolean expanded) { |
Ned Burns | c586467 | 2019-02-20 12:57:29 -0500 | [diff] [blame] | 264 | mNotificationLogger.onExpansionChanged(key, userAction, expanded); |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /** Callback for when a row is bound to an entry. */ |
| 268 | public interface BindRowCallback { |
| 269 | /** |
| 270 | * Called when a new notification and row is created. |
| 271 | * |
| 272 | * @param entry entry for the notification |
| 273 | * @param pmUser package manager for user |
| 274 | * @param sbn notification |
| 275 | * @param row row for the notification |
| 276 | */ |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 277 | void onBindRow(NotificationEntry entry, PackageManager pmUser, |
Gus Prevas | 8ba88a8 | 2018-12-18 11:13:44 -0500 | [diff] [blame] | 278 | StatusBarNotification sbn, ExpandableNotificationRow row); |
| 279 | } |
| 280 | } |