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