Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +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 | package com.android.systemui.statusbar; |
| 17 | |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 18 | import android.app.INotificationManager; |
| 19 | import android.app.NotificationChannel; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.pm.PackageManager; |
| 23 | import android.content.res.Resources; |
| 24 | import android.os.RemoteException; |
| 25 | import android.os.ServiceManager; |
| 26 | import android.os.UserHandle; |
| 27 | import android.provider.Settings; |
| 28 | import android.service.notification.StatusBarNotification; |
| 29 | import android.util.ArraySet; |
| 30 | import android.util.Log; |
| 31 | import android.view.HapticFeedbackConstants; |
| 32 | import android.view.View; |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 33 | import android.view.accessibility.AccessibilityManager; |
| 34 | |
| 35 | import com.android.internal.logging.MetricsLogger; |
| 36 | import com.android.internal.logging.nano.MetricsProto; |
| 37 | import com.android.systemui.Dependency; |
| 38 | import com.android.systemui.Dumpable; |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 39 | import com.android.systemui.plugins.statusbar.NotificationMenuRowPlugin; |
| 40 | import com.android.systemui.statusbar.phone.StatusBar; |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 41 | |
| 42 | import java.io.FileDescriptor; |
| 43 | import java.io.PrintWriter; |
| 44 | import java.util.ArrayList; |
| 45 | import java.util.Collections; |
| 46 | import java.util.HashSet; |
| 47 | import java.util.List; |
| 48 | import java.util.Set; |
| 49 | |
| 50 | /** |
| 51 | * Handles various NotificationGuts related tasks, such as binding guts to a row, opening and |
| 52 | * closing guts, and keeping track of the currently exposed notification guts. |
| 53 | */ |
| 54 | public class NotificationGutsManager implements Dumpable { |
| 55 | private static final String TAG = "NotificationGutsManager"; |
| 56 | |
| 57 | // Must match constant in Settings. Used to highlight preferences when linking to Settings. |
| 58 | private static final String EXTRA_FRAGMENT_ARG_KEY = ":settings:fragment_args_key"; |
| 59 | |
| 60 | private final MetricsLogger mMetricsLogger = Dependency.get(MetricsLogger.class); |
| 61 | private final Set<String> mNonBlockablePkgs; |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 62 | private final Context mContext; |
| 63 | private final AccessibilityManager mAccessibilityManager; |
Eliot Courtney | 6c313d3 | 2017-12-14 19:57:51 +0900 | [diff] [blame] | 64 | |
| 65 | // Dependencies: |
| 66 | private final NotificationLockscreenUserManager mLockscreenUserManager = |
| 67 | Dependency.get(NotificationLockscreenUserManager.class); |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 68 | |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 69 | // which notification is currently being longpress-examined by the user |
| 70 | private NotificationGuts mNotificationGutsExposed; |
| 71 | private NotificationMenuRowPlugin.MenuItem mGutsMenuItem; |
Eliot Courtney | 4a96b36 | 2017-12-14 19:38:52 +0900 | [diff] [blame] | 72 | protected NotificationPresenter mPresenter; |
| 73 | protected NotificationEntryManager mEntryManager; |
Eliot Courtney | 2b4c3a0 | 2017-11-27 13:27:46 +0900 | [diff] [blame] | 74 | private NotificationListContainer mListContainer; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 75 | private NotificationInfo.CheckSaveListener mCheckSaveListener; |
| 76 | private OnSettingsClickListener mOnSettingsClickListener; |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 77 | private String mKeyToRemoveOnGutsClosed; |
| 78 | |
Eliot Courtney | 6c313d3 | 2017-12-14 19:57:51 +0900 | [diff] [blame] | 79 | public NotificationGutsManager(Context context) { |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 80 | mContext = context; |
| 81 | Resources res = context.getResources(); |
| 82 | |
| 83 | mNonBlockablePkgs = new HashSet<>(); |
| 84 | Collections.addAll(mNonBlockablePkgs, res.getStringArray( |
| 85 | com.android.internal.R.array.config_nonBlockableNotificationPackages)); |
| 86 | |
| 87 | mAccessibilityManager = (AccessibilityManager) |
| 88 | mContext.getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 89 | } |
| 90 | |
Eliot Courtney | 4a96b36 | 2017-12-14 19:38:52 +0900 | [diff] [blame] | 91 | public void setUpWithPresenter(NotificationPresenter presenter, |
| 92 | NotificationEntryManager entryManager, NotificationListContainer listContainer, |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 93 | NotificationInfo.CheckSaveListener checkSaveListener, |
| 94 | OnSettingsClickListener onSettingsClickListener) { |
| 95 | mPresenter = presenter; |
Eliot Courtney | 4a96b36 | 2017-12-14 19:38:52 +0900 | [diff] [blame] | 96 | mEntryManager = entryManager; |
Eliot Courtney | 2b4c3a0 | 2017-11-27 13:27:46 +0900 | [diff] [blame] | 97 | mListContainer = listContainer; |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 98 | mCheckSaveListener = checkSaveListener; |
| 99 | mOnSettingsClickListener = onSettingsClickListener; |
| 100 | } |
| 101 | |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 102 | public String getKeyToRemoveOnGutsClosed() { |
| 103 | return mKeyToRemoveOnGutsClosed; |
| 104 | } |
| 105 | |
| 106 | public void setKeyToRemoveOnGutsClosed(String keyToRemoveOnGutsClosed) { |
| 107 | mKeyToRemoveOnGutsClosed = keyToRemoveOnGutsClosed; |
| 108 | } |
| 109 | |
yoshiki iguchi | a85c2a0 | 2018-01-12 11:28:06 +0900 | [diff] [blame] | 110 | public void onDensityOrFontScaleChanged(ExpandableNotificationRow row) { |
| 111 | setExposedGuts(row.getGuts()); |
| 112 | bindGuts(row); |
| 113 | } |
| 114 | |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 115 | private void saveAndCloseNotificationMenu( |
| 116 | ExpandableNotificationRow row, NotificationGuts guts, View done) { |
| 117 | guts.resetFalsingCheck(); |
| 118 | int[] rowLocation = new int[2]; |
| 119 | int[] doneLocation = new int[2]; |
| 120 | row.getLocationOnScreen(rowLocation); |
| 121 | done.getLocationOnScreen(doneLocation); |
| 122 | |
| 123 | final int centerX = done.getWidth() / 2; |
| 124 | final int centerY = done.getHeight() / 2; |
| 125 | final int x = doneLocation[0] - rowLocation[0] + centerX; |
| 126 | final int y = doneLocation[1] - rowLocation[1] + centerY; |
| 127 | closeAndSaveGuts(false /* removeLeavebehind */, false /* force */, |
| 128 | true /* removeControls */, x, y, true /* resetMenu */); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Sends an intent to open the notification settings for a particular package and optional |
| 133 | * channel. |
| 134 | */ |
| 135 | private void startAppNotificationSettingsActivity(String packageName, final int appUid, |
| 136 | final NotificationChannel channel) { |
| 137 | final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS); |
| 138 | intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName); |
| 139 | intent.putExtra(Settings.EXTRA_APP_UID, appUid); |
| 140 | if (channel != null) { |
| 141 | intent.putExtra(EXTRA_FRAGMENT_ARG_KEY, channel.getId()); |
| 142 | } |
| 143 | mPresenter.startNotificationGutsIntent(intent, appUid); |
| 144 | } |
| 145 | |
| 146 | public void bindGuts(final ExpandableNotificationRow row) { |
| 147 | bindGuts(row, mGutsMenuItem); |
| 148 | } |
| 149 | |
| 150 | private void bindGuts(final ExpandableNotificationRow row, |
| 151 | NotificationMenuRowPlugin.MenuItem item) { |
| 152 | row.inflateGuts(); |
| 153 | row.setGutsView(item); |
| 154 | final StatusBarNotification sbn = row.getStatusBarNotification(); |
| 155 | row.setTag(sbn.getPackageName()); |
| 156 | final NotificationGuts guts = row.getGuts(); |
| 157 | guts.setClosedListener((NotificationGuts g) -> { |
| 158 | if (!g.willBeRemoved() && !row.isRemoved()) { |
Eliot Courtney | 2b4c3a0 | 2017-11-27 13:27:46 +0900 | [diff] [blame] | 159 | mListContainer.onHeightChanged( |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 160 | row, !mPresenter.isPresenterFullyCollapsed() /* needsAnimation */); |
| 161 | } |
| 162 | if (mNotificationGutsExposed == g) { |
| 163 | mNotificationGutsExposed = null; |
| 164 | mGutsMenuItem = null; |
| 165 | } |
| 166 | String key = sbn.getKey(); |
| 167 | if (key.equals(mKeyToRemoveOnGutsClosed)) { |
| 168 | mKeyToRemoveOnGutsClosed = null; |
Eliot Courtney | 4a96b36 | 2017-12-14 19:38:52 +0900 | [diff] [blame] | 169 | mEntryManager.removeNotification(key, mEntryManager.getLatestRankingMap()); |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 170 | } |
| 171 | }); |
| 172 | |
| 173 | View gutsView = item.getGutsView(); |
| 174 | if (gutsView instanceof NotificationSnooze) { |
| 175 | NotificationSnooze snoozeGuts = (NotificationSnooze) gutsView; |
Eliot Courtney | 2b4c3a0 | 2017-11-27 13:27:46 +0900 | [diff] [blame] | 176 | snoozeGuts.setSnoozeListener(mListContainer.getSwipeActionHelper()); |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 177 | snoozeGuts.setStatusBarNotification(sbn); |
| 178 | snoozeGuts.setSnoozeOptions(row.getEntry().snoozeCriteria); |
| 179 | guts.setHeightChangedListener((NotificationGuts g) -> { |
Eliot Courtney | 2b4c3a0 | 2017-11-27 13:27:46 +0900 | [diff] [blame] | 180 | mListContainer.onHeightChanged(row, row.isShown() /* needsAnimation */); |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 181 | }); |
| 182 | } |
| 183 | |
| 184 | if (gutsView instanceof NotificationInfo) { |
| 185 | final UserHandle userHandle = sbn.getUser(); |
| 186 | PackageManager pmUser = StatusBar.getPackageManagerForUser(mContext, |
| 187 | userHandle.getIdentifier()); |
| 188 | final INotificationManager iNotificationManager = INotificationManager.Stub.asInterface( |
| 189 | ServiceManager.getService(Context.NOTIFICATION_SERVICE)); |
| 190 | final String pkg = sbn.getPackageName(); |
| 191 | NotificationInfo info = (NotificationInfo) gutsView; |
| 192 | // Settings link is only valid for notifications that specify a user, unless this is the |
| 193 | // system user. |
| 194 | NotificationInfo.OnSettingsClickListener onSettingsClick = null; |
| 195 | if (!userHandle.equals(UserHandle.ALL) |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 196 | || mLockscreenUserManager.getCurrentUserId() == UserHandle.USER_SYSTEM) { |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 197 | onSettingsClick = (View v, NotificationChannel channel, int appUid) -> { |
| 198 | mMetricsLogger.action(MetricsProto.MetricsEvent.ACTION_NOTE_INFO); |
| 199 | guts.resetFalsingCheck(); |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 200 | mOnSettingsClickListener.onClick(sbn.getKey()); |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 201 | startAppNotificationSettingsActivity(pkg, appUid, channel); |
| 202 | }; |
| 203 | } |
| 204 | final NotificationInfo.OnAppSettingsClickListener onAppSettingsClick = (View v, |
| 205 | Intent intent) -> { |
| 206 | mMetricsLogger.action(MetricsProto.MetricsEvent.ACTION_APP_NOTE_SETTINGS); |
| 207 | guts.resetFalsingCheck(); |
| 208 | mPresenter.startNotificationGutsIntent(intent, sbn.getUid()); |
| 209 | }; |
| 210 | final View.OnClickListener onDoneClick = (View v) -> { |
| 211 | saveAndCloseNotificationMenu(row, guts, v); |
| 212 | }; |
| 213 | |
| 214 | ArraySet<NotificationChannel> channels = new ArraySet<>(); |
| 215 | channels.add(row.getEntry().channel); |
| 216 | if (row.isSummaryWithChildren()) { |
| 217 | // If this is a summary, then add in the children notification channels for the |
| 218 | // same user and pkg. |
| 219 | final List<ExpandableNotificationRow> childrenRows = row.getNotificationChildren(); |
| 220 | final int numChildren = childrenRows.size(); |
| 221 | for (int i = 0; i < numChildren; i++) { |
| 222 | final ExpandableNotificationRow childRow = childrenRows.get(i); |
| 223 | final NotificationChannel childChannel = childRow.getEntry().channel; |
| 224 | final StatusBarNotification childSbn = childRow.getStatusBarNotification(); |
| 225 | if (childSbn.getUser().equals(userHandle) && |
| 226 | childSbn.getPackageName().equals(pkg)) { |
| 227 | channels.add(childChannel); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | try { |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 232 | info.bindNotification(pmUser, iNotificationManager, pkg, row.getEntry().channel, |
| 233 | channels.size(), sbn, mCheckSaveListener, onSettingsClick, |
| 234 | onAppSettingsClick, mNonBlockablePkgs); |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 235 | } catch (RemoteException e) { |
| 236 | Log.e(TAG, e.toString()); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Closes guts or notification menus that might be visible and saves any changes. |
| 243 | * |
| 244 | * @param removeLeavebehinds true if leavebehinds (e.g. snooze) should be closed. |
| 245 | * @param force true if guts should be closed regardless of state (used for snooze only). |
| 246 | * @param removeControls true if controls (e.g. info) should be closed. |
| 247 | * @param x if closed based on touch location, this is the x touch location. |
| 248 | * @param y if closed based on touch location, this is the y touch location. |
| 249 | * @param resetMenu if any notification menus that might be revealed should be closed. |
| 250 | */ |
| 251 | public void closeAndSaveGuts(boolean removeLeavebehinds, boolean force, boolean removeControls, |
| 252 | int x, int y, boolean resetMenu) { |
| 253 | if (mNotificationGutsExposed != null) { |
| 254 | mNotificationGutsExposed.closeControls(removeLeavebehinds, removeControls, x, y, force); |
| 255 | } |
| 256 | if (resetMenu) { |
Eliot Courtney | 2b4c3a0 | 2017-11-27 13:27:46 +0900 | [diff] [blame] | 257 | mListContainer.resetExposedMenuView(false /* animate */, true /* force */); |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Returns the exposed NotificationGuts or null if none are exposed. |
| 263 | */ |
| 264 | public NotificationGuts getExposedGuts() { |
| 265 | return mNotificationGutsExposed; |
| 266 | } |
| 267 | |
| 268 | public void setExposedGuts(NotificationGuts guts) { |
| 269 | mNotificationGutsExposed = guts; |
| 270 | } |
| 271 | |
| 272 | /** |
yoshiki iguchi | a85c2a0 | 2018-01-12 11:28:06 +0900 | [diff] [blame] | 273 | * Opens guts on the given ExpandableNotificationRow |v|. |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 274 | * |
| 275 | * @param v ExpandableNotificationRow to open guts on |
| 276 | * @param x x coordinate of origin of circular reveal |
| 277 | * @param y y coordinate of origin of circular reveal |
| 278 | * @param item MenuItem the guts should display |
| 279 | * @return true if guts was opened |
| 280 | */ |
| 281 | public boolean openGuts(View v, int x, int y, |
| 282 | NotificationMenuRowPlugin.MenuItem item) { |
| 283 | if (!(v instanceof ExpandableNotificationRow)) { |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | if (v.getWindowToken() == null) { |
| 288 | Log.e(TAG, "Trying to show notification guts, but not attached to window"); |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | final ExpandableNotificationRow row = (ExpandableNotificationRow) v; |
| 293 | if (row.isDark()) { |
| 294 | return false; |
| 295 | } |
| 296 | v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); |
| 297 | if (row.areGutsExposed()) { |
| 298 | closeAndSaveGuts(false /* removeLeavebehind */, false /* force */, |
| 299 | true /* removeControls */, -1 /* x */, -1 /* y */, |
| 300 | true /* resetMenu */); |
| 301 | return false; |
| 302 | } |
| 303 | bindGuts(row, item); |
| 304 | NotificationGuts guts = row.getGuts(); |
| 305 | |
| 306 | // Assume we are a status_bar_notification_row |
| 307 | if (guts == null) { |
| 308 | // This view has no guts. Examples are the more card or the dismiss all view |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | mMetricsLogger.action(MetricsProto.MetricsEvent.ACTION_NOTE_CONTROLS); |
| 313 | |
| 314 | // ensure that it's laid but not visible until actually laid out |
| 315 | guts.setVisibility(View.INVISIBLE); |
| 316 | // Post to ensure the the guts are properly laid out. |
| 317 | guts.post(new Runnable() { |
| 318 | @Override |
| 319 | public void run() { |
| 320 | if (row.getWindowToken() == null) { |
| 321 | Log.e(TAG, "Trying to show notification guts, but not attached to " |
| 322 | + "window"); |
| 323 | return; |
| 324 | } |
| 325 | closeAndSaveGuts(true /* removeLeavebehind */, true /* force */, |
| 326 | true /* removeControls */, -1 /* x */, -1 /* y */, |
| 327 | false /* resetMenu */); |
| 328 | guts.setVisibility(View.VISIBLE); |
yoshiki iguchi | a85c2a0 | 2018-01-12 11:28:06 +0900 | [diff] [blame] | 329 | |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 330 | final boolean needsFalsingProtection = |
| 331 | (mPresenter.isPresenterLocked() && |
| 332 | !mAccessibilityManager.isTouchExplorationEnabled()); |
yoshiki iguchi | a85c2a0 | 2018-01-12 11:28:06 +0900 | [diff] [blame] | 333 | guts.openControls(x, y, needsFalsingProtection, () -> { |
| 334 | // Move the notification view back over the menu |
| 335 | row.resetTranslation(); |
| 336 | }); |
| 337 | |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 338 | row.closeRemoteInput(); |
Eliot Courtney | 2b4c3a0 | 2017-11-27 13:27:46 +0900 | [diff] [blame] | 339 | mListContainer.onHeightChanged(row, true /* needsAnimation */); |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 340 | mNotificationGutsExposed = guts; |
| 341 | mGutsMenuItem = item; |
| 342 | } |
| 343 | }); |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Eliot Courtney | 0932228 | 2017-11-09 15:31:19 +0900 | [diff] [blame] | 349 | pw.println("NotificationGutsManager state:"); |
| 350 | pw.print(" mKeyToRemoveOnGutsClosed: "); |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 351 | pw.println(mKeyToRemoveOnGutsClosed); |
| 352 | } |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 353 | |
| 354 | public interface OnSettingsClickListener { |
| 355 | void onClick(String key); |
| 356 | } |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 357 | } |