The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 17 | package com.android.server.notification; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 19 | import static android.app.Notification.FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 20 | import static android.app.NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 21 | import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED; |
| 22 | import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 23 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 24 | import static android.app.NotificationManager.IMPORTANCE_MIN; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 25 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 26 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECTS_UNSET; |
| 27 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT; |
| 28 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE; |
| 29 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 30 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS; |
| 31 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST; |
| 32 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK; |
| 33 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF; |
| 34 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON; |
| 35 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_STATUS_BAR; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 36 | import static android.content.pm.PackageManager.FEATURE_LEANBACK; |
| 37 | import static android.content.pm.PackageManager.FEATURE_TELEVISION; |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 38 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 39 | import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_CRITICAL; |
| 40 | import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_NORMAL; |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 41 | import static android.os.UserHandle.USER_NULL; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 42 | import static android.os.UserHandle.USER_SYSTEM; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 43 | import static android.service.notification.NotificationListenerService |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 44 | .HINT_HOST_DISABLE_CALL_EFFECTS; |
| 45 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_EFFECTS; |
| 46 | import static android.service.notification.NotificationListenerService |
| 47 | .HINT_HOST_DISABLE_NOTIFICATION_EFFECTS; |
| 48 | import static android.service.notification.NotificationListenerService |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 49 | .NOTIFICATION_CHANNEL_OR_GROUP_ADDED; |
| 50 | import static android.service.notification.NotificationListenerService |
| 51 | .NOTIFICATION_CHANNEL_OR_GROUP_DELETED; |
| 52 | import static android.service.notification.NotificationListenerService |
| 53 | .NOTIFICATION_CHANNEL_OR_GROUP_UPDATED; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 54 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL; |
| 55 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL; |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 56 | import static android.service.notification.NotificationListenerService.REASON_CANCEL; |
| 57 | import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 58 | import static android.service.notification.NotificationListenerService.REASON_CHANNEL_BANNED; |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 59 | import static android.service.notification.NotificationListenerService.REASON_CLICK; |
| 60 | import static android.service.notification.NotificationListenerService.REASON_ERROR; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 61 | import static android.service.notification.NotificationListenerService |
| 62 | .REASON_GROUP_SUMMARY_CANCELED; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 63 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL; |
| 64 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL_ALL; |
| 65 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_BANNED; |
| 66 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_CHANGED; |
| 67 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_SUSPENDED; |
| 68 | import static android.service.notification.NotificationListenerService.REASON_PROFILE_TURNED_OFF; |
| 69 | import static android.service.notification.NotificationListenerService.REASON_SNOOZED; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 70 | import static android.service.notification.NotificationListenerService.REASON_TIMEOUT; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 71 | import static android.service.notification.NotificationListenerService.REASON_UNAUTOBUNDLED; |
| 72 | import static android.service.notification.NotificationListenerService.REASON_USER_STOPPED; |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 73 | import static android.service.notification.NotificationListenerService.TRIM_FULL; |
| 74 | import static android.service.notification.NotificationListenerService.TRIM_LIGHT; |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 75 | import static android.view.Display.DEFAULT_DISPLAY; |
| 76 | import static android.view.WindowManager.LayoutParams.TYPE_TOAST; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 77 | |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 78 | import static com.android.server.utils.PriorityDump.PRIORITY_ARG; |
| 79 | import static com.android.server.utils.PriorityDump.PRIORITY_ARG_CRITICAL; |
| 80 | import static com.android.server.utils.PriorityDump.PRIORITY_ARG_NORMAL; |
| 81 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 82 | import android.Manifest; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 83 | import android.annotation.NonNull; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 84 | import android.annotation.Nullable; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 85 | import android.app.ActivityManager; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 86 | import android.app.ActivityManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 87 | import android.app.AlarmManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 88 | import android.app.AppGlobals; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 89 | import android.app.AppOpsManager; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 90 | import android.app.AutomaticZenRule; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | import android.app.IActivityManager; |
| 92 | import android.app.INotificationManager; |
| 93 | import android.app.ITransientNotification; |
| 94 | import android.app.Notification; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 95 | import android.app.NotificationChannel; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 96 | import android.app.NotificationChannelGroup; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 97 | import android.app.NotificationManager; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 98 | import android.app.NotificationManager.Policy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | import android.app.PendingIntent; |
| 100 | import android.app.StatusBarManager; |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 101 | import android.app.admin.DeviceAdminInfo; |
| 102 | import android.app.admin.DevicePolicyManagerInternal; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 103 | import android.app.backup.BackupManager; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 104 | import android.app.usage.UsageEvents; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 105 | import android.app.usage.UsageStatsManagerInternal; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 106 | import android.companion.ICompanionDeviceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | import android.content.BroadcastReceiver; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 108 | import android.content.ComponentName; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 109 | import android.content.ContentProvider; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 110 | import android.content.ContentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | import android.content.Context; |
| 112 | import android.content.Intent; |
| 113 | import android.content.IntentFilter; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 114 | import android.content.pm.ApplicationInfo; |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 115 | import android.content.pm.IPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | import android.content.pm.PackageManager; |
| 117 | import android.content.pm.PackageManager.NameNotFoundException; |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 118 | import android.content.pm.ParceledListSlice; |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 119 | import android.content.pm.UserInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | import android.content.res.Resources; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 121 | import android.database.ContentObserver; |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 122 | import android.media.AudioAttributes; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 123 | import android.media.AudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 124 | import android.media.AudioManagerInternal; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 125 | import android.media.IRingtonePlayer; |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 126 | import android.metrics.LogMaker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | import android.os.Binder; |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 129 | import android.os.Build; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 130 | import android.os.Bundle; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 131 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | import android.os.Handler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 133 | import android.os.HandlerThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | import android.os.IBinder; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 135 | import android.os.IDeviceIdleController; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 136 | import android.os.IInterface; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 137 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | import android.os.Message; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 139 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 140 | import android.os.RemoteException; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 141 | import android.os.ResultReceiver; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 142 | import android.os.ServiceManager; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 143 | import android.os.ShellCallback; |
| 144 | import android.os.ShellCommand; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 145 | import android.os.SystemClock; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 146 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 147 | import android.os.UserHandle; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 148 | import android.os.VibrationEffect; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 149 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | import android.provider.Settings; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 151 | import android.service.notification.Adjustment; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 152 | import android.service.notification.Condition; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 153 | import android.service.notification.IConditionProvider; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 154 | import android.service.notification.INotificationListener; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 155 | import android.service.notification.IStatusBarNotificationHolder; |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 156 | import android.service.notification.ListenersDisablingEffectsProto; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 157 | import android.service.notification.NotificationAssistantService; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 158 | import android.service.notification.NotificationListenerService; |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 159 | import android.service.notification.NotificationRankingUpdate; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 160 | import android.service.notification.NotificationRecordProto; |
| 161 | import android.service.notification.NotificationServiceDumpProto; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 162 | import android.service.notification.NotificationStats; |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 163 | import android.service.notification.NotifyingApp; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 164 | import android.service.notification.SnoozeCriterion; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 165 | import android.service.notification.StatusBarNotification; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 166 | import android.service.notification.ZenModeConfig; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 167 | import android.service.notification.ZenModeProto; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 168 | import android.telephony.PhoneStateListener; |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 169 | import android.telephony.TelephonyManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 170 | import android.text.TextUtils; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 171 | import android.util.ArrayMap; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 172 | import android.util.ArraySet; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 173 | import android.util.AtomicFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | import android.util.Log; |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 175 | import android.util.Slog; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 176 | import android.util.SparseArray; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 177 | import android.util.Xml; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 178 | import android.util.proto.ProtoOutputStream; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 179 | import android.view.accessibility.AccessibilityEvent; |
| 180 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | import android.widget.Toast; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 182 | |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 183 | import com.android.internal.R; |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 184 | import com.android.internal.annotations.GuardedBy; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 185 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 186 | import com.android.internal.logging.MetricsLogger; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 187 | import com.android.internal.logging.nano.MetricsProto; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 188 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Beverly | 70dcd00 | 2018-03-29 17:09:16 -0400 | [diff] [blame] | 189 | import com.android.internal.notification.SystemNotificationChannels; |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 190 | import com.android.internal.os.BackgroundThread; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 191 | import com.android.internal.statusbar.NotificationVisibility; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 192 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 193 | import com.android.internal.util.DumpUtils; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 194 | import com.android.internal.util.FastXmlSerializer; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 195 | import com.android.internal.util.Preconditions; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 196 | import com.android.internal.util.XmlUtils; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 197 | import com.android.server.DeviceIdleController; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 198 | import com.android.server.EventLogTags; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 199 | import com.android.server.LocalServices; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 200 | import com.android.server.SystemService; |
| 201 | import com.android.server.lights.Light; |
| 202 | import com.android.server.lights.LightsManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 203 | import com.android.server.notification.ManagedServices.ManagedServiceInfo; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 204 | import com.android.server.notification.ManagedServices.UserProfiles; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 205 | import com.android.server.policy.PhoneWindowManager; |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 206 | import com.android.server.statusbar.StatusBarManagerInternal; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 207 | import com.android.server.wm.WindowManagerInternal; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 208 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 209 | import libcore.io.IoUtils; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 210 | |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 211 | import org.json.JSONException; |
| 212 | import org.json.JSONObject; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 213 | import org.xmlpull.v1.XmlPullParser; |
| 214 | import org.xmlpull.v1.XmlPullParserException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 215 | import org.xmlpull.v1.XmlSerializer; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 216 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 217 | import java.io.ByteArrayInputStream; |
| 218 | import java.io.ByteArrayOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 219 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | import java.io.FileDescriptor; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 221 | import java.io.FileNotFoundException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 222 | import java.io.FileOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 223 | import java.io.IOException; |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 224 | import java.io.InputStream; |
| 225 | import java.io.OutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 227 | import java.nio.charset.StandardCharsets; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 228 | import java.util.ArrayDeque; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | import java.util.ArrayList; |
Michael Wright | bc4d0d9 | 2017-03-23 18:57:57 +0000 | [diff] [blame] | 230 | import java.util.Arrays; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 231 | import java.util.Iterator; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 232 | import java.util.List; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 233 | import java.util.Map.Entry; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 234 | import java.util.Objects; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 235 | import java.util.Set; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 236 | import java.util.concurrent.TimeUnit; |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 237 | import java.util.function.Predicate; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 238 | |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 239 | /** {@hide} */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 240 | public class NotificationManagerService extends SystemService { |
| 241 | static final String TAG = "NotificationService"; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 242 | static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Selim Cinek | 4041249 | 2015-12-08 18:03:22 -0800 | [diff] [blame] | 243 | public static final boolean ENABLE_CHILD_NOTIFICATIONS |
| 244 | = SystemProperties.getBoolean("debug.child_notifs", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 246 | static final boolean DEBUG_INTERRUPTIVENESS = SystemProperties.getBoolean( |
| 247 | "debug.notification.interruptiveness", false); |
| 248 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 249 | static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 250 | static final float DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE = 5f; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 251 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | // message codes |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 253 | static final int MESSAGE_DURATION_REACHED = 2; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 254 | static final int MESSAGE_SAVE_POLICY_FILE = 3; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 255 | static final int MESSAGE_SEND_RANKING_UPDATE = 4; |
| 256 | static final int MESSAGE_LISTENER_HINTS_CHANGED = 5; |
| 257 | static final int MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED = 6; |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 258 | static final int MESSAGE_FINISH_TOKEN_TIMEOUT = 7; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 259 | |
| 260 | // ranking thread messages |
| 261 | private static final int MESSAGE_RECONSIDER_RANKING = 1000; |
| 262 | private static final int MESSAGE_RANKING_SORT = 1001; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 264 | static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 265 | static final int SHORT_DELAY = 2000; // 2 seconds |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 266 | |
Robert Carr | 3406d46 | 2018-03-15 16:19:07 -0700 | [diff] [blame] | 267 | // 1 second past the ANR timeout. |
| 268 | static final int FINISH_TOKEN_TIMEOUT = 11 * 1000; |
| 269 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 270 | static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 271 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 272 | static final long SNOOZE_UNTIL_UNSPECIFIED = -1; |
| 273 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 274 | static final int VIBRATE_PATTERN_MAXLEN = 8 * 2 + 1; // up to eight bumps |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 276 | static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION; |
Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 277 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 278 | static final boolean ENABLE_BLOCKED_TOASTS = true; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 279 | |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 280 | // When #matchesCallFilter is called from the ringer, wait at most |
| 281 | // 3s to resolve the contacts. This timeout is required since |
| 282 | // ContactsProvider might take a long time to start up. |
| 283 | // |
| 284 | // Return STARRED_CONTACT when the timeout is hit in order to avoid |
| 285 | // missed calls in ZEN mode "Important". |
| 286 | static final int MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS = 3000; |
| 287 | static final float MATCHES_CALL_FILTER_TIMEOUT_AFFINITY = |
| 288 | ValidateNotificationPeople.STARRED_CONTACT; |
| 289 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 290 | /** notification_enqueue status value for a newly enqueued notification. */ |
| 291 | private static final int EVENTLOG_ENQUEUE_STATUS_NEW = 0; |
| 292 | |
| 293 | /** notification_enqueue status value for an existing notification. */ |
| 294 | private static final int EVENTLOG_ENQUEUE_STATUS_UPDATE = 1; |
| 295 | |
| 296 | /** notification_enqueue status value for an ignored notification. */ |
| 297 | private static final int EVENTLOG_ENQUEUE_STATUS_IGNORED = 2; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 298 | private static final long MIN_PACKAGE_OVERRATE_LOG_INTERVAL = 5000; // milliseconds |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 299 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 300 | private static final long DELAY_FOR_ASSISTANT_TIME = 100; |
| 301 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 302 | private static final String ACTION_NOTIFICATION_TIMEOUT = |
| 303 | NotificationManagerService.class.getSimpleName() + ".TIMEOUT"; |
| 304 | private static final int REQUEST_CODE_TIMEOUT = 1; |
| 305 | private static final String SCHEME_TIMEOUT = "timeout"; |
| 306 | private static final String EXTRA_KEY = "key"; |
| 307 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 308 | private IActivityManager mAm; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 309 | private ActivityManager mActivityManager; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 310 | private IPackageManager mPackageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 311 | private PackageManager mPackageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 312 | AudioManager mAudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 313 | AudioManagerInternal mAudioManagerInternal; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 314 | @Nullable StatusBarManagerInternal mStatusBar; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 315 | Vibrator mVibrator; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 316 | private WindowManagerInternal mWindowManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 317 | private AlarmManager mAlarmManager; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 318 | private ICompanionDeviceManager mCompanionManager; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 319 | private AccessibilityManager mAccessibilityManager; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 320 | private IDeviceIdleController mDeviceIdleController; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 321 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | final IBinder mForegroundToken = new Binder(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 323 | private WorkerHandler mHandler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 324 | private final HandlerThread mRankingThread = new HandlerThread("ranker", |
| 325 | Process.THREAD_PRIORITY_BACKGROUND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 327 | private Light mNotificationLight; |
| 328 | Light mAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 329 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 330 | private long[] mFallbackVibrationPattern; |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 331 | private boolean mUseAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 332 | boolean mSystemReady; |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 333 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 334 | private boolean mDisableNotificationEffects; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 335 | private int mCallState; |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 336 | private String mSoundNotificationKey; |
| 337 | private String mVibrateNotificationKey; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 339 | private final SparseArray<ArraySet<ManagedServiceInfo>> mListenersDisablingEffects = |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 340 | new SparseArray<>(); |
| 341 | private List<ComponentName> mEffectsSuppressors = new ArrayList<>(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 342 | private int mListenerHints; // right now, all hints are global |
John Spurlock | 8310410 | 2015-02-12 23:25:12 -0500 | [diff] [blame] | 343 | private int mInterruptionFilter = NotificationListenerService.INTERRUPTION_FILTER_UNKNOWN; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 344 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 345 | // for enabling and disabling notification pulse behavior |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 346 | private boolean mScreenOn = true; |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 347 | protected boolean mInCall = false; |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 348 | private boolean mNotificationPulseEnabled; |
| 349 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 350 | private Uri mInCallNotificationUri; |
| 351 | private AudioAttributes mInCallNotificationAudioAttributes; |
| 352 | private float mInCallNotificationVolume; |
luochaojiang | 50e5273c | 2018-04-16 16:55:03 +0800 | [diff] [blame] | 353 | private Binder mCallNotificationToken = null; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 354 | |
Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 355 | // used as a mutex for access to all active notifications & listeners |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 356 | final Object mNotificationLock = new Object(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 357 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 358 | final ArrayList<NotificationRecord> mNotificationList = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 359 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 360 | final ArrayMap<String, NotificationRecord> mNotificationsByKey = new ArrayMap<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 361 | @GuardedBy("mNotificationLock") |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 362 | final ArrayList<NotificationRecord> mEnqueuedNotifications = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 363 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 364 | final ArrayMap<Integer, ArrayMap<String, String>> mAutobundledSummaries = new ArrayMap<>(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 365 | final ArrayList<ToastRecord> mToastQueue = new ArrayList<>(); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 366 | final ArrayMap<String, NotificationRecord> mSummaryByGroupKey = new ArrayMap<>(); |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 367 | final ArrayMap<Integer, ArrayList<NotifyingApp>> mRecentApps = new ArrayMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 369 | // The last key in this list owns the hardware. |
| 370 | ArrayList<String> mLights = new ArrayList<>(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 371 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 372 | private AppOpsManager mAppOps; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 373 | private UsageStatsManagerInternal mAppUsageStats; |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 374 | private DevicePolicyManagerInternal mDpm; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 375 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 376 | private Archive mArchive; |
| 377 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 378 | // Persistent storage for notification policy |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 379 | private AtomicFile mPolicyFile; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 380 | |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 381 | private static final int DB_VERSION = 1; |
| 382 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 383 | private static final String TAG_NOTIFICATION_POLICY = "notification-policy"; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 384 | private static final String ATTR_VERSION = "version"; |
| 385 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 386 | private RankingHelper mRankingHelper; |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 387 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 388 | private final UserProfiles mUserProfiles = new UserProfiles(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 389 | private NotificationListeners mListeners; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 390 | private NotificationAssistants mAssistants; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 391 | private ConditionProviders mConditionProviders; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 392 | private NotificationUsageStats mUsageStats; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 393 | |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 394 | private static final int MY_UID = Process.myUid(); |
| 395 | private static final int MY_PID = Process.myPid(); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 396 | private static final IBinder WHITELIST_TOKEN = new Binder(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 397 | private RankingHandler mRankingHandler; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 398 | private long mLastOverRateLogTime; |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 399 | private float mMaxPackageEnqueueRate = DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE; |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 400 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 401 | private SnoozeHelper mSnoozeHelper; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 402 | private GroupHelper mGroupHelper; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 403 | private boolean mIsTelevision; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 404 | |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 405 | private MetricsLogger mMetricsLogger; |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 406 | private Predicate<String> mAllowedManagedServicePackages; |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 407 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 408 | private static class Archive { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 409 | final int mBufferSize; |
| 410 | final ArrayDeque<StatusBarNotification> mBuffer; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 411 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 412 | public Archive(int size) { |
| 413 | mBufferSize = size; |
| 414 | mBuffer = new ArrayDeque<StatusBarNotification>(mBufferSize); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 415 | } |
Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 416 | |
Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 417 | public String toString() { |
| 418 | final StringBuilder sb = new StringBuilder(); |
| 419 | final int N = mBuffer.size(); |
| 420 | sb.append("Archive ("); |
| 421 | sb.append(N); |
| 422 | sb.append(" notification"); |
| 423 | sb.append((N==1)?")":"s)"); |
| 424 | return sb.toString(); |
| 425 | } |
| 426 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 427 | public void record(StatusBarNotification nr) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 428 | if (mBuffer.size() == mBufferSize) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 429 | mBuffer.removeFirst(); |
| 430 | } |
Daniel Sandler | 26b81d5 | 2013-05-20 20:56:43 -0400 | [diff] [blame] | 431 | |
| 432 | // We don't want to store the heavy bits of the notification in the archive, |
| 433 | // but other clients in the system process might be using the object, so we |
| 434 | // store a (lightened) copy. |
| 435 | mBuffer.addLast(nr.cloneLight()); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 436 | } |
| 437 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 438 | public Iterator<StatusBarNotification> descendingIterator() { |
| 439 | return mBuffer.descendingIterator(); |
| 440 | } |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 441 | |
| 442 | public StatusBarNotification[] getArray(int count) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 443 | if (count == 0) count = mBufferSize; |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 444 | final StatusBarNotification[] a |
| 445 | = new StatusBarNotification[Math.min(count, mBuffer.size())]; |
| 446 | Iterator<StatusBarNotification> iter = descendingIterator(); |
| 447 | int i=0; |
| 448 | while (iter.hasNext() && i < count) { |
| 449 | a[i++] = iter.next(); |
| 450 | } |
| 451 | return a; |
| 452 | } |
| 453 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 454 | } |
| 455 | |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 456 | protected void readDefaultApprovedServices(int userId) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 457 | String defaultListenerAccess = getContext().getResources().getString( |
| 458 | com.android.internal.R.string.config_defaultListenerAccessPackages); |
| 459 | if (defaultListenerAccess != null) { |
| 460 | for (String whitelisted : |
| 461 | defaultListenerAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR)) { |
| 462 | // Gather all notification listener components for candidate pkgs. |
| 463 | Set<ComponentName> approvedListeners = |
| 464 | mListeners.queryPackageForServices(whitelisted, |
| 465 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 466 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId); |
| 467 | for (ComponentName cn : approvedListeners) { |
| 468 | try { |
| 469 | getBinderService().setNotificationListenerAccessGrantedForUser(cn, |
| 470 | userId, true); |
| 471 | } catch (RemoteException e) { |
| 472 | e.printStackTrace(); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 477 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 478 | String defaultDndAccess = getContext().getResources().getString( |
| 479 | com.android.internal.R.string.config_defaultDndAccessPackages); |
| 480 | if (defaultListenerAccess != null) { |
| 481 | for (String whitelisted : |
| 482 | defaultDndAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR)) { |
| 483 | try { |
| 484 | getBinderService().setNotificationPolicyAccessGranted(whitelisted, true); |
| 485 | } catch (RemoteException e) { |
| 486 | e.printStackTrace(); |
| 487 | } |
| 488 | } |
| 489 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 490 | |
| 491 | readDefaultAssistant(userId); |
| 492 | } |
| 493 | |
| 494 | protected void readDefaultAssistant(int userId) { |
| 495 | String defaultAssistantAccess = getContext().getResources().getString( |
| 496 | com.android.internal.R.string.config_defaultAssistantAccessPackage); |
| 497 | if (defaultAssistantAccess != null) { |
| 498 | // Gather all notification assistant components for candidate pkg. There should |
| 499 | // only be one |
| 500 | Set<ComponentName> approvedAssistants = |
| 501 | mAssistants.queryPackageForServices(defaultAssistantAccess, |
| 502 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 503 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId); |
| 504 | for (ComponentName cn : approvedAssistants) { |
| 505 | try { |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 506 | getBinderService().setNotificationAssistantAccessGrantedForUser( |
| 507 | cn, userId, true); |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 508 | } catch (RemoteException e) { |
| 509 | e.printStackTrace(); |
| 510 | } |
| 511 | } |
| 512 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 513 | } |
| 514 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 515 | void readPolicyXml(InputStream stream, boolean forRestore) |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 516 | throws XmlPullParserException, NumberFormatException, IOException { |
| 517 | final XmlPullParser parser = Xml.newPullParser(); |
| 518 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 519 | XmlUtils.beginDocument(parser, TAG_NOTIFICATION_POLICY); |
| 520 | boolean migratedManagedServices = false; |
| 521 | int outerDepth = parser.getDepth(); |
| 522 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 523 | if (ZenModeConfig.ZEN_TAG.equals(parser.getName())) { |
| 524 | mZenModeHelper.readXml(parser, forRestore); |
| 525 | } else if (RankingHelper.TAG_RANKING.equals(parser.getName())){ |
| 526 | mRankingHelper.readXml(parser, forRestore); |
| 527 | } |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 528 | if (mListeners.getConfig().xmlTag.equals(parser.getName())) { |
| 529 | mListeners.readXml(parser, mAllowedManagedServicePackages); |
| 530 | migratedManagedServices = true; |
| 531 | } else if (mAssistants.getConfig().xmlTag.equals(parser.getName())) { |
| 532 | mAssistants.readXml(parser, mAllowedManagedServicePackages); |
| 533 | migratedManagedServices = true; |
| 534 | } else if (mConditionProviders.getConfig().xmlTag.equals(parser.getName())) { |
| 535 | mConditionProviders.readXml(parser, mAllowedManagedServicePackages); |
| 536 | migratedManagedServices = true; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 537 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 538 | } |
| 539 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 540 | if (!migratedManagedServices) { |
| 541 | mListeners.migrateToXml(); |
| 542 | mAssistants.migrateToXml(); |
| 543 | mConditionProviders.migrateToXml(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 544 | savePolicyFile(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 545 | } |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 546 | |
| 547 | mAssistants.ensureAssistant(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 548 | } |
| 549 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 550 | private void loadPolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 551 | if (DBG) Slog.d(TAG, "loadPolicyFile"); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 552 | synchronized (mPolicyFile) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 553 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 554 | InputStream infile = null; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 555 | try { |
| 556 | infile = mPolicyFile.openRead(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 557 | readPolicyXml(infile, false /*forRestore*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 558 | } catch (FileNotFoundException e) { |
| 559 | // No data yet |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 560 | // Load default managed services approvals |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 561 | readDefaultApprovedServices(USER_SYSTEM); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 562 | } catch (IOException e) { |
| 563 | Log.wtf(TAG, "Unable to read notification policy", e); |
| 564 | } catch (NumberFormatException e) { |
| 565 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 566 | } catch (XmlPullParserException e) { |
| 567 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 568 | } finally { |
| 569 | IoUtils.closeQuietly(infile); |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | public void savePolicyFile() { |
| 575 | mHandler.removeMessages(MESSAGE_SAVE_POLICY_FILE); |
| 576 | mHandler.sendEmptyMessage(MESSAGE_SAVE_POLICY_FILE); |
| 577 | } |
| 578 | |
| 579 | private void handleSavePolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 580 | if (DBG) Slog.d(TAG, "handleSavePolicyFile"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 581 | synchronized (mPolicyFile) { |
| 582 | final FileOutputStream stream; |
| 583 | try { |
| 584 | stream = mPolicyFile.startWrite(); |
| 585 | } catch (IOException e) { |
| 586 | Slog.w(TAG, "Failed to save policy file", e); |
| 587 | return; |
| 588 | } |
| 589 | |
| 590 | try { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 591 | writePolicyXml(stream, false /*forBackup*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 592 | mPolicyFile.finishWrite(stream); |
| 593 | } catch (IOException e) { |
| 594 | Slog.w(TAG, "Failed to save policy file, restoring backup", e); |
| 595 | mPolicyFile.failWrite(stream); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 596 | } |
| 597 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 598 | BackupManager.dataChanged(getContext().getPackageName()); |
| 599 | } |
| 600 | |
| 601 | private void writePolicyXml(OutputStream stream, boolean forBackup) throws IOException { |
| 602 | final XmlSerializer out = new FastXmlSerializer(); |
| 603 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
| 604 | out.startDocument(null, true); |
| 605 | out.startTag(null, TAG_NOTIFICATION_POLICY); |
| 606 | out.attribute(null, ATTR_VERSION, Integer.toString(DB_VERSION)); |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 607 | mZenModeHelper.writeXml(out, forBackup, null); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 608 | mRankingHelper.writeXml(out, forBackup); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 609 | mListeners.writeXml(out, forBackup); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 610 | mAssistants.writeXml(out, forBackup); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 611 | mConditionProviders.writeXml(out, forBackup); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 612 | out.endTag(null, TAG_NOTIFICATION_POLICY); |
| 613 | out.endDocument(); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 614 | } |
| 615 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | private static final class ToastRecord |
| 617 | { |
| 618 | final int pid; |
| 619 | final String pkg; |
Beverly Tai | 98efc79 | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 620 | final ITransientNotification callback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | int duration; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 622 | Binder token; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 624 | ToastRecord(int pid, String pkg, ITransientNotification callback, int duration, |
| 625 | Binder token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | this.pid = pid; |
| 627 | this.pkg = pkg; |
| 628 | this.callback = callback; |
| 629 | this.duration = duration; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 630 | this.token = token; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | void update(int duration) { |
| 634 | this.duration = duration; |
| 635 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 636 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 637 | void dump(PrintWriter pw, String prefix, DumpFilter filter) { |
| 638 | if (filter != null && !filter.matches(pkg)) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | pw.println(prefix + this); |
| 640 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 641 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 642 | @Override |
| 643 | public final String toString() |
| 644 | { |
| 645 | return "ToastRecord{" |
| 646 | + Integer.toHexString(System.identityHashCode(this)) |
| 647 | + " pkg=" + pkg |
| 648 | + " callback=" + callback |
| 649 | + " duration=" + duration; |
| 650 | } |
| 651 | } |
| 652 | |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 653 | @VisibleForTesting |
| 654 | final NotificationDelegate mNotificationDelegate = new NotificationDelegate() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 655 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 656 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | public void onSetDisabled(int status) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 658 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 659 | mDisableNotificationEffects = |
| 660 | (status & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 661 | if (disableNotificationEffects(null) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 662 | // cancel whatever's going on |
| 663 | long identity = Binder.clearCallingIdentity(); |
| 664 | try { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 665 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 666 | if (player != null) { |
| 667 | player.stopAsync(); |
| 668 | } |
| 669 | } catch (RemoteException e) { |
| 670 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 671 | Binder.restoreCallingIdentity(identity); |
| 672 | } |
| 673 | |
| 674 | identity = Binder.clearCallingIdentity(); |
| 675 | try { |
| 676 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 677 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | Binder.restoreCallingIdentity(identity); |
| 679 | } |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 684 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 685 | public void onClearAll(int callingUid, int callingPid, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 686 | synchronized (mNotificationLock) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 687 | cancelAllLocked(callingUid, callingPid, userId, REASON_CANCEL_ALL, null, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 688 | /*includeCurrentProfiles*/ true); |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 689 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | } |
| 691 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 692 | @Override |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 693 | public void onNotificationClick(int callingUid, int callingPid, String key, NotificationVisibility nv) { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 694 | exitIdle(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 695 | synchronized (mNotificationLock) { |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 696 | NotificationRecord r = mNotificationsByKey.get(key); |
| 697 | if (r == null) { |
| 698 | Log.w(TAG, "No notification with key: " + key); |
| 699 | return; |
| 700 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 701 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 702 | MetricsLogger.action(r.getLogMaker(now) |
| 703 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 704 | .setType(MetricsEvent.TYPE_ACTION) |
| 705 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, nv.rank) |
| 706 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, nv.count)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 707 | EventLogTags.writeNotificationClicked(key, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 708 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now), |
| 709 | nv.rank, nv.count); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 710 | |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 711 | StatusBarNotification sbn = r.sbn; |
| 712 | cancelNotification(callingUid, callingPid, sbn.getPackageName(), sbn.getTag(), |
| 713 | sbn.getId(), Notification.FLAG_AUTO_CANCEL, |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 714 | FLAG_FOREGROUND_SERVICE, false, r.getUserId(), |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 715 | REASON_CLICK, nv.rank, nv.count, null); |
| 716 | nv.recycle(); |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 717 | reportUserInteraction(r); |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 718 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | } |
| 720 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 721 | @Override |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 722 | public void onNotificationActionClick(int callingUid, int callingPid, String key, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 723 | int actionIndex, NotificationVisibility nv) { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 724 | exitIdle(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 725 | synchronized (mNotificationLock) { |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 726 | NotificationRecord r = mNotificationsByKey.get(key); |
| 727 | if (r == null) { |
| 728 | Log.w(TAG, "No notification with key: " + key); |
| 729 | return; |
| 730 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 731 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 732 | MetricsLogger.action(r.getLogMaker(now) |
| 733 | .setCategory(MetricsEvent.NOTIFICATION_ITEM_ACTION) |
| 734 | .setType(MetricsEvent.TYPE_ACTION) |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 735 | .setSubtype(actionIndex) |
| 736 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, nv.rank) |
| 737 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, nv.count)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 738 | EventLogTags.writeNotificationActionClicked(key, actionIndex, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 739 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now), |
| 740 | nv.rank, nv.count); |
| 741 | nv.recycle(); |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 742 | reportUserInteraction(r); |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
| 746 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 747 | public void onNotificationClear(int callingUid, int callingPid, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 748 | String pkg, String tag, int id, int userId, String key, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 749 | @NotificationStats.DismissalSurface int dismissalSurface, |
| 750 | NotificationVisibility nv) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 751 | synchronized (mNotificationLock) { |
| 752 | NotificationRecord r = mNotificationsByKey.get(key); |
| 753 | if (r != null) { |
| 754 | r.recordDismissalSurface(dismissalSurface); |
| 755 | } |
| 756 | } |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 757 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 758 | Notification.FLAG_ONGOING_EVENT | FLAG_FOREGROUND_SERVICE, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 759 | true, userId, REASON_CANCEL, nv.rank, nv.count,null); |
| 760 | nv.recycle(); |
Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 761 | } |
| 762 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 763 | @Override |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 764 | public void onPanelRevealed(boolean clearEffects, int items) { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 765 | MetricsLogger.visible(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Chris Wren | 621933f | 2017-06-14 15:59:03 -0400 | [diff] [blame] | 766 | MetricsLogger.histogram(getContext(), "note_load", items); |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 767 | EventLogTags.writeNotificationPanelRevealed(items); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 768 | if (clearEffects) { |
| 769 | clearEffects(); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | @Override |
| 774 | public void onPanelHidden() { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 775 | MetricsLogger.hidden(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 776 | EventLogTags.writeNotificationPanelHidden(); |
| 777 | } |
| 778 | |
| 779 | @Override |
| 780 | public void clearEffects() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 781 | synchronized (mNotificationLock) { |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 782 | if (DBG) Slog.d(TAG, "clearEffects"); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 783 | clearSoundLocked(); |
| 784 | clearVibrateLocked(); |
| 785 | clearLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 786 | } |
| 787 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 788 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 789 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 790 | public void onNotificationError(int callingUid, int callingPid, String pkg, String tag, int id, |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 791 | int uid, int initialPid, String message, int userId) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 792 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, 0, false, userId, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 793 | REASON_ERROR, null); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 794 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 795 | |
| 796 | @Override |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 797 | public void onNotificationVisibilityChanged(NotificationVisibility[] newlyVisibleKeys, |
| 798 | NotificationVisibility[] noLongerVisibleKeys) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 799 | synchronized (mNotificationLock) { |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 800 | for (NotificationVisibility nv : newlyVisibleKeys) { |
| 801 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 802 | if (r == null) continue; |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 803 | if (!r.isSeen()) { |
| 804 | // Report to usage stats that notification was made visible |
| 805 | if (DBG) Slog.d(TAG, "Marking notification as visible " + nv.key); |
| 806 | reportSeen(r); |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 807 | |
| 808 | // If the newly visible notification has smart replies |
| 809 | // then log that the user has seen them. |
| 810 | if (r.getNumSmartRepliesAdded() > 0 |
| 811 | && !r.hasSeenSmartReplies()) { |
| 812 | r.setSeenSmartReplies(true); |
| 813 | LogMaker logMaker = r.getLogMaker() |
| 814 | .setCategory(MetricsEvent.SMART_REPLY_VISIBLE) |
| 815 | .addTaggedData(MetricsEvent.NOTIFICATION_SMART_REPLY_COUNT, |
| 816 | r.getNumSmartRepliesAdded()); |
| 817 | mMetricsLogger.write(logMaker); |
| 818 | } |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 819 | } |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 820 | r.setVisibility(true, nv.rank, nv.count); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 821 | maybeRecordInterruptionLocked(r); |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 822 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 823 | } |
| 824 | // Note that we might receive this event after notifications |
| 825 | // have already left the system, e.g. after dismissing from the |
| 826 | // shade. Hence not finding notifications in |
| 827 | // mNotificationsByKey is not an exceptional condition. |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 828 | for (NotificationVisibility nv : noLongerVisibleKeys) { |
| 829 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 830 | if (r == null) continue; |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 831 | r.setVisibility(false, nv.rank, nv.count); |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 832 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 833 | } |
| 834 | } |
Christoph Studer | 92b389d | 2014-04-01 18:44:40 +0200 | [diff] [blame] | 835 | } |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 836 | |
| 837 | @Override |
| 838 | public void onNotificationExpansionChanged(String key, |
| 839 | boolean userAction, boolean expanded) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 840 | synchronized (mNotificationLock) { |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 841 | NotificationRecord r = mNotificationsByKey.get(key); |
| 842 | if (r != null) { |
| 843 | r.stats.onExpansionChanged(userAction, expanded); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 844 | final long now = System.currentTimeMillis(); |
Chris Wren | f734271 | 2017-09-14 10:55:55 -0400 | [diff] [blame] | 845 | if (userAction) { |
Chris Wren | 377ac6d | 2017-09-12 14:15:23 -0400 | [diff] [blame] | 846 | MetricsLogger.action(r.getLogMaker(now) |
| 847 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
Chris Wren | f734271 | 2017-09-14 10:55:55 -0400 | [diff] [blame] | 848 | .setType(expanded ? MetricsEvent.TYPE_DETAIL |
| 849 | : MetricsEvent.TYPE_COLLAPSE)); |
Chris Wren | 377ac6d | 2017-09-12 14:15:23 -0400 | [diff] [blame] | 850 | } |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 851 | if (expanded && userAction) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 852 | r.recordExpanded(); |
| 853 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 854 | EventLogTags.writeNotificationExpansion(key, |
| 855 | userAction ? 1 : 0, expanded ? 1 : 0, |
| 856 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 860 | |
| 861 | @Override |
| 862 | public void onNotificationDirectReplied(String key) { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 863 | exitIdle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 864 | synchronized (mNotificationLock) { |
| 865 | NotificationRecord r = mNotificationsByKey.get(key); |
| 866 | if (r != null) { |
| 867 | r.recordDirectReplied(); |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 868 | reportUserInteraction(r); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | @Override |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 874 | public void onNotificationSmartRepliesAdded(String key, int replyCount) { |
| 875 | synchronized (mNotificationLock) { |
| 876 | NotificationRecord r = mNotificationsByKey.get(key); |
| 877 | if (r != null) { |
| 878 | r.setNumSmartRepliesAdded(replyCount); |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | @Override |
| 884 | public void onNotificationSmartReplySent(String key, int replyIndex) { |
| 885 | synchronized (mNotificationLock) { |
| 886 | NotificationRecord r = mNotificationsByKey.get(key); |
| 887 | if (r != null) { |
| 888 | LogMaker logMaker = r.getLogMaker() |
| 889 | .setCategory(MetricsEvent.SMART_REPLY_ACTION) |
| 890 | .setSubtype(replyIndex); |
| 891 | mMetricsLogger.write(logMaker); |
| 892 | // Treat clicking on a smart reply as a user interaction. |
| 893 | reportUserInteraction(r); |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | @Override |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 899 | public void onNotificationSettingsViewed(String key) { |
| 900 | synchronized (mNotificationLock) { |
| 901 | NotificationRecord r = mNotificationsByKey.get(key); |
| 902 | if (r != null) { |
| 903 | r.recordViewedSettings(); |
| 904 | } |
| 905 | } |
| 906 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | }; |
| 908 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 909 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 910 | private void clearSoundLocked() { |
| 911 | mSoundNotificationKey = null; |
| 912 | long identity = Binder.clearCallingIdentity(); |
| 913 | try { |
| 914 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 915 | if (player != null) { |
| 916 | player.stopAsync(); |
| 917 | } |
| 918 | } catch (RemoteException e) { |
| 919 | } finally { |
| 920 | Binder.restoreCallingIdentity(identity); |
| 921 | } |
| 922 | } |
| 923 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 924 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 925 | private void clearVibrateLocked() { |
| 926 | mVibrateNotificationKey = null; |
| 927 | long identity = Binder.clearCallingIdentity(); |
| 928 | try { |
| 929 | mVibrator.cancel(); |
| 930 | } finally { |
| 931 | Binder.restoreCallingIdentity(identity); |
| 932 | } |
| 933 | } |
| 934 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 935 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 936 | private void clearLightsLocked() { |
| 937 | // light |
| 938 | mLights.clear(); |
| 939 | updateLightsLocked(); |
| 940 | } |
| 941 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 942 | protected final BroadcastReceiver mLocaleChangeReceiver = new BroadcastReceiver() { |
| 943 | @Override |
| 944 | public void onReceive(Context context, Intent intent) { |
| 945 | if (Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) { |
Beverly | 70dcd00 | 2018-03-29 17:09:16 -0400 | [diff] [blame] | 946 | // update system notification channels |
| 947 | SystemNotificationChannels.createAll(context); |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 948 | mZenModeHelper.updateDefaultZenRules(); |
Julia Reynolds | 816797a | 2017-08-11 15:47:09 -0400 | [diff] [blame] | 949 | mRankingHelper.onLocaleChanged(context, ActivityManager.getCurrentUser()); |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | }; |
| 953 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 954 | private final BroadcastReceiver mRestoreReceiver = new BroadcastReceiver() { |
| 955 | @Override |
| 956 | public void onReceive(Context context, Intent intent) { |
| 957 | if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) { |
| 958 | try { |
| 959 | String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME); |
| 960 | String newValue = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE); |
Michal Karpinski | 6135a26 | 2017-08-11 10:45:58 +0100 | [diff] [blame] | 961 | int restoredFromSdkInt = intent.getIntExtra( |
| 962 | Intent.EXTRA_SETTING_RESTORED_FROM_SDK_INT, 0); |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 963 | mListeners.onSettingRestored( |
| 964 | element, newValue, restoredFromSdkInt, getSendingUserId()); |
| 965 | mConditionProviders.onSettingRestored( |
| 966 | element, newValue, restoredFromSdkInt, getSendingUserId()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 967 | } catch (Exception e) { |
| 968 | Slog.wtf(TAG, "Cannot restore managed services from settings", e); |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | }; |
| 973 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 974 | private final BroadcastReceiver mNotificationTimeoutReceiver = new BroadcastReceiver() { |
| 975 | @Override |
| 976 | public void onReceive(Context context, Intent intent) { |
| 977 | String action = intent.getAction(); |
| 978 | if (action == null) { |
| 979 | return; |
| 980 | } |
| 981 | if (ACTION_NOTIFICATION_TIMEOUT.equals(action)) { |
| 982 | final NotificationRecord record; |
| 983 | synchronized (mNotificationLock) { |
| 984 | record = findNotificationByKeyLocked(intent.getStringExtra(EXTRA_KEY)); |
| 985 | } |
| 986 | if (record != null) { |
| 987 | cancelNotification(record.sbn.getUid(), record.sbn.getInitialPid(), |
| 988 | record.sbn.getPackageName(), record.sbn.getTag(), |
| 989 | record.sbn.getId(), 0, |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 990 | FLAG_FOREGROUND_SERVICE, true, record.getUserId(), |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 991 | REASON_TIMEOUT, null); |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | }; |
| 996 | |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 997 | private final BroadcastReceiver mPackageIntentReceiver = new BroadcastReceiver() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 998 | @Override |
| 999 | public void onReceive(Context context, Intent intent) { |
| 1000 | String action = intent.getAction(); |
Dianne Hackborn | 29cd7f1 | 2015-01-08 10:37:05 -0800 | [diff] [blame] | 1001 | if (action == null) { |
| 1002 | return; |
| 1003 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1005 | boolean queryRestart = false; |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 1006 | boolean queryRemove = false; |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1007 | boolean packageChanged = false; |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 1008 | boolean cancelNotifications = true; |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1009 | boolean hideNotifications = false; |
| 1010 | boolean unhideNotifications = false; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1011 | int reason = REASON_PACKAGE_CHANGED; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1012 | |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1013 | if (action.equals(Intent.ACTION_PACKAGE_ADDED) |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 1014 | || (queryRemove=action.equals(Intent.ACTION_PACKAGE_REMOVED)) |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1015 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1016 | || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED)) |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1017 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1018 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE) |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1019 | || action.equals(Intent.ACTION_PACKAGES_SUSPENDED) |
| 1020 | || action.equals(Intent.ACTION_PACKAGES_UNSUSPENDED)) { |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1021 | int changeUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 1022 | UserHandle.USER_ALL); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1023 | String pkgList[] = null; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1024 | int uidList[] = null; |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 1025 | boolean removingPackage = queryRemove && |
| 1026 | !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 1027 | if (DBG) Slog.i(TAG, "action=" + action + " removing=" + removingPackage); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1028 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1029 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1030 | uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1031 | } else if (action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
| 1032 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1033 | cancelNotifications = false; |
| 1034 | hideNotifications = true; |
| 1035 | } else if (action.equals(Intent.ACTION_PACKAGES_UNSUSPENDED)) { |
| 1036 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 1037 | cancelNotifications = false; |
| 1038 | unhideNotifications = true; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1039 | } else if (queryRestart) { |
| 1040 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1041 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1042 | } else { |
| 1043 | Uri uri = intent.getData(); |
| 1044 | if (uri == null) { |
| 1045 | return; |
| 1046 | } |
| 1047 | String pkgName = uri.getSchemeSpecificPart(); |
| 1048 | if (pkgName == null) { |
| 1049 | return; |
| 1050 | } |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1051 | if (packageChanged) { |
| 1052 | // We cancel notifications for packages which have just been disabled |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 1053 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1054 | final int enabled = mPackageManager.getApplicationEnabledSetting( |
| 1055 | pkgName, |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1056 | changeUserId != UserHandle.USER_ALL ? changeUserId : |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1057 | USER_SYSTEM); |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 1058 | if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 1059 | || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 1060 | cancelNotifications = false; |
| 1061 | } |
| 1062 | } catch (IllegalArgumentException e) { |
| 1063 | // Package doesn't exist; probably racing with uninstall. |
| 1064 | // cancelNotifications is already true, so nothing to do here. |
| 1065 | if (DBG) { |
| 1066 | Slog.i(TAG, "Exception trying to look up app enabled setting", e); |
| 1067 | } |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1068 | } catch (RemoteException e) { |
| 1069 | // Failed to talk to PackageManagerService Should never happen! |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1070 | } |
| 1071 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1072 | pkgList = new String[]{pkgName}; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1073 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1074 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1075 | if (pkgList != null && (pkgList.length > 0)) { |
| 1076 | for (String pkgName : pkgList) { |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 1077 | if (cancelNotifications) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1078 | cancelAllNotificationsInt(MY_UID, MY_PID, pkgName, null, 0, 0, |
| 1079 | !queryRestart, changeUserId, reason, null); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1080 | } else if (hideNotifications) { |
| 1081 | hideNotificationsForPackages(pkgList); |
| 1082 | } else if (unhideNotifications) { |
| 1083 | unhideNotificationsForPackages(pkgList); |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 1084 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1085 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1086 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1087 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1088 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1089 | mListeners.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1090 | mAssistants.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1091 | mConditionProviders.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1092 | mRankingHelper.onPackagesChanged(removingPackage, changeUserId, pkgList, uidList); |
| 1093 | savePolicyFile(); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | }; |
| 1097 | |
| 1098 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 1099 | @Override |
| 1100 | public void onReceive(Context context, Intent intent) { |
| 1101 | String action = intent.getAction(); |
| 1102 | |
| 1103 | if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1104 | // Keep track of screen on/off state, but do not turn off the notification light |
| 1105 | // until user passes through the lock screen or views the notification. |
| 1106 | mScreenOn = true; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 1107 | updateNotificationPulse(); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1108 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 1109 | mScreenOn = false; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 1110 | updateNotificationPulse(); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1111 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
John Spurlock | 5d2eeb1 | 2014-01-16 10:46:36 -0500 | [diff] [blame] | 1112 | mInCall = TelephonyManager.EXTRA_STATE_OFFHOOK |
| 1113 | .equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE)); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1114 | updateNotificationPulse(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1115 | } else if (action.equals(Intent.ACTION_USER_STOPPED)) { |
| 1116 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 1117 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1118 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1119 | REASON_USER_STOPPED, null); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1120 | } |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1121 | } else if (action.equals(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) { |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 1122 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1123 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1124 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1125 | REASON_PROFILE_TURNED_OFF, null); |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 1126 | } |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1127 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { |
| 1128 | // turn off LED when user passes through lock screen |
| 1129 | mNotificationLight.turnOff(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1130 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1131 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1132 | // reload per-user settings |
| 1133 | mSettingsObserver.update(null); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1134 | mUserProfiles.updateCache(context); |
Christoph Studer | b53dfd4 | 2014-09-12 14:45:59 +0200 | [diff] [blame] | 1135 | // Refresh managed services |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 1136 | mConditionProviders.onUserSwitched(user); |
| 1137 | mListeners.onUserSwitched(user); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1138 | mAssistants.onUserSwitched(user); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1139 | mZenModeHelper.onUserSwitched(user); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1140 | } else if (action.equals(Intent.ACTION_USER_ADDED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1141 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
| 1142 | if (userId != USER_NULL) { |
| 1143 | mUserProfiles.updateCache(context); |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 1144 | if (!mUserProfiles.isManagedProfile(userId)) { |
| 1145 | readDefaultApprovedServices(userId); |
| 1146 | } |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1147 | } |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1148 | } else if (action.equals(Intent.ACTION_USER_REMOVED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1149 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1150 | mUserProfiles.updateCache(context); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1151 | mZenModeHelper.onUserRemoved(user); |
Julia Reynolds | 2e9bf5f | 2017-05-03 13:23:30 -0400 | [diff] [blame] | 1152 | mRankingHelper.onUserRemoved(user); |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 1153 | mListeners.onUserRemoved(user); |
| 1154 | mConditionProviders.onUserRemoved(user); |
| 1155 | mAssistants.onUserRemoved(user); |
Julia Reynolds | 2e9bf5f | 2017-05-03 13:23:30 -0400 | [diff] [blame] | 1156 | savePolicyFile(); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1157 | } else if (action.equals(Intent.ACTION_USER_UNLOCKED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1158 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1159 | mConditionProviders.onUserUnlocked(user); |
| 1160 | mListeners.onUserUnlocked(user); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1161 | mAssistants.onUserUnlocked(user); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1162 | mZenModeHelper.onUserUnlocked(user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | } |
| 1164 | } |
| 1165 | }; |
| 1166 | |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 1167 | private final class SettingsObserver extends ContentObserver { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1168 | private final Uri NOTIFICATION_BADGING_URI |
| 1169 | = Settings.Secure.getUriFor(Settings.Secure.NOTIFICATION_BADGING); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1170 | private final Uri NOTIFICATION_LIGHT_PULSE_URI |
| 1171 | = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1172 | private final Uri NOTIFICATION_RATE_LIMIT_URI |
| 1173 | = Settings.Global.getUriFor(Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1174 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1175 | SettingsObserver(Handler handler) { |
| 1176 | super(handler); |
| 1177 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1178 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1179 | void observe() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1180 | ContentResolver resolver = getContext().getContentResolver(); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1181 | resolver.registerContentObserver(NOTIFICATION_BADGING_URI, |
| 1182 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1183 | resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1184 | false, this, UserHandle.USER_ALL); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1185 | resolver.registerContentObserver(NOTIFICATION_RATE_LIMIT_URI, |
| 1186 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1187 | update(null); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1190 | @Override public void onChange(boolean selfChange, Uri uri) { |
| 1191 | update(uri); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1194 | public void update(Uri uri) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1195 | ContentResolver resolver = getContext().getContentResolver(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1196 | if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { |
zhoulei | 7e37697 | 2017-05-17 18:41:25 +0800 | [diff] [blame] | 1197 | boolean pulseEnabled = Settings.System.getIntForUser(resolver, |
| 1198 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0, UserHandle.USER_CURRENT) != 0; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1199 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 1200 | mNotificationPulseEnabled = pulseEnabled; |
| 1201 | updateNotificationPulse(); |
| 1202 | } |
| 1203 | } |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1204 | if (uri == null || NOTIFICATION_RATE_LIMIT_URI.equals(uri)) { |
| 1205 | mMaxPackageEnqueueRate = Settings.Global.getFloat(resolver, |
| 1206 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, mMaxPackageEnqueueRate); |
| 1207 | } |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1208 | if (uri == null || NOTIFICATION_BADGING_URI.equals(uri)) { |
| 1209 | mRankingHelper.updateBadgingEnabled(); |
| 1210 | } |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1211 | } |
| 1212 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1213 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1214 | private SettingsObserver mSettingsObserver; |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1215 | protected ZenModeHelper mZenModeHelper; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1216 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1217 | static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) { |
| 1218 | int[] ar = r.getIntArray(resid); |
| 1219 | if (ar == null) { |
| 1220 | return def; |
| 1221 | } |
| 1222 | final int len = ar.length > maxlen ? maxlen : ar.length; |
| 1223 | long[] out = new long[len]; |
| 1224 | for (int i=0; i<len; i++) { |
| 1225 | out[i] = ar[i]; |
| 1226 | } |
| 1227 | return out; |
| 1228 | } |
| 1229 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 1230 | public NotificationManagerService(Context context) { |
| 1231 | super(context); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 1232 | Notification.processWhitelistToken = WHITELIST_TOKEN; |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1235 | // TODO - replace these methods with a single VisibleForTesting constructor |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1236 | @VisibleForTesting |
| 1237 | void setAudioManager(AudioManager audioMananger) { |
| 1238 | mAudioManager = audioMananger; |
| 1239 | } |
| 1240 | |
| 1241 | @VisibleForTesting |
| 1242 | void setVibrator(Vibrator vibrator) { |
| 1243 | mVibrator = vibrator; |
| 1244 | } |
| 1245 | |
| 1246 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1247 | void setLights(Light light) { |
| 1248 | mNotificationLight = light; |
| 1249 | mAttentionLight = light; |
Julia Reynolds | 033a412 | 2017-01-31 16:50:38 -0500 | [diff] [blame] | 1250 | mNotificationPulseEnabled = true; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | @VisibleForTesting |
| 1254 | void setScreenOn(boolean on) { |
| 1255 | mScreenOn = on; |
| 1256 | } |
| 1257 | |
| 1258 | @VisibleForTesting |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1259 | int getNotificationRecordCount() { |
| 1260 | synchronized (mNotificationLock) { |
| 1261 | int count = mNotificationList.size() + mNotificationsByKey.size() |
| 1262 | + mSummaryByGroupKey.size() + mEnqueuedNotifications.size(); |
| 1263 | // subtract duplicates |
| 1264 | for (NotificationRecord posted : mNotificationList) { |
| 1265 | if (mNotificationsByKey.containsKey(posted.getKey())) { |
| 1266 | count--; |
| 1267 | } |
| 1268 | if (posted.sbn.isGroup() && posted.getNotification().isGroupSummary()) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1269 | count--; |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | return count; |
| 1274 | } |
| 1275 | } |
| 1276 | |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 1277 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1278 | void clearNotifications() { |
| 1279 | mEnqueuedNotifications.clear(); |
| 1280 | mNotificationList.clear(); |
| 1281 | mNotificationsByKey.clear(); |
| 1282 | mSummaryByGroupKey.clear(); |
| 1283 | } |
| 1284 | |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1285 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1286 | void addNotification(NotificationRecord r) { |
| 1287 | mNotificationList.add(r); |
| 1288 | mNotificationsByKey.put(r.sbn.getKey(), r); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1289 | if (r.sbn.isGroup()) { |
| 1290 | mSummaryByGroupKey.put(r.getGroupKey(), r); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | @VisibleForTesting |
| 1295 | void addEnqueuedNotification(NotificationRecord r) { |
| 1296 | mEnqueuedNotifications.add(r); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | @VisibleForTesting |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1300 | NotificationRecord getNotificationRecord(String key) { |
| 1301 | return mNotificationsByKey.get(key); |
| 1302 | } |
| 1303 | |
| 1304 | |
| 1305 | @VisibleForTesting |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1306 | void setSystemReady(boolean systemReady) { |
| 1307 | mSystemReady = systemReady; |
| 1308 | } |
| 1309 | |
| 1310 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1311 | void setHandler(WorkerHandler handler) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1312 | mHandler = handler; |
| 1313 | } |
| 1314 | |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1315 | @VisibleForTesting |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 1316 | void setFallbackVibrationPattern(long[] vibrationPattern) { |
| 1317 | mFallbackVibrationPattern = vibrationPattern; |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1318 | } |
| 1319 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1320 | @VisibleForTesting |
| 1321 | void setPackageManager(IPackageManager packageManager) { |
| 1322 | mPackageManager = packageManager; |
| 1323 | } |
| 1324 | |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1325 | @VisibleForTesting |
| 1326 | void setRankingHelper(RankingHelper rankingHelper) { |
| 1327 | mRankingHelper = rankingHelper; |
| 1328 | } |
| 1329 | |
| 1330 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1331 | void setRankingHandler(RankingHandler rankingHandler) { |
| 1332 | mRankingHandler = rankingHandler; |
| 1333 | } |
| 1334 | |
| 1335 | @VisibleForTesting |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1336 | void setIsTelevision(boolean isTelevision) { |
| 1337 | mIsTelevision = isTelevision; |
| 1338 | } |
| 1339 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 1340 | @VisibleForTesting |
| 1341 | void setUsageStats(NotificationUsageStats us) { |
| 1342 | mUsageStats = us; |
| 1343 | } |
| 1344 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 1345 | @VisibleForTesting |
| 1346 | void setAccessibilityManager(AccessibilityManager am) { |
| 1347 | mAccessibilityManager = am; |
| 1348 | } |
| 1349 | |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1350 | // TODO: All tests should use this init instead of the one-off setters above. |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1351 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1352 | void init(Looper looper, IPackageManager packageManager, |
| 1353 | PackageManager packageManagerClient, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1354 | LightsManager lightsManager, NotificationListeners notificationListeners, |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1355 | NotificationAssistants notificationAssistants, ConditionProviders conditionProviders, |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1356 | ICompanionDeviceManager companionManager, SnoozeHelper snoozeHelper, |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1357 | NotificationUsageStats usageStats, AtomicFile policyFile, |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 1358 | ActivityManager activityManager, GroupHelper groupHelper, IActivityManager am, |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 1359 | UsageStatsManagerInternal appUsageStats, DevicePolicyManagerInternal dpm) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1360 | Resources resources = getContext().getResources(); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1361 | mMaxPackageEnqueueRate = Settings.Global.getFloat(getContext().getContentResolver(), |
| 1362 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, |
| 1363 | DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE); |
| 1364 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 1365 | mAccessibilityManager = |
| 1366 | (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1367 | mAm = am; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1368 | mPackageManager = packageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1369 | mPackageManagerClient = packageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1370 | mAppOps = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE); |
| 1371 | mVibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE); |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 1372 | mAppUsageStats = appUsageStats; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1373 | mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1374 | mCompanionManager = companionManager; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1375 | mActivityManager = activityManager; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 1376 | mDeviceIdleController = IDeviceIdleController.Stub.asInterface( |
| 1377 | ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER)); |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 1378 | mDpm = dpm; |
| 1379 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1380 | mHandler = new WorkerHandler(looper); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1381 | mRankingThread.start(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1382 | String[] extractorNames; |
| 1383 | try { |
| 1384 | extractorNames = resources.getStringArray(R.array.config_notificationSignalExtractors); |
| 1385 | } catch (Resources.NotFoundException e) { |
| 1386 | extractorNames = new String[0]; |
| 1387 | } |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1388 | mUsageStats = usageStats; |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 1389 | mMetricsLogger = new MetricsLogger(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1390 | mRankingHandler = new RankingHandlerWorker(mRankingThread.getLooper()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1391 | mConditionProviders = conditionProviders; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1392 | mZenModeHelper = new ZenModeHelper(getContext(), mHandler.getLooper(), mConditionProviders); |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 1393 | mZenModeHelper.addCallback(new ZenModeHelper.Callback() { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1394 | @Override |
| 1395 | public void onConfigChanged() { |
| 1396 | savePolicyFile(); |
| 1397 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1398 | |
| 1399 | @Override |
| 1400 | void onZenModeChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1401 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED); |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1402 | getContext().sendBroadcastAsUser( |
Jason Monk | 6350674 | 2015-12-16 12:06:51 -0500 | [diff] [blame] | 1403 | new Intent(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL) |
| 1404 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT), |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1405 | UserHandle.ALL, android.Manifest.permission.MANAGE_NOTIFICATIONS); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1406 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1407 | updateInterruptionFilterLocked(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1408 | } |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 1409 | mRankingHandler.requestSort(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1410 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1411 | |
| 1412 | @Override |
| 1413 | void onPolicyChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1414 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED); |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 1415 | mRankingHandler.requestSort(); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1416 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1417 | }); |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 1418 | mRankingHelper = new RankingHelper(getContext(), |
| 1419 | mPackageManagerClient, |
| 1420 | mRankingHandler, |
| 1421 | mZenModeHelper, |
| 1422 | mUsageStats, |
| 1423 | extractorNames); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1424 | mSnoozeHelper = snoozeHelper; |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1425 | mGroupHelper = groupHelper; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 1426 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1427 | // This is a ManagedServices object that keeps track of the listeners. |
| 1428 | mListeners = notificationListeners; |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1429 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 1430 | // This is a MangedServices object that keeps track of the assistant. |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1431 | mAssistants = notificationAssistants; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1432 | |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 1433 | // Needs to be set before loadPolicyFile |
| 1434 | mAllowedManagedServicePackages = this::canUseManagedServices; |
| 1435 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1436 | mPolicyFile = policyFile; |
| 1437 | loadPolicyFile(); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1438 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1439 | mStatusBar = getLocalService(StatusBarManagerInternal.class); |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 1440 | if (mStatusBar != null) { |
| 1441 | mStatusBar.setNotificationDelegate(mNotificationDelegate); |
| 1442 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1443 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1444 | mNotificationLight = lightsManager.getLight(LightsManager.LIGHT_ID_NOTIFICATIONS); |
| 1445 | mAttentionLight = lightsManager.getLight(LightsManager.LIGHT_ID_ATTENTION); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1446 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1447 | mFallbackVibrationPattern = getLongArray(resources, |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 1448 | R.array.config_notificationFallbackVibePattern, |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1449 | VIBRATE_PATTERN_MAXLEN, |
| 1450 | DEFAULT_VIBRATE_PATTERN); |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 1451 | mInCallNotificationUri = Uri.parse("file://" + |
| 1452 | resources.getString(R.string.config_inCallNotificationSound)); |
| 1453 | mInCallNotificationAudioAttributes = new AudioAttributes.Builder() |
| 1454 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 1455 | .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 1456 | .build(); |
| 1457 | mInCallNotificationVolume = resources.getFloat(R.dimen.config_inCallNotificationVolume); |
| 1458 | |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 1459 | mUseAttentionLight = resources.getBoolean(R.bool.config_useAttentionLight); |
| 1460 | |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1461 | // Don't start allowing notifications until the setup wizard has run once. |
| 1462 | // After that, including subsequent boots, init with notifications turned on. |
| 1463 | // This works on the first boot because the setup wizard will toggle this |
| 1464 | // flag at least once and we'll go back to 0 after that. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1465 | if (0 == Settings.Global.getInt(getContext().getContentResolver(), |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1466 | Settings.Global.DEVICE_PROVISIONED, 0)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1467 | mDisableNotificationEffects = true; |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1468 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1469 | mZenModeHelper.initZenMode(); |
John Spurlock | f370177 | 2015-02-12 13:29:37 -0500 | [diff] [blame] | 1470 | mInterruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1471 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1472 | mUserProfiles.updateCache(getContext()); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 1473 | listenForCallState(); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1474 | |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1475 | mSettingsObserver = new SettingsObserver(mHandler); |
| 1476 | |
| 1477 | mArchive = new Archive(resources.getInteger( |
| 1478 | R.integer.config_notificationServiceArchiveSize)); |
| 1479 | |
| 1480 | mIsTelevision = mPackageManagerClient.hasSystemFeature(FEATURE_LEANBACK) |
| 1481 | || mPackageManagerClient.hasSystemFeature(FEATURE_TELEVISION); |
| 1482 | } |
| 1483 | |
| 1484 | @Override |
| 1485 | public void onStart() { |
| 1486 | SnoozeHelper snoozeHelper = new SnoozeHelper(getContext(), new SnoozeHelper.Callback() { |
| 1487 | @Override |
| 1488 | public void repost(int userId, NotificationRecord r) { |
| 1489 | try { |
| 1490 | if (DBG) { |
| 1491 | Slog.d(TAG, "Reposting " + r.getKey()); |
| 1492 | } |
| 1493 | enqueueNotificationInternal(r.sbn.getPackageName(), r.sbn.getOpPkg(), |
| 1494 | r.sbn.getUid(), r.sbn.getInitialPid(), r.sbn.getTag(), r.sbn.getId(), |
| 1495 | r.sbn.getNotification(), userId); |
| 1496 | } catch (Exception e) { |
| 1497 | Slog.e(TAG, "Cannot un-snooze notification", e); |
| 1498 | } |
| 1499 | } |
| 1500 | }, mUserProfiles); |
| 1501 | |
| 1502 | final File systemDir = new File(Environment.getDataDirectory(), "system"); |
| 1503 | |
| 1504 | init(Looper.myLooper(), |
| 1505 | AppGlobals.getPackageManager(), getContext().getPackageManager(), |
| 1506 | getLocalService(LightsManager.class), |
| 1507 | new NotificationListeners(AppGlobals.getPackageManager()), |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 1508 | new NotificationAssistants(getContext(), mNotificationLock, mUserProfiles, |
| 1509 | AppGlobals.getPackageManager()), |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1510 | new ConditionProviders(getContext(), mUserProfiles, AppGlobals.getPackageManager()), |
| 1511 | null, snoozeHelper, new NotificationUsageStats(getContext()), |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 1512 | new AtomicFile(new File(systemDir, "notification_policy.xml"), "notification-policy"), |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1513 | (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE), |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 1514 | getGroupHelper(), ActivityManager.getService(), |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 1515 | LocalServices.getService(UsageStatsManagerInternal.class), |
| 1516 | LocalServices.getService(DevicePolicyManagerInternal.class)); |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1517 | |
Mike Lockwood | 35e16bf | 2010-11-30 19:53:36 -0500 | [diff] [blame] | 1518 | // register for various Intents |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1519 | IntentFilter filter = new IntentFilter(); |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1520 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 1521 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1522 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1523 | filter.addAction(Intent.ACTION_USER_PRESENT); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1524 | filter.addAction(Intent.ACTION_USER_STOPPED); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1525 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1526 | filter.addAction(Intent.ACTION_USER_ADDED); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1527 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1528 | filter.addAction(Intent.ACTION_USER_UNLOCKED); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1529 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1530 | getContext().registerReceiver(mIntentReceiver, filter); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1531 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1532 | IntentFilter pkgFilter = new IntentFilter(); |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1533 | pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1534 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
Daniel Sandler | aac0eb0 | 2011-08-06 22:51:56 -0400 | [diff] [blame] | 1535 | pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1536 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 1537 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 1538 | pkgFilter.addDataScheme("package"); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1539 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, pkgFilter, null, |
| 1540 | null); |
| 1541 | |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1542 | IntentFilter suspendedPkgFilter = new IntentFilter(); |
| 1543 | suspendedPkgFilter.addAction(Intent.ACTION_PACKAGES_SUSPENDED); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1544 | suspendedPkgFilter.addAction(Intent.ACTION_PACKAGES_UNSUSPENDED); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1545 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, |
| 1546 | suspendedPkgFilter, null, null); |
| 1547 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1548 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1549 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, sdFilter, null, |
| 1550 | null); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1551 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1552 | IntentFilter timeoutFilter = new IntentFilter(ACTION_NOTIFICATION_TIMEOUT); |
| 1553 | timeoutFilter.addDataScheme(SCHEME_TIMEOUT); |
| 1554 | getContext().registerReceiver(mNotificationTimeoutReceiver, timeoutFilter); |
| 1555 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1556 | IntentFilter settingsRestoredFilter = new IntentFilter(Intent.ACTION_SETTING_RESTORED); |
| 1557 | getContext().registerReceiver(mRestoreReceiver, settingsRestoredFilter); |
| 1558 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1559 | IntentFilter localeChangedFilter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED); |
| 1560 | getContext().registerReceiver(mLocaleChangeReceiver, localeChangedFilter); |
| 1561 | |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 1562 | publishBinderService(Context.NOTIFICATION_SERVICE, mService, /* allowIsolated= */ false, |
| 1563 | DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1564 | publishLocalService(NotificationManagerInternal.class, mInternalService); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1565 | } |
| 1566 | |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1567 | private GroupHelper getGroupHelper() { |
| 1568 | return new GroupHelper(new GroupHelper.Callback() { |
| 1569 | @Override |
| 1570 | public void addAutoGroup(String key) { |
| 1571 | synchronized (mNotificationLock) { |
| 1572 | addAutogroupKeyLocked(key); |
| 1573 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | @Override |
| 1577 | public void removeAutoGroup(String key) { |
| 1578 | synchronized (mNotificationLock) { |
| 1579 | removeAutogroupKeyLocked(key); |
| 1580 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | @Override |
| 1584 | public void addAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 1585 | createAutoGroupSummary(userId, pkg, triggeringKey); |
| 1586 | } |
| 1587 | |
| 1588 | @Override |
| 1589 | public void removeAutoGroupSummary(int userId, String pkg) { |
| 1590 | synchronized (mNotificationLock) { |
| 1591 | clearAutogroupSummaryLocked(userId, pkg); |
| 1592 | } |
| 1593 | } |
| 1594 | }); |
| 1595 | } |
| 1596 | |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1597 | private void sendRegisteredOnlyBroadcast(String action) { |
| 1598 | getContext().sendBroadcastAsUser(new Intent(action) |
| 1599 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), UserHandle.ALL, null); |
| 1600 | } |
| 1601 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1602 | @Override |
| 1603 | public void onBootPhase(int phase) { |
| 1604 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 1605 | // no beeping until we're basically done booting |
| 1606 | mSystemReady = true; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1607 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1608 | // Grab our optional AudioService |
| 1609 | mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1610 | mAudioManagerInternal = getLocalService(AudioManagerInternal.class); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1611 | mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1612 | mZenModeHelper.onSystemReady(); |
Adam Lesinski | a6db4ab | 2014-03-24 12:31:45 -0700 | [diff] [blame] | 1613 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 1614 | // This observer will force an update when observe is called, causing us to |
| 1615 | // bind to listener services. |
| 1616 | mSettingsObserver.observe(); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1617 | mListeners.onBootPhaseAppsCanStart(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1618 | mAssistants.onBootPhaseAppsCanStart(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1619 | mConditionProviders.onBootPhaseAppsCanStart(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1620 | } |
| 1621 | } |
| 1622 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1623 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1624 | private void updateListenerHintsLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1625 | final int hints = calculateHints(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1626 | if (hints == mListenerHints) return; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1627 | ZenLog.traceListenerHintsChanged(mListenerHints, hints, mEffectsSuppressors.size()); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1628 | mListenerHints = hints; |
| 1629 | scheduleListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 1630 | } |
| 1631 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1632 | @GuardedBy("mNotificationLock") |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1633 | private void updateEffectsSuppressorLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1634 | final long updatedSuppressedEffects = calculateSuppressedEffects(); |
| 1635 | if (updatedSuppressedEffects == mZenModeHelper.getSuppressedEffects()) return; |
| 1636 | final List<ComponentName> suppressors = getSuppressors(); |
| 1637 | ZenLog.traceEffectsSuppressorChanged(mEffectsSuppressors, suppressors, updatedSuppressedEffects); |
| 1638 | mEffectsSuppressors = suppressors; |
| 1639 | mZenModeHelper.setSuppressedEffects(updatedSuppressedEffects); |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1640 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1641 | } |
| 1642 | |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 1643 | private void exitIdle() { |
| 1644 | try { |
| 1645 | if (mDeviceIdleController != null) { |
| 1646 | mDeviceIdleController.exitIdle("notification interaction"); |
| 1647 | } |
| 1648 | } catch (RemoteException e) { |
| 1649 | } |
| 1650 | } |
| 1651 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1652 | private void updateNotificationChannelInt(String pkg, int uid, NotificationChannel channel, |
| 1653 | boolean fromListener) { |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1654 | if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) { |
| 1655 | // cancel |
| 1656 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channel.getId(), 0, 0, true, |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1657 | UserHandle.getUserId(uid), REASON_CHANNEL_BANNED, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1658 | null); |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1659 | if (isUidSystemOrPhone(uid)) { |
| 1660 | int[] profileIds = mUserProfiles.getCurrentProfileIds(); |
| 1661 | int N = profileIds.length; |
| 1662 | for (int i = 0; i < N; i++) { |
| 1663 | int profileId = profileIds[i]; |
| 1664 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channel.getId(), 0, 0, true, |
| 1665 | profileId, REASON_CHANNEL_BANNED, |
| 1666 | null); |
| 1667 | } |
| 1668 | } |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1669 | } |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1670 | final NotificationChannel preUpdate = |
| 1671 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), true); |
| 1672 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1673 | mRankingHelper.updateNotificationChannel(pkg, uid, channel, true); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1674 | maybeNotifyChannelOwner(pkg, uid, preUpdate, channel); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1675 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1676 | if (!fromListener) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1677 | final NotificationChannel modifiedChannel = |
| 1678 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), false); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1679 | mListeners.notifyNotificationChannelChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1680 | pkg, UserHandle.getUserHandleForUid(uid), |
| 1681 | modifiedChannel, NOTIFICATION_CHANNEL_OR_GROUP_UPDATED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1682 | } |
| 1683 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1684 | savePolicyFile(); |
| 1685 | } |
| 1686 | |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1687 | private void maybeNotifyChannelOwner(String pkg, int uid, NotificationChannel preUpdate, |
| 1688 | NotificationChannel update) { |
| 1689 | try { |
| 1690 | if ((preUpdate.getImportance() == IMPORTANCE_NONE |
| 1691 | && update.getImportance() != IMPORTANCE_NONE) |
| 1692 | || (preUpdate.getImportance() != IMPORTANCE_NONE |
| 1693 | && update.getImportance() == IMPORTANCE_NONE)) { |
| 1694 | getContext().sendBroadcastAsUser( |
| 1695 | new Intent(ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED) |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 1696 | .putExtra(NotificationManager.EXTRA_NOTIFICATION_CHANNEL_ID, |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1697 | update.getId()) |
| 1698 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, |
| 1699 | update.getImportance() == IMPORTANCE_NONE) |
| 1700 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 1701 | .setPackage(pkg), |
| 1702 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 1703 | } |
| 1704 | } catch (SecurityException e) { |
| 1705 | Slog.w(TAG, "Can't notify app about channel change", e); |
| 1706 | } |
| 1707 | } |
| 1708 | |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1709 | private void createNotificationChannelGroup(String pkg, int uid, NotificationChannelGroup group, |
| 1710 | boolean fromApp, boolean fromListener) { |
| 1711 | Preconditions.checkNotNull(group); |
| 1712 | Preconditions.checkNotNull(pkg); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1713 | |
| 1714 | final NotificationChannelGroup preUpdate = |
| 1715 | mRankingHelper.getNotificationChannelGroup(group.getId(), pkg, uid); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1716 | mRankingHelper.createNotificationChannelGroup(pkg, uid, group, |
| 1717 | fromApp); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1718 | if (!fromApp) { |
| 1719 | maybeNotifyChannelGroupOwner(pkg, uid, preUpdate, group); |
| 1720 | } |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1721 | if (!fromListener) { |
| 1722 | mListeners.notifyNotificationChannelGroupChanged(pkg, |
| 1723 | UserHandle.of(UserHandle.getCallingUserId()), group, |
| 1724 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
| 1725 | } |
| 1726 | } |
| 1727 | |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1728 | private void maybeNotifyChannelGroupOwner(String pkg, int uid, |
| 1729 | NotificationChannelGroup preUpdate, NotificationChannelGroup update) { |
| 1730 | try { |
| 1731 | if (preUpdate.isBlocked() != update.isBlocked()) { |
| 1732 | getContext().sendBroadcastAsUser( |
| 1733 | new Intent(ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED) |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 1734 | .putExtra(NotificationManager.EXTRA_NOTIFICATION_CHANNEL_GROUP_ID, |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1735 | update.getId()) |
| 1736 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, |
| 1737 | update.isBlocked()) |
| 1738 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 1739 | .setPackage(pkg), |
| 1740 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 1741 | } |
| 1742 | } catch (SecurityException e) { |
| 1743 | Slog.w(TAG, "Can't notify app about group change", e); |
| 1744 | } |
| 1745 | } |
| 1746 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1747 | private ArrayList<ComponentName> getSuppressors() { |
| 1748 | ArrayList<ComponentName> names = new ArrayList<ComponentName>(); |
| 1749 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1750 | ArraySet<ManagedServiceInfo> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
| 1751 | |
| 1752 | for (ManagedServiceInfo info : serviceInfoList) { |
| 1753 | names.add(info.component); |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | return names; |
| 1758 | } |
| 1759 | |
| 1760 | private boolean removeDisabledHints(ManagedServiceInfo info) { |
| 1761 | return removeDisabledHints(info, 0); |
| 1762 | } |
| 1763 | |
| 1764 | private boolean removeDisabledHints(ManagedServiceInfo info, int hints) { |
| 1765 | boolean removed = false; |
| 1766 | |
| 1767 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1768 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 1769 | final ArraySet<ManagedServiceInfo> listeners = |
| 1770 | mListenersDisablingEffects.valueAt(i); |
| 1771 | |
| 1772 | if (hints == 0 || (hint & hints) == hint) { |
| 1773 | removed = removed || listeners.remove(info); |
| 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | return removed; |
| 1778 | } |
| 1779 | |
| 1780 | private void addDisabledHints(ManagedServiceInfo info, int hints) { |
| 1781 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1782 | addDisabledHint(info, HINT_HOST_DISABLE_EFFECTS); |
| 1783 | } |
| 1784 | |
| 1785 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1786 | addDisabledHint(info, HINT_HOST_DISABLE_NOTIFICATION_EFFECTS); |
| 1787 | } |
| 1788 | |
| 1789 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1790 | addDisabledHint(info, HINT_HOST_DISABLE_CALL_EFFECTS); |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | private void addDisabledHint(ManagedServiceInfo info, int hint) { |
| 1795 | if (mListenersDisablingEffects.indexOfKey(hint) < 0) { |
| 1796 | mListenersDisablingEffects.put(hint, new ArraySet<ManagedServiceInfo>()); |
| 1797 | } |
| 1798 | |
| 1799 | ArraySet<ManagedServiceInfo> hintListeners = mListenersDisablingEffects.get(hint); |
| 1800 | hintListeners.add(info); |
| 1801 | } |
| 1802 | |
| 1803 | private int calculateHints() { |
| 1804 | int hints = 0; |
| 1805 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1806 | int hint = mListenersDisablingEffects.keyAt(i); |
| 1807 | ArraySet<ManagedServiceInfo> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
| 1808 | |
| 1809 | if (!serviceInfoList.isEmpty()) { |
| 1810 | hints |= hint; |
| 1811 | } |
| 1812 | } |
| 1813 | |
| 1814 | return hints; |
| 1815 | } |
| 1816 | |
| 1817 | private long calculateSuppressedEffects() { |
| 1818 | int hints = calculateHints(); |
| 1819 | long suppressedEffects = 0; |
| 1820 | |
| 1821 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1822 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_ALL; |
| 1823 | } |
| 1824 | |
| 1825 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1826 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_NOTIFICATIONS; |
| 1827 | } |
| 1828 | |
| 1829 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1830 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_CALLS; |
| 1831 | } |
| 1832 | |
| 1833 | return suppressedEffects; |
| 1834 | } |
| 1835 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1836 | @GuardedBy("mNotificationLock") |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1837 | private void updateInterruptionFilterLocked() { |
| 1838 | int interruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
| 1839 | if (interruptionFilter == mInterruptionFilter) return; |
| 1840 | mInterruptionFilter = interruptionFilter; |
| 1841 | scheduleInterruptionFilterChanged(interruptionFilter); |
| 1842 | } |
| 1843 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1844 | @VisibleForTesting |
| 1845 | INotificationManager getBinderService() { |
| 1846 | return INotificationManager.Stub.asInterface(mService); |
| 1847 | } |
| 1848 | |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1849 | /** |
| 1850 | * Report to usage stats that the notification was seen. |
| 1851 | * @param r notification record |
| 1852 | */ |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1853 | @GuardedBy("mNotificationLock") |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 1854 | protected void reportSeen(NotificationRecord r) { |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 1855 | mAppUsageStats.reportEvent(r.sbn.getPackageName(), |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1856 | getRealUserId(r.sbn.getUserId()), |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 1857 | UsageEvents.Event.NOTIFICATION_SEEN); |
| 1858 | } |
| 1859 | |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 1860 | protected int calculateSuppressedVisualEffects(Policy incomingPolicy, Policy currPolicy, |
| 1861 | int targetSdkVersion) { |
| 1862 | if (incomingPolicy.suppressedVisualEffects == SUPPRESSED_EFFECTS_UNSET) { |
| 1863 | return incomingPolicy.suppressedVisualEffects; |
| 1864 | } |
| 1865 | final int[] effectsIntroducedInP = { |
| 1866 | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT, |
| 1867 | SUPPRESSED_EFFECT_LIGHTS, |
| 1868 | SUPPRESSED_EFFECT_PEEK, |
| 1869 | SUPPRESSED_EFFECT_STATUS_BAR, |
| 1870 | SUPPRESSED_EFFECT_BADGE, |
| 1871 | SUPPRESSED_EFFECT_AMBIENT, |
| 1872 | SUPPRESSED_EFFECT_NOTIFICATION_LIST |
| 1873 | }; |
| 1874 | |
| 1875 | int newSuppressedVisualEffects = incomingPolicy.suppressedVisualEffects; |
Jeff Sharkey | aa1a911 | 2018-04-10 15:18:12 -0600 | [diff] [blame] | 1876 | if (targetSdkVersion < Build.VERSION_CODES.P) { |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 1877 | // unset higher order bits introduced in P, maintain the user's higher order bits |
| 1878 | for (int i = 0; i < effectsIntroducedInP.length ; i++) { |
| 1879 | newSuppressedVisualEffects &= ~effectsIntroducedInP[i]; |
| 1880 | newSuppressedVisualEffects |= |
| 1881 | (currPolicy.suppressedVisualEffects & effectsIntroducedInP[i]); |
| 1882 | } |
| 1883 | // set higher order bits according to lower order bits |
| 1884 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_OFF) != 0) { |
| 1885 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_LIGHTS; |
| 1886 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 1887 | } |
| 1888 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_ON) != 0) { |
| 1889 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_PEEK; |
| 1890 | } |
| 1891 | } else { |
| 1892 | boolean hasNewEffects = (newSuppressedVisualEffects |
| 1893 | - SUPPRESSED_EFFECT_SCREEN_ON - SUPPRESSED_EFFECT_SCREEN_OFF) > 0; |
| 1894 | // if any of the new effects introduced in P are set |
| 1895 | if (hasNewEffects) { |
| 1896 | // clear out the deprecated effects |
| 1897 | newSuppressedVisualEffects &= ~ (SUPPRESSED_EFFECT_SCREEN_ON |
| 1898 | | SUPPRESSED_EFFECT_SCREEN_OFF); |
| 1899 | |
| 1900 | // set the deprecated effects according to the new more specific effects |
| 1901 | if ((newSuppressedVisualEffects & Policy.SUPPRESSED_EFFECT_PEEK) != 0) { |
| 1902 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_SCREEN_ON; |
| 1903 | } |
| 1904 | if ((newSuppressedVisualEffects & Policy.SUPPRESSED_EFFECT_LIGHTS) != 0 |
| 1905 | && (newSuppressedVisualEffects |
| 1906 | & Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT) != 0 |
| 1907 | && (newSuppressedVisualEffects |
| 1908 | & Policy.SUPPRESSED_EFFECT_AMBIENT) != 0) { |
| 1909 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_SCREEN_OFF; |
| 1910 | } |
| 1911 | } else { |
| 1912 | // set higher order bits according to lower order bits |
| 1913 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_OFF) != 0) { |
| 1914 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_LIGHTS; |
| 1915 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 1916 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_AMBIENT; |
| 1917 | } |
| 1918 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_ON) != 0) { |
| 1919 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_PEEK; |
| 1920 | } |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | return newSuppressedVisualEffects; |
| 1925 | } |
| 1926 | |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1927 | @GuardedBy("mNotificationLock") |
| 1928 | protected void maybeRecordInterruptionLocked(NotificationRecord r) { |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 1929 | if (r.isInterruptive() && !r.hasRecordedInterruption()) { |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1930 | mAppUsageStats.reportInterruptiveNotification(r.sbn.getPackageName(), |
| 1931 | r.getChannel().getId(), |
| 1932 | getRealUserId(r.sbn.getUserId())); |
Julia Reynolds | ad7d713 | 2018-03-21 16:05:00 -0400 | [diff] [blame] | 1933 | logRecentLocked(r); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 1934 | r.setRecordedInterruption(true); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1935 | } |
| 1936 | } |
| 1937 | |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1938 | /** |
| 1939 | * Report to usage stats that the notification was clicked. |
| 1940 | * @param r notification record |
| 1941 | */ |
| 1942 | protected void reportUserInteraction(NotificationRecord r) { |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1943 | mAppUsageStats.reportEvent(r.sbn.getPackageName(), |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1944 | getRealUserId(r.sbn.getUserId()), |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1945 | UsageEvents.Event.USER_INTERACTION); |
| 1946 | } |
| 1947 | |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1948 | private int getRealUserId(int userId) { |
| 1949 | return userId == UserHandle.USER_ALL ? UserHandle.USER_SYSTEM : userId; |
| 1950 | } |
| 1951 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1952 | @VisibleForTesting |
| 1953 | NotificationManagerInternal getInternalService() { |
| 1954 | return mInternalService; |
| 1955 | } |
| 1956 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1957 | private final IBinder mService = new INotificationManager.Stub() { |
| 1958 | // Toasts |
| 1959 | // ============================================================================ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1960 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1961 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1962 | public void enqueueToast(String pkg, ITransientNotification callback, int duration) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1963 | { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1964 | if (DBG) { |
| 1965 | Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback |
| 1966 | + " duration=" + duration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1967 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1968 | |
| 1969 | if (pkg == null || callback == null) { |
| 1970 | Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback); |
| 1971 | return ; |
| 1972 | } |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1973 | final boolean isSystemToast = isCallerSystemOrPhone() || ("android".equals(pkg)); |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 1974 | final boolean isPackageSuspended = |
| 1975 | isPackageSuspendedForUser(pkg, Binder.getCallingUid()); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1976 | |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 1977 | if (ENABLE_BLOCKED_TOASTS && !isSystemToast && |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1978 | (!areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()) |
| 1979 | || isPackageSuspended)) { |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 1980 | Slog.e(TAG, "Suppressing toast from package " + pkg |
| 1981 | + (isPackageSuspended |
| 1982 | ? " due to package suspended by administrator." |
| 1983 | : " by user request.")); |
| 1984 | return; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | synchronized (mToastQueue) { |
| 1988 | int callingPid = Binder.getCallingPid(); |
| 1989 | long callingId = Binder.clearCallingIdentity(); |
| 1990 | try { |
| 1991 | ToastRecord record; |
Beverly Tai | 98efc79 | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 1992 | int index = indexOfToastLocked(pkg, callback); |
| 1993 | // If it's already in the queue, we update it in place, we don't |
| 1994 | // move it to the end of the queue. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1995 | if (index >= 0) { |
| 1996 | record = mToastQueue.get(index); |
| 1997 | record.update(duration); |
| 1998 | } else { |
Beverly Tai | 98efc79 | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 1999 | // Limit the number of toasts that any given package except the android |
| 2000 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| 2001 | if (!isSystemToast) { |
| 2002 | int count = 0; |
| 2003 | final int N = mToastQueue.size(); |
| 2004 | for (int i=0; i<N; i++) { |
| 2005 | final ToastRecord r = mToastQueue.get(i); |
| 2006 | if (r.pkg.equals(pkg)) { |
| 2007 | count++; |
| 2008 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 2009 | Slog.e(TAG, "Package has already posted " + count |
| 2010 | + " toasts. Not showing more. Package=" + pkg); |
| 2011 | return; |
| 2012 | } |
| 2013 | } |
| 2014 | } |
| 2015 | } |
| 2016 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 2017 | Binder token = new Binder(); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 2018 | mWindowManagerInternal.addWindowToken(token, TYPE_TOAST, DEFAULT_DISPLAY); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 2019 | record = new ToastRecord(callingPid, pkg, callback, duration, token); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2020 | mToastQueue.add(record); |
| 2021 | index = mToastQueue.size() - 1; |
Beverly Tai | 98efc79 | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 2022 | keepProcessAliveIfNeededLocked(callingPid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2023 | } |
| 2024 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 2025 | // new or just been updated. Call back and tell it to show itself. |
| 2026 | // If the callback fails, this will remove it from the list, so don't |
| 2027 | // assume that it's valid after this. |
| 2028 | if (index == 0) { |
| 2029 | showNextToastLocked(); |
| 2030 | } |
| 2031 | } finally { |
| 2032 | Binder.restoreCallingIdentity(callingId); |
| 2033 | } |
| 2034 | } |
| 2035 | } |
| 2036 | |
| 2037 | @Override |
| 2038 | public void cancelToast(String pkg, ITransientNotification callback) { |
| 2039 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| 2040 | |
| 2041 | if (pkg == null || callback == null) { |
| 2042 | Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback); |
| 2043 | return ; |
| 2044 | } |
| 2045 | |
| 2046 | synchronized (mToastQueue) { |
| 2047 | long callingId = Binder.clearCallingIdentity(); |
| 2048 | try { |
| 2049 | int index = indexOfToastLocked(pkg, callback); |
| 2050 | if (index >= 0) { |
| 2051 | cancelToastLocked(index); |
| 2052 | } else { |
| 2053 | Slog.w(TAG, "Toast already cancelled. pkg=" + pkg |
| 2054 | + " callback=" + callback); |
| 2055 | } |
| 2056 | } finally { |
| 2057 | Binder.restoreCallingIdentity(callingId); |
| 2058 | } |
| 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | @Override |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 2063 | public void finishToken(String pkg, ITransientNotification callback) { |
| 2064 | synchronized (mToastQueue) { |
| 2065 | long callingId = Binder.clearCallingIdentity(); |
| 2066 | try { |
| 2067 | int index = indexOfToastLocked(pkg, callback); |
| 2068 | if (index >= 0) { |
| 2069 | ToastRecord record = mToastQueue.get(index); |
| 2070 | finishTokenLocked(record.token); |
| 2071 | } else { |
| 2072 | Slog.w(TAG, "Toast already killed. pkg=" + pkg |
| 2073 | + " callback=" + callback); |
| 2074 | } |
| 2075 | } finally { |
| 2076 | Binder.restoreCallingIdentity(callingId); |
| 2077 | } |
| 2078 | } |
| 2079 | } |
| 2080 | |
| 2081 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 2082 | public void enqueueNotificationWithTag(String pkg, String opPkg, String tag, int id, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 2083 | Notification notification, int userId) throws RemoteException { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 2084 | enqueueNotificationInternal(pkg, opPkg, Binder.getCallingUid(), |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 2085 | Binder.getCallingPid(), tag, id, notification, userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | @Override |
| 2089 | public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2090 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2091 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2092 | Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2093 | // Don't allow client applications to cancel foreground service notis or autobundled |
| 2094 | // summaries. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 2095 | final int mustNotHaveFlags = isCallingUidSystem() ? 0 : |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 2096 | (FLAG_FOREGROUND_SERVICE | Notification.FLAG_AUTOGROUP_SUMMARY); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2097 | cancelNotification(Binder.getCallingUid(), Binder.getCallingPid(), pkg, tag, id, 0, |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 2098 | mustNotHaveFlags, false, userId, REASON_APP_CANCEL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | @Override |
| 2102 | public void cancelAllNotifications(String pkg, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2103 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2104 | |
| 2105 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2106 | Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg); |
| 2107 | |
| 2108 | // Calling from user space, don't allow the canceling of actively |
| 2109 | // running foreground services. |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2110 | cancelAllNotificationsInt(Binder.getCallingUid(), Binder.getCallingPid(), |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 2111 | pkg, null, 0, FLAG_FOREGROUND_SERVICE, true, userId, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 2112 | REASON_APP_CANCEL_ALL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | @Override |
| 2116 | public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) { |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 2117 | enforceSystemOrSystemUI("setNotificationsEnabledForPackage"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2118 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 2119 | mRankingHelper.setEnabled(pkg, uid, enabled); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 2120 | // Now, cancel any outstanding notifications that are part of a just-disabled app |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 2121 | if (!enabled) { |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 2122 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, null, 0, 0, true, |
| 2123 | UserHandle.getUserId(uid), REASON_PACKAGE_BANNED, null); |
| 2124 | } |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 2125 | |
| 2126 | try { |
| 2127 | getContext().sendBroadcastAsUser( |
| 2128 | new Intent(ACTION_APP_BLOCK_STATE_CHANGED) |
| 2129 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, !enabled) |
| 2130 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 2131 | .setPackage(pkg), |
| 2132 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 2133 | } catch (SecurityException e) { |
| 2134 | Slog.w(TAG, "Can't notify app about app block change", e); |
| 2135 | } |
| 2136 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 2137 | savePolicyFile(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | /** |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 2141 | * Updates the enabled state for notifications for the given package (and uid). |
| 2142 | * Additionally, this method marks the app importance as locked by the user, which means |
| 2143 | * that notifications from the app will <b>not</b> be considered for showing a |
| 2144 | * blocking helper. |
| 2145 | * |
| 2146 | * @param pkg package that owns the notifications to update |
| 2147 | * @param uid uid of the app providing notifications |
| 2148 | * @param enabled whether notifications should be enabled for the app |
| 2149 | * |
| 2150 | * @see #setNotificationsEnabledForPackage(String, int, boolean) |
| 2151 | */ |
| 2152 | @Override |
| 2153 | public void setNotificationsEnabledWithImportanceLockForPackage( |
| 2154 | String pkg, int uid, boolean enabled) { |
| 2155 | setNotificationsEnabledForPackage(pkg, uid, enabled); |
| 2156 | |
| 2157 | mRankingHelper.setAppImportanceLocked(pkg, uid); |
| 2158 | } |
| 2159 | |
| 2160 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2161 | * Use this when you just want to know if notifications are OK for this package. |
| 2162 | */ |
| 2163 | @Override |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 2164 | public boolean areNotificationsEnabled(String pkg) { |
| 2165 | return areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()); |
| 2166 | } |
| 2167 | |
| 2168 | /** |
| 2169 | * Use this when you just want to know if notifications are OK for this package. |
| 2170 | */ |
| 2171 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2172 | public boolean areNotificationsEnabledForPackage(String pkg, int uid) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 2173 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 2174 | |
| 2175 | return mRankingHelper.getImportance(pkg, uid) != IMPORTANCE_NONE; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2176 | } |
| 2177 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 2178 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 2179 | public int getPackageImportance(String pkg) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 2180 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 2181 | return mRankingHelper.getImportance(pkg, Binder.getCallingUid()); |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | @Override |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2185 | public boolean canShowBadge(String pkg, int uid) { |
| 2186 | checkCallerIsSystem(); |
| 2187 | return mRankingHelper.canShowBadge(pkg, uid); |
| 2188 | } |
| 2189 | |
| 2190 | @Override |
| 2191 | public void setShowBadge(String pkg, int uid, boolean showBadge) { |
| 2192 | checkCallerIsSystem(); |
| 2193 | mRankingHelper.setShowBadge(pkg, uid, showBadge); |
| 2194 | savePolicyFile(); |
| 2195 | } |
| 2196 | |
| 2197 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2198 | public void updateNotificationChannelGroupForPackage(String pkg, int uid, |
| 2199 | NotificationChannelGroup group) throws RemoteException { |
| 2200 | enforceSystemOrSystemUI("Caller not system or systemui"); |
| 2201 | createNotificationChannelGroup(pkg, uid, group, false, false); |
| 2202 | savePolicyFile(); |
| 2203 | } |
| 2204 | |
| 2205 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2206 | public void createNotificationChannelGroups(String pkg, |
| 2207 | ParceledListSlice channelGroupList) throws RemoteException { |
| 2208 | checkCallerIsSystemOrSameApp(pkg); |
| 2209 | List<NotificationChannelGroup> groups = channelGroupList.getList(); |
| 2210 | final int groupSize = groups.size(); |
| 2211 | for (int i = 0; i < groupSize; i++) { |
| 2212 | final NotificationChannelGroup group = groups.get(i); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2213 | createNotificationChannelGroup(pkg, Binder.getCallingUid(), group, true, false); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2214 | } |
| 2215 | savePolicyFile(); |
| 2216 | } |
| 2217 | |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 2218 | private void createNotificationChannelsImpl(String pkg, int uid, |
| 2219 | ParceledListSlice channelsList) { |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 2220 | List<NotificationChannel> channels = channelsList.getList(); |
| 2221 | final int channelsSize = channels.size(); |
| 2222 | for (int i = 0; i < channelsSize; i++) { |
| 2223 | final NotificationChannel channel = channels.get(i); |
| 2224 | Preconditions.checkNotNull(channel, "channel in list is null"); |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 2225 | mRankingHelper.createNotificationChannel(pkg, uid, channel, |
Julia Reynolds | 1fe1094 | 2018-03-28 12:46:51 -0400 | [diff] [blame] | 2226 | true /* fromTargetApp */, mConditionProviders.isPackageOrComponentAllowed( |
| 2227 | pkg, UserHandle.getUserId(uid))); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2228 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2229 | UserHandle.getUserHandleForUid(uid), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2230 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), false), |
| 2231 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 2232 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2233 | savePolicyFile(); |
| 2234 | } |
| 2235 | |
| 2236 | @Override |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 2237 | public void createNotificationChannels(String pkg, |
| 2238 | ParceledListSlice channelsList) throws RemoteException { |
| 2239 | checkCallerIsSystemOrSameApp(pkg); |
| 2240 | createNotificationChannelsImpl(pkg, Binder.getCallingUid(), channelsList); |
| 2241 | } |
| 2242 | |
| 2243 | @Override |
| 2244 | public void createNotificationChannelsForPackage(String pkg, int uid, |
| 2245 | ParceledListSlice channelsList) throws RemoteException { |
| 2246 | checkCallerIsSystem(); |
| 2247 | createNotificationChannelsImpl(pkg, uid, channelsList); |
| 2248 | } |
| 2249 | |
| 2250 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2251 | public NotificationChannel getNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2252 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2253 | return mRankingHelper.getNotificationChannel( |
| 2254 | pkg, Binder.getCallingUid(), channelId, false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | @Override |
| 2258 | public NotificationChannel getNotificationChannelForPackage(String pkg, int uid, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2259 | String channelId, boolean includeDeleted) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2260 | checkCallerIsSystem(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2261 | return mRankingHelper.getNotificationChannel(pkg, uid, channelId, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | @Override |
| 2265 | public void deleteNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2266 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2267 | final int callingUid = Binder.getCallingUid(); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2268 | if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 2269 | throw new IllegalArgumentException("Cannot delete default channel"); |
| 2270 | } |
| 2271 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channelId, 0, 0, true, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2272 | UserHandle.getUserId(callingUid), REASON_CHANNEL_BANNED, null); |
| 2273 | mRankingHelper.deleteNotificationChannel(pkg, callingUid, channelId); |
| 2274 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2275 | UserHandle.getUserHandleForUid(callingUid), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2276 | mRankingHelper.getNotificationChannel(pkg, callingUid, channelId, true), |
| 2277 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2278 | savePolicyFile(); |
| 2279 | } |
| 2280 | |
| 2281 | @Override |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2282 | public NotificationChannelGroup getNotificationChannelGroup(String pkg, String groupId) { |
| 2283 | checkCallerIsSystemOrSameApp(pkg); |
| 2284 | return mRankingHelper.getNotificationChannelGroupWithChannels( |
| 2285 | pkg, Binder.getCallingUid(), groupId, false); |
| 2286 | } |
| 2287 | |
| 2288 | @Override |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2289 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroups( |
| 2290 | String pkg) { |
| 2291 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2292 | return mRankingHelper.getNotificationChannelGroups( |
| 2293 | pkg, Binder.getCallingUid(), false, false); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2297 | public void deleteNotificationChannelGroup(String pkg, String groupId) { |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2298 | checkCallerIsSystemOrSameApp(pkg); |
| 2299 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2300 | final int callingUid = Binder.getCallingUid(); |
| 2301 | NotificationChannelGroup groupToDelete = |
| 2302 | mRankingHelper.getNotificationChannelGroup(groupId, pkg, callingUid); |
| 2303 | if (groupToDelete != null) { |
| 2304 | List<NotificationChannel> deletedChannels = |
| 2305 | mRankingHelper.deleteNotificationChannelGroup(pkg, callingUid, groupId); |
| 2306 | for (int i = 0; i < deletedChannels.size(); i++) { |
| 2307 | final NotificationChannel deletedChannel = deletedChannels.get(i); |
| 2308 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, deletedChannel.getId(), 0, 0, |
| 2309 | true, |
| 2310 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, |
| 2311 | null); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2312 | mListeners.notifyNotificationChannelChanged(pkg, |
| 2313 | UserHandle.getUserHandleForUid(callingUid), |
| 2314 | deletedChannel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2315 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
| 2316 | } |
| 2317 | mListeners.notifyNotificationChannelGroupChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2318 | pkg, UserHandle.getUserHandleForUid(callingUid), groupToDelete, |
| 2319 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2320 | savePolicyFile(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2321 | } |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2325 | public void updateNotificationChannelForPackage(String pkg, int uid, |
| 2326 | NotificationChannel channel) { |
Geoffrey Pitsch | 4dd5006 | 2016-12-06 16:41:22 -0500 | [diff] [blame] | 2327 | enforceSystemOrSystemUI("Caller not system or systemui"); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2328 | Preconditions.checkNotNull(channel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2329 | updateNotificationChannelInt(pkg, uid, channel, false); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2330 | } |
| 2331 | |
| 2332 | @Override |
| 2333 | public ParceledListSlice<NotificationChannel> getNotificationChannelsForPackage(String pkg, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2334 | int uid, boolean includeDeleted) { |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2335 | enforceSystemOrSystemUI("getNotificationChannelsForPackage"); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2336 | return mRankingHelper.getNotificationChannels(pkg, uid, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2340 | public int getNumNotificationChannelsForPackage(String pkg, int uid, |
| 2341 | boolean includeDeleted) { |
| 2342 | enforceSystemOrSystemUI("getNumNotificationChannelsForPackage"); |
| 2343 | return mRankingHelper.getNotificationChannels(pkg, uid, includeDeleted) |
| 2344 | .getList().size(); |
| 2345 | } |
| 2346 | |
| 2347 | @Override |
Julia Reynolds | 17717f5 | 2017-05-09 11:46:06 -0400 | [diff] [blame] | 2348 | public boolean onlyHasDefaultChannel(String pkg, int uid) { |
| 2349 | enforceSystemOrSystemUI("onlyHasDefaultChannel"); |
| 2350 | return mRankingHelper.onlyHasDefaultChannel(pkg, uid); |
| 2351 | } |
| 2352 | |
| 2353 | @Override |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 2354 | public int getDeletedChannelCount(String pkg, int uid) { |
| 2355 | enforceSystemOrSystemUI("getDeletedChannelCount"); |
| 2356 | return mRankingHelper.getDeletedChannelCount(pkg, uid); |
| 2357 | } |
| 2358 | |
| 2359 | @Override |
Julia Reynolds | f2e499d | 2018-03-30 10:36:42 -0400 | [diff] [blame] | 2360 | public int getBlockedChannelCount(String pkg, int uid) { |
| 2361 | enforceSystemOrSystemUI("getBlockedChannelCount"); |
| 2362 | return mRankingHelper.getBlockedChannelCount(pkg, uid); |
| 2363 | } |
| 2364 | |
| 2365 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2366 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroupsForPackage( |
| 2367 | String pkg, int uid, boolean includeDeleted) { |
| 2368 | checkCallerIsSystem(); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2369 | return mRankingHelper.getNotificationChannelGroups(pkg, uid, includeDeleted, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2373 | public NotificationChannelGroup getPopulatedNotificationChannelGroupForPackage( |
| 2374 | String pkg, int uid, String groupId, boolean includeDeleted) { |
| 2375 | enforceSystemOrSystemUI("getPopulatedNotificationChannelGroupForPackage"); |
| 2376 | return mRankingHelper.getNotificationChannelGroupWithChannels( |
| 2377 | pkg, uid, groupId, includeDeleted); |
| 2378 | } |
| 2379 | |
| 2380 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2381 | public NotificationChannelGroup getNotificationChannelGroupForPackage( |
| 2382 | String groupId, String pkg, int uid) { |
| 2383 | enforceSystemOrSystemUI("getNotificationChannelGroupForPackage"); |
| 2384 | return mRankingHelper.getNotificationChannelGroup(groupId, pkg, uid); |
| 2385 | } |
| 2386 | |
| 2387 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2388 | public ParceledListSlice<NotificationChannel> getNotificationChannels(String pkg) { |
| 2389 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2390 | return mRankingHelper.getNotificationChannels( |
| 2391 | pkg, Binder.getCallingUid(), false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2392 | } |
| 2393 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2394 | @Override |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 2395 | public ParceledListSlice<NotifyingApp> getRecentNotifyingAppsForUser(int userId) { |
| 2396 | checkCallerIsSystem(); |
| 2397 | synchronized (mNotificationLock) { |
| 2398 | List<NotifyingApp> apps = new ArrayList<>( |
| 2399 | mRecentApps.getOrDefault(userId, new ArrayList<>())); |
| 2400 | return new ParceledListSlice<>(apps); |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | @Override |
Julia Reynolds | e273f08 | 2018-04-12 13:48:49 -0400 | [diff] [blame] | 2405 | public int getBlockedAppCount(int userId) { |
| 2406 | checkCallerIsSystem(); |
| 2407 | return mRankingHelper.getBlockedAppCount(userId); |
| 2408 | } |
| 2409 | |
| 2410 | @Override |
Beverly | 86d076f | 2018-04-17 14:44:52 -0400 | [diff] [blame] | 2411 | public boolean areChannelsBypassingDnd() { |
| 2412 | return mRankingHelper.areChannelsBypassingDnd(); |
| 2413 | } |
| 2414 | |
| 2415 | @Override |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2416 | public void clearData(String packageName, int uid, boolean fromApp) throws RemoteException { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2417 | checkCallerIsSystem(); |
| 2418 | |
| 2419 | // Cancel posted notifications |
| 2420 | cancelAllNotificationsInt(MY_UID, MY_PID, packageName, null, 0, 0, true, |
| 2421 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, null); |
| 2422 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2423 | final String[] packages = new String[] {packageName}; |
| 2424 | final int[] uids = new int[] {uid}; |
| 2425 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2426 | // Listener & assistant |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2427 | mListeners.onPackagesChanged(true, packages, uids); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2428 | mAssistants.onPackagesChanged(true, packages, uids); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2429 | |
| 2430 | // Zen |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2431 | mConditionProviders.onPackagesChanged(true, packages, uids); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2432 | |
| 2433 | // Reset notification preferences |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2434 | if (!fromApp) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2435 | mRankingHelper.onPackagesChanged( |
| 2436 | true, UserHandle.getCallingUserId(), packages, uids); |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2437 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2438 | |
| 2439 | savePolicyFile(); |
| 2440 | } |
| 2441 | |
| 2442 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2443 | /** |
| 2444 | * System-only API for getting a list of current (i.e. not cleared) notifications. |
| 2445 | * |
| 2446 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2447 | * @returns A list of all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2448 | */ |
| 2449 | @Override |
| 2450 | public StatusBarNotification[] getActiveNotifications(String callingPkg) { |
| 2451 | // enforce() will ensure the calling uid has the correct permission |
| 2452 | getContext().enforceCallingOrSelfPermission( |
| 2453 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 2454 | "NotificationManagerService.getActiveNotifications"); |
| 2455 | |
| 2456 | StatusBarNotification[] tmp = null; |
| 2457 | int uid = Binder.getCallingUid(); |
| 2458 | |
| 2459 | // noteOp will check to make sure the callingPkg matches the uid |
| 2460 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 2461 | == AppOpsManager.MODE_ALLOWED) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2462 | synchronized (mNotificationLock) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2463 | tmp = new StatusBarNotification[mNotificationList.size()]; |
| 2464 | final int N = mNotificationList.size(); |
| 2465 | for (int i=0; i<N; i++) { |
| 2466 | tmp[i] = mNotificationList.get(i).sbn; |
| 2467 | } |
| 2468 | } |
| 2469 | } |
| 2470 | return tmp; |
| 2471 | } |
| 2472 | |
| 2473 | /** |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2474 | * Public API for getting a list of current notifications for the calling package/uid. |
| 2475 | * |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 2476 | * Note that since notification posting is done asynchronously, this will not return |
| 2477 | * notifications that are in the process of being posted. |
| 2478 | * |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2479 | * @returns A list of all the package's notifications, in natural order. |
| 2480 | */ |
| 2481 | @Override |
| 2482 | public ParceledListSlice<StatusBarNotification> getAppActiveNotifications(String pkg, |
| 2483 | int incomingUserId) { |
| 2484 | checkCallerIsSystemOrSameApp(pkg); |
| 2485 | int userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2486 | Binder.getCallingUid(), incomingUserId, true, false, |
| 2487 | "getAppActiveNotifications", pkg); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2488 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2489 | final ArrayMap<String, StatusBarNotification> map |
| 2490 | = new ArrayMap<>(mNotificationList.size() + mEnqueuedNotifications.size()); |
Erik Wolsheimer | 2242b4d | 2015-11-24 13:22:04 -0800 | [diff] [blame] | 2491 | final int N = mNotificationList.size(); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2492 | for (int i = 0; i < N; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2493 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 2494 | mNotificationList.get(i).sbn); |
| 2495 | if (sbn != null) { |
| 2496 | map.put(sbn.getKey(), sbn); |
| 2497 | } |
| 2498 | } |
| 2499 | for(NotificationRecord snoozed: mSnoozeHelper.getSnoozed(userId, pkg)) { |
| 2500 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, snoozed.sbn); |
| 2501 | if (sbn != null) { |
| 2502 | map.put(sbn.getKey(), sbn); |
| 2503 | } |
| 2504 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2505 | final int M = mEnqueuedNotifications.size(); |
| 2506 | for (int i = 0; i < M; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2507 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 2508 | mEnqueuedNotifications.get(i).sbn); |
| 2509 | if (sbn != null) { |
| 2510 | map.put(sbn.getKey(), sbn); // pending update overwrites existing post here |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2511 | } |
| 2512 | } |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2513 | final ArrayList<StatusBarNotification> list = new ArrayList<>(map.size()); |
| 2514 | list.addAll(map.values()); |
| 2515 | return new ParceledListSlice<StatusBarNotification>(list); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2516 | } |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2517 | } |
| 2518 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2519 | private StatusBarNotification sanitizeSbn(String pkg, int userId, |
| 2520 | StatusBarNotification sbn) { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 2521 | if (sbn.getPackageName().equals(pkg) && sbn.getUserId() == userId) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2522 | // We could pass back a cloneLight() but clients might get confused and |
| 2523 | // try to send this thing back to notify() again, which would not work |
| 2524 | // very well. |
| 2525 | return new StatusBarNotification( |
| 2526 | sbn.getPackageName(), |
| 2527 | sbn.getOpPkg(), |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2528 | sbn.getId(), sbn.getTag(), sbn.getUid(), sbn.getInitialPid(), |
| 2529 | sbn.getNotification().clone(), |
| 2530 | sbn.getUser(), sbn.getOverrideGroupKey(), sbn.getPostTime()); |
| 2531 | } |
| 2532 | return null; |
| 2533 | } |
| 2534 | |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2535 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2536 | * System-only API for getting a list of recent (cleared, no longer shown) notifications. |
| 2537 | * |
| 2538 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
| 2539 | */ |
| 2540 | @Override |
| 2541 | public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) { |
| 2542 | // enforce() will ensure the calling uid has the correct permission |
| 2543 | getContext().enforceCallingOrSelfPermission( |
| 2544 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 2545 | "NotificationManagerService.getHistoricalNotifications"); |
| 2546 | |
| 2547 | StatusBarNotification[] tmp = null; |
| 2548 | int uid = Binder.getCallingUid(); |
| 2549 | |
| 2550 | // noteOp will check to make sure the callingPkg matches the uid |
| 2551 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 2552 | == AppOpsManager.MODE_ALLOWED) { |
| 2553 | synchronized (mArchive) { |
| 2554 | tmp = mArchive.getArray(count); |
| 2555 | } |
| 2556 | } |
| 2557 | return tmp; |
| 2558 | } |
| 2559 | |
| 2560 | /** |
| 2561 | * Register a listener binder directly with the notification manager. |
| 2562 | * |
| 2563 | * Only works with system callers. Apps should extend |
| 2564 | * {@link android.service.notification.NotificationListenerService}. |
| 2565 | */ |
| 2566 | @Override |
| 2567 | public void registerListener(final INotificationListener listener, |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 2568 | final ComponentName component, final int userid) { |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 2569 | enforceSystemOrSystemUI("INotificationManager.registerListener"); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 2570 | mListeners.registerService(listener, component, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2571 | } |
| 2572 | |
| 2573 | /** |
| 2574 | * Remove a listener binder directly |
| 2575 | */ |
| 2576 | @Override |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 2577 | public void unregisterListener(INotificationListener token, int userid) { |
Chris Wren | b7c8109 | 2016-03-10 11:41:10 -0500 | [diff] [blame] | 2578 | mListeners.unregisterService(token, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | /** |
| 2582 | * Allow an INotificationListener to simulate a "clear all" operation. |
| 2583 | * |
| 2584 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications} |
| 2585 | * |
| 2586 | * @param token The binder for the listener, to check that the caller is allowed |
| 2587 | */ |
| 2588 | @Override |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2589 | public void cancelNotificationsFromListener(INotificationListener token, String[] keys) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2590 | final int callingUid = Binder.getCallingUid(); |
| 2591 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2592 | long identity = Binder.clearCallingIdentity(); |
| 2593 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2594 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2595 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 2596 | |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2597 | if (keys != null) { |
| 2598 | final int N = keys.length; |
| 2599 | for (int i = 0; i < N; i++) { |
| 2600 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 2601 | if (r == null) continue; |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2602 | final int userId = r.sbn.getUserId(); |
| 2603 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 2604 | !mUserProfiles.isCurrentProfile(userId)) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2605 | throw new SecurityException("Disallowed call from listener: " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2606 | + info.service); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2607 | } |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 2608 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2609 | r.sbn.getPackageName(), r.sbn.getTag(), r.sbn.getId(), |
| 2610 | userId); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2611 | } |
| 2612 | } else { |
| 2613 | cancelAllLocked(callingUid, callingPid, info.userid, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2614 | REASON_LISTENER_CANCEL_ALL, info, info.supportsProfiles()); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2615 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2616 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2617 | } finally { |
| 2618 | Binder.restoreCallingIdentity(identity); |
| 2619 | } |
| 2620 | } |
| 2621 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2622 | /** |
| 2623 | * Handle request from an approved listener to re-enable itself. |
| 2624 | * |
| 2625 | * @param component The componenet to be re-enabled, caller must match package. |
| 2626 | */ |
| 2627 | @Override |
| 2628 | public void requestBindListener(ComponentName component) { |
| 2629 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 2630 | long identity = Binder.clearCallingIdentity(); |
| 2631 | try { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2632 | ManagedServices manager = |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2633 | mAssistants.isComponentEnabledForCurrentProfiles(component) |
| 2634 | ? mAssistants |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2635 | : mListeners; |
| 2636 | manager.setComponentState(component, true); |
| 2637 | } finally { |
| 2638 | Binder.restoreCallingIdentity(identity); |
| 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | @Override |
| 2643 | public void requestUnbindListener(INotificationListener token) { |
| 2644 | long identity = Binder.clearCallingIdentity(); |
| 2645 | try { |
| 2646 | // allow bound services to disable themselves |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2647 | synchronized (mNotificationLock) { |
| 2648 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2649 | info.getOwner().setComponentState(info.component, false); |
| 2650 | } |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2651 | } finally { |
| 2652 | Binder.restoreCallingIdentity(identity); |
| 2653 | } |
| 2654 | } |
| 2655 | |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2656 | @Override |
| 2657 | public void setNotificationsShownFromListener(INotificationListener token, String[] keys) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2658 | long identity = Binder.clearCallingIdentity(); |
| 2659 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2660 | synchronized (mNotificationLock) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2661 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2662 | if (keys != null) { |
| 2663 | final int N = keys.length; |
| 2664 | for (int i = 0; i < N; i++) { |
| 2665 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
| 2666 | if (r == null) continue; |
| 2667 | final int userId = r.sbn.getUserId(); |
| 2668 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
| 2669 | !mUserProfiles.isCurrentProfile(userId)) { |
| 2670 | throw new SecurityException("Disallowed call from listener: " |
| 2671 | + info.service); |
| 2672 | } |
| 2673 | if (!r.isSeen()) { |
| 2674 | if (DBG) Slog.d(TAG, "Marking notification as seen " + keys[i]); |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 2675 | reportSeen(r); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2676 | r.setSeen(); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 2677 | maybeRecordInterruptionLocked(r); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2678 | } |
| 2679 | } |
| 2680 | } |
| 2681 | } |
| 2682 | } finally { |
| 2683 | Binder.restoreCallingIdentity(identity); |
| 2684 | } |
| 2685 | } |
| 2686 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2687 | /** |
| 2688 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2689 | * |
| 2690 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2691 | * |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2692 | * @param info The binder for the listener, to check that the caller is allowed |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2693 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2694 | @GuardedBy("mNotificationLock") |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2695 | private void cancelNotificationFromListenerLocked(ManagedServiceInfo info, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2696 | int callingUid, int callingPid, String pkg, String tag, int id, int userId) { |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2697 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 2698 | Notification.FLAG_ONGOING_EVENT | FLAG_FOREGROUND_SERVICE, |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2699 | true, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2700 | userId, REASON_LISTENER_CANCEL, info); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2701 | } |
| 2702 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2703 | /** |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2704 | * Allow an INotificationListener to snooze a single notification until a context. |
| 2705 | * |
| 2706 | * @param token The binder for the listener, to check that the caller is allowed |
| 2707 | */ |
| 2708 | @Override |
| 2709 | public void snoozeNotificationUntilContextFromListener(INotificationListener token, |
| 2710 | String key, String snoozeCriterionId) { |
| 2711 | long identity = Binder.clearCallingIdentity(); |
| 2712 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2713 | synchronized (mNotificationLock) { |
| 2714 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2715 | snoozeNotificationInt(key, SNOOZE_UNTIL_UNSPECIFIED, snoozeCriterionId, info); |
| 2716 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2717 | } finally { |
| 2718 | Binder.restoreCallingIdentity(identity); |
| 2719 | } |
| 2720 | } |
| 2721 | |
| 2722 | /** |
| 2723 | * Allow an INotificationListener to snooze a single notification until a time. |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2724 | * |
| 2725 | * @param token The binder for the listener, to check that the caller is allowed |
| 2726 | */ |
| 2727 | @Override |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2728 | public void snoozeNotificationUntilFromListener(INotificationListener token, String key, |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 2729 | long duration) { |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2730 | long identity = Binder.clearCallingIdentity(); |
| 2731 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2732 | synchronized (mNotificationLock) { |
| 2733 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2734 | snoozeNotificationInt(key, duration, null, info); |
| 2735 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2736 | } finally { |
| 2737 | Binder.restoreCallingIdentity(identity); |
| 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | /** |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2742 | * Allows the notification assistant to un-snooze a single notification. |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2743 | * |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2744 | * @param token The binder for the assistant, to check that the caller is allowed |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2745 | */ |
| 2746 | @Override |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2747 | public void unsnoozeNotificationFromAssistant(INotificationListener token, String key) { |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2748 | long identity = Binder.clearCallingIdentity(); |
| 2749 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2750 | synchronized (mNotificationLock) { |
| 2751 | final ManagedServiceInfo info = |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2752 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2753 | unsnoozeNotificationInt(key, info); |
| 2754 | } |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2755 | } finally { |
| 2756 | Binder.restoreCallingIdentity(identity); |
| 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2761 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2762 | * |
| 2763 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2764 | * |
| 2765 | * @param token The binder for the listener, to check that the caller is allowed |
| 2766 | */ |
| 2767 | @Override |
| 2768 | public void cancelNotificationFromListener(INotificationListener token, String pkg, |
| 2769 | String tag, int id) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2770 | final int callingUid = Binder.getCallingUid(); |
| 2771 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2772 | long identity = Binder.clearCallingIdentity(); |
| 2773 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2774 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2775 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2776 | if (info.supportsProfiles()) { |
| 2777 | Log.e(TAG, "Ignoring deprecated cancelNotification(pkg, tag, id) " |
| 2778 | + "from " + info.component |
| 2779 | + " use cancelNotification(key) instead."); |
| 2780 | } else { |
| 2781 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2782 | pkg, tag, id, info.userid); |
| 2783 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2784 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2785 | } finally { |
| 2786 | Binder.restoreCallingIdentity(identity); |
| 2787 | } |
| 2788 | } |
| 2789 | |
| 2790 | /** |
| 2791 | * Allow an INotificationListener to request the list of outstanding notifications seen by |
| 2792 | * the current user. Useful when starting up, after which point the listener callbacks |
| 2793 | * should be used. |
| 2794 | * |
| 2795 | * @param token The binder for the listener, to check that the caller is allowed |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2796 | * @param keys An array of notification keys to fetch, or null to fetch everything |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2797 | * @returns The return value will contain the notifications specified in keys, in that |
| 2798 | * order, or if keys is null, all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2799 | */ |
| 2800 | @Override |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2801 | public ParceledListSlice<StatusBarNotification> getActiveNotificationsFromListener( |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2802 | INotificationListener token, String[] keys, int trim) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2803 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2804 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2805 | final boolean getKeys = keys != null; |
| 2806 | final int N = getKeys ? keys.length : mNotificationList.size(); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2807 | final ArrayList<StatusBarNotification> list |
| 2808 | = new ArrayList<StatusBarNotification>(N); |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2809 | for (int i=0; i<N; i++) { |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2810 | final NotificationRecord r = getKeys |
| 2811 | ? mNotificationsByKey.get(keys[i]) |
| 2812 | : mNotificationList.get(i); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2813 | if (r == null) continue; |
| 2814 | StatusBarNotification sbn = r.sbn; |
| 2815 | if (!isVisibleToListener(sbn, info)) continue; |
| 2816 | StatusBarNotification sbnToSend = |
| 2817 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2818 | list.add(sbnToSend); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2819 | } |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2820 | return new ParceledListSlice<StatusBarNotification>(list); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2821 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2822 | } |
| 2823 | |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2824 | /** |
| 2825 | * Allow an INotificationListener to request the list of outstanding snoozed notifications |
| 2826 | * seen by the current user. Useful when starting up, after which point the listener |
| 2827 | * callbacks should be used. |
| 2828 | * |
| 2829 | * @param token The binder for the listener, to check that the caller is allowed |
| 2830 | * @returns The return value will contain the notifications specified in keys, in that |
| 2831 | * order, or if keys is null, all the notifications, in natural order. |
| 2832 | */ |
| 2833 | @Override |
| 2834 | public ParceledListSlice<StatusBarNotification> getSnoozedNotificationsFromListener( |
| 2835 | INotificationListener token, int trim) { |
| 2836 | synchronized (mNotificationLock) { |
| 2837 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2838 | List<NotificationRecord> snoozedRecords = mSnoozeHelper.getSnoozed(); |
| 2839 | final int N = snoozedRecords.size(); |
| 2840 | final ArrayList<StatusBarNotification> list = new ArrayList<>(N); |
| 2841 | for (int i=0; i < N; i++) { |
| 2842 | final NotificationRecord r = snoozedRecords.get(i); |
| 2843 | if (r == null) continue; |
| 2844 | StatusBarNotification sbn = r.sbn; |
| 2845 | if (!isVisibleToListener(sbn, info)) continue; |
| 2846 | StatusBarNotification sbnToSend = |
| 2847 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2848 | list.add(sbnToSend); |
| 2849 | } |
| 2850 | return new ParceledListSlice<>(list); |
| 2851 | } |
| 2852 | } |
| 2853 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2854 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2855 | public void requestHintsFromListener(INotificationListener token, int hints) { |
| 2856 | final long identity = Binder.clearCallingIdentity(); |
| 2857 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2858 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2859 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2860 | final int disableEffectsMask = HINT_HOST_DISABLE_EFFECTS |
| 2861 | | HINT_HOST_DISABLE_NOTIFICATION_EFFECTS |
| 2862 | | HINT_HOST_DISABLE_CALL_EFFECTS; |
| 2863 | final boolean disableEffects = (hints & disableEffectsMask) != 0; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2864 | if (disableEffects) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2865 | addDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2866 | } else { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2867 | removeDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2868 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2869 | updateListenerHintsLocked(); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2870 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2871 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2872 | } finally { |
| 2873 | Binder.restoreCallingIdentity(identity); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2878 | public int getHintsFromListener(INotificationListener token) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2879 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2880 | return mListenerHints; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2881 | } |
| 2882 | } |
| 2883 | |
| 2884 | @Override |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2885 | public void requestInterruptionFilterFromListener(INotificationListener token, |
| 2886 | int interruptionFilter) throws RemoteException { |
| 2887 | final long identity = Binder.clearCallingIdentity(); |
| 2888 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2889 | synchronized (mNotificationLock) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2890 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2891 | mZenModeHelper.requestFromListener(info.component, interruptionFilter); |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2892 | updateInterruptionFilterLocked(); |
| 2893 | } |
| 2894 | } finally { |
| 2895 | Binder.restoreCallingIdentity(identity); |
| 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | @Override |
| 2900 | public int getInterruptionFilterFromListener(INotificationListener token) |
| 2901 | throws RemoteException { |
| 2902 | synchronized (mNotificationLight) { |
| 2903 | return mInterruptionFilter; |
| 2904 | } |
| 2905 | } |
| 2906 | |
| 2907 | @Override |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2908 | public void setOnNotificationPostedTrimFromListener(INotificationListener token, int trim) |
| 2909 | throws RemoteException { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2910 | synchronized (mNotificationLock) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2911 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2912 | if (info == null) return; |
| 2913 | mListeners.setOnNotificationPostedTrimLocked(info, trim); |
| 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2918 | public int getZenMode() { |
| 2919 | return mZenModeHelper.getZenMode(); |
| 2920 | } |
| 2921 | |
| 2922 | @Override |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 2923 | public ZenModeConfig getZenModeConfig() { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2924 | enforceSystemOrSystemUI("INotificationManager.getZenModeConfig"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 2925 | return mZenModeHelper.getConfig(); |
| 2926 | } |
| 2927 | |
| 2928 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2929 | public void setZenMode(int mode, Uri conditionId, String reason) throws RemoteException { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2930 | enforceSystemOrSystemUI("INotificationManager.setZenMode"); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2931 | final long identity = Binder.clearCallingIdentity(); |
| 2932 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 2933 | mZenModeHelper.setManualZenMode(mode, conditionId, null, reason); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2934 | } finally { |
| 2935 | Binder.restoreCallingIdentity(identity); |
| 2936 | } |
| 2937 | } |
| 2938 | |
| 2939 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2940 | public List<ZenModeConfig.ZenRule> getZenRules() throws RemoteException { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2941 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRules"); |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2942 | return mZenModeHelper.getZenRules(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2946 | public AutomaticZenRule getAutomaticZenRule(String id) throws RemoteException { |
| 2947 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2948 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRule"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2949 | return mZenModeHelper.getAutomaticZenRule(id); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2953 | public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2954 | throws RemoteException { |
| 2955 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 2956 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 2957 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 2958 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2959 | enforcePolicyAccess(Binder.getCallingUid(), "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2960 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2961 | return mZenModeHelper.addAutomaticZenRule(automaticZenRule, |
| 2962 | "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2966 | public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2967 | throws RemoteException { |
| 2968 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 2969 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 2970 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 2971 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
| 2972 | enforcePolicyAccess(Binder.getCallingUid(), "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2973 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2974 | return mZenModeHelper.updateAutomaticZenRule(id, automaticZenRule, |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2975 | "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2979 | public boolean removeAutomaticZenRule(String id) throws RemoteException { |
| 2980 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2981 | // Verify that they can modify zen rules. |
| 2982 | enforcePolicyAccess(Binder.getCallingUid(), "removeAutomaticZenRule"); |
| 2983 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2984 | return mZenModeHelper.removeAutomaticZenRule(id, "removeAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | @Override |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 2988 | public boolean removeAutomaticZenRules(String packageName) throws RemoteException { |
| 2989 | Preconditions.checkNotNull(packageName, "Package name is null"); |
| 2990 | enforceSystemOrSystemUI("removeAutomaticZenRules"); |
| 2991 | |
| 2992 | return mZenModeHelper.removeAutomaticZenRules(packageName, "removeAutomaticZenRules"); |
| 2993 | } |
| 2994 | |
| 2995 | @Override |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 2996 | public int getRuleInstanceCount(ComponentName owner) throws RemoteException { |
| 2997 | Preconditions.checkNotNull(owner, "Owner is null"); |
| 2998 | enforceSystemOrSystemUI("getRuleInstanceCount"); |
| 2999 | |
| 3000 | return mZenModeHelper.getCurrentInstanceCount(owner); |
| 3001 | } |
| 3002 | |
| 3003 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3004 | public void setInterruptionFilter(String pkg, int filter) throws RemoteException { |
| 3005 | enforcePolicyAccess(pkg, "setInterruptionFilter"); |
| 3006 | final int zen = NotificationManager.zenModeFromInterruptionFilter(filter, -1); |
| 3007 | if (zen == -1) throw new IllegalArgumentException("Invalid filter: " + filter); |
| 3008 | final long identity = Binder.clearCallingIdentity(); |
| 3009 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 3010 | mZenModeHelper.setManualZenMode(zen, null, pkg, "setInterruptionFilter"); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3011 | } finally { |
| 3012 | Binder.restoreCallingIdentity(identity); |
| 3013 | } |
| 3014 | } |
| 3015 | |
| 3016 | @Override |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 3017 | public void notifyConditions(final String pkg, IConditionProvider provider, |
| 3018 | final Condition[] conditions) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3019 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 3020 | checkCallerIsSystemOrSameApp(pkg); |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 3021 | mHandler.post(new Runnable() { |
| 3022 | @Override |
| 3023 | public void run() { |
| 3024 | mConditionProviders.notifyConditions(pkg, info, conditions); |
| 3025 | } |
| 3026 | }); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3027 | } |
| 3028 | |
Julia Reynolds | 38e6ca4 | 2016-08-08 08:38:09 -0400 | [diff] [blame] | 3029 | @Override |
| 3030 | public void requestUnbindProvider(IConditionProvider provider) { |
| 3031 | long identity = Binder.clearCallingIdentity(); |
| 3032 | try { |
| 3033 | // allow bound services to disable themselves |
| 3034 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 3035 | info.getOwner().setComponentState(info.component, false); |
| 3036 | } finally { |
| 3037 | Binder.restoreCallingIdentity(identity); |
| 3038 | } |
| 3039 | } |
| 3040 | |
| 3041 | @Override |
| 3042 | public void requestBindProvider(ComponentName component) { |
| 3043 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 3044 | long identity = Binder.clearCallingIdentity(); |
| 3045 | try { |
| 3046 | mConditionProviders.setComponentState(component, true); |
| 3047 | } finally { |
| 3048 | Binder.restoreCallingIdentity(identity); |
| 3049 | } |
| 3050 | } |
| 3051 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3052 | private void enforceSystemOrSystemUI(String message) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3053 | if (isCallerSystemOrPhone()) return; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3054 | getContext().enforceCallingPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 3055 | message); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3056 | } |
| 3057 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 3058 | private void enforceSystemOrSystemUIOrSamePackage(String pkg, String message) { |
| 3059 | try { |
| 3060 | checkCallerIsSystemOrSameApp(pkg); |
| 3061 | } catch (SecurityException e) { |
| 3062 | getContext().enforceCallingPermission( |
| 3063 | android.Manifest.permission.STATUS_BAR_SERVICE, |
| 3064 | message); |
| 3065 | } |
| 3066 | } |
| 3067 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3068 | private void enforcePolicyAccess(int uid, String method) { |
| 3069 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 3070 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 3071 | return; |
| 3072 | } |
| 3073 | boolean accessAllowed = false; |
| 3074 | String[] packages = getContext().getPackageManager().getPackagesForUid(uid); |
| 3075 | final int packageCount = packages.length; |
| 3076 | for (int i = 0; i < packageCount; i++) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3077 | if (mConditionProviders.isPackageOrComponentAllowed( |
| 3078 | packages[i], UserHandle.getUserId(uid))) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3079 | accessAllowed = true; |
| 3080 | } |
| 3081 | } |
| 3082 | if (!accessAllowed) { |
| 3083 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 3084 | throw new SecurityException("Notification policy access denied"); |
| 3085 | } |
| 3086 | } |
| 3087 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3088 | private void enforcePolicyAccess(String pkg, String method) { |
Julia Reynolds | 6ee2617 | 2015-09-28 11:34:48 -0400 | [diff] [blame] | 3089 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 3090 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 3091 | return; |
| 3092 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 3093 | checkCallerIsSameApp(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3094 | if (!checkPolicyAccess(pkg)) { |
| 3095 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 3096 | throw new SecurityException("Notification policy access denied"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3097 | } |
| 3098 | } |
| 3099 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3100 | private boolean checkPackagePolicyAccess(String pkg) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3101 | return mConditionProviders.isPackageOrComponentAllowed( |
| 3102 | pkg, getCallingUserHandle().getIdentifier()); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | private boolean checkPolicyAccess(String pkg) { |
Julia Reynolds | 0867b3a | 2016-03-30 17:29:54 -0400 | [diff] [blame] | 3106 | try { |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 3107 | int uid = getContext().getPackageManager().getPackageUidAsUser(pkg, |
| 3108 | UserHandle.getCallingUserId()); |
Julia Reynolds | 0867b3a | 2016-03-30 17:29:54 -0400 | [diff] [blame] | 3109 | if (PackageManager.PERMISSION_GRANTED == ActivityManager.checkComponentPermission( |
| 3110 | android.Manifest.permission.MANAGE_NOTIFICATIONS, uid, |
| 3111 | -1, true)) { |
| 3112 | return true; |
| 3113 | } |
| 3114 | } catch (NameNotFoundException e) { |
| 3115 | return false; |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 3116 | } |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 3117 | return checkPackagePolicyAccess(pkg) |
| 3118 | || mListeners.isComponentEnabledForPackage(pkg) |
| 3119 | || (mDpm != null && |
| 3120 | mDpm.isActiveAdminWithPolicy(Binder.getCallingUid(), |
| 3121 | DeviceAdminInfo.USES_POLICY_PROFILE_OWNER)); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3122 | } |
| 3123 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3124 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3125 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 3126 | if (!DumpUtils.checkDumpAndUsageStatsPermission(getContext(), TAG, pw)) return; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3127 | final DumpFilter filter = DumpFilter.parseFromArguments(args); |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3128 | if (filter.stats) { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3129 | dumpJson(pw, filter); |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3130 | } else if (filter.proto) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3131 | dumpProto(fd, filter); |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 3132 | } else if (filter.criticalPriority) { |
| 3133 | dumpNotificationRecords(pw, filter); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3134 | } else { |
| 3135 | dumpImpl(pw, filter); |
| 3136 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3137 | } |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 3138 | |
| 3139 | @Override |
| 3140 | public ComponentName getEffectsSuppressor() { |
Bryce Lee | ba3d895 | 2016-04-12 12:39:15 -0700 | [diff] [blame] | 3141 | return !mEffectsSuppressors.isEmpty() ? mEffectsSuppressors.get(0) : null; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 3142 | } |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 3143 | |
| 3144 | @Override |
| 3145 | public boolean matchesCallFilter(Bundle extras) { |
| 3146 | enforceSystemOrSystemUI("INotificationManager.matchesCallFilter"); |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 3147 | return mZenModeHelper.matchesCallFilter( |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3148 | Binder.getCallingUserHandle(), |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 3149 | extras, |
| 3150 | mRankingHelper.findExtractor(ValidateNotificationPeople.class), |
| 3151 | MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS, |
| 3152 | MATCHES_CALL_FILTER_TIMEOUT_AFFINITY); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 3153 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 3154 | |
| 3155 | @Override |
| 3156 | public boolean isSystemConditionProviderEnabled(String path) { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 3157 | enforceSystemOrSystemUI("INotificationManager.isSystemConditionProviderEnabled"); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 3158 | return mConditionProviders.isSystemProviderEnabled(path); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 3159 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3160 | |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 3161 | // Backup/restore interface |
| 3162 | @Override |
| 3163 | public byte[] getBackupPayload(int user) { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3164 | checkCallerIsSystem(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3165 | if (DBG) Slog.d(TAG, "getBackupPayload u=" + user); |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 3166 | //TODO: http://b/22388012 |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3167 | if (user != USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3168 | Slog.w(TAG, "getBackupPayload: cannot backup policy for user " + user); |
| 3169 | return null; |
| 3170 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 3171 | synchronized(mPolicyFile) { |
| 3172 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 3173 | try { |
| 3174 | writePolicyXml(baos, true /*forBackup*/); |
| 3175 | return baos.toByteArray(); |
| 3176 | } catch (IOException e) { |
| 3177 | Slog.w(TAG, "getBackupPayload: error writing payload for user " + user, e); |
| 3178 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3179 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 3180 | return null; |
| 3181 | } |
| 3182 | |
| 3183 | @Override |
| 3184 | public void applyRestore(byte[] payload, int user) { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3185 | checkCallerIsSystem(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3186 | if (DBG) Slog.d(TAG, "applyRestore u=" + user + " payload=" |
| 3187 | + (payload != null ? new String(payload, StandardCharsets.UTF_8) : null)); |
| 3188 | if (payload == null) { |
| 3189 | Slog.w(TAG, "applyRestore: no payload to restore for user " + user); |
| 3190 | return; |
| 3191 | } |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 3192 | //TODO: http://b/22388012 |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3193 | if (user != USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3194 | Slog.w(TAG, "applyRestore: cannot restore policy for user " + user); |
| 3195 | return; |
| 3196 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 3197 | synchronized(mPolicyFile) { |
| 3198 | final ByteArrayInputStream bais = new ByteArrayInputStream(payload); |
| 3199 | try { |
| 3200 | readPolicyXml(bais, true /*forRestore*/); |
| 3201 | savePolicyFile(); |
| 3202 | } catch (NumberFormatException | XmlPullParserException | IOException e) { |
| 3203 | Slog.w(TAG, "applyRestore: error reading payload", e); |
| 3204 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3205 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 3206 | } |
| 3207 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3208 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3209 | public boolean isNotificationPolicyAccessGranted(String pkg) { |
| 3210 | return checkPolicyAccess(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | @Override |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 3214 | public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {; |
| 3215 | enforceSystemOrSystemUIOrSamePackage(pkg, |
| 3216 | "request policy access status for another package"); |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 3217 | return checkPolicyAccess(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3221 | public void setNotificationPolicyAccessGranted(String pkg, boolean granted) |
| 3222 | throws RemoteException { |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3223 | setNotificationPolicyAccessGrantedForUser( |
| 3224 | pkg, getCallingUserHandle().getIdentifier(), granted); |
| 3225 | } |
| 3226 | |
| 3227 | @Override |
| 3228 | public void setNotificationPolicyAccessGrantedForUser( |
| 3229 | String pkg, int userId, boolean granted) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3230 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3231 | final long identity = Binder.clearCallingIdentity(); |
| 3232 | try { |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3233 | if (mAllowedManagedServicePackages.test(pkg)) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3234 | mConditionProviders.setPackageOrComponentEnabled( |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3235 | pkg, userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3236 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3237 | getContext().sendBroadcastAsUser(new Intent( |
| 3238 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 3239 | .setPackage(pkg) |
| 3240 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3241 | UserHandle.of(userId), null); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3242 | savePolicyFile(); |
| 3243 | } |
| 3244 | } finally { |
| 3245 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3246 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | @Override |
| 3250 | public Policy getNotificationPolicy(String pkg) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3251 | final long identity = Binder.clearCallingIdentity(); |
| 3252 | try { |
| 3253 | return mZenModeHelper.getNotificationPolicy(); |
| 3254 | } finally { |
| 3255 | Binder.restoreCallingIdentity(identity); |
| 3256 | } |
| 3257 | } |
| 3258 | |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3259 | /** |
| 3260 | * Sets the notification policy. Apps that target API levels below |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3261 | * {@link android.os.Build.VERSION_CODES#P} cannot change user-designated values to |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3262 | * allow or disallow {@link Policy#PRIORITY_CATEGORY_ALARMS}, |
| 3263 | * {@link Policy#PRIORITY_CATEGORY_SYSTEM} and |
| 3264 | * {@link Policy#PRIORITY_CATEGORY_MEDIA} from bypassing dnd |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3265 | */ |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3266 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3267 | public void setNotificationPolicy(String pkg, Policy policy) { |
| 3268 | enforcePolicyAccess(pkg, "setNotificationPolicy"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3269 | final long identity = Binder.clearCallingIdentity(); |
| 3270 | try { |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3271 | final ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(pkg, |
| 3272 | 0, UserHandle.getUserId(MY_UID)); |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3273 | Policy currPolicy = mZenModeHelper.getNotificationPolicy(); |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3274 | |
Jeff Sharkey | aa1a911 | 2018-04-10 15:18:12 -0600 | [diff] [blame] | 3275 | if (applicationInfo.targetSdkVersion < Build.VERSION_CODES.P) { |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3276 | int priorityCategories = policy.priorityCategories; |
| 3277 | // ignore alarm and media values from new policy |
| 3278 | priorityCategories &= ~Policy.PRIORITY_CATEGORY_ALARMS; |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3279 | priorityCategories &= ~Policy.PRIORITY_CATEGORY_MEDIA; |
| 3280 | priorityCategories &= ~Policy.PRIORITY_CATEGORY_SYSTEM; |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3281 | // use user-designated values |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3282 | priorityCategories |= currPolicy.priorityCategories |
| 3283 | & Policy.PRIORITY_CATEGORY_ALARMS; |
| 3284 | priorityCategories |= currPolicy.priorityCategories |
| 3285 | & Policy.PRIORITY_CATEGORY_MEDIA; |
| 3286 | priorityCategories |= currPolicy.priorityCategories |
| 3287 | & Policy.PRIORITY_CATEGORY_SYSTEM; |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3288 | |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3289 | policy = new Policy(priorityCategories, |
| 3290 | policy.priorityCallSenders, policy.priorityMessageSenders, |
| 3291 | policy.suppressedVisualEffects); |
| 3292 | } |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3293 | int newVisualEffects = calculateSuppressedVisualEffects( |
| 3294 | policy, currPolicy, applicationInfo.targetSdkVersion); |
| 3295 | policy = new Policy(policy.priorityCategories, |
| 3296 | policy.priorityCallSenders, policy.priorityMessageSenders, |
| 3297 | newVisualEffects); |
Beverly | 5e07322 | 2018-03-08 10:36:25 -0500 | [diff] [blame] | 3298 | ZenLog.traceSetNotificationPolicy(pkg, applicationInfo.targetSdkVersion, policy); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3299 | mZenModeHelper.setNotificationPolicy(policy); |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3300 | } catch (RemoteException e) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3301 | } finally { |
| 3302 | Binder.restoreCallingIdentity(identity); |
| 3303 | } |
| 3304 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3305 | |
| 3306 | @Override |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3307 | public List<String> getEnabledNotificationListenerPackages() { |
| 3308 | checkCallerIsSystem(); |
| 3309 | return mListeners.getAllowedPackages(getCallingUserHandle().getIdentifier()); |
| 3310 | } |
| 3311 | |
| 3312 | @Override |
| 3313 | public List<ComponentName> getEnabledNotificationListeners(int userId) { |
| 3314 | checkCallerIsSystem(); |
| 3315 | return mListeners.getAllowedComponents(userId); |
| 3316 | } |
| 3317 | |
| 3318 | @Override |
| 3319 | public boolean isNotificationListenerAccessGranted(ComponentName listener) { |
| 3320 | Preconditions.checkNotNull(listener); |
| 3321 | checkCallerIsSystemOrSameApp(listener.getPackageName()); |
| 3322 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 3323 | getCallingUserHandle().getIdentifier()); |
| 3324 | } |
| 3325 | |
| 3326 | @Override |
| 3327 | public boolean isNotificationListenerAccessGrantedForUser(ComponentName listener, |
| 3328 | int userId) { |
| 3329 | Preconditions.checkNotNull(listener); |
| 3330 | checkCallerIsSystem(); |
| 3331 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 3332 | userId); |
| 3333 | } |
| 3334 | |
| 3335 | @Override |
| 3336 | public boolean isNotificationAssistantAccessGranted(ComponentName assistant) { |
| 3337 | Preconditions.checkNotNull(assistant); |
| 3338 | checkCallerIsSystemOrSameApp(assistant.getPackageName()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3339 | return mAssistants.isPackageOrComponentAllowed(assistant.flattenToString(), |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3340 | getCallingUserHandle().getIdentifier()); |
| 3341 | } |
| 3342 | |
| 3343 | @Override |
| 3344 | public void setNotificationListenerAccessGranted(ComponentName listener, |
| 3345 | boolean granted) throws RemoteException { |
| 3346 | setNotificationListenerAccessGrantedForUser( |
| 3347 | listener, getCallingUserHandle().getIdentifier(), granted); |
| 3348 | } |
| 3349 | |
| 3350 | @Override |
| 3351 | public void setNotificationAssistantAccessGranted(ComponentName assistant, |
| 3352 | boolean granted) throws RemoteException { |
| 3353 | setNotificationAssistantAccessGrantedForUser( |
| 3354 | assistant, getCallingUserHandle().getIdentifier(), granted); |
| 3355 | } |
| 3356 | |
| 3357 | @Override |
| 3358 | public void setNotificationListenerAccessGrantedForUser(ComponentName listener, int userId, |
| 3359 | boolean granted) throws RemoteException { |
| 3360 | Preconditions.checkNotNull(listener); |
Julia Reynolds | 0d21764 | 2017-08-11 11:26:04 -0400 | [diff] [blame] | 3361 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3362 | final long identity = Binder.clearCallingIdentity(); |
| 3363 | try { |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3364 | if (mAllowedManagedServicePackages.test(listener.getPackageName())) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3365 | mConditionProviders.setPackageOrComponentEnabled(listener.flattenToString(), |
| 3366 | userId, false, granted); |
| 3367 | mListeners.setPackageOrComponentEnabled(listener.flattenToString(), |
| 3368 | userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3369 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3370 | getContext().sendBroadcastAsUser(new Intent( |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3371 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3372 | .setPackage(listener.getPackageName()) |
| 3373 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3374 | UserHandle.of(userId), null); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3375 | |
| 3376 | savePolicyFile(); |
| 3377 | } |
| 3378 | } finally { |
| 3379 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3380 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3381 | } |
| 3382 | |
| 3383 | @Override |
| 3384 | public void setNotificationAssistantAccessGrantedForUser(ComponentName assistant, |
| 3385 | int userId, boolean granted) throws RemoteException { |
| 3386 | Preconditions.checkNotNull(assistant); |
Julia Reynolds | 0d21764 | 2017-08-11 11:26:04 -0400 | [diff] [blame] | 3387 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3388 | final long identity = Binder.clearCallingIdentity(); |
| 3389 | try { |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3390 | if (mAllowedManagedServicePackages.test(assistant.getPackageName())) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3391 | mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(), |
| 3392 | userId, false, granted); |
| 3393 | mAssistants.setPackageOrComponentEnabled(assistant.flattenToString(), |
| 3394 | userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3395 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3396 | getContext().sendBroadcastAsUser(new Intent( |
| 3397 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 3398 | .setPackage(assistant.getPackageName()) |
| 3399 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3400 | UserHandle.of(userId), null); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3401 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3402 | savePolicyFile(); |
| 3403 | } |
| 3404 | } finally { |
| 3405 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3406 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | @Override |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3410 | public void applyEnqueuedAdjustmentFromAssistant(INotificationListener token, |
| 3411 | Adjustment adjustment) throws RemoteException { |
| 3412 | final long identity = Binder.clearCallingIdentity(); |
| 3413 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3414 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3415 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3416 | int N = mEnqueuedNotifications.size(); |
| 3417 | for (int i = 0; i < N; i++) { |
| 3418 | final NotificationRecord n = mEnqueuedNotifications.get(i); |
| 3419 | if (Objects.equals(adjustment.getKey(), n.getKey()) |
| 3420 | && Objects.equals(adjustment.getUser(), n.getUserId())) { |
| 3421 | applyAdjustment(n, adjustment); |
| 3422 | break; |
| 3423 | } |
| 3424 | } |
| 3425 | } |
| 3426 | } finally { |
| 3427 | Binder.restoreCallingIdentity(identity); |
| 3428 | } |
| 3429 | } |
| 3430 | |
| 3431 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 3432 | public void applyAdjustmentFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3433 | Adjustment adjustment) throws RemoteException { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3434 | final long identity = Binder.clearCallingIdentity(); |
| 3435 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3436 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3437 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3438 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 3439 | applyAdjustment(n, adjustment); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3440 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3441 | mRankingHandler.requestSort(); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3442 | } finally { |
| 3443 | Binder.restoreCallingIdentity(identity); |
| 3444 | } |
| 3445 | } |
| 3446 | |
| 3447 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 3448 | public void applyAdjustmentsFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3449 | List<Adjustment> adjustments) throws RemoteException { |
| 3450 | |
| 3451 | final long identity = Binder.clearCallingIdentity(); |
| 3452 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3453 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3454 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3455 | for (Adjustment adjustment : adjustments) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3456 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 3457 | applyAdjustment(n, adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3458 | } |
| 3459 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3460 | mRankingHandler.requestSort(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3461 | } finally { |
| 3462 | Binder.restoreCallingIdentity(identity); |
| 3463 | } |
| 3464 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3465 | |
| 3466 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 3467 | public void updateNotificationChannelGroupFromPrivilegedListener( |
| 3468 | INotificationListener token, String pkg, UserHandle user, |
| 3469 | NotificationChannelGroup group) throws RemoteException { |
| 3470 | Preconditions.checkNotNull(user); |
| 3471 | verifyPrivilegedListener(token, user); |
| 3472 | createNotificationChannelGroup( |
| 3473 | pkg, getUidForPackageAndUser(pkg, user), group, false, true); |
| 3474 | savePolicyFile(); |
| 3475 | } |
| 3476 | |
| 3477 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3478 | public void updateNotificationChannelFromPrivilegedListener(INotificationListener token, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3479 | String pkg, UserHandle user, NotificationChannel channel) throws RemoteException { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3480 | Preconditions.checkNotNull(channel); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3481 | Preconditions.checkNotNull(pkg); |
| 3482 | Preconditions.checkNotNull(user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3483 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3484 | verifyPrivilegedListener(token, user); |
| 3485 | updateNotificationChannelInt(pkg, getUidForPackageAndUser(pkg, user), channel, true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | @Override |
| 3489 | public ParceledListSlice<NotificationChannel> getNotificationChannelsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3490 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 3491 | Preconditions.checkNotNull(pkg); |
| 3492 | Preconditions.checkNotNull(user); |
| 3493 | verifyPrivilegedListener(token, user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3494 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3495 | return mRankingHelper.getNotificationChannels(pkg, getUidForPackageAndUser(pkg, user), |
| 3496 | false /* includeDeleted */); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3497 | } |
| 3498 | |
| 3499 | @Override |
| 3500 | public ParceledListSlice<NotificationChannelGroup> |
| 3501 | getNotificationChannelGroupsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3502 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 3503 | Preconditions.checkNotNull(pkg); |
| 3504 | Preconditions.checkNotNull(user); |
| 3505 | verifyPrivilegedListener(token, user); |
| 3506 | |
| 3507 | List<NotificationChannelGroup> groups = new ArrayList<>(); |
| 3508 | groups.addAll(mRankingHelper.getNotificationChannelGroups( |
| 3509 | pkg, getUidForPackageAndUser(pkg, user))); |
| 3510 | return new ParceledListSlice<>(groups); |
| 3511 | } |
| 3512 | |
| 3513 | private void verifyPrivilegedListener(INotificationListener token, UserHandle user) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3514 | ManagedServiceInfo info; |
| 3515 | synchronized (mNotificationLock) { |
| 3516 | info = mListeners.checkServiceTokenLocked(token); |
| 3517 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 3518 | if (!hasCompanionDevice(info)) { |
| 3519 | throw new SecurityException(info + " does not have access"); |
| 3520 | } |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3521 | if (!info.enabledAndUserMatches(user.getIdentifier())) { |
| 3522 | throw new SecurityException(info + " does not have access"); |
| 3523 | } |
| 3524 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3525 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3526 | private int getUidForPackageAndUser(String pkg, UserHandle user) throws RemoteException { |
| 3527 | int uid = 0; |
| 3528 | long identity = Binder.clearCallingIdentity(); |
| 3529 | try { |
| 3530 | uid = mPackageManager.getPackageUid(pkg, 0, user.getIdentifier()); |
| 3531 | } finally { |
| 3532 | Binder.restoreCallingIdentity(identity); |
| 3533 | } |
| 3534 | return uid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3535 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3536 | |
| 3537 | @Override |
| 3538 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 3539 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) |
| 3540 | throws RemoteException { |
| 3541 | new ShellCmd().exec(this, in, out, err, args, callback, resultReceiver); |
| 3542 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3543 | }; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 3544 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3545 | private void applyAdjustment(NotificationRecord r, Adjustment adjustment) { |
| 3546 | if (r == null) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3547 | return; |
| 3548 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3549 | if (adjustment.getSignals() != null) { |
| 3550 | Bundle.setDefusable(adjustment.getSignals(), true); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3551 | r.addAdjustment(adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3552 | } |
| 3553 | } |
| 3554 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3555 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3556 | void addAutogroupKeyLocked(String key) { |
| 3557 | NotificationRecord r = mNotificationsByKey.get(key); |
| 3558 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3559 | return; |
| 3560 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3561 | if (r.sbn.getOverrideGroupKey() == null) { |
| 3562 | addAutoGroupAdjustment(r, GroupHelper.AUTOGROUP_KEY); |
| 3563 | EventLogTags.writeNotificationAutogrouped(key); |
| 3564 | mRankingHandler.requestSort(); |
| 3565 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3566 | } |
| 3567 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3568 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3569 | void removeAutogroupKeyLocked(String key) { |
| 3570 | NotificationRecord r = mNotificationsByKey.get(key); |
| 3571 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3572 | return; |
| 3573 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3574 | if (r.sbn.getOverrideGroupKey() != null) { |
| 3575 | addAutoGroupAdjustment(r, null); |
| 3576 | EventLogTags.writeNotificationUnautogrouped(key); |
| 3577 | mRankingHandler.requestSort(); |
| 3578 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | private void addAutoGroupAdjustment(NotificationRecord r, String overrideGroupKey) { |
| 3582 | Bundle signals = new Bundle(); |
| 3583 | signals.putString(Adjustment.KEY_GROUP_KEY, overrideGroupKey); |
| 3584 | Adjustment adjustment = |
| 3585 | new Adjustment(r.sbn.getPackageName(), r.getKey(), signals, "", r.sbn.getUserId()); |
| 3586 | r.addAdjustment(adjustment); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3587 | } |
| 3588 | |
| 3589 | // Clears the 'fake' auto-group summary. |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3590 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3591 | private void clearAutogroupSummaryLocked(int userId, String pkg) { |
| 3592 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 3593 | if (summaries != null && summaries.containsKey(pkg)) { |
| 3594 | // Clear summary. |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3595 | final NotificationRecord removed = findNotificationByKeyLocked(summaries.remove(pkg)); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3596 | if (removed != null) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 3597 | boolean wasPosted = removeFromNotificationListsLocked(removed); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 3598 | cancelNotificationLocked(removed, false, REASON_UNAUTOBUNDLED, wasPosted, null); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3599 | } |
| 3600 | } |
| 3601 | } |
| 3602 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 3603 | @GuardedBy("mNotificationLock") |
| 3604 | private boolean hasAutoGroupSummaryLocked(StatusBarNotification sbn) { |
| 3605 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(sbn.getUserId()); |
| 3606 | return summaries != null && summaries.containsKey(sbn.getPackageName()); |
| 3607 | } |
| 3608 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3609 | // Posts a 'fake' summary for a package that has exceeded the solo-notification limit. |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3610 | private void createAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 3611 | NotificationRecord summaryRecord = null; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3612 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3613 | NotificationRecord notificationRecord = mNotificationsByKey.get(triggeringKey); |
| 3614 | if (notificationRecord == null) { |
| 3615 | // The notification could have been cancelled again already. A successive |
| 3616 | // adjustment will post a summary if needed. |
| 3617 | return; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3618 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3619 | final StatusBarNotification adjustedSbn = notificationRecord.sbn; |
| 3620 | userId = adjustedSbn.getUser().getIdentifier(); |
| 3621 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 3622 | if (summaries == null) { |
| 3623 | summaries = new ArrayMap<>(); |
| 3624 | } |
| 3625 | mAutobundledSummaries.put(userId, summaries); |
| 3626 | if (!summaries.containsKey(pkg)) { |
| 3627 | // Add summary |
| 3628 | final ApplicationInfo appInfo = |
| 3629 | adjustedSbn.getNotification().extras.getParcelable( |
| 3630 | Notification.EXTRA_BUILDER_APPLICATION_INFO); |
| 3631 | final Bundle extras = new Bundle(); |
| 3632 | extras.putParcelable(Notification.EXTRA_BUILDER_APPLICATION_INFO, appInfo); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3633 | final String channelId = notificationRecord.getChannel().getId(); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3634 | final Notification summaryNotification = |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3635 | new Notification.Builder(getContext(), channelId) |
| 3636 | .setSmallIcon(adjustedSbn.getNotification().getSmallIcon()) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3637 | .setGroupSummary(true) |
Julia Reynolds | 9d5786e | 2017-04-28 10:26:32 -0400 | [diff] [blame] | 3638 | .setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3639 | .setGroup(GroupHelper.AUTOGROUP_KEY) |
| 3640 | .setFlag(Notification.FLAG_AUTOGROUP_SUMMARY, true) |
| 3641 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 3642 | .setColor(adjustedSbn.getNotification().color) |
| 3643 | .setLocalOnly(true) |
| 3644 | .build(); |
| 3645 | summaryNotification.extras.putAll(extras); |
| 3646 | Intent appIntent = getContext().getPackageManager().getLaunchIntentForPackage(pkg); |
| 3647 | if (appIntent != null) { |
| 3648 | summaryNotification.contentIntent = PendingIntent.getActivityAsUser( |
| 3649 | getContext(), 0, appIntent, 0, null, UserHandle.of(userId)); |
| 3650 | } |
| 3651 | final StatusBarNotification summarySbn = |
| 3652 | new StatusBarNotification(adjustedSbn.getPackageName(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3653 | adjustedSbn.getOpPkg(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3654 | Integer.MAX_VALUE, |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3655 | GroupHelper.AUTOGROUP_KEY, adjustedSbn.getUid(), |
| 3656 | adjustedSbn.getInitialPid(), summaryNotification, |
| 3657 | adjustedSbn.getUser(), GroupHelper.AUTOGROUP_KEY, |
| 3658 | System.currentTimeMillis()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 3659 | summaryRecord = new NotificationRecord(getContext(), summarySbn, |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 3660 | notificationRecord.getChannel()); |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 3661 | summaryRecord.setIsAppImportanceLocked( |
| 3662 | notificationRecord.getIsAppImportanceLocked()); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3663 | summaries.put(pkg, summarySbn.getKey()); |
| 3664 | } |
| 3665 | } |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3666 | if (summaryRecord != null && checkDisqualifyingFeatures(userId, MY_UID, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3667 | summaryRecord.sbn.getId(), summaryRecord.sbn.getTag(), summaryRecord, true)) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3668 | mHandler.post(new EnqueueNotificationRunnable(userId, summaryRecord)); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3669 | } |
| 3670 | } |
| 3671 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3672 | private String disableNotificationEffects(NotificationRecord record) { |
| 3673 | if (mDisableNotificationEffects) { |
| 3674 | return "booleanState"; |
| 3675 | } |
| 3676 | if ((mListenerHints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 3677 | return "listenerHints"; |
| 3678 | } |
| 3679 | if (mCallState != TelephonyManager.CALL_STATE_IDLE && !mZenModeHelper.isCall(record)) { |
| 3680 | return "callState"; |
| 3681 | } |
| 3682 | return null; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3683 | }; |
| 3684 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3685 | private void dumpJson(PrintWriter pw, @NonNull DumpFilter filter) { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3686 | JSONObject dump = new JSONObject(); |
| 3687 | try { |
| 3688 | dump.put("service", "Notification Manager"); |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 3689 | dump.put("bans", mRankingHelper.dumpBansJson(filter)); |
| 3690 | dump.put("ranking", mRankingHelper.dumpJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3691 | dump.put("stats", mUsageStats.dumpJson(filter)); |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 3692 | dump.put("channels", mRankingHelper.dumpChannelsJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3693 | } catch (JSONException e) { |
| 3694 | e.printStackTrace(); |
| 3695 | } |
| 3696 | pw.println(dump); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3697 | } |
| 3698 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3699 | private void dumpProto(FileDescriptor fd, @NonNull DumpFilter filter) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3700 | final ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 3701 | synchronized (mNotificationLock) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3702 | int N = mNotificationList.size(); |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 3703 | for (int i = 0; i < N; i++) { |
| 3704 | final NotificationRecord nr = mNotificationList.get(i); |
| 3705 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3706 | nr.dump(proto, NotificationServiceDumpProto.RECORDS, filter.redact, |
| 3707 | NotificationRecordProto.POSTED); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3708 | } |
| 3709 | N = mEnqueuedNotifications.size(); |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 3710 | for (int i = 0; i < N; i++) { |
| 3711 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 3712 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3713 | nr.dump(proto, NotificationServiceDumpProto.RECORDS, filter.redact, |
| 3714 | NotificationRecordProto.ENQUEUED); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3715 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3716 | List<NotificationRecord> snoozed = mSnoozeHelper.getSnoozed(); |
| 3717 | N = snoozed.size(); |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 3718 | for (int i = 0; i < N; i++) { |
| 3719 | final NotificationRecord nr = snoozed.get(i); |
| 3720 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3721 | nr.dump(proto, NotificationServiceDumpProto.RECORDS, filter.redact, |
| 3722 | NotificationRecordProto.SNOOZED); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3723 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3724 | |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3725 | long zenLog = proto.start(NotificationServiceDumpProto.ZEN); |
| 3726 | mZenModeHelper.dump(proto); |
| 3727 | for (ComponentName suppressor : mEffectsSuppressors) { |
Kweku Adams | 9954633 | 2018-01-24 17:03:50 -0800 | [diff] [blame] | 3728 | suppressor.writeToProto(proto, ZenModeProto.SUPPRESSORS); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3729 | } |
| 3730 | proto.end(zenLog); |
| 3731 | |
| 3732 | long listenersToken = proto.start(NotificationServiceDumpProto.NOTIFICATION_LISTENERS); |
| 3733 | mListeners.dump(proto, filter); |
| 3734 | proto.end(listenersToken); |
| 3735 | |
| 3736 | proto.write(NotificationServiceDumpProto.LISTENER_HINTS, mListenerHints); |
| 3737 | |
| 3738 | for (int i = 0; i < mListenersDisablingEffects.size(); ++i) { |
| 3739 | long effectsToken = proto.start( |
| 3740 | NotificationServiceDumpProto.LISTENERS_DISABLING_EFFECTS); |
| 3741 | |
| 3742 | proto.write( |
| 3743 | ListenersDisablingEffectsProto.HINT, mListenersDisablingEffects.keyAt(i)); |
| 3744 | final ArraySet<ManagedServiceInfo> listeners = |
| 3745 | mListenersDisablingEffects.valueAt(i); |
| 3746 | for (int j = 0; j < listeners.size(); j++) { |
| 3747 | final ManagedServiceInfo listener = listeners.valueAt(i); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 3748 | listener.writeToProto(proto, ListenersDisablingEffectsProto.LISTENERS, null); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3749 | } |
| 3750 | |
| 3751 | proto.end(effectsToken); |
| 3752 | } |
| 3753 | |
| 3754 | long assistantsToken = proto.start( |
| 3755 | NotificationServiceDumpProto.NOTIFICATION_ASSISTANTS); |
| 3756 | mAssistants.dump(proto, filter); |
| 3757 | proto.end(assistantsToken); |
| 3758 | |
| 3759 | long conditionsToken = proto.start(NotificationServiceDumpProto.CONDITION_PROVIDERS); |
| 3760 | mConditionProviders.dump(proto, filter); |
| 3761 | proto.end(conditionsToken); |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 3762 | |
| 3763 | long rankingToken = proto.start(NotificationServiceDumpProto.RANKING_CONFIG); |
| 3764 | mRankingHelper.dump(proto, filter); |
| 3765 | proto.end(rankingToken); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3766 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3767 | |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3768 | proto.flush(); |
| 3769 | } |
| 3770 | |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 3771 | private void dumpNotificationRecords(PrintWriter pw, @NonNull DumpFilter filter) { |
| 3772 | synchronized (mNotificationLock) { |
| 3773 | int N; |
| 3774 | N = mNotificationList.size(); |
| 3775 | if (N > 0) { |
| 3776 | pw.println(" Notification List:"); |
| 3777 | for (int i = 0; i < N; i++) { |
| 3778 | final NotificationRecord nr = mNotificationList.get(i); |
| 3779 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3780 | nr.dump(pw, " ", getContext(), filter.redact); |
| 3781 | } |
| 3782 | pw.println(" "); |
| 3783 | } |
| 3784 | } |
| 3785 | } |
| 3786 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3787 | void dumpImpl(PrintWriter pw, @NonNull DumpFilter filter) { |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3788 | pw.print("Current Notification Manager state"); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3789 | if (filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3790 | pw.print(" (filtered to "); pw.print(filter); pw.print(")"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3791 | } |
| 3792 | pw.println(':'); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3793 | int N; |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3794 | final boolean zenOnly = filter.filtered && filter.zen; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3795 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3796 | if (!zenOnly) { |
| 3797 | synchronized (mToastQueue) { |
| 3798 | N = mToastQueue.size(); |
| 3799 | if (N > 0) { |
| 3800 | pw.println(" Toast Queue:"); |
| 3801 | for (int i=0; i<N; i++) { |
| 3802 | mToastQueue.get(i).dump(pw, " ", filter); |
| 3803 | } |
| 3804 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3805 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3806 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3807 | } |
| 3808 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3809 | synchronized (mNotificationLock) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3810 | if (!zenOnly) { |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 3811 | // Priority filters are only set when called via bugreport. If set |
| 3812 | // skip sections that are part of the critical section. |
| 3813 | if (!filter.normalPriority) { |
| 3814 | dumpNotificationRecords(pw, filter); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3815 | } |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3816 | if (!filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3817 | N = mLights.size(); |
| 3818 | if (N > 0) { |
| 3819 | pw.println(" Lights List:"); |
| 3820 | for (int i=0; i<N; i++) { |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3821 | if (i == N - 1) { |
| 3822 | pw.print(" > "); |
| 3823 | } else { |
| 3824 | pw.print(" "); |
| 3825 | } |
| 3826 | pw.println(mLights.get(i)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3827 | } |
| 3828 | pw.println(" "); |
| 3829 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 3830 | pw.println(" mUseAttentionLight=" + mUseAttentionLight); |
| 3831 | pw.println(" mNotificationPulseEnabled=" + mNotificationPulseEnabled); |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3832 | pw.println(" mSoundNotificationKey=" + mSoundNotificationKey); |
| 3833 | pw.println(" mVibrateNotificationKey=" + mVibrateNotificationKey); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3834 | pw.println(" mDisableNotificationEffects=" + mDisableNotificationEffects); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3835 | pw.println(" mCallState=" + callStateToString(mCallState)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3836 | pw.println(" mSystemReady=" + mSystemReady); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 3837 | pw.println(" mMaxPackageEnqueueRate=" + mMaxPackageEnqueueRate); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3838 | } |
| 3839 | pw.println(" mArchive=" + mArchive.toString()); |
| 3840 | Iterator<StatusBarNotification> iter = mArchive.descendingIterator(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3841 | int j=0; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3842 | while (iter.hasNext()) { |
| 3843 | final StatusBarNotification sbn = iter.next(); |
| 3844 | if (filter != null && !filter.matches(sbn)) continue; |
| 3845 | pw.println(" " + sbn); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3846 | if (++j >= 5) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3847 | if (iter.hasNext()) pw.println(" ..."); |
| 3848 | break; |
| 3849 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3850 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3851 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3852 | if (!zenOnly) { |
| 3853 | N = mEnqueuedNotifications.size(); |
| 3854 | if (N > 0) { |
| 3855 | pw.println(" Enqueued Notification List:"); |
| 3856 | for (int i = 0; i < N; i++) { |
| 3857 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 3858 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3859 | nr.dump(pw, " ", getContext(), filter.redact); |
| 3860 | } |
| 3861 | pw.println(" "); |
| 3862 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3863 | |
| 3864 | mSnoozeHelper.dump(pw, filter); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3865 | } |
| 3866 | } |
| 3867 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3868 | if (!zenOnly) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3869 | pw.println("\n Ranking Config:"); |
| 3870 | mRankingHelper.dump(pw, " ", filter); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 3871 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3872 | pw.println("\n Notification listeners:"); |
| 3873 | mListeners.dump(pw, filter); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3874 | pw.print(" mListenerHints: "); pw.println(mListenerHints); |
| 3875 | pw.print(" mListenersDisablingEffects: ("); |
| 3876 | N = mListenersDisablingEffects.size(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3877 | for (int i = 0; i < N; i++) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 3878 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 3879 | if (i > 0) pw.print(';'); |
| 3880 | pw.print("hint[" + hint + "]:"); |
| 3881 | |
| 3882 | final ArraySet<ManagedServiceInfo> listeners = |
| 3883 | mListenersDisablingEffects.valueAt(i); |
| 3884 | final int listenerSize = listeners.size(); |
| 3885 | |
| 3886 | for (int j = 0; j < listenerSize; j++) { |
| 3887 | if (i > 0) pw.print(','); |
| 3888 | final ManagedServiceInfo listener = listeners.valueAt(i); |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 3889 | if (listener != null) { |
| 3890 | pw.print(listener.component); |
| 3891 | } |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 3892 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3893 | } |
| 3894 | pw.println(')'); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 3895 | pw.println("\n Notification assistant services:"); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3896 | mAssistants.dump(pw, filter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3897 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3898 | |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3899 | if (!filter.filtered || zenOnly) { |
| 3900 | pw.println("\n Zen Mode:"); |
| 3901 | pw.print(" mInterruptionFilter="); pw.println(mInterruptionFilter); |
| 3902 | mZenModeHelper.dump(pw, " "); |
| 3903 | |
| 3904 | pw.println("\n Zen Log:"); |
| 3905 | ZenLog.dump(pw, " "); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3906 | } |
| 3907 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3908 | pw.println("\n Condition providers:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3909 | mConditionProviders.dump(pw, filter); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3910 | |
| 3911 | pw.println("\n Group summaries:"); |
| 3912 | for (Entry<String, NotificationRecord> entry : mSummaryByGroupKey.entrySet()) { |
| 3913 | NotificationRecord r = entry.getValue(); |
| 3914 | pw.println(" " + entry.getKey() + " -> " + r.getKey()); |
| 3915 | if (mNotificationsByKey.get(r.getKey()) != r) { |
| 3916 | pw.println("!!!!!!LEAK: Record not found in mNotificationsByKey."); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3917 | r.dump(pw, " ", getContext(), filter.redact); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3918 | } |
| 3919 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3920 | |
| 3921 | if (!zenOnly) { |
| 3922 | pw.println("\n Usage Stats:"); |
| 3923 | mUsageStats.dump(pw, " ", filter); |
| 3924 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3925 | } |
| 3926 | } |
| 3927 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3928 | /** |
| 3929 | * The private API only accessible to the system process. |
| 3930 | */ |
| 3931 | private final NotificationManagerInternal mInternalService = new NotificationManagerInternal() { |
| 3932 | @Override |
Julia Reynolds | f3de8aa | 2017-09-29 15:52:37 -0400 | [diff] [blame] | 3933 | public NotificationChannel getNotificationChannel(String pkg, int uid, String |
| 3934 | channelId) { |
| 3935 | return mRankingHelper.getNotificationChannel(pkg, uid, channelId, false); |
| 3936 | } |
| 3937 | |
| 3938 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3939 | public void enqueueNotification(String pkg, String opPkg, int callingUid, int callingPid, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3940 | String tag, int id, Notification notification, int userId) { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3941 | enqueueNotificationInternal(pkg, opPkg, callingUid, callingPid, tag, id, notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3942 | userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3943 | } |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3944 | |
| 3945 | @Override |
| 3946 | public void removeForegroundServiceFlagFromNotification(String pkg, int notificationId, |
| 3947 | int userId) { |
| 3948 | checkCallerIsSystem(); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3949 | mHandler.post(new Runnable() { |
| 3950 | @Override |
| 3951 | public void run() { |
| 3952 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3953 | removeForegroundServiceFlagByListLocked( |
| 3954 | mEnqueuedNotifications, pkg, notificationId, userId); |
| 3955 | removeForegroundServiceFlagByListLocked( |
| 3956 | mNotificationList, pkg, notificationId, userId); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3957 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3958 | } |
| 3959 | }); |
| 3960 | } |
| 3961 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3962 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3963 | private void removeForegroundServiceFlagByListLocked( |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3964 | ArrayList<NotificationRecord> notificationList, String pkg, int notificationId, |
| 3965 | int userId) { |
| 3966 | NotificationRecord r = findNotificationByListLocked( |
| 3967 | notificationList, pkg, null, notificationId, userId); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3968 | if (r == null) { |
| 3969 | return; |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3970 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3971 | StatusBarNotification sbn = r.sbn; |
| 3972 | // NoMan adds flags FLAG_NO_CLEAR and FLAG_ONGOING_EVENT when it sees |
| 3973 | // FLAG_FOREGROUND_SERVICE. Hence it's not enough to remove |
| 3974 | // FLAG_FOREGROUND_SERVICE, we have to revert to the flags we received |
| 3975 | // initially *and* force remove FLAG_FOREGROUND_SERVICE. |
| 3976 | sbn.getNotification().flags = |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 3977 | (r.mOriginalFlags & ~FLAG_FOREGROUND_SERVICE); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3978 | mRankingHelper.sort(mNotificationList); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3979 | mListeners.notifyPostedLocked(r, r); |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3980 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3981 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3982 | |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3983 | void enqueueNotificationInternal(final String pkg, final String opPkg, final int callingUid, |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 3984 | final int callingPid, final String tag, final int id, final Notification notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3985 | int incomingUserId) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 3986 | if (DBG) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3987 | Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id |
| 3988 | + " notification=" + notification); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 3989 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3990 | checkCallerIsSystemOrSameApp(pkg); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3991 | |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 3992 | final int userId = ActivityManager.handleIncomingUser(callingPid, |
| 3993 | callingUid, incomingUserId, true, false, "enqueueNotification", pkg); |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 3994 | final UserHandle user = new UserHandle(userId); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3995 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3996 | if (pkg == null || notification == null) { |
| 3997 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 3998 | + " id=" + id + " notification=" + notification); |
| 3999 | } |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4000 | |
| 4001 | // The system can post notifications for any package, let us resolve that. |
| 4002 | final int notificationUid = resolveNotificationUid(opPkg, callingUid, userId); |
| 4003 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4004 | // Fix the notification as best we can. |
| 4005 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4006 | final ApplicationInfo ai = mPackageManagerClient.getApplicationInfoAsUser( |
Jeff Sharkey | 012bc7b | 2016-04-11 16:30:27 -0600 | [diff] [blame] | 4007 | pkg, PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 4008 | (userId == UserHandle.USER_ALL) ? USER_SYSTEM : userId); |
Julia Reynolds | e071abd | 2017-03-22 10:52:11 -0400 | [diff] [blame] | 4009 | Notification.addFieldsFromContext(ai, notification); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 4010 | |
| 4011 | int canColorize = mPackageManagerClient.checkPermission( |
| 4012 | android.Manifest.permission.USE_COLORIZED_NOTIFICATIONS, pkg); |
| 4013 | if (canColorize == PERMISSION_GRANTED) { |
| 4014 | notification.flags |= Notification.FLAG_CAN_COLORIZE; |
| 4015 | } else { |
| 4016 | notification.flags &= ~Notification.FLAG_CAN_COLORIZE; |
| 4017 | } |
| 4018 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4019 | } catch (NameNotFoundException e) { |
| 4020 | Slog.e(TAG, "Cannot create a context for sending app", e); |
| 4021 | return; |
| 4022 | } |
| 4023 | |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 4024 | mUsageStats.registerEnqueuedByApp(pkg); |
| 4025 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4026 | // setup local book-keeping |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 4027 | String channelId = notification.getChannelId(); |
| 4028 | if (mIsTelevision && (new Notification.TvExtender(notification)).getChannelId() != null) { |
| 4029 | channelId = (new Notification.TvExtender(notification)).getChannelId(); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 4030 | } |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 4031 | final NotificationChannel channel = mRankingHelper.getNotificationChannel(pkg, |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4032 | notificationUid, channelId, false /* includeDeleted */); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 4033 | if (channel == null) { |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 4034 | final String noChannelStr = "No Channel found for " |
| 4035 | + "pkg=" + pkg |
| 4036 | + ", channelId=" + channelId |
Julia Reynolds | f26eb91 | 2017-05-22 15:47:06 -0400 | [diff] [blame] | 4037 | + ", id=" + id |
| 4038 | + ", tag=" + tag |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 4039 | + ", opPkg=" + opPkg |
| 4040 | + ", callingUid=" + callingUid |
| 4041 | + ", userId=" + userId |
| 4042 | + ", incomingUserId=" + incomingUserId |
| 4043 | + ", notificationUid=" + notificationUid |
| 4044 | + ", notification=" + notification; |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 4045 | Log.e(TAG, noChannelStr); |
Beverly | 5d4564b | 2018-04-10 20:09:23 -0400 | [diff] [blame] | 4046 | boolean appNotificationsOff = mRankingHelper.getImportance(pkg, notificationUid) |
| 4047 | == NotificationManager.IMPORTANCE_NONE; |
| 4048 | |
| 4049 | if (!appNotificationsOff) { |
| 4050 | doChannelWarningToast("Developer warning for package \"" + pkg + "\"\n" + |
| 4051 | "Failed to post notification on channel \"" + channelId + "\"\n" + |
| 4052 | "See log for more details"); |
| 4053 | } |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 4054 | return; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 4055 | } |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 4056 | |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 4057 | final StatusBarNotification n = new StatusBarNotification( |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4058 | pkg, opPkg, id, tag, notificationUid, callingPid, notification, |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 4059 | user, null, System.currentTimeMillis()); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 4060 | final NotificationRecord r = new NotificationRecord(getContext(), n, channel); |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 4061 | r.setIsAppImportanceLocked(mRankingHelper.getIsAppImportanceLocked(pkg, callingUid)); |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 4062 | |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 4063 | if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 4064 | final boolean fgServiceShown = channel.isFgServiceShown(); |
| 4065 | if (((channel.getUserLockedFields() & NotificationChannel.USER_LOCKED_IMPORTANCE) == 0 |
| 4066 | || !fgServiceShown) |
| 4067 | && (r.getImportance() == IMPORTANCE_MIN |
| 4068 | || r.getImportance() == IMPORTANCE_NONE)) { |
| 4069 | // Increase the importance of foreground service notifications unless the user had |
| 4070 | // an opinion otherwise (and the channel hasn't yet shown a fg service). |
| 4071 | if (TextUtils.isEmpty(channelId) |
| 4072 | || NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 4073 | r.setImportance(IMPORTANCE_LOW, "Bumped for foreground service"); |
| 4074 | } else { |
| 4075 | channel.setImportance(IMPORTANCE_LOW); |
| 4076 | if (!fgServiceShown) { |
| 4077 | channel.unlockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); |
| 4078 | channel.setFgServiceShown(true); |
| 4079 | } |
| 4080 | mRankingHelper.updateNotificationChannel(pkg, notificationUid, channel, false); |
| 4081 | r.updateNotificationChannel(channel); |
| 4082 | } |
| 4083 | } else if (!fgServiceShown && !TextUtils.isEmpty(channelId) |
| 4084 | && !NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 4085 | channel.setFgServiceShown(true); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 4086 | r.updateNotificationChannel(channel); |
| 4087 | } |
| 4088 | } |
| 4089 | |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 4090 | if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r, |
| 4091 | r.sbn.getOverrideGroupKey() != null)) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4092 | return; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 4093 | } |
| 4094 | |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 4095 | // Whitelist pending intents. |
| 4096 | if (notification.allPendingIntents != null) { |
| 4097 | final int intentCount = notification.allPendingIntents.size(); |
| 4098 | if (intentCount > 0) { |
| 4099 | final ActivityManagerInternal am = LocalServices |
| 4100 | .getService(ActivityManagerInternal.class); |
| 4101 | final long duration = LocalServices.getService( |
| 4102 | DeviceIdleController.LocalService.class).getNotificationWhitelistDuration(); |
| 4103 | for (int i = 0; i < intentCount; i++) { |
| 4104 | PendingIntent pendingIntent = notification.allPendingIntents.valueAt(i); |
| 4105 | if (pendingIntent != null) { |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 4106 | am.setPendingIntentWhitelistDuration(pendingIntent.getTarget(), |
| 4107 | WHITELIST_TOKEN, duration); |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 4108 | } |
| 4109 | } |
| 4110 | } |
| 4111 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 4112 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4113 | mHandler.post(new EnqueueNotificationRunnable(userId, r)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4114 | } |
| 4115 | |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 4116 | private void doChannelWarningToast(CharSequence toastText) { |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 4117 | final int defaultWarningEnabled = Build.IS_DEBUGGABLE ? 1 : 0; |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 4118 | final boolean warningEnabled = Settings.Global.getInt(getContext().getContentResolver(), |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 4119 | Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, defaultWarningEnabled) != 0; |
| 4120 | if (warningEnabled) { |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 4121 | Toast toast = Toast.makeText(getContext(), mHandler.getLooper(), toastText, |
Geoffrey Pitsch | 5bdddbd | 2017-05-26 10:50:05 -0400 | [diff] [blame] | 4122 | Toast.LENGTH_SHORT); |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 4123 | toast.show(); |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 4124 | } |
| 4125 | } |
| 4126 | |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4127 | private int resolveNotificationUid(String opPackageName, int callingUid, int userId) { |
| 4128 | // The system can post notifications on behalf of any package it wants |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4129 | if (isCallerSystemOrPhone() && opPackageName != null && !"android".equals(opPackageName)) { |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4130 | try { |
| 4131 | return getContext().getPackageManager() |
| 4132 | .getPackageUidAsUser(opPackageName, userId); |
| 4133 | } catch (NameNotFoundException e) { |
| 4134 | /* ignore */ |
| 4135 | } |
| 4136 | } |
| 4137 | return callingUid; |
| 4138 | } |
| 4139 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4140 | /** |
| 4141 | * Checks if a notification can be posted. checks rate limiter, snooze helper, and blocking. |
| 4142 | * |
| 4143 | * Has side effects. |
| 4144 | */ |
| 4145 | private boolean checkDisqualifyingFeatures(int userId, int callingUid, int id, String tag, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 4146 | NotificationRecord r, boolean isAutogroup) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4147 | final String pkg = r.sbn.getPackageName(); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4148 | final boolean isSystemNotification = |
| 4149 | isUidSystemOrPhone(callingUid) || ("android".equals(pkg)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4150 | final boolean isNotificationFromListener = mListeners.isListenerPackage(pkg); |
| 4151 | |
| 4152 | // Limit the number of notifications that any given package except the android |
| 4153 | // package or a registered listener can enqueue. Prevents DOS attacks and deals with leaks. |
| 4154 | if (!isSystemNotification && !isNotificationFromListener) { |
| 4155 | synchronized (mNotificationLock) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 4156 | if (mNotificationsByKey.get(r.sbn.getKey()) == null && isCallerInstantApp(pkg)) { |
| 4157 | // Ephemeral apps have some special constraints for notifications. |
| 4158 | // They are not allowed to create new notifications however they are allowed to |
| 4159 | // update notifications created by the system (e.g. a foreground service |
| 4160 | // notification). |
| 4161 | throw new SecurityException("Instant app " + pkg |
| 4162 | + " cannot create notifications"); |
| 4163 | } |
| 4164 | |
| 4165 | // rate limit updates that aren't completed progress notifications |
| 4166 | if (mNotificationsByKey.get(r.sbn.getKey()) != null |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 4167 | && !r.getNotification().hasCompletedProgress() |
| 4168 | && !isAutogroup) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 4169 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4170 | final float appEnqueueRate = mUsageStats.getAppEnqueueRate(pkg); |
| 4171 | if (appEnqueueRate > mMaxPackageEnqueueRate) { |
| 4172 | mUsageStats.registerOverRateQuota(pkg); |
| 4173 | final long now = SystemClock.elapsedRealtime(); |
| 4174 | if ((now - mLastOverRateLogTime) > MIN_PACKAGE_OVERRATE_LOG_INTERVAL) { |
| 4175 | Slog.e(TAG, "Package enqueue rate is " + appEnqueueRate |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 4176 | + ". Shedding " + r.sbn.getKey() + ". package=" + pkg); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4177 | mLastOverRateLogTime = now; |
| 4178 | } |
| 4179 | return false; |
| 4180 | } |
| 4181 | } |
| 4182 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 4183 | // limit the number of outstanding notificationrecords an app can have |
| 4184 | int count = getNotificationCountLocked(pkg, userId, id, tag); |
| 4185 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 4186 | mUsageStats.registerOverCountQuota(pkg); |
| 4187 | Slog.e(TAG, "Package has already posted or enqueued " + count |
| 4188 | + " notifications. Not showing more. package=" + pkg); |
| 4189 | return false; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4190 | } |
| 4191 | } |
| 4192 | } |
| 4193 | |
| 4194 | // snoozed apps |
| 4195 | if (mSnoozeHelper.isSnoozed(userId, pkg, r.getKey())) { |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4196 | MetricsLogger.action(r.getLogMaker() |
| 4197 | .setType(MetricsProto.MetricsEvent.TYPE_UPDATE) |
| 4198 | .setCategory(MetricsProto.MetricsEvent.NOTIFICATION_SNOOZED)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4199 | if (DBG) { |
| 4200 | Slog.d(TAG, "Ignored enqueue for snoozed notification " + r.getKey()); |
| 4201 | } |
| 4202 | mSnoozeHelper.update(userId, r); |
| 4203 | savePolicyFile(); |
| 4204 | return false; |
| 4205 | } |
| 4206 | |
| 4207 | |
| 4208 | // blocked apps |
| 4209 | if (isBlocked(r, mUsageStats)) { |
| 4210 | return false; |
| 4211 | } |
| 4212 | |
| 4213 | return true; |
| 4214 | } |
| 4215 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 4216 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 4217 | protected int getNotificationCountLocked(String pkg, int userId, int excludedId, |
| 4218 | String excludedTag) { |
| 4219 | int count = 0; |
| 4220 | final int N = mNotificationList.size(); |
| 4221 | for (int i = 0; i < N; i++) { |
| 4222 | final NotificationRecord existing = mNotificationList.get(i); |
| 4223 | if (existing.sbn.getPackageName().equals(pkg) |
| 4224 | && existing.sbn.getUserId() == userId) { |
| 4225 | if (existing.sbn.getId() == excludedId |
| 4226 | && TextUtils.equals(existing.sbn.getTag(), excludedTag)) { |
| 4227 | continue; |
| 4228 | } |
| 4229 | count++; |
| 4230 | } |
| 4231 | } |
| 4232 | final int M = mEnqueuedNotifications.size(); |
| 4233 | for (int i = 0; i < M; i++) { |
| 4234 | final NotificationRecord existing = mEnqueuedNotifications.get(i); |
| 4235 | if (existing.sbn.getPackageName().equals(pkg) |
| 4236 | && existing.sbn.getUserId() == userId) { |
| 4237 | count++; |
| 4238 | } |
| 4239 | } |
| 4240 | return count; |
| 4241 | } |
| 4242 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4243 | protected boolean isBlocked(NotificationRecord r, NotificationUsageStats usageStats) { |
| 4244 | final String pkg = r.sbn.getPackageName(); |
| 4245 | final int callingUid = r.sbn.getUid(); |
| 4246 | |
| 4247 | final boolean isPackageSuspended = isPackageSuspendedForUser(pkg, callingUid); |
| 4248 | if (isPackageSuspended) { |
| 4249 | Slog.e(TAG, "Suppressing notification from package due to package " |
| 4250 | + "suspended by administrator."); |
| 4251 | usageStats.registerSuspendedByAdmin(r); |
| 4252 | return isPackageSuspended; |
| 4253 | } |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 4254 | final boolean isBlocked = |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 4255 | mRankingHelper.isGroupBlocked(pkg, callingUid, r.getChannel().getGroup()) |
| 4256 | || mRankingHelper.getImportance(pkg, callingUid) |
| 4257 | == NotificationManager.IMPORTANCE_NONE |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 4258 | || r.getChannel().getImportance() == NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4259 | if (isBlocked) { |
| 4260 | Slog.e(TAG, "Suppressing notification from package by user request."); |
| 4261 | usageStats.registerBlocked(r); |
| 4262 | } |
| 4263 | return isBlocked; |
| 4264 | } |
| 4265 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4266 | protected class SnoozeNotificationRunnable implements Runnable { |
| 4267 | private final String mKey; |
| 4268 | private final long mDuration; |
| 4269 | private final String mSnoozeCriterionId; |
| 4270 | |
| 4271 | SnoozeNotificationRunnable(String key, long duration, String snoozeCriterionId) { |
| 4272 | mKey = key; |
| 4273 | mDuration = duration; |
| 4274 | mSnoozeCriterionId = snoozeCriterionId; |
| 4275 | } |
| 4276 | |
| 4277 | @Override |
| 4278 | public void run() { |
| 4279 | synchronized (mNotificationLock) { |
| 4280 | final NotificationRecord r = findNotificationByKeyLocked(mKey); |
| 4281 | if (r != null) { |
| 4282 | snoozeLocked(r); |
| 4283 | } |
| 4284 | } |
| 4285 | } |
| 4286 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4287 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4288 | void snoozeLocked(NotificationRecord r) { |
| 4289 | if (r.sbn.isGroup()) { |
| 4290 | final List<NotificationRecord> groupNotifications = findGroupNotificationsLocked( |
| 4291 | r.sbn.getPackageName(), r.sbn.getGroupKey(), r.sbn.getUserId()); |
| 4292 | if (r.getNotification().isGroupSummary()) { |
| 4293 | // snooze summary and all children |
| 4294 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 4295 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 4296 | } |
| 4297 | } else { |
| 4298 | // if there is a valid summary for this group, and we are snoozing the only |
| 4299 | // child, also snooze the summary |
| 4300 | if (mSummaryByGroupKey.containsKey(r.sbn.getGroupKey())) { |
| 4301 | if (groupNotifications.size() != 2) { |
| 4302 | snoozeNotificationLocked(r); |
| 4303 | } else { |
| 4304 | // snooze summary and the one child |
| 4305 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 4306 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 4307 | } |
| 4308 | } |
| 4309 | } else { |
| 4310 | snoozeNotificationLocked(r); |
| 4311 | } |
| 4312 | } |
| 4313 | } else { |
| 4314 | // just snooze the one notification |
| 4315 | snoozeNotificationLocked(r); |
| 4316 | } |
| 4317 | } |
| 4318 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4319 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4320 | void snoozeNotificationLocked(NotificationRecord r) { |
| 4321 | MetricsLogger.action(r.getLogMaker() |
| 4322 | .setCategory(MetricsEvent.NOTIFICATION_SNOOZED) |
| 4323 | .setType(MetricsEvent.TYPE_CLOSE) |
Chris Wren | 21a2e72 | 2017-10-02 17:44:53 -0400 | [diff] [blame] | 4324 | .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_SNOOZE_DURATION_MS, |
| 4325 | mDuration) |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4326 | .addTaggedData(MetricsEvent.NOTIFICATION_SNOOZED_CRITERIA, |
| 4327 | mSnoozeCriterionId == null ? 0 : 1)); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4328 | boolean wasPosted = removeFromNotificationListsLocked(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4329 | cancelNotificationLocked(r, false, REASON_SNOOZED, wasPosted, null); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4330 | updateLightsLocked(); |
| 4331 | if (mSnoozeCriterionId != null) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4332 | mAssistants.notifyAssistantSnoozedLocked(r.sbn, mSnoozeCriterionId); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4333 | mSnoozeHelper.snooze(r); |
| 4334 | } else { |
| 4335 | mSnoozeHelper.snooze(r, mDuration); |
| 4336 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4337 | r.recordSnoozed(); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4338 | savePolicyFile(); |
| 4339 | } |
| 4340 | } |
| 4341 | |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 4342 | protected class EnqueueNotificationRunnable implements Runnable { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4343 | private final NotificationRecord r; |
| 4344 | private final int userId; |
| 4345 | |
| 4346 | EnqueueNotificationRunnable(int userId, NotificationRecord r) { |
| 4347 | this.userId = userId; |
| 4348 | this.r = r; |
| 4349 | }; |
| 4350 | |
| 4351 | @Override |
| 4352 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4353 | synchronized (mNotificationLock) { |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 4354 | mEnqueuedNotifications.add(r); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4355 | scheduleTimeoutLocked(r); |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 4356 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4357 | final StatusBarNotification n = r.sbn; |
| 4358 | if (DBG) Slog.d(TAG, "EnqueueNotificationRunnable.run for: " + n.getKey()); |
| 4359 | NotificationRecord old = mNotificationsByKey.get(n.getKey()); |
| 4360 | if (old != null) { |
| 4361 | // Retain ranking information from previous record |
| 4362 | r.copyRankingInformation(old); |
| 4363 | } |
| 4364 | |
| 4365 | final int callingUid = n.getUid(); |
| 4366 | final int callingPid = n.getInitialPid(); |
| 4367 | final Notification notification = n.getNotification(); |
| 4368 | final String pkg = n.getPackageName(); |
| 4369 | final int id = n.getId(); |
| 4370 | final String tag = n.getTag(); |
| 4371 | |
| 4372 | // Handle grouped notifications and bail out early if we |
| 4373 | // can to avoid extracting signals. |
| 4374 | handleGroupedNotificationLocked(r, old, callingUid, callingPid); |
| 4375 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4376 | // if this is a group child, unsnooze parent summary |
| 4377 | if (n.isGroup() && notification.isGroupChild()) { |
| 4378 | mSnoozeHelper.repostGroupSummary(pkg, r.getUserId(), n.getGroupKey()); |
| 4379 | } |
| 4380 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4381 | // This conditional is a dirty hack to limit the logging done on |
| 4382 | // behalf of the download manager without affecting other apps. |
| 4383 | if (!pkg.equals("com.android.providers.downloads") |
| 4384 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| 4385 | int enqueueStatus = EVENTLOG_ENQUEUE_STATUS_NEW; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4386 | if (old != null) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4387 | enqueueStatus = EVENTLOG_ENQUEUE_STATUS_UPDATE; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4388 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4389 | EventLogTags.writeNotificationEnqueue(callingUid, callingPid, |
| 4390 | pkg, id, tag, userId, notification.toString(), |
| 4391 | enqueueStatus); |
| 4392 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4393 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4394 | mRankingHelper.extractSignals(r); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4395 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4396 | // tell the assistant service about the notification |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4397 | if (mAssistants.isEnabled()) { |
| 4398 | mAssistants.onNotificationEnqueued(r); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4399 | mHandler.postDelayed(new PostNotificationRunnable(r.getKey()), |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4400 | DELAY_FOR_ASSISTANT_TIME); |
| 4401 | } else { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4402 | mHandler.post(new PostNotificationRunnable(r.getKey())); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4403 | } |
| 4404 | } |
| 4405 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4406 | } |
| 4407 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 4408 | @GuardedBy("mNotificationLock") |
| 4409 | private boolean isPackageSuspendedLocked(NotificationRecord r) { |
| 4410 | final String pkg = r.sbn.getPackageName(); |
| 4411 | final int callingUid = r.sbn.getUid(); |
| 4412 | |
| 4413 | return isPackageSuspendedForUser(pkg, callingUid); |
| 4414 | } |
| 4415 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4416 | protected class PostNotificationRunnable implements Runnable { |
| 4417 | private final String key; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4418 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4419 | PostNotificationRunnable(String key) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4420 | this.key = key; |
| 4421 | } |
| 4422 | |
| 4423 | @Override |
| 4424 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4425 | synchronized (mNotificationLock) { |
| 4426 | try { |
| 4427 | NotificationRecord r = null; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4428 | int N = mEnqueuedNotifications.size(); |
| 4429 | for (int i = 0; i < N; i++) { |
| 4430 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 4431 | if (Objects.equals(key, enqueued.getKey())) { |
| 4432 | r = enqueued; |
| 4433 | break; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4434 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4435 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4436 | if (r == null) { |
| 4437 | Slog.i(TAG, "Cannot find enqueued record for key: " + key); |
| 4438 | return; |
| 4439 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 4440 | |
| 4441 | r.setHidden(isPackageSuspendedLocked(r)); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4442 | NotificationRecord old = mNotificationsByKey.get(key); |
| 4443 | final StatusBarNotification n = r.sbn; |
| 4444 | final Notification notification = n.getNotification(); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4445 | int index = indexOfNotificationLocked(n.getKey()); |
| 4446 | if (index < 0) { |
| 4447 | mNotificationList.add(r); |
| 4448 | mUsageStats.registerPostedByApp(r); |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 4449 | r.setInterruptive(isVisuallyInterruptive(null, r)); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4450 | } else { |
| 4451 | old = mNotificationList.get(index); |
| 4452 | mNotificationList.set(index, r); |
| 4453 | mUsageStats.registerUpdatedByApp(r, old); |
| 4454 | // Make sure we don't lose the foreground service state. |
| 4455 | notification.flags |= |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4456 | old.getNotification().flags & FLAG_FOREGROUND_SERVICE; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4457 | r.isUpdate = true; |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 4458 | r.setTextChanged(isVisuallyInterruptive(old, r)); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4459 | } |
| 4460 | |
| 4461 | mNotificationsByKey.put(n.getKey(), r); |
| 4462 | |
| 4463 | // Ensure if this is a foreground service that the proper additional |
| 4464 | // flags are set. |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4465 | if ((notification.flags & FLAG_FOREGROUND_SERVICE) != 0) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4466 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 4467 | | Notification.FLAG_NO_CLEAR; |
| 4468 | } |
| 4469 | |
| 4470 | applyZenModeLocked(r); |
| 4471 | mRankingHelper.sort(mNotificationList); |
| 4472 | |
| 4473 | if (notification.getSmallIcon() != null) { |
| 4474 | StatusBarNotification oldSbn = (old != null) ? old.sbn : null; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 4475 | mListeners.notifyPostedLocked(r, old); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 4476 | if (oldSbn == null || !Objects.equals(oldSbn.getGroup(), n.getGroup())) { |
| 4477 | mHandler.post(new Runnable() { |
| 4478 | @Override |
| 4479 | public void run() { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 4480 | mGroupHelper.onNotificationPosted( |
| 4481 | n, hasAutoGroupSummaryLocked(n)); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 4482 | } |
| 4483 | }); |
| 4484 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4485 | } else { |
| 4486 | Slog.e(TAG, "Not posting notification without small icon: " + notification); |
| 4487 | if (old != null && !old.isCanceled) { |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 4488 | mListeners.notifyRemovedLocked(r, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4489 | NotificationListenerService.REASON_ERROR, null); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4490 | mHandler.post(new Runnable() { |
| 4491 | @Override |
| 4492 | public void run() { |
| 4493 | mGroupHelper.onNotificationRemoved(n); |
| 4494 | } |
| 4495 | }); |
| 4496 | } |
| 4497 | // ATTENTION: in a future release we will bail out here |
| 4498 | // so that we do not play sounds, show lights, etc. for invalid |
| 4499 | // notifications |
| 4500 | Slog.e(TAG, "WARNING: In a future release this will crash the app: " |
| 4501 | + n.getPackageName()); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4502 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4503 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 4504 | if (!r.isHidden()) { |
| 4505 | buzzBeepBlinkLocked(r); |
| 4506 | } |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 4507 | maybeRecordInterruptionLocked(r); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4508 | } finally { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4509 | int N = mEnqueuedNotifications.size(); |
| 4510 | for (int i = 0; i < N; i++) { |
| 4511 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 4512 | if (Objects.equals(key, enqueued.getKey())) { |
| 4513 | mEnqueuedNotifications.remove(i); |
| 4514 | break; |
| 4515 | } |
| 4516 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4517 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4518 | } |
| 4519 | } |
| 4520 | } |
| 4521 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4522 | /** |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4523 | * If the notification differs enough visually, consider it a new interruptive notification. |
| 4524 | */ |
| 4525 | @GuardedBy("mNotificationLock") |
| 4526 | @VisibleForTesting |
| 4527 | protected boolean isVisuallyInterruptive(NotificationRecord old, NotificationRecord r) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4528 | if (old == null) { |
| 4529 | if (DEBUG_INTERRUPTIVENESS) { |
| 4530 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4531 | + r.getKey() + " is interruptive: new notification"); |
| 4532 | } |
| 4533 | return true; |
| 4534 | } |
| 4535 | |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 4536 | if (r == null) { |
| 4537 | if (DEBUG_INTERRUPTIVENESS) { |
| 4538 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4539 | + r.getKey() + " is not interruptive: null"); |
| 4540 | } |
| 4541 | return false; |
| 4542 | } |
| 4543 | |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4544 | Notification oldN = old.sbn.getNotification(); |
| 4545 | Notification newN = r.sbn.getNotification(); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4546 | |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4547 | if (oldN.extras == null || newN.extras == null) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4548 | if (DEBUG_INTERRUPTIVENESS) { |
| 4549 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4550 | + r.getKey() + " is not interruptive: no extras"); |
| 4551 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4552 | return false; |
| 4553 | } |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4554 | |
| 4555 | // Ignore visual interruptions from foreground services because users |
| 4556 | // consider them one 'session'. Count them for everything else. |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 4557 | if ((r.sbn.getNotification().flags & FLAG_FOREGROUND_SERVICE) != 0) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4558 | if (DEBUG_INTERRUPTIVENESS) { |
| 4559 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4560 | + r.getKey() + " is not interruptive: foreground service"); |
| 4561 | } |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4562 | return false; |
| 4563 | } |
| 4564 | |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 4565 | // Ignore summary updates because we don't display most of the information. |
| 4566 | if (r.sbn.isGroup() && r.sbn.getNotification().isGroupSummary()) { |
| 4567 | if (DEBUG_INTERRUPTIVENESS) { |
| 4568 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4569 | + r.getKey() + " is not interruptive: summary"); |
| 4570 | } |
| 4571 | return false; |
| 4572 | } |
| 4573 | |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4574 | final String oldTitle = String.valueOf(oldN.extras.get(Notification.EXTRA_TITLE)); |
| 4575 | final String newTitle = String.valueOf(newN.extras.get(Notification.EXTRA_TITLE)); |
| 4576 | if (!Objects.equals(oldTitle, newTitle)) { |
| 4577 | if (DEBUG_INTERRUPTIVENESS) { |
| 4578 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4579 | + r.getKey() + " is interruptive: changed title"); |
| 4580 | Log.v(TAG, "INTERRUPTIVENESS: " + String.format(" old title: %s (%s@0x%08x)", |
| 4581 | oldTitle, oldTitle.getClass(), oldTitle.hashCode())); |
| 4582 | Log.v(TAG, "INTERRUPTIVENESS: " + String.format(" new title: %s (%s@0x%08x)", |
| 4583 | newTitle, newTitle.getClass(), newTitle.hashCode())); |
| 4584 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4585 | return true; |
| 4586 | } |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4587 | // Do not compare Spannables (will always return false); compare unstyled Strings |
| 4588 | final String oldText = String.valueOf(oldN.extras.get(Notification.EXTRA_TEXT)); |
| 4589 | final String newText = String.valueOf(newN.extras.get(Notification.EXTRA_TEXT)); |
| 4590 | if (!Objects.equals(oldText, newText)) { |
| 4591 | if (DEBUG_INTERRUPTIVENESS) { |
| 4592 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4593 | + r.getKey() + " is interruptive: changed text"); |
| 4594 | Log.v(TAG, "INTERRUPTIVENESS: " + String.format(" old text: %s (%s@0x%08x)", |
| 4595 | oldText, oldText.getClass(), oldText.hashCode())); |
| 4596 | Log.v(TAG, "INTERRUPTIVENESS: " + String.format(" new text: %s (%s@0x%08x)", |
| 4597 | newText, newText.getClass(), newText.hashCode())); |
| 4598 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4599 | return true; |
| 4600 | } |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4601 | if (oldN.hasCompletedProgress() != newN.hasCompletedProgress()) { |
| 4602 | if (DEBUG_INTERRUPTIVENESS) { |
| 4603 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4604 | + r.getKey() + " is interruptive: completed progress"); |
| 4605 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4606 | return true; |
| 4607 | } |
| 4608 | // Actions |
| 4609 | if (Notification.areActionsVisiblyDifferent(oldN, newN)) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4610 | if (DEBUG_INTERRUPTIVENESS) { |
| 4611 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4612 | + r.getKey() + " is interruptive: changed actions"); |
| 4613 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4614 | return true; |
| 4615 | } |
| 4616 | |
| 4617 | try { |
| 4618 | Notification.Builder oldB = Notification.Builder.recoverBuilder(getContext(), oldN); |
| 4619 | Notification.Builder newB = Notification.Builder.recoverBuilder(getContext(), newN); |
| 4620 | |
| 4621 | // Style based comparisons |
| 4622 | if (Notification.areStyledNotificationsVisiblyDifferent(oldB, newB)) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4623 | if (DEBUG_INTERRUPTIVENESS) { |
| 4624 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4625 | + r.getKey() + " is interruptive: styles differ"); |
| 4626 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4627 | return true; |
| 4628 | } |
| 4629 | |
| 4630 | // Remote views |
| 4631 | if (Notification.areRemoteViewsChanged(oldB, newB)) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4632 | if (DEBUG_INTERRUPTIVENESS) { |
| 4633 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4634 | + r.getKey() + " is interruptive: remoteviews differ"); |
| 4635 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4636 | return true; |
| 4637 | } |
| 4638 | } catch (Exception e) { |
| 4639 | Slog.w(TAG, "error recovering builder", e); |
| 4640 | } |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4641 | |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4642 | return false; |
| 4643 | } |
| 4644 | |
| 4645 | /** |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 4646 | * Keeps the last 5 packages that have notified, by user. |
| 4647 | */ |
| 4648 | @GuardedBy("mNotificationLock") |
| 4649 | @VisibleForTesting |
| 4650 | protected void logRecentLocked(NotificationRecord r) { |
| 4651 | if (r.isUpdate) { |
| 4652 | return; |
| 4653 | } |
| 4654 | ArrayList<NotifyingApp> recentAppsForUser = |
| 4655 | mRecentApps.getOrDefault(r.getUser().getIdentifier(), new ArrayList<>(6)); |
| 4656 | NotifyingApp na = new NotifyingApp() |
| 4657 | .setPackage(r.sbn.getPackageName()) |
| 4658 | .setUid(r.sbn.getUid()) |
| 4659 | .setLastNotified(r.sbn.getPostTime()); |
| 4660 | // A new notification gets an app moved to the front of the list |
| 4661 | for (int i = recentAppsForUser.size() - 1; i >= 0; i--) { |
| 4662 | NotifyingApp naExisting = recentAppsForUser.get(i); |
| 4663 | if (na.getPackage().equals(naExisting.getPackage()) |
| 4664 | && na.getUid() == naExisting.getUid()) { |
| 4665 | recentAppsForUser.remove(i); |
| 4666 | break; |
| 4667 | } |
| 4668 | } |
| 4669 | // time is always increasing, so always add to the front of the list |
| 4670 | recentAppsForUser.add(0, na); |
| 4671 | if (recentAppsForUser.size() > 5) { |
| 4672 | recentAppsForUser.remove(recentAppsForUser.size() -1); |
| 4673 | } |
| 4674 | mRecentApps.put(r.getUser().getIdentifier(), recentAppsForUser); |
| 4675 | } |
| 4676 | |
| 4677 | /** |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4678 | * Ensures that grouped notification receive their special treatment. |
| 4679 | * |
| 4680 | * <p>Cancels group children if the new notification causes a group to lose |
| 4681 | * its summary.</p> |
| 4682 | * |
| 4683 | * <p>Updates mSummaryByGroupKey.</p> |
| 4684 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4685 | @GuardedBy("mNotificationLock") |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4686 | private void handleGroupedNotificationLocked(NotificationRecord r, NotificationRecord old, |
| 4687 | int callingUid, int callingPid) { |
| 4688 | StatusBarNotification sbn = r.sbn; |
| 4689 | Notification n = sbn.getNotification(); |
Selim Cinek | 5b03ce9 | 2016-05-18 15:16:58 -0700 | [diff] [blame] | 4690 | if (n.isGroupSummary() && !sbn.isAppGroup()) { |
| 4691 | // notifications without a group shouldn't be a summary, otherwise autobundling can |
| 4692 | // lead to bugs |
| 4693 | n.flags &= ~Notification.FLAG_GROUP_SUMMARY; |
| 4694 | } |
| 4695 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4696 | String group = sbn.getGroupKey(); |
| 4697 | boolean isSummary = n.isGroupSummary(); |
| 4698 | |
| 4699 | Notification oldN = old != null ? old.sbn.getNotification() : null; |
| 4700 | String oldGroup = old != null ? old.sbn.getGroupKey() : null; |
| 4701 | boolean oldIsSummary = old != null && oldN.isGroupSummary(); |
| 4702 | |
| 4703 | if (oldIsSummary) { |
| 4704 | NotificationRecord removedSummary = mSummaryByGroupKey.remove(oldGroup); |
| 4705 | if (removedSummary != old) { |
| 4706 | String removedKey = |
| 4707 | removedSummary != null ? removedSummary.getKey() : "<null>"; |
| 4708 | Slog.w(TAG, "Removed summary didn't match old notification: old=" + old.getKey() + |
| 4709 | ", removed=" + removedKey); |
| 4710 | } |
| 4711 | } |
| 4712 | if (isSummary) { |
| 4713 | mSummaryByGroupKey.put(group, r); |
| 4714 | } |
| 4715 | |
| 4716 | // Clear out group children of the old notification if the update |
| 4717 | // causes the group summary to go away. This happens when the old |
| 4718 | // notification was a summary and the new one isn't, or when the old |
| 4719 | // notification was a summary and its group key changed. |
| 4720 | if (oldIsSummary && (!isSummary || !oldGroup.equals(group))) { |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 4721 | cancelGroupChildrenLocked(old, callingUid, callingPid, null, false /* sendDelete */, |
| 4722 | null); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4723 | } |
| 4724 | } |
| 4725 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4726 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4727 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4728 | void scheduleTimeoutLocked(NotificationRecord record) { |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 4729 | if (record.getNotification().getTimeoutAfter() > 0) { |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4730 | final PendingIntent pi = PendingIntent.getBroadcast(getContext(), |
| 4731 | REQUEST_CODE_TIMEOUT, |
| 4732 | new Intent(ACTION_NOTIFICATION_TIMEOUT) |
| 4733 | .setData(new Uri.Builder().scheme(SCHEME_TIMEOUT) |
| 4734 | .appendPath(record.getKey()).build()) |
| 4735 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 4736 | .putExtra(EXTRA_KEY, record.getKey()), |
| 4737 | PendingIntent.FLAG_UPDATE_CURRENT); |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 4738 | mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 4739 | SystemClock.elapsedRealtime() + record.getNotification().getTimeoutAfter(), pi); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4740 | } |
| 4741 | } |
| 4742 | |
| 4743 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4744 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4745 | void buzzBeepBlinkLocked(NotificationRecord record) { |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4746 | boolean buzz = false; |
| 4747 | boolean beep = false; |
| 4748 | boolean blink = false; |
| 4749 | |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4750 | final Notification notification = record.sbn.getNotification(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4751 | final String key = record.getKey(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4752 | |
| 4753 | // Should this notification make noise, vibe, or use the LED? |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 4754 | final boolean aboveThreshold = |
| 4755 | record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4756 | |
| 4757 | // Remember if this notification already owns the notification channels. |
| 4758 | boolean wasBeep = key != null && key.equals(mSoundNotificationKey); |
| 4759 | boolean wasBuzz = key != null && key.equals(mVibrateNotificationKey); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4760 | // These are set inside the conditional if the notification is allowed to make noise. |
| 4761 | boolean hasValidVibrate = false; |
| 4762 | boolean hasValidSound = false; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4763 | boolean sentAccessibilityEvent = false; |
| 4764 | // If the notification will appear in the status bar, it should send an accessibility |
| 4765 | // event |
| 4766 | if (!record.isUpdate && record.getImportance() > IMPORTANCE_MIN) { |
| 4767 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
| 4768 | sentAccessibilityEvent = true; |
| 4769 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4770 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4771 | if (aboveThreshold && isNotificationForCurrentUser(record)) { |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4772 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4773 | if (mSystemReady && mAudioManager != null) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4774 | Uri soundUri = record.getSound(); |
| 4775 | hasValidSound = soundUri != null && !Uri.EMPTY.equals(soundUri); |
| 4776 | long[] vibration = record.getVibration(); |
| 4777 | // Demote sound to vibration if vibration missing & phone in vibration mode. |
| 4778 | if (vibration == null |
| 4779 | && hasValidSound |
| 4780 | && (mAudioManager.getRingerModeInternal() |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 4781 | == AudioManager.RINGER_MODE_VIBRATE) |
| 4782 | && mAudioManager.getStreamVolume( |
| 4783 | AudioAttributes.toLegacyStreamType(record.getAudioAttributes())) == 0) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4784 | vibration = mFallbackVibrationPattern; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4785 | } |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4786 | hasValidVibrate = vibration != null; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4787 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4788 | boolean hasAudibleAlert = hasValidSound || hasValidVibrate; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4789 | if (hasAudibleAlert && !shouldMuteNotificationLocked(record)) { |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4790 | if (!sentAccessibilityEvent) { |
| 4791 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
| 4792 | sentAccessibilityEvent = true; |
| 4793 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4794 | if (DBG) Slog.v(TAG, "Interrupting!"); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4795 | if (hasValidSound) { |
| 4796 | mSoundNotificationKey = key; |
| 4797 | if (mInCall) { |
| 4798 | playInCallNotification(); |
| 4799 | beep = true; |
| 4800 | } else { |
| 4801 | beep = playSound(record, soundUri); |
| 4802 | } |
| 4803 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4804 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4805 | final boolean ringerModeSilent = |
| 4806 | mAudioManager.getRingerModeInternal() |
| 4807 | == AudioManager.RINGER_MODE_SILENT; |
| 4808 | if (!mInCall && hasValidVibrate && !ringerModeSilent) { |
| 4809 | mVibrateNotificationKey = key; |
| 4810 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4811 | buzz = playVibration(record, vibration, hasValidSound); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4812 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4813 | } |
| 4814 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4815 | } |
| 4816 | // If a notification is updated to remove the actively playing sound or vibrate, |
| 4817 | // cancel that feedback now |
| 4818 | if (wasBeep && !hasValidSound) { |
| 4819 | clearSoundLocked(); |
| 4820 | } |
| 4821 | if (wasBuzz && !hasValidVibrate) { |
| 4822 | clearVibrateLocked(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4823 | } |
| 4824 | |
| 4825 | // light |
| 4826 | // release the light |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4827 | boolean wasShowLights = mLights.remove(key); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 4828 | if (record.getLight() != null && aboveThreshold |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 4829 | && ((record.getSuppressedVisualEffects() & SUPPRESSED_EFFECT_LIGHTS) == 0)) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4830 | mLights.add(key); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4831 | updateLightsLocked(); |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 4832 | if (mUseAttentionLight) { |
| 4833 | mAttentionLight.pulse(); |
| 4834 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4835 | blink = true; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4836 | } else if (wasShowLights) { |
| 4837 | updateLightsLocked(); |
| 4838 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4839 | if (buzz || beep || blink) { |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 4840 | record.setInterruptive(true); |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 4841 | MetricsLogger.action(record.getLogMaker() |
| 4842 | .setCategory(MetricsEvent.NOTIFICATION_ALERT) |
| 4843 | .setType(MetricsEvent.TYPE_OPEN) |
| 4844 | .setSubtype((buzz ? 1 : 0) | (beep ? 2 : 0) | (blink ? 4 : 0))); |
| 4845 | EventLogTags.writeNotificationAlert(key, buzz ? 1 : 0, beep ? 1 : 0, blink ? 1 : 0); |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 4846 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4847 | } |
| 4848 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4849 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4850 | boolean shouldMuteNotificationLocked(final NotificationRecord record) { |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4851 | // Suppressed because it's a silent update |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4852 | final Notification notification = record.getNotification(); |
| 4853 | if(record.isUpdate |
| 4854 | && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) { |
| 4855 | return true; |
| 4856 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4857 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4858 | // muted by listener |
| 4859 | final String disableEffects = disableNotificationEffects(record); |
| 4860 | if (disableEffects != null) { |
| 4861 | ZenLog.traceDisableEffects(record, disableEffects); |
| 4862 | return true; |
| 4863 | } |
| 4864 | |
| 4865 | // suppressed due to DND |
| 4866 | if (record.isIntercepted()) { |
| 4867 | return true; |
| 4868 | } |
| 4869 | |
| 4870 | // Suppressed because another notification in its group handles alerting |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4871 | if (record.sbn.isGroup()) { |
Julia Reynolds | 79dfdd6 | 2018-04-17 15:36:33 -0400 | [diff] [blame] | 4872 | if (notification.suppressAlertingDueToGrouping()) { |
| 4873 | return true; |
| 4874 | } |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4875 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4876 | |
Julia Reynolds | 65b85cf | 2017-07-20 09:19:20 -0400 | [diff] [blame] | 4877 | // Suppressed for being too recently noisy |
| 4878 | final String pkg = record.sbn.getPackageName(); |
| 4879 | if (mUsageStats.isAlertRateLimited(pkg)) { |
| 4880 | Slog.e(TAG, "Muting recently noisy " + record.getKey()); |
| 4881 | return true; |
| 4882 | } |
| 4883 | |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4884 | return false; |
| 4885 | } |
| 4886 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 4887 | private boolean playSound(final NotificationRecord record, Uri soundUri) { |
| 4888 | boolean looping = (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
Jean-Michel Trivi | ec2bb18 | 2018-03-23 18:04:00 -0700 | [diff] [blame] | 4889 | // play notifications if there is no user of exclusive audio focus |
| 4890 | // and the stream volume is not 0 (non-zero volume implies not silenced by SILENT or |
| 4891 | // VIBRATE ringer mode) |
| 4892 | if (!mAudioManager.isAudioFocusExclusive() |
| 4893 | && (mAudioManager.getStreamVolume( |
| 4894 | AudioAttributes.toLegacyStreamType(record.getAudioAttributes())) != 0)) { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 4895 | final long identity = Binder.clearCallingIdentity(); |
| 4896 | try { |
| 4897 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4898 | if (player != null) { |
| 4899 | if (DBG) Slog.v(TAG, "Playing sound " + soundUri |
| 4900 | + " with attributes " + record.getAudioAttributes()); |
| 4901 | player.playAsync(soundUri, record.sbn.getUser(), looping, |
| 4902 | record.getAudioAttributes()); |
| 4903 | return true; |
| 4904 | } |
| 4905 | } catch (RemoteException e) { |
| 4906 | } finally { |
| 4907 | Binder.restoreCallingIdentity(identity); |
| 4908 | } |
| 4909 | } |
| 4910 | return false; |
| 4911 | } |
| 4912 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4913 | private boolean playVibration(final NotificationRecord record, long[] vibration, |
| 4914 | boolean delayVibForSound) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 4915 | // Escalate privileges so we can use the vibrator even if the |
| 4916 | // notifying app does not have the VIBRATE permission. |
| 4917 | long identity = Binder.clearCallingIdentity(); |
| 4918 | try { |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4919 | final VibrationEffect effect; |
| 4920 | try { |
| 4921 | final boolean insistent = |
| 4922 | (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 4923 | effect = VibrationEffect.createWaveform( |
| 4924 | vibration, insistent ? 0 : -1 /*repeatIndex*/); |
| 4925 | } catch (IllegalArgumentException e) { |
| 4926 | Slog.e(TAG, "Error creating vibration waveform with pattern: " + |
| 4927 | Arrays.toString(vibration)); |
| 4928 | return false; |
| 4929 | } |
| 4930 | if (delayVibForSound) { |
| 4931 | new Thread(() -> { |
| 4932 | // delay the vibration by the same amount as the notification sound |
| 4933 | final int waitMs = mAudioManager.getFocusRampTimeMs( |
| 4934 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 4935 | record.getAudioAttributes()); |
| 4936 | if (DBG) Slog.v(TAG, "Delaying vibration by " + waitMs + "ms"); |
| 4937 | try { |
| 4938 | Thread.sleep(waitMs); |
| 4939 | } catch (InterruptedException e) { } |
| 4940 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 4941 | effect, record.getAudioAttributes()); |
| 4942 | }).start(); |
| 4943 | } else { |
| 4944 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 4945 | effect, record.getAudioAttributes()); |
| 4946 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 4947 | return true; |
| 4948 | } finally{ |
| 4949 | Binder.restoreCallingIdentity(identity); |
| 4950 | } |
| 4951 | } |
| 4952 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4953 | private boolean isNotificationForCurrentUser(NotificationRecord record) { |
| 4954 | final int currentUser; |
| 4955 | final long token = Binder.clearCallingIdentity(); |
| 4956 | try { |
| 4957 | currentUser = ActivityManager.getCurrentUser(); |
| 4958 | } finally { |
| 4959 | Binder.restoreCallingIdentity(token); |
| 4960 | } |
| 4961 | return (record.getUserId() == UserHandle.USER_ALL || |
| 4962 | record.getUserId() == currentUser || |
| 4963 | mUserProfiles.isCurrentProfile(record.getUserId())); |
| 4964 | } |
| 4965 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4966 | protected void playInCallNotification() { |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4967 | new Thread() { |
| 4968 | @Override |
| 4969 | public void run() { |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4970 | final long identity = Binder.clearCallingIdentity(); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4971 | try { |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4972 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4973 | if (player != null) { |
luochaojiang | 50e5273c | 2018-04-16 16:55:03 +0800 | [diff] [blame] | 4974 | if (mCallNotificationToken != null) { |
| 4975 | player.stop(mCallNotificationToken); |
| 4976 | } |
| 4977 | mCallNotificationToken = new Binder(); |
| 4978 | player.play(mCallNotificationToken, mInCallNotificationUri, |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4979 | mInCallNotificationAudioAttributes, |
| 4980 | mInCallNotificationVolume, false); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4981 | } |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4982 | } catch (RemoteException e) { |
| 4983 | } finally { |
| 4984 | Binder.restoreCallingIdentity(identity); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4985 | } |
| 4986 | } |
| 4987 | }.start(); |
| 4988 | } |
| 4989 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4990 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4991 | void showNextToastLocked() { |
| 4992 | ToastRecord record = mToastQueue.get(0); |
| 4993 | while (record != null) { |
| 4994 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| 4995 | try { |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4996 | record.callback.show(record.token); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 4997 | scheduleDurationReachedLocked(record); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4998 | return; |
| 4999 | } catch (RemoteException e) { |
| 5000 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| 5001 | + " in package " + record.pkg); |
| 5002 | // remove it from the list and let the process die |
| 5003 | int index = mToastQueue.indexOf(record); |
| 5004 | if (index >= 0) { |
| 5005 | mToastQueue.remove(index); |
| 5006 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5007 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5008 | if (mToastQueue.size() > 0) { |
| 5009 | record = mToastQueue.get(0); |
| 5010 | } else { |
| 5011 | record = null; |
| 5012 | } |
| 5013 | } |
| 5014 | } |
| 5015 | } |
| 5016 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5017 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5018 | void cancelToastLocked(int index) { |
| 5019 | ToastRecord record = mToastQueue.get(index); |
| 5020 | try { |
| 5021 | record.callback.hide(); |
| 5022 | } catch (RemoteException e) { |
| 5023 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| 5024 | + " in package " + record.pkg); |
| 5025 | // don't worry about this, we're about to remove it from |
| 5026 | // the list anyway |
| 5027 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5028 | |
| 5029 | ToastRecord lastToast = mToastQueue.remove(index); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5030 | |
| 5031 | mWindowManagerInternal.removeWindowToken(lastToast.token, false /* removeWindows */, |
| 5032 | DEFAULT_DISPLAY); |
| 5033 | // We passed 'false' for 'removeWindows' so that the client has time to stop |
| 5034 | // rendering (as hide above is a one-way message), otherwise we could crash |
| 5035 | // a client which was actively using a surface made from the token. However |
| 5036 | // we need to schedule a timeout to make sure the token is eventually killed |
| 5037 | // one way or another. |
| 5038 | scheduleKillTokenTimeout(lastToast.token); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5039 | |
| 5040 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5041 | if (mToastQueue.size() > 0) { |
| 5042 | // Show the next one. If the callback fails, this will remove |
| 5043 | // it from the list, so don't assume that the list hasn't changed |
| 5044 | // after this point. |
| 5045 | showNextToastLocked(); |
| 5046 | } |
| 5047 | } |
| 5048 | |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5049 | void finishTokenLocked(IBinder t) { |
| 5050 | mHandler.removeCallbacksAndMessages(t); |
| 5051 | // We pass 'true' for 'removeWindows' to let the WindowManager destroy any |
| 5052 | // remaining surfaces as either the client has called finishToken indicating |
| 5053 | // it has successfully removed the views, or the client has timed out |
| 5054 | // at which point anything goes. |
| 5055 | mWindowManagerInternal.removeWindowToken(t, true /* removeWindows */, |
| 5056 | DEFAULT_DISPLAY); |
| 5057 | } |
| 5058 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5059 | @GuardedBy("mToastQueue") |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5060 | private void scheduleDurationReachedLocked(ToastRecord r) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5061 | { |
| 5062 | mHandler.removeCallbacksAndMessages(r); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5063 | Message m = Message.obtain(mHandler, MESSAGE_DURATION_REACHED, r); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5064 | long delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY; |
| 5065 | mHandler.sendMessageDelayed(m, delay); |
| 5066 | } |
| 5067 | |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5068 | private void handleDurationReached(ToastRecord record) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5069 | { |
| 5070 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| 5071 | synchronized (mToastQueue) { |
| 5072 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 5073 | if (index >= 0) { |
| 5074 | cancelToastLocked(index); |
| 5075 | } |
| 5076 | } |
| 5077 | } |
| 5078 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5079 | @GuardedBy("mToastQueue") |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5080 | private void scheduleKillTokenTimeout(IBinder token) |
| 5081 | { |
| 5082 | mHandler.removeCallbacksAndMessages(token); |
| 5083 | Message m = Message.obtain(mHandler, MESSAGE_FINISH_TOKEN_TIMEOUT, token); |
Robert Carr | 3406d46 | 2018-03-15 16:19:07 -0700 | [diff] [blame] | 5084 | mHandler.sendMessageDelayed(m, FINISH_TOKEN_TIMEOUT); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5085 | } |
| 5086 | |
| 5087 | private void handleKillTokenTimeout(IBinder token) |
| 5088 | { |
| 5089 | if (DBG) Slog.d(TAG, "Kill Token Timeout token=" + token); |
| 5090 | synchronized (mToastQueue) { |
| 5091 | finishTokenLocked(token); |
| 5092 | } |
| 5093 | } |
| 5094 | |
| 5095 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5096 | int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 5097 | { |
| 5098 | IBinder cbak = callback.asBinder(); |
| 5099 | ArrayList<ToastRecord> list = mToastQueue; |
| 5100 | int len = list.size(); |
| 5101 | for (int i=0; i<len; i++) { |
| 5102 | ToastRecord r = list.get(i); |
Beverly Tai | 98efc79 | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 5103 | if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5104 | return i; |
| 5105 | } |
| 5106 | } |
| 5107 | return -1; |
| 5108 | } |
| 5109 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5110 | @GuardedBy("mToastQueue") |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5111 | void keepProcessAliveIfNeededLocked(int pid) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5112 | { |
| 5113 | int toastCount = 0; // toasts from this pid |
| 5114 | ArrayList<ToastRecord> list = mToastQueue; |
| 5115 | int N = list.size(); |
| 5116 | for (int i=0; i<N; i++) { |
| 5117 | ToastRecord r = list.get(i); |
| 5118 | if (r.pid == pid) { |
| 5119 | toastCount++; |
| 5120 | } |
| 5121 | } |
| 5122 | try { |
Dianne Hackborn | f965f40 | 2017-05-04 23:27:23 -0700 | [diff] [blame] | 5123 | mAm.setProcessImportant(mForegroundToken, pid, toastCount > 0, "toast"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5124 | } catch (RemoteException e) { |
| 5125 | // Shouldn't happen. |
| 5126 | } |
| 5127 | } |
| 5128 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5129 | private void handleRankingReconsideration(Message message) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 5130 | if (!(message.obj instanceof RankingReconsideration)) return; |
| 5131 | RankingReconsideration recon = (RankingReconsideration) message.obj; |
| 5132 | recon.run(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5133 | boolean changed; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5134 | synchronized (mNotificationLock) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 5135 | final NotificationRecord record = mNotificationsByKey.get(recon.getKey()); |
| 5136 | if (record == null) { |
| 5137 | return; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5138 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5139 | int indexBefore = findNotificationRecordIndexLocked(record); |
| 5140 | boolean interceptBefore = record.isIntercepted(); |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 5141 | float contactAffinityBefore = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5142 | int visibilityBefore = record.getPackageVisibilityOverride(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 5143 | recon.applyChangesLocked(record); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5144 | applyZenModeLocked(record); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5145 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5146 | int indexAfter = findNotificationRecordIndexLocked(record); |
| 5147 | boolean interceptAfter = record.isIntercepted(); |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 5148 | float contactAffinityAfter = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5149 | int visibilityAfter = record.getPackageVisibilityOverride(); |
| 5150 | changed = indexBefore != indexAfter || interceptBefore != interceptAfter |
| 5151 | || visibilityBefore != visibilityAfter; |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 5152 | if (interceptBefore && !interceptAfter |
| 5153 | && Float.compare(contactAffinityBefore, contactAffinityAfter) != 0) { |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5154 | buzzBeepBlinkLocked(record); |
| 5155 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5156 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5157 | if (changed) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5158 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 5159 | } |
| 5160 | } |
| 5161 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5162 | void handleRankingSort() { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 5163 | if (mRankingHelper == null) return; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5164 | synchronized (mNotificationLock) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5165 | final int N = mNotificationList.size(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5166 | // Any field that can change via one of the extractors needs to be added here. |
| 5167 | ArrayList<String> orderBefore = new ArrayList<>(N); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5168 | int[] visibilities = new int[N]; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5169 | boolean[] showBadges = new boolean[N]; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5170 | ArrayList<NotificationChannel> channelBefore = new ArrayList<>(N); |
| 5171 | ArrayList<String> groupKeyBefore = new ArrayList<>(N); |
| 5172 | ArrayList<ArrayList<String>> overridePeopleBefore = new ArrayList<>(N); |
| 5173 | ArrayList<ArrayList<SnoozeCriterion>> snoozeCriteriaBefore = new ArrayList<>(N); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5174 | ArrayList<Integer> userSentimentBefore = new ArrayList<>(N); |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 5175 | ArrayList<Integer> suppressVisuallyBefore = new ArrayList<>(N); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5176 | for (int i = 0; i < N; i++) { |
| 5177 | final NotificationRecord r = mNotificationList.get(i); |
| 5178 | orderBefore.add(r.getKey()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5179 | visibilities[i] = r.getPackageVisibilityOverride(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5180 | showBadges[i] = r.canShowBadge(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5181 | channelBefore.add(r.getChannel()); |
| 5182 | groupKeyBefore.add(r.getGroupKey()); |
| 5183 | overridePeopleBefore.add(r.getPeopleOverride()); |
| 5184 | snoozeCriteriaBefore.add(r.getSnoozeCriteria()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5185 | userSentimentBefore.add(r.getUserSentiment()); |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 5186 | suppressVisuallyBefore.add(r.getSuppressedVisualEffects()); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5187 | mRankingHelper.extractSignals(r); |
| 5188 | } |
Chris Wren | 19a02b0 | 2015-12-22 10:34:22 -0500 | [diff] [blame] | 5189 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5190 | for (int i = 0; i < N; i++) { |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5191 | final NotificationRecord r = mNotificationList.get(i); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5192 | if (!orderBefore.get(i).equals(r.getKey()) |
Julia Reynolds | 6976669 | 2016-02-01 15:35:08 -0500 | [diff] [blame] | 5193 | || visibilities[i] != r.getPackageVisibilityOverride() |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5194 | || showBadges[i] != r.canShowBadge() |
| 5195 | || !Objects.equals(channelBefore.get(i), r.getChannel()) |
| 5196 | || !Objects.equals(groupKeyBefore.get(i), r.getGroupKey()) |
| 5197 | || !Objects.equals(overridePeopleBefore.get(i), r.getPeopleOverride()) |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5198 | || !Objects.equals(snoozeCriteriaBefore.get(i), r.getSnoozeCriteria()) |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 5199 | || !Objects.equals(userSentimentBefore.get(i), r.getUserSentiment()) |
| 5200 | || !Objects.equals(suppressVisuallyBefore.get(i), |
| 5201 | r.getSuppressedVisualEffects())) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5202 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5203 | return; |
| 5204 | } |
| 5205 | } |
| 5206 | } |
| 5207 | } |
| 5208 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5209 | @GuardedBy("mNotificationLock") |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 5210 | private void recordCallerLocked(NotificationRecord record) { |
| 5211 | if (mZenModeHelper.isCall(record)) { |
| 5212 | mZenModeHelper.recordCaller(record); |
| 5213 | } |
| 5214 | } |
| 5215 | |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 5216 | // let zen mode evaluate this record |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5217 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5218 | private void applyZenModeLocked(NotificationRecord record) { |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 5219 | record.setIntercepted(mZenModeHelper.shouldIntercept(record)); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 5220 | if (record.isIntercepted()) { |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 5221 | record.setSuppressedVisualEffects( |
| 5222 | mZenModeHelper.getNotificationPolicy().suppressedVisualEffects); |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 5223 | } else { |
| 5224 | record.setSuppressedVisualEffects(0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 5225 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5226 | } |
| 5227 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5228 | @GuardedBy("mNotificationLock") |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 5229 | private int findNotificationRecordIndexLocked(NotificationRecord target) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5230 | return mRankingHelper.indexOf(mNotificationList, target); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5231 | } |
| 5232 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5233 | private void handleSendRankingUpdate() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5234 | synchronized (mNotificationLock) { |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 5235 | mListeners.notifyRankingUpdateLocked(null); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5236 | } |
| 5237 | } |
| 5238 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5239 | private void scheduleListenerHintsChanged(int state) { |
| 5240 | mHandler.removeMessages(MESSAGE_LISTENER_HINTS_CHANGED); |
| 5241 | mHandler.obtainMessage(MESSAGE_LISTENER_HINTS_CHANGED, state, 0).sendToTarget(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5242 | } |
| 5243 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5244 | private void scheduleInterruptionFilterChanged(int listenerInterruptionFilter) { |
| 5245 | mHandler.removeMessages(MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED); |
| 5246 | mHandler.obtainMessage( |
| 5247 | MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED, |
| 5248 | listenerInterruptionFilter, |
| 5249 | 0).sendToTarget(); |
| 5250 | } |
| 5251 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5252 | private void handleListenerHintsChanged(int hints) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5253 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5254 | mListeners.notifyListenerHintsChangedLocked(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5255 | } |
| 5256 | } |
| 5257 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5258 | private void handleListenerInterruptionFilterChanged(int interruptionFilter) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5259 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5260 | mListeners.notifyInterruptionFilterChanged(interruptionFilter); |
| 5261 | } |
| 5262 | } |
| 5263 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5264 | protected class WorkerHandler extends Handler |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5265 | { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5266 | public WorkerHandler(Looper looper) { |
| 5267 | super(looper); |
| 5268 | } |
| 5269 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5270 | @Override |
| 5271 | public void handleMessage(Message msg) |
| 5272 | { |
| 5273 | switch (msg.what) |
| 5274 | { |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5275 | case MESSAGE_DURATION_REACHED: |
| 5276 | handleDurationReached((ToastRecord)msg.obj); |
| 5277 | break; |
| 5278 | case MESSAGE_FINISH_TOKEN_TIMEOUT: |
| 5279 | handleKillTokenTimeout((IBinder)msg.obj); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5280 | break; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 5281 | case MESSAGE_SAVE_POLICY_FILE: |
| 5282 | handleSavePolicyFile(); |
| 5283 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5284 | case MESSAGE_SEND_RANKING_UPDATE: |
| 5285 | handleSendRankingUpdate(); |
| 5286 | break; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5287 | case MESSAGE_LISTENER_HINTS_CHANGED: |
| 5288 | handleListenerHintsChanged(msg.arg1); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5289 | break; |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5290 | case MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED: |
| 5291 | handleListenerInterruptionFilterChanged(msg.arg1); |
| 5292 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5293 | } |
| 5294 | } |
| 5295 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5296 | protected void scheduleSendRankingUpdate() { |
| 5297 | if (!hasMessages(MESSAGE_SEND_RANKING_UPDATE)) { |
| 5298 | Message m = Message.obtain(this, MESSAGE_SEND_RANKING_UPDATE); |
| 5299 | sendMessage(m); |
| 5300 | } |
| 5301 | } |
| 5302 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5303 | } |
| 5304 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5305 | private final class RankingHandlerWorker extends Handler implements RankingHandler |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5306 | { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5307 | public RankingHandlerWorker(Looper looper) { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5308 | super(looper); |
| 5309 | } |
| 5310 | |
| 5311 | @Override |
| 5312 | public void handleMessage(Message msg) { |
| 5313 | switch (msg.what) { |
| 5314 | case MESSAGE_RECONSIDER_RANKING: |
| 5315 | handleRankingReconsideration(msg); |
| 5316 | break; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5317 | case MESSAGE_RANKING_SORT: |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5318 | handleRankingSort(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5319 | break; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5320 | } |
| 5321 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5322 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5323 | public void requestSort() { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5324 | removeMessages(MESSAGE_RANKING_SORT); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5325 | Message msg = Message.obtain(); |
| 5326 | msg.what = MESSAGE_RANKING_SORT; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5327 | sendMessage(msg); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5328 | } |
| 5329 | |
| 5330 | public void requestReconsideration(RankingReconsideration recon) { |
| 5331 | Message m = Message.obtain(this, |
| 5332 | NotificationManagerService.MESSAGE_RECONSIDER_RANKING, recon); |
| 5333 | long delay = recon.getDelay(TimeUnit.MILLISECONDS); |
| 5334 | sendMessageDelayed(m, delay); |
| 5335 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5336 | } |
| 5337 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5338 | // Notifications |
| 5339 | // ============================================================================ |
| 5340 | static int clamp(int x, int low, int high) { |
| 5341 | return (x < low) ? low : ((x > high) ? high : x); |
| 5342 | } |
| 5343 | |
| 5344 | void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
Eugene Susla | d4128ec | 2017-12-04 19:48:41 +0000 | [diff] [blame] | 5345 | if (!mAccessibilityManager.isEnabled()) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 5346 | return; |
| 5347 | } |
| 5348 | |
| 5349 | AccessibilityEvent event = |
| 5350 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 5351 | event.setPackageName(packageName); |
| 5352 | event.setClassName(Notification.class.getName()); |
| 5353 | event.setParcelableData(notification); |
| 5354 | CharSequence tickerText = notification.tickerText; |
| 5355 | if (!TextUtils.isEmpty(tickerText)) { |
| 5356 | event.getText().add(tickerText); |
| 5357 | } |
| 5358 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 5359 | mAccessibilityManager.sendAccessibilityEvent(event); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 5360 | } |
| 5361 | |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5362 | /** |
| 5363 | * Removes all NotificationsRecords with the same key as the given notification record |
| 5364 | * from both lists. Do not call this method while iterating over either list. |
| 5365 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5366 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5367 | private boolean removeFromNotificationListsLocked(NotificationRecord r) { |
| 5368 | // Remove from both lists, either list could have a separate Record for what is |
| 5369 | // effectively the same notification. |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5370 | boolean wasPosted = false; |
| 5371 | NotificationRecord recordInList = null; |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5372 | if ((recordInList = findNotificationByListLocked(mNotificationList, r.getKey())) |
| 5373 | != null) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5374 | mNotificationList.remove(recordInList); |
| 5375 | mNotificationsByKey.remove(recordInList.sbn.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5376 | wasPosted = true; |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5377 | } |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5378 | while ((recordInList = findNotificationByListLocked(mEnqueuedNotifications, r.getKey())) |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5379 | != null) { |
| 5380 | mEnqueuedNotifications.remove(recordInList); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5381 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5382 | return wasPosted; |
| 5383 | } |
| 5384 | |
| 5385 | @GuardedBy("mNotificationLock") |
| 5386 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason, |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5387 | boolean wasPosted, String listenerName) { |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5388 | cancelNotificationLocked(r, sendDelete, reason, -1, -1, wasPosted, listenerName); |
| 5389 | } |
| 5390 | |
| 5391 | @GuardedBy("mNotificationLock") |
| 5392 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason, |
| 5393 | int rank, int count, boolean wasPosted, String listenerName) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5394 | final String canceledKey = r.getKey(); |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 5395 | |
| 5396 | // Record caller. |
| 5397 | recordCallerLocked(r); |
| 5398 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5399 | if (r.getStats().getDismissalSurface() == NotificationStats.DISMISSAL_NOT_DISMISSED) { |
| 5400 | r.recordDismissalSurface(NotificationStats.DISMISSAL_OTHER); |
| 5401 | } |
| 5402 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 5403 | // tell the app |
| 5404 | if (sendDelete) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 5405 | if (r.getNotification().deleteIntent != null) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 5406 | try { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 5407 | r.getNotification().deleteIntent.send(); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 5408 | } catch (PendingIntent.CanceledException ex) { |
| 5409 | // do nothing - there's no relevant way to recover, and |
| 5410 | // no reason to let this propagate |
Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 5411 | Slog.w(TAG, "canceled PendingIntent for " + r.sbn.getPackageName(), ex); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 5412 | } |
| 5413 | } |
| 5414 | } |
| 5415 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5416 | // Only cancel these if this notification actually got to be posted. |
| 5417 | if (wasPosted) { |
| 5418 | // status bar |
| 5419 | if (r.getNotification().getSmallIcon() != null) { |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 5420 | if (reason != REASON_SNOOZED) { |
| 5421 | r.isCanceled = true; |
| 5422 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 5423 | mListeners.notifyRemovedLocked(r, reason, r.getStats()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5424 | mHandler.post(new Runnable() { |
| 5425 | @Override |
| 5426 | public void run() { |
| 5427 | mGroupHelper.onNotificationRemoved(r.sbn); |
| 5428 | } |
| 5429 | }); |
| 5430 | } |
| 5431 | |
| 5432 | // sound |
| 5433 | if (canceledKey.equals(mSoundNotificationKey)) { |
| 5434 | mSoundNotificationKey = null; |
| 5435 | final long identity = Binder.clearCallingIdentity(); |
| 5436 | try { |
| 5437 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 5438 | if (player != null) { |
| 5439 | player.stopAsync(); |
| 5440 | } |
| 5441 | } catch (RemoteException e) { |
| 5442 | } finally { |
| 5443 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 5444 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5445 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5446 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5447 | // vibrate |
| 5448 | if (canceledKey.equals(mVibrateNotificationKey)) { |
| 5449 | mVibrateNotificationKey = null; |
| 5450 | long identity = Binder.clearCallingIdentity(); |
| 5451 | try { |
| 5452 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 5453 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5454 | finally { |
| 5455 | Binder.restoreCallingIdentity(identity); |
| 5456 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5457 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5458 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5459 | // light |
| 5460 | mLights.remove(canceledKey); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5461 | } |
| 5462 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5463 | // Record usage stats |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5464 | // TODO: add unbundling stats? |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5465 | switch (reason) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 5466 | case REASON_CANCEL: |
| 5467 | case REASON_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5468 | case REASON_LISTENER_CANCEL: |
| 5469 | case REASON_LISTENER_CANCEL_ALL: |
| 5470 | mUsageStats.registerDismissedByUser(r); |
| 5471 | break; |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 5472 | case REASON_APP_CANCEL: |
| 5473 | case REASON_APP_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5474 | mUsageStats.registerRemovedByApp(r); |
| 5475 | break; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5476 | } |
| 5477 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5478 | String groupKey = r.getGroupKey(); |
| 5479 | NotificationRecord groupSummary = mSummaryByGroupKey.get(groupKey); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5480 | if (groupSummary != null && groupSummary.getKey().equals(canceledKey)) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5481 | mSummaryByGroupKey.remove(groupKey); |
| 5482 | } |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 5483 | final ArrayMap<String, String> summaries = mAutobundledSummaries.get(r.sbn.getUserId()); |
| 5484 | if (summaries != null && r.sbn.getKey().equals(summaries.get(r.sbn.getPackageName()))) { |
| 5485 | summaries.remove(r.sbn.getPackageName()); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5486 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5487 | |
Daniel Sandler | 23d7c70 | 2013-03-07 16:32:06 -0500 | [diff] [blame] | 5488 | // Save it for users of getHistoricalNotifications() |
| 5489 | mArchive.record(r.sbn); |
Christoph Studer | 81e5b5f | 2014-10-22 17:19:56 +0200 | [diff] [blame] | 5490 | |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 5491 | final long now = System.currentTimeMillis(); |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5492 | final LogMaker logMaker = r.getLogMaker(now) |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 5493 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
| 5494 | .setType(MetricsEvent.TYPE_DISMISS) |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5495 | .setSubtype(reason); |
| 5496 | if (rank != -1 && count != -1) { |
| 5497 | logMaker.addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, rank) |
| 5498 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, count); |
| 5499 | } |
| 5500 | MetricsLogger.action(logMaker); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 5501 | EventLogTags.writeNotificationCanceled(canceledKey, reason, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5502 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now), |
| 5503 | rank, count, listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5504 | } |
| 5505 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 5506 | @VisibleForTesting |
| 5507 | void updateUriPermissions(@Nullable NotificationRecord newRecord, |
| 5508 | @Nullable NotificationRecord oldRecord, String targetPkg, int targetUserId) { |
| 5509 | final String key = (newRecord != null) ? newRecord.getKey() : oldRecord.getKey(); |
| 5510 | if (DBG) Slog.d(TAG, key + ": updating permissions"); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 5511 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 5512 | final ArraySet<Uri> newUris = (newRecord != null) ? newRecord.getGrantableUris() : null; |
| 5513 | final ArraySet<Uri> oldUris = (oldRecord != null) ? oldRecord.getGrantableUris() : null; |
| 5514 | |
| 5515 | // Shortcut when no Uris involved |
| 5516 | if (newUris == null && oldUris == null) { |
| 5517 | return; |
| 5518 | } |
| 5519 | |
| 5520 | // Inherit any existing owner |
| 5521 | IBinder permissionOwner = null; |
| 5522 | if (newRecord != null && permissionOwner == null) { |
| 5523 | permissionOwner = newRecord.permissionOwner; |
| 5524 | } |
| 5525 | if (oldRecord != null && permissionOwner == null) { |
| 5526 | permissionOwner = oldRecord.permissionOwner; |
| 5527 | } |
| 5528 | |
| 5529 | // If we have Uris to grant, but no owner yet, go create one |
| 5530 | if (newUris != null && permissionOwner == null) { |
| 5531 | try { |
| 5532 | if (DBG) Slog.d(TAG, key + ": creating owner"); |
| 5533 | permissionOwner = mAm.newUriPermissionOwner("NOTIF:" + key); |
| 5534 | } catch (RemoteException ignored) { |
| 5535 | // Ignored because we're in same process |
| 5536 | } |
| 5537 | } |
| 5538 | |
| 5539 | // If we have no Uris to grant, but an existing owner, go destroy it |
| 5540 | if (newUris == null && permissionOwner != null) { |
| 5541 | final long ident = Binder.clearCallingIdentity(); |
| 5542 | try { |
| 5543 | if (DBG) Slog.d(TAG, key + ": destroying owner"); |
| 5544 | mAm.revokeUriPermissionFromOwner(permissionOwner, null, ~0, |
| 5545 | UserHandle.getUserId(oldRecord.getUid())); |
| 5546 | permissionOwner = null; |
| 5547 | } catch (RemoteException ignored) { |
| 5548 | // Ignored because we're in same process |
| 5549 | } finally { |
| 5550 | Binder.restoreCallingIdentity(ident); |
| 5551 | } |
| 5552 | } |
| 5553 | |
| 5554 | // Grant access to new Uris |
| 5555 | if (newUris != null && permissionOwner != null) { |
| 5556 | for (int i = 0; i < newUris.size(); i++) { |
| 5557 | final Uri uri = newUris.valueAt(i); |
| 5558 | if (oldUris == null || !oldUris.contains(uri)) { |
| 5559 | if (DBG) Slog.d(TAG, key + ": granting " + uri); |
| 5560 | grantUriPermission(permissionOwner, uri, newRecord.getUid(), targetPkg, |
| 5561 | targetUserId); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 5562 | } |
| 5563 | } |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 5564 | } |
| 5565 | |
| 5566 | // Revoke access to old Uris |
| 5567 | if (oldUris != null && permissionOwner != null) { |
| 5568 | for (int i = 0; i < oldUris.size(); i++) { |
| 5569 | final Uri uri = oldUris.valueAt(i); |
| 5570 | if (newUris == null || !newUris.contains(uri)) { |
| 5571 | if (DBG) Slog.d(TAG, key + ": revoking " + uri); |
| 5572 | revokeUriPermission(permissionOwner, uri, oldRecord.getUid()); |
| 5573 | } |
| 5574 | } |
| 5575 | } |
| 5576 | |
| 5577 | if (newRecord != null) { |
| 5578 | newRecord.permissionOwner = permissionOwner; |
| 5579 | } |
| 5580 | } |
| 5581 | |
| 5582 | private void grantUriPermission(IBinder owner, Uri uri, int sourceUid, String targetPkg, |
| 5583 | int targetUserId) { |
| 5584 | if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return; |
| 5585 | |
| 5586 | final long ident = Binder.clearCallingIdentity(); |
| 5587 | try { |
| 5588 | mAm.grantUriPermissionFromOwner(owner, sourceUid, targetPkg, |
| 5589 | ContentProvider.getUriWithoutUserId(uri), |
| 5590 | Intent.FLAG_GRANT_READ_URI_PERMISSION, |
| 5591 | ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid)), |
| 5592 | targetUserId); |
| 5593 | } catch (RemoteException ignored) { |
| 5594 | // Ignored because we're in same process |
| 5595 | } finally { |
| 5596 | Binder.restoreCallingIdentity(ident); |
| 5597 | } |
| 5598 | } |
| 5599 | |
| 5600 | private void revokeUriPermission(IBinder owner, Uri uri, int sourceUid) { |
| 5601 | if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return; |
| 5602 | |
| 5603 | final long ident = Binder.clearCallingIdentity(); |
| 5604 | try { |
| 5605 | mAm.revokeUriPermissionFromOwner(owner, |
| 5606 | ContentProvider.getUriWithoutUserId(uri), |
| 5607 | Intent.FLAG_GRANT_READ_URI_PERMISSION, |
| 5608 | ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid))); |
| 5609 | } catch (RemoteException ignored) { |
| 5610 | // Ignored because we're in same process |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 5611 | } finally { |
| 5612 | Binder.restoreCallingIdentity(ident); |
| 5613 | } |
| 5614 | } |
| 5615 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5616 | /** |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 5617 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5618 | * and none of the {@code mustNotHaveFlags}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5619 | */ |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 5620 | void cancelNotification(final int callingUid, final int callingPid, |
| 5621 | final String pkg, final String tag, final int id, |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5622 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5623 | final int userId, final int reason, final ManagedServiceInfo listener) { |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5624 | cancelNotification(callingUid, callingPid, pkg, tag, id, mustHaveFlags, mustNotHaveFlags, |
| 5625 | sendDelete, userId, reason, -1 /* rank */, -1 /* count */, listener); |
| 5626 | } |
| 5627 | |
| 5628 | /** |
| 5629 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
| 5630 | * and none of the {@code mustNotHaveFlags}. |
| 5631 | */ |
| 5632 | void cancelNotification(final int callingUid, final int callingPid, |
| 5633 | final String pkg, final String tag, final int id, |
| 5634 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
| 5635 | final int userId, final int reason, int rank, int count, final ManagedServiceInfo listener) { |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 5636 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5637 | // In enqueueNotificationInternal notifications are added by scheduling the |
| 5638 | // work on the worker handler. Hence, we also schedule the cancel on this |
| 5639 | // handler to avoid a scenario where an add notification call followed by a |
| 5640 | // remove notification call ends up in not removing the notification. |
| 5641 | mHandler.post(new Runnable() { |
| 5642 | @Override |
| 5643 | public void run() { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5644 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Chris Wren | bddb5bc | 2015-03-04 08:47:46 -0800 | [diff] [blame] | 5645 | if (DBG) EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, id, tag, |
| 5646 | userId, mustHaveFlags, mustNotHaveFlags, reason, listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5647 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5648 | synchronized (mNotificationLock) { |
| 5649 | // Look for the notification, searching both the posted and enqueued lists. |
| 5650 | NotificationRecord r = findNotificationLocked(pkg, tag, id, userId); |
| 5651 | if (r != null) { |
| 5652 | // The notification was found, check if it should be removed. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5653 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5654 | // Ideally we'd do this in the caller of this method. However, that would |
| 5655 | // require the caller to also find the notification. |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 5656 | if (reason == REASON_CLICK) { |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5657 | mUsageStats.registerClickedByUser(r); |
| 5658 | } |
| 5659 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5660 | if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) { |
| 5661 | return; |
| 5662 | } |
| 5663 | if ((r.getNotification().flags & mustNotHaveFlags) != 0) { |
| 5664 | return; |
| 5665 | } |
| 5666 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5667 | // Cancel the notification. |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5668 | boolean wasPosted = removeFromNotificationListsLocked(r); |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5669 | cancelNotificationLocked(r, sendDelete, reason, rank, count, wasPosted, listenerName); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5670 | cancelGroupChildrenLocked(r, callingUid, callingPid, listenerName, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5671 | sendDelete, null); |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5672 | updateLightsLocked(); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 5673 | } else { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5674 | // No notification was found, assume that it is snoozed and cancel it. |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 5675 | if (reason != REASON_SNOOZED) { |
| 5676 | final boolean wasSnoozed = mSnoozeHelper.cancel(userId, pkg, tag, id); |
| 5677 | if (wasSnoozed) { |
| 5678 | savePolicyFile(); |
| 5679 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 5680 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5681 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5682 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5683 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5684 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5685 | } |
| 5686 | |
| 5687 | /** |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 5688 | * Determine whether the userId applies to the notification in question, either because |
| 5689 | * they match exactly, or one of them is USER_ALL (which is treated as a wildcard). |
| 5690 | */ |
| 5691 | private boolean notificationMatchesUserId(NotificationRecord r, int userId) { |
| 5692 | return |
| 5693 | // looking for USER_ALL notifications? match everything |
| 5694 | userId == UserHandle.USER_ALL |
| 5695 | // a notification sent to USER_ALL matches any query |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 5696 | || r.getUserId() == UserHandle.USER_ALL |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 5697 | // an exact user match |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 5698 | || r.getUserId() == userId; |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 5699 | } |
| 5700 | |
| 5701 | /** |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5702 | * Determine whether the userId applies to the notification in question, either because |
| 5703 | * they match exactly, or one of them is USER_ALL (which is treated as a wildcard) or |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 5704 | * because it matches one of the users profiles. |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5705 | */ |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 5706 | private boolean notificationMatchesCurrentProfiles(NotificationRecord r, int userId) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5707 | return notificationMatchesUserId(r, userId) |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 5708 | || mUserProfiles.isCurrentProfile(r.getUserId()); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5709 | } |
| 5710 | |
| 5711 | /** |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 5712 | * Cancels all notifications from a given package that have all of the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5713 | * {@code mustHaveFlags}. |
| 5714 | */ |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5715 | void cancelAllNotificationsInt(int callingUid, int callingPid, String pkg, String channelId, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 5716 | int mustHaveFlags, int mustNotHaveFlags, boolean doit, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5717 | ManagedServiceInfo listener) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5718 | mHandler.post(new Runnable() { |
| 5719 | @Override |
| 5720 | public void run() { |
| 5721 | String listenerName = listener == null ? null : listener.component.toShortString(); |
| 5722 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 5723 | pkg, userId, mustHaveFlags, mustNotHaveFlags, reason, |
| 5724 | listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5725 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5726 | // Why does this parameter exist? Do we actually want to execute the above if doit |
| 5727 | // is false? |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 5728 | if (!doit) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5729 | return; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 5730 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5731 | |
| 5732 | synchronized (mNotificationLock) { |
| 5733 | FlagChecker flagChecker = (int flags) -> { |
| 5734 | if ((flags & mustHaveFlags) != mustHaveFlags) { |
| 5735 | return false; |
| 5736 | } |
| 5737 | if ((flags & mustNotHaveFlags) != 0) { |
| 5738 | return false; |
| 5739 | } |
| 5740 | return true; |
| 5741 | }; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5742 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 5743 | pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, flagChecker, |
| 5744 | false /*includeCurrentProfiles*/, userId, false /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5745 | listenerName, true /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5746 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 5747 | callingPid, pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, |
| 5748 | flagChecker, false /*includeCurrentProfiles*/, userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5749 | false /*sendDelete*/, reason, listenerName, false /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5750 | mSnoozeHelper.cancel(userId, pkg); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5751 | } |
| 5752 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5753 | }); |
| 5754 | } |
| 5755 | |
| 5756 | private interface FlagChecker { |
| 5757 | // Returns false if these flags do not pass the defined flag test. |
| 5758 | public boolean apply(int flags); |
| 5759 | } |
| 5760 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5761 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5762 | private void cancelAllNotificationsByListLocked(ArrayList<NotificationRecord> notificationList, |
| 5763 | int callingUid, int callingPid, String pkg, boolean nullPkgIndicatesUserSwitch, |
| 5764 | String channelId, FlagChecker flagChecker, boolean includeCurrentProfiles, int userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5765 | boolean sendDelete, int reason, String listenerName, boolean wasPosted) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5766 | ArrayList<NotificationRecord> canceledNotifications = null; |
| 5767 | for (int i = notificationList.size() - 1; i >= 0; --i) { |
| 5768 | NotificationRecord r = notificationList.get(i); |
| 5769 | if (includeCurrentProfiles) { |
| 5770 | if (!notificationMatchesCurrentProfiles(r, userId)) { |
| 5771 | continue; |
| 5772 | } |
| 5773 | } else if (!notificationMatchesUserId(r, userId)) { |
| 5774 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5775 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5776 | // Don't remove notifications to all, if there's no package name specified |
| 5777 | if (nullPkgIndicatesUserSwitch && pkg == null && r.getUserId() == UserHandle.USER_ALL) { |
| 5778 | continue; |
| 5779 | } |
| 5780 | if (!flagChecker.apply(r.getFlags())) { |
| 5781 | continue; |
| 5782 | } |
| 5783 | if (pkg != null && !r.sbn.getPackageName().equals(pkg)) { |
| 5784 | continue; |
| 5785 | } |
| 5786 | if (channelId != null && !channelId.equals(r.getChannel().getId())) { |
| 5787 | continue; |
| 5788 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5789 | if (canceledNotifications == null) { |
| 5790 | canceledNotifications = new ArrayList<>(); |
| 5791 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5792 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 5793 | mNotificationsByKey.remove(r.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5794 | canceledNotifications.add(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5795 | cancelNotificationLocked(r, sendDelete, reason, wasPosted, listenerName); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5796 | } |
| 5797 | if (canceledNotifications != null) { |
| 5798 | final int M = canceledNotifications.size(); |
| 5799 | for (int i = 0; i < M; i++) { |
| 5800 | cancelGroupChildrenLocked(canceledNotifications.get(i), callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5801 | listenerName, false /* sendDelete */, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5802 | } |
| 5803 | updateLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5804 | } |
| 5805 | } |
| 5806 | |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 5807 | void snoozeNotificationInt(String key, long duration, String snoozeCriterionId, |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5808 | ManagedServiceInfo listener) { |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5809 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 5810 | if (duration <= 0 && snoozeCriterionId == null || key == null) { |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 5811 | return; |
| 5812 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 5813 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5814 | if (DBG) { |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 5815 | Slog.d(TAG, String.format("snooze event(%s, %d, %s, %s)", key, duration, |
| 5816 | snoozeCriterionId, listenerName)); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5817 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5818 | // Needs to post so that it can cancel notifications not yet enqueued. |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5819 | mHandler.post(new SnoozeNotificationRunnable(key, duration, snoozeCriterionId)); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 5820 | } |
| 5821 | |
| 5822 | void unsnoozeNotificationInt(String key, ManagedServiceInfo listener) { |
| 5823 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 5824 | if (DBG) { |
| 5825 | Slog.d(TAG, String.format("unsnooze event(%s, %s)", key, listenerName)); |
| 5826 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5827 | mSnoozeHelper.repost(key); |
| 5828 | savePolicyFile(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 5829 | } |
| 5830 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5831 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 350159c | 2014-03-27 11:15:11 -0700 | [diff] [blame] | 5832 | void cancelAllLocked(int callingUid, int callingPid, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5833 | ManagedServiceInfo listener, boolean includeCurrentProfiles) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5834 | mHandler.post(new Runnable() { |
| 5835 | @Override |
| 5836 | public void run() { |
| 5837 | synchronized (mNotificationLock) { |
| 5838 | String listenerName = |
| 5839 | listener == null ? null : listener.component.toShortString(); |
| 5840 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 5841 | null, userId, 0, 0, reason, listenerName); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5842 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5843 | FlagChecker flagChecker = (int flags) -> { |
| 5844 | if ((flags & (Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR)) |
| 5845 | != 0) { |
| 5846 | return false; |
| 5847 | } |
| 5848 | return true; |
| 5849 | }; |
| 5850 | |
| 5851 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 5852 | null, false /*nullPkgIndicatesUserSwitch*/, null, flagChecker, |
| 5853 | includeCurrentProfiles, userId, true /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5854 | listenerName, true); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5855 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 5856 | callingPid, null, false /*nullPkgIndicatesUserSwitch*/, null, |
| 5857 | flagChecker, includeCurrentProfiles, userId, true /*sendDelete*/, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5858 | reason, listenerName, false); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5859 | mSnoozeHelper.cancel(userId, includeCurrentProfiles); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5860 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5861 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5862 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5863 | } |
| 5864 | |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5865 | // Warning: The caller is responsible for invoking updateLightsLocked(). |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5866 | @GuardedBy("mNotificationLock") |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5867 | private void cancelGroupChildrenLocked(NotificationRecord r, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5868 | String listenerName, boolean sendDelete, FlagChecker flagChecker) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5869 | Notification n = r.getNotification(); |
Christoph Studer | 3f31f5d | 2014-07-31 16:55:32 +0200 | [diff] [blame] | 5870 | if (!n.isGroupSummary()) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5871 | return; |
| 5872 | } |
| 5873 | |
| 5874 | String pkg = r.sbn.getPackageName(); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5875 | |
| 5876 | if (pkg == null) { |
| 5877 | if (DBG) Log.e(TAG, "No package for group summary: " + r.getKey()); |
| 5878 | return; |
| 5879 | } |
| 5880 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5881 | cancelGroupChildrenByListLocked(mNotificationList, r, callingUid, callingPid, listenerName, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5882 | sendDelete, true, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5883 | cancelGroupChildrenByListLocked(mEnqueuedNotifications, r, callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5884 | listenerName, sendDelete, false, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5885 | } |
| 5886 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5887 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5888 | private void cancelGroupChildrenByListLocked(ArrayList<NotificationRecord> notificationList, |
| 5889 | NotificationRecord parentNotification, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5890 | String listenerName, boolean sendDelete, boolean wasPosted, FlagChecker flagChecker) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5891 | final String pkg = parentNotification.sbn.getPackageName(); |
| 5892 | final int userId = parentNotification.getUserId(); |
| 5893 | final int reason = REASON_GROUP_SUMMARY_CANCELED; |
| 5894 | for (int i = notificationList.size() - 1; i >= 0; i--) { |
| 5895 | final NotificationRecord childR = notificationList.get(i); |
| 5896 | final StatusBarNotification childSbn = childR.sbn; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5897 | if ((childSbn.isGroup() && !childSbn.getNotification().isGroupSummary()) && |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5898 | childR.getGroupKey().equals(parentNotification.getGroupKey()) |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 5899 | && (childR.getFlags() & FLAG_FOREGROUND_SERVICE) == 0 |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5900 | && (flagChecker == null || flagChecker.apply(childR.getFlags()))) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5901 | EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, childSbn.getId(), |
| 5902 | childSbn.getTag(), userId, 0, 0, reason, listenerName); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5903 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 5904 | mNotificationsByKey.remove(childR.getKey()); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5905 | cancelNotificationLocked(childR, sendDelete, reason, wasPosted, listenerName); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5906 | } |
| 5907 | } |
| 5908 | } |
| 5909 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5910 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5911 | void updateLightsLocked() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5912 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5913 | // handle notification lights |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 5914 | NotificationRecord ledNotification = null; |
| 5915 | while (ledNotification == null && !mLights.isEmpty()) { |
| 5916 | final String owner = mLights.get(mLights.size() - 1); |
| 5917 | ledNotification = mNotificationsByKey.get(owner); |
| 5918 | if (ledNotification == null) { |
| 5919 | Slog.wtfStack(TAG, "LED Notification does not exist: " + owner); |
| 5920 | mLights.remove(owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5921 | } |
| 5922 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 5923 | |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 5924 | // Don't flash while we are in a call or screen is on |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 5925 | if (ledNotification == null || mInCall || mScreenOn) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 5926 | mNotificationLight.turnOff(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5927 | } else { |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 5928 | NotificationRecord.Light light = ledNotification.getLight(); |
| 5929 | if (light != null && mNotificationPulseEnabled) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 5930 | // pulse repeatedly |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 5931 | mNotificationLight.setFlashing(light.color, Light.LIGHT_FLASH_TIMED, |
| 5932 | light.onMs, light.offMs); |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 5933 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5934 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5935 | } |
| 5936 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5937 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5938 | @NonNull List<NotificationRecord> findGroupNotificationsLocked(String pkg, |
| 5939 | String groupKey, int userId) { |
| 5940 | List<NotificationRecord> records = new ArrayList<>(); |
| 5941 | records.addAll(findGroupNotificationByListLocked(mNotificationList, pkg, groupKey, userId)); |
| 5942 | records.addAll( |
| 5943 | findGroupNotificationByListLocked(mEnqueuedNotifications, pkg, groupKey, userId)); |
| 5944 | return records; |
| 5945 | } |
| 5946 | |
| 5947 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5948 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5949 | private @NonNull List<NotificationRecord> findGroupNotificationByListLocked( |
| 5950 | ArrayList<NotificationRecord> list, String pkg, String groupKey, int userId) { |
| 5951 | List<NotificationRecord> records = new ArrayList<>(); |
| 5952 | final int len = list.size(); |
| 5953 | for (int i = 0; i < len; i++) { |
| 5954 | NotificationRecord r = list.get(i); |
| 5955 | if (notificationMatchesUserId(r, userId) && r.getGroupKey().equals(groupKey) |
| 5956 | && r.sbn.getPackageName().equals(pkg)) { |
| 5957 | records.add(r); |
| 5958 | } |
| 5959 | } |
| 5960 | return records; |
| 5961 | } |
| 5962 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5963 | // Searches both enqueued and posted notifications by key. |
| 5964 | // TODO: need to combine a bunch of these getters with slightly different behavior. |
| 5965 | // TODO: Should enqueuing just add to mNotificationsByKey instead? |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5966 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5967 | private NotificationRecord findNotificationByKeyLocked(String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5968 | NotificationRecord r; |
| 5969 | if ((r = findNotificationByListLocked(mNotificationList, key)) != null) { |
| 5970 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5971 | } |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5972 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, key)) != null) { |
| 5973 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5974 | } |
| 5975 | return null; |
| 5976 | } |
| 5977 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5978 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5979 | NotificationRecord findNotificationLocked(String pkg, String tag, int id, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5980 | NotificationRecord r; |
| 5981 | if ((r = findNotificationByListLocked(mNotificationList, pkg, tag, id, userId)) != null) { |
| 5982 | return r; |
| 5983 | } |
| 5984 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, pkg, tag, id, userId)) |
| 5985 | != null) { |
| 5986 | return r; |
| 5987 | } |
| 5988 | return null; |
| 5989 | } |
| 5990 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5991 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5992 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5993 | String pkg, String tag, int id, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5994 | final int len = list.size(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5995 | for (int i = 0; i < len; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5996 | NotificationRecord r = list.get(i); |
Vladimir Marko | 2526f33 | 2013-09-11 11:13:55 +0100 | [diff] [blame] | 5997 | if (notificationMatchesUserId(r, userId) && r.sbn.getId() == id && |
| 5998 | TextUtils.equals(r.sbn.getTag(), tag) && r.sbn.getPackageName().equals(pkg)) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5999 | return r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6000 | } |
| 6001 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6002 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6003 | } |
| 6004 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6005 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6006 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6007 | String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6008 | final int N = list.size(); |
| 6009 | for (int i = 0; i < N; i++) { |
| 6010 | if (key.equals(list.get(i).getKey())) { |
| 6011 | return list.get(i); |
| 6012 | } |
| 6013 | } |
| 6014 | return null; |
| 6015 | } |
| 6016 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6017 | @GuardedBy("mNotificationLock") |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 6018 | int indexOfNotificationLocked(String key) { |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 6019 | final int N = mNotificationList.size(); |
| 6020 | for (int i = 0; i < N; i++) { |
| 6021 | if (key.equals(mNotificationList.get(i).getKey())) { |
| 6022 | return i; |
| 6023 | } |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 6024 | } |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 6025 | return -1; |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 6026 | } |
| 6027 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6028 | @VisibleForTesting |
| 6029 | protected void hideNotificationsForPackages(String[] pkgs) { |
| 6030 | synchronized (mNotificationLock) { |
| 6031 | List<String> pkgList = Arrays.asList(pkgs); |
| 6032 | List<NotificationRecord> changedNotifications = new ArrayList<>(); |
| 6033 | int numNotifications = mNotificationList.size(); |
| 6034 | for (int i = 0; i < numNotifications; i++) { |
| 6035 | NotificationRecord rec = mNotificationList.get(i); |
| 6036 | if (pkgList.contains(rec.sbn.getPackageName())) { |
| 6037 | rec.setHidden(true); |
| 6038 | changedNotifications.add(rec); |
| 6039 | } |
| 6040 | } |
| 6041 | |
| 6042 | mListeners.notifyHiddenLocked(changedNotifications); |
| 6043 | } |
| 6044 | } |
| 6045 | |
| 6046 | @VisibleForTesting |
| 6047 | protected void unhideNotificationsForPackages(String[] pkgs) { |
| 6048 | synchronized (mNotificationLock) { |
| 6049 | List<String> pkgList = Arrays.asList(pkgs); |
| 6050 | List<NotificationRecord> changedNotifications = new ArrayList<>(); |
| 6051 | int numNotifications = mNotificationList.size(); |
| 6052 | for (int i = 0; i < numNotifications; i++) { |
| 6053 | NotificationRecord rec = mNotificationList.get(i); |
| 6054 | if (pkgList.contains(rec.sbn.getPackageName())) { |
| 6055 | rec.setHidden(false); |
| 6056 | changedNotifications.add(rec); |
| 6057 | } |
| 6058 | } |
| 6059 | |
| 6060 | mListeners.notifyUnhiddenLocked(changedNotifications); |
| 6061 | } |
| 6062 | } |
| 6063 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 6064 | private void updateNotificationPulse() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6065 | synchronized (mNotificationLock) { |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 6066 | updateLightsLocked(); |
| 6067 | } |
| 6068 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 6069 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 6070 | protected boolean isCallingUidSystem() { |
| 6071 | final int uid = Binder.getCallingUid(); |
| 6072 | return uid == Process.SYSTEM_UID; |
| 6073 | } |
| 6074 | |
| 6075 | protected boolean isUidSystemOrPhone(int uid) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6076 | final int appid = UserHandle.getAppId(uid); |
| 6077 | return (appid == Process.SYSTEM_UID || appid == Process.PHONE_UID || uid == 0); |
| 6078 | } |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 6079 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 6080 | // TODO: Most calls should probably move to isCallerSystem. |
| 6081 | protected boolean isCallerSystemOrPhone() { |
| 6082 | return isUidSystemOrPhone(Binder.getCallingUid()); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6083 | } |
| 6084 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6085 | private void checkCallerIsSystemOrShell() { |
| 6086 | if (Binder.getCallingUid() == Process.SHELL_UID) { |
| 6087 | return; |
| 6088 | } |
| 6089 | checkCallerIsSystem(); |
| 6090 | } |
| 6091 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6092 | private void checkCallerIsSystem() { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 6093 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6094 | return; |
| 6095 | } |
| 6096 | throw new SecurityException("Disallowed call for uid " + Binder.getCallingUid()); |
| 6097 | } |
| 6098 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6099 | private void checkCallerIsSystemOrSameApp(String pkg) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 6100 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6101 | return; |
| 6102 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 6103 | checkCallerIsSameApp(pkg); |
| 6104 | } |
| 6105 | |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 6106 | private boolean isCallerInstantApp(String pkg) { |
| 6107 | // System is always allowed to act for ephemeral apps. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 6108 | if (isCallerSystemOrPhone()) { |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 6109 | return false; |
| 6110 | } |
| 6111 | |
| 6112 | mAppOps.checkPackage(Binder.getCallingUid(), pkg); |
| 6113 | |
| 6114 | try { |
| 6115 | ApplicationInfo ai = mPackageManager.getApplicationInfo(pkg, 0, |
| 6116 | UserHandle.getCallingUserId()); |
| 6117 | if (ai == null) { |
| 6118 | throw new SecurityException("Unknown package " + pkg); |
| 6119 | } |
| 6120 | return ai.isInstantApp(); |
| 6121 | } catch (RemoteException re) { |
| 6122 | throw new SecurityException("Unknown package " + pkg, re); |
| 6123 | } |
| 6124 | |
| 6125 | } |
| 6126 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6127 | private void checkCallerIsSameApp(String pkg) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6128 | final int uid = Binder.getCallingUid(); |
| 6129 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6130 | ApplicationInfo ai = mPackageManager.getApplicationInfo( |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6131 | pkg, 0, UserHandle.getCallingUserId()); |
Dan Sandler | 09afc2e | 2014-07-18 14:29:20 -0400 | [diff] [blame] | 6132 | if (ai == null) { |
| 6133 | throw new SecurityException("Unknown package " + pkg); |
| 6134 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6135 | if (!UserHandle.isSameApp(ai.uid, uid)) { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6136 | throw new SecurityException("Calling uid " + uid + " gave package " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6137 | + pkg + " which is owned by uid " + ai.uid); |
| 6138 | } |
| 6139 | } catch (RemoteException re) { |
| 6140 | throw new SecurityException("Unknown package " + pkg + "\n" + re); |
| 6141 | } |
| 6142 | } |
| 6143 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 6144 | private static String callStateToString(int state) { |
| 6145 | switch (state) { |
| 6146 | case TelephonyManager.CALL_STATE_IDLE: return "CALL_STATE_IDLE"; |
| 6147 | case TelephonyManager.CALL_STATE_RINGING: return "CALL_STATE_RINGING"; |
| 6148 | case TelephonyManager.CALL_STATE_OFFHOOK: return "CALL_STATE_OFFHOOK"; |
| 6149 | default: return "CALL_STATE_UNKNOWN_" + state; |
| 6150 | } |
| 6151 | } |
| 6152 | |
| 6153 | private void listenForCallState() { |
| 6154 | TelephonyManager.from(getContext()).listen(new PhoneStateListener() { |
| 6155 | @Override |
| 6156 | public void onCallStateChanged(int state, String incomingNumber) { |
| 6157 | if (mCallState == state) return; |
| 6158 | if (DBG) Slog.d(TAG, "Call state changed: " + callStateToString(state)); |
| 6159 | mCallState = state; |
| 6160 | } |
| 6161 | }, PhoneStateListener.LISTEN_CALL_STATE); |
| 6162 | } |
| 6163 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6164 | /** |
| 6165 | * Generates a NotificationRankingUpdate from 'sbns', considering only |
| 6166 | * notifications visible to the given listener. |
| 6167 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6168 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6169 | private NotificationRankingUpdate makeRankingUpdateLocked(ManagedServiceInfo info) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6170 | final int N = mNotificationList.size(); |
| 6171 | ArrayList<String> keys = new ArrayList<String>(N); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 6172 | ArrayList<String> interceptedKeys = new ArrayList<String>(N); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6173 | ArrayList<Integer> importance = new ArrayList<>(N); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 6174 | Bundle overrideGroupKeys = new Bundle(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6175 | Bundle visibilityOverrides = new Bundle(); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 6176 | Bundle suppressedVisualEffects = new Bundle(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6177 | Bundle explanation = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 6178 | Bundle channels = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 6179 | Bundle overridePeople = new Bundle(); |
| 6180 | Bundle snoozeCriteria = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 6181 | Bundle showBadge = new Bundle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6182 | Bundle userSentiment = new Bundle(); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6183 | Bundle hidden = new Bundle(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6184 | for (int i = 0; i < N; i++) { |
| 6185 | NotificationRecord record = mNotificationList.get(i); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6186 | if (!isVisibleToListener(record.sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6187 | continue; |
| 6188 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6189 | final String key = record.sbn.getKey(); |
| 6190 | keys.add(key); |
| 6191 | importance.add(record.getImportance()); |
| 6192 | if (record.getImportanceExplanation() != null) { |
| 6193 | explanation.putCharSequence(key, record.getImportanceExplanation()); |
| 6194 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6195 | if (record.isIntercepted()) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6196 | interceptedKeys.add(key); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 6197 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6198 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6199 | suppressedVisualEffects.putInt(key, record.getSuppressedVisualEffects()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6200 | if (record.getPackageVisibilityOverride() |
| 6201 | != NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6202 | visibilityOverrides.putInt(key, record.getPackageVisibilityOverride()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6203 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 6204 | overrideGroupKeys.putString(key, record.sbn.getOverrideGroupKey()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 6205 | channels.putParcelable(key, record.getChannel()); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 6206 | overridePeople.putStringArrayList(key, record.getPeopleOverride()); |
| 6207 | snoozeCriteria.putParcelableArrayList(key, record.getSnoozeCriteria()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 6208 | showBadge.putBoolean(key, record.canShowBadge()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6209 | userSentiment.putInt(key, record.getUserSentiment()); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6210 | hidden.putBoolean(key, record.isHidden()); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6211 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6212 | final int M = keys.size(); |
| 6213 | String[] keysAr = keys.toArray(new String[M]); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 6214 | String[] interceptedKeysAr = interceptedKeys.toArray(new String[interceptedKeys.size()]); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6215 | int[] importanceAr = new int[M]; |
| 6216 | for (int i = 0; i < M; i++) { |
| 6217 | importanceAr[i] = importance.get(i); |
| 6218 | } |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6219 | return new NotificationRankingUpdate(keysAr, interceptedKeysAr, visibilityOverrides, |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 6220 | suppressedVisualEffects, importanceAr, explanation, overrideGroupKeys, |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6221 | channels, overridePeople, snoozeCriteria, showBadge, userSentiment, hidden); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6222 | } |
| 6223 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6224 | boolean hasCompanionDevice(ManagedServiceInfo info) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6225 | if (mCompanionManager == null) { |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 6226 | mCompanionManager = getCompanionManager(); |
| 6227 | } |
| 6228 | // Companion mgr doesn't exist on all device types |
| 6229 | if (mCompanionManager == null) { |
| 6230 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6231 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6232 | long identity = Binder.clearCallingIdentity(); |
| 6233 | try { |
| 6234 | List<String> associations = mCompanionManager.getAssociations( |
| 6235 | info.component.getPackageName(), info.userid); |
| 6236 | if (!ArrayUtils.isEmpty(associations)) { |
| 6237 | return true; |
| 6238 | } |
| 6239 | } catch (SecurityException se) { |
| 6240 | // Not a privileged listener |
| 6241 | } catch (RemoteException re) { |
| 6242 | Slog.e(TAG, "Cannot reach companion device service", re); |
| 6243 | } catch (Exception e) { |
| 6244 | Slog.e(TAG, "Cannot verify listener " + info, e); |
| 6245 | } finally { |
| 6246 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6247 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6248 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6249 | } |
| 6250 | |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 6251 | protected ICompanionDeviceManager getCompanionManager() { |
| 6252 | return ICompanionDeviceManager.Stub.asInterface( |
| 6253 | ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE)); |
| 6254 | } |
| 6255 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6256 | private boolean isVisibleToListener(StatusBarNotification sbn, ManagedServiceInfo listener) { |
| 6257 | if (!listener.enabledAndUserMatches(sbn.getUserId())) { |
| 6258 | return false; |
| 6259 | } |
Justin Koh | 8d11a5a | 2014-08-04 18:29:49 -0700 | [diff] [blame] | 6260 | // TODO: remove this for older listeners. |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6261 | return true; |
| 6262 | } |
| 6263 | |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 6264 | private boolean isPackageSuspendedForUser(String pkg, int uid) { |
Beverly | 2be7a05 | 2018-03-27 11:37:58 -0400 | [diff] [blame] | 6265 | final long identity = Binder.clearCallingIdentity(); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 6266 | int userId = UserHandle.getUserId(uid); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 6267 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6268 | return mPackageManager.isPackageSuspendedForUser(pkg, userId); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 6269 | } catch (RemoteException re) { |
| 6270 | throw new SecurityException("Could not talk to package manager service"); |
Andrei Stingaceanu | efc4a34 | 2016-03-22 14:43:01 +0000 | [diff] [blame] | 6271 | } catch (IllegalArgumentException ex) { |
| 6272 | // Package not found. |
| 6273 | return false; |
Beverly | 2be7a05 | 2018-03-27 11:37:58 -0400 | [diff] [blame] | 6274 | } finally { |
| 6275 | Binder.restoreCallingIdentity(identity); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 6276 | } |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 6277 | } |
| 6278 | |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 6279 | @VisibleForTesting |
| 6280 | boolean canUseManagedServices(String pkg) { |
| 6281 | boolean canUseManagedServices = !mActivityManager.isLowRamDevice() |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 6282 | || mPackageManagerClient.hasSystemFeature(PackageManager.FEATURE_WATCH); |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 6283 | |
| 6284 | for (String whitelisted : getContext().getResources().getStringArray( |
| 6285 | R.array.config_allowedManagedServicesOnLowRamDevices)) { |
| 6286 | if (whitelisted.equals(pkg)) { |
| 6287 | canUseManagedServices = true; |
| 6288 | } |
| 6289 | } |
| 6290 | |
| 6291 | return canUseManagedServices; |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 6292 | } |
| 6293 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6294 | private class TrimCache { |
| 6295 | StatusBarNotification heavy; |
| 6296 | StatusBarNotification sbnClone; |
| 6297 | StatusBarNotification sbnCloneLight; |
| 6298 | |
| 6299 | TrimCache(StatusBarNotification sbn) { |
| 6300 | heavy = sbn; |
| 6301 | } |
| 6302 | |
| 6303 | StatusBarNotification ForListener(ManagedServiceInfo info) { |
| 6304 | if (mListeners.getOnNotificationPostedTrim(info) == TRIM_LIGHT) { |
| 6305 | if (sbnCloneLight == null) { |
| 6306 | sbnCloneLight = heavy.cloneLight(); |
| 6307 | } |
| 6308 | return sbnCloneLight; |
| 6309 | } else { |
| 6310 | if (sbnClone == null) { |
| 6311 | sbnClone = heavy.clone(); |
| 6312 | } |
| 6313 | return sbnClone; |
| 6314 | } |
| 6315 | } |
| 6316 | } |
| 6317 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 6318 | public class NotificationAssistants extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6319 | static final String TAG_ENABLED_NOTIFICATION_ASSISTANTS = "enabled_assistants"; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6320 | |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 6321 | public NotificationAssistants(Context context, Object lock, UserProfiles up, |
| 6322 | IPackageManager pm) { |
| 6323 | super(context, lock, up, pm); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6324 | } |
| 6325 | |
| 6326 | @Override |
| 6327 | protected Config getConfig() { |
| 6328 | Config c = new Config(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6329 | c.caption = "notification assistant"; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 6330 | c.serviceInterface = NotificationAssistantService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6331 | c.xmlTag = TAG_ENABLED_NOTIFICATION_ASSISTANTS; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 6332 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_ASSISTANT; |
| 6333 | c.bindPermission = Manifest.permission.BIND_NOTIFICATION_ASSISTANT_SERVICE; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6334 | c.settingsAction = Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS; |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 6335 | c.clientLabel = R.string.notification_ranker_binding_label; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6336 | return c; |
| 6337 | } |
| 6338 | |
| 6339 | @Override |
| 6340 | protected IInterface asInterface(IBinder binder) { |
| 6341 | return INotificationListener.Stub.asInterface(binder); |
| 6342 | } |
| 6343 | |
| 6344 | @Override |
| 6345 | protected boolean checkType(IInterface service) { |
| 6346 | return service instanceof INotificationListener; |
| 6347 | } |
| 6348 | |
| 6349 | @Override |
| 6350 | protected void onServiceAdded(ManagedServiceInfo info) { |
| 6351 | mListeners.registerGuestService(info); |
| 6352 | } |
| 6353 | |
| 6354 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6355 | @GuardedBy("mNotificationLock") |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6356 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
| 6357 | mListeners.unregisterService(removed.service, removed.userid); |
| 6358 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6359 | |
Julia Reynolds | ef934fd | 2018-02-01 14:39:17 -0500 | [diff] [blame] | 6360 | @Override |
| 6361 | public void onUserUnlocked(int user) { |
| 6362 | if (DEBUG) Slog.d(TAG, "onUserUnlocked u=" + user); |
| 6363 | rebindServices(true); |
| 6364 | } |
| 6365 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6366 | public void onNotificationEnqueued(final NotificationRecord r) { |
| 6367 | final StatusBarNotification sbn = r.sbn; |
| 6368 | TrimCache trimCache = new TrimCache(sbn); |
| 6369 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6370 | // There should be only one, but it's a list, so while we enforce |
| 6371 | // singularity elsewhere, we keep it general here, to avoid surprises. |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6372 | for (final ManagedServiceInfo info : NotificationAssistants.this.getServices()) { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6373 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 6374 | if (!sbnVisible) { |
| 6375 | continue; |
| 6376 | } |
| 6377 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6378 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 6379 | mHandler.post(new Runnable() { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6380 | @Override |
| 6381 | public void run() { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 6382 | notifyEnqueued(info, sbnToPost); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6383 | } |
| 6384 | }); |
| 6385 | } |
| 6386 | } |
| 6387 | |
| 6388 | private void notifyEnqueued(final ManagedServiceInfo info, |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 6389 | final StatusBarNotification sbn) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 6390 | final INotificationListener assistant = (INotificationListener) info.service; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6391 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
| 6392 | try { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 6393 | assistant.onNotificationEnqueued(sbnHolder); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6394 | } catch (RemoteException ex) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 6395 | Log.e(TAG, "unable to notify assistant (enqueued): " + assistant, ex); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6396 | } |
| 6397 | } |
| 6398 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6399 | /** |
| 6400 | * asynchronously notify the assistant that a notification has been snoozed until a |
| 6401 | * context |
| 6402 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6403 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6404 | public void notifyAssistantSnoozedLocked(final StatusBarNotification sbn, |
| 6405 | final String snoozeCriterionId) { |
| 6406 | TrimCache trimCache = new TrimCache(sbn); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6407 | for (final ManagedServiceInfo info : getServices()) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6408 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 6409 | if (!sbnVisible) { |
| 6410 | continue; |
| 6411 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6412 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
| 6413 | mHandler.post(new Runnable() { |
| 6414 | @Override |
| 6415 | public void run() { |
| 6416 | final INotificationListener assistant = |
| 6417 | (INotificationListener) info.service; |
| 6418 | StatusBarNotificationHolder sbnHolder |
| 6419 | = new StatusBarNotificationHolder(sbnToPost); |
| 6420 | try { |
| 6421 | assistant.onNotificationSnoozedUntilContext( |
| 6422 | sbnHolder, snoozeCriterionId); |
| 6423 | } catch (RemoteException ex) { |
| 6424 | Log.e(TAG, "unable to notify assistant (snoozed): " + assistant, ex); |
| 6425 | } |
| 6426 | } |
| 6427 | }); |
| 6428 | } |
| 6429 | } |
| 6430 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6431 | public boolean isEnabled() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6432 | return !getServices().isEmpty(); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6433 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 6434 | |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 6435 | protected void ensureAssistant() { |
| 6436 | final List<UserInfo> activeUsers = mUm.getUsers(true); |
| 6437 | for (UserInfo userInfo : activeUsers) { |
| 6438 | int userId = userInfo.getUserHandle().getIdentifier(); |
| 6439 | if (getAllowedPackages(userId).isEmpty()) { |
| 6440 | Slog.d(TAG, "Approving default notification assistant for user " + userId); |
| 6441 | readDefaultAssistant(userId); |
| 6442 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 6443 | } |
| 6444 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6445 | } |
| 6446 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6447 | public class NotificationListeners extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6448 | static final String TAG_ENABLED_NOTIFICATION_LISTENERS = "enabled_listeners"; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6449 | |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6450 | private final ArraySet<ManagedServiceInfo> mLightTrimListeners = new ArraySet<>(); |
| 6451 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6452 | public NotificationListeners(IPackageManager pm) { |
| 6453 | super(getContext(), mNotificationLock, mUserProfiles, pm); |
| 6454 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6455 | } |
| 6456 | |
| 6457 | @Override |
| 6458 | protected Config getConfig() { |
| 6459 | Config c = new Config(); |
| 6460 | c.caption = "notification listener"; |
| 6461 | c.serviceInterface = NotificationListenerService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6462 | c.xmlTag = TAG_ENABLED_NOTIFICATION_LISTENERS; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6463 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS; |
| 6464 | c.bindPermission = android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE; |
| 6465 | c.settingsAction = Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS; |
| 6466 | c.clientLabel = R.string.notification_listener_binding_label; |
| 6467 | return c; |
| 6468 | } |
| 6469 | |
| 6470 | @Override |
| 6471 | protected IInterface asInterface(IBinder binder) { |
| 6472 | return INotificationListener.Stub.asInterface(binder); |
| 6473 | } |
| 6474 | |
| 6475 | @Override |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6476 | protected boolean checkType(IInterface service) { |
| 6477 | return service instanceof INotificationListener; |
| 6478 | } |
| 6479 | |
| 6480 | @Override |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 6481 | public void onServiceAdded(ManagedServiceInfo info) { |
| 6482 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6483 | final NotificationRankingUpdate update; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6484 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6485 | update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6486 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6487 | try { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6488 | listener.onListenerConnected(update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6489 | } catch (RemoteException e) { |
| 6490 | // we tried |
| 6491 | } |
| 6492 | } |
| 6493 | |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6494 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6495 | @GuardedBy("mNotificationLock") |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6496 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 6497 | if (removeDisabledHints(removed)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6498 | updateListenerHintsLocked(); |
Christoph Studer | 0d6ef4b | 2014-12-02 15:00:48 +0100 | [diff] [blame] | 6499 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6500 | } |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6501 | mLightTrimListeners.remove(removed); |
| 6502 | } |
| 6503 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6504 | @GuardedBy("mNotificationLock") |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6505 | public void setOnNotificationPostedTrimLocked(ManagedServiceInfo info, int trim) { |
| 6506 | if (trim == TRIM_LIGHT) { |
| 6507 | mLightTrimListeners.add(info); |
| 6508 | } else { |
| 6509 | mLightTrimListeners.remove(info); |
| 6510 | } |
| 6511 | } |
| 6512 | |
| 6513 | public int getOnNotificationPostedTrim(ManagedServiceInfo info) { |
| 6514 | return mLightTrimListeners.contains(info) ? TRIM_LIGHT : TRIM_FULL; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6515 | } |
| 6516 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6517 | /** |
| 6518 | * asynchronously notify all listeners about a new notification |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6519 | * |
| 6520 | * <p> |
| 6521 | * Also takes care of removing a notification that has been visible to a listener before, |
| 6522 | * but isn't anymore. |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6523 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6524 | @GuardedBy("mNotificationLock") |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6525 | public void notifyPostedLocked(NotificationRecord r, NotificationRecord old) { |
| 6526 | notifyPostedLocked(r, old, true); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6527 | } |
| 6528 | |
| 6529 | /** |
| 6530 | * @param notifyAllListeners notifies all listeners if true, else only notifies listeners |
| 6531 | * targetting <= O_MR1 |
| 6532 | */ |
| 6533 | @GuardedBy("mNotificationLock") |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6534 | private void notifyPostedLocked(NotificationRecord r, NotificationRecord old, |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6535 | boolean notifyAllListeners) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6536 | // Lazily initialized snapshots of the notification. |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 6537 | StatusBarNotification sbn = r.sbn; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6538 | StatusBarNotification oldSbn = (old != null) ? old.sbn : null; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6539 | TrimCache trimCache = new TrimCache(sbn); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6540 | |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6541 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6542 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 6543 | boolean oldSbnVisible = oldSbn != null ? isVisibleToListener(oldSbn, info) : false; |
| 6544 | // This notification hasn't been and still isn't visible -> ignore. |
| 6545 | if (!oldSbnVisible && !sbnVisible) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6546 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6547 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6548 | |
| 6549 | // If the notification is hidden, don't notifyPosted listeners targeting < P. |
| 6550 | // Instead, those listeners will receive notifyPosted when the notification is |
| 6551 | // unhidden. |
| 6552 | if (r.isHidden() && info.targetSdkVersion < Build.VERSION_CODES.P) { |
| 6553 | continue; |
| 6554 | } |
| 6555 | |
| 6556 | // If we shouldn't notify all listeners, this means the hidden state of |
| 6557 | // a notification was changed. Don't notifyPosted listeners targeting >= P. |
| 6558 | // Instead, those listeners will receive notifyRankingUpdate. |
| 6559 | if (!notifyAllListeners && info.targetSdkVersion >= Build.VERSION_CODES.P) { |
| 6560 | continue; |
| 6561 | } |
| 6562 | |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6563 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6564 | |
| 6565 | // This notification became invisible -> remove the old one. |
| 6566 | if (oldSbnVisible && !sbnVisible) { |
| 6567 | final StatusBarNotification oldSbnLightClone = oldSbn.cloneLight(); |
| 6568 | mHandler.post(new Runnable() { |
| 6569 | @Override |
| 6570 | public void run() { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6571 | notifyRemoved( |
| 6572 | info, oldSbnLightClone, update, null, REASON_USER_STOPPED); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6573 | } |
| 6574 | }); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6575 | continue; |
| 6576 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6577 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6578 | // Grant access before listener is notified |
| 6579 | final int targetUserId = (info.userid == UserHandle.USER_ALL) |
| 6580 | ? UserHandle.USER_SYSTEM : info.userid; |
| 6581 | updateUriPermissions(r, old, info.component.getPackageName(), targetUserId); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 6582 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6583 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6584 | mHandler.post(new Runnable() { |
| 6585 | @Override |
| 6586 | public void run() { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6587 | notifyPosted(info, sbnToPost, update); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6588 | } |
| 6589 | }); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6590 | } |
| 6591 | } |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6592 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6593 | /** |
| 6594 | * asynchronously notify all listeners about a removed notification |
| 6595 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6596 | @GuardedBy("mNotificationLock") |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6597 | public void notifyRemovedLocked(NotificationRecord r, int reason, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6598 | NotificationStats notificationStats) { |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6599 | final StatusBarNotification sbn = r.sbn; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6600 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6601 | // make a copy in case changes are made to the underlying Notification object |
| 6602 | // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the |
| 6603 | // notification |
| 6604 | final StatusBarNotification sbnLight = sbn.cloneLight(); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6605 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6606 | if (!isVisibleToListener(sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6607 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6608 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6609 | |
| 6610 | // don't notifyRemoved for listeners targeting < P |
| 6611 | // if not for reason package suspended |
| 6612 | if (r.isHidden() && reason != REASON_PACKAGE_SUSPENDED |
| 6613 | && info.targetSdkVersion < Build.VERSION_CODES.P) { |
| 6614 | continue; |
| 6615 | } |
| 6616 | |
| 6617 | // don't notifyRemoved for listeners targeting >= P |
| 6618 | // if the reason is package suspended |
| 6619 | if (reason == REASON_PACKAGE_SUSPENDED |
| 6620 | && info.targetSdkVersion >= Build.VERSION_CODES.P) { |
| 6621 | continue; |
| 6622 | } |
| 6623 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6624 | // Only assistants can get stats |
| 6625 | final NotificationStats stats = mAssistants.isServiceTokenValidLocked(info.service) |
| 6626 | ? notificationStats : null; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6627 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6628 | mHandler.post(new Runnable() { |
| 6629 | @Override |
| 6630 | public void run() { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6631 | notifyRemoved(info, sbnLight, update, stats, reason); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6632 | } |
| 6633 | }); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6634 | } |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6635 | |
| 6636 | // Revoke access after all listeners have been updated |
| 6637 | mHandler.post(() -> { |
| 6638 | updateUriPermissions(null, r, null, UserHandle.USER_SYSTEM); |
| 6639 | }); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6640 | } |
| 6641 | |
| 6642 | /** |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6643 | * Asynchronously notify all listeners about a reordering of notifications |
| 6644 | * unless changedHiddenNotifications is populated. |
| 6645 | * If changedHiddenNotifications is populated, there was a change in the hidden state |
| 6646 | * of the notifications. In this case, we only send updates to listeners that |
| 6647 | * target >= P. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6648 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6649 | @GuardedBy("mNotificationLock") |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6650 | public void notifyRankingUpdateLocked(List<NotificationRecord> changedHiddenNotifications) { |
| 6651 | boolean isHiddenRankingUpdate = changedHiddenNotifications != null |
| 6652 | && changedHiddenNotifications.size() > 0; |
| 6653 | |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6654 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6655 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 6656 | continue; |
| 6657 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6658 | |
| 6659 | boolean notifyThisListener = false; |
| 6660 | if (isHiddenRankingUpdate && serviceInfo.targetSdkVersion >= |
| 6661 | Build.VERSION_CODES.P) { |
| 6662 | for (NotificationRecord rec : changedHiddenNotifications) { |
| 6663 | if (isVisibleToListener(rec.sbn, serviceInfo)) { |
| 6664 | notifyThisListener = true; |
| 6665 | break; |
| 6666 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6667 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6668 | } |
| 6669 | |
| 6670 | if (notifyThisListener || !isHiddenRankingUpdate) { |
| 6671 | final NotificationRankingUpdate update = makeRankingUpdateLocked( |
| 6672 | serviceInfo); |
| 6673 | |
| 6674 | mHandler.post(new Runnable() { |
| 6675 | @Override |
| 6676 | public void run() { |
| 6677 | notifyRankingUpdate(serviceInfo, update); |
| 6678 | } |
| 6679 | }); |
| 6680 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6681 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6682 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6683 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6684 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6685 | public void notifyListenerHintsChangedLocked(final int hints) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6686 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6687 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 6688 | continue; |
| 6689 | } |
| 6690 | mHandler.post(new Runnable() { |
| 6691 | @Override |
| 6692 | public void run() { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6693 | notifyListenerHintsChanged(serviceInfo, hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6694 | } |
| 6695 | }); |
| 6696 | } |
| 6697 | } |
| 6698 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6699 | /** |
| 6700 | * asynchronously notify relevant listeners their notification is hidden |
| 6701 | * NotificationListenerServices that target P+: |
| 6702 | * NotificationListenerService#notifyRankingUpdateLocked() |
| 6703 | * NotificationListenerServices that target <= P: |
| 6704 | * NotificationListenerService#notifyRemovedLocked() with REASON_PACKAGE_SUSPENDED. |
| 6705 | */ |
| 6706 | @GuardedBy("mNotificationLock") |
| 6707 | public void notifyHiddenLocked(List<NotificationRecord> changedNotifications) { |
| 6708 | if (changedNotifications == null || changedNotifications.size() == 0) { |
| 6709 | return; |
| 6710 | } |
| 6711 | |
| 6712 | notifyRankingUpdateLocked(changedNotifications); |
| 6713 | |
| 6714 | // for listeners that target < P, notifyRemoveLocked |
| 6715 | int numChangedNotifications = changedNotifications.size(); |
| 6716 | for (int i = 0; i < numChangedNotifications; i++) { |
| 6717 | NotificationRecord rec = changedNotifications.get(i); |
| 6718 | mListeners.notifyRemovedLocked(rec, REASON_PACKAGE_SUSPENDED, rec.getStats()); |
| 6719 | } |
| 6720 | } |
| 6721 | |
| 6722 | /** |
| 6723 | * asynchronously notify relevant listeners their notification is unhidden |
| 6724 | * NotificationListenerServices that target P+: |
| 6725 | * NotificationListenerService#notifyRankingUpdateLocked() |
| 6726 | * NotificationListenerServices that target <= P: |
| 6727 | * NotificationListeners#notifyPostedLocked() |
| 6728 | */ |
| 6729 | @GuardedBy("mNotificationLock") |
| 6730 | public void notifyUnhiddenLocked(List<NotificationRecord> changedNotifications) { |
| 6731 | if (changedNotifications == null || changedNotifications.size() == 0) { |
| 6732 | return; |
| 6733 | } |
| 6734 | |
| 6735 | notifyRankingUpdateLocked(changedNotifications); |
| 6736 | |
| 6737 | // for listeners that target < P, notifyPostedLocked |
| 6738 | int numChangedNotifications = changedNotifications.size(); |
| 6739 | for (int i = 0; i < numChangedNotifications; i++) { |
| 6740 | NotificationRecord rec = changedNotifications.get(i); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6741 | mListeners.notifyPostedLocked(rec, rec, false); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6742 | } |
| 6743 | } |
| 6744 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 6745 | public void notifyInterruptionFilterChanged(final int interruptionFilter) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6746 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 6747 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 6748 | continue; |
| 6749 | } |
| 6750 | mHandler.post(new Runnable() { |
| 6751 | @Override |
| 6752 | public void run() { |
| 6753 | notifyInterruptionFilterChanged(serviceInfo, interruptionFilter); |
| 6754 | } |
| 6755 | }); |
| 6756 | } |
| 6757 | } |
| 6758 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6759 | protected void notifyNotificationChannelChanged(final String pkg, final UserHandle user, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6760 | final NotificationChannel channel, final int modificationType) { |
| 6761 | if (channel == null) { |
| 6762 | return; |
| 6763 | } |
| 6764 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6765 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6766 | continue; |
| 6767 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 6768 | |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 6769 | BackgroundThread.getHandler().post(() -> { |
| 6770 | if (hasCompanionDevice(serviceInfo)) { |
| 6771 | notifyNotificationChannelChanged( |
| 6772 | serviceInfo, pkg, user, channel, modificationType); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6773 | } |
| 6774 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6775 | } |
| 6776 | } |
| 6777 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6778 | protected void notifyNotificationChannelGroupChanged( |
| 6779 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
| 6780 | final int modificationType) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6781 | if (group == null) { |
| 6782 | return; |
| 6783 | } |
| 6784 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6785 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6786 | continue; |
| 6787 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 6788 | |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 6789 | BackgroundThread.getHandler().post(() -> { |
| 6790 | if (hasCompanionDevice(serviceInfo)) { |
| 6791 | notifyNotificationChannelGroupChanged( |
| 6792 | serviceInfo, pkg, user, group, modificationType); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6793 | } |
| 6794 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6795 | } |
| 6796 | } |
| 6797 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6798 | private void notifyPosted(final ManagedServiceInfo info, |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6799 | final StatusBarNotification sbn, NotificationRankingUpdate rankingUpdate) { |
Julia Reynolds | a75c752 | 2017-03-21 17:34:25 -0400 | [diff] [blame] | 6800 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6801 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6802 | try { |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6803 | listener.onNotificationPosted(sbnHolder, rankingUpdate); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6804 | } catch (RemoteException ex) { |
| 6805 | Log.e(TAG, "unable to notify listener (posted): " + listener, ex); |
| 6806 | } |
| 6807 | } |
| 6808 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6809 | private void notifyRemoved(ManagedServiceInfo info, StatusBarNotification sbn, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6810 | NotificationRankingUpdate rankingUpdate, NotificationStats stats, int reason) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6811 | if (!info.enabledAndUserMatches(sbn.getUserId())) { |
| 6812 | return; |
| 6813 | } |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6814 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6815 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6816 | try { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6817 | listener.onNotificationRemoved(sbnHolder, rankingUpdate, stats, reason); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6818 | } catch (RemoteException ex) { |
| 6819 | Log.e(TAG, "unable to notify listener (removed): " + listener, ex); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 6820 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6821 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6822 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6823 | private void notifyRankingUpdate(ManagedServiceInfo info, |
| 6824 | NotificationRankingUpdate rankingUpdate) { |
| 6825 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6826 | try { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6827 | listener.onNotificationRankingUpdate(rankingUpdate); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6828 | } catch (RemoteException ex) { |
| 6829 | Log.e(TAG, "unable to notify listener (ranking update): " + listener, ex); |
| 6830 | } |
| 6831 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6832 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6833 | private void notifyListenerHintsChanged(ManagedServiceInfo info, int hints) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6834 | final INotificationListener listener = (INotificationListener) info.service; |
| 6835 | try { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6836 | listener.onListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6837 | } catch (RemoteException ex) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6838 | Log.e(TAG, "unable to notify listener (listener hints): " + listener, ex); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6839 | } |
| 6840 | } |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 6841 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 6842 | private void notifyInterruptionFilterChanged(ManagedServiceInfo info, |
| 6843 | int interruptionFilter) { |
| 6844 | final INotificationListener listener = (INotificationListener) info.service; |
| 6845 | try { |
| 6846 | listener.onInterruptionFilterChanged(interruptionFilter); |
| 6847 | } catch (RemoteException ex) { |
| 6848 | Log.e(TAG, "unable to notify listener (interruption filter): " + listener, ex); |
| 6849 | } |
| 6850 | } |
| 6851 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6852 | void notifyNotificationChannelChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6853 | final String pkg, final UserHandle user, final NotificationChannel channel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6854 | final int modificationType) { |
| 6855 | final INotificationListener listener = (INotificationListener) info.service; |
| 6856 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6857 | listener.onNotificationChannelModification(pkg, user, channel, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6858 | } catch (RemoteException ex) { |
| 6859 | Log.e(TAG, "unable to notify listener (channel changed): " + listener, ex); |
| 6860 | } |
| 6861 | } |
| 6862 | |
| 6863 | private void notifyNotificationChannelGroupChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6864 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6865 | final int modificationType) { |
| 6866 | final INotificationListener listener = (INotificationListener) info.service; |
| 6867 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6868 | listener.onNotificationChannelGroupModification(pkg, user, group, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6869 | } catch (RemoteException ex) { |
| 6870 | Log.e(TAG, "unable to notify listener (channel group changed): " + listener, ex); |
| 6871 | } |
| 6872 | } |
| 6873 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6874 | public boolean isListenerPackage(String packageName) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 6875 | if (packageName == null) { |
| 6876 | return false; |
| 6877 | } |
| 6878 | // TODO: clean up locking object later |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6879 | synchronized (mNotificationLock) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6880 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 6881 | if (packageName.equals(serviceInfo.component.getPackageName())) { |
| 6882 | return true; |
| 6883 | } |
| 6884 | } |
| 6885 | } |
| 6886 | return false; |
| 6887 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6888 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6889 | |
| 6890 | public static final class DumpFilter { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6891 | public boolean filtered = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6892 | public String pkgFilter; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 6893 | public boolean zen; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 6894 | public long since; |
| 6895 | public boolean stats; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6896 | public boolean redact = true; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 6897 | public boolean proto = false; |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 6898 | public boolean criticalPriority = false; |
| 6899 | public boolean normalPriority = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6900 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 6901 | @NonNull |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6902 | public static DumpFilter parseFromArguments(String[] args) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6903 | final DumpFilter filter = new DumpFilter(); |
| 6904 | for (int ai = 0; ai < args.length; ai++) { |
| 6905 | final String a = args[ai]; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 6906 | if ("--proto".equals(a)) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 6907 | filter.proto = true; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 6908 | } else if ("--noredact".equals(a) || "--reveal".equals(a)) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6909 | filter.redact = false; |
| 6910 | } else if ("p".equals(a) || "pkg".equals(a) || "--package".equals(a)) { |
| 6911 | if (ai < args.length-1) { |
| 6912 | ai++; |
| 6913 | filter.pkgFilter = args[ai].trim().toLowerCase(); |
| 6914 | if (filter.pkgFilter.isEmpty()) { |
| 6915 | filter.pkgFilter = null; |
| 6916 | } else { |
| 6917 | filter.filtered = true; |
| 6918 | } |
| 6919 | } |
| 6920 | } else if ("--zen".equals(a) || "zen".equals(a)) { |
| 6921 | filter.filtered = true; |
| 6922 | filter.zen = true; |
| 6923 | } else if ("--stats".equals(a)) { |
| 6924 | filter.stats = true; |
| 6925 | if (ai < args.length-1) { |
| 6926 | ai++; |
Tobias Thierer | 28532d0 | 2016-04-21 14:52:10 +0100 | [diff] [blame] | 6927 | filter.since = Long.parseLong(args[ai]); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6928 | } else { |
| 6929 | filter.since = 0; |
| 6930 | } |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 6931 | } else if (PRIORITY_ARG.equals(a)) { |
| 6932 | // Bugreport will call the service twice with priority arguments, first to dump |
| 6933 | // critical sections and then non critical ones. Set approriate filters |
| 6934 | // to generate the desired data. |
| 6935 | if (ai < args.length - 1) { |
| 6936 | ai++; |
| 6937 | switch (args[ai]) { |
| 6938 | case PRIORITY_ARG_CRITICAL: |
| 6939 | filter.criticalPriority = true; |
| 6940 | break; |
| 6941 | case PRIORITY_ARG_NORMAL: |
| 6942 | filter.normalPriority = true; |
| 6943 | break; |
| 6944 | } |
| 6945 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6946 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6947 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6948 | return filter; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6949 | } |
| 6950 | |
| 6951 | public boolean matches(StatusBarNotification sbn) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6952 | if (!filtered) return true; |
| 6953 | return zen ? true : sbn != null |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 6954 | && (matches(sbn.getPackageName()) || matches(sbn.getOpPkg())); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6955 | } |
| 6956 | |
| 6957 | public boolean matches(ComponentName component) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6958 | if (!filtered) return true; |
| 6959 | return zen ? true : component != null && matches(component.getPackageName()); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6960 | } |
| 6961 | |
| 6962 | public boolean matches(String pkg) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6963 | if (!filtered) return true; |
| 6964 | return zen ? true : pkg != null && pkg.toLowerCase().contains(pkgFilter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 6965 | } |
| 6966 | |
| 6967 | @Override |
| 6968 | public String toString() { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 6969 | return stats ? "stats" : zen ? "zen" : ('\'' + pkgFilter + '\''); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6970 | } |
| 6971 | } |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6972 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6973 | @VisibleForTesting |
| 6974 | protected void simulatePackageSuspendBroadcast(boolean suspend, String pkg) { |
| 6975 | // only use for testing: mimic receive broadcast that package is (un)suspended |
| 6976 | // but does not actually (un)suspend the package |
| 6977 | final Bundle extras = new Bundle(); |
| 6978 | extras.putStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST, |
| 6979 | new String[]{pkg}); |
| 6980 | |
| 6981 | final String action = suspend ? Intent.ACTION_PACKAGES_SUSPENDED |
| 6982 | : Intent.ACTION_PACKAGES_UNSUSPENDED; |
| 6983 | final Intent intent = new Intent(action); |
| 6984 | intent.putExtras(extras); |
| 6985 | |
| 6986 | mPackageIntentReceiver.onReceive(getContext(), intent); |
| 6987 | } |
| 6988 | |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6989 | /** |
| 6990 | * Wrapper for a StatusBarNotification object that allows transfer across a oneway |
| 6991 | * binder without sending large amounts of data over a oneway transaction. |
| 6992 | */ |
| 6993 | private static final class StatusBarNotificationHolder |
| 6994 | extends IStatusBarNotificationHolder.Stub { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 6995 | private StatusBarNotification mValue; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6996 | |
| 6997 | public StatusBarNotificationHolder(StatusBarNotification value) { |
| 6998 | mValue = value; |
| 6999 | } |
| 7000 | |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 7001 | /** Get the held value and clear it. This function should only be called once per holder */ |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 7002 | @Override |
| 7003 | public StatusBarNotification get() { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 7004 | StatusBarNotification value = mValue; |
| 7005 | mValue = null; |
| 7006 | return value; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 7007 | } |
| 7008 | } |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7009 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7010 | private class ShellCmd extends ShellCommand { |
| 7011 | public static final String USAGE = "help\n" |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 7012 | + "allow_listener COMPONENT [user_id]\n" |
| 7013 | + "disallow_listener COMPONENT [user_id]\n" |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 7014 | + "allow_assistant COMPONENT\n" |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 7015 | + "remove_assistant COMPONENT\n" |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7016 | + "allow_dnd PACKAGE\n" |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7017 | + "disallow_dnd PACKAGE\n" |
| 7018 | + "suspend_package PACKAGE\n" |
| 7019 | + "unsuspend_package PACKAGE"; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7020 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7021 | @Override |
| 7022 | public int onCommand(String cmd) { |
Felipe Leme | 68d8041 | 2017-07-14 11:18:08 -0700 | [diff] [blame] | 7023 | if (cmd == null) { |
| 7024 | return handleDefaultCommands(cmd); |
| 7025 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 7026 | final PrintWriter pw = getOutPrintWriter(); |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 7027 | try { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7028 | switch (cmd) { |
| 7029 | case "allow_dnd": { |
| 7030 | getBinderService().setNotificationPolicyAccessGranted( |
| 7031 | getNextArgRequired(), true); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7032 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7033 | break; |
| 7034 | |
| 7035 | case "disallow_dnd": { |
| 7036 | getBinderService().setNotificationPolicyAccessGranted( |
| 7037 | getNextArgRequired(), false); |
| 7038 | } |
| 7039 | break; |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 7040 | case "allow_listener": { |
| 7041 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 7042 | if (cn == null) { |
| 7043 | pw.println("Invalid listener - must be a ComponentName"); |
| 7044 | return -1; |
| 7045 | } |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 7046 | String userId = getNextArg(); |
| 7047 | if (userId == null) { |
| 7048 | getBinderService().setNotificationListenerAccessGranted(cn, true); |
| 7049 | } else { |
| 7050 | getBinderService().setNotificationListenerAccessGrantedForUser( |
| 7051 | cn, Integer.parseInt(userId), true); |
| 7052 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 7053 | } |
| 7054 | break; |
| 7055 | case "disallow_listener": { |
| 7056 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 7057 | if (cn == null) { |
| 7058 | pw.println("Invalid listener - must be a ComponentName"); |
| 7059 | return -1; |
| 7060 | } |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 7061 | String userId = getNextArg(); |
| 7062 | if (userId == null) { |
| 7063 | getBinderService().setNotificationListenerAccessGranted(cn, false); |
| 7064 | } else { |
| 7065 | getBinderService().setNotificationListenerAccessGrantedForUser( |
| 7066 | cn, Integer.parseInt(userId), false); |
| 7067 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 7068 | } |
| 7069 | break; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 7070 | case "allow_assistant": { |
| 7071 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 7072 | if (cn == null) { |
| 7073 | pw.println("Invalid assistant - must be a ComponentName"); |
| 7074 | return -1; |
| 7075 | } |
| 7076 | getBinderService().setNotificationAssistantAccessGranted(cn, true); |
| 7077 | } |
| 7078 | break; |
| 7079 | case "disallow_assistant": { |
| 7080 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 7081 | if (cn == null) { |
| 7082 | pw.println("Invalid assistant - must be a ComponentName"); |
| 7083 | return -1; |
| 7084 | } |
| 7085 | getBinderService().setNotificationAssistantAccessGranted(cn, false); |
| 7086 | } |
| 7087 | break; |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7088 | case "suspend_package": { |
| 7089 | // only use for testing |
| 7090 | simulatePackageSuspendBroadcast(true, getNextArgRequired()); |
| 7091 | } |
| 7092 | break; |
| 7093 | case "unsuspend_package": { |
| 7094 | // only use for testing |
| 7095 | simulatePackageSuspendBroadcast(false, getNextArgRequired()); |
| 7096 | } |
| 7097 | break; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7098 | default: |
| 7099 | return handleDefaultCommands(cmd); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7100 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 7101 | } catch (Exception e) { |
| 7102 | pw.println("Error occurred. Check logcat for details. " + e.getMessage()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7103 | Slog.e(TAG, "Error running shell command", e); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7104 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7105 | return 0; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7106 | } |
| 7107 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7108 | @Override |
| 7109 | public void onHelp() { |
| 7110 | getOutPrintWriter().println(USAGE); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7111 | } |
| 7112 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7113 | } |