blob: ac24bbd6bc833798412b2313aa18c0c660c833cc [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;
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500164 closeControls(v);
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;
170 closeControls(v);
171 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);
453 mChannelEditorDialogController.show();
454 }
455 });
456 }
457
Julia Reynolds52a27372019-04-03 11:00:17 -0400458 private void bindChannelDetails() throws RemoteException {
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000459 bindName();
Julia Reynolds268647a2018-10-25 16:54:27 -0400460 bindGroup();
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000461 }
462
463 private void bindName() {
464 final TextView channelName = findViewById(R.id.channel_name);
465 if (mIsSingleDefaultChannel || mNumUniqueChannelsInRow > 1) {
466 channelName.setVisibility(View.GONE);
467 } else {
468 channelName.setText(mSingleNotificationChannel.getName());
469 }
Julia Reynoldsac98aea2018-10-25 16:54:27 -0400470 }
471
Julia Reynolds268647a2018-10-25 16:54:27 -0400472 private void bindDelegate() {
473 TextView delegateView = findViewById(R.id.delegate_name);
474 TextView dividerView = findViewById(R.id.pkg_divider);
475
476 CharSequence delegatePkg = null;
477 if (!TextUtils.equals(mPackageName, mDelegatePkg)) {
478 // this notification was posted by a delegate!
Julia Reynolds268647a2018-10-25 16:54:27 -0400479 delegateView.setVisibility(View.VISIBLE);
480 dividerView.setVisibility(View.VISIBLE);
481 } else {
482 delegateView.setVisibility(View.GONE);
483 dividerView.setVisibility(View.GONE);
484 }
485 }
486
487 private void bindGroup() throws RemoteException {
488 // Set group information if this channel has an associated group.
489 CharSequence groupName = null;
490 if (mSingleNotificationChannel != null && mSingleNotificationChannel.getGroup() != null) {
491 final NotificationChannelGroup notificationChannelGroup =
492 mINotificationManager.getNotificationChannelGroupForPackage(
493 mSingleNotificationChannel.getGroup(), mPackageName, mAppUid);
494 if (notificationChannelGroup != null) {
495 groupName = notificationChannelGroup.getName();
496 }
497 }
498 TextView groupNameView = findViewById(R.id.group_name);
Julia Reynolds268647a2018-10-25 16:54:27 -0400499 if (groupName != null) {
500 groupNameView.setText(groupName);
501 groupNameView.setVisibility(View.VISIBLE);
Julia Reynolds268647a2018-10-25 16:54:27 -0400502 } else {
503 groupNameView.setVisibility(View.GONE);
Julia Reynolds268647a2018-10-25 16:54:27 -0400504 }
505 }
506
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500507
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700508 @VisibleForTesting
509 void logBlockingHelperCounter(String counterTag) {
510 if (mIsForBlockingHelper) {
511 mMetricsLogger.count(counterTag, 1);
512 }
513 }
514
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500515 private void saveImportance() {
Gus Prevas894d9152018-11-12 13:51:40 -0500516 if (!mIsNonblockable
517 || mExitReason != NotificationCounters.BLOCKING_HELPER_STOP_NOTIFICATIONS) {
Gus Prevascaed15c2019-01-18 14:19:51 -0500518 if (mChosenImportance == null) {
519 mChosenImportance = mStartingChannelImportance;
520 }
Gus Prevas533836a2018-09-24 17:15:32 -0400521 updateImportance();
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500522 }
Rohan Shahca0447e2018-03-30 15:18:27 -0700523 }
524
525 /**
526 * Commits the updated importance values on the background thread.
527 */
528 private void updateImportance() {
Gus Prevascaed15c2019-01-18 14:19:51 -0500529 if (mChosenImportance != null) {
530 mMetricsLogger.write(importanceChangeLogMaker());
Rohan Shahca0447e2018-03-30 15:18:27 -0700531
Julia Reynolds8728d002019-04-19 15:46:13 -0400532 int newImportance = mChosenImportance;
533 if (mStartingChannelImportance != IMPORTANCE_UNSPECIFIED) {
534 if ((mWasShownHighPriority && mChosenImportance >= IMPORTANCE_DEFAULT)
535 || (!mWasShownHighPriority && mChosenImportance < IMPORTANCE_DEFAULT)) {
536 newImportance = mStartingChannelImportance;
537 }
538 }
539
Gus Prevascaed15c2019-01-18 14:19:51 -0500540 Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
541 bgHandler.post(
542 new UpdateImportanceRunnable(mINotificationManager, mPackageName, mAppUid,
543 mNumUniqueChannelsInRow == 1 ? mSingleNotificationChannel : null,
Julia Reynolds8728d002019-04-19 15:46:13 -0400544 mStartingChannelImportance, newImportance));
Ned Burns9512e0c2019-05-30 19:36:04 -0400545 mVisualStabilityManager.temporarilyAllowReordering();
Gus Prevascaed15c2019-01-18 14:19:51 -0500546 }
Mady Mellor87d79452017-01-10 11:52:52 -0800547 }
548
Ned Burns369f1472019-05-22 21:07:26 -0400549 private void applyAlertingBehavior(@AlertingBehavior int behavior, boolean userTriggered) {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400550 if (userTriggered) {
Ned Burns369f1472019-05-22 21:07:26 -0400551 TransitionSet transition = new TransitionSet();
552 transition.setOrdering(TransitionSet.ORDERING_TOGETHER);
553 transition.addTransition(new Fade(Fade.OUT))
554 .addTransition(new ChangeBounds())
555 .addTransition(
556 new Fade(Fade.IN)
557 .setStartDelay(150)
558 .setDuration(200)
559 .setInterpolator(FAST_OUT_SLOW_IN));
560 transition.setDuration(350);
561 transition.setInterpolator(FAST_OUT_SLOW_IN);
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400562 TransitionManager.beginDelayedTransition(this, transition);
563 }
Ned Burns369f1472019-05-22 21:07:26 -0400564
565 View alert = findViewById(R.id.alert);
566 View silence = findViewById(R.id.silence);
567
568 switch (behavior) {
569 case BEHAVIOR_ALERTING:
570 alert.setSelected(true);
571 silence.setSelected(false);
572 mPriorityDescriptionView.setVisibility(VISIBLE);
573 mSilentDescriptionView.setVisibility(GONE);
574 break;
575 case BEHAVIOR_SILENT:
576 alert.setSelected(false);
577 silence.setSelected(true);
578 mSilentDescriptionView.setVisibility(VISIBLE);
579 mPriorityDescriptionView.setVisibility(GONE);
580 break;
581 default:
582 throw new IllegalArgumentException("Unrecognized alerting behavior: " + behavior);
Julia Reynolds8728d002019-04-19 15:46:13 -0400583 }
Ned Burns369f1472019-05-22 21:07:26 -0400584
585 boolean isAChange = mWasShownHighPriority != (behavior == BEHAVIOR_ALERTING);
586 TextView done = findViewById(R.id.done);
587 done.setText(isAChange ? R.string.inline_ok_button : R.string.inline_done_button);
Julia Reynolds8728d002019-04-19 15:46:13 -0400588 }
589
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500590 private void saveImportanceAndExitReason(@NotificationInfoAction int action) {
591 switch (action) {
592 case ACTION_UNDO:
593 mChosenImportance = mStartingChannelImportance;
594 break;
595 case ACTION_DELIVER_SILENTLY:
596 mExitReason = NotificationCounters.BLOCKING_HELPER_DELIVER_SILENTLY;
Julia Reynolds8728d002019-04-19 15:46:13 -0400597 mChosenImportance = mWasShownHighPriority
598 ? IMPORTANCE_LOW : mStartingChannelImportance;
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500599 break;
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500600 default:
601 throw new IllegalArgumentException();
602 }
603 }
604
Julia Reynolds52a27372019-04-03 11:00:17 -0400605 // only used for blocking helper
Gus Prevas894d9152018-11-12 13:51:40 -0500606 private void swapContent(@NotificationInfoAction int action, boolean animate) {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500607 if (mExpandAnimation != null) {
608 mExpandAnimation.cancel();
609 }
Mady Mellor87d79452017-01-10 11:52:52 -0800610
Julia Reynolds52a27372019-04-03 11:00:17 -0400611 View blockingHelper = findViewById(R.id.blocking_helper);
Julia Reynoldse0341482018-03-08 14:42:50 -0500612 ViewGroup confirmation = findViewById(R.id.confirmation);
613 TextView confirmationText = findViewById(R.id.confirmation_text);
Julia Reynoldse0341482018-03-08 14:42:50 -0500614
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500615 saveImportanceAndExitReason(action);
616
Gus Prevas9abc5062018-10-31 16:11:04 -0400617 switch (action) {
Gus Prevas894d9152018-11-12 13:51:40 -0500618 case ACTION_UNDO:
Gus Prevas9abc5062018-10-31 16:11:04 -0400619 break;
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500620 case ACTION_DELIVER_SILENTLY:
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500621 confirmationText.setText(R.string.notification_channel_silenced);
622 break;
Gus Prevas9abc5062018-10-31 16:11:04 -0400623 default:
624 throw new IllegalArgumentException();
Julia Reynolds437cdb12018-01-03 12:27:24 -0500625 }
626
Gus Prevas894d9152018-11-12 13:51:40 -0500627 boolean isUndo = action == ACTION_UNDO;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500628
Julia Reynolds52a27372019-04-03 11:00:17 -0400629 blockingHelper.setVisibility(isUndo ? VISIBLE : GONE);
630 findViewById(R.id.channel_info).setVisibility(isUndo ? VISIBLE : GONE);
631 findViewById(R.id.header).setVisibility(isUndo ? VISIBLE : GONE);
Gus Prevas9abc5062018-10-31 16:11:04 -0400632 confirmation.setVisibility(isUndo ? GONE : VISIBLE);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500633
Gus Prevas894d9152018-11-12 13:51:40 -0500634 if (animate) {
Julia Reynolds52a27372019-04-03 11:00:17 -0400635 ObjectAnimator promptAnim = ObjectAnimator.ofFloat(blockingHelper, View.ALPHA,
636 blockingHelper.getAlpha(), isUndo ? 1f : 0f);
Gus Prevas894d9152018-11-12 13:51:40 -0500637 promptAnim.setInterpolator(isUndo ? Interpolators.ALPHA_IN : Interpolators.ALPHA_OUT);
638 ObjectAnimator confirmAnim = ObjectAnimator.ofFloat(confirmation, View.ALPHA,
639 confirmation.getAlpha(), isUndo ? 0f : 1f);
640 confirmAnim.setInterpolator(isUndo ? Interpolators.ALPHA_OUT : Interpolators.ALPHA_IN);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500641
Gus Prevas894d9152018-11-12 13:51:40 -0500642 mExpandAnimation = new AnimatorSet();
643 mExpandAnimation.playTogether(promptAnim, confirmAnim);
644 mExpandAnimation.setDuration(150);
645 mExpandAnimation.addListener(new AnimatorListenerAdapter() {
646 boolean mCancelled = false;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500647
Gus Prevas894d9152018-11-12 13:51:40 -0500648 @Override
649 public void onAnimationCancel(Animator animation) {
650 mCancelled = true;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500651 }
Gus Prevas894d9152018-11-12 13:51:40 -0500652
653 @Override
654 public void onAnimationEnd(Animator animation) {
655 if (!mCancelled) {
Julia Reynolds52a27372019-04-03 11:00:17 -0400656 blockingHelper.setVisibility(isUndo ? VISIBLE : GONE);
Gus Prevas894d9152018-11-12 13:51:40 -0500657 confirmation.setVisibility(isUndo ? GONE : VISIBLE);
658 }
659 }
660 });
661 mExpandAnimation.start();
662 }
Rohan Shah142e2da2018-06-14 13:14:18 -0700663
664 // Since we're swapping/update the content, reset the timeout so the UI can't close
665 // immediately after the update.
666 if (mGutsContainer != null) {
667 mGutsContainer.resetFalsingCheck();
668 }
Mady Mellor87d79452017-01-10 11:52:52 -0800669 }
670
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400671 @Override
Gus Prevas9abc5062018-10-31 16:11:04 -0400672 public void onFinishedClosing() {
Gus Prevascaed15c2019-01-18 14:19:51 -0500673 if (mChosenImportance != null) {
674 mStartingChannelImportance = mChosenImportance;
Gus Prevas9abc5062018-10-31 16:11:04 -0400675 }
676 mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED;
677
Julia Reynolds52a27372019-04-03 11:00:17 -0400678 if (mIsForBlockingHelper) {
679 bindBlockingHelper();
680 } else {
681 bindInlineControls();
682 }
Will Brockmana399f022019-01-30 10:59:36 -0500683
684 mMetricsLogger.write(notificationControlsLogMaker().setType(MetricsEvent.TYPE_CLOSE));
Gus Prevas9abc5062018-10-31 16:11:04 -0400685 }
686
687 @Override
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400688 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
689 super.onInitializeAccessibilityEvent(event);
690 if (mGutsContainer != null &&
691 event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
692 if (mGutsContainer.isExposed()) {
693 event.getText().add(mContext.getString(
694 R.string.notification_channel_controls_opened_accessibility, mAppName));
695 } else {
696 event.getText().add(mContext.getString(
697 R.string.notification_channel_controls_closed_accessibility, mAppName));
698 }
699 }
700 }
701
Julia Reynolds3aedded2017-03-31 14:42:09 -0400702 private Intent getAppSettingsIntent(PackageManager pm, String packageName,
703 NotificationChannel channel, int id, String tag) {
704 Intent intent = new Intent(Intent.ACTION_MAIN)
705 .addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES)
706 .setPackage(packageName);
707 final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(
708 intent,
709 PackageManager.MATCH_DEFAULT_ONLY
710 );
711 if (resolveInfos == null || resolveInfos.size() == 0 || resolveInfos.get(0) == null) {
712 return null;
713 }
714 final ActivityInfo activityInfo = resolveInfos.get(0).activityInfo;
715 intent.setClassName(activityInfo.packageName, activityInfo.name);
716 if (channel != null) {
717 intent.putExtra(Notification.EXTRA_CHANNEL_ID, channel.getId());
718 }
719 intent.putExtra(Notification.EXTRA_NOTIFICATION_ID, id);
720 intent.putExtra(Notification.EXTRA_NOTIFICATION_TAG, tag);
721 return intent;
722 }
723
Rohan Shahca0447e2018-03-30 15:18:27 -0700724 /**
725 * Closes the controls and commits the updated importance values (indirectly). If this view is
726 * being used to show the blocking helper, this will immediately dismiss the blocking helper and
727 * commit the updated importance.
728 *
729 * <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 -0400730 * user does not have the ability to undo the action anymore. See
731 * {@link #swapContent(boolean, boolean)} for where undo is handled.
Rohan Shahca0447e2018-03-30 15:18:27 -0700732 */
Rohan Shah524cf7b2018-03-15 14:40:02 -0700733 @VisibleForTesting
734 void closeControls(View v) {
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700735 int[] parentLoc = new int[2];
736 int[] targetLoc = new int[2];
737 mGutsContainer.getLocationOnScreen(parentLoc);
738 v.getLocationOnScreen(targetLoc);
739 final int centerX = v.getWidth() / 2;
740 final int centerY = v.getHeight() / 2;
741 final int x = targetLoc[0] - parentLoc[0] + centerX;
742 final int y = targetLoc[1] - parentLoc[1] + centerY;
743 mGutsContainer.closeControls(x, y, true /* save */, false /* force */);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500744 }
745
Mady Mellor87d79452017-01-10 11:52:52 -0800746 @Override
Mady Mellor95d743c2017-01-10 12:05:27 -0800747 public void setGutsParent(NotificationGuts guts) {
748 mGutsContainer = guts;
Mady Mellor87d79452017-01-10 11:52:52 -0800749 }
750
751 @Override
Mady Mellor434180c2017-02-13 11:29:42 -0800752 public boolean willBeRemoved() {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400753 return false;
Mady Mellor434180c2017-02-13 11:29:42 -0800754 }
755
756 @Override
Lucas Dupin9b08c012018-05-16 19:53:32 -0700757 public boolean shouldBeSaved() {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400758 return mPressedApply;
Lucas Dupin9b08c012018-05-16 19:53:32 -0700759 }
760
761 @Override
Mady Mellor87d79452017-01-10 11:52:52 -0800762 public View getContentView() {
763 return this;
764 }
765
766 @Override
Mady Mellorc2dbe492017-03-30 13:22:03 -0700767 public boolean handleCloseControls(boolean save, boolean force) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500768 // Save regardless of the importance so we can lock the importance field if the user wants
769 // to keep getting notifications
770 if (save) {
Rohan Shahca0447e2018-03-30 15:18:27 -0700771 saveImportance();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500772 }
Rohan Shahdd588c72018-05-09 20:32:15 -0700773 logBlockingHelperCounter(mExitReason);
Mady Mellor87d79452017-01-10 11:52:52 -0800774 return false;
775 }
Mady Mellore09fb702017-03-30 13:23:29 -0700776
777 @Override
778 public int getActualHeight() {
779 return getHeight();
780 }
Rohan Shahca0447e2018-03-30 15:18:27 -0700781
Gus Prevas533836a2018-09-24 17:15:32 -0400782 @VisibleForTesting
783 public boolean isAnimating() {
784 return mExpandAnimation != null && mExpandAnimation.isRunning();
785 }
786
Rohan Shahca0447e2018-03-30 15:18:27 -0700787 /**
788 * Runnable to either update the given channel (with a new importance value) or, if no channel
789 * is provided, update notifications enabled state for the package.
790 */
791 private static class UpdateImportanceRunnable implements Runnable {
792 private final INotificationManager mINotificationManager;
793 private final String mPackageName;
794 private final int mAppUid;
795 private final @Nullable NotificationChannel mChannelToUpdate;
796 private final int mCurrentImportance;
797 private final int mNewImportance;
798
799
800 public UpdateImportanceRunnable(INotificationManager notificationManager,
801 String packageName, int appUid, @Nullable NotificationChannel channelToUpdate,
802 int currentImportance, int newImportance) {
803 mINotificationManager = notificationManager;
804 mPackageName = packageName;
805 mAppUid = appUid;
806 mChannelToUpdate = channelToUpdate;
807 mCurrentImportance = currentImportance;
808 mNewImportance = newImportance;
809 }
810
811 @Override
812 public void run() {
813 try {
814 if (mChannelToUpdate != null) {
815 mChannelToUpdate.setImportance(mNewImportance);
816 mChannelToUpdate.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
817 mINotificationManager.updateNotificationChannelForPackage(
818 mPackageName, mAppUid, mChannelToUpdate);
819 } else {
820 // For notifications with more than one channel, update notification enabled
821 // state. If the importance was lowered, we disable notifications.
Rohan Shah590e1b22018-04-10 23:48:47 -0400822 mINotificationManager.setNotificationsEnabledWithImportanceLockForPackage(
Rohan Shahca0447e2018-03-30 15:18:27 -0700823 mPackageName, mAppUid, mNewImportance >= mCurrentImportance);
824 }
825 } catch (RemoteException e) {
826 Log.e(TAG, "Unable to update notification importance", e);
827 }
828 }
829 }
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200830
Will Brockmana399f022019-01-30 10:59:36 -0500831 /**
832 * Returns a LogMaker with all available notification information.
833 * Caller should set category, type, and maybe subtype, before passing it to mMetricsLogger.
834 * @return LogMaker
835 */
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200836 private LogMaker getLogMaker() {
Will Brockmana399f022019-01-30 10:59:36 -0500837 // The constructor requires a category, so also do it in the other branch for consistency.
838 return mSbn == null ? new LogMaker(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
839 : mSbn.getLogMaker().setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER);
840 }
841
842 /**
843 * Returns an initialized LogMaker for logging importance changes.
844 * The caller may override the type before passing it to mMetricsLogger.
845 * @return LogMaker
846 */
847 private LogMaker importanceChangeLogMaker() {
848 Integer chosenImportance =
849 mChosenImportance != null ? mChosenImportance : mStartingChannelImportance;
850 return getLogMaker().setCategory(MetricsEvent.ACTION_SAVE_IMPORTANCE)
851 .setType(MetricsEvent.TYPE_ACTION)
852 .setSubtype(chosenImportance - mStartingChannelImportance);
853 }
854
855 /**
856 * Returns an initialized LogMaker for logging open/close of the info display.
857 * The caller may override the type before passing it to mMetricsLogger.
858 * @return LogMaker
859 */
860 private LogMaker notificationControlsLogMaker() {
861 return getLogMaker().setCategory(MetricsEvent.ACTION_NOTE_CONTROLS)
862 .setType(MetricsEvent.TYPE_OPEN)
863 .setSubtype(mIsForBlockingHelper ? MetricsEvent.BLOCKING_HELPER_DISPLAY
864 : MetricsEvent.BLOCKING_HELPER_UNKNOWN);
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200865 }
Ned Burns369f1472019-05-22 21:07:26 -0400866
867 @Retention(SOURCE)
868 @IntDef({BEHAVIOR_ALERTING, BEHAVIOR_SILENT})
869 private @interface AlertingBehavior {}
870 private static final int BEHAVIOR_ALERTING = 0;
871 private static final int BEHAVIOR_SILENT = 1;
Mady Mellor87d79452017-01-10 11:52:52 -0800872}