blob: 1dc96b82f4a58516f579bc79f51a23971658e30e [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;
Rohan Shah20790b82018-07-02 17:21:04 -070068import com.android.systemui.statusbar.notification.logging.NotificationCounters;
Mady Mellor87d79452017-01-10 11:52:52 -080069
Ned Burns369f1472019-05-22 21:07:26 -040070import java.lang.annotation.Retention;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050071import java.util.List;
Evan Laird47dc4542019-04-24 15:10:52 -040072import java.util.Set;
Mady Mellor87d79452017-01-10 11:52:52 -080073
74/**
Rohan Shahda5dcdd2018-04-27 17:21:50 -070075 * The guts of a notification revealed when performing a long press. This also houses the blocking
76 * helper affordance that allows a user to keep/stop notifications after swiping one away.
Mady Mellor87d79452017-01-10 11:52:52 -080077 */
Mady Mellor95d743c2017-01-10 12:05:27 -080078public class NotificationInfo extends LinearLayout implements NotificationGuts.GutsContent {
Mady Mellor87d79452017-01-10 11:52:52 -080079 private static final String TAG = "InfoGuts";
80
Gus Prevas894d9152018-11-12 13:51:40 -050081 @IntDef(prefix = { "ACTION_" }, value = {
82 ACTION_NONE,
83 ACTION_UNDO,
84 ACTION_TOGGLE_SILENT,
85 ACTION_BLOCK,
Gus Prevas9abc5062018-10-31 16:11:04 -040086 })
Gus Prevas894d9152018-11-12 13:51:40 -050087 public @interface NotificationInfoAction {
88 }
Gus Prevas9abc5062018-10-31 16:11:04 -040089
Gus Prevas894d9152018-11-12 13:51:40 -050090 public static final int ACTION_NONE = 0;
Nadia Benbernoub66516c2019-03-06 11:01:53 -050091 static final int ACTION_UNDO = 1;
Julia Reynolds52a27372019-04-03 11:00:17 -040092 // standard controls
Nadia Benbernoub66516c2019-03-06 11:01:53 -050093 static final int ACTION_TOGGLE_SILENT = 2;
Julia Reynolds52a27372019-04-03 11:00:17 -040094 // unused
Nadia Benbernoub66516c2019-03-06 11:01:53 -050095 static final int ACTION_BLOCK = 3;
Julia Reynolds52a27372019-04-03 11:00:17 -040096 // blocking helper
Nadia Benbernoub66516c2019-03-06 11:01:53 -050097 static final int ACTION_DELIVER_SILENTLY = 4;
Julia Reynolds52a27372019-04-03 11:00:17 -040098 // standard controls
Nadia Benbernoub66516c2019-03-06 11:01:53 -050099 private static final int ACTION_ALERT = 5;
Gus Prevas9abc5062018-10-31 16:11:04 -0400100
Ned Burns369f1472019-05-22 21:07:26 -0400101 private TextView mPriorityDescriptionView;
102 private TextView mSilentDescriptionView;
Dan Sandler0ed02292019-05-14 19:18:35 -0400103
Mady Mellor87d79452017-01-10 11:52:52 -0800104 private INotificationManager mINotificationManager;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500105 private PackageManager mPm;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700106 private MetricsLogger mMetricsLogger;
Evan Laird47dc4542019-04-24 15:10:52 -0400107 private ChannelEditorDialogController mChannelEditorDialogController;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500108
Rohan Shahca0447e2018-03-30 15:18:27 -0700109 private String mPackageName;
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400110 private String mAppName;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500111 private int mAppUid;
Julia Reynolds268647a2018-10-25 16:54:27 -0400112 private String mDelegatePkg;
Rohan Shahca0447e2018-03-30 15:18:27 -0700113 private int mNumUniqueChannelsInRow;
Evan Laird47dc4542019-04-24 15:10:52 -0400114 private Set<NotificationChannel> mUniqueChannelsInRow;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500115 private NotificationChannel mSingleNotificationChannel;
Gus Prevas9abc5062018-10-31 16:11:04 -0400116 private int mStartingChannelImportance;
Gus Prevascaed15c2019-01-18 14:19:51 -0500117 private boolean mWasShownHighPriority;
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400118 private boolean mPressedApply;
119
Gus Prevascaed15c2019-01-18 14:19:51 -0500120 /**
121 * The last importance level chosen by the user. Null if the user has not chosen an importance
122 * level; non-null once the user takes an action which indicates an explicit preference.
123 */
124 @Nullable private Integer mChosenImportance;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500125 private boolean mIsSingleDefaultChannel;
Rohan Shah63411fc2018-03-28 19:05:52 -0700126 private boolean mIsNonblockable;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500127 private StatusBarNotification mSbn;
128 private AnimatorSet mExpandAnimation;
Julia Reynolds35765d82018-08-17 11:39:19 -0400129 private boolean mIsDeviceProvisioned;
Mady Mellor87d79452017-01-10 11:52:52 -0800130
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400131 private CheckSaveListener mCheckSaveListener;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500132 private OnSettingsClickListener mOnSettingsClickListener;
Julia Reynolds3aedded2017-03-31 14:42:09 -0400133 private OnAppSettingsClickListener mAppSettingsClickListener;
Mady Mellor95d743c2017-01-10 12:05:27 -0800134 private NotificationGuts mGutsContainer;
Evan Laird47dc4542019-04-24 15:10:52 -0400135 private Drawable mPkgIcon;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700136
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700137 /** Whether this view is being shown as part of the blocking helper. */
Rohan Shah524cf7b2018-03-15 14:40:02 -0700138 private boolean mIsForBlockingHelper;
Mady Mellor87d79452017-01-10 11:52:52 -0800139
Rohan Shahdd588c72018-05-09 20:32:15 -0700140 /**
141 * String that describes how the user exit or quit out of this view, also used as a counter tag.
142 */
143 private String mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700144
Julia Reynolds52a27372019-04-03 11:00:17 -0400145 // used by standard ui
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500146 private OnClickListener mOnAlert = v -> {
147 mExitReason = NotificationCounters.BLOCKING_HELPER_KEEP_SHOWING;
148 mChosenImportance = IMPORTANCE_DEFAULT;
Ned Burns369f1472019-05-22 21:07:26 -0400149 applyAlertingBehavior(BEHAVIOR_ALERTING, true /* userTriggered */);
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500150 };
151
Julia Reynolds52a27372019-04-03 11:00:17 -0400152 // used by standard ui
153 private OnClickListener mOnSilent = v -> {
154 mExitReason = NotificationCounters.BLOCKING_HELPER_DELIVER_SILENTLY;
155 mChosenImportance = IMPORTANCE_LOW;
Ned Burns369f1472019-05-22 21:07:26 -0400156 applyAlertingBehavior(BEHAVIOR_SILENT, true /* userTriggered */);
Julia Reynolds52a27372019-04-03 11:00:17 -0400157 };
158
159 // used by standard ui
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500160 private OnClickListener mOnDismissSettings = v -> {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400161 mPressedApply = true;
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500162 closeControls(v);
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500163 };
164
Julia Reynolds52a27372019-04-03 11:00:17 -0400165 // used by blocking helper
166 private OnClickListener mOnKeepShowing = v -> {
167 mExitReason = NotificationCounters.BLOCKING_HELPER_KEEP_SHOWING;
168 closeControls(v);
169 mMetricsLogger.write(getLogMaker().setCategory(
170 MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
171 .setType(MetricsEvent.TYPE_ACTION)
172 .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_STAY_SILENT));
173 };
174
175 // used by blocking helper
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500176 private OnClickListener mOnDeliverSilently = v -> {
177 handleSaveImportance(
178 ACTION_DELIVER_SILENTLY, MetricsEvent.BLOCKING_HELPER_CLICK_STAY_SILENT);
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500179 };
180
181 private void handleSaveImportance(int action, int metricsSubtype) {
Gus Prevas533836a2018-09-24 17:15:32 -0400182 Runnable saveImportance = () -> {
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500183 saveImportanceAndExitReason(action);
Will Brockmana399f022019-01-30 10:59:36 -0500184 if (mIsForBlockingHelper) {
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500185 swapContent(action, true /* animate */);
Will Brockmana399f022019-01-30 10:59:36 -0500186 mMetricsLogger.write(getLogMaker()
187 .setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
188 .setType(MetricsEvent.TYPE_ACTION)
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500189 .setSubtype(metricsSubtype));
Will Brockmana399f022019-01-30 10:59:36 -0500190 }
Gus Prevas533836a2018-09-24 17:15:32 -0400191 };
192 if (mCheckSaveListener != null) {
193 mCheckSaveListener.checkSave(saveImportance, mSbn);
194 } else {
195 saveImportance.run();
196 }
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500197 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500198
199 private OnClickListener mOnUndo = v -> {
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700200 // Reset exit counter that we'll log and record an undo event separately (not an exit event)
Rohan Shahdd588c72018-05-09 20:32:15 -0700201 mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED;
Will Brockmana399f022019-01-30 10:59:36 -0500202 if (mIsForBlockingHelper) {
203 logBlockingHelperCounter(NotificationCounters.BLOCKING_HELPER_UNDO);
204 mMetricsLogger.write(getLogMaker().setCategory(
205 MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
206 .setType(MetricsEvent.TYPE_DISMISS)
207 .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_UNDO));
208 } else {
Julia Reynolds52a27372019-04-03 11:00:17 -0400209 // TODO: this can't happen?
Will Brockmana399f022019-01-30 10:59:36 -0500210 mMetricsLogger.write(importanceChangeLogMaker().setType(MetricsEvent.TYPE_DISMISS));
211 }
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500212 saveImportanceAndExitReason(ACTION_UNDO);
Gus Prevas894d9152018-11-12 13:51:40 -0500213 swapContent(ACTION_UNDO, true /* animate */);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500214 };
215
Mady Mellor87d79452017-01-10 11:52:52 -0800216 public NotificationInfo(Context context, AttributeSet attrs) {
217 super(context, attrs);
218 }
219
Ned Burns369f1472019-05-22 21:07:26 -0400220 @Override
221 protected void onFinishInflate() {
222 super.onFinishInflate();
223
224 mPriorityDescriptionView = findViewById(R.id.alert_summary);
225 mSilentDescriptionView = findViewById(R.id.silence_summary);
226 }
227
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400228 // Specify a CheckSaveListener to override when/if the user's changes are committed.
229 public interface CheckSaveListener {
230 // Invoked when importance has changed and the NotificationInfo wants to try to save it.
231 // Listener should run saveImportance unless the change should be canceled.
Eliot Courtney47098cb2017-10-18 17:30:30 +0900232 void checkSave(Runnable saveImportance, StatusBarNotification sbn);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400233 }
234
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500235 public interface OnSettingsClickListener {
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500236 void onClick(View v, NotificationChannel channel, int appUid);
Mady Mellor87d79452017-01-10 11:52:52 -0800237 }
238
Julia Reynolds3aedded2017-03-31 14:42:09 -0400239 public interface OnAppSettingsClickListener {
240 void onClick(View v, Intent intent);
241 }
242
Rohan Shah524cf7b2018-03-15 14:40:02 -0700243 @VisibleForTesting
244 void bindNotification(
245 final PackageManager pm,
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500246 final INotificationManager iNotificationManager,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500247 final String pkg,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500248 final NotificationChannel notificationChannel,
Evan Laird47dc4542019-04-24 15:10:52 -0400249 final Set<NotificationChannel> uniqueChannelsInRow,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400250 final StatusBarNotification sbn,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500251 final CheckSaveListener checkSaveListener,
252 final OnSettingsClickListener onSettingsClick,
253 final OnAppSettingsClickListener onAppSettingsClick,
Julia Reynolds35765d82018-08-17 11:39:19 -0400254 boolean isDeviceProvisioned,
Gus Prevas9abc5062018-10-31 16:11:04 -0400255 boolean isNonblockable,
Gus Prevascaed15c2019-01-18 14:19:51 -0500256 int importance,
257 boolean wasShownHighPriority)
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500258 throws RemoteException {
Rohan Shahca0447e2018-03-30 15:18:27 -0700259 bindNotification(pm, iNotificationManager, pkg, notificationChannel,
Evan Laird47dc4542019-04-24 15:10:52 -0400260 uniqueChannelsInRow, sbn, checkSaveListener, onSettingsClick,
Julia Reynolds35765d82018-08-17 11:39:19 -0400261 onAppSettingsClick, isDeviceProvisioned, isNonblockable,
Julia Reynolds52a27372019-04-03 11:00:17 -0400262 false /* isBlockingHelper */,
Gus Prevascaed15c2019-01-18 14:19:51 -0500263 importance, wasShownHighPriority);
Julia Reynolds0ef7d842018-01-24 17:50:31 -0500264 }
265
Rohan Shah524cf7b2018-03-15 14:40:02 -0700266 public void bindNotification(
267 PackageManager pm,
268 INotificationManager iNotificationManager,
269 String pkg,
270 NotificationChannel notificationChannel,
Evan Laird47dc4542019-04-24 15:10:52 -0400271 Set<NotificationChannel> uniqueChannelsInRow,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700272 StatusBarNotification sbn,
273 CheckSaveListener checkSaveListener,
274 OnSettingsClickListener onSettingsClick,
275 OnAppSettingsClickListener onAppSettingsClick,
Julia Reynolds35765d82018-08-17 11:39:19 -0400276 boolean isDeviceProvisioned,
Rohan Shah63411fc2018-03-28 19:05:52 -0700277 boolean isNonblockable,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700278 boolean isForBlockingHelper,
Gus Prevascaed15c2019-01-18 14:19:51 -0500279 int importance,
280 boolean wasShownHighPriority)
Rohan Shah524cf7b2018-03-15 14:40:02 -0700281 throws RemoteException {
Mady Mellor87d79452017-01-10 11:52:52 -0800282 mINotificationManager = iNotificationManager;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700283 mMetricsLogger = Dependency.get(MetricsLogger.class);
Evan Laird47dc4542019-04-24 15:10:52 -0400284 mChannelEditorDialogController = Dependency.get(ChannelEditorDialogController.class);
Rohan Shahca0447e2018-03-30 15:18:27 -0700285 mPackageName = pkg;
Evan Laird47dc4542019-04-24 15:10:52 -0400286 mUniqueChannelsInRow = uniqueChannelsInRow;
287 mNumUniqueChannelsInRow = uniqueChannelsInRow.size();
Julia Reynolds3aedded2017-03-31 14:42:09 -0400288 mSbn = sbn;
289 mPm = pm;
290 mAppSettingsClickListener = onAppSettingsClick;
Rohan Shahca0447e2018-03-30 15:18:27 -0700291 mAppName = mPackageName;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500292 mCheckSaveListener = checkSaveListener;
293 mOnSettingsClickListener = onSettingsClick;
294 mSingleNotificationChannel = notificationChannel;
Gus Prevascaed15c2019-01-18 14:19:51 -0500295 mStartingChannelImportance = mSingleNotificationChannel.getImportance();
296 mWasShownHighPriority = wasShownHighPriority;
Rohan Shah63411fc2018-03-28 19:05:52 -0700297 mIsNonblockable = isNonblockable;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700298 mIsForBlockingHelper = isForBlockingHelper;
Rohan Shahdbd64e72018-03-28 14:46:50 -0700299 mAppUid = mSbn.getUid();
Julia Reynolds268647a2018-10-25 16:54:27 -0400300 mDelegatePkg = mSbn.getOpPkg();
Julia Reynolds35765d82018-08-17 11:39:19 -0400301 mIsDeviceProvisioned = isDeviceProvisioned;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500302
Julia Reynolds437cdb12018-01-03 12:27:24 -0500303 int numTotalChannels = mINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400304 pkg, mAppUid, false /* includeDeleted */);
Rohan Shahca0447e2018-03-30 15:18:27 -0700305 if (mNumUniqueChannelsInRow == 0) {
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400306 throw new IllegalArgumentException("bindNotification requires at least one channel");
307 } else {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500308 // Special behavior for the Default channel if no other channels have been defined.
Rohan Shahca0447e2018-03-30 15:18:27 -0700309 mIsSingleDefaultChannel = mNumUniqueChannelsInRow == 1
Rohan Shahdbd64e72018-03-28 14:46:50 -0700310 && mSingleNotificationChannel.getId().equals(
311 NotificationChannel.DEFAULT_CHANNEL_ID)
312 && numTotalChannels == 1;
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400313 }
314
Julia Reynolds437cdb12018-01-03 12:27:24 -0500315 bindHeader();
Julia Reynolds52a27372019-04-03 11:00:17 -0400316 bindChannelDetails();
317
318 if (mIsForBlockingHelper) {
319 bindBlockingHelper();
320 } else {
321 bindInlineControls();
322 }
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200323
Will Brockmana399f022019-01-30 10:59:36 -0500324 mMetricsLogger.write(notificationControlsLogMaker());
Julia Reynolds437cdb12018-01-03 12:27:24 -0500325 }
326
Julia Reynolds52a27372019-04-03 11:00:17 -0400327 private void bindBlockingHelper() {
328 findViewById(R.id.inline_controls).setVisibility(GONE);
329 findViewById(R.id.blocking_helper).setVisibility(VISIBLE);
330
331 findViewById(R.id.undo).setOnClickListener(mOnUndo);
332
333 View turnOffButton = findViewById(R.id.blocking_helper_turn_off_notifications);
334 turnOffButton.setOnClickListener(getSettingsOnClickListener());
335 turnOffButton.setVisibility(turnOffButton.hasOnClickListeners() ? VISIBLE : GONE);
336
337 TextView keepShowing = findViewById(R.id.keep_showing);
338 keepShowing.setOnClickListener(mOnKeepShowing);
339
340 View deliverSilently = findViewById(R.id.deliver_silently);
341 deliverSilently.setOnClickListener(mOnDeliverSilently);
342 }
343
344 private void bindInlineControls() {
345 findViewById(R.id.inline_controls).setVisibility(VISIBLE);
346 findViewById(R.id.blocking_helper).setVisibility(GONE);
347
348 if (mIsNonblockable) {
349 findViewById(R.id.non_configurable_text).setVisibility(VISIBLE);
350 findViewById(R.id.non_configurable_multichannel_text).setVisibility(GONE);
351 findViewById(R.id.interruptiveness_settings).setVisibility(GONE);
Julia Reynolds8728d002019-04-19 15:46:13 -0400352 ((TextView) findViewById(R.id.done)).setText(R.string.inline_done_button);
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400353 findViewById(R.id.turn_off_notifications).setVisibility(GONE);
Julia Reynolds52a27372019-04-03 11:00:17 -0400354 } else if (mNumUniqueChannelsInRow > 1) {
355 findViewById(R.id.non_configurable_text).setVisibility(GONE);
356 findViewById(R.id.interruptiveness_settings).setVisibility(GONE);
357 findViewById(R.id.non_configurable_multichannel_text).setVisibility(VISIBLE);
358 } else {
359 findViewById(R.id.non_configurable_text).setVisibility(GONE);
360 findViewById(R.id.non_configurable_multichannel_text).setVisibility(GONE);
361 findViewById(R.id.interruptiveness_settings).setVisibility(VISIBLE);
362 }
363
364 View turnOffButton = findViewById(R.id.turn_off_notifications);
Evan Laird47dc4542019-04-24 15:10:52 -0400365 turnOffButton.setOnClickListener(getTurnOffNotificationsClickListener());
Julia Reynolds52a27372019-04-03 11:00:17 -0400366 turnOffButton.setVisibility(turnOffButton.hasOnClickListeners() && !mIsNonblockable
367 ? VISIBLE : GONE);
368
369 View done = findViewById(R.id.done);
370 done.setOnClickListener(mOnDismissSettings);
371
372
Julia Reynolds8728d002019-04-19 15:46:13 -0400373 View silent = findViewById(R.id.silence);
374 View alert = findViewById(R.id.alert);
Julia Reynolds52a27372019-04-03 11:00:17 -0400375 silent.setOnClickListener(mOnSilent);
376 alert.setOnClickListener(mOnAlert);
377
Ned Burns369f1472019-05-22 21:07:26 -0400378 applyAlertingBehavior(
379 mWasShownHighPriority ? BEHAVIOR_ALERTING : BEHAVIOR_SILENT,
380 false /* userTriggered */);
Julia Reynolds52a27372019-04-03 11:00:17 -0400381 }
382
383 private void bindHeader() {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500384 // Package name
Evan Laird47dc4542019-04-24 15:10:52 -0400385 mPkgIcon = null;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500386 ApplicationInfo info;
387 try {
Rohan Shahca0447e2018-03-30 15:18:27 -0700388 info = mPm.getApplicationInfo(
389 mPackageName,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500390 PackageManager.MATCH_UNINSTALLED_PACKAGES
391 | PackageManager.MATCH_DISABLED_COMPONENTS
392 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
393 | PackageManager.MATCH_DIRECT_BOOT_AWARE);
394 if (info != null) {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500395 mAppName = String.valueOf(mPm.getApplicationLabel(info));
Evan Laird47dc4542019-04-24 15:10:52 -0400396 mPkgIcon = mPm.getApplicationIcon(info);
Geoffrey Pitschee8c81e2017-03-23 11:38:56 -0400397 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500398 } catch (PackageManager.NameNotFoundException e) {
399 // app is gone, just show package name and generic icon
Evan Laird47dc4542019-04-24 15:10:52 -0400400 mPkgIcon = mPm.getDefaultActivityIcon();
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500401 }
Evan Laird47dc4542019-04-24 15:10:52 -0400402 ((ImageView) findViewById(R.id.pkgicon)).setImageDrawable(mPkgIcon);
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400403 ((TextView) findViewById(R.id.pkgname)).setText(mAppName);
Mady Mellor87d79452017-01-10 11:52:52 -0800404
Julia Reynolds268647a2018-10-25 16:54:27 -0400405 // Delegate
406 bindDelegate();
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000407
Julia Reynolds52a27372019-04-03 11:00:17 -0400408 // Set up app settings link (i.e. Customize)
409 View settingsLinkView = findViewById(R.id.app_settings);
410 Intent settingsIntent = getAppSettingsIntent(mPm, mPackageName,
411 mSingleNotificationChannel,
412 mSbn.getId(), mSbn.getTag());
413 if (settingsIntent != null
414 && !TextUtils.isEmpty(mSbn.getNotification().getSettingsText())) {
415 settingsLinkView.setVisibility(VISIBLE);
416 settingsLinkView.setOnClickListener((View view) -> {
417 mAppSettingsClickListener.onClick(view, settingsIntent);
418 });
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000419 } else {
Julia Reynolds52a27372019-04-03 11:00:17 -0400420 settingsLinkView.setVisibility(View.GONE);
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000421 }
Julia Reynolds52a27372019-04-03 11:00:17 -0400422
423 // System Settings button.
424 final View settingsButton = findViewById(R.id.info);
425 settingsButton.setOnClickListener(getSettingsOnClickListener());
426 settingsButton.setVisibility(settingsButton.hasOnClickListeners() ? VISIBLE : GONE);
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000427 }
428
Julia Reynolds52a27372019-04-03 11:00:17 -0400429 private OnClickListener getSettingsOnClickListener() {
430 if (mAppUid >= 0 && mOnSettingsClickListener != null && mIsDeviceProvisioned) {
431 final int appUidF = mAppUid;
432 return ((View view) -> {
433 logBlockingHelperCounter(
434 NotificationCounters.BLOCKING_HELPER_NOTIF_SETTINGS);
435 mOnSettingsClickListener.onClick(view,
436 mNumUniqueChannelsInRow > 1 ? null : mSingleNotificationChannel,
437 appUidF);
438 });
439 }
440 return null;
441 }
442
Evan Laird47dc4542019-04-24 15:10:52 -0400443 private OnClickListener getTurnOffNotificationsClickListener() {
444 return ((View view) -> {
445 if (mChannelEditorDialogController != null) {
446 mChannelEditorDialogController.prepareDialogForApp(mAppName, mPackageName, mAppUid,
447 mUniqueChannelsInRow, mPkgIcon, mOnSettingsClickListener);
448 mChannelEditorDialogController.show();
449 }
450 });
451 }
452
Julia Reynolds52a27372019-04-03 11:00:17 -0400453 private void bindChannelDetails() throws RemoteException {
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000454 bindName();
Julia Reynolds268647a2018-10-25 16:54:27 -0400455 bindGroup();
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000456 }
457
458 private void bindName() {
459 final TextView channelName = findViewById(R.id.channel_name);
460 if (mIsSingleDefaultChannel || mNumUniqueChannelsInRow > 1) {
461 channelName.setVisibility(View.GONE);
462 } else {
463 channelName.setText(mSingleNotificationChannel.getName());
464 }
Julia Reynoldsac98aea2018-10-25 16:54:27 -0400465 }
466
Julia Reynolds268647a2018-10-25 16:54:27 -0400467 private void bindDelegate() {
468 TextView delegateView = findViewById(R.id.delegate_name);
469 TextView dividerView = findViewById(R.id.pkg_divider);
470
471 CharSequence delegatePkg = null;
472 if (!TextUtils.equals(mPackageName, mDelegatePkg)) {
473 // this notification was posted by a delegate!
Julia Reynolds268647a2018-10-25 16:54:27 -0400474 delegateView.setVisibility(View.VISIBLE);
475 dividerView.setVisibility(View.VISIBLE);
476 } else {
477 delegateView.setVisibility(View.GONE);
478 dividerView.setVisibility(View.GONE);
479 }
480 }
481
482 private void bindGroup() throws RemoteException {
483 // Set group information if this channel has an associated group.
484 CharSequence groupName = null;
485 if (mSingleNotificationChannel != null && mSingleNotificationChannel.getGroup() != null) {
486 final NotificationChannelGroup notificationChannelGroup =
487 mINotificationManager.getNotificationChannelGroupForPackage(
488 mSingleNotificationChannel.getGroup(), mPackageName, mAppUid);
489 if (notificationChannelGroup != null) {
490 groupName = notificationChannelGroup.getName();
491 }
492 }
493 TextView groupNameView = findViewById(R.id.group_name);
Julia Reynolds268647a2018-10-25 16:54:27 -0400494 if (groupName != null) {
495 groupNameView.setText(groupName);
496 groupNameView.setVisibility(View.VISIBLE);
Julia Reynolds268647a2018-10-25 16:54:27 -0400497 } else {
498 groupNameView.setVisibility(View.GONE);
Julia Reynolds268647a2018-10-25 16:54:27 -0400499 }
500 }
501
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500502
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700503 @VisibleForTesting
504 void logBlockingHelperCounter(String counterTag) {
505 if (mIsForBlockingHelper) {
506 mMetricsLogger.count(counterTag, 1);
507 }
508 }
509
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500510 private void saveImportance() {
Gus Prevas894d9152018-11-12 13:51:40 -0500511 if (!mIsNonblockable
512 || mExitReason != NotificationCounters.BLOCKING_HELPER_STOP_NOTIFICATIONS) {
Gus Prevascaed15c2019-01-18 14:19:51 -0500513 if (mChosenImportance == null) {
514 mChosenImportance = mStartingChannelImportance;
515 }
Gus Prevas533836a2018-09-24 17:15:32 -0400516 updateImportance();
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500517 }
Rohan Shahca0447e2018-03-30 15:18:27 -0700518 }
519
520 /**
521 * Commits the updated importance values on the background thread.
522 */
523 private void updateImportance() {
Gus Prevascaed15c2019-01-18 14:19:51 -0500524 if (mChosenImportance != null) {
525 mMetricsLogger.write(importanceChangeLogMaker());
Rohan Shahca0447e2018-03-30 15:18:27 -0700526
Julia Reynolds8728d002019-04-19 15:46:13 -0400527 int newImportance = mChosenImportance;
528 if (mStartingChannelImportance != IMPORTANCE_UNSPECIFIED) {
529 if ((mWasShownHighPriority && mChosenImportance >= IMPORTANCE_DEFAULT)
530 || (!mWasShownHighPriority && mChosenImportance < IMPORTANCE_DEFAULT)) {
531 newImportance = mStartingChannelImportance;
532 }
533 }
534
Gus Prevascaed15c2019-01-18 14:19:51 -0500535 Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
536 bgHandler.post(
537 new UpdateImportanceRunnable(mINotificationManager, mPackageName, mAppUid,
538 mNumUniqueChannelsInRow == 1 ? mSingleNotificationChannel : null,
Julia Reynolds8728d002019-04-19 15:46:13 -0400539 mStartingChannelImportance, newImportance));
Gus Prevascaed15c2019-01-18 14:19:51 -0500540 }
Mady Mellor87d79452017-01-10 11:52:52 -0800541 }
542
Ned Burns369f1472019-05-22 21:07:26 -0400543 private void applyAlertingBehavior(@AlertingBehavior int behavior, boolean userTriggered) {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400544 if (userTriggered) {
Ned Burns369f1472019-05-22 21:07:26 -0400545 TransitionSet transition = new TransitionSet();
546 transition.setOrdering(TransitionSet.ORDERING_TOGETHER);
547 transition.addTransition(new Fade(Fade.OUT))
548 .addTransition(new ChangeBounds())
549 .addTransition(
550 new Fade(Fade.IN)
551 .setStartDelay(150)
552 .setDuration(200)
553 .setInterpolator(FAST_OUT_SLOW_IN));
554 transition.setDuration(350);
555 transition.setInterpolator(FAST_OUT_SLOW_IN);
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400556 TransitionManager.beginDelayedTransition(this, transition);
557 }
Ned Burns369f1472019-05-22 21:07:26 -0400558
559 View alert = findViewById(R.id.alert);
560 View silence = findViewById(R.id.silence);
561
562 switch (behavior) {
563 case BEHAVIOR_ALERTING:
564 alert.setSelected(true);
565 silence.setSelected(false);
566 mPriorityDescriptionView.setVisibility(VISIBLE);
567 mSilentDescriptionView.setVisibility(GONE);
568 break;
569 case BEHAVIOR_SILENT:
570 alert.setSelected(false);
571 silence.setSelected(true);
572 mSilentDescriptionView.setVisibility(VISIBLE);
573 mPriorityDescriptionView.setVisibility(GONE);
574 break;
575 default:
576 throw new IllegalArgumentException("Unrecognized alerting behavior: " + behavior);
Julia Reynolds8728d002019-04-19 15:46:13 -0400577 }
Ned Burns369f1472019-05-22 21:07:26 -0400578
579 boolean isAChange = mWasShownHighPriority != (behavior == BEHAVIOR_ALERTING);
580 TextView done = findViewById(R.id.done);
581 done.setText(isAChange ? R.string.inline_ok_button : R.string.inline_done_button);
Julia Reynolds8728d002019-04-19 15:46:13 -0400582 }
583
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500584 private void saveImportanceAndExitReason(@NotificationInfoAction int action) {
585 switch (action) {
586 case ACTION_UNDO:
587 mChosenImportance = mStartingChannelImportance;
588 break;
589 case ACTION_DELIVER_SILENTLY:
590 mExitReason = NotificationCounters.BLOCKING_HELPER_DELIVER_SILENTLY;
Julia Reynolds8728d002019-04-19 15:46:13 -0400591 mChosenImportance = mWasShownHighPriority
592 ? IMPORTANCE_LOW : mStartingChannelImportance;
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500593 break;
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500594 default:
595 throw new IllegalArgumentException();
596 }
597 }
598
Julia Reynolds52a27372019-04-03 11:00:17 -0400599 // only used for blocking helper
Gus Prevas894d9152018-11-12 13:51:40 -0500600 private void swapContent(@NotificationInfoAction int action, boolean animate) {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500601 if (mExpandAnimation != null) {
602 mExpandAnimation.cancel();
603 }
Mady Mellor87d79452017-01-10 11:52:52 -0800604
Julia Reynolds52a27372019-04-03 11:00:17 -0400605 View blockingHelper = findViewById(R.id.blocking_helper);
Julia Reynoldse0341482018-03-08 14:42:50 -0500606 ViewGroup confirmation = findViewById(R.id.confirmation);
607 TextView confirmationText = findViewById(R.id.confirmation_text);
Julia Reynoldse0341482018-03-08 14:42:50 -0500608
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500609 saveImportanceAndExitReason(action);
610
Gus Prevas9abc5062018-10-31 16:11:04 -0400611 switch (action) {
Gus Prevas894d9152018-11-12 13:51:40 -0500612 case ACTION_UNDO:
Gus Prevas9abc5062018-10-31 16:11:04 -0400613 break;
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500614 case ACTION_DELIVER_SILENTLY:
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500615 confirmationText.setText(R.string.notification_channel_silenced);
616 break;
Gus Prevas9abc5062018-10-31 16:11:04 -0400617 default:
618 throw new IllegalArgumentException();
Julia Reynolds437cdb12018-01-03 12:27:24 -0500619 }
620
Gus Prevas894d9152018-11-12 13:51:40 -0500621 boolean isUndo = action == ACTION_UNDO;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500622
Julia Reynolds52a27372019-04-03 11:00:17 -0400623 blockingHelper.setVisibility(isUndo ? VISIBLE : GONE);
624 findViewById(R.id.channel_info).setVisibility(isUndo ? VISIBLE : GONE);
625 findViewById(R.id.header).setVisibility(isUndo ? VISIBLE : GONE);
Gus Prevas9abc5062018-10-31 16:11:04 -0400626 confirmation.setVisibility(isUndo ? GONE : VISIBLE);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500627
Gus Prevas894d9152018-11-12 13:51:40 -0500628 if (animate) {
Julia Reynolds52a27372019-04-03 11:00:17 -0400629 ObjectAnimator promptAnim = ObjectAnimator.ofFloat(blockingHelper, View.ALPHA,
630 blockingHelper.getAlpha(), isUndo ? 1f : 0f);
Gus Prevas894d9152018-11-12 13:51:40 -0500631 promptAnim.setInterpolator(isUndo ? Interpolators.ALPHA_IN : Interpolators.ALPHA_OUT);
632 ObjectAnimator confirmAnim = ObjectAnimator.ofFloat(confirmation, View.ALPHA,
633 confirmation.getAlpha(), isUndo ? 0f : 1f);
634 confirmAnim.setInterpolator(isUndo ? Interpolators.ALPHA_OUT : Interpolators.ALPHA_IN);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500635
Gus Prevas894d9152018-11-12 13:51:40 -0500636 mExpandAnimation = new AnimatorSet();
637 mExpandAnimation.playTogether(promptAnim, confirmAnim);
638 mExpandAnimation.setDuration(150);
639 mExpandAnimation.addListener(new AnimatorListenerAdapter() {
640 boolean mCancelled = false;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500641
Gus Prevas894d9152018-11-12 13:51:40 -0500642 @Override
643 public void onAnimationCancel(Animator animation) {
644 mCancelled = true;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500645 }
Gus Prevas894d9152018-11-12 13:51:40 -0500646
647 @Override
648 public void onAnimationEnd(Animator animation) {
649 if (!mCancelled) {
Julia Reynolds52a27372019-04-03 11:00:17 -0400650 blockingHelper.setVisibility(isUndo ? VISIBLE : GONE);
Gus Prevas894d9152018-11-12 13:51:40 -0500651 confirmation.setVisibility(isUndo ? GONE : VISIBLE);
652 }
653 }
654 });
655 mExpandAnimation.start();
656 }
Rohan Shah142e2da2018-06-14 13:14:18 -0700657
658 // Since we're swapping/update the content, reset the timeout so the UI can't close
659 // immediately after the update.
660 if (mGutsContainer != null) {
661 mGutsContainer.resetFalsingCheck();
662 }
Mady Mellor87d79452017-01-10 11:52:52 -0800663 }
664
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400665 @Override
Gus Prevas9abc5062018-10-31 16:11:04 -0400666 public void onFinishedClosing() {
Gus Prevascaed15c2019-01-18 14:19:51 -0500667 if (mChosenImportance != null) {
668 mStartingChannelImportance = mChosenImportance;
Gus Prevas9abc5062018-10-31 16:11:04 -0400669 }
670 mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED;
671
Julia Reynolds52a27372019-04-03 11:00:17 -0400672 if (mIsForBlockingHelper) {
673 bindBlockingHelper();
674 } else {
675 bindInlineControls();
676 }
Will Brockmana399f022019-01-30 10:59:36 -0500677
678 mMetricsLogger.write(notificationControlsLogMaker().setType(MetricsEvent.TYPE_CLOSE));
Gus Prevas9abc5062018-10-31 16:11:04 -0400679 }
680
681 @Override
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400682 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
683 super.onInitializeAccessibilityEvent(event);
684 if (mGutsContainer != null &&
685 event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
686 if (mGutsContainer.isExposed()) {
687 event.getText().add(mContext.getString(
688 R.string.notification_channel_controls_opened_accessibility, mAppName));
689 } else {
690 event.getText().add(mContext.getString(
691 R.string.notification_channel_controls_closed_accessibility, mAppName));
692 }
693 }
694 }
695
Julia Reynolds3aedded2017-03-31 14:42:09 -0400696 private Intent getAppSettingsIntent(PackageManager pm, String packageName,
697 NotificationChannel channel, int id, String tag) {
698 Intent intent = new Intent(Intent.ACTION_MAIN)
699 .addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES)
700 .setPackage(packageName);
701 final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(
702 intent,
703 PackageManager.MATCH_DEFAULT_ONLY
704 );
705 if (resolveInfos == null || resolveInfos.size() == 0 || resolveInfos.get(0) == null) {
706 return null;
707 }
708 final ActivityInfo activityInfo = resolveInfos.get(0).activityInfo;
709 intent.setClassName(activityInfo.packageName, activityInfo.name);
710 if (channel != null) {
711 intent.putExtra(Notification.EXTRA_CHANNEL_ID, channel.getId());
712 }
713 intent.putExtra(Notification.EXTRA_NOTIFICATION_ID, id);
714 intent.putExtra(Notification.EXTRA_NOTIFICATION_TAG, tag);
715 return intent;
716 }
717
Rohan Shahca0447e2018-03-30 15:18:27 -0700718 /**
719 * Closes the controls and commits the updated importance values (indirectly). If this view is
720 * being used to show the blocking helper, this will immediately dismiss the blocking helper and
721 * commit the updated importance.
722 *
723 * <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 -0400724 * user does not have the ability to undo the action anymore. See
725 * {@link #swapContent(boolean, boolean)} for where undo is handled.
Rohan Shahca0447e2018-03-30 15:18:27 -0700726 */
Rohan Shah524cf7b2018-03-15 14:40:02 -0700727 @VisibleForTesting
728 void closeControls(View v) {
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700729 int[] parentLoc = new int[2];
730 int[] targetLoc = new int[2];
731 mGutsContainer.getLocationOnScreen(parentLoc);
732 v.getLocationOnScreen(targetLoc);
733 final int centerX = v.getWidth() / 2;
734 final int centerY = v.getHeight() / 2;
735 final int x = targetLoc[0] - parentLoc[0] + centerX;
736 final int y = targetLoc[1] - parentLoc[1] + centerY;
737 mGutsContainer.closeControls(x, y, true /* save */, false /* force */);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500738 }
739
Mady Mellor87d79452017-01-10 11:52:52 -0800740 @Override
Mady Mellor95d743c2017-01-10 12:05:27 -0800741 public void setGutsParent(NotificationGuts guts) {
742 mGutsContainer = guts;
Mady Mellor87d79452017-01-10 11:52:52 -0800743 }
744
745 @Override
Mady Mellor434180c2017-02-13 11:29:42 -0800746 public boolean willBeRemoved() {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400747 return false;
Mady Mellor434180c2017-02-13 11:29:42 -0800748 }
749
750 @Override
Lucas Dupin9b08c012018-05-16 19:53:32 -0700751 public boolean shouldBeSaved() {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400752 return mPressedApply;
Lucas Dupin9b08c012018-05-16 19:53:32 -0700753 }
754
755 @Override
Mady Mellor87d79452017-01-10 11:52:52 -0800756 public View getContentView() {
757 return this;
758 }
759
760 @Override
Mady Mellorc2dbe492017-03-30 13:22:03 -0700761 public boolean handleCloseControls(boolean save, boolean force) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500762 // Save regardless of the importance so we can lock the importance field if the user wants
763 // to keep getting notifications
764 if (save) {
Rohan Shahca0447e2018-03-30 15:18:27 -0700765 saveImportance();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500766 }
Rohan Shahdd588c72018-05-09 20:32:15 -0700767 logBlockingHelperCounter(mExitReason);
Mady Mellor87d79452017-01-10 11:52:52 -0800768 return false;
769 }
Mady Mellore09fb702017-03-30 13:23:29 -0700770
771 @Override
772 public int getActualHeight() {
773 return getHeight();
774 }
Rohan Shahca0447e2018-03-30 15:18:27 -0700775
Gus Prevas533836a2018-09-24 17:15:32 -0400776 @VisibleForTesting
777 public boolean isAnimating() {
778 return mExpandAnimation != null && mExpandAnimation.isRunning();
779 }
780
Rohan Shahca0447e2018-03-30 15:18:27 -0700781 /**
782 * Runnable to either update the given channel (with a new importance value) or, if no channel
783 * is provided, update notifications enabled state for the package.
784 */
785 private static class UpdateImportanceRunnable implements Runnable {
786 private final INotificationManager mINotificationManager;
787 private final String mPackageName;
788 private final int mAppUid;
789 private final @Nullable NotificationChannel mChannelToUpdate;
790 private final int mCurrentImportance;
791 private final int mNewImportance;
792
793
794 public UpdateImportanceRunnable(INotificationManager notificationManager,
795 String packageName, int appUid, @Nullable NotificationChannel channelToUpdate,
796 int currentImportance, int newImportance) {
797 mINotificationManager = notificationManager;
798 mPackageName = packageName;
799 mAppUid = appUid;
800 mChannelToUpdate = channelToUpdate;
801 mCurrentImportance = currentImportance;
802 mNewImportance = newImportance;
803 }
804
805 @Override
806 public void run() {
807 try {
808 if (mChannelToUpdate != null) {
809 mChannelToUpdate.setImportance(mNewImportance);
810 mChannelToUpdate.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
811 mINotificationManager.updateNotificationChannelForPackage(
812 mPackageName, mAppUid, mChannelToUpdate);
813 } else {
814 // For notifications with more than one channel, update notification enabled
815 // state. If the importance was lowered, we disable notifications.
Rohan Shah590e1b22018-04-10 23:48:47 -0400816 mINotificationManager.setNotificationsEnabledWithImportanceLockForPackage(
Rohan Shahca0447e2018-03-30 15:18:27 -0700817 mPackageName, mAppUid, mNewImportance >= mCurrentImportance);
818 }
819 } catch (RemoteException e) {
820 Log.e(TAG, "Unable to update notification importance", e);
821 }
822 }
823 }
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200824
Will Brockmana399f022019-01-30 10:59:36 -0500825 /**
826 * Returns a LogMaker with all available notification information.
827 * Caller should set category, type, and maybe subtype, before passing it to mMetricsLogger.
828 * @return LogMaker
829 */
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200830 private LogMaker getLogMaker() {
Will Brockmana399f022019-01-30 10:59:36 -0500831 // The constructor requires a category, so also do it in the other branch for consistency.
832 return mSbn == null ? new LogMaker(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
833 : mSbn.getLogMaker().setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER);
834 }
835
836 /**
837 * Returns an initialized LogMaker for logging importance changes.
838 * The caller may override the type before passing it to mMetricsLogger.
839 * @return LogMaker
840 */
841 private LogMaker importanceChangeLogMaker() {
842 Integer chosenImportance =
843 mChosenImportance != null ? mChosenImportance : mStartingChannelImportance;
844 return getLogMaker().setCategory(MetricsEvent.ACTION_SAVE_IMPORTANCE)
845 .setType(MetricsEvent.TYPE_ACTION)
846 .setSubtype(chosenImportance - mStartingChannelImportance);
847 }
848
849 /**
850 * Returns an initialized LogMaker for logging open/close of the info display.
851 * The caller may override the type before passing it to mMetricsLogger.
852 * @return LogMaker
853 */
854 private LogMaker notificationControlsLogMaker() {
855 return getLogMaker().setCategory(MetricsEvent.ACTION_NOTE_CONTROLS)
856 .setType(MetricsEvent.TYPE_OPEN)
857 .setSubtype(mIsForBlockingHelper ? MetricsEvent.BLOCKING_HELPER_DISPLAY
858 : MetricsEvent.BLOCKING_HELPER_UNKNOWN);
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200859 }
Ned Burns369f1472019-05-22 21:07:26 -0400860
861 @Retention(SOURCE)
862 @IntDef({BEHAVIOR_ALERTING, BEHAVIOR_SILENT})
863 private @interface AlertingBehavior {}
864 private static final int BEHAVIOR_ALERTING = 0;
865 private static final int BEHAVIOR_SILENT = 1;
Mady Mellor87d79452017-01-10 11:52:52 -0800866}