blob: 903c27277b709489d529f8f6b807baa2edae1c4b [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;
Julia Reynolds35765d82018-08-17 11:39:19 -040084 private boolean mIsDeviceProvisioned;
Mady Mellor87d79452017-01-10 11:52:52 -080085
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -040086 private CheckSaveListener mCheckSaveListener;
Julia Reynolds437cdb12018-01-03 12:27:24 -050087 private OnSettingsClickListener mOnSettingsClickListener;
Julia Reynolds3aedded2017-03-31 14:42:09 -040088 private OnAppSettingsClickListener mAppSettingsClickListener;
Mady Mellor95d743c2017-01-10 12:05:27 -080089 private NotificationGuts mGutsContainer;
Rohan Shah524cf7b2018-03-15 14:40:02 -070090
Rohan Shahda5dcdd2018-04-27 17:21:50 -070091 /** Whether this view is being shown as part of the blocking helper. */
Rohan Shah524cf7b2018-03-15 14:40:02 -070092 private boolean mIsForBlockingHelper;
Julia Reynolds0ef7d842018-01-24 17:50:31 -050093 private boolean mNegativeUserSentiment;
Mady Mellor87d79452017-01-10 11:52:52 -080094
Rohan Shahdd588c72018-05-09 20:32:15 -070095 /**
96 * String that describes how the user exit or quit out of this view, also used as a counter tag.
97 */
98 private String mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED;
Rohan Shahda5dcdd2018-04-27 17:21:50 -070099
100 private OnClickListener mOnKeepShowing = v -> {
Rohan Shahdd588c72018-05-09 20:32:15 -0700101 mExitReason = NotificationCounters.BLOCKING_HELPER_KEEP_SHOWING;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700102 closeControls(v);
103 };
Julia Reynolds437cdb12018-01-03 12:27:24 -0500104
Rohan Shahca0447e2018-03-30 15:18:27 -0700105 private OnClickListener mOnStopOrMinimizeNotifications = v -> {
Gus Prevas533836a2018-09-24 17:15:32 -0400106 Runnable saveImportance = () -> {
107 mExitReason = NotificationCounters.BLOCKING_HELPER_STOP_NOTIFICATIONS;
108 swapContent(false);
109 };
110 if (mCheckSaveListener != null) {
111 mCheckSaveListener.checkSave(saveImportance, mSbn);
112 } else {
113 saveImportance.run();
114 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500115 };
116
117 private OnClickListener mOnUndo = v -> {
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700118 // Reset exit counter that we'll log and record an undo event separately (not an exit event)
Rohan Shahdd588c72018-05-09 20:32:15 -0700119 mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700120 logBlockingHelperCounter(NotificationCounters.BLOCKING_HELPER_UNDO);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500121 swapContent(true);
122 };
123
Mady Mellor87d79452017-01-10 11:52:52 -0800124 public NotificationInfo(Context context, AttributeSet attrs) {
125 super(context, attrs);
126 }
127
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400128 // Specify a CheckSaveListener to override when/if the user's changes are committed.
129 public interface CheckSaveListener {
130 // Invoked when importance has changed and the NotificationInfo wants to try to save it.
131 // Listener should run saveImportance unless the change should be canceled.
Eliot Courtney47098cb2017-10-18 17:30:30 +0900132 void checkSave(Runnable saveImportance, StatusBarNotification sbn);
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400133 }
134
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500135 public interface OnSettingsClickListener {
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500136 void onClick(View v, NotificationChannel channel, int appUid);
Mady Mellor87d79452017-01-10 11:52:52 -0800137 }
138
Julia Reynolds3aedded2017-03-31 14:42:09 -0400139 public interface OnAppSettingsClickListener {
140 void onClick(View v, Intent intent);
141 }
142
Rohan Shah524cf7b2018-03-15 14:40:02 -0700143 @VisibleForTesting
144 void bindNotification(
145 final PackageManager pm,
Jason Monk2a6ea9c2017-01-26 11:14:51 -0500146 final INotificationManager iNotificationManager,
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500147 final String pkg,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500148 final NotificationChannel notificationChannel,
Rohan Shahca0447e2018-03-30 15:18:27 -0700149 final int numUniqueChannelsInRow,
Julia Reynolds3aedded2017-03-31 14:42:09 -0400150 final StatusBarNotification sbn,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500151 final CheckSaveListener checkSaveListener,
152 final OnSettingsClickListener onSettingsClick,
153 final OnAppSettingsClickListener onAppSettingsClick,
Julia Reynolds35765d82018-08-17 11:39:19 -0400154 boolean isDeviceProvisioned,
Rohan Shah63411fc2018-03-28 19:05:52 -0700155 boolean isNonblockable)
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500156 throws RemoteException {
Rohan Shahca0447e2018-03-30 15:18:27 -0700157 bindNotification(pm, iNotificationManager, pkg, notificationChannel,
158 numUniqueChannelsInRow, sbn, checkSaveListener, onSettingsClick,
Julia Reynolds35765d82018-08-17 11:39:19 -0400159 onAppSettingsClick, isDeviceProvisioned, isNonblockable,
160 false /* isBlockingHelper */, false /* isUserSentimentNegative */);
Julia Reynolds0ef7d842018-01-24 17:50:31 -0500161 }
162
Rohan Shah524cf7b2018-03-15 14:40:02 -0700163 public void bindNotification(
164 PackageManager pm,
165 INotificationManager iNotificationManager,
166 String pkg,
167 NotificationChannel notificationChannel,
Rohan Shahca0447e2018-03-30 15:18:27 -0700168 int numUniqueChannelsInRow,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700169 StatusBarNotification sbn,
170 CheckSaveListener checkSaveListener,
171 OnSettingsClickListener onSettingsClick,
172 OnAppSettingsClickListener onAppSettingsClick,
Julia Reynolds35765d82018-08-17 11:39:19 -0400173 boolean isDeviceProvisioned,
Rohan Shah63411fc2018-03-28 19:05:52 -0700174 boolean isNonblockable,
Rohan Shah524cf7b2018-03-15 14:40:02 -0700175 boolean isForBlockingHelper,
176 boolean isUserSentimentNegative)
177 throws RemoteException {
Mady Mellor87d79452017-01-10 11:52:52 -0800178 mINotificationManager = iNotificationManager;
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700179 mMetricsLogger = Dependency.get(MetricsLogger.class);
Rohan Shahca0447e2018-03-30 15:18:27 -0700180 mPackageName = pkg;
181 mNumUniqueChannelsInRow = numUniqueChannelsInRow;
Julia Reynolds3aedded2017-03-31 14:42:09 -0400182 mSbn = sbn;
183 mPm = pm;
184 mAppSettingsClickListener = onAppSettingsClick;
Rohan Shahca0447e2018-03-30 15:18:27 -0700185 mAppName = mPackageName;
Julia Reynolds437cdb12018-01-03 12:27:24 -0500186 mCheckSaveListener = checkSaveListener;
187 mOnSettingsClickListener = onSettingsClick;
188 mSingleNotificationChannel = notificationChannel;
189 mStartingUserImportance = mChosenImportance = mSingleNotificationChannel.getImportance();
Rohan Shah524cf7b2018-03-15 14:40:02 -0700190 mNegativeUserSentiment = isUserSentimentNegative;
Rohan Shah63411fc2018-03-28 19:05:52 -0700191 mIsNonblockable = isNonblockable;
Julia Reynoldse0341482018-03-08 14:42:50 -0500192 mIsForeground =
193 (mSbn.getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Rohan Shah524cf7b2018-03-15 14:40:02 -0700194 mIsForBlockingHelper = isForBlockingHelper;
Rohan Shahdbd64e72018-03-28 14:46:50 -0700195 mAppUid = mSbn.getUid();
Julia Reynolds35765d82018-08-17 11:39:19 -0400196 mIsDeviceProvisioned = isDeviceProvisioned;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500197
Julia Reynolds437cdb12018-01-03 12:27:24 -0500198 int numTotalChannels = mINotificationManager.getNumNotificationChannelsForPackage(
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400199 pkg, mAppUid, false /* includeDeleted */);
Rohan Shahca0447e2018-03-30 15:18:27 -0700200 if (mNumUniqueChannelsInRow == 0) {
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400201 throw new IllegalArgumentException("bindNotification requires at least one channel");
202 } else {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500203 // Special behavior for the Default channel if no other channels have been defined.
Rohan Shahca0447e2018-03-30 15:18:27 -0700204 mIsSingleDefaultChannel = mNumUniqueChannelsInRow == 1
Rohan Shahdbd64e72018-03-28 14:46:50 -0700205 && mSingleNotificationChannel.getId().equals(
206 NotificationChannel.DEFAULT_CHANNEL_ID)
207 && numTotalChannels == 1;
Geoffrey Pitschd034d292017-05-12 11:59:20 -0400208 }
209
Julia Reynolds437cdb12018-01-03 12:27:24 -0500210 bindHeader();
211 bindPrompt();
212 bindButtons();
213 }
214
215 private void bindHeader() throws RemoteException {
216 // Package name
217 Drawable pkgicon = null;
218 ApplicationInfo info;
219 try {
Rohan Shahca0447e2018-03-30 15:18:27 -0700220 info = mPm.getApplicationInfo(
221 mPackageName,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500222 PackageManager.MATCH_UNINSTALLED_PACKAGES
223 | PackageManager.MATCH_DISABLED_COMPONENTS
224 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
225 | PackageManager.MATCH_DIRECT_BOOT_AWARE);
226 if (info != null) {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500227 mAppName = String.valueOf(mPm.getApplicationLabel(info));
228 pkgicon = mPm.getApplicationIcon(info);
Geoffrey Pitschee8c81e2017-03-23 11:38:56 -0400229 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500230 } catch (PackageManager.NameNotFoundException e) {
231 // app is gone, just show package name and generic icon
232 pkgicon = mPm.getDefaultActivityIcon();
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500233 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500234 ((ImageView) findViewById(R.id.pkgicon)).setImageDrawable(pkgicon);
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400235 ((TextView) findViewById(R.id.pkgname)).setText(mAppName);
Mady Mellor87d79452017-01-10 11:52:52 -0800236
Julia Reynoldsac98aea2018-10-25 16:54:27 -0400237 // Set group information if this channel has an associated group.
238 CharSequence groupName = null;
239 if (mSingleNotificationChannel != null && mSingleNotificationChannel.getGroup() != null) {
240 final NotificationChannelGroup notificationChannelGroup =
241 mINotificationManager.getNotificationChannelGroupForPackage(
242 mSingleNotificationChannel.getGroup(), mPackageName, mAppUid);
243 if (notificationChannelGroup != null) {
244 groupName = notificationChannelGroup.getName();
245 }
246 }
247 TextView groupNameView = findViewById(R.id.group_name);
248 TextView groupDividerView = findViewById(R.id.pkg_group_divider);
249 if (groupName != null) {
250 groupNameView.setText(groupName);
251 groupNameView.setVisibility(View.VISIBLE);
252 groupDividerView.setVisibility(View.VISIBLE);
253 } else {
254 groupNameView.setVisibility(View.GONE);
255 groupDividerView.setVisibility(View.GONE);
256 }
Daniel Sandlerd0a52b62018-10-31 20:13:22 +0000257
258 // Settings button.
259 final View settingsButton = findViewById(R.id.info);
260 if (mAppUid >= 0 && mOnSettingsClickListener != null && mIsDeviceProvisioned) {
261 settingsButton.setVisibility(View.VISIBLE);
262 final int appUidF = mAppUid;
263 settingsButton.setOnClickListener(
264 (View view) -> {
265 logBlockingHelperCounter(
266 NotificationCounters.BLOCKING_HELPER_NOTIF_SETTINGS);
267 mOnSettingsClickListener.onClick(view,
268 mNumUniqueChannelsInRow > 1 ? null : mSingleNotificationChannel,
269 appUidF);
270 });
271 } else {
272 settingsButton.setVisibility(View.GONE);
273 }
274 }
275
276 private void bindPrompt() {
277 final TextView blockPrompt = findViewById(R.id.block_prompt);
278 bindName();
279 if (mIsNonblockable) {
280 blockPrompt.setText(R.string.notification_unblockable_desc);
281 } else {
282 if (mNegativeUserSentiment) {
283 blockPrompt.setText(R.string.inline_blocking_helper);
284 } else if (mIsSingleDefaultChannel || mNumUniqueChannelsInRow > 1) {
285 blockPrompt.setText(R.string.inline_keep_showing_app);
286 } else {
287 blockPrompt.setText(R.string.inline_keep_showing);
288 }
289 }
290 }
291
292 private void bindName() {
293 final TextView channelName = findViewById(R.id.channel_name);
294 if (mIsSingleDefaultChannel || mNumUniqueChannelsInRow > 1) {
295 channelName.setVisibility(View.GONE);
296 } else {
297 channelName.setText(mSingleNotificationChannel.getName());
298 }
Julia Reynoldsac98aea2018-10-25 16:54:27 -0400299 }
300
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700301 @VisibleForTesting
302 void logBlockingHelperCounter(String counterTag) {
303 if (mIsForBlockingHelper) {
304 mMetricsLogger.count(counterTag, 1);
305 }
306 }
307
Geoffrey Pitsch5278d3d2017-03-29 13:39:10 -0400308 private boolean hasImportanceChanged() {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500309 return mSingleNotificationChannel != null && mStartingUserImportance != mChosenImportance;
Mady Mellor87d79452017-01-10 11:52:52 -0800310 }
311
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500312 private void saveImportance() {
Rohan Shahca0447e2018-03-30 15:18:27 -0700313 if (!mIsNonblockable) {
Gus Prevas533836a2018-09-24 17:15:32 -0400314 updateImportance();
Geoffrey Pitschd0856f02017-02-16 10:51:18 -0500315 }
Rohan Shahca0447e2018-03-30 15:18:27 -0700316 }
317
318 /**
319 * Commits the updated importance values on the background thread.
320 */
321 private void updateImportance() {
Mady Mellor87d79452017-01-10 11:52:52 -0800322 MetricsLogger.action(mContext, MetricsEvent.ACTION_SAVE_IMPORTANCE,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500323 mChosenImportance - mStartingUserImportance);
Rohan Shahca0447e2018-03-30 15:18:27 -0700324
325 Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
326 bgHandler.post(new UpdateImportanceRunnable(mINotificationManager, mPackageName, mAppUid,
327 mNumUniqueChannelsInRow == 1 ? mSingleNotificationChannel : null,
328 mStartingUserImportance, mChosenImportance));
Mady Mellor87d79452017-01-10 11:52:52 -0800329 }
330
Julia Reynolds437cdb12018-01-03 12:27:24 -0500331 private void bindButtons() {
Rohan Shah524cf7b2018-03-15 14:40:02 -0700332 // Set up stay-in-notification actions
Julia Reynolds437cdb12018-01-03 12:27:24 -0500333 View block = findViewById(R.id.block);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500334 TextView keep = findViewById(R.id.keep);
Julia Reynoldse0341482018-03-08 14:42:50 -0500335 View minimize = findViewById(R.id.minimize);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700336
337 findViewById(R.id.undo).setOnClickListener(mOnUndo);
Rohan Shahca0447e2018-03-30 15:18:27 -0700338 block.setOnClickListener(mOnStopOrMinimizeNotifications);
Rohan Shah524cf7b2018-03-15 14:40:02 -0700339 keep.setOnClickListener(mOnKeepShowing);
Rohan Shahca0447e2018-03-30 15:18:27 -0700340 minimize.setOnClickListener(mOnStopOrMinimizeNotifications);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500341
Rohan Shah63411fc2018-03-28 19:05:52 -0700342 if (mIsNonblockable) {
Rohan Shah53529aa2018-04-12 00:14:50 -0400343 keep.setText(android.R.string.ok);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500344 block.setVisibility(GONE);
Julia Reynoldse0341482018-03-08 14:42:50 -0500345 minimize.setVisibility(GONE);
346 } else if (mIsForeground) {
347 block.setVisibility(GONE);
348 minimize.setVisibility(VISIBLE);
349 } else if (!mIsForeground) {
350 block.setVisibility(VISIBLE);
351 minimize.setVisibility(GONE);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500352 }
353
Rohan Shah7c6b37a2018-03-30 21:07:45 +0000354 // Set up app settings link (i.e. Customize)
Julia Reynolds437cdb12018-01-03 12:27:24 -0500355 TextView settingsLinkView = findViewById(R.id.app_settings);
Rohan Shahca0447e2018-03-30 15:18:27 -0700356 Intent settingsIntent = getAppSettingsIntent(mPm, mPackageName, mSingleNotificationChannel,
Julia Reynolds437cdb12018-01-03 12:27:24 -0500357 mSbn.getId(), mSbn.getTag());
Rohan Shah7c6b37a2018-03-30 21:07:45 +0000358 if (!mIsForBlockingHelper
359 && settingsIntent != null
Julia Reynolds437cdb12018-01-03 12:27:24 -0500360 && !TextUtils.isEmpty(mSbn.getNotification().getSettingsText())) {
Julia Reynoldse0341482018-03-08 14:42:50 -0500361 settingsLinkView.setVisibility(VISIBLE);
Rohan Shah7c6b37a2018-03-30 21:07:45 +0000362 settingsLinkView.setText(mContext.getString(R.string.notification_app_settings));
Julia Reynolds437cdb12018-01-03 12:27:24 -0500363 settingsLinkView.setOnClickListener((View view) -> {
364 mAppSettingsClickListener.onClick(view, settingsIntent);
365 });
Mady Mellor87d79452017-01-10 11:52:52 -0800366 } else {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500367 settingsLinkView.setVisibility(View.GONE);
Mady Mellor87d79452017-01-10 11:52:52 -0800368 }
369 }
370
Julia Reynolds437cdb12018-01-03 12:27:24 -0500371 private void swapContent(boolean showPrompt) {
372 if (mExpandAnimation != null) {
373 mExpandAnimation.cancel();
374 }
Mady Mellor87d79452017-01-10 11:52:52 -0800375
Julia Reynoldse0341482018-03-08 14:42:50 -0500376 View prompt = findViewById(R.id.prompt);
377 ViewGroup confirmation = findViewById(R.id.confirmation);
378 TextView confirmationText = findViewById(R.id.confirmation_text);
379 View header = findViewById(R.id.header);
380
Julia Reynolds437cdb12018-01-03 12:27:24 -0500381 if (showPrompt) {
382 mChosenImportance = mStartingUserImportance;
Julia Reynoldse0341482018-03-08 14:42:50 -0500383 } else if (mIsForeground) {
384 mChosenImportance = IMPORTANCE_MIN;
385 confirmationText.setText(R.string.notification_channel_minimized);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500386 } else {
387 mChosenImportance = IMPORTANCE_NONE;
Julia Reynoldse0341482018-03-08 14:42:50 -0500388 confirmationText.setText(R.string.notification_channel_disabled);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500389 }
390
Julia Reynolds437cdb12018-01-03 12:27:24 -0500391 ObjectAnimator promptAnim = ObjectAnimator.ofFloat(prompt, View.ALPHA,
392 prompt.getAlpha(), showPrompt ? 1f : 0f);
393 promptAnim.setInterpolator(showPrompt ? Interpolators.ALPHA_IN : Interpolators.ALPHA_OUT);
394 ObjectAnimator confirmAnim = ObjectAnimator.ofFloat(confirmation, View.ALPHA,
395 confirmation.getAlpha(), showPrompt ? 0f : 1f);
396 confirmAnim.setInterpolator(showPrompt ? Interpolators.ALPHA_OUT : Interpolators.ALPHA_IN);
397
398 prompt.setVisibility(showPrompt ? VISIBLE : GONE);
399 confirmation.setVisibility(showPrompt ? GONE : VISIBLE);
Julia Reynoldse0341482018-03-08 14:42:50 -0500400 header.setVisibility(showPrompt ? VISIBLE : GONE);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500401
402 mExpandAnimation = new AnimatorSet();
403 mExpandAnimation.playTogether(promptAnim, confirmAnim);
404 mExpandAnimation.setDuration(150);
405 mExpandAnimation.addListener(new AnimatorListenerAdapter() {
406 boolean cancelled = false;
407
408 @Override
409 public void onAnimationCancel(Animator animation) {
410 cancelled = true;
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500411 }
Julia Reynolds437cdb12018-01-03 12:27:24 -0500412
413 @Override
414 public void onAnimationEnd(Animator animation) {
415 if (!cancelled) {
416 prompt.setVisibility(showPrompt ? VISIBLE : GONE);
417 confirmation.setVisibility(showPrompt ? GONE : VISIBLE);
418 }
419 }
Mady Mellor87d79452017-01-10 11:52:52 -0800420 });
Julia Reynolds437cdb12018-01-03 12:27:24 -0500421 mExpandAnimation.start();
Rohan Shah142e2da2018-06-14 13:14:18 -0700422
423 // Since we're swapping/update the content, reset the timeout so the UI can't close
424 // immediately after the update.
425 if (mGutsContainer != null) {
426 mGutsContainer.resetFalsingCheck();
427 }
Mady Mellor87d79452017-01-10 11:52:52 -0800428 }
429
Geoffrey Pitschd94e7882017-04-06 09:52:11 -0400430 @Override
431 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
432 super.onInitializeAccessibilityEvent(event);
433 if (mGutsContainer != null &&
434 event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
435 if (mGutsContainer.isExposed()) {
436 event.getText().add(mContext.getString(
437 R.string.notification_channel_controls_opened_accessibility, mAppName));
438 } else {
439 event.getText().add(mContext.getString(
440 R.string.notification_channel_controls_closed_accessibility, mAppName));
441 }
442 }
443 }
444
Julia Reynolds3aedded2017-03-31 14:42:09 -0400445 private Intent getAppSettingsIntent(PackageManager pm, String packageName,
446 NotificationChannel channel, int id, String tag) {
447 Intent intent = new Intent(Intent.ACTION_MAIN)
448 .addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES)
449 .setPackage(packageName);
450 final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(
451 intent,
452 PackageManager.MATCH_DEFAULT_ONLY
453 );
454 if (resolveInfos == null || resolveInfos.size() == 0 || resolveInfos.get(0) == null) {
455 return null;
456 }
457 final ActivityInfo activityInfo = resolveInfos.get(0).activityInfo;
458 intent.setClassName(activityInfo.packageName, activityInfo.name);
459 if (channel != null) {
460 intent.putExtra(Notification.EXTRA_CHANNEL_ID, channel.getId());
461 }
462 intent.putExtra(Notification.EXTRA_NOTIFICATION_ID, id);
463 intent.putExtra(Notification.EXTRA_NOTIFICATION_TAG, tag);
464 return intent;
465 }
466
Rohan Shahca0447e2018-03-30 15:18:27 -0700467 /**
468 * Closes the controls and commits the updated importance values (indirectly). If this view is
469 * being used to show the blocking helper, this will immediately dismiss the blocking helper and
470 * commit the updated importance.
471 *
472 * <p><b>Note,</b> this will only get called once the view is dismissing. This means that the
473 * user does not have the ability to undo the action anymore. See {@link #swapContent(boolean)}
474 * for where undo is handled.
475 */
Rohan Shah524cf7b2018-03-15 14:40:02 -0700476 @VisibleForTesting
477 void closeControls(View v) {
Rohan Shahda5dcdd2018-04-27 17:21:50 -0700478 int[] parentLoc = new int[2];
479 int[] targetLoc = new int[2];
480 mGutsContainer.getLocationOnScreen(parentLoc);
481 v.getLocationOnScreen(targetLoc);
482 final int centerX = v.getWidth() / 2;
483 final int centerY = v.getHeight() / 2;
484 final int x = targetLoc[0] - parentLoc[0] + centerX;
485 final int y = targetLoc[1] - parentLoc[1] + centerY;
486 mGutsContainer.closeControls(x, y, true /* save */, false /* force */);
Julia Reynolds437cdb12018-01-03 12:27:24 -0500487 }
488
Mady Mellor87d79452017-01-10 11:52:52 -0800489 @Override
Mady Mellor95d743c2017-01-10 12:05:27 -0800490 public void setGutsParent(NotificationGuts guts) {
491 mGutsContainer = guts;
Mady Mellor87d79452017-01-10 11:52:52 -0800492 }
493
494 @Override
Mady Mellor434180c2017-02-13 11:29:42 -0800495 public boolean willBeRemoved() {
Julia Reynolds437cdb12018-01-03 12:27:24 -0500496 return hasImportanceChanged();
Mady Mellor434180c2017-02-13 11:29:42 -0800497 }
498
499 @Override
Lucas Dupin9b08c012018-05-16 19:53:32 -0700500 public boolean shouldBeSaved() {
501 return hasImportanceChanged();
502 }
503
504 @Override
Mady Mellor87d79452017-01-10 11:52:52 -0800505 public View getContentView() {
506 return this;
507 }
508
509 @Override
Mady Mellorc2dbe492017-03-30 13:22:03 -0700510 public boolean handleCloseControls(boolean save, boolean force) {
Julia Reynoldsc65656a2018-02-12 09:55:14 -0500511 // Save regardless of the importance so we can lock the importance field if the user wants
512 // to keep getting notifications
513 if (save) {
Rohan Shahca0447e2018-03-30 15:18:27 -0700514 saveImportance();
Geoffrey Pitschdf44b602017-02-03 13:31:50 -0500515 }
Rohan Shahdd588c72018-05-09 20:32:15 -0700516 logBlockingHelperCounter(mExitReason);
Mady Mellor87d79452017-01-10 11:52:52 -0800517 return false;
518 }
Mady Mellore09fb702017-03-30 13:23:29 -0700519
520 @Override
521 public int getActualHeight() {
522 return getHeight();
523 }
Rohan Shahca0447e2018-03-30 15:18:27 -0700524
Gus Prevas533836a2018-09-24 17:15:32 -0400525 @VisibleForTesting
526 public boolean isAnimating() {
527 return mExpandAnimation != null && mExpandAnimation.isRunning();
528 }
529
Rohan Shahca0447e2018-03-30 15:18:27 -0700530 /**
531 * Runnable to either update the given channel (with a new importance value) or, if no channel
532 * is provided, update notifications enabled state for the package.
533 */
534 private static class UpdateImportanceRunnable implements Runnable {
535 private final INotificationManager mINotificationManager;
536 private final String mPackageName;
537 private final int mAppUid;
538 private final @Nullable NotificationChannel mChannelToUpdate;
539 private final int mCurrentImportance;
540 private final int mNewImportance;
541
542
543 public UpdateImportanceRunnable(INotificationManager notificationManager,
544 String packageName, int appUid, @Nullable NotificationChannel channelToUpdate,
545 int currentImportance, int newImportance) {
546 mINotificationManager = notificationManager;
547 mPackageName = packageName;
548 mAppUid = appUid;
549 mChannelToUpdate = channelToUpdate;
550 mCurrentImportance = currentImportance;
551 mNewImportance = newImportance;
552 }
553
554 @Override
555 public void run() {
556 try {
557 if (mChannelToUpdate != null) {
558 mChannelToUpdate.setImportance(mNewImportance);
559 mChannelToUpdate.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
560 mINotificationManager.updateNotificationChannelForPackage(
561 mPackageName, mAppUid, mChannelToUpdate);
562 } else {
563 // For notifications with more than one channel, update notification enabled
564 // state. If the importance was lowered, we disable notifications.
Rohan Shah590e1b22018-04-10 23:48:47 -0400565 mINotificationManager.setNotificationsEnabledWithImportanceLockForPackage(
Rohan Shahca0447e2018-03-30 15:18:27 -0700566 mPackageName, mAppUid, mNewImportance >= mCurrentImportance);
567 }
568 } catch (RemoteException e) {
569 Log.e(TAG, "Unable to update notification importance", e);
570 }
571 }
572 }
Mady Mellor87d79452017-01-10 11:52:52 -0800573}