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