blob: 7c6c556b5241b0f88730405ee41cbc195629180c [file] [log] [blame]
Mady Mellor87d79452017-01-10 11:52:52 -08001/*
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
Rohan Shah20790b82018-07-02 17:21:04 -070017package com.android.systemui.statusbar.notification.row;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050018
Gus Prevas9abc5062018-10-31 16:11:04 -040019import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
Gus Prevas9abc5062018-10-31 16:11:04 -040020import static android.app.NotificationManager.IMPORTANCE_LOW;
Julia Reynolds8728d002019-04-19 15:46:13 -040021import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
Julia Reynolds3aedded2017-03-31 14:42:09 -040022
Ned Burns369f1472019-05-22 21:07:26 -040023import static com.android.systemui.Interpolators.FAST_OUT_SLOW_IN;
24
25import static java.lang.annotation.RetentionPolicy.SOURCE;
26
Julia Reynolds437cdb12018-01-03 12:27:24 -050027import android.animation.Animator;
28import android.animation.AnimatorListenerAdapter;
29import android.animation.AnimatorSet;
30import android.animation.ObjectAnimator;
Gus Prevas9abc5062018-10-31 16:11:04 -040031import android.annotation.IntDef;
Rohan Shahca0447e2018-03-30 15:18:27 -070032import android.annotation.Nullable;
Mady Mellor87d79452017-01-10 11:52:52 -080033import android.app.INotificationManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040034import android.app.Notification;
Mady Mellor87d79452017-01-10 11:52:52 -080035import android.app.NotificationChannel;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050036import android.app.NotificationChannelGroup;
Mady Mellor87d79452017-01-10 11:52:52 -080037import android.content.Context;
Julia Reynolds3aedded2017-03-31 14:42:09 -040038import android.content.Intent;
39import android.content.pm.ActivityInfo;
Mady Mellor87d79452017-01-10 11:52:52 -080040import android.content.pm.ApplicationInfo;
Mady Mellor87d79452017-01-10 11:52:52 -080041import android.content.pm.PackageManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040042import android.content.pm.ResolveInfo;
Mady Mellor87d79452017-01-10 11:52:52 -080043import android.graphics.drawable.Drawable;
William Brockman75cf66a2018-12-21 13:25:01 -050044import android.metrics.LogMaker;
Rohan Shahca0447e2018-03-30 15:18:27 -070045import android.os.Handler;
Mady Mellor87d79452017-01-10 11:52:52 -080046import android.os.RemoteException;
Julia Reynolds3aedded2017-03-31 14:42:09 -040047import android.service.notification.StatusBarNotification;
48import android.text.TextUtils;
Ned Burns369f1472019-05-22 21:07:26 -040049import android.transition.ChangeBounds;
50import android.transition.Fade;
Julia Reynolds4d1dd792019-04-29 11:48:29 -040051import android.transition.TransitionManager;
Ned Burns369f1472019-05-22 21:07:26 -040052import android.transition.TransitionSet;
Mady Mellor87d79452017-01-10 11:52:52 -080053import android.util.AttributeSet;
Rohan Shahca0447e2018-03-30 15:18:27 -070054import android.util.Log;
Mady Mellor87d79452017-01-10 11:52:52 -080055import android.view.View;
Julia Reynoldse0341482018-03-08 14:42:50 -050056import android.view.ViewGroup;
Geoffrey Pitschd94e7882017-04-06 09:52:11 -040057import android.view.accessibility.AccessibilityEvent;
Mady Mellor87d79452017-01-10 11:52:52 -080058import android.widget.ImageView;
59import android.widget.LinearLayout;
Mady Mellor87d79452017-01-10 11:52:52 -080060import android.widget.TextView;
61
Rohan Shah524cf7b2018-03-15 14:40:02 -070062import com.android.internal.annotations.VisibleForTesting;
Mady Mellor87d79452017-01-10 11:52:52 -080063import com.android.internal.logging.MetricsLogger;
64import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Rohan Shah524cf7b2018-03-15 14:40:02 -070065import com.android.systemui.Dependency;
Julia Reynolds437cdb12018-01-03 12:27:24 -050066import com.android.systemui.Interpolators;
Mady Mellor87d79452017-01-10 11:52:52 -080067import com.android.systemui.R;
Ned Burns9512e0c2019-05-30 19:36:04 -040068import com.android.systemui.statusbar.notification.VisualStabilityManager;
Rohan Shah20790b82018-07-02 17:21:04 -070069import com.android.systemui.statusbar.notification.logging.NotificationCounters;
Mady Mellor87d79452017-01-10 11:52:52 -080070
Ned Burns369f1472019-05-22 21:07:26 -040071import java.lang.annotation.Retention;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050072import java.util.List;
Evan Laird47dc4542019-04-24 15:10:52 -040073import java.util.Set;
Mady Mellor87d79452017-01-10 11:52:52 -080074
75/**
Rohan Shahda5dcdd2018-04-27 17:21:50 -070076 * The guts of a notification revealed when performing a long press. This also houses the blocking
77 * helper affordance that allows a user to keep/stop notifications after swiping one away.
Mady Mellor87d79452017-01-10 11:52:52 -080078 */
Mady Mellor95d743c2017-01-10 12:05:27 -080079public class NotificationInfo extends LinearLayout implements NotificationGuts.GutsContent {
Mady Mellor87d79452017-01-10 11:52:52 -080080 private static final String TAG = "InfoGuts";
81
Gus Prevas894d9152018-11-12 13:51:40 -050082 @IntDef(prefix = { "ACTION_" }, value = {
83 ACTION_NONE,
84 ACTION_UNDO,
85 ACTION_TOGGLE_SILENT,
86 ACTION_BLOCK,
Gus Prevas9abc5062018-10-31 16:11:04 -040087 })
Gus Prevas894d9152018-11-12 13:51:40 -050088 public @interface NotificationInfoAction {
89 }
Gus Prevas9abc5062018-10-31 16:11:04 -040090
Gus Prevas894d9152018-11-12 13:51:40 -050091 public static final int ACTION_NONE = 0;
Nadia Benbernoub66516c2019-03-06 11:01:53 -050092 static final int ACTION_UNDO = 1;
Julia Reynolds52a27372019-04-03 11:00:17 -040093 // standard controls
Nadia Benbernoub66516c2019-03-06 11:01:53 -050094 static final int ACTION_TOGGLE_SILENT = 2;
Julia Reynolds52a27372019-04-03 11:00:17 -040095 // unused
Nadia Benbernoub66516c2019-03-06 11:01:53 -050096 static final int ACTION_BLOCK = 3;
Julia Reynolds52a27372019-04-03 11:00:17 -040097 // blocking helper
Nadia Benbernoub66516c2019-03-06 11:01:53 -050098 static final int ACTION_DELIVER_SILENTLY = 4;
Julia Reynolds52a27372019-04-03 11:00:17 -040099 // standard controls
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500100 private static final int ACTION_ALERT = 5;
Gus Prevas9abc5062018-10-31 16:11:04 -0400101
Ned Burns369f1472019-05-22 21:07:26 -0400102 private TextView mPriorityDescriptionView;
103 private TextView mSilentDescriptionView;
Dan Sandler0ed02292019-05-14 19:18:35 -0400104
Mady Mellor87d79452017-01-10 11:52:52 -0800105 private INotificationManager mINotificationManager;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500106 private PackageManager mPm;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700107 private MetricsLogger mMetricsLogger;
Ned Burns9512e0c2019-05-30 19:36:04 -0400108 private VisualStabilityManager mVisualStabilityManager;
Evan Laird47dc4542019-04-24 15:10:52 -0400109 private ChannelEditorDialogController mChannelEditorDialogController;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500110
Rohan Shahca0447e2018-03-30 15:18:27 -0700111 private String mPackageName;
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400112 private String mAppName;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500113 private int mAppUid;
Julia Reynolds268647a2018-10-25 16:54:27 -0400114 private String mDelegatePkg;
Rohan Shahca0447e2018-03-30 15:18:27 -0700115 private int mNumUniqueChannelsInRow;
Evan Laird47dc4542019-04-24 15:10:52 -0400116 private Set<NotificationChannel> mUniqueChannelsInRow;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500117 private NotificationChannel mSingleNotificationChannel;
Gus Prevas9abc5062018-10-31 16:11:04 -0400118 private int mStartingChannelImportance;
Gus Prevascaed15c2019-01-18 14:19:51 -0500119 private boolean mWasShownHighPriority;
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400120 private boolean mPressedApply;
121
Gus Prevascaed15c2019-01-18 14:19:51 -0500122 /**
123 * The last importance level chosen by the user. Null if the user has not chosen an importance
124 * level; non-null once the user takes an action which indicates an explicit preference.
125 */
126 @Nullable private Integer mChosenImportance;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500127 private boolean mIsSingleDefaultChannel;
Rohan Shah63411fc2018-03-28 19:05:52 -0700128 private boolean mIsNonblockable;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500129 private StatusBarNotification mSbn;
130 private AnimatorSet mExpandAnimation;
Julia Reynolds35765d82018-08-17 11:39:19 -0400131 private boolean mIsDeviceProvisioned;
Mady Mellor87d79452017-01-10 11:52:52 -0800132
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400133 private CheckSaveListener mCheckSaveListener;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500134 private OnSettingsClickListener mOnSettingsClickListener;
Julia Reynolds3aedded2017-03-31 14:42:09 -0400135 private OnAppSettingsClickListener mAppSettingsClickListener;
Mady Mellor95d743c2017-01-10 12:05:27 -0800136 private NotificationGuts mGutsContainer;
Evan Laird47dc4542019-04-24 15:10:52 -0400137 private Drawable mPkgIcon;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700138
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700139 /** Whether this view is being shown as part of the blocking helper. */
Rohan Shah524cf7b2018-03-15 14:40:02 -0700140 private boolean mIsForBlockingHelper;
Mady Mellor87d79452017-01-10 11:52:52 -0800141
Rohan Shahdd588c72018-05-09 20:32:15 -0700142 /**
143 * String that describes how the user exit or quit out of this view, also used as a counter tag.
144 */
145 private String mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700146
Julia Reynolds52a27372019-04-03 11:00:17 -0400147 // used by standard ui
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500148 private OnClickListener mOnAlert = v -> {
149 mExitReason = NotificationCounters.BLOCKING_HELPER_KEEP_SHOWING;
150 mChosenImportance = IMPORTANCE_DEFAULT;
Ned Burns369f1472019-05-22 21:07:26 -0400151 applyAlertingBehavior(BEHAVIOR_ALERTING, true /* userTriggered */);
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500152 };
153
Julia Reynolds52a27372019-04-03 11:00:17 -0400154 // used by standard ui
155 private OnClickListener mOnSilent = v -> {
156 mExitReason = NotificationCounters.BLOCKING_HELPER_DELIVER_SILENTLY;
157 mChosenImportance = IMPORTANCE_LOW;
Ned Burns369f1472019-05-22 21:07:26 -0400158 applyAlertingBehavior(BEHAVIOR_SILENT, true /* userTriggered */);
Julia Reynolds52a27372019-04-03 11:00:17 -0400159 };
160
161 // used by standard ui
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500162 private OnClickListener mOnDismissSettings = v -> {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400163 mPressedApply = true;
Evan Laird7956fe82019-05-30 17:14:57 -0400164 closeControls(v, true);
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500165 };
166
Julia Reynolds52a27372019-04-03 11:00:17 -0400167 // used by blocking helper
168 private OnClickListener mOnKeepShowing = v -> {
169 mExitReason = NotificationCounters.BLOCKING_HELPER_KEEP_SHOWING;
Evan Laird7956fe82019-05-30 17:14:57 -0400170 closeControls(v, true);
Julia Reynolds52a27372019-04-03 11:00:17 -0400171 mMetricsLogger.write(getLogMaker().setCategory(
172 MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
173 .setType(MetricsEvent.TYPE_ACTION)
174 .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_STAY_SILENT));
175 };
176
177 // used by blocking helper
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500178 private OnClickListener mOnDeliverSilently = v -> {
179 handleSaveImportance(
180 ACTION_DELIVER_SILENTLY, MetricsEvent.BLOCKING_HELPER_CLICK_STAY_SILENT);
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500181 };
182
183 private void handleSaveImportance(int action, int metricsSubtype) {
Gus Prevas533836a2018-09-24 17:15:32 -0400184 Runnable saveImportance = () -> {
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500185 saveImportanceAndExitReason(action);
Will Brockmana399f022019-01-30 10:59:36 -0500186 if (mIsForBlockingHelper) {
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500187 swapContent(action, true /* animate */);
Will Brockmana399f022019-01-30 10:59:36 -0500188 mMetricsLogger.write(getLogMaker()
189 .setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
190 .setType(MetricsEvent.TYPE_ACTION)
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500191 .setSubtype(metricsSubtype));
Will Brockmana399f022019-01-30 10:59:36 -0500192 }
Gus Prevas533836a2018-09-24 17:15:32 -0400193 };
194 if (mCheckSaveListener != null) {
195 mCheckSaveListener.checkSave(saveImportance, mSbn);
196 } else {
197 saveImportance.run();
198 }
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500199 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500200
201 private OnClickListener mOnUndo = v -> {
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700202 // Reset exit counter that we'll log and record an undo event separately (not an exit event)
Rohan Shahdd588c72018-05-09 20:32:15 -0700203 mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED;
Will Brockmana399f022019-01-30 10:59:36 -0500204 if (mIsForBlockingHelper) {
205 logBlockingHelperCounter(NotificationCounters.BLOCKING_HELPER_UNDO);
206 mMetricsLogger.write(getLogMaker().setCategory(
207 MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
208 .setType(MetricsEvent.TYPE_DISMISS)
209 .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_UNDO));
210 } else {
Julia Reynolds52a27372019-04-03 11:00:17 -0400211 // TODO: this can't happen?
Will Brockmana399f022019-01-30 10:59:36 -0500212 mMetricsLogger.write(importanceChangeLogMaker().setType(MetricsEvent.TYPE_DISMISS));
213 }
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500214 saveImportanceAndExitReason(ACTION_UNDO);
Gus Prevas894d9152018-11-12 13:51:40 -0500215 swapContent(ACTION_UNDO, true /* animate */);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500216 };
217
Mady Mellor87d79452017-01-10 11:52:52 -0800218 public NotificationInfo(Context context, AttributeSet attrs) {
219 super(context, attrs);
220 }
221
Ned Burns369f1472019-05-22 21:07:26 -0400222 @Override
223 protected void onFinishInflate() {
224 super.onFinishInflate();
225
226 mPriorityDescriptionView = findViewById(R.id.alert_summary);
227 mSilentDescriptionView = findViewById(R.id.silence_summary);
228 }
229
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400230 // Specify a CheckSaveListener to override when/if the user's changes are committed.
231 public interface CheckSaveListener {
232 // Invoked when importance has changed and the NotificationInfo wants to try to save it.
233 // Listener should run saveImportance unless the change should be canceled.
Eliot Courtney47098cb2017-10-18 17:30:30 +0900234 void checkSave(Runnable saveImportance, StatusBarNotification sbn);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400235 }
236
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500237 public interface OnSettingsClickListener {
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500238 void onClick(View v, NotificationChannel channel, int appUid);
Mady Mellor87d79452017-01-10 11:52:52 -0800239 }
240
Julia Reynolds3aedded2017-03-31 14:42:09 -0400241 public interface OnAppSettingsClickListener {
242 void onClick(View v, Intent intent);
243 }
244
Rohan Shah524cf7b2018-03-15 14:40:02 -0700245 @VisibleForTesting
246 void bindNotification(
247 final PackageManager pm,
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500248 final INotificationManager iNotificationManager,
Ned Burns9512e0c2019-05-30 19:36:04 -0400249 final VisualStabilityManager visualStabilityManager,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500250 final String pkg,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500251 final NotificationChannel notificationChannel,
Evan Laird47dc4542019-04-24 15:10:52 -0400252 final Set<NotificationChannel> uniqueChannelsInRow,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400253 final StatusBarNotification sbn,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500254 final CheckSaveListener checkSaveListener,
255 final OnSettingsClickListener onSettingsClick,
256 final OnAppSettingsClickListener onAppSettingsClick,
Julia Reynolds35765d82018-08-17 11:39:19 -0400257 boolean isDeviceProvisioned,
Gus Prevas9abc5062018-10-31 16:11:04 -0400258 boolean isNonblockable,
Gus Prevascaed15c2019-01-18 14:19:51 -0500259 int importance,
260 boolean wasShownHighPriority)
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500261 throws RemoteException {
Ned Burns9512e0c2019-05-30 19:36:04 -0400262 bindNotification(pm, iNotificationManager, visualStabilityManager, pkg, notificationChannel,
Evan Laird47dc4542019-04-24 15:10:52 -0400263 uniqueChannelsInRow, sbn, checkSaveListener, onSettingsClick,
Julia Reynolds35765d82018-08-17 11:39:19 -0400264 onAppSettingsClick, isDeviceProvisioned, isNonblockable,
Julia Reynolds52a27372019-04-03 11:00:17 -0400265 false /* isBlockingHelper */,
Gus Prevascaed15c2019-01-18 14:19:51 -0500266 importance, wasShownHighPriority);
Julia Reynolds0ef7d842018-01-24 17:50:31 -0500267 }
268
Rohan Shah524cf7b2018-03-15 14:40:02 -0700269 public void bindNotification(
270 PackageManager pm,
271 INotificationManager iNotificationManager,
Ned Burns9512e0c2019-05-30 19:36:04 -0400272 VisualStabilityManager visualStabilityManager,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700273 String pkg,
274 NotificationChannel notificationChannel,
Evan Laird47dc4542019-04-24 15:10:52 -0400275 Set<NotificationChannel> uniqueChannelsInRow,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700276 StatusBarNotification sbn,
277 CheckSaveListener checkSaveListener,
278 OnSettingsClickListener onSettingsClick,
279 OnAppSettingsClickListener onAppSettingsClick,
Julia Reynolds35765d82018-08-17 11:39:19 -0400280 boolean isDeviceProvisioned,
Rohan Shah63411fc2018-03-28 19:05:52 -0700281 boolean isNonblockable,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700282 boolean isForBlockingHelper,
Gus Prevascaed15c2019-01-18 14:19:51 -0500283 int importance,
284 boolean wasShownHighPriority)
Rohan Shah524cf7b2018-03-15 14:40:02 -0700285 throws RemoteException {
Mady Mellor87d79452017-01-10 11:52:52 -0800286 mINotificationManager = iNotificationManager;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700287 mMetricsLogger = Dependency.get(MetricsLogger.class);
Ned Burns9512e0c2019-05-30 19:36:04 -0400288 mVisualStabilityManager = visualStabilityManager;
Evan Laird47dc4542019-04-24 15:10:52 -0400289 mChannelEditorDialogController = Dependency.get(ChannelEditorDialogController.class);
Rohan Shahca0447e2018-03-30 15:18:27 -0700290 mPackageName = pkg;
Evan Laird47dc4542019-04-24 15:10:52 -0400291 mUniqueChannelsInRow = uniqueChannelsInRow;
292 mNumUniqueChannelsInRow = uniqueChannelsInRow.size();
Julia Reynolds3aedded2017-03-31 14:42:09 -0400293 mSbn = sbn;
294 mPm = pm;
295 mAppSettingsClickListener = onAppSettingsClick;
Rohan Shahca0447e2018-03-30 15:18:27 -0700296 mAppName = mPackageName;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500297 mCheckSaveListener = checkSaveListener;
298 mOnSettingsClickListener = onSettingsClick;
299 mSingleNotificationChannel = notificationChannel;
Gus Prevascaed15c2019-01-18 14:19:51 -0500300 mStartingChannelImportance = mSingleNotificationChannel.getImportance();
301 mWasShownHighPriority = wasShownHighPriority;
Rohan Shah63411fc2018-03-28 19:05:52 -0700302 mIsNonblockable = isNonblockable;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700303 mIsForBlockingHelper = isForBlockingHelper;
Rohan Shahdbd64e72018-03-28 14:46:50 -0700304 mAppUid = mSbn.getUid();
Julia Reynolds268647a2018-10-25 16:54:27 -0400305 mDelegatePkg = mSbn.getOpPkg();
Julia Reynolds35765d82018-08-17 11:39:19 -0400306 mIsDeviceProvisioned = isDeviceProvisioned;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500307
Julia Reynolds437cdb12018-01-03 12:27:24 -0500308 int numTotalChannels = mINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400309 pkg, mAppUid, false /* includeDeleted */);
Rohan Shahca0447e2018-03-30 15:18:27 -0700310 if (mNumUniqueChannelsInRow == 0) {
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400311 throw new IllegalArgumentException("bindNotification requires at least one channel");
312 } else {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500313 // Special behavior for the Default channel if no other channels have been defined.
Rohan Shahca0447e2018-03-30 15:18:27 -0700314 mIsSingleDefaultChannel = mNumUniqueChannelsInRow == 1
Rohan Shahdbd64e72018-03-28 14:46:50 -0700315 && mSingleNotificationChannel.getId().equals(
316 NotificationChannel.DEFAULT_CHANNEL_ID)
317 && numTotalChannels == 1;
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400318 }
319
Julia Reynolds437cdb12018-01-03 12:27:24 -0500320 bindHeader();
Julia Reynolds52a27372019-04-03 11:00:17 -0400321 bindChannelDetails();
322
323 if (mIsForBlockingHelper) {
324 bindBlockingHelper();
325 } else {
326 bindInlineControls();
327 }
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200328
Will Brockmana399f022019-01-30 10:59:36 -0500329 mMetricsLogger.write(notificationControlsLogMaker());
Julia Reynolds437cdb12018-01-03 12:27:24 -0500330 }
331
Julia Reynolds52a27372019-04-03 11:00:17 -0400332 private void bindBlockingHelper() {
333 findViewById(R.id.inline_controls).setVisibility(GONE);
334 findViewById(R.id.blocking_helper).setVisibility(VISIBLE);
335
336 findViewById(R.id.undo).setOnClickListener(mOnUndo);
337
338 View turnOffButton = findViewById(R.id.blocking_helper_turn_off_notifications);
339 turnOffButton.setOnClickListener(getSettingsOnClickListener());
340 turnOffButton.setVisibility(turnOffButton.hasOnClickListeners() ? VISIBLE : GONE);
341
342 TextView keepShowing = findViewById(R.id.keep_showing);
343 keepShowing.setOnClickListener(mOnKeepShowing);
344
345 View deliverSilently = findViewById(R.id.deliver_silently);
346 deliverSilently.setOnClickListener(mOnDeliverSilently);
347 }
348
349 private void bindInlineControls() {
350 findViewById(R.id.inline_controls).setVisibility(VISIBLE);
351 findViewById(R.id.blocking_helper).setVisibility(GONE);
352
353 if (mIsNonblockable) {
354 findViewById(R.id.non_configurable_text).setVisibility(VISIBLE);
355 findViewById(R.id.non_configurable_multichannel_text).setVisibility(GONE);
356 findViewById(R.id.interruptiveness_settings).setVisibility(GONE);
Julia Reynolds8728d002019-04-19 15:46:13 -0400357 ((TextView) findViewById(R.id.done)).setText(R.string.inline_done_button);
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400358 findViewById(R.id.turn_off_notifications).setVisibility(GONE);
Julia Reynolds52a27372019-04-03 11:00:17 -0400359 } else if (mNumUniqueChannelsInRow > 1) {
360 findViewById(R.id.non_configurable_text).setVisibility(GONE);
361 findViewById(R.id.interruptiveness_settings).setVisibility(GONE);
362 findViewById(R.id.non_configurable_multichannel_text).setVisibility(VISIBLE);
363 } else {
364 findViewById(R.id.non_configurable_text).setVisibility(GONE);
365 findViewById(R.id.non_configurable_multichannel_text).setVisibility(GONE);
366 findViewById(R.id.interruptiveness_settings).setVisibility(VISIBLE);
367 }
368
369 View turnOffButton = findViewById(R.id.turn_off_notifications);
Evan Laird47dc4542019-04-24 15:10:52 -0400370 turnOffButton.setOnClickListener(getTurnOffNotificationsClickListener());
Julia Reynolds52a27372019-04-03 11:00:17 -0400371 turnOffButton.setVisibility(turnOffButton.hasOnClickListeners() && !mIsNonblockable
372 ? VISIBLE : GONE);
373
374 View done = findViewById(R.id.done);
375 done.setOnClickListener(mOnDismissSettings);
376
377
Julia Reynolds8728d002019-04-19 15:46:13 -0400378 View silent = findViewById(R.id.silence);
379 View alert = findViewById(R.id.alert);
Julia Reynolds52a27372019-04-03 11:00:17 -0400380 silent.setOnClickListener(mOnSilent);
381 alert.setOnClickListener(mOnAlert);
382
Ned Burns369f1472019-05-22 21:07:26 -0400383 applyAlertingBehavior(
384 mWasShownHighPriority ? BEHAVIOR_ALERTING : BEHAVIOR_SILENT,
385 false /* userTriggered */);
Julia Reynolds52a27372019-04-03 11:00:17 -0400386 }
387
388 private void bindHeader() {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500389 // Package name
Evan Laird47dc4542019-04-24 15:10:52 -0400390 mPkgIcon = null;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500391 ApplicationInfo info;
392 try {
Rohan Shahca0447e2018-03-30 15:18:27 -0700393 info = mPm.getApplicationInfo(
394 mPackageName,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500395 PackageManager.MATCH_UNINSTALLED_PACKAGES
396 | PackageManager.MATCH_DISABLED_COMPONENTS
397 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
398 | PackageManager.MATCH_DIRECT_BOOT_AWARE);
399 if (info != null) {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500400 mAppName = String.valueOf(mPm.getApplicationLabel(info));
Evan Laird47dc4542019-04-24 15:10:52 -0400401 mPkgIcon = mPm.getApplicationIcon(info);
Geoffrey Pitschee8c81e2017-03-23 11:38:56 -0400402 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500403 } catch (PackageManager.NameNotFoundException e) {
404 // app is gone, just show package name and generic icon
Evan Laird47dc4542019-04-24 15:10:52 -0400405 mPkgIcon = mPm.getDefaultActivityIcon();
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500406 }
Evan Laird47dc4542019-04-24 15:10:52 -0400407 ((ImageView) findViewById(R.id.pkgicon)).setImageDrawable(mPkgIcon);
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400408 ((TextView) findViewById(R.id.pkgname)).setText(mAppName);
Mady Mellor87d79452017-01-10 11:52:52 -0800409
Julia Reynolds268647a2018-10-25 16:54:27 -0400410 // Delegate
411 bindDelegate();
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000412
Julia Reynolds52a27372019-04-03 11:00:17 -0400413 // Set up app settings link (i.e. Customize)
414 View settingsLinkView = findViewById(R.id.app_settings);
415 Intent settingsIntent = getAppSettingsIntent(mPm, mPackageName,
416 mSingleNotificationChannel,
417 mSbn.getId(), mSbn.getTag());
418 if (settingsIntent != null
419 && !TextUtils.isEmpty(mSbn.getNotification().getSettingsText())) {
420 settingsLinkView.setVisibility(VISIBLE);
421 settingsLinkView.setOnClickListener((View view) -> {
422 mAppSettingsClickListener.onClick(view, settingsIntent);
423 });
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000424 } else {
Julia Reynolds52a27372019-04-03 11:00:17 -0400425 settingsLinkView.setVisibility(View.GONE);
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000426 }
Julia Reynolds52a27372019-04-03 11:00:17 -0400427
428 // System Settings button.
429 final View settingsButton = findViewById(R.id.info);
430 settingsButton.setOnClickListener(getSettingsOnClickListener());
431 settingsButton.setVisibility(settingsButton.hasOnClickListeners() ? VISIBLE : GONE);
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000432 }
433
Julia Reynolds52a27372019-04-03 11:00:17 -0400434 private OnClickListener getSettingsOnClickListener() {
435 if (mAppUid >= 0 && mOnSettingsClickListener != null && mIsDeviceProvisioned) {
436 final int appUidF = mAppUid;
437 return ((View view) -> {
438 logBlockingHelperCounter(
439 NotificationCounters.BLOCKING_HELPER_NOTIF_SETTINGS);
440 mOnSettingsClickListener.onClick(view,
441 mNumUniqueChannelsInRow > 1 ? null : mSingleNotificationChannel,
442 appUidF);
443 });
444 }
445 return null;
446 }
447
Evan Laird47dc4542019-04-24 15:10:52 -0400448 private OnClickListener getTurnOffNotificationsClickListener() {
449 return ((View view) -> {
450 if (mChannelEditorDialogController != null) {
451 mChannelEditorDialogController.prepareDialogForApp(mAppName, mPackageName, mAppUid,
452 mUniqueChannelsInRow, mPkgIcon, mOnSettingsClickListener);
Evan Laird7956fe82019-05-30 17:14:57 -0400453 mChannelEditorDialogController.setOnFinishListener(
454 () -> closeControls(this, false));
Evan Laird47dc4542019-04-24 15:10:52 -0400455 mChannelEditorDialogController.show();
456 }
457 });
458 }
459
Julia Reynolds52a27372019-04-03 11:00:17 -0400460 private void bindChannelDetails() throws RemoteException {
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000461 bindName();
Julia Reynolds268647a2018-10-25 16:54:27 -0400462 bindGroup();
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000463 }
464
465 private void bindName() {
466 final TextView channelName = findViewById(R.id.channel_name);
467 if (mIsSingleDefaultChannel || mNumUniqueChannelsInRow > 1) {
468 channelName.setVisibility(View.GONE);
469 } else {
470 channelName.setText(mSingleNotificationChannel.getName());
471 }
Julia Reynoldsac98aea2018-10-25 16:54:27 -0400472 }
473
Julia Reynolds268647a2018-10-25 16:54:27 -0400474 private void bindDelegate() {
475 TextView delegateView = findViewById(R.id.delegate_name);
476 TextView dividerView = findViewById(R.id.pkg_divider);
477
478 CharSequence delegatePkg = null;
479 if (!TextUtils.equals(mPackageName, mDelegatePkg)) {
480 // this notification was posted by a delegate!
Julia Reynolds268647a2018-10-25 16:54:27 -0400481 delegateView.setVisibility(View.VISIBLE);
482 dividerView.setVisibility(View.VISIBLE);
483 } else {
484 delegateView.setVisibility(View.GONE);
485 dividerView.setVisibility(View.GONE);
486 }
487 }
488
489 private void bindGroup() throws RemoteException {
490 // Set group information if this channel has an associated group.
491 CharSequence groupName = null;
492 if (mSingleNotificationChannel != null && mSingleNotificationChannel.getGroup() != null) {
493 final NotificationChannelGroup notificationChannelGroup =
494 mINotificationManager.getNotificationChannelGroupForPackage(
495 mSingleNotificationChannel.getGroup(), mPackageName, mAppUid);
496 if (notificationChannelGroup != null) {
497 groupName = notificationChannelGroup.getName();
498 }
499 }
500 TextView groupNameView = findViewById(R.id.group_name);
Julia Reynolds268647a2018-10-25 16:54:27 -0400501 if (groupName != null) {
502 groupNameView.setText(groupName);
503 groupNameView.setVisibility(View.VISIBLE);
Julia Reynolds268647a2018-10-25 16:54:27 -0400504 } else {
505 groupNameView.setVisibility(View.GONE);
Julia Reynolds268647a2018-10-25 16:54:27 -0400506 }
507 }
508
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500509
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700510 @VisibleForTesting
511 void logBlockingHelperCounter(String counterTag) {
512 if (mIsForBlockingHelper) {
513 mMetricsLogger.count(counterTag, 1);
514 }
515 }
516
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500517 private void saveImportance() {
Gus Prevas894d9152018-11-12 13:51:40 -0500518 if (!mIsNonblockable
519 || mExitReason != NotificationCounters.BLOCKING_HELPER_STOP_NOTIFICATIONS) {
Gus Prevascaed15c2019-01-18 14:19:51 -0500520 if (mChosenImportance == null) {
521 mChosenImportance = mStartingChannelImportance;
522 }
Gus Prevas533836a2018-09-24 17:15:32 -0400523 updateImportance();
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500524 }
Rohan Shahca0447e2018-03-30 15:18:27 -0700525 }
526
527 /**
528 * Commits the updated importance values on the background thread.
529 */
530 private void updateImportance() {
Gus Prevascaed15c2019-01-18 14:19:51 -0500531 if (mChosenImportance != null) {
532 mMetricsLogger.write(importanceChangeLogMaker());
Rohan Shahca0447e2018-03-30 15:18:27 -0700533
Julia Reynolds8728d002019-04-19 15:46:13 -0400534 int newImportance = mChosenImportance;
535 if (mStartingChannelImportance != IMPORTANCE_UNSPECIFIED) {
536 if ((mWasShownHighPriority && mChosenImportance >= IMPORTANCE_DEFAULT)
537 || (!mWasShownHighPriority && mChosenImportance < IMPORTANCE_DEFAULT)) {
538 newImportance = mStartingChannelImportance;
539 }
540 }
541
Gus Prevascaed15c2019-01-18 14:19:51 -0500542 Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
543 bgHandler.post(
544 new UpdateImportanceRunnable(mINotificationManager, mPackageName, mAppUid,
545 mNumUniqueChannelsInRow == 1 ? mSingleNotificationChannel : null,
Julia Reynolds8728d002019-04-19 15:46:13 -0400546 mStartingChannelImportance, newImportance));
Ned Burns9512e0c2019-05-30 19:36:04 -0400547 mVisualStabilityManager.temporarilyAllowReordering();
Gus Prevascaed15c2019-01-18 14:19:51 -0500548 }
Mady Mellor87d79452017-01-10 11:52:52 -0800549 }
550
Ned Burns369f1472019-05-22 21:07:26 -0400551 private void applyAlertingBehavior(@AlertingBehavior int behavior, boolean userTriggered) {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400552 if (userTriggered) {
Ned Burns369f1472019-05-22 21:07:26 -0400553 TransitionSet transition = new TransitionSet();
554 transition.setOrdering(TransitionSet.ORDERING_TOGETHER);
555 transition.addTransition(new Fade(Fade.OUT))
556 .addTransition(new ChangeBounds())
557 .addTransition(
558 new Fade(Fade.IN)
559 .setStartDelay(150)
560 .setDuration(200)
561 .setInterpolator(FAST_OUT_SLOW_IN));
562 transition.setDuration(350);
563 transition.setInterpolator(FAST_OUT_SLOW_IN);
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400564 TransitionManager.beginDelayedTransition(this, transition);
565 }
Ned Burns369f1472019-05-22 21:07:26 -0400566
567 View alert = findViewById(R.id.alert);
568 View silence = findViewById(R.id.silence);
569
570 switch (behavior) {
571 case BEHAVIOR_ALERTING:
Ned Burns369f1472019-05-22 21:07:26 -0400572 mPriorityDescriptionView.setVisibility(VISIBLE);
573 mSilentDescriptionView.setVisibility(GONE);
Julia Reynoldsdaab21a2019-06-05 14:30:05 -0400574 post(() -> {
575 alert.setSelected(true);
576 silence.setSelected(false);
577 });
Ned Burns369f1472019-05-22 21:07:26 -0400578 break;
579 case BEHAVIOR_SILENT:
Julia Reynoldsdaab21a2019-06-05 14:30:05 -0400580
Ned Burns369f1472019-05-22 21:07:26 -0400581 mSilentDescriptionView.setVisibility(VISIBLE);
582 mPriorityDescriptionView.setVisibility(GONE);
Julia Reynoldsdaab21a2019-06-05 14:30:05 -0400583 post(() -> {
584 alert.setSelected(false);
585 silence.setSelected(true);
586 });
Ned Burns369f1472019-05-22 21:07:26 -0400587 break;
588 default:
589 throw new IllegalArgumentException("Unrecognized alerting behavior: " + behavior);
Julia Reynolds8728d002019-04-19 15:46:13 -0400590 }
Ned Burns369f1472019-05-22 21:07:26 -0400591
592 boolean isAChange = mWasShownHighPriority != (behavior == BEHAVIOR_ALERTING);
593 TextView done = findViewById(R.id.done);
594 done.setText(isAChange ? R.string.inline_ok_button : R.string.inline_done_button);
Julia Reynolds8728d002019-04-19 15:46:13 -0400595 }
596
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500597 private void saveImportanceAndExitReason(@NotificationInfoAction int action) {
598 switch (action) {
599 case ACTION_UNDO:
600 mChosenImportance = mStartingChannelImportance;
601 break;
602 case ACTION_DELIVER_SILENTLY:
603 mExitReason = NotificationCounters.BLOCKING_HELPER_DELIVER_SILENTLY;
Julia Reynolds8728d002019-04-19 15:46:13 -0400604 mChosenImportance = mWasShownHighPriority
605 ? IMPORTANCE_LOW : mStartingChannelImportance;
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500606 break;
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500607 default:
608 throw new IllegalArgumentException();
609 }
610 }
611
Julia Reynolds52a27372019-04-03 11:00:17 -0400612 // only used for blocking helper
Gus Prevas894d9152018-11-12 13:51:40 -0500613 private void swapContent(@NotificationInfoAction int action, boolean animate) {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500614 if (mExpandAnimation != null) {
615 mExpandAnimation.cancel();
616 }
Mady Mellor87d79452017-01-10 11:52:52 -0800617
Julia Reynolds52a27372019-04-03 11:00:17 -0400618 View blockingHelper = findViewById(R.id.blocking_helper);
Julia Reynoldse0341482018-03-08 14:42:50 -0500619 ViewGroup confirmation = findViewById(R.id.confirmation);
620 TextView confirmationText = findViewById(R.id.confirmation_text);
Julia Reynoldse0341482018-03-08 14:42:50 -0500621
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500622 saveImportanceAndExitReason(action);
623
Gus Prevas9abc5062018-10-31 16:11:04 -0400624 switch (action) {
Gus Prevas894d9152018-11-12 13:51:40 -0500625 case ACTION_UNDO:
Gus Prevas9abc5062018-10-31 16:11:04 -0400626 break;
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500627 case ACTION_DELIVER_SILENTLY:
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500628 confirmationText.setText(R.string.notification_channel_silenced);
629 break;
Gus Prevas9abc5062018-10-31 16:11:04 -0400630 default:
631 throw new IllegalArgumentException();
Julia Reynolds437cdb12018-01-03 12:27:24 -0500632 }
633
Gus Prevas894d9152018-11-12 13:51:40 -0500634 boolean isUndo = action == ACTION_UNDO;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500635
Julia Reynolds52a27372019-04-03 11:00:17 -0400636 blockingHelper.setVisibility(isUndo ? VISIBLE : GONE);
637 findViewById(R.id.channel_info).setVisibility(isUndo ? VISIBLE : GONE);
638 findViewById(R.id.header).setVisibility(isUndo ? VISIBLE : GONE);
Gus Prevas9abc5062018-10-31 16:11:04 -0400639 confirmation.setVisibility(isUndo ? GONE : VISIBLE);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500640
Gus Prevas894d9152018-11-12 13:51:40 -0500641 if (animate) {
Julia Reynolds52a27372019-04-03 11:00:17 -0400642 ObjectAnimator promptAnim = ObjectAnimator.ofFloat(blockingHelper, View.ALPHA,
643 blockingHelper.getAlpha(), isUndo ? 1f : 0f);
Gus Prevas894d9152018-11-12 13:51:40 -0500644 promptAnim.setInterpolator(isUndo ? Interpolators.ALPHA_IN : Interpolators.ALPHA_OUT);
645 ObjectAnimator confirmAnim = ObjectAnimator.ofFloat(confirmation, View.ALPHA,
646 confirmation.getAlpha(), isUndo ? 0f : 1f);
647 confirmAnim.setInterpolator(isUndo ? Interpolators.ALPHA_OUT : Interpolators.ALPHA_IN);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500648
Gus Prevas894d9152018-11-12 13:51:40 -0500649 mExpandAnimation = new AnimatorSet();
650 mExpandAnimation.playTogether(promptAnim, confirmAnim);
651 mExpandAnimation.setDuration(150);
652 mExpandAnimation.addListener(new AnimatorListenerAdapter() {
653 boolean mCancelled = false;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500654
Gus Prevas894d9152018-11-12 13:51:40 -0500655 @Override
656 public void onAnimationCancel(Animator animation) {
657 mCancelled = true;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500658 }
Gus Prevas894d9152018-11-12 13:51:40 -0500659
660 @Override
661 public void onAnimationEnd(Animator animation) {
662 if (!mCancelled) {
Julia Reynolds52a27372019-04-03 11:00:17 -0400663 blockingHelper.setVisibility(isUndo ? VISIBLE : GONE);
Gus Prevas894d9152018-11-12 13:51:40 -0500664 confirmation.setVisibility(isUndo ? GONE : VISIBLE);
665 }
666 }
667 });
668 mExpandAnimation.start();
669 }
Rohan Shah142e2da2018-06-14 13:14:18 -0700670
671 // Since we're swapping/update the content, reset the timeout so the UI can't close
672 // immediately after the update.
673 if (mGutsContainer != null) {
674 mGutsContainer.resetFalsingCheck();
675 }
Mady Mellor87d79452017-01-10 11:52:52 -0800676 }
677
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400678 @Override
Gus Prevas9abc5062018-10-31 16:11:04 -0400679 public void onFinishedClosing() {
Gus Prevascaed15c2019-01-18 14:19:51 -0500680 if (mChosenImportance != null) {
681 mStartingChannelImportance = mChosenImportance;
Gus Prevas9abc5062018-10-31 16:11:04 -0400682 }
683 mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED;
684
Julia Reynolds52a27372019-04-03 11:00:17 -0400685 if (mIsForBlockingHelper) {
686 bindBlockingHelper();
687 } else {
688 bindInlineControls();
689 }
Will Brockmana399f022019-01-30 10:59:36 -0500690
691 mMetricsLogger.write(notificationControlsLogMaker().setType(MetricsEvent.TYPE_CLOSE));
Gus Prevas9abc5062018-10-31 16:11:04 -0400692 }
693
694 @Override
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400695 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
696 super.onInitializeAccessibilityEvent(event);
697 if (mGutsContainer != null &&
698 event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
699 if (mGutsContainer.isExposed()) {
700 event.getText().add(mContext.getString(
701 R.string.notification_channel_controls_opened_accessibility, mAppName));
702 } else {
703 event.getText().add(mContext.getString(
704 R.string.notification_channel_controls_closed_accessibility, mAppName));
705 }
706 }
707 }
708
Julia Reynolds3aedded2017-03-31 14:42:09 -0400709 private Intent getAppSettingsIntent(PackageManager pm, String packageName,
710 NotificationChannel channel, int id, String tag) {
711 Intent intent = new Intent(Intent.ACTION_MAIN)
712 .addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES)
713 .setPackage(packageName);
714 final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(
715 intent,
716 PackageManager.MATCH_DEFAULT_ONLY
717 );
718 if (resolveInfos == null || resolveInfos.size() == 0 || resolveInfos.get(0) == null) {
719 return null;
720 }
721 final ActivityInfo activityInfo = resolveInfos.get(0).activityInfo;
722 intent.setClassName(activityInfo.packageName, activityInfo.name);
723 if (channel != null) {
724 intent.putExtra(Notification.EXTRA_CHANNEL_ID, channel.getId());
725 }
726 intent.putExtra(Notification.EXTRA_NOTIFICATION_ID, id);
727 intent.putExtra(Notification.EXTRA_NOTIFICATION_TAG, tag);
728 return intent;
729 }
730
Rohan Shahca0447e2018-03-30 15:18:27 -0700731 /**
732 * Closes the controls and commits the updated importance values (indirectly). If this view is
733 * being used to show the blocking helper, this will immediately dismiss the blocking helper and
734 * commit the updated importance.
735 *
736 * <p><b>Note,</b> this will only get called once the view is dismissing. This means that the
Julia Reynolds52a27372019-04-03 11:00:17 -0400737 * user does not have the ability to undo the action anymore. See
738 * {@link #swapContent(boolean, boolean)} for where undo is handled.
Rohan Shahca0447e2018-03-30 15:18:27 -0700739 */
Rohan Shah524cf7b2018-03-15 14:40:02 -0700740 @VisibleForTesting
Evan Laird7956fe82019-05-30 17:14:57 -0400741 void closeControls(View v, boolean save) {
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700742 int[] parentLoc = new int[2];
743 int[] targetLoc = new int[2];
744 mGutsContainer.getLocationOnScreen(parentLoc);
745 v.getLocationOnScreen(targetLoc);
746 final int centerX = v.getWidth() / 2;
747 final int centerY = v.getHeight() / 2;
748 final int x = targetLoc[0] - parentLoc[0] + centerX;
749 final int y = targetLoc[1] - parentLoc[1] + centerY;
Evan Laird7956fe82019-05-30 17:14:57 -0400750 mGutsContainer.closeControls(x, y, save, false /* force */);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500751 }
752
Mady Mellor87d79452017-01-10 11:52:52 -0800753 @Override
Mady Mellor95d743c2017-01-10 12:05:27 -0800754 public void setGutsParent(NotificationGuts guts) {
755 mGutsContainer = guts;
Mady Mellor87d79452017-01-10 11:52:52 -0800756 }
757
758 @Override
Mady Mellor434180c2017-02-13 11:29:42 -0800759 public boolean willBeRemoved() {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400760 return false;
Mady Mellor434180c2017-02-13 11:29:42 -0800761 }
762
763 @Override
Lucas Dupin9b08c012018-05-16 19:53:32 -0700764 public boolean shouldBeSaved() {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400765 return mPressedApply;
Lucas Dupin9b08c012018-05-16 19:53:32 -0700766 }
767
768 @Override
Mady Mellor87d79452017-01-10 11:52:52 -0800769 public View getContentView() {
770 return this;
771 }
772
773 @Override
Mady Mellorc2dbe492017-03-30 13:22:03 -0700774 public boolean handleCloseControls(boolean save, boolean force) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500775 // Save regardless of the importance so we can lock the importance field if the user wants
776 // to keep getting notifications
777 if (save) {
Rohan Shahca0447e2018-03-30 15:18:27 -0700778 saveImportance();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500779 }
Rohan Shahdd588c72018-05-09 20:32:15 -0700780 logBlockingHelperCounter(mExitReason);
Mady Mellor87d79452017-01-10 11:52:52 -0800781 return false;
782 }
Mady Mellore09fb702017-03-30 13:23:29 -0700783
784 @Override
785 public int getActualHeight() {
786 return getHeight();
787 }
Rohan Shahca0447e2018-03-30 15:18:27 -0700788
Gus Prevas533836a2018-09-24 17:15:32 -0400789 @VisibleForTesting
790 public boolean isAnimating() {
791 return mExpandAnimation != null && mExpandAnimation.isRunning();
792 }
793
Rohan Shahca0447e2018-03-30 15:18:27 -0700794 /**
795 * Runnable to either update the given channel (with a new importance value) or, if no channel
796 * is provided, update notifications enabled state for the package.
797 */
798 private static class UpdateImportanceRunnable implements Runnable {
799 private final INotificationManager mINotificationManager;
800 private final String mPackageName;
801 private final int mAppUid;
802 private final @Nullable NotificationChannel mChannelToUpdate;
803 private final int mCurrentImportance;
804 private final int mNewImportance;
805
806
807 public UpdateImportanceRunnable(INotificationManager notificationManager,
808 String packageName, int appUid, @Nullable NotificationChannel channelToUpdate,
809 int currentImportance, int newImportance) {
810 mINotificationManager = notificationManager;
811 mPackageName = packageName;
812 mAppUid = appUid;
813 mChannelToUpdate = channelToUpdate;
814 mCurrentImportance = currentImportance;
815 mNewImportance = newImportance;
816 }
817
818 @Override
819 public void run() {
820 try {
821 if (mChannelToUpdate != null) {
822 mChannelToUpdate.setImportance(mNewImportance);
823 mChannelToUpdate.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
824 mINotificationManager.updateNotificationChannelForPackage(
825 mPackageName, mAppUid, mChannelToUpdate);
826 } else {
827 // For notifications with more than one channel, update notification enabled
828 // state. If the importance was lowered, we disable notifications.
Rohan Shah590e1b22018-04-10 23:48:47 -0400829 mINotificationManager.setNotificationsEnabledWithImportanceLockForPackage(
Rohan Shahca0447e2018-03-30 15:18:27 -0700830 mPackageName, mAppUid, mNewImportance >= mCurrentImportance);
831 }
832 } catch (RemoteException e) {
833 Log.e(TAG, "Unable to update notification importance", e);
834 }
835 }
836 }
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200837
Will Brockmana399f022019-01-30 10:59:36 -0500838 /**
839 * Returns a LogMaker with all available notification information.
840 * Caller should set category, type, and maybe subtype, before passing it to mMetricsLogger.
841 * @return LogMaker
842 */
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200843 private LogMaker getLogMaker() {
Will Brockmana399f022019-01-30 10:59:36 -0500844 // The constructor requires a category, so also do it in the other branch for consistency.
845 return mSbn == null ? new LogMaker(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
846 : mSbn.getLogMaker().setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER);
847 }
848
849 /**
850 * Returns an initialized LogMaker for logging importance changes.
851 * The caller may override the type before passing it to mMetricsLogger.
852 * @return LogMaker
853 */
854 private LogMaker importanceChangeLogMaker() {
855 Integer chosenImportance =
856 mChosenImportance != null ? mChosenImportance : mStartingChannelImportance;
857 return getLogMaker().setCategory(MetricsEvent.ACTION_SAVE_IMPORTANCE)
858 .setType(MetricsEvent.TYPE_ACTION)
859 .setSubtype(chosenImportance - mStartingChannelImportance);
860 }
861
862 /**
863 * Returns an initialized LogMaker for logging open/close of the info display.
864 * The caller may override the type before passing it to mMetricsLogger.
865 * @return LogMaker
866 */
867 private LogMaker notificationControlsLogMaker() {
868 return getLogMaker().setCategory(MetricsEvent.ACTION_NOTE_CONTROLS)
869 .setType(MetricsEvent.TYPE_OPEN)
870 .setSubtype(mIsForBlockingHelper ? MetricsEvent.BLOCKING_HELPER_DISPLAY
871 : MetricsEvent.BLOCKING_HELPER_UNKNOWN);
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200872 }
Ned Burns369f1472019-05-22 21:07:26 -0400873
874 @Retention(SOURCE)
875 @IntDef({BEHAVIOR_ALERTING, BEHAVIOR_SILENT})
876 private @interface AlertingBehavior {}
877 private static final int BEHAVIOR_ALERTING = 0;
878 private static final int BEHAVIOR_SILENT = 1;
Mady Mellor87d79452017-01-10 11:52:52 -0800879}