blob: a131ebef77dbb155693719d5e50020857568963b [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
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 Reynolds3aedded2017-03-31 14:42:09 -040043import android.service.notification.StatusBarNotification;
44import android.text.TextUtils;
Ned Burns369f1472019-05-22 21:07:26 -040045import android.transition.ChangeBounds;
46import android.transition.Fade;
Julia Reynolds4d1dd792019-04-29 11:48:29 -040047import android.transition.TransitionManager;
Ned Burns369f1472019-05-22 21:07:26 -040048import android.transition.TransitionSet;
Mady Mellor87d79452017-01-10 11:52:52 -080049import android.util.AttributeSet;
Rohan Shahca0447e2018-03-30 15:18:27 -070050import android.util.Log;
Mady Mellor87d79452017-01-10 11:52:52 -080051import android.view.View;
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;
Mady Mellor87d79452017-01-10 11:52:52 -080061import com.android.systemui.R;
Ned Burns9512e0c2019-05-30 19:36:04 -040062import com.android.systemui.statusbar.notification.VisualStabilityManager;
Mady Mellor53162c12019-10-22 17:12:59 -070063import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor87d79452017-01-10 11:52:52 -080064
Ned Burns369f1472019-05-22 21:07:26 -040065import java.lang.annotation.Retention;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050066import java.util.List;
Evan Laird47dc4542019-04-24 15:10:52 -040067import java.util.Set;
Mady Mellor87d79452017-01-10 11:52:52 -080068
69/**
Julia Reynolds143441a2020-03-03 08:57:42 -050070 * The guts of a notification revealed when performing a long press.
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";
Selim Cineka5a4ee42020-05-21 15:38:32 -070074 private int mActualHeight;
Mady Mellor87d79452017-01-10 11:52:52 -080075
Gus Prevas894d9152018-11-12 13:51:40 -050076 @IntDef(prefix = { "ACTION_" }, value = {
77 ACTION_NONE,
Julia Reynolds143441a2020-03-03 08:57:42 -050078 ACTION_TOGGLE_ALERT,
Gus Prevas894d9152018-11-12 13:51:40 -050079 ACTION_TOGGLE_SILENT,
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;
Julia Reynolds52a27372019-04-03 11:00:17 -040085 // standard controls
Nadia Benbernoub66516c2019-03-06 11:01:53 -050086 static final int ACTION_TOGGLE_SILENT = 2;
Julia Reynolds52a27372019-04-03 11:00:17 -040087 // standard controls
Julia Reynolds143441a2020-03-03 08:57:42 -050088 private static final int ACTION_TOGGLE_ALERT = 5;
Gus Prevas9abc5062018-10-31 16:11:04 -040089
Ned Burns369f1472019-05-22 21:07:26 -040090 private TextView mPriorityDescriptionView;
91 private TextView mSilentDescriptionView;
Dan Sandler0ed02292019-05-14 19:18:35 -040092
Mady Mellor87d79452017-01-10 11:52:52 -080093 private INotificationManager mINotificationManager;
Julia Reynolds437cdb12018-01-03 12:27:24 -050094 private PackageManager mPm;
Rohan Shahda5dcdd2018-04-27 17:21:50 -070095 private MetricsLogger mMetricsLogger;
Ned Burns9512e0c2019-05-30 19:36:04 -040096 private VisualStabilityManager mVisualStabilityManager;
Evan Laird47dc4542019-04-24 15:10:52 -040097 private ChannelEditorDialogController mChannelEditorDialogController;
Julia Reynolds437cdb12018-01-03 12:27:24 -050098
Rohan Shahca0447e2018-03-30 15:18:27 -070099 private String mPackageName;
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400100 private String mAppName;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500101 private int mAppUid;
Julia Reynolds268647a2018-10-25 16:54:27 -0400102 private String mDelegatePkg;
Rohan Shahca0447e2018-03-30 15:18:27 -0700103 private int mNumUniqueChannelsInRow;
Evan Laird47dc4542019-04-24 15:10:52 -0400104 private Set<NotificationChannel> mUniqueChannelsInRow;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500105 private NotificationChannel mSingleNotificationChannel;
Gus Prevas9abc5062018-10-31 16:11:04 -0400106 private int mStartingChannelImportance;
Gus Prevascaed15c2019-01-18 14:19:51 -0500107 private boolean mWasShownHighPriority;
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400108 private boolean mPressedApply;
Evan Laird03cf3502019-05-31 16:46:48 -0400109 private boolean mPresentingChannelEditorDialog = false;
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400110
Gus Prevascaed15c2019-01-18 14:19:51 -0500111 /**
112 * The last importance level chosen by the user. Null if the user has not chosen an importance
113 * level; non-null once the user takes an action which indicates an explicit preference.
114 */
115 @Nullable private Integer mChosenImportance;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500116 private boolean mIsSingleDefaultChannel;
Rohan Shah63411fc2018-03-28 19:05:52 -0700117 private boolean mIsNonblockable;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500118 private StatusBarNotification mSbn;
Julia Reynolds35765d82018-08-17 11:39:19 -0400119 private boolean mIsDeviceProvisioned;
Mady Mellor87d79452017-01-10 11:52:52 -0800120
Julia Reynolds437cdb12018-01-03 12:27:24 -0500121 private OnSettingsClickListener mOnSettingsClickListener;
Julia Reynolds3aedded2017-03-31 14:42:09 -0400122 private OnAppSettingsClickListener mAppSettingsClickListener;
Mady Mellor95d743c2017-01-10 12:05:27 -0800123 private NotificationGuts mGutsContainer;
Evan Laird47dc4542019-04-24 15:10:52 -0400124 private Drawable mPkgIcon;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700125
Mady Mellor53162c12019-10-22 17:12:59 -0700126 @VisibleForTesting
127 boolean mSkipPost = false;
128
Julia Reynolds52a27372019-04-03 11:00:17 -0400129 // used by standard ui
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500130 private OnClickListener mOnAlert = v -> {
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500131 mChosenImportance = IMPORTANCE_DEFAULT;
Ned Burns369f1472019-05-22 21:07:26 -0400132 applyAlertingBehavior(BEHAVIOR_ALERTING, true /* userTriggered */);
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500133 };
134
Julia Reynolds52a27372019-04-03 11:00:17 -0400135 // used by standard ui
136 private OnClickListener mOnSilent = v -> {
Julia Reynolds52a27372019-04-03 11:00:17 -0400137 mChosenImportance = IMPORTANCE_LOW;
Ned Burns369f1472019-05-22 21:07:26 -0400138 applyAlertingBehavior(BEHAVIOR_SILENT, true /* userTriggered */);
Julia Reynolds52a27372019-04-03 11:00:17 -0400139 };
140
141 // used by standard ui
Nadia Benbernoub66516c2019-03-06 11:01:53 -0500142 private OnClickListener mOnDismissSettings = v -> {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400143 mPressedApply = true;
Dave Mankoffc97a0e62020-06-01 12:13:05 -0400144 mGutsContainer.closeControls(v, true);
Nadia Benbernou3b4a93a2019-02-06 16:16:47 -0500145 };
146
Mady Mellor87d79452017-01-10 11:52:52 -0800147 public NotificationInfo(Context context, AttributeSet attrs) {
148 super(context, attrs);
149 }
150
Ned Burns369f1472019-05-22 21:07:26 -0400151 @Override
152 protected void onFinishInflate() {
153 super.onFinishInflate();
154
155 mPriorityDescriptionView = findViewById(R.id.alert_summary);
156 mSilentDescriptionView = findViewById(R.id.silence_summary);
157 }
158
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400159 // Specify a CheckSaveListener to override when/if the user's changes are committed.
160 public interface CheckSaveListener {
161 // Invoked when importance has changed and the NotificationInfo wants to try to save it.
162 // Listener should run saveImportance unless the change should be canceled.
Eliot Courtney47098cb2017-10-18 17:30:30 +0900163 void checkSave(Runnable saveImportance, StatusBarNotification sbn);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400164 }
165
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500166 public interface OnSettingsClickListener {
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500167 void onClick(View v, NotificationChannel channel, int appUid);
Mady Mellor87d79452017-01-10 11:52:52 -0800168 }
169
Julia Reynolds3aedded2017-03-31 14:42:09 -0400170 public interface OnAppSettingsClickListener {
171 void onClick(View v, Intent intent);
172 }
173
Rohan Shah524cf7b2018-03-15 14:40:02 -0700174 public void bindNotification(
175 PackageManager pm,
176 INotificationManager iNotificationManager,
Ned Burns9512e0c2019-05-30 19:36:04 -0400177 VisualStabilityManager visualStabilityManager,
Evan Laird18bd6e62019-08-30 16:39:25 -0400178 ChannelEditorDialogController channelEditorDialogController,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700179 String pkg,
180 NotificationChannel notificationChannel,
Evan Laird47dc4542019-04-24 15:10:52 -0400181 Set<NotificationChannel> uniqueChannelsInRow,
Mady Mellor53162c12019-10-22 17:12:59 -0700182 NotificationEntry entry,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700183 OnSettingsClickListener onSettingsClick,
184 OnAppSettingsClickListener onAppSettingsClick,
Julia Reynolds35765d82018-08-17 11:39:19 -0400185 boolean isDeviceProvisioned,
Rohan Shah63411fc2018-03-28 19:05:52 -0700186 boolean isNonblockable,
Gus Prevascaed15c2019-01-18 14:19:51 -0500187 boolean wasShownHighPriority)
Rohan Shah524cf7b2018-03-15 14:40:02 -0700188 throws RemoteException {
Mady Mellor87d79452017-01-10 11:52:52 -0800189 mINotificationManager = iNotificationManager;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700190 mMetricsLogger = Dependency.get(MetricsLogger.class);
Ned Burns9512e0c2019-05-30 19:36:04 -0400191 mVisualStabilityManager = visualStabilityManager;
Evan Laird18bd6e62019-08-30 16:39:25 -0400192 mChannelEditorDialogController = channelEditorDialogController;
Rohan Shahca0447e2018-03-30 15:18:27 -0700193 mPackageName = pkg;
Evan Laird47dc4542019-04-24 15:10:52 -0400194 mUniqueChannelsInRow = uniqueChannelsInRow;
195 mNumUniqueChannelsInRow = uniqueChannelsInRow.size();
Mady Mellor53162c12019-10-22 17:12:59 -0700196 mSbn = entry.getSbn();
Julia Reynolds3aedded2017-03-31 14:42:09 -0400197 mPm = pm;
198 mAppSettingsClickListener = onAppSettingsClick;
Rohan Shahca0447e2018-03-30 15:18:27 -0700199 mAppName = mPackageName;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500200 mOnSettingsClickListener = onSettingsClick;
201 mSingleNotificationChannel = notificationChannel;
Gus Prevascaed15c2019-01-18 14:19:51 -0500202 mStartingChannelImportance = mSingleNotificationChannel.getImportance();
203 mWasShownHighPriority = wasShownHighPriority;
Rohan Shah63411fc2018-03-28 19:05:52 -0700204 mIsNonblockable = isNonblockable;
Rohan Shahdbd64e72018-03-28 14:46:50 -0700205 mAppUid = mSbn.getUid();
Julia Reynolds268647a2018-10-25 16:54:27 -0400206 mDelegatePkg = mSbn.getOpPkg();
Julia Reynolds35765d82018-08-17 11:39:19 -0400207 mIsDeviceProvisioned = isDeviceProvisioned;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500208
Julia Reynolds437cdb12018-01-03 12:27:24 -0500209 int numTotalChannels = mINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400210 pkg, mAppUid, false /* includeDeleted */);
Rohan Shahca0447e2018-03-30 15:18:27 -0700211 if (mNumUniqueChannelsInRow == 0) {
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400212 throw new IllegalArgumentException("bindNotification requires at least one channel");
213 } else {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500214 // Special behavior for the Default channel if no other channels have been defined.
Rohan Shahca0447e2018-03-30 15:18:27 -0700215 mIsSingleDefaultChannel = mNumUniqueChannelsInRow == 1
Rohan Shahdbd64e72018-03-28 14:46:50 -0700216 && mSingleNotificationChannel.getId().equals(
217 NotificationChannel.DEFAULT_CHANNEL_ID)
218 && numTotalChannels == 1;
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400219 }
220
Julia Reynolds437cdb12018-01-03 12:27:24 -0500221 bindHeader();
Julia Reynolds52a27372019-04-03 11:00:17 -0400222 bindChannelDetails();
223
Julia Reynolds143441a2020-03-03 08:57:42 -0500224 bindInlineControls();
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200225
Will Brockmana399f022019-01-30 10:59:36 -0500226 mMetricsLogger.write(notificationControlsLogMaker());
Julia Reynolds437cdb12018-01-03 12:27:24 -0500227 }
228
Julia Reynolds52a27372019-04-03 11:00:17 -0400229 private void bindInlineControls() {
Julia Reynolds52a27372019-04-03 11:00:17 -0400230 if (mIsNonblockable) {
231 findViewById(R.id.non_configurable_text).setVisibility(VISIBLE);
232 findViewById(R.id.non_configurable_multichannel_text).setVisibility(GONE);
233 findViewById(R.id.interruptiveness_settings).setVisibility(GONE);
Julia Reynolds8728d002019-04-19 15:46:13 -0400234 ((TextView) findViewById(R.id.done)).setText(R.string.inline_done_button);
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400235 findViewById(R.id.turn_off_notifications).setVisibility(GONE);
Julia Reynolds52a27372019-04-03 11:00:17 -0400236 } else if (mNumUniqueChannelsInRow > 1) {
237 findViewById(R.id.non_configurable_text).setVisibility(GONE);
238 findViewById(R.id.interruptiveness_settings).setVisibility(GONE);
239 findViewById(R.id.non_configurable_multichannel_text).setVisibility(VISIBLE);
240 } else {
241 findViewById(R.id.non_configurable_text).setVisibility(GONE);
242 findViewById(R.id.non_configurable_multichannel_text).setVisibility(GONE);
243 findViewById(R.id.interruptiveness_settings).setVisibility(VISIBLE);
244 }
245
246 View turnOffButton = findViewById(R.id.turn_off_notifications);
Evan Laird47dc4542019-04-24 15:10:52 -0400247 turnOffButton.setOnClickListener(getTurnOffNotificationsClickListener());
Julia Reynolds52a27372019-04-03 11:00:17 -0400248 turnOffButton.setVisibility(turnOffButton.hasOnClickListeners() && !mIsNonblockable
249 ? VISIBLE : GONE);
250
251 View done = findViewById(R.id.done);
252 done.setOnClickListener(mOnDismissSettings);
Dave Mankoffc97a0e62020-06-01 12:13:05 -0400253 done.setAccessibilityDelegate(mGutsContainer.getAccessibilityDelegate());
Julia Reynolds52a27372019-04-03 11:00:17 -0400254
Julia Reynolds8728d002019-04-19 15:46:13 -0400255 View silent = findViewById(R.id.silence);
256 View alert = findViewById(R.id.alert);
Julia Reynolds52a27372019-04-03 11:00:17 -0400257 silent.setOnClickListener(mOnSilent);
258 alert.setOnClickListener(mOnAlert);
259
Julia Reynoldsc3e922b2020-01-16 16:12:56 -0500260 int behavior = mWasShownHighPriority
Mady Mellor53162c12019-10-22 17:12:59 -0700261 ? BEHAVIOR_ALERTING
262 : BEHAVIOR_SILENT;
263 applyAlertingBehavior(behavior, false /* userTriggered */);
Julia Reynolds52a27372019-04-03 11:00:17 -0400264 }
265
266 private void bindHeader() {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500267 // Package name
Evan Laird47dc4542019-04-24 15:10:52 -0400268 mPkgIcon = null;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500269 ApplicationInfo info;
270 try {
Rohan Shahca0447e2018-03-30 15:18:27 -0700271 info = mPm.getApplicationInfo(
272 mPackageName,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500273 PackageManager.MATCH_UNINSTALLED_PACKAGES
274 | PackageManager.MATCH_DISABLED_COMPONENTS
275 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
276 | PackageManager.MATCH_DIRECT_BOOT_AWARE);
277 if (info != null) {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500278 mAppName = String.valueOf(mPm.getApplicationLabel(info));
Evan Laird47dc4542019-04-24 15:10:52 -0400279 mPkgIcon = mPm.getApplicationIcon(info);
Geoffrey Pitschee8c81e2017-03-23 11:38:56 -0400280 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500281 } catch (PackageManager.NameNotFoundException e) {
282 // app is gone, just show package name and generic icon
Evan Laird47dc4542019-04-24 15:10:52 -0400283 mPkgIcon = mPm.getDefaultActivityIcon();
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500284 }
Julia Reynolds143441a2020-03-03 08:57:42 -0500285 ((ImageView) findViewById(R.id.pkg_icon)).setImageDrawable(mPkgIcon);
286 ((TextView) findViewById(R.id.pkg_name)).setText(mAppName);
Mady Mellor87d79452017-01-10 11:52:52 -0800287
Julia Reynolds268647a2018-10-25 16:54:27 -0400288 // Delegate
289 bindDelegate();
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000290
Julia Reynolds52a27372019-04-03 11:00:17 -0400291 // Set up app settings link (i.e. Customize)
292 View settingsLinkView = findViewById(R.id.app_settings);
293 Intent settingsIntent = getAppSettingsIntent(mPm, mPackageName,
294 mSingleNotificationChannel,
295 mSbn.getId(), mSbn.getTag());
296 if (settingsIntent != null
297 && !TextUtils.isEmpty(mSbn.getNotification().getSettingsText())) {
298 settingsLinkView.setVisibility(VISIBLE);
299 settingsLinkView.setOnClickListener((View view) -> {
300 mAppSettingsClickListener.onClick(view, settingsIntent);
301 });
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000302 } else {
Julia Reynolds52a27372019-04-03 11:00:17 -0400303 settingsLinkView.setVisibility(View.GONE);
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000304 }
Julia Reynolds52a27372019-04-03 11:00:17 -0400305
306 // System Settings button.
307 final View settingsButton = findViewById(R.id.info);
308 settingsButton.setOnClickListener(getSettingsOnClickListener());
309 settingsButton.setVisibility(settingsButton.hasOnClickListeners() ? VISIBLE : GONE);
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000310 }
311
Julia Reynolds52a27372019-04-03 11:00:17 -0400312 private OnClickListener getSettingsOnClickListener() {
313 if (mAppUid >= 0 && mOnSettingsClickListener != null && mIsDeviceProvisioned) {
314 final int appUidF = mAppUid;
315 return ((View view) -> {
Julia Reynolds52a27372019-04-03 11:00:17 -0400316 mOnSettingsClickListener.onClick(view,
317 mNumUniqueChannelsInRow > 1 ? null : mSingleNotificationChannel,
318 appUidF);
319 });
320 }
321 return null;
322 }
323
Evan Laird47dc4542019-04-24 15:10:52 -0400324 private OnClickListener getTurnOffNotificationsClickListener() {
325 return ((View view) -> {
Evan Laird03cf3502019-05-31 16:46:48 -0400326 if (!mPresentingChannelEditorDialog && mChannelEditorDialogController != null) {
327 mPresentingChannelEditorDialog = true;
328
Evan Laird47dc4542019-04-24 15:10:52 -0400329 mChannelEditorDialogController.prepareDialogForApp(mAppName, mPackageName, mAppUid,
330 mUniqueChannelsInRow, mPkgIcon, mOnSettingsClickListener);
Evan Laird03cf3502019-05-31 16:46:48 -0400331 mChannelEditorDialogController.setOnFinishListener(() -> {
332 mPresentingChannelEditorDialog = false;
Dave Mankoffc97a0e62020-06-01 12:13:05 -0400333 mGutsContainer.closeControls(this, false);
Evan Laird03cf3502019-05-31 16:46:48 -0400334 });
Evan Laird47dc4542019-04-24 15:10:52 -0400335 mChannelEditorDialogController.show();
336 }
337 });
338 }
339
Julia Reynolds52a27372019-04-03 11:00:17 -0400340 private void bindChannelDetails() throws RemoteException {
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000341 bindName();
Julia Reynolds268647a2018-10-25 16:54:27 -0400342 bindGroup();
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000343 }
344
345 private void bindName() {
346 final TextView channelName = findViewById(R.id.channel_name);
347 if (mIsSingleDefaultChannel || mNumUniqueChannelsInRow > 1) {
348 channelName.setVisibility(View.GONE);
349 } else {
350 channelName.setText(mSingleNotificationChannel.getName());
351 }
Julia Reynoldsac98aea2018-10-25 16:54:27 -0400352 }
353
Julia Reynolds268647a2018-10-25 16:54:27 -0400354 private void bindDelegate() {
355 TextView delegateView = findViewById(R.id.delegate_name);
Julia Reynolds268647a2018-10-25 16:54:27 -0400356
357 CharSequence delegatePkg = null;
358 if (!TextUtils.equals(mPackageName, mDelegatePkg)) {
359 // this notification was posted by a delegate!
Julia Reynolds268647a2018-10-25 16:54:27 -0400360 delegateView.setVisibility(View.VISIBLE);
Julia Reynolds268647a2018-10-25 16:54:27 -0400361 } else {
362 delegateView.setVisibility(View.GONE);
Julia Reynolds268647a2018-10-25 16:54:27 -0400363 }
364 }
365
366 private void bindGroup() throws RemoteException {
367 // Set group information if this channel has an associated group.
368 CharSequence groupName = null;
369 if (mSingleNotificationChannel != null && mSingleNotificationChannel.getGroup() != null) {
370 final NotificationChannelGroup notificationChannelGroup =
371 mINotificationManager.getNotificationChannelGroupForPackage(
372 mSingleNotificationChannel.getGroup(), mPackageName, mAppUid);
373 if (notificationChannelGroup != null) {
374 groupName = notificationChannelGroup.getName();
375 }
376 }
377 TextView groupNameView = findViewById(R.id.group_name);
Julia Reynolds268647a2018-10-25 16:54:27 -0400378 if (groupName != null) {
379 groupNameView.setText(groupName);
Julia Reynolds143441a2020-03-03 08:57:42 -0500380 groupNameView.setVisibility(VISIBLE);
Julia Reynolds268647a2018-10-25 16:54:27 -0400381 } else {
Julia Reynolds143441a2020-03-03 08:57:42 -0500382 groupNameView.setVisibility(GONE);
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700383 }
384 }
385
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500386 private void saveImportance() {
Julia Reynolds143441a2020-03-03 08:57:42 -0500387 if (!mIsNonblockable) {
Gus Prevascaed15c2019-01-18 14:19:51 -0500388 if (mChosenImportance == null) {
389 mChosenImportance = mStartingChannelImportance;
390 }
Gus Prevas533836a2018-09-24 17:15:32 -0400391 updateImportance();
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500392 }
Rohan Shahca0447e2018-03-30 15:18:27 -0700393 }
394
395 /**
396 * Commits the updated importance values on the background thread.
397 */
398 private void updateImportance() {
Gus Prevascaed15c2019-01-18 14:19:51 -0500399 if (mChosenImportance != null) {
400 mMetricsLogger.write(importanceChangeLogMaker());
Rohan Shahca0447e2018-03-30 15:18:27 -0700401
Julia Reynolds8728d002019-04-19 15:46:13 -0400402 int newImportance = mChosenImportance;
403 if (mStartingChannelImportance != IMPORTANCE_UNSPECIFIED) {
404 if ((mWasShownHighPriority && mChosenImportance >= IMPORTANCE_DEFAULT)
405 || (!mWasShownHighPriority && mChosenImportance < IMPORTANCE_DEFAULT)) {
406 newImportance = mStartingChannelImportance;
407 }
408 }
409
Gus Prevascaed15c2019-01-18 14:19:51 -0500410 Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
411 bgHandler.post(
412 new UpdateImportanceRunnable(mINotificationManager, mPackageName, mAppUid,
413 mNumUniqueChannelsInRow == 1 ? mSingleNotificationChannel : null,
Julia Reynolds8728d002019-04-19 15:46:13 -0400414 mStartingChannelImportance, newImportance));
Ned Burns9512e0c2019-05-30 19:36:04 -0400415 mVisualStabilityManager.temporarilyAllowReordering();
Gus Prevascaed15c2019-01-18 14:19:51 -0500416 }
Mady Mellor87d79452017-01-10 11:52:52 -0800417 }
418
Mady Mellor53162c12019-10-22 17:12:59 -0700419 @Override
420 public boolean post(Runnable action) {
421 if (mSkipPost) {
422 action.run();
423 return true;
424 } else {
425 return super.post(action);
426 }
427 }
428
Ned Burns369f1472019-05-22 21:07:26 -0400429 private void applyAlertingBehavior(@AlertingBehavior int behavior, boolean userTriggered) {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400430 if (userTriggered) {
Ned Burns369f1472019-05-22 21:07:26 -0400431 TransitionSet transition = new TransitionSet();
432 transition.setOrdering(TransitionSet.ORDERING_TOGETHER);
433 transition.addTransition(new Fade(Fade.OUT))
434 .addTransition(new ChangeBounds())
435 .addTransition(
436 new Fade(Fade.IN)
437 .setStartDelay(150)
438 .setDuration(200)
439 .setInterpolator(FAST_OUT_SLOW_IN));
440 transition.setDuration(350);
441 transition.setInterpolator(FAST_OUT_SLOW_IN);
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400442 TransitionManager.beginDelayedTransition(this, transition);
443 }
Ned Burns369f1472019-05-22 21:07:26 -0400444
445 View alert = findViewById(R.id.alert);
446 View silence = findViewById(R.id.silence);
447
448 switch (behavior) {
449 case BEHAVIOR_ALERTING:
Ned Burns369f1472019-05-22 21:07:26 -0400450 mPriorityDescriptionView.setVisibility(VISIBLE);
451 mSilentDescriptionView.setVisibility(GONE);
Julia Reynoldsdaab21a2019-06-05 14:30:05 -0400452 post(() -> {
453 alert.setSelected(true);
454 silence.setSelected(false);
455 });
Ned Burns369f1472019-05-22 21:07:26 -0400456 break;
Julia Reynoldsdaab21a2019-06-05 14:30:05 -0400457
Mady Mellor53162c12019-10-22 17:12:59 -0700458 case BEHAVIOR_SILENT:
Ned Burns369f1472019-05-22 21:07:26 -0400459 mSilentDescriptionView.setVisibility(VISIBLE);
460 mPriorityDescriptionView.setVisibility(GONE);
Julia Reynoldsdaab21a2019-06-05 14:30:05 -0400461 post(() -> {
462 alert.setSelected(false);
463 silence.setSelected(true);
Mady Mellor53162c12019-10-22 17:12:59 -0700464 });
465 break;
466
Ned Burns369f1472019-05-22 21:07:26 -0400467 default:
468 throw new IllegalArgumentException("Unrecognized alerting behavior: " + behavior);
Julia Reynolds8728d002019-04-19 15:46:13 -0400469 }
Ned Burns369f1472019-05-22 21:07:26 -0400470
471 boolean isAChange = mWasShownHighPriority != (behavior == BEHAVIOR_ALERTING);
472 TextView done = findViewById(R.id.done);
Julia Reynoldsc3e922b2020-01-16 16:12:56 -0500473 done.setText(isAChange
Mady Mellor53162c12019-10-22 17:12:59 -0700474 ? R.string.inline_ok_button
475 : R.string.inline_done_button);
Julia Reynolds8728d002019-04-19 15:46:13 -0400476 }
477
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400478 @Override
Gus Prevas9abc5062018-10-31 16:11:04 -0400479 public void onFinishedClosing() {
Gus Prevascaed15c2019-01-18 14:19:51 -0500480 if (mChosenImportance != null) {
481 mStartingChannelImportance = mChosenImportance;
Gus Prevas9abc5062018-10-31 16:11:04 -0400482 }
Gus Prevas9abc5062018-10-31 16:11:04 -0400483
Julia Reynolds143441a2020-03-03 08:57:42 -0500484 bindInlineControls();
Will Brockmana399f022019-01-30 10:59:36 -0500485
486 mMetricsLogger.write(notificationControlsLogMaker().setType(MetricsEvent.TYPE_CLOSE));
Gus Prevas9abc5062018-10-31 16:11:04 -0400487 }
488
489 @Override
Julia Reynoldsbe371072020-05-28 11:32:01 -0400490 public boolean needsFalsingProtection() {
491 return true;
492 }
493
494 @Override
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400495 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
496 super.onInitializeAccessibilityEvent(event);
497 if (mGutsContainer != null &&
498 event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
499 if (mGutsContainer.isExposed()) {
500 event.getText().add(mContext.getString(
501 R.string.notification_channel_controls_opened_accessibility, mAppName));
502 } else {
503 event.getText().add(mContext.getString(
504 R.string.notification_channel_controls_closed_accessibility, mAppName));
505 }
506 }
507 }
508
Julia Reynolds3aedded2017-03-31 14:42:09 -0400509 private Intent getAppSettingsIntent(PackageManager pm, String packageName,
510 NotificationChannel channel, int id, String tag) {
511 Intent intent = new Intent(Intent.ACTION_MAIN)
512 .addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES)
513 .setPackage(packageName);
514 final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(
515 intent,
516 PackageManager.MATCH_DEFAULT_ONLY
517 );
518 if (resolveInfos == null || resolveInfos.size() == 0 || resolveInfos.get(0) == null) {
519 return null;
520 }
521 final ActivityInfo activityInfo = resolveInfos.get(0).activityInfo;
522 intent.setClassName(activityInfo.packageName, activityInfo.name);
523 if (channel != null) {
524 intent.putExtra(Notification.EXTRA_CHANNEL_ID, channel.getId());
525 }
526 intent.putExtra(Notification.EXTRA_NOTIFICATION_ID, id);
527 intent.putExtra(Notification.EXTRA_NOTIFICATION_TAG, tag);
528 return intent;
529 }
530
Mady Mellor87d79452017-01-10 11:52:52 -0800531 @Override
Mady Mellor95d743c2017-01-10 12:05:27 -0800532 public void setGutsParent(NotificationGuts guts) {
533 mGutsContainer = guts;
Mady Mellor87d79452017-01-10 11:52:52 -0800534 }
535
536 @Override
Mady Mellor434180c2017-02-13 11:29:42 -0800537 public boolean willBeRemoved() {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400538 return false;
Mady Mellor434180c2017-02-13 11:29:42 -0800539 }
540
541 @Override
Lucas Dupin9b08c012018-05-16 19:53:32 -0700542 public boolean shouldBeSaved() {
Julia Reynolds4d1dd792019-04-29 11:48:29 -0400543 return mPressedApply;
Lucas Dupin9b08c012018-05-16 19:53:32 -0700544 }
545
546 @Override
Mady Mellor87d79452017-01-10 11:52:52 -0800547 public View getContentView() {
548 return this;
549 }
550
551 @Override
Mady Mellorc2dbe492017-03-30 13:22:03 -0700552 public boolean handleCloseControls(boolean save, boolean force) {
Evan Laird03cf3502019-05-31 16:46:48 -0400553 if (mPresentingChannelEditorDialog && mChannelEditorDialogController != null) {
554 mPresentingChannelEditorDialog = false;
555 // No need for the finish listener because we're closing
556 mChannelEditorDialogController.setOnFinishListener(null);
557 mChannelEditorDialogController.close();
558 }
559
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500560 // Save regardless of the importance so we can lock the importance field if the user wants
561 // to keep getting notifications
562 if (save) {
Rohan Shahca0447e2018-03-30 15:18:27 -0700563 saveImportance();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500564 }
Mady Mellor87d79452017-01-10 11:52:52 -0800565 return false;
566 }
Mady Mellore09fb702017-03-30 13:23:29 -0700567
568 @Override
569 public int getActualHeight() {
Selim Cineka5a4ee42020-05-21 15:38:32 -0700570 // Because we're animating the bounds, getHeight will return the small height at the
571 // beginning of the animation. Instead we'd want it to already return the end value
572 return mActualHeight;
573 }
574
575 @Override
576 protected void onLayout(boolean changed, int l, int t, int r, int b) {
577 super.onLayout(changed, l, t, r, b);
578 mActualHeight = getHeight();
Mady Mellore09fb702017-03-30 13:23:29 -0700579 }
Rohan Shahca0447e2018-03-30 15:18:27 -0700580
Gus Prevas533836a2018-09-24 17:15:32 -0400581 @VisibleForTesting
582 public boolean isAnimating() {
Julia Reynolds143441a2020-03-03 08:57:42 -0500583 return false;
Gus Prevas533836a2018-09-24 17:15:32 -0400584 }
585
Rohan Shahca0447e2018-03-30 15:18:27 -0700586 /**
587 * Runnable to either update the given channel (with a new importance value) or, if no channel
588 * is provided, update notifications enabled state for the package.
589 */
590 private static class UpdateImportanceRunnable implements Runnable {
591 private final INotificationManager mINotificationManager;
592 private final String mPackageName;
593 private final int mAppUid;
594 private final @Nullable NotificationChannel mChannelToUpdate;
595 private final int mCurrentImportance;
596 private final int mNewImportance;
597
598
599 public UpdateImportanceRunnable(INotificationManager notificationManager,
600 String packageName, int appUid, @Nullable NotificationChannel channelToUpdate,
601 int currentImportance, int newImportance) {
602 mINotificationManager = notificationManager;
603 mPackageName = packageName;
604 mAppUid = appUid;
605 mChannelToUpdate = channelToUpdate;
606 mCurrentImportance = currentImportance;
607 mNewImportance = newImportance;
608 }
609
610 @Override
611 public void run() {
612 try {
613 if (mChannelToUpdate != null) {
614 mChannelToUpdate.setImportance(mNewImportance);
615 mChannelToUpdate.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
616 mINotificationManager.updateNotificationChannelForPackage(
617 mPackageName, mAppUid, mChannelToUpdate);
618 } else {
619 // For notifications with more than one channel, update notification enabled
620 // state. If the importance was lowered, we disable notifications.
Rohan Shah590e1b22018-04-10 23:48:47 -0400621 mINotificationManager.setNotificationsEnabledWithImportanceLockForPackage(
Rohan Shahca0447e2018-03-30 15:18:27 -0700622 mPackageName, mAppUid, mNewImportance >= mCurrentImportance);
623 }
624 } catch (RemoteException e) {
625 Log.e(TAG, "Unable to update notification importance", e);
626 }
627 }
628 }
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200629
Will Brockmana399f022019-01-30 10:59:36 -0500630 /**
631 * Returns a LogMaker with all available notification information.
632 * Caller should set category, type, and maybe subtype, before passing it to mMetricsLogger.
633 * @return LogMaker
634 */
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200635 private LogMaker getLogMaker() {
Will Brockmana399f022019-01-30 10:59:36 -0500636 // The constructor requires a category, so also do it in the other branch for consistency.
637 return mSbn == null ? new LogMaker(MetricsEvent.NOTIFICATION_BLOCKING_HELPER)
638 : mSbn.getLogMaker().setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER);
639 }
640
641 /**
642 * Returns an initialized LogMaker for logging importance changes.
643 * The caller may override the type before passing it to mMetricsLogger.
644 * @return LogMaker
645 */
646 private LogMaker importanceChangeLogMaker() {
647 Integer chosenImportance =
648 mChosenImportance != null ? mChosenImportance : mStartingChannelImportance;
649 return getLogMaker().setCategory(MetricsEvent.ACTION_SAVE_IMPORTANCE)
650 .setType(MetricsEvent.TYPE_ACTION)
651 .setSubtype(chosenImportance - mStartingChannelImportance);
652 }
653
654 /**
655 * Returns an initialized LogMaker for logging open/close of the info display.
656 * The caller may override the type before passing it to mMetricsLogger.
657 * @return LogMaker
658 */
659 private LogMaker notificationControlsLogMaker() {
660 return getLogMaker().setCategory(MetricsEvent.ACTION_NOTE_CONTROLS)
661 .setType(MetricsEvent.TYPE_OPEN)
Julia Reynolds143441a2020-03-03 08:57:42 -0500662 .setSubtype(MetricsEvent.BLOCKING_HELPER_UNKNOWN);
Eyal Posenera9cf9c72018-12-18 16:23:54 +0200663 }
Ned Burns369f1472019-05-22 21:07:26 -0400664
665 @Retention(SOURCE)
Julia Reynoldsc3e922b2020-01-16 16:12:56 -0500666 @IntDef({BEHAVIOR_ALERTING, BEHAVIOR_SILENT})
Ned Burns369f1472019-05-22 21:07:26 -0400667 private @interface AlertingBehavior {}
668 private static final int BEHAVIOR_ALERTING = 0;
669 private static final int BEHAVIOR_SILENT = 1;
Mady Mellor87d79452017-01-10 11:52:52 -0800670}