blob: e4e8c80867b1d5ccb14139bb5d48070b5efe0caa [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
Julia Reynolds437cdb12018-01-03 12:27:24 -050023import android.animation.Animator;
24import android.animation.AnimatorListenerAdapter;
25import android.animation.AnimatorSet;
26import android.animation.ObjectAnimator;
Gus Prevas9abc5062018-10-31 16:11:04 -040027import android.annotation.IntDef;
Rohan Shahca0447e2018-03-30 15:18:27 -070028import android.annotation.Nullable;
Mady Mellor87d79452017-01-10 11:52:52 -080029import android.app.INotificationManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040030import android.app.Notification;
Mady Mellor87d79452017-01-10 11:52:52 -080031import android.app.NotificationChannel;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050032import android.app.NotificationChannelGroup;
Mady Mellor87d79452017-01-10 11:52:52 -080033import android.content.Context;
Julia Reynolds3aedded2017-03-31 14:42:09 -040034import android.content.Intent;
35import android.content.pm.ActivityInfo;
Mady Mellor87d79452017-01-10 11:52:52 -080036import android.content.pm.ApplicationInfo;
Mady Mellor87d79452017-01-10 11:52:52 -080037import android.content.pm.PackageManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040038import android.content.pm.ResolveInfo;
Mady Mellor87d79452017-01-10 11:52:52 -080039import android.graphics.drawable.Drawable;
William Brockman75cf66a2018-12-21 13:25:01 -050040import android.metrics.LogMaker;
Rohan Shahca0447e2018-03-30 15:18:27 -070041import android.os.Handler;
Mady Mellor87d79452017-01-10 11:52:52 -080042import android.os.RemoteException;
Julia Reynolds4d1dd792019-04-29 11:48:29 -040043import android.provider.Settings;
Julia Reynolds3aedded2017-03-31 14:42:09 -040044import android.service.notification.StatusBarNotification;
45import android.text.TextUtils;
Julia Reynolds4d1dd792019-04-29 11:48:29 -040046import android.transition.AutoTransition;
47import android.transition.TransitionManager;
Mady Mellor87d79452017-01-10 11:52:52 -080048import android.util.AttributeSet;
Rohan Shahca0447e2018-03-30 15:18:27 -070049import android.util.Log;
Mady Mellor87d79452017-01-10 11:52:52 -080050import android.view.View;
Julia Reynoldse0341482018-03-08 14:42:50 -050051import android.view.ViewGroup;
Geoffrey Pitschd94e7882017-04-06 09:52:11 -040052import android.view.accessibility.AccessibilityEvent;
Mady Mellor87d79452017-01-10 11:52:52 -080053import android.widget.ImageView;
54import android.widget.LinearLayout;
Mady Mellor87d79452017-01-10 11:52:52 -080055import android.widget.TextView;
56
Rohan Shah524cf7b2018-03-15 14:40:02 -070057import com.android.internal.annotations.VisibleForTesting;
Mady Mellor87d79452017-01-10 11:52:52 -080058import com.android.internal.logging.MetricsLogger;
59import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Rohan Shah524cf7b2018-03-15 14:40:02 -070060import com.android.systemui.Dependency;
Julia Reynolds437cdb12018-01-03 12:27:24 -050061import com.android.systemui.Interpolators;
Mady Mellor87d79452017-01-10 11:52:52 -080062import com.android.systemui.R;
Rohan Shah20790b82018-07-02 17:21:04 -070063import com.android.systemui.statusbar.notification.logging.NotificationCounters;
Mady Mellor87d79452017-01-10 11:52:52 -080064
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050065import java.util.List;
Evan Laird47dc4542019-04-24 15:10:52 -040066import java.util.Set;
Mady Mellor87d79452017-01-10 11:52:52 -080067
68/**
Rohan Shahda5dcdd2018-04-27 17:21:50 -070069 * The guts of a notification revealed when performing a long press. This also houses the blocking
70 * helper affordance that allows a user to keep/stop notifications after swiping one away.
Mady Mellor87d79452017-01-10 11:52:52 -080071 */
Mady Mellor95d743c2017-01-10 12:05:27 -080072public class NotificationInfo extends LinearLayout implements NotificationGuts.GutsContent {
Mady Mellor87d79452017-01-10 11:52:52 -080073 private static final String TAG = "InfoGuts";
74
Gus Prevas894d9152018-11-12 13:51:40 -050075 @IntDef(prefix = { "ACTION_" }, value = {
76 ACTION_NONE,
77 ACTION_UNDO,
78 ACTION_TOGGLE_SILENT,
79 ACTION_BLOCK,
Gus Prevas9abc5062018-10-31 16:11:04 -040080 })
Gus Prevas894d9152018-11-12 13:51:40 -050081 public @interface NotificationInfoAction {
82 }
Gus Prevas9abc5062018-10-31 16:11:04 -040083
Gus Prevas894d9152018-11-12 13:51:40 -050084 public static final int ACTION_NONE = 0;
Nadia Benbernoub66516c2019-03-06 11:01:53 -050085 static final int ACTION_UNDO = 1;
Julia Reynolds52a27372019-04-03 11:00:17 -040086 // standard controls
Nadia Benbernoub66516c2019-03-06 11:01:53 -050087 static final int ACTION_TOGGLE_SILENT = 2;
Julia Reynolds52a27372019-04-03 11:00:17 -040088 // unused
Nadia Benbernoub66516c2019-03-06 11:01:53 -050089 static final int ACTION_BLOCK = 3;
Julia Reynolds52a27372019-04-03 11:00:17 -040090 // blocking helper
Nadia Benbernoub66516c2019-03-06 11:01:53 -050091 static final int ACTION_DELIVER_SILENTLY = 4;
Julia Reynolds52a27372019-04-03 11:00:17 -040092 // standard controls
Nadia Benbernoub66516c2019-03-06 11:01:53 -050093 private static final int ACTION_ALERT = 5;
Gus Prevas9abc5062018-10-31 16:11:04 -040094
Julia Reynolds4d1dd792019-04-29 11:48:29 -040095 private static final int BUTTON_ANIM_TIME_MS = 200;
96
Dan Sandler0ed02292019-05-14 19:18:35 -040097 private static final boolean SHOW_BUTTON_SUMMARY = false;
98
Mady Mellor87d79452017-01-10 11:52:52 -080099 private INotificationManager mINotificationManager;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500100 private PackageManager mPm;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700101 private MetricsLogger mMetricsLogger;
Evan Laird47dc4542019-04-24 15:10:52 -0400102 private ChannelEditorDialogController mChannelEditorDialogController;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500103
Rohan Shahca0447e2018-03-30 15:18:27 -0700104 private String mPackageName;
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400105 private String mAppName;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500106 private int mAppUid;
Julia Reynolds268647a2018-10-25 16:54:27 -0400107 private String mDelegatePkg;
Rohan Shahca0447e2018-03-30 15:18:27 -0700108 private int mNumUniqueChannelsInRow;
Evan Laird47dc4542019-04-24 15:10:52 -0400109 private Set<NotificationChannel> mUniqueChannelsInRow;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500110 private NotificationChannel mSingleNotificationChannel;
Gus Prevas9abc5062018-10-31 16:11:04 -0400111 private int mStartingChannelImportance;
Gus Prevascaed15c2019-01-18 14:19:51 -0500112 private boolean mWasShownHighPriority;
Julia Reynolds8728d002019-04-19 15:46:13 -0400113 private boolean mShowOnLockscreen;
114 private boolean mShowInStatusBar;
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400115 private boolean mPressedApply;
116
Gus Prevascaed15c2019-01-18 14:19:51 -0500117 /**
118 * The last importance level chosen by the user. Null if the user has not chosen an importance
119 * level; non-null once the user takes an action which indicates an explicit preference.
120 */
121 @Nullable private Integer mChosenImportance;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500122 private boolean mIsSingleDefaultChannel;
Rohan Shah63411fc2018-03-28 19:05:52 -0700123 private boolean mIsNonblockable;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500124 private StatusBarNotification mSbn;
125 private AnimatorSet mExpandAnimation;
Julia Reynolds35765d82018-08-17 11:39:19 -0400126 private boolean mIsDeviceProvisioned;
Mady Mellor87d79452017-01-10 11:52:52 -0800127
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400128 private CheckSaveListener mCheckSaveListener;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500129 private OnSettingsClickListener mOnSettingsClickListener;
Julia Reynolds3aedded2017-03-31 14:42:09 -0400130 private OnAppSettingsClickListener mAppSettingsClickListener;
Mady Mellor95d743c2017-01-10 12:05:27 -0800131 private NotificationGuts mGutsContainer;
Julia Reynolds8728d002019-04-19 15:46:13 -0400132 private Drawable mSelectedBackground;
133 private Drawable mUnselectedBackground;
Evan Laird47dc4542019-04-24 15:10:52 -0400134 private Drawable mPkgIcon;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700135
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700136 /** Whether this view is being shown as part of the blocking helper. */
Rohan Shah524cf7b2018-03-15 14:40:02 -0700137 private boolean mIsForBlockingHelper;
Mady Mellor87d79452017-01-10 11:52:52 -0800138
Rohan Shahdd588c72018-05-09 20:32:15 -0700139 /**
140 * String that describes how the user exit or quit out of this view, also used as a counter tag.
141 */
142 private String mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700143
Julia Reynolds52a27372019-04-03 11:00:17 -0400144 // used by standard ui
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500145 private OnClickListener mOnAlert = v -> {
146 mExitReason = NotificationCounters.BLOCKING_HELPER_KEEP_SHOWING;
147 mChosenImportance = IMPORTANCE_DEFAULT;
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400148 setImportanceSummary(ACTION_ALERT, true);
Julia Reynolds52a27372019-04-03 11:00:17 -0400149 updateButtons(ACTION_ALERT);
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;
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400156 setImportanceSummary(ACTION_TOGGLE_SILENT, true);
Julia Reynolds52a27372019-04-03 11:00:17 -0400157 updateButtons(ACTION_TOGGLE_SILENT);
158 };
159
160 // used by standard ui
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500161 private OnClickListener mOnDismissSettings = v -> {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400162 mPressedApply = true;
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500163 closeControls(v);
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500164 };
165
Julia Reynolds52a27372019-04-03 11:00:17 -0400166 // used by blocking helper
167 private OnClickListener mOnKeepShowing = v -> {
168 mExitReason = NotificationCounters.BLOCKING_HELPER_KEEP_SHOWING;
169 closeControls(v);
170 mMetricsLogger.write(getLogMaker().setCategory(
171 MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
172 .setType(MetricsEvent.TYPE_ACTION)
173 .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_STAY_SILENT));
174 };
175
176 // used by blocking helper
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500177 private OnClickListener mOnDeliverSilently = v -> {
178 handleSaveImportance(
179 ACTION_DELIVER_SILENTLY, MetricsEvent.BLOCKING_HELPER_CLICK_STAY_SILENT);
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500180 };
181
182 private void handleSaveImportance(int action, int metricsSubtype) {
Gus Prevas533836a2018-09-24 17:15:32 -0400183 Runnable saveImportance = () -> {
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500184 saveImportanceAndExitReason(action);
Will Brockmana399f022019-01-30 10:59:36 -0500185 if (mIsForBlockingHelper) {
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500186 swapContent(action, true /* animate */);
Will Brockmana399f022019-01-30 10:59:36 -0500187 mMetricsLogger.write(getLogMaker()
188 .setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
189 .setType(MetricsEvent.TYPE_ACTION)
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500190 .setSubtype(metricsSubtype));
Will Brockmana399f022019-01-30 10:59:36 -0500191 }
Gus Prevas533836a2018-09-24 17:15:32 -0400192 };
193 if (mCheckSaveListener != null) {
194 mCheckSaveListener.checkSave(saveImportance, mSbn);
195 } else {
196 saveImportance.run();
197 }
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500198 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500199
200 private OnClickListener mOnUndo = v -> {
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700201 // Reset exit counter that we'll log and record an undo event separately (not an exit event)
Rohan Shahdd588c72018-05-09 20:32:15 -0700202 mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED;
Will Brockmana399f022019-01-30 10:59:36 -0500203 if (mIsForBlockingHelper) {
204 logBlockingHelperCounter(NotificationCounters.BLOCKING_HELPER_UNDO);
205 mMetricsLogger.write(getLogMaker().setCategory(
206 MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
207 .setType(MetricsEvent.TYPE_DISMISS)
208 .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_UNDO));
209 } else {
Julia Reynolds52a27372019-04-03 11:00:17 -0400210 // TODO: this can't happen?
Will Brockmana399f022019-01-30 10:59:36 -0500211 mMetricsLogger.write(importanceChangeLogMaker().setType(MetricsEvent.TYPE_DISMISS));
212 }
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500213 saveImportanceAndExitReason(ACTION_UNDO);
Gus Prevas894d9152018-11-12 13:51:40 -0500214 swapContent(ACTION_UNDO, true /* animate */);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500215 };
216
Mady Mellor87d79452017-01-10 11:52:52 -0800217 public NotificationInfo(Context context, AttributeSet attrs) {
218 super(context, attrs);
219 }
220
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400221 // Specify a CheckSaveListener to override when/if the user's changes are committed.
222 public interface CheckSaveListener {
223 // Invoked when importance has changed and the NotificationInfo wants to try to save it.
224 // Listener should run saveImportance unless the change should be canceled.
Eliot Courtney47098cb2017-10-18 17:30:30 +0900225 void checkSave(Runnable saveImportance, StatusBarNotification sbn);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400226 }
227
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500228 public interface OnSettingsClickListener {
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500229 void onClick(View v, NotificationChannel channel, int appUid);
Mady Mellor87d79452017-01-10 11:52:52 -0800230 }
231
Julia Reynolds3aedded2017-03-31 14:42:09 -0400232 public interface OnAppSettingsClickListener {
233 void onClick(View v, Intent intent);
234 }
235
Rohan Shah524cf7b2018-03-15 14:40:02 -0700236 @VisibleForTesting
237 void bindNotification(
238 final PackageManager pm,
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500239 final INotificationManager iNotificationManager,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500240 final String pkg,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500241 final NotificationChannel notificationChannel,
Evan Laird47dc4542019-04-24 15:10:52 -0400242 final Set<NotificationChannel> uniqueChannelsInRow,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400243 final StatusBarNotification sbn,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500244 final CheckSaveListener checkSaveListener,
245 final OnSettingsClickListener onSettingsClick,
246 final OnAppSettingsClickListener onAppSettingsClick,
Julia Reynolds35765d82018-08-17 11:39:19 -0400247 boolean isDeviceProvisioned,
Gus Prevas9abc5062018-10-31 16:11:04 -0400248 boolean isNonblockable,
Gus Prevascaed15c2019-01-18 14:19:51 -0500249 int importance,
250 boolean wasShownHighPriority)
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500251 throws RemoteException {
Rohan Shahca0447e2018-03-30 15:18:27 -0700252 bindNotification(pm, iNotificationManager, pkg, notificationChannel,
Evan Laird47dc4542019-04-24 15:10:52 -0400253 uniqueChannelsInRow, sbn, checkSaveListener, onSettingsClick,
Julia Reynolds35765d82018-08-17 11:39:19 -0400254 onAppSettingsClick, isDeviceProvisioned, isNonblockable,
Julia Reynolds52a27372019-04-03 11:00:17 -0400255 false /* isBlockingHelper */,
Gus Prevascaed15c2019-01-18 14:19:51 -0500256 importance, wasShownHighPriority);
Julia Reynolds0ef7d842018-01-24 17:50:31 -0500257 }
258
Rohan Shah524cf7b2018-03-15 14:40:02 -0700259 public void bindNotification(
260 PackageManager pm,
261 INotificationManager iNotificationManager,
262 String pkg,
263 NotificationChannel notificationChannel,
Evan Laird47dc4542019-04-24 15:10:52 -0400264 Set<NotificationChannel> uniqueChannelsInRow,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700265 StatusBarNotification sbn,
266 CheckSaveListener checkSaveListener,
267 OnSettingsClickListener onSettingsClick,
268 OnAppSettingsClickListener onAppSettingsClick,
Julia Reynolds35765d82018-08-17 11:39:19 -0400269 boolean isDeviceProvisioned,
Rohan Shah63411fc2018-03-28 19:05:52 -0700270 boolean isNonblockable,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700271 boolean isForBlockingHelper,
Gus Prevascaed15c2019-01-18 14:19:51 -0500272 int importance,
273 boolean wasShownHighPriority)
Rohan Shah524cf7b2018-03-15 14:40:02 -0700274 throws RemoteException {
Mady Mellor87d79452017-01-10 11:52:52 -0800275 mINotificationManager = iNotificationManager;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700276 mMetricsLogger = Dependency.get(MetricsLogger.class);
Evan Laird47dc4542019-04-24 15:10:52 -0400277 mChannelEditorDialogController = Dependency.get(ChannelEditorDialogController.class);
Rohan Shahca0447e2018-03-30 15:18:27 -0700278 mPackageName = pkg;
Evan Laird47dc4542019-04-24 15:10:52 -0400279 mUniqueChannelsInRow = uniqueChannelsInRow;
280 mNumUniqueChannelsInRow = uniqueChannelsInRow.size();
Julia Reynolds3aedded2017-03-31 14:42:09 -0400281 mSbn = sbn;
282 mPm = pm;
283 mAppSettingsClickListener = onAppSettingsClick;
Rohan Shahca0447e2018-03-30 15:18:27 -0700284 mAppName = mPackageName;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500285 mCheckSaveListener = checkSaveListener;
286 mOnSettingsClickListener = onSettingsClick;
287 mSingleNotificationChannel = notificationChannel;
Gus Prevascaed15c2019-01-18 14:19:51 -0500288 mStartingChannelImportance = mSingleNotificationChannel.getImportance();
289 mWasShownHighPriority = wasShownHighPriority;
Rohan Shah63411fc2018-03-28 19:05:52 -0700290 mIsNonblockable = isNonblockable;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700291 mIsForBlockingHelper = isForBlockingHelper;
Rohan Shahdbd64e72018-03-28 14:46:50 -0700292 mAppUid = mSbn.getUid();
Julia Reynolds268647a2018-10-25 16:54:27 -0400293 mDelegatePkg = mSbn.getOpPkg();
Julia Reynolds35765d82018-08-17 11:39:19 -0400294 mIsDeviceProvisioned = isDeviceProvisioned;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500295
Julia Reynolds8728d002019-04-19 15:46:13 -0400296 mSelectedBackground = mContext.getDrawable(R.drawable.button_border_selected);
297 mUnselectedBackground = mContext.getDrawable(R.drawable.button_border_unselected);
Julia Reynolds52a27372019-04-03 11:00:17 -0400298
Julia Reynolds437cdb12018-01-03 12:27:24 -0500299 int numTotalChannels = mINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400300 pkg, mAppUid, false /* includeDeleted */);
Rohan Shahca0447e2018-03-30 15:18:27 -0700301 if (mNumUniqueChannelsInRow == 0) {
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400302 throw new IllegalArgumentException("bindNotification requires at least one channel");
303 } else {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500304 // Special behavior for the Default channel if no other channels have been defined.
Rohan Shahca0447e2018-03-30 15:18:27 -0700305 mIsSingleDefaultChannel = mNumUniqueChannelsInRow == 1
Rohan Shahdbd64e72018-03-28 14:46:50 -0700306 && mSingleNotificationChannel.getId().equals(
307 NotificationChannel.DEFAULT_CHANNEL_ID)
308 && numTotalChannels == 1;
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400309 }
310
Julia Reynolds8728d002019-04-19 15:46:13 -0400311 mShowInStatusBar = !mINotificationManager.shouldHideSilentStatusIcons(
312 mContext.getPackageName());
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400313 mShowOnLockscreen = Settings.Secure.getInt(mContext.getContentResolver(),
314 Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, 0) == 1;
Julia Reynolds8728d002019-04-19 15:46:13 -0400315
Julia Reynolds437cdb12018-01-03 12:27:24 -0500316 bindHeader();
Julia Reynolds52a27372019-04-03 11:00:17 -0400317 bindChannelDetails();
318
319 if (mIsForBlockingHelper) {
320 bindBlockingHelper();
321 } else {
322 bindInlineControls();
323 }
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200324
Will Brockmana399f022019-01-30 10:59:36 -0500325 mMetricsLogger.write(notificationControlsLogMaker());
Julia Reynolds437cdb12018-01-03 12:27:24 -0500326 }
327
Julia Reynolds52a27372019-04-03 11:00:17 -0400328 private void bindBlockingHelper() {
329 findViewById(R.id.inline_controls).setVisibility(GONE);
330 findViewById(R.id.blocking_helper).setVisibility(VISIBLE);
331
332 findViewById(R.id.undo).setOnClickListener(mOnUndo);
333
334 View turnOffButton = findViewById(R.id.blocking_helper_turn_off_notifications);
335 turnOffButton.setOnClickListener(getSettingsOnClickListener());
336 turnOffButton.setVisibility(turnOffButton.hasOnClickListeners() ? VISIBLE : GONE);
337
338 TextView keepShowing = findViewById(R.id.keep_showing);
339 keepShowing.setOnClickListener(mOnKeepShowing);
340
341 View deliverSilently = findViewById(R.id.deliver_silently);
342 deliverSilently.setOnClickListener(mOnDeliverSilently);
343 }
344
345 private void bindInlineControls() {
346 findViewById(R.id.inline_controls).setVisibility(VISIBLE);
347 findViewById(R.id.blocking_helper).setVisibility(GONE);
348
349 if (mIsNonblockable) {
350 findViewById(R.id.non_configurable_text).setVisibility(VISIBLE);
351 findViewById(R.id.non_configurable_multichannel_text).setVisibility(GONE);
352 findViewById(R.id.interruptiveness_settings).setVisibility(GONE);
Julia Reynolds8728d002019-04-19 15:46:13 -0400353 ((TextView) findViewById(R.id.done)).setText(R.string.inline_done_button);
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400354 findViewById(R.id.turn_off_notifications).setVisibility(GONE);
Julia Reynolds52a27372019-04-03 11:00:17 -0400355 } else if (mNumUniqueChannelsInRow > 1) {
356 findViewById(R.id.non_configurable_text).setVisibility(GONE);
357 findViewById(R.id.interruptiveness_settings).setVisibility(GONE);
358 findViewById(R.id.non_configurable_multichannel_text).setVisibility(VISIBLE);
359 } else {
360 findViewById(R.id.non_configurable_text).setVisibility(GONE);
361 findViewById(R.id.non_configurable_multichannel_text).setVisibility(GONE);
362 findViewById(R.id.interruptiveness_settings).setVisibility(VISIBLE);
363 }
364
365 View turnOffButton = findViewById(R.id.turn_off_notifications);
Evan Laird47dc4542019-04-24 15:10:52 -0400366 turnOffButton.setOnClickListener(getTurnOffNotificationsClickListener());
Julia Reynolds52a27372019-04-03 11:00:17 -0400367 turnOffButton.setVisibility(turnOffButton.hasOnClickListeners() && !mIsNonblockable
368 ? VISIBLE : GONE);
369
370 View done = findViewById(R.id.done);
371 done.setOnClickListener(mOnDismissSettings);
372
373
Julia Reynolds8728d002019-04-19 15:46:13 -0400374 View silent = findViewById(R.id.silence);
375 View alert = findViewById(R.id.alert);
Julia Reynolds52a27372019-04-03 11:00:17 -0400376 silent.setOnClickListener(mOnSilent);
377 alert.setOnClickListener(mOnAlert);
378
379 if (mWasShownHighPriority) {
380 updateButtons(ACTION_ALERT);
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400381 setImportanceSummary(ACTION_ALERT, false);
Julia Reynolds52a27372019-04-03 11:00:17 -0400382 } else {
383 updateButtons(ACTION_TOGGLE_SILENT);
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400384 setImportanceSummary(ACTION_TOGGLE_SILENT, false);
Julia Reynolds52a27372019-04-03 11:00:17 -0400385 }
386 }
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));
Gus Prevascaed15c2019-01-18 14:19:51 -0500545 }
Mady Mellor87d79452017-01-10 11:52:52 -0800546 }
547
Julia Reynolds52a27372019-04-03 11:00:17 -0400548 private void updateButtons(int blockState) {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400549 View silence = findViewById(R.id.silence);
550 View alert = findViewById(R.id.alert);
Julia Reynolds8728d002019-04-19 15:46:13 -0400551 TextView done = findViewById(R.id.done);
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500552 switch (blockState) {
Julia Reynolds52a27372019-04-03 11:00:17 -0400553 case ACTION_TOGGLE_SILENT:
Julia Reynolds8728d002019-04-19 15:46:13 -0400554 updateButtons(silence, alert);
555 if (mWasShownHighPriority) {
556 done.setText(R.string.inline_ok_button);
557 } else {
558 done.setText(R.string.inline_done_button);
559 }
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500560 break;
561 case ACTION_ALERT:
Julia Reynolds8728d002019-04-19 15:46:13 -0400562 updateButtons(alert, silence);
563 if (mWasShownHighPriority) {
564 done.setText(R.string.inline_done_button);
565 } else {
566 done.setText(R.string.inline_ok_button);
567 }
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500568 break;
569 }
570 }
571
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400572 private void updateButtons(View selected, View unselected) {
Julia Reynolds8728d002019-04-19 15:46:13 -0400573 selected.setBackground(mSelectedBackground);
574 selected.setSelected(true);
Julia Reynolds8728d002019-04-19 15:46:13 -0400575 unselected.setBackground(mUnselectedBackground);
576 unselected.setSelected(false);
Julia Reynolds8728d002019-04-19 15:46:13 -0400577 }
578
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400579 void setImportanceSummary(int blockState, boolean userTriggered) {
580 if (userTriggered) {
581 AutoTransition transition = new AutoTransition();
582 transition.setDuration(BUTTON_ANIM_TIME_MS);
583 TransitionManager.beginDelayedTransition(this, transition);
584 }
Dan Sandler0ed02292019-05-14 19:18:35 -0400585 if (SHOW_BUTTON_SUMMARY) {
586 if (blockState == ACTION_ALERT) {
587 TextView view = findViewById(R.id.alert_summary);
588 view.setVisibility(VISIBLE);
589 findViewById(R.id.silence_summary).setVisibility(GONE);
590 view.setText(R.string.notification_channel_summary_default);
Julia Reynolds8728d002019-04-19 15:46:13 -0400591 } else {
Dan Sandler0ed02292019-05-14 19:18:35 -0400592 TextView view = findViewById(R.id.silence_summary);
593 view.setVisibility(VISIBLE);
594 findViewById(R.id.alert_summary).setVisibility(GONE);
595 if (mShowInStatusBar) {
596 if (mShowOnLockscreen) {
597 view.setText(R.string.notification_channel_summary_low_status_lock);
598 } else {
599 view.setText(R.string.notification_channel_summary_low_status);
600 }
601 } else if (mShowOnLockscreen) {
602 view.setText(R.string.notification_channel_summary_low_lock);
603 } else {
604 view.setText(R.string.notification_channel_summary_low);
605 }
Julia Reynolds8728d002019-04-19 15:46:13 -0400606 }
607 }
608 }
609
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500610 private void saveImportanceAndExitReason(@NotificationInfoAction int action) {
611 switch (action) {
612 case ACTION_UNDO:
613 mChosenImportance = mStartingChannelImportance;
614 break;
615 case ACTION_DELIVER_SILENTLY:
616 mExitReason = NotificationCounters.BLOCKING_HELPER_DELIVER_SILENTLY;
Julia Reynolds8728d002019-04-19 15:46:13 -0400617 mChosenImportance = mWasShownHighPriority
618 ? IMPORTANCE_LOW : mStartingChannelImportance;
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500619 break;
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500620 default:
621 throw new IllegalArgumentException();
622 }
623 }
624
Julia Reynolds52a27372019-04-03 11:00:17 -0400625 // only used for blocking helper
Gus Prevas894d9152018-11-12 13:51:40 -0500626 private void swapContent(@NotificationInfoAction int action, boolean animate) {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500627 if (mExpandAnimation != null) {
628 mExpandAnimation.cancel();
629 }
Mady Mellor87d79452017-01-10 11:52:52 -0800630
Julia Reynolds52a27372019-04-03 11:00:17 -0400631 View blockingHelper = findViewById(R.id.blocking_helper);
Julia Reynoldse0341482018-03-08 14:42:50 -0500632 ViewGroup confirmation = findViewById(R.id.confirmation);
633 TextView confirmationText = findViewById(R.id.confirmation_text);
Julia Reynoldse0341482018-03-08 14:42:50 -0500634
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500635 saveImportanceAndExitReason(action);
636
Gus Prevas9abc5062018-10-31 16:11:04 -0400637 switch (action) {
Gus Prevas894d9152018-11-12 13:51:40 -0500638 case ACTION_UNDO:
Gus Prevas9abc5062018-10-31 16:11:04 -0400639 break;
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500640 case ACTION_DELIVER_SILENTLY:
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500641 confirmationText.setText(R.string.notification_channel_silenced);
642 break;
Gus Prevas9abc5062018-10-31 16:11:04 -0400643 default:
644 throw new IllegalArgumentException();
Julia Reynolds437cdb12018-01-03 12:27:24 -0500645 }
646
Gus Prevas894d9152018-11-12 13:51:40 -0500647 boolean isUndo = action == ACTION_UNDO;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500648
Julia Reynolds52a27372019-04-03 11:00:17 -0400649 blockingHelper.setVisibility(isUndo ? VISIBLE : GONE);
650 findViewById(R.id.channel_info).setVisibility(isUndo ? VISIBLE : GONE);
651 findViewById(R.id.header).setVisibility(isUndo ? VISIBLE : GONE);
Gus Prevas9abc5062018-10-31 16:11:04 -0400652 confirmation.setVisibility(isUndo ? GONE : VISIBLE);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500653
Gus Prevas894d9152018-11-12 13:51:40 -0500654 if (animate) {
Julia Reynolds52a27372019-04-03 11:00:17 -0400655 ObjectAnimator promptAnim = ObjectAnimator.ofFloat(blockingHelper, View.ALPHA,
656 blockingHelper.getAlpha(), isUndo ? 1f : 0f);
Gus Prevas894d9152018-11-12 13:51:40 -0500657 promptAnim.setInterpolator(isUndo ? Interpolators.ALPHA_IN : Interpolators.ALPHA_OUT);
658 ObjectAnimator confirmAnim = ObjectAnimator.ofFloat(confirmation, View.ALPHA,
659 confirmation.getAlpha(), isUndo ? 0f : 1f);
660 confirmAnim.setInterpolator(isUndo ? Interpolators.ALPHA_OUT : Interpolators.ALPHA_IN);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500661
Gus Prevas894d9152018-11-12 13:51:40 -0500662 mExpandAnimation = new AnimatorSet();
663 mExpandAnimation.playTogether(promptAnim, confirmAnim);
664 mExpandAnimation.setDuration(150);
665 mExpandAnimation.addListener(new AnimatorListenerAdapter() {
666 boolean mCancelled = false;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500667
Gus Prevas894d9152018-11-12 13:51:40 -0500668 @Override
669 public void onAnimationCancel(Animator animation) {
670 mCancelled = true;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500671 }
Gus Prevas894d9152018-11-12 13:51:40 -0500672
673 @Override
674 public void onAnimationEnd(Animator animation) {
675 if (!mCancelled) {
Julia Reynolds52a27372019-04-03 11:00:17 -0400676 blockingHelper.setVisibility(isUndo ? VISIBLE : GONE);
Gus Prevas894d9152018-11-12 13:51:40 -0500677 confirmation.setVisibility(isUndo ? GONE : VISIBLE);
678 }
679 }
680 });
681 mExpandAnimation.start();
682 }
Rohan Shah142e2da2018-06-14 13:14:18 -0700683
684 // Since we're swapping/update the content, reset the timeout so the UI can't close
685 // immediately after the update.
686 if (mGutsContainer != null) {
687 mGutsContainer.resetFalsingCheck();
688 }
Mady Mellor87d79452017-01-10 11:52:52 -0800689 }
690
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400691 @Override
Gus Prevas9abc5062018-10-31 16:11:04 -0400692 public void onFinishedClosing() {
Gus Prevascaed15c2019-01-18 14:19:51 -0500693 if (mChosenImportance != null) {
694 mStartingChannelImportance = mChosenImportance;
Gus Prevas9abc5062018-10-31 16:11:04 -0400695 }
696 mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED;
697
Julia Reynolds52a27372019-04-03 11:00:17 -0400698 if (mIsForBlockingHelper) {
699 bindBlockingHelper();
700 } else {
701 bindInlineControls();
702 }
Will Brockmana399f022019-01-30 10:59:36 -0500703
704 mMetricsLogger.write(notificationControlsLogMaker().setType(MetricsEvent.TYPE_CLOSE));
Gus Prevas9abc5062018-10-31 16:11:04 -0400705 }
706
707 @Override
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400708 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
709 super.onInitializeAccessibilityEvent(event);
710 if (mGutsContainer != null &&
711 event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
712 if (mGutsContainer.isExposed()) {
713 event.getText().add(mContext.getString(
714 R.string.notification_channel_controls_opened_accessibility, mAppName));
715 } else {
716 event.getText().add(mContext.getString(
717 R.string.notification_channel_controls_closed_accessibility, mAppName));
718 }
719 }
720 }
721
Julia Reynolds3aedded2017-03-31 14:42:09 -0400722 private Intent getAppSettingsIntent(PackageManager pm, String packageName,
723 NotificationChannel channel, int id, String tag) {
724 Intent intent = new Intent(Intent.ACTION_MAIN)
725 .addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES)
726 .setPackage(packageName);
727 final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(
728 intent,
729 PackageManager.MATCH_DEFAULT_ONLY
730 );
731 if (resolveInfos == null || resolveInfos.size() == 0 || resolveInfos.get(0) == null) {
732 return null;
733 }
734 final ActivityInfo activityInfo = resolveInfos.get(0).activityInfo;
735 intent.setClassName(activityInfo.packageName, activityInfo.name);
736 if (channel != null) {
737 intent.putExtra(Notification.EXTRA_CHANNEL_ID, channel.getId());
738 }
739 intent.putExtra(Notification.EXTRA_NOTIFICATION_ID, id);
740 intent.putExtra(Notification.EXTRA_NOTIFICATION_TAG, tag);
741 return intent;
742 }
743
Rohan Shahca0447e2018-03-30 15:18:27 -0700744 /**
745 * Closes the controls and commits the updated importance values (indirectly). If this view is
746 * being used to show the blocking helper, this will immediately dismiss the blocking helper and
747 * commit the updated importance.
748 *
749 * <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 -0400750 * user does not have the ability to undo the action anymore. See
751 * {@link #swapContent(boolean, boolean)} for where undo is handled.
Rohan Shahca0447e2018-03-30 15:18:27 -0700752 */
Rohan Shah524cf7b2018-03-15 14:40:02 -0700753 @VisibleForTesting
754 void closeControls(View v) {
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700755 int[] parentLoc = new int[2];
756 int[] targetLoc = new int[2];
757 mGutsContainer.getLocationOnScreen(parentLoc);
758 v.getLocationOnScreen(targetLoc);
759 final int centerX = v.getWidth() / 2;
760 final int centerY = v.getHeight() / 2;
761 final int x = targetLoc[0] - parentLoc[0] + centerX;
762 final int y = targetLoc[1] - parentLoc[1] + centerY;
763 mGutsContainer.closeControls(x, y, true /* save */, false /* force */);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500764 }
765
Mady Mellor87d79452017-01-10 11:52:52 -0800766 @Override
Mady Mellor95d743c2017-01-10 12:05:27 -0800767 public void setGutsParent(NotificationGuts guts) {
768 mGutsContainer = guts;
Mady Mellor87d79452017-01-10 11:52:52 -0800769 }
770
771 @Override
Mady Mellor434180c2017-02-13 11:29:42 -0800772 public boolean willBeRemoved() {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400773 return false;
Mady Mellor434180c2017-02-13 11:29:42 -0800774 }
775
776 @Override
Lucas Dupin9b08c012018-05-16 19:53:32 -0700777 public boolean shouldBeSaved() {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400778 return mPressedApply;
Lucas Dupin9b08c012018-05-16 19:53:32 -0700779 }
780
781 @Override
Mady Mellor87d79452017-01-10 11:52:52 -0800782 public View getContentView() {
783 return this;
784 }
785
786 @Override
Mady Mellorc2dbe492017-03-30 13:22:03 -0700787 public boolean handleCloseControls(boolean save, boolean force) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500788 // Save regardless of the importance so we can lock the importance field if the user wants
789 // to keep getting notifications
790 if (save) {
Rohan Shahca0447e2018-03-30 15:18:27 -0700791 saveImportance();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500792 }
Rohan Shahdd588c72018-05-09 20:32:15 -0700793 logBlockingHelperCounter(mExitReason);
Mady Mellor87d79452017-01-10 11:52:52 -0800794 return false;
795 }
Mady Mellore09fb702017-03-30 13:23:29 -0700796
797 @Override
798 public int getActualHeight() {
799 return getHeight();
800 }
Rohan Shahca0447e2018-03-30 15:18:27 -0700801
Gus Prevas533836a2018-09-24 17:15:32 -0400802 @VisibleForTesting
803 public boolean isAnimating() {
804 return mExpandAnimation != null && mExpandAnimation.isRunning();
805 }
806
Rohan Shahca0447e2018-03-30 15:18:27 -0700807 /**
808 * Runnable to either update the given channel (with a new importance value) or, if no channel
809 * is provided, update notifications enabled state for the package.
810 */
811 private static class UpdateImportanceRunnable implements Runnable {
812 private final INotificationManager mINotificationManager;
813 private final String mPackageName;
814 private final int mAppUid;
815 private final @Nullable NotificationChannel mChannelToUpdate;
816 private final int mCurrentImportance;
817 private final int mNewImportance;
818
819
820 public UpdateImportanceRunnable(INotificationManager notificationManager,
821 String packageName, int appUid, @Nullable NotificationChannel channelToUpdate,
822 int currentImportance, int newImportance) {
823 mINotificationManager = notificationManager;
824 mPackageName = packageName;
825 mAppUid = appUid;
826 mChannelToUpdate = channelToUpdate;
827 mCurrentImportance = currentImportance;
828 mNewImportance = newImportance;
829 }
830
831 @Override
832 public void run() {
833 try {
834 if (mChannelToUpdate != null) {
835 mChannelToUpdate.setImportance(mNewImportance);
836 mChannelToUpdate.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
837 mINotificationManager.updateNotificationChannelForPackage(
838 mPackageName, mAppUid, mChannelToUpdate);
839 } else {
840 // For notifications with more than one channel, update notification enabled
841 // state. If the importance was lowered, we disable notifications.
Rohan Shah590e1b22018-04-10 23:48:47 -0400842 mINotificationManager.setNotificationsEnabledWithImportanceLockForPackage(
Rohan Shahca0447e2018-03-30 15:18:27 -0700843 mPackageName, mAppUid, mNewImportance >= mCurrentImportance);
844 }
845 } catch (RemoteException e) {
846 Log.e(TAG, "Unable to update notification importance", e);
847 }
848 }
849 }
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200850
Will Brockmana399f022019-01-30 10:59:36 -0500851 /**
852 * Returns a LogMaker with all available notification information.
853 * Caller should set category, type, and maybe subtype, before passing it to mMetricsLogger.
854 * @return LogMaker
855 */
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200856 private LogMaker getLogMaker() {
Will Brockmana399f022019-01-30 10:59:36 -0500857 // The constructor requires a category, so also do it in the other branch for consistency.
858 return mSbn == null ? new LogMaker(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
859 : mSbn.getLogMaker().setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER);
860 }
861
862 /**
863 * Returns an initialized LogMaker for logging importance changes.
864 * The caller may override the type before passing it to mMetricsLogger.
865 * @return LogMaker
866 */
867 private LogMaker importanceChangeLogMaker() {
868 Integer chosenImportance =
869 mChosenImportance != null ? mChosenImportance : mStartingChannelImportance;
870 return getLogMaker().setCategory(MetricsEvent.ACTION_SAVE_IMPORTANCE)
871 .setType(MetricsEvent.TYPE_ACTION)
872 .setSubtype(chosenImportance - mStartingChannelImportance);
873 }
874
875 /**
876 * Returns an initialized LogMaker for logging open/close of the info display.
877 * The caller may override the type before passing it to mMetricsLogger.
878 * @return LogMaker
879 */
880 private LogMaker notificationControlsLogMaker() {
881 return getLogMaker().setCategory(MetricsEvent.ACTION_NOTE_CONTROLS)
882 .setType(MetricsEvent.TYPE_OPEN)
883 .setSubtype(mIsForBlockingHelper ? MetricsEvent.BLOCKING_HELPER_DISPLAY
884 : MetricsEvent.BLOCKING_HELPER_UNKNOWN);
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200885 }
Mady Mellor87d79452017-01-10 11:52:52 -0800886}