Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 1 | /* |
| 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 Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 17 | package com.android.systemui.statusbar.notification.row; |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 18 | |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 19 | import static android.app.NotificationManager.IMPORTANCE_DEFAULT; |
| 20 | import static android.app.NotificationManager.IMPORTANCE_HIGH; |
| 21 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Julia Reynolds | e034148 | 2018-03-08 14:42:50 -0500 | [diff] [blame] | 22 | import static android.app.NotificationManager.IMPORTANCE_MIN; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 23 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
| 24 | |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 25 | import android.animation.Animator; |
| 26 | import android.animation.AnimatorListenerAdapter; |
| 27 | import android.animation.AnimatorSet; |
| 28 | import android.animation.ObjectAnimator; |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 29 | import android.annotation.IntDef; |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 30 | import android.annotation.Nullable; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 31 | import android.app.INotificationManager; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 32 | import android.app.Notification; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 33 | import android.app.NotificationChannel; |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 34 | import android.app.NotificationChannelGroup; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 35 | import android.content.Context; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 36 | import android.content.Intent; |
| 37 | import android.content.pm.ActivityInfo; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 38 | import android.content.pm.ApplicationInfo; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 39 | import android.content.pm.PackageManager; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 40 | import android.content.pm.ResolveInfo; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 41 | import android.graphics.drawable.Drawable; |
William Brockman | 75cf66a | 2018-12-21 13:25:01 -0500 | [diff] [blame] | 42 | import android.metrics.LogMaker; |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 43 | import android.os.Handler; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 44 | import android.os.RemoteException; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 45 | import android.service.notification.StatusBarNotification; |
| 46 | import android.text.TextUtils; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 47 | import android.util.AttributeSet; |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 48 | import android.util.Log; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 49 | import android.view.View; |
Julia Reynolds | e034148 | 2018-03-08 14:42:50 -0500 | [diff] [blame] | 50 | import android.view.ViewGroup; |
Geoffrey Pitsch | d94e788 | 2017-04-06 09:52:11 -0400 | [diff] [blame] | 51 | import android.view.accessibility.AccessibilityEvent; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 52 | import android.widget.ImageView; |
| 53 | import android.widget.LinearLayout; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 54 | import android.widget.TextView; |
| 55 | |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 56 | import com.android.internal.annotations.VisibleForTesting; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 57 | import com.android.internal.logging.MetricsLogger; |
| 58 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 59 | import com.android.systemui.Dependency; |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 60 | import com.android.systemui.Interpolators; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 61 | import com.android.systemui.R; |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 62 | import com.android.systemui.statusbar.notification.NotificationUtils; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 63 | import com.android.systemui.statusbar.notification.logging.NotificationCounters; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 64 | |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 65 | import java.util.List; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 66 | |
| 67 | /** |
Rohan Shah | da5dcdd | 2018-04-27 17:21:50 -0700 | [diff] [blame] | 68 | * The guts of a notification revealed when performing a long press. This also houses the blocking |
| 69 | * helper affordance that allows a user to keep/stop notifications after swiping one away. |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 70 | */ |
Mady Mellor | 95d743c | 2017-01-10 12:05:27 -0800 | [diff] [blame] | 71 | public class NotificationInfo extends LinearLayout implements NotificationGuts.GutsContent { |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 72 | private static final String TAG = "InfoGuts"; |
| 73 | |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 74 | @IntDef(prefix = { "ACTION_" }, value = { |
| 75 | ACTION_NONE, |
| 76 | ACTION_UNDO, |
| 77 | ACTION_TOGGLE_SILENT, |
| 78 | ACTION_BLOCK, |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 79 | }) |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 80 | public @interface NotificationInfoAction { |
| 81 | } |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 82 | |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 83 | public static final int ACTION_NONE = 0; |
| 84 | public static final int ACTION_UNDO = 1; |
| 85 | public static final int ACTION_TOGGLE_SILENT = 2; |
| 86 | public static final int ACTION_BLOCK = 3; |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 87 | |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 88 | private INotificationManager mINotificationManager; |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 89 | private PackageManager mPm; |
Rohan Shah | da5dcdd | 2018-04-27 17:21:50 -0700 | [diff] [blame] | 90 | private MetricsLogger mMetricsLogger; |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 91 | |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 92 | private String mPackageName; |
Geoffrey Pitsch | d94e788 | 2017-04-06 09:52:11 -0400 | [diff] [blame] | 93 | private String mAppName; |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 94 | private int mAppUid; |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 95 | private String mDelegatePkg; |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 96 | private int mNumUniqueChannelsInRow; |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 97 | private NotificationChannel mSingleNotificationChannel; |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 98 | private int mStartingChannelImportance; |
Gus Prevas | caed15c | 2019-01-18 14:19:51 -0500 | [diff] [blame] | 99 | private boolean mWasShownHighPriority; |
| 100 | /** |
| 101 | * The last importance level chosen by the user. Null if the user has not chosen an importance |
| 102 | * level; non-null once the user takes an action which indicates an explicit preference. |
| 103 | */ |
| 104 | @Nullable private Integer mChosenImportance; |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 105 | private boolean mIsSingleDefaultChannel; |
Rohan Shah | 63411fc | 2018-03-28 19:05:52 -0700 | [diff] [blame] | 106 | private boolean mIsNonblockable; |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 107 | private StatusBarNotification mSbn; |
| 108 | private AnimatorSet mExpandAnimation; |
Julia Reynolds | e034148 | 2018-03-08 14:42:50 -0500 | [diff] [blame] | 109 | private boolean mIsForeground; |
Julia Reynolds | 35765d8 | 2018-08-17 11:39:19 -0400 | [diff] [blame] | 110 | private boolean mIsDeviceProvisioned; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 111 | |
Geoffrey Pitsch | 5278d3d | 2017-03-29 13:39:10 -0400 | [diff] [blame] | 112 | private CheckSaveListener mCheckSaveListener; |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 113 | private OnSettingsClickListener mOnSettingsClickListener; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 114 | private OnAppSettingsClickListener mAppSettingsClickListener; |
Mady Mellor | 95d743c | 2017-01-10 12:05:27 -0800 | [diff] [blame] | 115 | private NotificationGuts mGutsContainer; |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 116 | |
Rohan Shah | da5dcdd | 2018-04-27 17:21:50 -0700 | [diff] [blame] | 117 | /** Whether this view is being shown as part of the blocking helper. */ |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 118 | private boolean mIsForBlockingHelper; |
Julia Reynolds | 0ef7d84 | 2018-01-24 17:50:31 -0500 | [diff] [blame] | 119 | private boolean mNegativeUserSentiment; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 120 | |
Rohan Shah | dd588c7 | 2018-05-09 20:32:15 -0700 | [diff] [blame] | 121 | /** |
| 122 | * String that describes how the user exit or quit out of this view, also used as a counter tag. |
| 123 | */ |
| 124 | private String mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED; |
Rohan Shah | da5dcdd | 2018-04-27 17:21:50 -0700 | [diff] [blame] | 125 | |
| 126 | private OnClickListener mOnKeepShowing = v -> { |
Rohan Shah | dd588c7 | 2018-05-09 20:32:15 -0700 | [diff] [blame] | 127 | mExitReason = NotificationCounters.BLOCKING_HELPER_KEEP_SHOWING; |
Rohan Shah | da5dcdd | 2018-04-27 17:21:50 -0700 | [diff] [blame] | 128 | closeControls(v); |
Eyal Posener | aea29ed | 2019-01-24 09:54:08 +0200 | [diff] [blame] | 129 | mMetricsLogger.write(getLogMaker().setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER) |
| 130 | .setType(MetricsEvent.TYPE_ACTION) |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 131 | .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_STAY_SILENT)); |
Rohan Shah | da5dcdd | 2018-04-27 17:21:50 -0700 | [diff] [blame] | 132 | }; |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 133 | |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 134 | private OnClickListener mOnToggleSilent = v -> { |
| 135 | Runnable saveImportance = () -> { |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 136 | swapContent(ACTION_TOGGLE_SILENT, true /* animate */); |
Eyal Posener | aea29ed | 2019-01-24 09:54:08 +0200 | [diff] [blame] | 137 | mMetricsLogger.write(getLogMaker() |
| 138 | .setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER) |
| 139 | .setType(MetricsEvent.TYPE_ACTION) |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 140 | .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_ALERT_ME)); |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 141 | }; |
| 142 | if (mCheckSaveListener != null) { |
| 143 | mCheckSaveListener.checkSave(saveImportance, mSbn); |
| 144 | } else { |
| 145 | saveImportance.run(); |
| 146 | } |
| 147 | }; |
| 148 | |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 149 | private OnClickListener mOnStopOrMinimizeNotifications = v -> { |
Gus Prevas | 533836a | 2018-09-24 17:15:32 -0400 | [diff] [blame] | 150 | Runnable saveImportance = () -> { |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 151 | swapContent(ACTION_BLOCK, true /* animate */); |
Eyal Posener | aea29ed | 2019-01-24 09:54:08 +0200 | [diff] [blame] | 152 | mMetricsLogger.write(getLogMaker() |
| 153 | .setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER) |
| 154 | .setType(MetricsEvent.TYPE_ACTION) |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 155 | .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_BLOCKED)); |
Gus Prevas | 533836a | 2018-09-24 17:15:32 -0400 | [diff] [blame] | 156 | }; |
| 157 | if (mCheckSaveListener != null) { |
| 158 | mCheckSaveListener.checkSave(saveImportance, mSbn); |
| 159 | } else { |
| 160 | saveImportance.run(); |
| 161 | } |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | private OnClickListener mOnUndo = v -> { |
Rohan Shah | da5dcdd | 2018-04-27 17:21:50 -0700 | [diff] [blame] | 165 | // Reset exit counter that we'll log and record an undo event separately (not an exit event) |
Rohan Shah | dd588c7 | 2018-05-09 20:32:15 -0700 | [diff] [blame] | 166 | mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED; |
Rohan Shah | da5dcdd | 2018-04-27 17:21:50 -0700 | [diff] [blame] | 167 | logBlockingHelperCounter(NotificationCounters.BLOCKING_HELPER_UNDO); |
William Brockman | 75cf66a | 2018-12-21 13:25:01 -0500 | [diff] [blame] | 168 | mMetricsLogger.write(importanceChangeLogMaker().setType(MetricsEvent.TYPE_DISMISS)); |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 169 | swapContent(ACTION_UNDO, true /* animate */); |
Eyal Posener | aea29ed | 2019-01-24 09:54:08 +0200 | [diff] [blame] | 170 | mMetricsLogger.write(getLogMaker().setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER) |
| 171 | .setType(MetricsEvent.TYPE_DISMISS) |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 172 | .setSubtype(MetricsEvent.BLOCKING_HELPER_CLICK_UNDO)); |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 173 | }; |
| 174 | |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 175 | public NotificationInfo(Context context, AttributeSet attrs) { |
| 176 | super(context, attrs); |
| 177 | } |
| 178 | |
Geoffrey Pitsch | 5278d3d | 2017-03-29 13:39:10 -0400 | [diff] [blame] | 179 | // Specify a CheckSaveListener to override when/if the user's changes are committed. |
| 180 | public interface CheckSaveListener { |
| 181 | // Invoked when importance has changed and the NotificationInfo wants to try to save it. |
| 182 | // Listener should run saveImportance unless the change should be canceled. |
Eliot Courtney | 47098cb | 2017-10-18 17:30:30 +0900 | [diff] [blame] | 183 | void checkSave(Runnable saveImportance, StatusBarNotification sbn); |
Geoffrey Pitsch | 5278d3d | 2017-03-29 13:39:10 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Jason Monk | 2a6ea9c | 2017-01-26 11:14:51 -0500 | [diff] [blame] | 186 | public interface OnSettingsClickListener { |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 187 | void onClick(View v, NotificationChannel channel, int appUid); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 190 | public interface OnAppSettingsClickListener { |
| 191 | void onClick(View v, Intent intent); |
| 192 | } |
| 193 | |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 194 | @VisibleForTesting |
| 195 | void bindNotification( |
| 196 | final PackageManager pm, |
Jason Monk | 2a6ea9c | 2017-01-26 11:14:51 -0500 | [diff] [blame] | 197 | final INotificationManager iNotificationManager, |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 198 | final String pkg, |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 199 | final NotificationChannel notificationChannel, |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 200 | final int numUniqueChannelsInRow, |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 201 | final StatusBarNotification sbn, |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 202 | final CheckSaveListener checkSaveListener, |
| 203 | final OnSettingsClickListener onSettingsClick, |
| 204 | final OnAppSettingsClickListener onAppSettingsClick, |
Julia Reynolds | 35765d8 | 2018-08-17 11:39:19 -0400 | [diff] [blame] | 205 | boolean isDeviceProvisioned, |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 206 | boolean isNonblockable, |
Gus Prevas | caed15c | 2019-01-18 14:19:51 -0500 | [diff] [blame] | 207 | int importance, |
| 208 | boolean wasShownHighPriority) |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 209 | throws RemoteException { |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 210 | bindNotification(pm, iNotificationManager, pkg, notificationChannel, |
| 211 | numUniqueChannelsInRow, sbn, checkSaveListener, onSettingsClick, |
Julia Reynolds | 35765d8 | 2018-08-17 11:39:19 -0400 | [diff] [blame] | 212 | onAppSettingsClick, isDeviceProvisioned, isNonblockable, |
Gus Prevas | 82ad059 | 2018-11-28 14:27:40 -0500 | [diff] [blame] | 213 | false /* isBlockingHelper */, false /* isUserSentimentNegative */, |
Gus Prevas | caed15c | 2019-01-18 14:19:51 -0500 | [diff] [blame] | 214 | importance, wasShownHighPriority); |
Julia Reynolds | 0ef7d84 | 2018-01-24 17:50:31 -0500 | [diff] [blame] | 215 | } |
| 216 | |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 217 | public void bindNotification( |
| 218 | PackageManager pm, |
| 219 | INotificationManager iNotificationManager, |
| 220 | String pkg, |
| 221 | NotificationChannel notificationChannel, |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 222 | int numUniqueChannelsInRow, |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 223 | StatusBarNotification sbn, |
| 224 | CheckSaveListener checkSaveListener, |
| 225 | OnSettingsClickListener onSettingsClick, |
| 226 | OnAppSettingsClickListener onAppSettingsClick, |
Julia Reynolds | 35765d8 | 2018-08-17 11:39:19 -0400 | [diff] [blame] | 227 | boolean isDeviceProvisioned, |
Rohan Shah | 63411fc | 2018-03-28 19:05:52 -0700 | [diff] [blame] | 228 | boolean isNonblockable, |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 229 | boolean isForBlockingHelper, |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 230 | boolean isUserSentimentNegative, |
Gus Prevas | caed15c | 2019-01-18 14:19:51 -0500 | [diff] [blame] | 231 | int importance, |
| 232 | boolean wasShownHighPriority) |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 233 | throws RemoteException { |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 234 | mINotificationManager = iNotificationManager; |
Rohan Shah | da5dcdd | 2018-04-27 17:21:50 -0700 | [diff] [blame] | 235 | mMetricsLogger = Dependency.get(MetricsLogger.class); |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 236 | mPackageName = pkg; |
| 237 | mNumUniqueChannelsInRow = numUniqueChannelsInRow; |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 238 | mSbn = sbn; |
| 239 | mPm = pm; |
| 240 | mAppSettingsClickListener = onAppSettingsClick; |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 241 | mAppName = mPackageName; |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 242 | mCheckSaveListener = checkSaveListener; |
| 243 | mOnSettingsClickListener = onSettingsClick; |
| 244 | mSingleNotificationChannel = notificationChannel; |
Gus Prevas | caed15c | 2019-01-18 14:19:51 -0500 | [diff] [blame] | 245 | mStartingChannelImportance = mSingleNotificationChannel.getImportance(); |
| 246 | mWasShownHighPriority = wasShownHighPriority; |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 247 | mNegativeUserSentiment = isUserSentimentNegative; |
Rohan Shah | 63411fc | 2018-03-28 19:05:52 -0700 | [diff] [blame] | 248 | mIsNonblockable = isNonblockable; |
Julia Reynolds | e034148 | 2018-03-08 14:42:50 -0500 | [diff] [blame] | 249 | mIsForeground = |
| 250 | (mSbn.getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE) != 0; |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 251 | mIsForBlockingHelper = isForBlockingHelper; |
Rohan Shah | dbd64e7 | 2018-03-28 14:46:50 -0700 | [diff] [blame] | 252 | mAppUid = mSbn.getUid(); |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 253 | mDelegatePkg = mSbn.getOpPkg(); |
Julia Reynolds | 35765d8 | 2018-08-17 11:39:19 -0400 | [diff] [blame] | 254 | mIsDeviceProvisioned = isDeviceProvisioned; |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 255 | |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 256 | int numTotalChannels = mINotificationManager.getNumNotificationChannelsForPackage( |
Geoffrey Pitsch | d034d29 | 2017-05-12 11:59:20 -0400 | [diff] [blame] | 257 | pkg, mAppUid, false /* includeDeleted */); |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 258 | if (mNumUniqueChannelsInRow == 0) { |
Geoffrey Pitsch | d034d29 | 2017-05-12 11:59:20 -0400 | [diff] [blame] | 259 | throw new IllegalArgumentException("bindNotification requires at least one channel"); |
| 260 | } else { |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 261 | // Special behavior for the Default channel if no other channels have been defined. |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 262 | mIsSingleDefaultChannel = mNumUniqueChannelsInRow == 1 |
Rohan Shah | dbd64e7 | 2018-03-28 14:46:50 -0700 | [diff] [blame] | 263 | && mSingleNotificationChannel.getId().equals( |
| 264 | NotificationChannel.DEFAULT_CHANNEL_ID) |
| 265 | && numTotalChannels == 1; |
Geoffrey Pitsch | d034d29 | 2017-05-12 11:59:20 -0400 | [diff] [blame] | 266 | } |
| 267 | |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 268 | bindHeader(); |
| 269 | bindPrompt(); |
| 270 | bindButtons(); |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 271 | |
Eyal Posener | aea29ed | 2019-01-24 09:54:08 +0200 | [diff] [blame] | 272 | mMetricsLogger.write(getLogMaker().setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER) |
| 273 | .setType(MetricsEvent.TYPE_OPEN) |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 274 | .setSubtype(MetricsEvent.BLOCKING_HELPER_DISPLAY)); |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | private void bindHeader() throws RemoteException { |
| 278 | // Package name |
| 279 | Drawable pkgicon = null; |
| 280 | ApplicationInfo info; |
| 281 | try { |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 282 | info = mPm.getApplicationInfo( |
| 283 | mPackageName, |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 284 | PackageManager.MATCH_UNINSTALLED_PACKAGES |
| 285 | | PackageManager.MATCH_DISABLED_COMPONENTS |
| 286 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
| 287 | | PackageManager.MATCH_DIRECT_BOOT_AWARE); |
| 288 | if (info != null) { |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 289 | mAppName = String.valueOf(mPm.getApplicationLabel(info)); |
| 290 | pkgicon = mPm.getApplicationIcon(info); |
Geoffrey Pitsch | ee8c81e | 2017-03-23 11:38:56 -0400 | [diff] [blame] | 291 | } |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 292 | } catch (PackageManager.NameNotFoundException e) { |
| 293 | // app is gone, just show package name and generic icon |
| 294 | pkgicon = mPm.getDefaultActivityIcon(); |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 295 | } |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 296 | ((ImageView) findViewById(R.id.pkgicon)).setImageDrawable(pkgicon); |
Geoffrey Pitsch | d94e788 | 2017-04-06 09:52:11 -0400 | [diff] [blame] | 297 | ((TextView) findViewById(R.id.pkgname)).setText(mAppName); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 298 | |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 299 | // Delegate |
| 300 | bindDelegate(); |
Daniel Sandler | d0a52b6 | 2018-10-31 20:13:22 +0000 | [diff] [blame] | 301 | |
| 302 | // Settings button. |
| 303 | final View settingsButton = findViewById(R.id.info); |
| 304 | if (mAppUid >= 0 && mOnSettingsClickListener != null && mIsDeviceProvisioned) { |
| 305 | settingsButton.setVisibility(View.VISIBLE); |
| 306 | final int appUidF = mAppUid; |
| 307 | settingsButton.setOnClickListener( |
| 308 | (View view) -> { |
| 309 | logBlockingHelperCounter( |
| 310 | NotificationCounters.BLOCKING_HELPER_NOTIF_SETTINGS); |
| 311 | mOnSettingsClickListener.onClick(view, |
| 312 | mNumUniqueChannelsInRow > 1 ? null : mSingleNotificationChannel, |
| 313 | appUidF); |
| 314 | }); |
| 315 | } else { |
| 316 | settingsButton.setVisibility(View.GONE); |
| 317 | } |
| 318 | } |
| 319 | |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 320 | private void bindPrompt() throws RemoteException { |
Daniel Sandler | d0a52b6 | 2018-10-31 20:13:22 +0000 | [diff] [blame] | 321 | final TextView blockPrompt = findViewById(R.id.block_prompt); |
| 322 | bindName(); |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 323 | bindGroup(); |
Daniel Sandler | d0a52b6 | 2018-10-31 20:13:22 +0000 | [diff] [blame] | 324 | if (mIsNonblockable) { |
| 325 | blockPrompt.setText(R.string.notification_unblockable_desc); |
| 326 | } else { |
| 327 | if (mNegativeUserSentiment) { |
| 328 | blockPrompt.setText(R.string.inline_blocking_helper); |
| 329 | } else if (mIsSingleDefaultChannel || mNumUniqueChannelsInRow > 1) { |
| 330 | blockPrompt.setText(R.string.inline_keep_showing_app); |
| 331 | } else { |
| 332 | blockPrompt.setText(R.string.inline_keep_showing); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | private void bindName() { |
| 338 | final TextView channelName = findViewById(R.id.channel_name); |
| 339 | if (mIsSingleDefaultChannel || mNumUniqueChannelsInRow > 1) { |
| 340 | channelName.setVisibility(View.GONE); |
| 341 | } else { |
| 342 | channelName.setText(mSingleNotificationChannel.getName()); |
| 343 | } |
Julia Reynolds | ac98aea | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 344 | } |
| 345 | |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 346 | private void bindDelegate() { |
| 347 | TextView delegateView = findViewById(R.id.delegate_name); |
| 348 | TextView dividerView = findViewById(R.id.pkg_divider); |
| 349 | |
| 350 | CharSequence delegatePkg = null; |
| 351 | if (!TextUtils.equals(mPackageName, mDelegatePkg)) { |
| 352 | // this notification was posted by a delegate! |
| 353 | ApplicationInfo info; |
| 354 | try { |
| 355 | info = mPm.getApplicationInfo( |
| 356 | mDelegatePkg, |
| 357 | PackageManager.MATCH_UNINSTALLED_PACKAGES |
| 358 | | PackageManager.MATCH_DISABLED_COMPONENTS |
| 359 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
| 360 | | PackageManager.MATCH_DIRECT_BOOT_AWARE); |
| 361 | if (info != null) { |
| 362 | delegatePkg = String.valueOf(mPm.getApplicationLabel(info)); |
| 363 | } |
| 364 | } catch (PackageManager.NameNotFoundException e) { } |
| 365 | } |
| 366 | if (delegatePkg != null) { |
| 367 | delegateView.setText(mContext.getResources().getString( |
| 368 | R.string.notification_delegate_header, delegatePkg)); |
| 369 | delegateView.setVisibility(View.VISIBLE); |
| 370 | dividerView.setVisibility(View.VISIBLE); |
| 371 | } else { |
| 372 | delegateView.setVisibility(View.GONE); |
| 373 | dividerView.setVisibility(View.GONE); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | private void bindGroup() throws RemoteException { |
| 378 | // Set group information if this channel has an associated group. |
| 379 | CharSequence groupName = null; |
| 380 | if (mSingleNotificationChannel != null && mSingleNotificationChannel.getGroup() != null) { |
| 381 | final NotificationChannelGroup notificationChannelGroup = |
| 382 | mINotificationManager.getNotificationChannelGroupForPackage( |
| 383 | mSingleNotificationChannel.getGroup(), mPackageName, mAppUid); |
| 384 | if (notificationChannelGroup != null) { |
| 385 | groupName = notificationChannelGroup.getName(); |
| 386 | } |
| 387 | } |
| 388 | TextView groupNameView = findViewById(R.id.group_name); |
| 389 | TextView groupDividerView = findViewById(R.id.pkg_group_divider); |
| 390 | if (groupName != null) { |
| 391 | groupNameView.setText(groupName); |
| 392 | groupNameView.setVisibility(View.VISIBLE); |
| 393 | groupDividerView.setVisibility(View.VISIBLE); |
| 394 | } else { |
| 395 | groupNameView.setVisibility(View.GONE); |
| 396 | groupDividerView.setVisibility(View.GONE); |
| 397 | } |
| 398 | } |
| 399 | |
Rohan Shah | da5dcdd | 2018-04-27 17:21:50 -0700 | [diff] [blame] | 400 | @VisibleForTesting |
| 401 | void logBlockingHelperCounter(String counterTag) { |
| 402 | if (mIsForBlockingHelper) { |
| 403 | mMetricsLogger.count(counterTag, 1); |
| 404 | } |
| 405 | } |
| 406 | |
William Brockman | 75cf66a | 2018-12-21 13:25:01 -0500 | [diff] [blame] | 407 | /** |
| 408 | * Returns an initialized LogMaker for logging importance changes. |
| 409 | * The caller may override the type (to DISMISS) before passing it to mMetricsLogger. |
| 410 | * @return new LogMaker |
| 411 | */ |
| 412 | private LogMaker importanceChangeLogMaker() { |
Gus Prevas | caed15c | 2019-01-18 14:19:51 -0500 | [diff] [blame] | 413 | Integer chosenImportance = |
| 414 | mChosenImportance != null ? mChosenImportance : mStartingChannelImportance; |
William Brockman | 75cf66a | 2018-12-21 13:25:01 -0500 | [diff] [blame] | 415 | return new LogMaker(MetricsEvent.ACTION_SAVE_IMPORTANCE) |
| 416 | .setType(MetricsEvent.TYPE_ACTION) |
Gus Prevas | caed15c | 2019-01-18 14:19:51 -0500 | [diff] [blame] | 417 | .setSubtype(chosenImportance - mStartingChannelImportance); |
William Brockman | 75cf66a | 2018-12-21 13:25:01 -0500 | [diff] [blame] | 418 | } |
| 419 | |
Geoffrey Pitsch | 5278d3d | 2017-03-29 13:39:10 -0400 | [diff] [blame] | 420 | private boolean hasImportanceChanged() { |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 421 | return mSingleNotificationChannel != null |
Gus Prevas | caed15c | 2019-01-18 14:19:51 -0500 | [diff] [blame] | 422 | && mChosenImportance != null |
| 423 | && (mStartingChannelImportance != mChosenImportance |
| 424 | || (mWasShownHighPriority && mChosenImportance < IMPORTANCE_DEFAULT) |
| 425 | || (!mWasShownHighPriority && mChosenImportance >= IMPORTANCE_DEFAULT)); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 426 | } |
| 427 | |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 428 | private void saveImportance() { |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 429 | if (!mIsNonblockable |
| 430 | || mExitReason != NotificationCounters.BLOCKING_HELPER_STOP_NOTIFICATIONS) { |
Gus Prevas | caed15c | 2019-01-18 14:19:51 -0500 | [diff] [blame] | 431 | if (mChosenImportance == null) { |
| 432 | mChosenImportance = mStartingChannelImportance; |
| 433 | } |
Gus Prevas | 533836a | 2018-09-24 17:15:32 -0400 | [diff] [blame] | 434 | updateImportance(); |
Geoffrey Pitsch | d0856f0 | 2017-02-16 10:51:18 -0500 | [diff] [blame] | 435 | } |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Commits the updated importance values on the background thread. |
| 440 | */ |
| 441 | private void updateImportance() { |
Gus Prevas | caed15c | 2019-01-18 14:19:51 -0500 | [diff] [blame] | 442 | if (mChosenImportance != null) { |
| 443 | mMetricsLogger.write(importanceChangeLogMaker()); |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 444 | |
Gus Prevas | caed15c | 2019-01-18 14:19:51 -0500 | [diff] [blame] | 445 | Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER)); |
| 446 | bgHandler.post( |
| 447 | new UpdateImportanceRunnable(mINotificationManager, mPackageName, mAppUid, |
| 448 | mNumUniqueChannelsInRow == 1 ? mSingleNotificationChannel : null, |
| 449 | mStartingChannelImportance, mChosenImportance)); |
| 450 | } |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 453 | private void bindButtons() { |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 454 | findViewById(R.id.undo).setOnClickListener(mOnUndo); |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 455 | |
Gus Prevas | 82ad059 | 2018-11-28 14:27:40 -0500 | [diff] [blame] | 456 | boolean showInterruptivenessSettings = |
| 457 | !mIsNonblockable |
| 458 | && !mIsForeground |
| 459 | && !mIsForBlockingHelper |
| 460 | && NotificationUtils.useNewInterruptionModel(mContext); |
| 461 | if (showInterruptivenessSettings) { |
| 462 | findViewById(R.id.block_or_minimize).setVisibility(GONE); |
| 463 | findViewById(R.id.interruptiveness_settings).setVisibility(VISIBLE); |
| 464 | View block = findViewById(R.id.int_block); |
| 465 | TextView silent = findViewById(R.id.int_silent); |
| 466 | TextView alert = findViewById(R.id.int_alert); |
| 467 | |
Gus Prevas | 82ad059 | 2018-11-28 14:27:40 -0500 | [diff] [blame] | 468 | block.setOnClickListener(mOnStopOrMinimizeNotifications); |
Gus Prevas | caed15c | 2019-01-18 14:19:51 -0500 | [diff] [blame] | 469 | if (mWasShownHighPriority) { |
Gus Prevas | 82ad059 | 2018-11-28 14:27:40 -0500 | [diff] [blame] | 470 | silent.setOnClickListener(mOnToggleSilent); |
| 471 | silent.setText(R.string.inline_silent_button_silent); |
| 472 | alert.setOnClickListener(mOnKeepShowing); |
| 473 | alert.setText(R.string.inline_silent_button_keep_alerting); |
| 474 | } else { |
| 475 | silent.setOnClickListener(mOnKeepShowing); |
| 476 | silent.setText(R.string.inline_silent_button_stay_silent); |
| 477 | alert.setOnClickListener(mOnToggleSilent); |
| 478 | alert.setText(R.string.inline_silent_button_alert); |
| 479 | } |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 480 | } else { |
Gus Prevas | 82ad059 | 2018-11-28 14:27:40 -0500 | [diff] [blame] | 481 | findViewById(R.id.block_or_minimize).setVisibility(VISIBLE); |
| 482 | findViewById(R.id.interruptiveness_settings).setVisibility(GONE); |
| 483 | View block = findViewById(R.id.block); |
| 484 | TextView keep = findViewById(R.id.keep); |
| 485 | View minimize = findViewById(R.id.minimize); |
| 486 | |
| 487 | block.setOnClickListener(mOnStopOrMinimizeNotifications); |
| 488 | keep.setOnClickListener(mOnKeepShowing); |
| 489 | minimize.setOnClickListener(mOnStopOrMinimizeNotifications); |
| 490 | |
| 491 | if (mIsNonblockable) { |
| 492 | keep.setText(android.R.string.ok); |
| 493 | block.setVisibility(GONE); |
| 494 | minimize.setVisibility(GONE); |
| 495 | } else if (mIsForeground) { |
| 496 | block.setVisibility(GONE); |
| 497 | minimize.setVisibility(VISIBLE); |
| 498 | } else { |
| 499 | block.setVisibility(VISIBLE); |
| 500 | minimize.setVisibility(GONE); |
| 501 | } |
| 502 | |
| 503 | // Set up app settings link (i.e. Customize) |
| 504 | TextView settingsLinkView = findViewById(R.id.app_settings); |
| 505 | Intent settingsIntent = getAppSettingsIntent(mPm, mPackageName, |
| 506 | mSingleNotificationChannel, |
| 507 | mSbn.getId(), mSbn.getTag()); |
| 508 | if (!mIsForBlockingHelper |
| 509 | && settingsIntent != null |
| 510 | && !TextUtils.isEmpty(mSbn.getNotification().getSettingsText())) { |
| 511 | settingsLinkView.setVisibility(VISIBLE); |
| 512 | settingsLinkView.setText(mContext.getString(R.string.notification_app_settings)); |
| 513 | settingsLinkView.setOnClickListener((View view) -> { |
| 514 | mAppSettingsClickListener.onClick(view, settingsIntent); |
| 515 | }); |
| 516 | } else { |
| 517 | settingsLinkView.setVisibility(View.GONE); |
| 518 | } |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 522 | private void swapContent(@NotificationInfoAction int action, boolean animate) { |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 523 | if (mExpandAnimation != null) { |
| 524 | mExpandAnimation.cancel(); |
| 525 | } |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 526 | |
Julia Reynolds | e034148 | 2018-03-08 14:42:50 -0500 | [diff] [blame] | 527 | View prompt = findViewById(R.id.prompt); |
| 528 | ViewGroup confirmation = findViewById(R.id.confirmation); |
| 529 | TextView confirmationText = findViewById(R.id.confirmation_text); |
| 530 | View header = findViewById(R.id.header); |
| 531 | |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 532 | switch (action) { |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 533 | case ACTION_UNDO: |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 534 | mChosenImportance = mStartingChannelImportance; |
| 535 | break; |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 536 | case ACTION_TOGGLE_SILENT: |
| 537 | mExitReason = NotificationCounters.BLOCKING_HELPER_TOGGLE_SILENT; |
Gus Prevas | caed15c | 2019-01-18 14:19:51 -0500 | [diff] [blame] | 538 | if (mWasShownHighPriority) { |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 539 | mChosenImportance = IMPORTANCE_LOW; |
| 540 | confirmationText.setText(R.string.notification_channel_silenced); |
| 541 | } else { |
| 542 | mChosenImportance = IMPORTANCE_HIGH; |
| 543 | confirmationText.setText(R.string.notification_channel_unsilenced); |
| 544 | } |
| 545 | break; |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 546 | case ACTION_BLOCK: |
| 547 | mExitReason = NotificationCounters.BLOCKING_HELPER_STOP_NOTIFICATIONS; |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 548 | if (mIsForeground) { |
| 549 | mChosenImportance = IMPORTANCE_MIN; |
| 550 | confirmationText.setText(R.string.notification_channel_minimized); |
| 551 | } else { |
| 552 | mChosenImportance = IMPORTANCE_NONE; |
| 553 | confirmationText.setText(R.string.notification_channel_disabled); |
| 554 | } |
| 555 | break; |
| 556 | default: |
| 557 | throw new IllegalArgumentException(); |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 558 | } |
| 559 | |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 560 | boolean isUndo = action == ACTION_UNDO; |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 561 | |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 562 | prompt.setVisibility(isUndo ? VISIBLE : GONE); |
| 563 | confirmation.setVisibility(isUndo ? GONE : VISIBLE); |
| 564 | header.setVisibility(isUndo ? VISIBLE : GONE); |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 565 | |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 566 | if (animate) { |
| 567 | ObjectAnimator promptAnim = ObjectAnimator.ofFloat(prompt, View.ALPHA, |
| 568 | prompt.getAlpha(), isUndo ? 1f : 0f); |
| 569 | promptAnim.setInterpolator(isUndo ? Interpolators.ALPHA_IN : Interpolators.ALPHA_OUT); |
| 570 | ObjectAnimator confirmAnim = ObjectAnimator.ofFloat(confirmation, View.ALPHA, |
| 571 | confirmation.getAlpha(), isUndo ? 0f : 1f); |
| 572 | confirmAnim.setInterpolator(isUndo ? Interpolators.ALPHA_OUT : Interpolators.ALPHA_IN); |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 573 | |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 574 | mExpandAnimation = new AnimatorSet(); |
| 575 | mExpandAnimation.playTogether(promptAnim, confirmAnim); |
| 576 | mExpandAnimation.setDuration(150); |
| 577 | mExpandAnimation.addListener(new AnimatorListenerAdapter() { |
| 578 | boolean mCancelled = false; |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 579 | |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 580 | @Override |
| 581 | public void onAnimationCancel(Animator animation) { |
| 582 | mCancelled = true; |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 583 | } |
Gus Prevas | 894d915 | 2018-11-12 13:51:40 -0500 | [diff] [blame] | 584 | |
| 585 | @Override |
| 586 | public void onAnimationEnd(Animator animation) { |
| 587 | if (!mCancelled) { |
| 588 | prompt.setVisibility(isUndo ? VISIBLE : GONE); |
| 589 | confirmation.setVisibility(isUndo ? GONE : VISIBLE); |
| 590 | } |
| 591 | } |
| 592 | }); |
| 593 | mExpandAnimation.start(); |
| 594 | } |
Rohan Shah | 142e2da | 2018-06-14 13:14:18 -0700 | [diff] [blame] | 595 | |
| 596 | // Since we're swapping/update the content, reset the timeout so the UI can't close |
| 597 | // immediately after the update. |
| 598 | if (mGutsContainer != null) { |
| 599 | mGutsContainer.resetFalsingCheck(); |
| 600 | } |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 601 | } |
| 602 | |
Geoffrey Pitsch | d94e788 | 2017-04-06 09:52:11 -0400 | [diff] [blame] | 603 | @Override |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 604 | public void onFinishedClosing() { |
Gus Prevas | caed15c | 2019-01-18 14:19:51 -0500 | [diff] [blame] | 605 | if (mChosenImportance != null) { |
| 606 | mStartingChannelImportance = mChosenImportance; |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 607 | } |
| 608 | mExitReason = NotificationCounters.BLOCKING_HELPER_DISMISSED; |
| 609 | |
| 610 | View prompt = findViewById(R.id.prompt); |
| 611 | ViewGroup confirmation = findViewById(R.id.confirmation); |
| 612 | View header = findViewById(R.id.header); |
| 613 | prompt.setVisibility(VISIBLE); |
| 614 | prompt.setAlpha(1f); |
| 615 | confirmation.setVisibility(GONE); |
| 616 | confirmation.setAlpha(1f); |
| 617 | header.setVisibility(VISIBLE); |
| 618 | header.setAlpha(1f); |
Eyal Posener | aea29ed | 2019-01-24 09:54:08 +0200 | [diff] [blame] | 619 | mMetricsLogger.write(getLogMaker().setCategory(MetricsEvent.NOTIFICATION_BLOCKING_HELPER) |
| 620 | .setType(MetricsEvent.TYPE_CLOSE)); |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | @Override |
Geoffrey Pitsch | d94e788 | 2017-04-06 09:52:11 -0400 | [diff] [blame] | 624 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 625 | super.onInitializeAccessibilityEvent(event); |
| 626 | if (mGutsContainer != null && |
| 627 | event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { |
| 628 | if (mGutsContainer.isExposed()) { |
| 629 | event.getText().add(mContext.getString( |
| 630 | R.string.notification_channel_controls_opened_accessibility, mAppName)); |
| 631 | } else { |
| 632 | event.getText().add(mContext.getString( |
| 633 | R.string.notification_channel_controls_closed_accessibility, mAppName)); |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
Julia Reynolds | 3aedded | 2017-03-31 14:42:09 -0400 | [diff] [blame] | 638 | private Intent getAppSettingsIntent(PackageManager pm, String packageName, |
| 639 | NotificationChannel channel, int id, String tag) { |
| 640 | Intent intent = new Intent(Intent.ACTION_MAIN) |
| 641 | .addCategory(Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCES) |
| 642 | .setPackage(packageName); |
| 643 | final List<ResolveInfo> resolveInfos = pm.queryIntentActivities( |
| 644 | intent, |
| 645 | PackageManager.MATCH_DEFAULT_ONLY |
| 646 | ); |
| 647 | if (resolveInfos == null || resolveInfos.size() == 0 || resolveInfos.get(0) == null) { |
| 648 | return null; |
| 649 | } |
| 650 | final ActivityInfo activityInfo = resolveInfos.get(0).activityInfo; |
| 651 | intent.setClassName(activityInfo.packageName, activityInfo.name); |
| 652 | if (channel != null) { |
| 653 | intent.putExtra(Notification.EXTRA_CHANNEL_ID, channel.getId()); |
| 654 | } |
| 655 | intent.putExtra(Notification.EXTRA_NOTIFICATION_ID, id); |
| 656 | intent.putExtra(Notification.EXTRA_NOTIFICATION_TAG, tag); |
| 657 | return intent; |
| 658 | } |
| 659 | |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 660 | /** |
| 661 | * Closes the controls and commits the updated importance values (indirectly). If this view is |
| 662 | * being used to show the blocking helper, this will immediately dismiss the blocking helper and |
| 663 | * commit the updated importance. |
| 664 | * |
| 665 | * <p><b>Note,</b> this will only get called once the view is dismissing. This means that the |
| 666 | * user does not have the ability to undo the action anymore. See {@link #swapContent(boolean)} |
| 667 | * for where undo is handled. |
| 668 | */ |
Rohan Shah | 524cf7b | 2018-03-15 14:40:02 -0700 | [diff] [blame] | 669 | @VisibleForTesting |
| 670 | void closeControls(View v) { |
Rohan Shah | da5dcdd | 2018-04-27 17:21:50 -0700 | [diff] [blame] | 671 | int[] parentLoc = new int[2]; |
| 672 | int[] targetLoc = new int[2]; |
| 673 | mGutsContainer.getLocationOnScreen(parentLoc); |
| 674 | v.getLocationOnScreen(targetLoc); |
| 675 | final int centerX = v.getWidth() / 2; |
| 676 | final int centerY = v.getHeight() / 2; |
| 677 | final int x = targetLoc[0] - parentLoc[0] + centerX; |
| 678 | final int y = targetLoc[1] - parentLoc[1] + centerY; |
| 679 | mGutsContainer.closeControls(x, y, true /* save */, false /* force */); |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 680 | } |
| 681 | |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 682 | @Override |
Mady Mellor | 95d743c | 2017-01-10 12:05:27 -0800 | [diff] [blame] | 683 | public void setGutsParent(NotificationGuts guts) { |
| 684 | mGutsContainer = guts; |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | @Override |
Mady Mellor | 434180c | 2017-02-13 11:29:42 -0800 | [diff] [blame] | 688 | public boolean willBeRemoved() { |
Julia Reynolds | 437cdb1 | 2018-01-03 12:27:24 -0500 | [diff] [blame] | 689 | return hasImportanceChanged(); |
Mady Mellor | 434180c | 2017-02-13 11:29:42 -0800 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | @Override |
Lucas Dupin | 9b08c01 | 2018-05-16 19:53:32 -0700 | [diff] [blame] | 693 | public boolean shouldBeSaved() { |
| 694 | return hasImportanceChanged(); |
| 695 | } |
| 696 | |
| 697 | @Override |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 698 | public View getContentView() { |
| 699 | return this; |
| 700 | } |
| 701 | |
| 702 | @Override |
Mady Mellor | c2dbe49 | 2017-03-30 13:22:03 -0700 | [diff] [blame] | 703 | public boolean handleCloseControls(boolean save, boolean force) { |
Julia Reynolds | c65656a | 2018-02-12 09:55:14 -0500 | [diff] [blame] | 704 | // Save regardless of the importance so we can lock the importance field if the user wants |
| 705 | // to keep getting notifications |
| 706 | if (save) { |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 707 | saveImportance(); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 708 | } |
Rohan Shah | dd588c7 | 2018-05-09 20:32:15 -0700 | [diff] [blame] | 709 | logBlockingHelperCounter(mExitReason); |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 710 | return false; |
| 711 | } |
Mady Mellor | e09fb70 | 2017-03-30 13:23:29 -0700 | [diff] [blame] | 712 | |
| 713 | @Override |
| 714 | public int getActualHeight() { |
| 715 | return getHeight(); |
| 716 | } |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 717 | |
Gus Prevas | 533836a | 2018-09-24 17:15:32 -0400 | [diff] [blame] | 718 | @VisibleForTesting |
| 719 | public boolean isAnimating() { |
| 720 | return mExpandAnimation != null && mExpandAnimation.isRunning(); |
| 721 | } |
| 722 | |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 723 | /** |
| 724 | * Runnable to either update the given channel (with a new importance value) or, if no channel |
| 725 | * is provided, update notifications enabled state for the package. |
| 726 | */ |
| 727 | private static class UpdateImportanceRunnable implements Runnable { |
| 728 | private final INotificationManager mINotificationManager; |
| 729 | private final String mPackageName; |
| 730 | private final int mAppUid; |
| 731 | private final @Nullable NotificationChannel mChannelToUpdate; |
| 732 | private final int mCurrentImportance; |
| 733 | private final int mNewImportance; |
| 734 | |
| 735 | |
| 736 | public UpdateImportanceRunnable(INotificationManager notificationManager, |
| 737 | String packageName, int appUid, @Nullable NotificationChannel channelToUpdate, |
| 738 | int currentImportance, int newImportance) { |
| 739 | mINotificationManager = notificationManager; |
| 740 | mPackageName = packageName; |
| 741 | mAppUid = appUid; |
| 742 | mChannelToUpdate = channelToUpdate; |
| 743 | mCurrentImportance = currentImportance; |
| 744 | mNewImportance = newImportance; |
| 745 | } |
| 746 | |
| 747 | @Override |
| 748 | public void run() { |
| 749 | try { |
| 750 | if (mChannelToUpdate != null) { |
| 751 | mChannelToUpdate.setImportance(mNewImportance); |
| 752 | mChannelToUpdate.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); |
| 753 | mINotificationManager.updateNotificationChannelForPackage( |
| 754 | mPackageName, mAppUid, mChannelToUpdate); |
| 755 | } else { |
| 756 | // For notifications with more than one channel, update notification enabled |
| 757 | // state. If the importance was lowered, we disable notifications. |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 758 | mINotificationManager.setNotificationsEnabledWithImportanceLockForPackage( |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 759 | mPackageName, mAppUid, mNewImportance >= mCurrentImportance); |
| 760 | } |
| 761 | } catch (RemoteException e) { |
| 762 | Log.e(TAG, "Unable to update notification importance", e); |
| 763 | } |
| 764 | } |
| 765 | } |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 766 | |
| 767 | private LogMaker getLogMaker() { |
Eyal Posener | aea29ed | 2019-01-24 09:54:08 +0200 | [diff] [blame] | 768 | return mSbn.getLogMaker(); |
Eyal Posener | a9cf9c7 | 2018-12-18 16:23:54 +0200 | [diff] [blame] | 769 | } |
Mady Mellor | 87d7945 | 2017-01-10 11:52:52 -0800 | [diff] [blame] | 770 | } |