blob: 3e380d1ea192ebcf0c284518f3a660d369bdb979 [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
Julia Reynoldse0341482018-03-08 14:42:50 -050019import static android.app.NotificationManager.IMPORTANCE_MIN;
Julia Reynolds3aedded2017-03-31 14:42:09 -040020import static android.app.NotificationManager.IMPORTANCE_NONE;
21
Julia Reynolds437cdb12018-01-03 12:27:24 -050022import android.animation.Animator;
23import android.animation.AnimatorListenerAdapter;
24import android.animation.AnimatorSet;
25import android.animation.ObjectAnimator;
Rohan Shahca0447e2018-03-30 15:18:27 -070026import android.annotation.Nullable;
Mady Mellor87d79452017-01-10 11:52:52 -080027import android.app.INotificationManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040028import android.app.Notification;
Mady Mellor87d79452017-01-10 11:52:52 -080029import android.app.NotificationChannel;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -050030import android.app.NotificationChannelGroup;
Mady Mellor87d79452017-01-10 11:52:52 -080031import android.content.Context;
Julia Reynolds3aedded2017-03-31 14:42:09 -040032import android.content.Intent;
33import android.content.pm.ActivityInfo;
Mady Mellor87d79452017-01-10 11:52:52 -080034import android.content.pm.ApplicationInfo;
Mady Mellor87d79452017-01-10 11:52:52 -080035import android.content.pm.PackageManager;
Julia Reynolds3aedded2017-03-31 14:42:09 -040036import android.content.pm.ResolveInfo;
Mady Mellor87d79452017-01-10 11:52:52 -080037import android.graphics.drawable.Drawable;
Rohan Shahca0447e2018-03-30 15:18:27 -070038import android.os.Handler;
Mady Mellor87d79452017-01-10 11:52:52 -080039import android.os.RemoteException;
Julia Reynolds3aedded2017-03-31 14:42:09 -040040import android.service.notification.StatusBarNotification;
41import android.text.TextUtils;
Mady Mellor87d79452017-01-10 11:52:52 -080042import android.util.AttributeSet;
Rohan Shahca0447e2018-03-30 15:18:27 -070043import android.util.Log;
Mady Mellor87d79452017-01-10 11:52:52 -080044import android.view.View;
Julia Reynoldse0341482018-03-08 14:42:50 -050045import android.view.ViewGroup;
Geoffrey Pitschd94e7882017-04-06 09:52:11 -040046import android.view.accessibility.AccessibilityEvent;
Mady Mellor87d79452017-01-10 11:52:52 -080047import android.widget.ImageView;
48import android.widget.LinearLayout;
Mady Mellor87d79452017-01-10 11:52:52 -080049import android.widget.TextView;
50
Rohan Shah524cf7b2018-03-15 14:40:02 -070051import com.android.internal.annotations.VisibleForTesting;
Mady Mellor87d79452017-01-10 11:52:52 -080052import com.android.internal.logging.MetricsLogger;
53import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Rohan Shah524cf7b2018-03-15 14:40:02 -070054import com.android.systemui.Dependency;
Julia Reynolds437cdb12018-01-03 12:27:24 -050055import com.android.systemui.Interpolators;
Mady Mellor87d79452017-01-10 11:52:52 -080056import com.android.systemui.R;
Rohan Shah20790b82018-07-02 17:21:04 -070057import com.android.systemui.statusbar.notification.logging.NotificationCounters;
Mady Mellor87d79452017-01-10 11:52:52 -080058
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050059import java.util.List;
Mady Mellor87d79452017-01-10 11:52:52 -080060
61/**
Rohan Shahda5dcdd2018-04-27 17:21:50 -070062 * The guts of a notification revealed when performing a long press. This also houses the blocking
63 * helper affordance that allows a user to keep/stop notifications after swiping one away.
Mady Mellor87d79452017-01-10 11:52:52 -080064 */
Mady Mellor95d743c2017-01-10 12:05:27 -080065public class NotificationInfo extends LinearLayout implements NotificationGuts.GutsContent {
Mady Mellor87d79452017-01-10 11:52:52 -080066 private static final String TAG = "InfoGuts";
67
68 private INotificationManager mINotificationManager;
Julia Reynolds437cdb12018-01-03 12:27:24 -050069 private PackageManager mPm;
Rohan Shahda5dcdd2018-04-27 17:21:50 -070070 private MetricsLogger mMetricsLogger;
Julia Reynolds437cdb12018-01-03 12:27:24 -050071
Rohan Shahca0447e2018-03-30 15:18:27 -070072 private String mPackageName;
Geoffrey Pitschd94e7882017-04-06 09:52:11 -040073 private String mAppName;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050074 private int mAppUid;
Rohan Shahca0447e2018-03-30 15:18:27 -070075 private int mNumUniqueChannelsInRow;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -050076 private NotificationChannel mSingleNotificationChannel;
Mady Mellor87d79452017-01-10 11:52:52 -080077 private int mStartingUserImportance;
Julia Reynolds437cdb12018-01-03 12:27:24 -050078 private int mChosenImportance;
79 private boolean mIsSingleDefaultChannel;
Rohan Shah63411fc2018-03-28 19:05:52 -070080 private boolean mIsNonblockable;
Julia Reynolds437cdb12018-01-03 12:27:24 -050081 private StatusBarNotification mSbn;
82 private AnimatorSet mExpandAnimation;
Julia Reynoldse0341482018-03-08 14:42:50 -050083 private boolean mIsForeground;
Mady Mellor87d79452017-01-10 11:52:52 -080084
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -040085 private CheckSaveListener mCheckSaveListener;
Julia Reynolds437cdb12018-01-03 12:27:24 -050086 private OnSettingsClickListener mOnSettingsClickListener;
Julia Reynolds3aedded2017-03-31 14:42:09 -040087 private OnAppSettingsClickListener mAppSettingsClickListener;
Mady Mellor95d743c2017-01-10 12:05:27 -080088 private NotificationGuts mGutsContainer;
Rohan Shah524cf7b2018-03-15 14:40:02 -070089
Rohan Shahda5dcdd2018-04-27 17:21:50 -070090 /** Whether this view is being shown as part of the blocking helper. */
Rohan Shah524cf7b2018-03-15 14:40:02 -070091 private boolean mIsForBlockingHelper;
Julia Reynolds0ef7d842018-01-24 17:50:31 -050092 private boolean mNegativeUserSentiment;
Mady Mellor87d79452017-01-10 11:52:52 -080093
Rohan Shahdd588c72018-05-09 20:32:15 -070094 /**
95 * String that describes how the user exit or quit out of this view, also used as a counter tag.
96 */
97 private String mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED;
Rohan Shahda5dcdd2018-04-27 17:21:50 -070098
99 private OnClickListener mOnKeepShowing = v -> {
Rohan Shahdd588c72018-05-09 20:32:15 -0700100 mExitReason = NotificationCounters.BLOCKING_HELPER_KEEP_SHOWING;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700101 closeControls(v);
102 };
Julia Reynolds437cdb12018-01-03 12:27:24 -0500103
Rohan Shahca0447e2018-03-30 15:18:27 -0700104 private OnClickListener mOnStopOrMinimizeNotifications = v -> {
Rohan Shahdd588c72018-05-09 20:32:15 -0700105 mExitReason = NotificationCounters.BLOCKING_HELPER_STOP_NOTIFICATIONS;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500106 swapContent(false);
107 };
108
109 private OnClickListener mOnUndo = v -> {
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700110 // Reset exit counter that we'll log and record an undo event separately (not an exit event)
Rohan Shahdd588c72018-05-09 20:32:15 -0700111 mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700112 logBlockingHelperCounter(NotificationCounters.BLOCKING_HELPER_UNDO);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500113 swapContent(true);
114 };
115
Mady Mellor87d79452017-01-10 11:52:52 -0800116 public NotificationInfo(Context context, AttributeSet attrs) {
117 super(context, attrs);
118 }
119
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400120 // Specify a CheckSaveListener to override when/if the user's changes are committed.
121 public interface CheckSaveListener {
122 // Invoked when importance has changed and the NotificationInfo wants to try to save it.
123 // Listener should run saveImportance unless the change should be canceled.
Eliot Courtney47098cb2017-10-18 17:30:30 +0900124 void checkSave(Runnable saveImportance, StatusBarNotification sbn);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400125 }
126
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500127 public interface OnSettingsClickListener {
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500128 void onClick(View v, NotificationChannel channel, int appUid);
Mady Mellor87d79452017-01-10 11:52:52 -0800129 }
130
Julia Reynolds3aedded2017-03-31 14:42:09 -0400131 public interface OnAppSettingsClickListener {
132 void onClick(View v, Intent intent);
133 }
134
Rohan Shah524cf7b2018-03-15 14:40:02 -0700135 @VisibleForTesting
136 void bindNotification(
137 final PackageManager pm,
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500138 final INotificationManager iNotificationManager,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500139 final String pkg,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500140 final NotificationChannel notificationChannel,
Rohan Shahca0447e2018-03-30 15:18:27 -0700141 final int numUniqueChannelsInRow,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400142 final StatusBarNotification sbn,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500143 final CheckSaveListener checkSaveListener,
144 final OnSettingsClickListener onSettingsClick,
145 final OnAppSettingsClickListener onAppSettingsClick,
Rohan Shah63411fc2018-03-28 19:05:52 -0700146 boolean isNonblockable)
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500147 throws RemoteException {
Rohan Shahca0447e2018-03-30 15:18:27 -0700148 bindNotification(pm, iNotificationManager, pkg, notificationChannel,
149 numUniqueChannelsInRow, sbn, checkSaveListener, onSettingsClick,
150 onAppSettingsClick, isNonblockable, false /* isBlockingHelper */,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700151 false /* isUserSentimentNegative */);
Julia Reynolds0ef7d842018-01-24 17:50:31 -0500152 }
153
Rohan Shah524cf7b2018-03-15 14:40:02 -0700154 public void bindNotification(
155 PackageManager pm,
156 INotificationManager iNotificationManager,
157 String pkg,
158 NotificationChannel notificationChannel,
Rohan Shahca0447e2018-03-30 15:18:27 -0700159 int numUniqueChannelsInRow,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700160 StatusBarNotification sbn,
161 CheckSaveListener checkSaveListener,
162 OnSettingsClickListener onSettingsClick,
163 OnAppSettingsClickListener onAppSettingsClick,
Rohan Shah63411fc2018-03-28 19:05:52 -0700164 boolean isNonblockable,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700165 boolean isForBlockingHelper,
166 boolean isUserSentimentNegative)
167 throws RemoteException {
Mady Mellor87d79452017-01-10 11:52:52 -0800168 mINotificationManager = iNotificationManager;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700169 mMetricsLogger = Dependency.get(MetricsLogger.class);
Rohan Shahca0447e2018-03-30 15:18:27 -0700170 mPackageName = pkg;
171 mNumUniqueChannelsInRow = numUniqueChannelsInRow;
Julia Reynolds3aedded2017-03-31 14:42:09 -0400172 mSbn = sbn;
173 mPm = pm;
174 mAppSettingsClickListener = onAppSettingsClick;
Rohan Shahca0447e2018-03-30 15:18:27 -0700175 mAppName = mPackageName;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500176 mCheckSaveListener = checkSaveListener;
177 mOnSettingsClickListener = onSettingsClick;
178 mSingleNotificationChannel = notificationChannel;
179 mStartingUserImportance = mChosenImportance = mSingleNotificationChannel.getImportance();
Rohan Shah524cf7b2018-03-15 14:40:02 -0700180 mNegativeUserSentiment = isUserSentimentNegative;
Rohan Shah63411fc2018-03-28 19:05:52 -0700181 mIsNonblockable = isNonblockable;
Julia Reynoldse0341482018-03-08 14:42:50 -0500182 mIsForeground =
183 (mSbn.getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700184 mIsForBlockingHelper = isForBlockingHelper;
Rohan Shahdbd64e72018-03-28 14:46:50 -0700185 mAppUid = mSbn.getUid();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500186
Julia Reynolds437cdb12018-01-03 12:27:24 -0500187 int numTotalChannels = mINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400188 pkg, mAppUid, false /* includeDeleted */);
Rohan Shahca0447e2018-03-30 15:18:27 -0700189 if (mNumUniqueChannelsInRow == 0) {
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400190 throw new IllegalArgumentException("bindNotification requires at least one channel");
191 } else {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500192 // Special behavior for the Default channel if no other channels have been defined.
Rohan Shahca0447e2018-03-30 15:18:27 -0700193 mIsSingleDefaultChannel = mNumUniqueChannelsInRow == 1
Rohan Shahdbd64e72018-03-28 14:46:50 -0700194 && mSingleNotificationChannel.getId().equals(
195 NotificationChannel.DEFAULT_CHANNEL_ID)
196 && numTotalChannels == 1;
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400197 }
198
Julia Reynolds437cdb12018-01-03 12:27:24 -0500199 bindHeader();
200 bindPrompt();
201 bindButtons();
202 }
203
204 private void bindHeader() throws RemoteException {
205 // Package name
206 Drawable pkgicon = null;
207 ApplicationInfo info;
208 try {
Rohan Shahca0447e2018-03-30 15:18:27 -0700209 info = mPm.getApplicationInfo(
210 mPackageName,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500211 PackageManager.MATCH_UNINSTALLED_PACKAGES
212 | PackageManager.MATCH_DISABLED_COMPONENTS
213 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
214 | PackageManager.MATCH_DIRECT_BOOT_AWARE);
215 if (info != null) {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500216 mAppName = String.valueOf(mPm.getApplicationLabel(info));
217 pkgicon = mPm.getApplicationIcon(info);
Geoffrey Pitschee8c81e2017-03-23 11:38:56 -0400218 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500219 } catch (PackageManager.NameNotFoundException e) {
220 // app is gone, just show package name and generic icon
221 pkgicon = mPm.getDefaultActivityIcon();
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500222 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500223 ((ImageView) findViewById(R.id.pkgicon)).setImageDrawable(pkgicon);
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400224 ((TextView) findViewById(R.id.pkgname)).setText(mAppName);
Mady Mellor87d79452017-01-10 11:52:52 -0800225
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500226 // Set group information if this channel has an associated group.
227 CharSequence groupName = null;
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500228 if (mSingleNotificationChannel != null && mSingleNotificationChannel.getGroup() != null) {
229 final NotificationChannelGroup notificationChannelGroup =
Julia Reynolds437cdb12018-01-03 12:27:24 -0500230 mINotificationManager.getNotificationChannelGroupForPackage(
Rohan Shahca0447e2018-03-30 15:18:27 -0700231 mSingleNotificationChannel.getGroup(), mPackageName, mAppUid);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500232 if (notificationChannelGroup != null) {
233 groupName = notificationChannelGroup.getName();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500234 }
Mady Mellor87d79452017-01-10 11:52:52 -0800235 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500236 TextView groupNameView = findViewById(R.id.group_name);
237 TextView groupDividerView = findViewById(R.id.pkg_group_divider);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500238 if (groupName != null) {
239 groupNameView.setText(groupName);
240 groupNameView.setVisibility(View.VISIBLE);
241 groupDividerView.setVisibility(View.VISIBLE);
242 } else {
243 groupNameView.setVisibility(View.GONE);
244 groupDividerView.setVisibility(View.GONE);
245 }
Mady Mellor87d79452017-01-10 11:52:52 -0800246
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500247 // Settings button.
Julia Reynolds437cdb12018-01-03 12:27:24 -0500248 final View settingsButton = findViewById(R.id.info);
249 if (mAppUid >= 0 && mOnSettingsClickListener != null) {
Geoffrey Pitsch029a3fa2017-03-30 13:01:57 -0400250 settingsButton.setVisibility(View.VISIBLE);
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500251 final int appUidF = mAppUid;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500252 settingsButton.setOnClickListener(
253 (View view) -> {
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700254 logBlockingHelperCounter(
255 NotificationCounters.BLOCKING_HELPER_NOTIF_SETTINGS);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500256 mOnSettingsClickListener.onClick(view,
Rohan Shahca0447e2018-03-30 15:18:27 -0700257 mNumUniqueChannelsInRow > 1 ? null : mSingleNotificationChannel,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500258 appUidF);
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500259 });
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500260 } else {
261 settingsButton.setVisibility(View.GONE);
262 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500263 }
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500264
Julia Reynolds437cdb12018-01-03 12:27:24 -0500265 private void bindPrompt() {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500266 final TextView blockPrompt = findViewById(R.id.block_prompt);
Julia Reynolds0ef7d842018-01-24 17:50:31 -0500267 bindName();
Rohan Shah63411fc2018-03-28 19:05:52 -0700268 if (mIsNonblockable) {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500269 blockPrompt.setText(R.string.notification_unblockable_desc);
270 } else {
Julia Reynolds0ef7d842018-01-24 17:50:31 -0500271 if (mNegativeUserSentiment) {
272 blockPrompt.setText(R.string.inline_blocking_helper);
Rohan Shahca0447e2018-03-30 15:18:27 -0700273 } else if (mIsSingleDefaultChannel || mNumUniqueChannelsInRow > 1) {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500274 blockPrompt.setText(R.string.inline_keep_showing_app);
275 } else {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500276 blockPrompt.setText(R.string.inline_keep_showing);
277 }
278 }
Mady Mellor87d79452017-01-10 11:52:52 -0800279 }
280
Julia Reynolds0ef7d842018-01-24 17:50:31 -0500281 private void bindName() {
282 final TextView channelName = findViewById(R.id.channel_name);
Rohan Shahca0447e2018-03-30 15:18:27 -0700283 if (mIsSingleDefaultChannel || mNumUniqueChannelsInRow > 1) {
Julia Reynolds0ef7d842018-01-24 17:50:31 -0500284 channelName.setVisibility(View.GONE);
285 } else {
286 channelName.setText(mSingleNotificationChannel.getName());
287 }
288 }
289
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700290 @VisibleForTesting
291 void logBlockingHelperCounter(String counterTag) {
292 if (mIsForBlockingHelper) {
293 mMetricsLogger.count(counterTag, 1);
294 }
295 }
296
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400297 private boolean hasImportanceChanged() {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500298 return mSingleNotificationChannel != null && mStartingUserImportance != mChosenImportance;
Mady Mellor87d79452017-01-10 11:52:52 -0800299 }
300
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500301 private void saveImportance() {
Rohan Shahca0447e2018-03-30 15:18:27 -0700302 if (!mIsNonblockable) {
Rohan Shah593e8f12018-05-22 10:06:23 -0700303 // Only go through the lock screen/bouncer if the user hit 'Stop notifications'.
304 // Otherwise, update the importance immediately.
Rohan Shahdd588c72018-05-09 20:32:15 -0700305 if (mCheckSaveListener != null
Rohan Shah593e8f12018-05-22 10:06:23 -0700306 && NotificationCounters.BLOCKING_HELPER_STOP_NOTIFICATIONS.equals(
307 mExitReason)) {
Rohan Shahca0447e2018-03-30 15:18:27 -0700308 mCheckSaveListener.checkSave(this::updateImportance, mSbn);
309 } else {
310 updateImportance();
311 }
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500312 }
Rohan Shahca0447e2018-03-30 15:18:27 -0700313 }
314
315 /**
316 * Commits the updated importance values on the background thread.
317 */
318 private void updateImportance() {
Mady Mellor87d79452017-01-10 11:52:52 -0800319 MetricsLogger.action(mContext, MetricsEvent.ACTION_SAVE_IMPORTANCE,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500320 mChosenImportance - mStartingUserImportance);
Rohan Shahca0447e2018-03-30 15:18:27 -0700321
322 Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
323 bgHandler.post(new UpdateImportanceRunnable(mINotificationManager, mPackageName, mAppUid,
324 mNumUniqueChannelsInRow == 1 ? mSingleNotificationChannel : null,
325 mStartingUserImportance, mChosenImportance));
Mady Mellor87d79452017-01-10 11:52:52 -0800326 }
327
Julia Reynolds437cdb12018-01-03 12:27:24 -0500328 private void bindButtons() {
Rohan Shah524cf7b2018-03-15 14:40:02 -0700329 // Set up stay-in-notification actions
Julia Reynolds437cdb12018-01-03 12:27:24 -0500330 View block = findViewById(R.id.block);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500331 TextView keep = findViewById(R.id.keep);
Julia Reynoldse0341482018-03-08 14:42:50 -0500332 View minimize = findViewById(R.id.minimize);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700333
334 findViewById(R.id.undo).setOnClickListener(mOnUndo);
Rohan Shahca0447e2018-03-30 15:18:27 -0700335 block.setOnClickListener(mOnStopOrMinimizeNotifications);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700336 keep.setOnClickListener(mOnKeepShowing);
Rohan Shahca0447e2018-03-30 15:18:27 -0700337 minimize.setOnClickListener(mOnStopOrMinimizeNotifications);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500338
Rohan Shah63411fc2018-03-28 19:05:52 -0700339 if (mIsNonblockable) {
Rohan Shah53529aa2018-04-12 00:14:50 -0400340 keep.setText(android.R.string.ok);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500341 block.setVisibility(GONE);
Julia Reynoldse0341482018-03-08 14:42:50 -0500342 minimize.setVisibility(GONE);
343 } else if (mIsForeground) {
344 block.setVisibility(GONE);
345 minimize.setVisibility(VISIBLE);
346 } else if (!mIsForeground) {
347 block.setVisibility(VISIBLE);
348 minimize.setVisibility(GONE);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500349 }
350
Rohan Shah7c6b37a2018-03-30 21:07:45 +0000351 // Set up app settings link (i.e. Customize)
Julia Reynolds437cdb12018-01-03 12:27:24 -0500352 TextView settingsLinkView = findViewById(R.id.app_settings);
Rohan Shahca0447e2018-03-30 15:18:27 -0700353 Intent settingsIntent = getAppSettingsIntent(mPm, mPackageName, mSingleNotificationChannel,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500354 mSbn.getId(), mSbn.getTag());
Rohan Shah7c6b37a2018-03-30 21:07:45 +0000355 if (!mIsForBlockingHelper
356 && settingsIntent != null
Julia Reynolds437cdb12018-01-03 12:27:24 -0500357 && !TextUtils.isEmpty(mSbn.getNotification().getSettingsText())) {
Julia Reynoldse0341482018-03-08 14:42:50 -0500358 settingsLinkView.setVisibility(VISIBLE);
Rohan Shah7c6b37a2018-03-30 21:07:45 +0000359 settingsLinkView.setText(mContext.getString(R.string.notification_app_settings));
Julia Reynolds437cdb12018-01-03 12:27:24 -0500360 settingsLinkView.setOnClickListener((View view) -> {
361 mAppSettingsClickListener.onClick(view, settingsIntent);
362 });
Mady Mellor87d79452017-01-10 11:52:52 -0800363 } else {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500364 settingsLinkView.setVisibility(View.GONE);
Mady Mellor87d79452017-01-10 11:52:52 -0800365 }
366 }
367
Julia Reynolds437cdb12018-01-03 12:27:24 -0500368 private void swapContent(boolean showPrompt) {
369 if (mExpandAnimation != null) {
370 mExpandAnimation.cancel();
371 }
Mady Mellor87d79452017-01-10 11:52:52 -0800372
Julia Reynoldse0341482018-03-08 14:42:50 -0500373 View prompt = findViewById(R.id.prompt);
374 ViewGroup confirmation = findViewById(R.id.confirmation);
375 TextView confirmationText = findViewById(R.id.confirmation_text);
376 View header = findViewById(R.id.header);
377
Julia Reynolds437cdb12018-01-03 12:27:24 -0500378 if (showPrompt) {
379 mChosenImportance = mStartingUserImportance;
Julia Reynoldse0341482018-03-08 14:42:50 -0500380 } else if (mIsForeground) {
381 mChosenImportance = IMPORTANCE_MIN;
382 confirmationText.setText(R.string.notification_channel_minimized);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500383 } else {
384 mChosenImportance = IMPORTANCE_NONE;
Julia Reynoldse0341482018-03-08 14:42:50 -0500385 confirmationText.setText(R.string.notification_channel_disabled);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500386 }
387
Julia Reynolds437cdb12018-01-03 12:27:24 -0500388 ObjectAnimator promptAnim = ObjectAnimator.ofFloat(prompt, View.ALPHA,
389 prompt.getAlpha(), showPrompt ? 1f : 0f);
390 promptAnim.setInterpolator(showPrompt ? Interpolators.ALPHA_IN : Interpolators.ALPHA_OUT);
391 ObjectAnimator confirmAnim = ObjectAnimator.ofFloat(confirmation, View.ALPHA,
392 confirmation.getAlpha(), showPrompt ? 0f : 1f);
393 confirmAnim.setInterpolator(showPrompt ? Interpolators.ALPHA_OUT : Interpolators.ALPHA_IN);
394
395 prompt.setVisibility(showPrompt ? VISIBLE : GONE);
396 confirmation.setVisibility(showPrompt ? GONE : VISIBLE);
Julia Reynoldse0341482018-03-08 14:42:50 -0500397 header.setVisibility(showPrompt ? VISIBLE : GONE);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500398
399 mExpandAnimation = new AnimatorSet();
400 mExpandAnimation.playTogether(promptAnim, confirmAnim);
401 mExpandAnimation.setDuration(150);
402 mExpandAnimation.addListener(new AnimatorListenerAdapter() {
403 boolean cancelled = false;
404
405 @Override
406 public void onAnimationCancel(Animator animation) {
407 cancelled = true;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500408 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500409
410 @Override
411 public void onAnimationEnd(Animator animation) {
412 if (!cancelled) {
413 prompt.setVisibility(showPrompt ? VISIBLE : GONE);
414 confirmation.setVisibility(showPrompt ? GONE : VISIBLE);
415 }
416 }
Mady Mellor87d79452017-01-10 11:52:52 -0800417 });
Julia Reynolds437cdb12018-01-03 12:27:24 -0500418 mExpandAnimation.start();
Rohan Shah142e2da2018-06-14 13:14:18 -0700419
420 // Since we're swapping/update the content, reset the timeout so the UI can't close
421 // immediately after the update.
422 if (mGutsContainer != null) {
423 mGutsContainer.resetFalsingCheck();
424 }
Mady Mellor87d79452017-01-10 11:52:52 -0800425 }
426
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400427 @Override
428 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
429 super.onInitializeAccessibilityEvent(event);
430 if (mGutsContainer != null &&
431 event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
432 if (mGutsContainer.isExposed()) {
433 event.getText().add(mContext.getString(
434 R.string.notification_channel_controls_opened_accessibility, mAppName));
435 } else {
436 event.getText().add(mContext.getString(
437 R.string.notification_channel_controls_closed_accessibility, mAppName));
438 }
439 }
440 }
441
Julia Reynolds3aedded2017-03-31 14:42:09 -0400442 private Intent getAppSettingsIntent(PackageManager pm, String packageName,
443 NotificationChannel channel, int id, String tag) {
444 Intent intent = new Intent(Intent.ACTION_MAIN)
445 .addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES)
446 .setPackage(packageName);
447 final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(
448 intent,
449 PackageManager.MATCH_DEFAULT_ONLY
450 );
451 if (resolveInfos == null || resolveInfos.size() == 0 || resolveInfos.get(0) == null) {
452 return null;
453 }
454 final ActivityInfo activityInfo = resolveInfos.get(0).activityInfo;
455 intent.setClassName(activityInfo.packageName, activityInfo.name);
456 if (channel != null) {
457 intent.putExtra(Notification.EXTRA_CHANNEL_ID, channel.getId());
458 }
459 intent.putExtra(Notification.EXTRA_NOTIFICATION_ID, id);
460 intent.putExtra(Notification.EXTRA_NOTIFICATION_TAG, tag);
461 return intent;
462 }
463
Rohan Shahca0447e2018-03-30 15:18:27 -0700464 /**
465 * Closes the controls and commits the updated importance values (indirectly). If this view is
466 * being used to show the blocking helper, this will immediately dismiss the blocking helper and
467 * commit the updated importance.
468 *
469 * <p><b>Note,</b> this will only get called once the view is dismissing. This means that the
470 * user does not have the ability to undo the action anymore. See {@link #swapContent(boolean)}
471 * for where undo is handled.
472 */
Rohan Shah524cf7b2018-03-15 14:40:02 -0700473 @VisibleForTesting
474 void closeControls(View v) {
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700475 int[] parentLoc = new int[2];
476 int[] targetLoc = new int[2];
477 mGutsContainer.getLocationOnScreen(parentLoc);
478 v.getLocationOnScreen(targetLoc);
479 final int centerX = v.getWidth() / 2;
480 final int centerY = v.getHeight() / 2;
481 final int x = targetLoc[0] - parentLoc[0] + centerX;
482 final int y = targetLoc[1] - parentLoc[1] + centerY;
483 mGutsContainer.closeControls(x, y, true /* save */, false /* force */);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500484 }
485
Mady Mellor87d79452017-01-10 11:52:52 -0800486 @Override
Mady Mellor95d743c2017-01-10 12:05:27 -0800487 public void setGutsParent(NotificationGuts guts) {
488 mGutsContainer = guts;
Mady Mellor87d79452017-01-10 11:52:52 -0800489 }
490
491 @Override
Mady Mellor434180c2017-02-13 11:29:42 -0800492 public boolean willBeRemoved() {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500493 return hasImportanceChanged();
Mady Mellor434180c2017-02-13 11:29:42 -0800494 }
495
496 @Override
Lucas Dupin9b08c012018-05-16 19:53:32 -0700497 public boolean shouldBeSaved() {
498 return hasImportanceChanged();
499 }
500
501 @Override
Mady Mellor87d79452017-01-10 11:52:52 -0800502 public View getContentView() {
503 return this;
504 }
505
506 @Override
Mady Mellorc2dbe492017-03-30 13:22:03 -0700507 public boolean handleCloseControls(boolean save, boolean force) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500508 // Save regardless of the importance so we can lock the importance field if the user wants
509 // to keep getting notifications
510 if (save) {
Rohan Shahca0447e2018-03-30 15:18:27 -0700511 saveImportance();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500512 }
Rohan Shahdd588c72018-05-09 20:32:15 -0700513 logBlockingHelperCounter(mExitReason);
Mady Mellor87d79452017-01-10 11:52:52 -0800514 return false;
515 }
Mady Mellore09fb702017-03-30 13:23:29 -0700516
517 @Override
518 public int getActualHeight() {
519 return getHeight();
520 }
Rohan Shahca0447e2018-03-30 15:18:27 -0700521
522 /**
523 * Runnable to either update the given channel (with a new importance value) or, if no channel
524 * is provided, update notifications enabled state for the package.
525 */
526 private static class UpdateImportanceRunnable implements Runnable {
527 private final INotificationManager mINotificationManager;
528 private final String mPackageName;
529 private final int mAppUid;
530 private final @Nullable NotificationChannel mChannelToUpdate;
531 private final int mCurrentImportance;
532 private final int mNewImportance;
533
534
535 public UpdateImportanceRunnable(INotificationManager notificationManager,
536 String packageName, int appUid, @Nullable NotificationChannel channelToUpdate,
537 int currentImportance, int newImportance) {
538 mINotificationManager = notificationManager;
539 mPackageName = packageName;
540 mAppUid = appUid;
541 mChannelToUpdate = channelToUpdate;
542 mCurrentImportance = currentImportance;
543 mNewImportance = newImportance;
544 }
545
546 @Override
547 public void run() {
548 try {
549 if (mChannelToUpdate != null) {
550 mChannelToUpdate.setImportance(mNewImportance);
551 mChannelToUpdate.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
552 mINotificationManager.updateNotificationChannelForPackage(
553 mPackageName, mAppUid, mChannelToUpdate);
554 } else {
555 // For notifications with more than one channel, update notification enabled
556 // state. If the importance was lowered, we disable notifications.
Rohan Shah590e1b22018-04-10 23:48:47 -0400557 mINotificationManager.setNotificationsEnabledWithImportanceLockForPackage(
Rohan Shahca0447e2018-03-30 15:18:27 -0700558 mPackageName, mAppUid, mNewImportance >= mCurrentImportance);
559 }
560 } catch (RemoteException e) {
561 Log.e(TAG, "Unable to update notification importance", e);
562 }
563 }
564 }
Mady Mellor87d79452017-01-10 11:52:52 -0800565}