Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Joe Onorato | 79de0c5 | 2010-05-26 17:03:26 -0400 | [diff] [blame] | 17 | package com.android.systemui.statusbar; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 18 | |
Julia Reynolds | 25926af | 2018-05-01 17:05:33 -0400 | [diff] [blame] | 19 | import static android.app.Notification.CATEGORY_ALARM; |
| 20 | import static android.app.Notification.CATEGORY_CALL; |
| 21 | import static android.app.Notification.CATEGORY_EVENT; |
| 22 | import static android.app.Notification.CATEGORY_MESSAGE; |
| 23 | import static android.app.Notification.CATEGORY_REMINDER; |
Julia Reynolds | 24653c3 | 2018-03-02 13:16:37 -0500 | [diff] [blame] | 24 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT; |
| 25 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
Julia Reynolds | 24653c3 | 2018-03-02 13:16:37 -0500 | [diff] [blame] | 26 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST; |
| 27 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK; |
| 28 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_STATUS_BAR; |
| 29 | |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 30 | import android.Manifest; |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 31 | import android.app.AppGlobals; |
Christoph Studer | 37fe693 | 2014-05-26 13:10:30 +0200 | [diff] [blame] | 32 | import android.app.Notification; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 33 | import android.app.NotificationChannel; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 34 | import android.app.NotificationManager; |
Selim Cinek | b0dc61b | 2018-05-22 18:49:36 -0700 | [diff] [blame] | 35 | import android.app.Person; |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 36 | import android.content.Context; |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 37 | import android.content.pm.IPackageManager; |
| 38 | import android.content.pm.PackageManager; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 39 | import android.graphics.drawable.Icon; |
Selim Cinek | b0dc61b | 2018-05-22 18:49:36 -0700 | [diff] [blame] | 40 | import android.os.Bundle; |
| 41 | import android.os.Parcelable; |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 42 | import android.os.RemoteException; |
Selim Cinek | b18a20f | 2015-06-04 17:08:35 +0200 | [diff] [blame] | 43 | import android.os.SystemClock; |
Christoph Studer | 37fe693 | 2014-05-26 13:10:30 +0200 | [diff] [blame] | 44 | import android.service.notification.NotificationListenerService.Ranking; |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 45 | import android.service.notification.NotificationListenerService.RankingMap; |
Mady Mellor | 754d822 | 2017-01-25 15:29:39 -0800 | [diff] [blame] | 46 | import android.service.notification.SnoozeCriterion; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 47 | import android.service.notification.StatusBarNotification; |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 48 | import android.util.ArrayMap; |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 49 | import android.util.ArraySet; |
Joe Onorato | e345fff | 2010-05-23 15:18:27 -0400 | [diff] [blame] | 50 | import android.view.View; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 51 | import android.widget.ImageView; |
Julia Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 52 | import android.widget.RemoteViews; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 53 | |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 54 | import com.android.internal.annotations.VisibleForTesting; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 55 | import com.android.internal.statusbar.StatusBarIcon; |
Selim Cinek | b0dc61b | 2018-05-22 18:49:36 -0700 | [diff] [blame] | 56 | import com.android.internal.util.ArrayUtils; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 57 | import com.android.internal.util.NotificationColorUtil; |
Dan Sandler | 008cea7 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 58 | import com.android.systemui.Dependency; |
| 59 | import com.android.systemui.ForegroundServiceController; |
Selim Cinek | 1a48bab | 2017-02-17 19:38:40 -0800 | [diff] [blame] | 60 | import com.android.systemui.statusbar.notification.InflationException; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 61 | import com.android.systemui.statusbar.phone.NotificationGroupManager; |
Jason Monk | 2a6ea9c | 2017-01-26 11:14:51 -0500 | [diff] [blame] | 62 | import com.android.systemui.statusbar.phone.StatusBar; |
Selim Cinek | fbe9a44 | 2015-04-13 16:09:49 -0700 | [diff] [blame] | 63 | import com.android.systemui.statusbar.policy.HeadsUpManager; |
Julia Reynolds | 25926af | 2018-05-01 17:05:33 -0400 | [diff] [blame] | 64 | import com.android.systemui.statusbar.policy.ZenModeController; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 65 | |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 66 | import java.io.PrintWriter; |
Joe Onorato | e345fff | 2010-05-23 15:18:27 -0400 | [diff] [blame] | 67 | import java.util.ArrayList; |
Christoph Studer | 37fe693 | 2014-05-26 13:10:30 +0200 | [diff] [blame] | 68 | import java.util.Collections; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 69 | import java.util.Comparator; |
Mady Mellor | 754d822 | 2017-01-25 15:29:39 -0800 | [diff] [blame] | 70 | import java.util.List; |
Selim Cinek | 247fa01 | 2016-02-18 09:50:48 -0800 | [diff] [blame] | 71 | import java.util.Objects; |
Joe Onorato | e345fff | 2010-05-23 15:18:27 -0400 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * The list of currently displaying notifications. |
| 75 | */ |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 76 | public class NotificationData { |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 77 | |
| 78 | private final Environment mEnvironment; |
Selim Cinek | fbe9a44 | 2015-04-13 16:09:49 -0700 | [diff] [blame] | 79 | private HeadsUpManager mHeadsUpManager; |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 80 | |
Julia Reynolds | 25926af | 2018-05-01 17:05:33 -0400 | [diff] [blame] | 81 | final ZenModeController mZen = Dependency.get(ZenModeController.class); |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 82 | final ForegroundServiceController mFsc = Dependency.get(ForegroundServiceController.class); |
| 83 | |
Joe Onorato | e345fff | 2010-05-23 15:18:27 -0400 | [diff] [blame] | 84 | public static final class Entry { |
Selim Cinek | b18a20f | 2015-06-04 17:08:35 +0200 | [diff] [blame] | 85 | private static final long LAUNCH_COOLDOWN = 2000; |
Selim Cinek | 1397ea3 | 2018-01-16 17:34:52 -0800 | [diff] [blame] | 86 | private static final long REMOTE_INPUT_COOLDOWN = 500; |
Selim Cinek | b18a20f | 2015-06-04 17:08:35 +0200 | [diff] [blame] | 87 | private static final long NOT_LAUNCHED_YET = -LAUNCH_COOLDOWN; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 88 | private static final int COLOR_INVALID = 1; |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 89 | public String key; |
Joe Onorato | e345fff | 2010-05-23 15:18:27 -0400 | [diff] [blame] | 90 | public StatusBarNotification notification; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 91 | public NotificationChannel channel; |
Joe Onorato | 66b4c5b | 2010-05-23 15:39:40 -0400 | [diff] [blame] | 92 | public StatusBarIconView icon; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 93 | public StatusBarIconView expandedIcon; |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 94 | public ExpandableNotificationRow row; // the outer expanded view |
Chris Wren | f0048ce | 2013-08-07 16:43:43 -0400 | [diff] [blame] | 95 | private boolean interruption; |
Jorim Jaggi | 36b1523 | 2014-06-10 17:24:20 +0200 | [diff] [blame] | 96 | public boolean autoRedacted; // whether the redacted notification was generated by us |
Jorim Jaggi | a1eeade | 2014-09-08 22:34:39 +0200 | [diff] [blame] | 97 | public int targetSdk; |
Selim Cinek | b18a20f | 2015-06-04 17:08:35 +0200 | [diff] [blame] | 98 | private long lastFullScreenIntentLaunchTime = NOT_LAUNCHED_YET; |
Julia Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 99 | public RemoteViews cachedContentView; |
| 100 | public RemoteViews cachedBigContentView; |
| 101 | public RemoteViews cachedHeadsUpContentView; |
| 102 | public RemoteViews cachedPublicContentView; |
Adrian Roos | 0aac04f | 2016-12-08 15:59:29 -0800 | [diff] [blame] | 103 | public RemoteViews cachedAmbientContentView; |
Adrian Roos | 777ef56 | 2015-12-01 17:37:14 -0800 | [diff] [blame] | 104 | public CharSequence remoteInputText; |
Mady Mellor | 754d822 | 2017-01-25 15:29:39 -0800 | [diff] [blame] | 105 | public List<SnoozeCriterion> snoozeCriteria; |
Dan Sandler | 1d958f8 | 2018-01-09 21:10:26 -0500 | [diff] [blame] | 106 | public int userSentiment = Ranking.USER_SENTIMENT_NEUTRAL; |
| 107 | |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 108 | private int mCachedContrastColor = COLOR_INVALID; |
| 109 | private int mCachedContrastColorIsFor = COLOR_INVALID; |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 110 | private InflationTask mRunningTask = null; |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 111 | private Throwable mDebugThrowable; |
Selim Cinek | 1397ea3 | 2018-01-16 17:34:52 -0800 | [diff] [blame] | 112 | public CharSequence remoteInputTextWhenReset; |
| 113 | public long lastRemoteInputSent = NOT_LAUNCHED_YET; |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 114 | public ArraySet<Integer> mActiveAppOps = new ArraySet<>(3); |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 115 | public CharSequence headsUpStatusBarText; |
| 116 | public CharSequence headsUpStatusBarTextPublic; |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 117 | /** |
| 118 | * Whether or not this row represents a system notification. Note that if this is |
| 119 | * {@code null}, that means we were either unable to retrieve the info or have yet to |
| 120 | * retrieve the info. |
| 121 | */ |
| 122 | public Boolean mIsSystemNotification; |
Jorim Jaggi | 36b1523 | 2014-06-10 17:24:20 +0200 | [diff] [blame] | 123 | |
Selim Cinek | b0dc61b | 2018-05-22 18:49:36 -0700 | [diff] [blame] | 124 | /** |
| 125 | * Has the user sent a reply through this Notification. |
| 126 | */ |
| 127 | private boolean hasSentReply; |
| 128 | |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 129 | public Entry(StatusBarNotification n) { |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 130 | this.key = n.getKey(); |
Daniel Sandler | 3eebd1f | 2010-07-27 08:39:33 -0400 | [diff] [blame] | 131 | this.notification = n; |
Daniel Sandler | 3eebd1f | 2010-07-27 08:39:33 -0400 | [diff] [blame] | 132 | } |
Chris Wren | f0048ce | 2013-08-07 16:43:43 -0400 | [diff] [blame] | 133 | |
| 134 | public void setInterruption() { |
| 135 | interruption = true; |
| 136 | } |
Chris Wren | d04f6ce | 2014-06-11 17:37:28 -0400 | [diff] [blame] | 137 | |
| 138 | public boolean hasInterrupted() { |
| 139 | return interruption; |
| 140 | } |
Christoph Studer | 22f2ee5 | 2014-07-29 22:57:21 +0200 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Resets the notification entry to be re-used. |
| 144 | */ |
| 145 | public void reset() { |
Christoph Studer | 22f2ee5 | 2014-07-29 22:57:21 +0200 | [diff] [blame] | 146 | if (row != null) { |
| 147 | row.reset(); |
| 148 | } |
| 149 | } |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 150 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 151 | public View getExpandedContentView() { |
| 152 | return row.getPrivateLayout().getExpandedChild(); |
| 153 | } |
| 154 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 155 | public View getPublicContentView() { |
| 156 | return row.getPublicLayout().getContractedChild(); |
| 157 | } |
Selim Cinek | b18a20f | 2015-06-04 17:08:35 +0200 | [diff] [blame] | 158 | |
| 159 | public void notifyFullScreenIntentLaunched() { |
Selim Cinek | da371df | 2017-07-21 16:18:21 -0700 | [diff] [blame] | 160 | setInterruption(); |
Selim Cinek | b18a20f | 2015-06-04 17:08:35 +0200 | [diff] [blame] | 161 | lastFullScreenIntentLaunchTime = SystemClock.elapsedRealtime(); |
| 162 | } |
| 163 | |
| 164 | public boolean hasJustLaunchedFullScreenIntent() { |
| 165 | return SystemClock.elapsedRealtime() < lastFullScreenIntentLaunchTime + LAUNCH_COOLDOWN; |
| 166 | } |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 167 | |
Selim Cinek | 1397ea3 | 2018-01-16 17:34:52 -0800 | [diff] [blame] | 168 | public boolean hasJustSentRemoteInput() { |
| 169 | return SystemClock.elapsedRealtime() < lastRemoteInputSent + REMOTE_INPUT_COOLDOWN; |
| 170 | } |
| 171 | |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 172 | /** |
| 173 | * Create the icons for a notification |
| 174 | * @param context the context to create the icons with |
| 175 | * @param sbn the notification |
Selim Cinek | 1a48bab | 2017-02-17 19:38:40 -0800 | [diff] [blame] | 176 | * @throws InflationException |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 177 | */ |
Selim Cinek | 1a48bab | 2017-02-17 19:38:40 -0800 | [diff] [blame] | 178 | public void createIcons(Context context, StatusBarNotification sbn) |
| 179 | throws InflationException { |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 180 | Notification n = sbn.getNotification(); |
| 181 | final Icon smallIcon = n.getSmallIcon(); |
| 182 | if (smallIcon == null) { |
Selim Cinek | 1a48bab | 2017-02-17 19:38:40 -0800 | [diff] [blame] | 183 | throw new InflationException("No small icon in notification from " |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 184 | + sbn.getPackageName()); |
| 185 | } |
| 186 | |
| 187 | // Construct the icon. |
| 188 | icon = new StatusBarIconView(context, |
Selim Cinek | 72fc8db | 2017-06-06 18:07:47 -0700 | [diff] [blame] | 189 | sbn.getPackageName() + "/0x" + Integer.toHexString(sbn.getId()), sbn); |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 190 | icon.setScaleType(ImageView.ScaleType.CENTER_INSIDE); |
| 191 | |
| 192 | // Construct the expanded icon. |
| 193 | expandedIcon = new StatusBarIconView(context, |
Selim Cinek | 72fc8db | 2017-06-06 18:07:47 -0700 | [diff] [blame] | 194 | sbn.getPackageName() + "/0x" + Integer.toHexString(sbn.getId()), sbn); |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 195 | expandedIcon.setScaleType(ImageView.ScaleType.CENTER_INSIDE); |
| 196 | final StatusBarIcon ic = new StatusBarIcon( |
| 197 | sbn.getUser(), |
| 198 | sbn.getPackageName(), |
| 199 | smallIcon, |
| 200 | n.iconLevel, |
| 201 | n.number, |
| 202 | StatusBarIconView.contentDescForNotification(context, n)); |
| 203 | if (!icon.set(ic) || !expandedIcon.set(ic)) { |
| 204 | icon = null; |
| 205 | expandedIcon = null; |
Selim Cinek | 1a48bab | 2017-02-17 19:38:40 -0800 | [diff] [blame] | 206 | throw new InflationException("Couldn't create icon: " + ic); |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 207 | } |
Selim Cinek | 6598974 | 2016-12-09 16:05:38 -0800 | [diff] [blame] | 208 | expandedIcon.setVisibility(View.INVISIBLE); |
Selim Cinek | 2b549f4 | 2016-11-22 16:38:51 -0800 | [diff] [blame] | 209 | expandedIcon.setOnVisibilityChangedListener( |
| 210 | newVisibility -> { |
| 211 | if (row != null) { |
| 212 | row.setIconsVisible(newVisibility != View.VISIBLE); |
| 213 | } |
| 214 | }); |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | public void setIconTag(int key, Object tag) { |
| 218 | if (icon != null) { |
| 219 | icon.setTag(key, tag); |
| 220 | expandedIcon.setTag(key, tag); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Update the notification icons. |
| 226 | * @param context the context to create the icons with. |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 227 | * @param sbn the notification to read the icon from. |
Selim Cinek | 1a48bab | 2017-02-17 19:38:40 -0800 | [diff] [blame] | 228 | * @throws InflationException |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 229 | */ |
Selim Cinek | 72fc8db | 2017-06-06 18:07:47 -0700 | [diff] [blame] | 230 | public void updateIcons(Context context, StatusBarNotification sbn) |
| 231 | throws InflationException { |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 232 | if (icon != null) { |
| 233 | // Update the icon |
Selim Cinek | 72fc8db | 2017-06-06 18:07:47 -0700 | [diff] [blame] | 234 | Notification n = sbn.getNotification(); |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 235 | final StatusBarIcon ic = new StatusBarIcon( |
| 236 | notification.getUser(), |
| 237 | notification.getPackageName(), |
| 238 | n.getSmallIcon(), |
| 239 | n.iconLevel, |
| 240 | n.number, |
| 241 | StatusBarIconView.contentDescForNotification(context, n)); |
Selim Cinek | 72fc8db | 2017-06-06 18:07:47 -0700 | [diff] [blame] | 242 | icon.setNotification(sbn); |
| 243 | expandedIcon.setNotification(sbn); |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 244 | if (!icon.set(ic) || !expandedIcon.set(ic)) { |
Selim Cinek | 1a48bab | 2017-02-17 19:38:40 -0800 | [diff] [blame] | 245 | throw new InflationException("Couldn't update icon: " + ic); |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
Selim Cinek | ac5f027 | 2017-05-02 16:05:41 -0700 | [diff] [blame] | 250 | public int getContrastedColor(Context context, boolean isLowPriority, |
| 251 | int backgroundColor) { |
| 252 | int rawColor = isLowPriority ? Notification.COLOR_DEFAULT : |
Selim Cinek | 6743c0b | 2017-01-18 18:24:01 -0800 | [diff] [blame] | 253 | notification.getNotification().color; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 254 | if (mCachedContrastColorIsFor == rawColor && mCachedContrastColor != COLOR_INVALID) { |
| 255 | return mCachedContrastColor; |
| 256 | } |
Selim Cinek | ac5f027 | 2017-05-02 16:05:41 -0700 | [diff] [blame] | 257 | final int contrasted = NotificationColorUtil.resolveContrastColor(context, rawColor, |
| 258 | backgroundColor); |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 259 | mCachedContrastColorIsFor = rawColor; |
| 260 | mCachedContrastColor = contrasted; |
| 261 | return mCachedContrastColor; |
| 262 | } |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 263 | |
| 264 | /** |
| 265 | * Abort all existing inflation tasks |
| 266 | */ |
Selim Cinek | 0f66a4c | 2017-04-28 19:26:28 -0700 | [diff] [blame] | 267 | public void abortTask() { |
Selim Cinek | 01d3da6 | 2017-04-28 15:03:48 -0700 | [diff] [blame] | 268 | if (mRunningTask != null) { |
| 269 | mRunningTask.abort(); |
| 270 | mRunningTask = null; |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 274 | public void setInflationTask(InflationTask abortableTask) { |
Selim Cinek | 01d3da6 | 2017-04-28 15:03:48 -0700 | [diff] [blame] | 275 | // abort any existing inflation |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 276 | InflationTask existing = mRunningTask; |
Selim Cinek | 0f66a4c | 2017-04-28 19:26:28 -0700 | [diff] [blame] | 277 | abortTask(); |
| 278 | mRunningTask = abortableTask; |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 279 | if (existing != null && mRunningTask != null) { |
| 280 | mRunningTask.supersedeTask(existing); |
| 281 | } |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 282 | } |
Selim Cinek | dc1231c | 2017-04-27 17:30:50 -0700 | [diff] [blame] | 283 | |
Selim Cinek | 01d3da6 | 2017-04-28 15:03:48 -0700 | [diff] [blame] | 284 | public void onInflationTaskFinished() { |
| 285 | mRunningTask = null; |
Selim Cinek | dc1231c | 2017-04-27 17:30:50 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | @VisibleForTesting |
Selim Cinek | 67ff248 | 2017-05-25 10:27:28 -0700 | [diff] [blame] | 289 | public InflationTask getRunningTask() { |
Selim Cinek | 01d3da6 | 2017-04-28 15:03:48 -0700 | [diff] [blame] | 290 | return mRunningTask; |
Selim Cinek | dc1231c | 2017-04-27 17:30:50 -0700 | [diff] [blame] | 291 | } |
Selim Cinek | 04be389 | 2017-08-08 10:58:32 -0700 | [diff] [blame] | 292 | |
| 293 | /** |
| 294 | * Set a throwable that is used for debugging |
| 295 | * |
| 296 | * @param debugThrowable the throwable to save |
| 297 | */ |
| 298 | public void setDebugThrowable(Throwable debugThrowable) { |
| 299 | mDebugThrowable = debugThrowable; |
| 300 | } |
| 301 | |
| 302 | public Throwable getDebugThrowable() { |
| 303 | return mDebugThrowable; |
| 304 | } |
Selim Cinek | 90d11a1 | 2018-01-17 16:24:36 -0800 | [diff] [blame] | 305 | |
| 306 | public void onRemoteInputInserted() { |
| 307 | lastRemoteInputSent = NOT_LAUNCHED_YET; |
| 308 | remoteInputTextWhenReset = null; |
| 309 | } |
Selim Cinek | b0dc61b | 2018-05-22 18:49:36 -0700 | [diff] [blame] | 310 | |
| 311 | public void setHasSentReply() { |
| 312 | hasSentReply = true; |
| 313 | } |
| 314 | |
| 315 | public boolean isLastMessageFromReply() { |
| 316 | if (!hasSentReply) { |
| 317 | return false; |
| 318 | } |
| 319 | Bundle extras = notification.getNotification().extras; |
| 320 | CharSequence[] replyTexts = extras.getCharSequenceArray( |
| 321 | Notification.EXTRA_REMOTE_INPUT_HISTORY); |
| 322 | if (!ArrayUtils.isEmpty(replyTexts)) { |
| 323 | return true; |
| 324 | } |
| 325 | Parcelable[] messages = extras.getParcelableArray(Notification.EXTRA_MESSAGES); |
| 326 | if (messages != null && messages.length > 0) { |
| 327 | Parcelable message = messages[messages.length - 1]; |
| 328 | if (message instanceof Bundle) { |
| 329 | Notification.MessagingStyle.Message lastMessage = |
| 330 | Notification.MessagingStyle.Message.getMessageFromBundle( |
| 331 | (Bundle) message); |
| 332 | if (lastMessage != null) { |
| 333 | Person senderPerson = lastMessage.getSenderPerson(); |
| 334 | if (senderPerson == null) { |
| 335 | return true; |
| 336 | } |
| 337 | Person user = extras.getParcelable(Notification.EXTRA_MESSAGING_PERSON); |
| 338 | return Objects.equals(user, senderPerson); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | return false; |
| 343 | } |
Joe Onorato | e345fff | 2010-05-23 15:18:27 -0400 | [diff] [blame] | 344 | } |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 345 | |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 346 | private final ArrayMap<String, Entry> mEntries = new ArrayMap<>(); |
| 347 | private final ArrayList<Entry> mSortedAndFiltered = new ArrayList<>(); |
Dieter Hsu | 36e1ebc | 2018-06-06 15:41:46 +0800 | [diff] [blame] | 348 | private final ArrayList<Entry> mFilteredForUser = new ArrayList<>(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 349 | |
| 350 | private NotificationGroupManager mGroupManager; |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 351 | |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 352 | private RankingMap mRankingMap; |
| 353 | private final Ranking mTmpRanking = new Ranking(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 354 | |
Selim Cinek | fbe9a44 | 2015-04-13 16:09:49 -0700 | [diff] [blame] | 355 | public void setHeadsUpManager(HeadsUpManager headsUpManager) { |
| 356 | mHeadsUpManager = headsUpManager; |
| 357 | } |
| 358 | |
Christoph Studer | 37fe693 | 2014-05-26 13:10:30 +0200 | [diff] [blame] | 359 | private final Comparator<Entry> mRankingComparator = new Comparator<Entry>() { |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 360 | private final Ranking mRankingA = new Ranking(); |
| 361 | private final Ranking mRankingB = new Ranking(); |
| 362 | |
Christoph Studer | 37fe693 | 2014-05-26 13:10:30 +0200 | [diff] [blame] | 363 | @Override |
Daniel Sandler | 379020a | 2010-07-29 16:20:06 -0400 | [diff] [blame] | 364 | public int compare(Entry a, Entry b) { |
Christoph Studer | 1492116 | 2014-09-03 12:51:26 +0200 | [diff] [blame] | 365 | final StatusBarNotification na = a.notification; |
| 366 | final StatusBarNotification nb = b.notification; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 367 | int aImportance = NotificationManager.IMPORTANCE_DEFAULT; |
| 368 | int bImportance = NotificationManager.IMPORTANCE_DEFAULT; |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 369 | int aRank = 0; |
| 370 | int bRank = 0; |
| 371 | |
| 372 | if (mRankingMap != null) { |
| 373 | // RankingMap as received from NoMan |
Kensuke Matsui | efa1a74 | 2017-08-03 12:12:36 +0900 | [diff] [blame] | 374 | getRanking(a.key, mRankingA); |
| 375 | getRanking(b.key, mRankingB); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 376 | aImportance = mRankingA.getImportance(); |
| 377 | bImportance = mRankingB.getImportance(); |
| 378 | aRank = mRankingA.getRank(); |
| 379 | bRank = mRankingB.getRank(); |
| 380 | } |
Christoph Studer | 1492116 | 2014-09-03 12:51:26 +0200 | [diff] [blame] | 381 | |
Dan Sandler | 4e78706 | 2015-06-17 15:09:48 -0400 | [diff] [blame] | 382 | String mediaNotification = mEnvironment.getCurrentMediaNotificationKey(); |
| 383 | |
Julia Reynolds | f0f629f | 2016-02-25 09:34:04 -0500 | [diff] [blame] | 384 | // IMPORTANCE_MIN media streams are allowed to drift to the bottom |
Dan Sandler | 4e78706 | 2015-06-17 15:09:48 -0400 | [diff] [blame] | 385 | final boolean aMedia = a.key.equals(mediaNotification) |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 386 | && aImportance > NotificationManager.IMPORTANCE_MIN; |
Dan Sandler | 4e78706 | 2015-06-17 15:09:48 -0400 | [diff] [blame] | 387 | final boolean bMedia = b.key.equals(mediaNotification) |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 388 | && bImportance > NotificationManager.IMPORTANCE_MIN; |
Dan Sandler | 4e78706 | 2015-06-17 15:09:48 -0400 | [diff] [blame] | 389 | |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 390 | boolean aSystemMax = aImportance >= NotificationManager.IMPORTANCE_HIGH && |
Christoph Studer | 1492116 | 2014-09-03 12:51:26 +0200 | [diff] [blame] | 391 | isSystemNotification(na); |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 392 | boolean bSystemMax = bImportance >= NotificationManager.IMPORTANCE_HIGH && |
Christoph Studer | 1492116 | 2014-09-03 12:51:26 +0200 | [diff] [blame] | 393 | isSystemNotification(nb); |
Christoph Studer | 1492116 | 2014-09-03 12:51:26 +0200 | [diff] [blame] | 394 | |
Selim Cinek | fbe9a44 | 2015-04-13 16:09:49 -0700 | [diff] [blame] | 395 | boolean isHeadsUp = a.row.isHeadsUp(); |
| 396 | if (isHeadsUp != b.row.isHeadsUp()) { |
| 397 | return isHeadsUp ? -1 : 1; |
| 398 | } else if (isHeadsUp) { |
| 399 | // Provide consistent ranking with headsUpManager |
| 400 | return mHeadsUpManager.compare(a, b); |
| 401 | } else if (aMedia != bMedia) { |
| 402 | // Upsort current media notification. |
| 403 | return aMedia ? -1 : 1; |
| 404 | } else if (aSystemMax != bSystemMax) { |
| 405 | // Upsort PRIORITY_MAX system notifications |
| 406 | return aSystemMax ? -1 : 1; |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 407 | } else if (aRank != bRank) { |
| 408 | return aRank - bRank; |
Chris Wren | f0048ce | 2013-08-07 16:43:43 -0400 | [diff] [blame] | 409 | } else { |
Ian Rogers | 31756df | 2016-06-06 14:49:30 -0700 | [diff] [blame] | 410 | return Long.compare(nb.getNotification().when, na.getNotification().when); |
Chris Wren | f0048ce | 2013-08-07 16:43:43 -0400 | [diff] [blame] | 411 | } |
Daniel Sandler | 379020a | 2010-07-29 16:20:06 -0400 | [diff] [blame] | 412 | } |
| 413 | }; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 414 | |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 415 | public NotificationData(Environment environment) { |
| 416 | mEnvironment = environment; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 417 | mGroupManager = environment.getGroupManager(); |
Joe Onorato | e345fff | 2010-05-23 15:18:27 -0400 | [diff] [blame] | 418 | } |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 419 | |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 420 | /** |
| 421 | * Returns the sorted list of active notifications (depending on {@link Environment} |
| 422 | * |
| 423 | * <p> |
| 424 | * This call doesn't update the list of active notifications. Call {@link #filterAndSort()} |
| 425 | * when the environment changes. |
| 426 | * <p> |
| 427 | * Don't hold on to or modify the returned list. |
| 428 | */ |
| 429 | public ArrayList<Entry> getActiveNotifications() { |
| 430 | return mSortedAndFiltered; |
Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 431 | } |
| 432 | |
Dieter Hsu | 36e1ebc | 2018-06-06 15:41:46 +0800 | [diff] [blame] | 433 | public ArrayList<Entry> getNotificationsForCurrentUser() { |
| 434 | mFilteredForUser.clear(); |
| 435 | |
| 436 | synchronized (mEntries) { |
| 437 | final int N = mEntries.size(); |
| 438 | for (int i = 0; i < N; i++) { |
| 439 | Entry entry = mEntries.valueAt(i); |
| 440 | final StatusBarNotification sbn = entry.notification; |
| 441 | if (!mEnvironment.isNotificationForCurrentProfiles(sbn)) { |
| 442 | continue; |
| 443 | } |
| 444 | mFilteredForUser.add(entry); |
| 445 | } |
| 446 | } |
| 447 | return mFilteredForUser; |
| 448 | } |
| 449 | |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 450 | public Entry get(String key) { |
| 451 | return mEntries.get(key); |
Joe Onorato | 0e26dff | 2010-05-24 16:17:02 -0400 | [diff] [blame] | 452 | } |
| 453 | |
Selim Cinek | 2630dc7 | 2017-04-20 15:16:10 -0700 | [diff] [blame] | 454 | public void add(Entry entry) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 455 | synchronized (mEntries) { |
| 456 | mEntries.put(entry.notification.getKey(), entry); |
| 457 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 458 | mGroupManager.onEntryAdded(entry); |
Dan Sandler | 008cea7 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 459 | |
Selim Cinek | 608a57a | 2017-04-28 16:50:41 -0700 | [diff] [blame] | 460 | updateRankingAndSort(mRankingMap); |
Daniel Sandler | 3eebd1f | 2010-07-27 08:39:33 -0400 | [diff] [blame] | 461 | } |
| 462 | |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 463 | public Entry remove(String key, RankingMap ranking) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 464 | Entry removed = null; |
| 465 | synchronized (mEntries) { |
| 466 | removed = mEntries.remove(key); |
| 467 | } |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 468 | if (removed == null) return null; |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 469 | mGroupManager.onEntryRemoved(removed); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 470 | updateRankingAndSort(ranking); |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 471 | return removed; |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 472 | } |
Joe Onorato | 20da8f8 | 2010-05-24 16:39:29 -0400 | [diff] [blame] | 473 | |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 474 | public void updateRanking(RankingMap ranking) { |
Christoph Studer | 37fe693 | 2014-05-26 13:10:30 +0200 | [diff] [blame] | 475 | updateRankingAndSort(ranking); |
| 476 | } |
| 477 | |
Julia Reynolds | 9159006 | 2018-04-02 16:24:11 -0400 | [diff] [blame] | 478 | public void updateAppOp(int appOp, int uid, String pkg, String key, boolean showIcon) { |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 479 | synchronized (mEntries) { |
| 480 | final int N = mEntries.size(); |
| 481 | for (int i = 0; i < N; i++) { |
| 482 | Entry entry = mEntries.valueAt(i); |
| 483 | if (uid == entry.notification.getUid() |
Julia Reynolds | 9159006 | 2018-04-02 16:24:11 -0400 | [diff] [blame] | 484 | && pkg.equals(entry.notification.getPackageName()) |
| 485 | && key.equals(entry.key)) { |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 486 | if (showIcon) { |
| 487 | entry.mActiveAppOps.add(appOp); |
| 488 | } else { |
| 489 | entry.mActiveAppOps.remove(appOp); |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
Christoph Studer | 37fe693 | 2014-05-26 13:10:30 +0200 | [diff] [blame] | 496 | public boolean isAmbient(String key) { |
John Spurlock | f079fc5 | 2014-08-09 11:10:03 -0400 | [diff] [blame] | 497 | if (mRankingMap != null) { |
Kensuke Matsui | efa1a74 | 2017-08-03 12:12:36 +0900 | [diff] [blame] | 498 | getRanking(key, mTmpRanking); |
John Spurlock | f079fc5 | 2014-08-09 11:10:03 -0400 | [diff] [blame] | 499 | return mTmpRanking.isAmbient(); |
| 500 | } |
| 501 | return false; |
Christoph Studer | 37fe693 | 2014-05-26 13:10:30 +0200 | [diff] [blame] | 502 | } |
| 503 | |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 504 | public int getVisibilityOverride(String key) { |
| 505 | if (mRankingMap != null) { |
Kensuke Matsui | efa1a74 | 2017-08-03 12:12:36 +0900 | [diff] [blame] | 506 | getRanking(key, mTmpRanking); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 507 | return mTmpRanking.getVisibilityOverride(); |
| 508 | } |
Julia Reynolds | ead00aa | 2015-12-07 08:23:48 -0500 | [diff] [blame] | 509 | return Ranking.VISIBILITY_NO_OVERRIDE; |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 510 | } |
| 511 | |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 512 | public boolean shouldSuppressFullScreenIntent(Entry entry) { |
| 513 | return shouldSuppressVisualEffect(entry, SUPPRESSED_EFFECT_FULL_SCREEN_INTENT); |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 514 | } |
| 515 | |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 516 | public boolean shouldSuppressPeek(Entry entry) { |
| 517 | return shouldSuppressVisualEffect(entry, SUPPRESSED_EFFECT_PEEK); |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 518 | } |
| 519 | |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 520 | public boolean shouldSuppressStatusBar(Entry entry) { |
| 521 | return shouldSuppressVisualEffect(entry, SUPPRESSED_EFFECT_STATUS_BAR); |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 522 | } |
| 523 | |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 524 | public boolean shouldSuppressAmbient(Entry entry) { |
| 525 | return shouldSuppressVisualEffect(entry, SUPPRESSED_EFFECT_AMBIENT); |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 526 | } |
| 527 | |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 528 | public boolean shouldSuppressNotificationList(Entry entry) { |
| 529 | return shouldSuppressVisualEffect(entry, SUPPRESSED_EFFECT_NOTIFICATION_LIST); |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 530 | } |
| 531 | |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 532 | private boolean shouldSuppressVisualEffect(Entry entry, int effect) { |
| 533 | if (isExemptFromDndVisualSuppression(entry)) { |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 534 | return false; |
| 535 | } |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 536 | String key = entry.key; |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 537 | if (mRankingMap != null) { |
Kensuke Matsui | efa1a74 | 2017-08-03 12:12:36 +0900 | [diff] [blame] | 538 | getRanking(key, mTmpRanking); |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 539 | return (mTmpRanking.getSuppressedVisualEffects() & effect) != 0; |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 540 | } |
| 541 | return false; |
| 542 | } |
| 543 | |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 544 | protected boolean isExemptFromDndVisualSuppression(Entry entry) { |
Julia Reynolds | 25926af | 2018-05-01 17:05:33 -0400 | [diff] [blame] | 545 | if (isNotificationBlockedByPolicy(entry.notification.getNotification())) { |
| 546 | return false; |
| 547 | } |
| 548 | |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 549 | if ((entry.notification.getNotification().flags |
| 550 | & Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 551 | return true; |
Julia Reynolds | 24653c3 | 2018-03-02 13:16:37 -0500 | [diff] [blame] | 552 | } |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 553 | if (entry.notification.getNotification().isMediaNotification()) { |
| 554 | return true; |
| 555 | } |
| 556 | if (entry.mIsSystemNotification != null && entry.mIsSystemNotification) { |
Julia Reynolds | 5bbb6da | 2018-03-28 10:48:37 -0400 | [diff] [blame] | 557 | return true; |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 558 | } |
| 559 | return false; |
| 560 | } |
| 561 | |
Julia Reynolds | 25926af | 2018-05-01 17:05:33 -0400 | [diff] [blame] | 562 | /** |
| 563 | * Categories that are explicitly called out on DND settings screens are always blocked, if |
| 564 | * DND has flagged them, even if they are foreground or system notifications that might |
| 565 | * otherwise visually bypass DND. |
| 566 | */ |
| 567 | protected boolean isNotificationBlockedByPolicy(Notification n) { |
| 568 | if (isCategory(CATEGORY_CALL, n) |
| 569 | || isCategory(CATEGORY_MESSAGE, n) |
| 570 | || isCategory(CATEGORY_ALARM, n) |
| 571 | || isCategory(CATEGORY_EVENT, n) |
| 572 | || isCategory(CATEGORY_REMINDER, n)) { |
| 573 | return true; |
| 574 | } |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | private boolean isCategory(String category, Notification n) { |
| 579 | return Objects.equals(n.category, category); |
| 580 | } |
| 581 | |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 582 | public int getImportance(String key) { |
| 583 | if (mRankingMap != null) { |
Kensuke Matsui | efa1a74 | 2017-08-03 12:12:36 +0900 | [diff] [blame] | 584 | getRanking(key, mTmpRanking); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 585 | return mTmpRanking.getImportance(); |
| 586 | } |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 587 | return NotificationManager.IMPORTANCE_UNSPECIFIED; |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 588 | } |
| 589 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 590 | public String getOverrideGroupKey(String key) { |
| 591 | if (mRankingMap != null) { |
Kensuke Matsui | efa1a74 | 2017-08-03 12:12:36 +0900 | [diff] [blame] | 592 | getRanking(key, mTmpRanking); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 593 | return mTmpRanking.getOverrideGroupKey(); |
| 594 | } |
| 595 | return null; |
| 596 | } |
| 597 | |
Mady Mellor | 754d822 | 2017-01-25 15:29:39 -0800 | [diff] [blame] | 598 | public List<SnoozeCriterion> getSnoozeCriteria(String key) { |
| 599 | if (mRankingMap != null) { |
Kensuke Matsui | efa1a74 | 2017-08-03 12:12:36 +0900 | [diff] [blame] | 600 | getRanking(key, mTmpRanking); |
Mady Mellor | 754d822 | 2017-01-25 15:29:39 -0800 | [diff] [blame] | 601 | return mTmpRanking.getSnoozeCriteria(); |
| 602 | } |
| 603 | return null; |
| 604 | } |
| 605 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 606 | public NotificationChannel getChannel(String key) { |
| 607 | if (mRankingMap != null) { |
Kensuke Matsui | efa1a74 | 2017-08-03 12:12:36 +0900 | [diff] [blame] | 608 | getRanking(key, mTmpRanking); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 609 | return mTmpRanking.getChannel(); |
| 610 | } |
| 611 | return null; |
| 612 | } |
| 613 | |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 614 | public int getRank(String key) { |
| 615 | if (mRankingMap != null) { |
| 616 | getRanking(key, mTmpRanking); |
| 617 | return mTmpRanking.getRank(); |
| 618 | } |
| 619 | return 0; |
| 620 | } |
| 621 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 622 | public boolean shouldHide(String key) { |
| 623 | if (mRankingMap != null) { |
| 624 | getRanking(key, mTmpRanking); |
| 625 | return mTmpRanking.isSuspended(); |
| 626 | } |
| 627 | return false; |
| 628 | } |
| 629 | |
Christoph Studer | d0694b6 | 2014-06-04 16:36:01 +0200 | [diff] [blame] | 630 | private void updateRankingAndSort(RankingMap ranking) { |
Christoph Studer | 37fe693 | 2014-05-26 13:10:30 +0200 | [diff] [blame] | 631 | if (ranking != null) { |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 632 | mRankingMap = ranking; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 633 | synchronized (mEntries) { |
| 634 | final int N = mEntries.size(); |
| 635 | for (int i = 0; i < N; i++) { |
| 636 | Entry entry = mEntries.valueAt(i); |
Kensuke Matsui | efa1a74 | 2017-08-03 12:12:36 +0900 | [diff] [blame] | 637 | if (!getRanking(entry.key, mTmpRanking)) { |
| 638 | continue; |
| 639 | } |
Selim Cinek | 7f09ce1 | 2017-06-08 13:20:08 -0700 | [diff] [blame] | 640 | final StatusBarNotification oldSbn = entry.notification.cloneLight(); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 641 | final String overrideGroupKey = getOverrideGroupKey(entry.key); |
| 642 | if (!Objects.equals(oldSbn.getOverrideGroupKey(), overrideGroupKey)) { |
| 643 | entry.notification.setOverrideGroupKey(overrideGroupKey); |
| 644 | mGroupManager.onEntryUpdated(entry, oldSbn); |
| 645 | } |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 646 | entry.channel = getChannel(entry.key); |
Mady Mellor | 754d822 | 2017-01-25 15:29:39 -0800 | [diff] [blame] | 647 | entry.snoozeCriteria = getSnoozeCriteria(entry.key); |
Dan Sandler | 1d958f8 | 2018-01-09 21:10:26 -0500 | [diff] [blame] | 648 | entry.userSentiment = mTmpRanking.getUserSentiment(); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 649 | } |
| 650 | } |
Christoph Studer | 37fe693 | 2014-05-26 13:10:30 +0200 | [diff] [blame] | 651 | } |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 652 | filterAndSort(); |
Christoph Studer | 37fe693 | 2014-05-26 13:10:30 +0200 | [diff] [blame] | 653 | } |
| 654 | |
Kensuke Matsui | efa1a74 | 2017-08-03 12:12:36 +0900 | [diff] [blame] | 655 | /** |
| 656 | * Get the ranking from the current ranking map. |
| 657 | * |
| 658 | * @param key the key to look up |
| 659 | * @param outRanking the ranking to populate |
| 660 | * |
| 661 | * @return {@code true} if the ranking was properly obtained. |
| 662 | */ |
| 663 | @VisibleForTesting |
| 664 | protected boolean getRanking(String key, Ranking outRanking) { |
| 665 | return mRankingMap.getRanking(key, outRanking); |
| 666 | } |
| 667 | |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 668 | // TODO: This should not be public. Instead the Environment should notify this class when |
| 669 | // anything changed, and this class should call back the UI so it updates itself. |
| 670 | public void filterAndSort() { |
| 671 | mSortedAndFiltered.clear(); |
| 672 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 673 | synchronized (mEntries) { |
| 674 | final int N = mEntries.size(); |
| 675 | for (int i = 0; i < N; i++) { |
| 676 | Entry entry = mEntries.valueAt(i); |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 677 | |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 678 | if (shouldFilterOut(entry)) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 679 | continue; |
| 680 | } |
| 681 | |
| 682 | mSortedAndFiltered.add(entry); |
Joe Onorato | 9c1d823 | 2010-05-24 20:02:53 -0400 | [diff] [blame] | 683 | } |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 684 | } |
| 685 | |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 686 | Collections.sort(mSortedAndFiltered, mRankingComparator); |
| 687 | } |
| 688 | |
Dan Sandler | 008cea7 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 689 | /** |
Dan Sandler | 008cea7 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 690 | * @return true if this notification should NOT be shown right now |
| 691 | */ |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 692 | public boolean shouldFilterOut(Entry entry) { |
| 693 | final StatusBarNotification sbn = entry.notification; |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 694 | if (!(mEnvironment.isDeviceProvisioned() || |
| 695 | showNotificationEvenIfUnprovisioned(sbn))) { |
| 696 | return true; |
| 697 | } |
| 698 | |
| 699 | if (!mEnvironment.isNotificationForCurrentProfiles(sbn)) { |
| 700 | return true; |
| 701 | } |
| 702 | |
Robin Lee | 92b83c6 | 2016-08-31 13:27:51 +0100 | [diff] [blame] | 703 | if (mEnvironment.isSecurelyLocked(sbn.getUserId()) && |
Julia Reynolds | 26fa8a5 | 2016-02-24 08:31:22 -0500 | [diff] [blame] | 704 | (sbn.getNotification().visibility == Notification.VISIBILITY_SECRET |
| 705 | || mEnvironment.shouldHideNotifications(sbn.getUserId()) |
| 706 | || mEnvironment.shouldHideNotifications(sbn.getKey()))) { |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 707 | return true; |
Joe Onorato | 9c1d823 | 2010-05-24 20:02:53 -0400 | [diff] [blame] | 708 | } |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 709 | |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 710 | if (mEnvironment.isDozing() && shouldSuppressAmbient(entry)) { |
Julia Reynolds | 24653c3 | 2018-03-02 13:16:37 -0500 | [diff] [blame] | 711 | return true; |
| 712 | } |
| 713 | |
Julia Reynolds | aa96cf3 | 2018-04-17 09:09:04 -0400 | [diff] [blame] | 714 | if (!mEnvironment.isDozing() && shouldSuppressNotificationList(entry)) { |
Julia Reynolds | 24653c3 | 2018-03-02 13:16:37 -0500 | [diff] [blame] | 715 | return true; |
| 716 | } |
| 717 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 718 | if (shouldHide(sbn.getKey())) { |
| 719 | return true; |
| 720 | } |
| 721 | |
Jason Monk | 2a6ea9c | 2017-01-26 11:14:51 -0500 | [diff] [blame] | 722 | if (!StatusBar.ENABLE_CHILD_NOTIFICATIONS |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 723 | && mGroupManager.isChildInGroupWithSummary(sbn)) { |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 724 | return true; |
| 725 | } |
Dan Sandler | 008cea7 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 726 | |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 727 | if (mFsc.isDungeonNotification(sbn) && !mFsc.isDungeonNeededForUser(sbn.getUserId())) { |
Dan Sandler | 008cea7 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 728 | // this is a foreground-service disclosure for a user that does not need to show one |
| 729 | return true; |
| 730 | } |
Julia Reynolds | 3c7de11 | 2018-03-28 09:33:13 -0400 | [diff] [blame] | 731 | if (mFsc.isSystemAlertNotification(sbn)) { |
| 732 | final String[] apps = sbn.getNotification().extras.getStringArray( |
| 733 | Notification.EXTRA_FOREGROUND_APPS); |
| 734 | if (apps != null && apps.length >= 1) { |
| 735 | if (!mFsc.isSystemAlertWarningNeeded(sbn.getUserId(), apps[0])) { |
| 736 | return true; |
| 737 | } |
| 738 | } |
Julia Reynolds | fc64001 | 2018-02-21 12:25:27 -0500 | [diff] [blame] | 739 | } |
Dan Sandler | 008cea7 | 2017-05-28 12:18:53 -0400 | [diff] [blame] | 740 | |
Joe Onorato | 9c1d823 | 2010-05-24 20:02:53 -0400 | [diff] [blame] | 741 | return false; |
Joe Onorato | 20da8f8 | 2010-05-24 16:39:29 -0400 | [diff] [blame] | 742 | } |
| 743 | |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 744 | // Q: What kinds of notifications should show during setup? |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 745 | // A: Almost none! Only things coming from packages with permission |
| 746 | // android.permission.NOTIFICATION_DURING_SETUP that also have special "kind" tags marking them |
| 747 | // as relevant for setup (see below). |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 748 | public static boolean showNotificationEvenIfUnprovisioned(StatusBarNotification sbn) { |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 749 | return showNotificationEvenIfUnprovisioned(AppGlobals.getPackageManager(), sbn); |
| 750 | } |
| 751 | |
| 752 | @VisibleForTesting |
| 753 | static boolean showNotificationEvenIfUnprovisioned(IPackageManager packageManager, |
| 754 | StatusBarNotification sbn) { |
| 755 | return checkUidPermission(packageManager, Manifest.permission.NOTIFICATION_DURING_SETUP, |
| 756 | sbn.getUid()) == PackageManager.PERMISSION_GRANTED |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 757 | && sbn.getNotification().extras.getBoolean(Notification.EXTRA_ALLOW_DURING_SETUP); |
| 758 | } |
| 759 | |
Maurice Lam | 132710e | 2017-03-03 19:13:42 -0800 | [diff] [blame] | 760 | private static int checkUidPermission(IPackageManager packageManager, String permission, |
| 761 | int uid) { |
| 762 | try { |
| 763 | return packageManager.checkUidPermission(permission, uid); |
| 764 | } catch (RemoteException e) { |
| 765 | throw e.rethrowFromSystemServer(); |
| 766 | } |
| 767 | } |
| 768 | |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 769 | public void dump(PrintWriter pw, String indent) { |
| 770 | int N = mSortedAndFiltered.size(); |
| 771 | pw.print(indent); |
| 772 | pw.println("active notifications: " + N); |
Christoph Studer | 11840cd | 2014-08-21 16:41:45 +0200 | [diff] [blame] | 773 | int active; |
| 774 | for (active = 0; active < N; active++) { |
| 775 | NotificationData.Entry e = mSortedAndFiltered.get(active); |
| 776 | dumpEntry(pw, indent, active, e); |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 777 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 778 | synchronized (mEntries) { |
| 779 | int M = mEntries.size(); |
| 780 | pw.print(indent); |
| 781 | pw.println("inactive notifications: " + (M - active)); |
| 782 | int inactiveCount = 0; |
| 783 | for (int i = 0; i < M; i++) { |
| 784 | Entry entry = mEntries.valueAt(i); |
| 785 | if (!mSortedAndFiltered.contains(entry)) { |
| 786 | dumpEntry(pw, indent, inactiveCount, entry); |
| 787 | inactiveCount++; |
| 788 | } |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | private void dumpEntry(PrintWriter pw, String indent, int i, Entry e) { |
Kensuke Matsui | efa1a74 | 2017-08-03 12:12:36 +0900 | [diff] [blame] | 794 | getRanking(e.key, mTmpRanking); |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 795 | pw.print(indent); |
| 796 | pw.println(" [" + i + "] key=" + e.key + " icon=" + e.icon); |
| 797 | StatusBarNotification n = e.notification; |
| 798 | pw.print(indent); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 799 | pw.println(" pkg=" + n.getPackageName() + " id=" + n.getId() + " importance=" + |
| 800 | mTmpRanking.getImportance()); |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 801 | pw.print(indent); |
| 802 | pw.println(" notification=" + n.getNotification()); |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 803 | } |
| 804 | |
Christoph Studer | 1492116 | 2014-09-03 12:51:26 +0200 | [diff] [blame] | 805 | private static boolean isSystemNotification(StatusBarNotification sbn) { |
| 806 | String sbnPackage = sbn.getPackageName(); |
| 807 | return "android".equals(sbnPackage) || "com.android.systemui".equals(sbnPackage); |
| 808 | } |
| 809 | |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 810 | /** |
| 811 | * Provides access to keyguard state and user settings dependent data. |
| 812 | */ |
| 813 | public interface Environment { |
Robin Lee | 92b83c6 | 2016-08-31 13:27:51 +0100 | [diff] [blame] | 814 | public boolean isSecurelyLocked(int userId); |
Julia Reynolds | 26fa8a5 | 2016-02-24 08:31:22 -0500 | [diff] [blame] | 815 | public boolean shouldHideNotifications(int userid); |
| 816 | public boolean shouldHideNotifications(String key); |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 817 | public boolean isDeviceProvisioned(); |
| 818 | public boolean isNotificationForCurrentProfiles(StatusBarNotification sbn); |
Christoph Studer | 2e731b5 | 2014-08-22 16:01:51 +0200 | [diff] [blame] | 819 | public String getCurrentMediaNotificationKey(); |
Selim Cinek | 25fd4e2b | 2015-02-20 17:46:07 +0100 | [diff] [blame] | 820 | public NotificationGroupManager getGroupManager(); |
Julia Reynolds | 24653c3 | 2018-03-02 13:16:37 -0500 | [diff] [blame] | 821 | /** |
| 822 | * @return true iff the device is dozing |
| 823 | */ |
| 824 | boolean isDozing(); |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 825 | } |
Joe Onorato | 503007d | 2010-04-16 09:20:55 -0700 | [diff] [blame] | 826 | } |