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 | |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 19 | import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 20 | import static android.app.Notification.CATEGORY_CALL; |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 21 | import static android.app.Notification.FLAG_AUTOGROUP_SUMMARY; |
| 22 | import static android.app.Notification.FLAG_BUBBLE; |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 23 | import static android.app.Notification.FLAG_FOREGROUND_SERVICE; |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 24 | import static android.app.Notification.FLAG_NO_CLEAR; |
| 25 | import static android.app.Notification.FLAG_ONGOING_EVENT; |
Mady Mellor | 66efd5e | 2019-05-15 13:38:11 -0700 | [diff] [blame] | 26 | import static android.app.Notification.FLAG_ONLY_ALERT_ONCE; |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 27 | import static android.app.NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 28 | import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED; |
| 29 | import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED; |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 30 | import static android.app.NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 31 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 32 | import static android.app.NotificationManager.IMPORTANCE_MIN; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 33 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 34 | import static android.app.NotificationManager.INTERRUPTION_FILTER_PRIORITY; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 35 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECTS_UNSET; |
| 36 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT; |
| 37 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE; |
| 38 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 39 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS; |
| 40 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST; |
| 41 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK; |
| 42 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF; |
| 43 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON; |
| 44 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_STATUS_BAR; |
Amith Yamasani | e5bfeee | 2018-09-05 18:52:35 -0700 | [diff] [blame] | 45 | import static android.content.Context.BIND_ADJUST_BELOW_PERCEPTIBLE; |
| 46 | import static android.content.Context.BIND_ALLOW_WHITELIST_MANAGEMENT; |
| 47 | import static android.content.Context.BIND_AUTO_CREATE; |
| 48 | import static android.content.Context.BIND_FOREGROUND_SERVICE; |
Mady Mellor | ca0c24c | 2019-05-16 16:14:32 -0700 | [diff] [blame] | 49 | import static android.content.pm.ActivityInfo.DOCUMENT_LAUNCH_ALWAYS; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 50 | import static android.content.pm.PackageManager.FEATURE_LEANBACK; |
| 51 | import static android.content.pm.PackageManager.FEATURE_TELEVISION; |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 52 | import static android.content.pm.PackageManager.MATCH_ALL; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 53 | import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_AWARE; |
| 54 | import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE; |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 55 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 56 | import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_CRITICAL; |
| 57 | import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_NORMAL; |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 58 | import static android.os.UserHandle.USER_NULL; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 59 | import static android.os.UserHandle.USER_SYSTEM; |
Gus Prevas | 7306b90 | 2018-12-11 10:57:06 -0500 | [diff] [blame] | 60 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 61 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_EFFECTS; |
Gus Prevas | 7306b90 | 2018-12-11 10:57:06 -0500 | [diff] [blame] | 62 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_NOTIFICATION_EFFECTS; |
| 63 | import static android.service.notification.NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_ADDED; |
| 64 | import static android.service.notification.NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_DELETED; |
| 65 | import static android.service.notification.NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 66 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL; |
| 67 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL; |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 68 | import static android.service.notification.NotificationListenerService.REASON_CANCEL; |
| 69 | import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 70 | import static android.service.notification.NotificationListenerService.REASON_CHANNEL_BANNED; |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 71 | import static android.service.notification.NotificationListenerService.REASON_CLICK; |
| 72 | import static android.service.notification.NotificationListenerService.REASON_ERROR; |
Gus Prevas | 7306b90 | 2018-12-11 10:57:06 -0500 | [diff] [blame] | 73 | import static android.service.notification.NotificationListenerService.REASON_GROUP_SUMMARY_CANCELED; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 74 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL; |
| 75 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL_ALL; |
| 76 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_BANNED; |
| 77 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_CHANGED; |
| 78 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_SUSPENDED; |
| 79 | import static android.service.notification.NotificationListenerService.REASON_PROFILE_TURNED_OFF; |
| 80 | import static android.service.notification.NotificationListenerService.REASON_SNOOZED; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 81 | import static android.service.notification.NotificationListenerService.REASON_TIMEOUT; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 82 | import static android.service.notification.NotificationListenerService.REASON_UNAUTOBUNDLED; |
| 83 | import static android.service.notification.NotificationListenerService.REASON_USER_STOPPED; |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 84 | import static android.service.notification.NotificationListenerService.TRIM_FULL; |
| 85 | import static android.service.notification.NotificationListenerService.TRIM_LIGHT; |
Mady Mellor | ca0c24c | 2019-05-16 16:14:32 -0700 | [diff] [blame] | 86 | import static android.util.StatsLogInternal.BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING; |
| 87 | import static android.util.StatsLogInternal.BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE; |
| 88 | import static android.util.StatsLogInternal.BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__DOCUMENT_LAUNCH_NOT_ALWAYS; |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 89 | import static android.view.WindowManager.LayoutParams.TYPE_TOAST; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 90 | |
Michal Karpinski | ac116df | 2018-12-10 17:51:42 +0000 | [diff] [blame] | 91 | import static com.android.server.am.PendingIntentRecord.FLAG_ACTIVITY_SENDER; |
| 92 | import static com.android.server.am.PendingIntentRecord.FLAG_BROADCAST_SENDER; |
Michal Karpinski | c8aa91b | 2019-01-10 16:45:59 +0000 | [diff] [blame] | 93 | import static com.android.server.am.PendingIntentRecord.FLAG_SERVICE_SENDER; |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 94 | import static com.android.server.utils.PriorityDump.PRIORITY_ARG; |
| 95 | import static com.android.server.utils.PriorityDump.PRIORITY_ARG_CRITICAL; |
| 96 | import static com.android.server.utils.PriorityDump.PRIORITY_ARG_NORMAL; |
| 97 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 98 | import android.Manifest; |
Zimuzo | b3b9c26 | 2018-10-31 11:54:20 +0000 | [diff] [blame] | 99 | import android.Manifest.permission; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 100 | import android.annotation.CallbackExecutor; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 101 | import android.annotation.NonNull; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 102 | import android.annotation.Nullable; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 103 | import android.app.ActivityManager; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 104 | import android.app.ActivityManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 105 | import android.app.AlarmManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 106 | import android.app.AppGlobals; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 107 | import android.app.AppOpsManager; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 108 | import android.app.AutomaticZenRule; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | import android.app.IActivityManager; |
| 110 | import android.app.INotificationManager; |
| 111 | import android.app.ITransientNotification; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 112 | import android.app.IUriGrantsManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | import android.app.Notification; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 114 | import android.app.NotificationChannel; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 115 | import android.app.NotificationChannelGroup; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 116 | import android.app.NotificationManager; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 117 | import android.app.NotificationManager.Policy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | import android.app.PendingIntent; |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 119 | import android.app.Person; |
Mady Mellor | a10448e | 2019-04-26 13:50:58 -0700 | [diff] [blame] | 120 | import android.app.RemoteInput; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | import android.app.StatusBarManager; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 122 | import android.app.UriGrantsManager; |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 123 | import android.app.admin.DeviceAdminInfo; |
| 124 | import android.app.admin.DevicePolicyManagerInternal; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 125 | import android.app.backup.BackupManager; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 126 | import android.app.role.OnRoleHoldersChangedListener; |
| 127 | import android.app.role.RoleManager; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 128 | import android.app.usage.UsageEvents; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 129 | import android.app.usage.UsageStatsManagerInternal; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 130 | import android.companion.ICompanionDeviceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | import android.content.BroadcastReceiver; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 132 | import android.content.ComponentName; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 133 | import android.content.ContentProvider; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 134 | import android.content.ContentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | import android.content.Context; |
| 136 | import android.content.Intent; |
| 137 | import android.content.IntentFilter; |
Mady Mellor | ca0c24c | 2019-05-16 16:14:32 -0700 | [diff] [blame] | 138 | import android.content.pm.ActivityInfo; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 139 | import android.content.pm.ApplicationInfo; |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 140 | import android.content.pm.IPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | import android.content.pm.PackageManager; |
| 142 | import android.content.pm.PackageManager.NameNotFoundException; |
Julia Reynolds | b40cd09 | 2019-01-25 09:35:02 -0500 | [diff] [blame] | 143 | import android.content.pm.PackageManagerInternal; |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 144 | import android.content.pm.ParceledListSlice; |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 145 | import android.content.pm.UserInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | import android.content.res.Resources; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 147 | import android.database.ContentObserver; |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 148 | import android.media.AudioAttributes; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 149 | import android.media.AudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 150 | import android.media.AudioManagerInternal; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 151 | import android.media.IRingtonePlayer; |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 152 | import android.metrics.LogMaker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | import android.os.Binder; |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 155 | import android.os.Build; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 156 | import android.os.Bundle; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 157 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | import android.os.Handler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 159 | import android.os.HandlerThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | import android.os.IBinder; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 161 | import android.os.IDeviceIdleController; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 162 | import android.os.IInterface; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 163 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | import android.os.Message; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 165 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 166 | import android.os.RemoteException; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 167 | import android.os.ResultReceiver; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 168 | import android.os.ServiceManager; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 169 | import android.os.ShellCallback; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 170 | import android.os.SystemClock; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 171 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 172 | import android.os.UserHandle; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 173 | import android.os.UserManager; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 174 | import android.os.VibrationEffect; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 175 | import android.os.Vibrator; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 176 | import android.provider.DeviceConfig; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | import android.provider.Settings; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 178 | import android.service.notification.Adjustment; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 179 | import android.service.notification.Condition; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 180 | import android.service.notification.IConditionProvider; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 181 | import android.service.notification.INotificationListener; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 182 | import android.service.notification.IStatusBarNotificationHolder; |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 183 | import android.service.notification.ListenersDisablingEffectsProto; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 184 | import android.service.notification.NotificationAssistantService; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 185 | import android.service.notification.NotificationListenerService; |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 186 | import android.service.notification.NotificationRankingUpdate; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 187 | import android.service.notification.NotificationRecordProto; |
| 188 | import android.service.notification.NotificationServiceDumpProto; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 189 | import android.service.notification.NotificationStats; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 190 | import android.service.notification.SnoozeCriterion; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 191 | import android.service.notification.StatusBarNotification; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 192 | import android.service.notification.ZenModeConfig; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 193 | import android.service.notification.ZenModeProto; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 194 | import android.telephony.PhoneStateListener; |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 195 | import android.telephony.TelephonyManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 196 | import android.text.TextUtils; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 197 | import android.util.ArrayMap; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 198 | import android.util.ArraySet; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 199 | import android.util.AtomicFile; |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 200 | import android.util.IntArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | import android.util.Log; |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 202 | import android.util.Pair; |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 203 | import android.util.Slog; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 204 | import android.util.SparseArray; |
Mady Mellor | ca0c24c | 2019-05-16 16:14:32 -0700 | [diff] [blame] | 205 | import android.util.StatsLog; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 206 | import android.util.Xml; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 207 | import android.util.proto.ProtoOutputStream; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 208 | import android.view.accessibility.AccessibilityEvent; |
| 209 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | import android.widget.Toast; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 211 | |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 212 | import com.android.internal.R; |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 213 | import com.android.internal.annotations.GuardedBy; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 214 | import com.android.internal.annotations.VisibleForTesting; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 215 | import com.android.internal.config.sysui.SystemUiDeviceConfigFlags; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 216 | import com.android.internal.logging.MetricsLogger; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 217 | import com.android.internal.logging.nano.MetricsProto; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 218 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Beverly | 70dcd00 | 2018-03-29 17:09:16 -0400 | [diff] [blame] | 219 | import com.android.internal.notification.SystemNotificationChannels; |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 220 | import com.android.internal.os.BackgroundThread; |
Julia Reynolds | b040559 | 2018-11-26 17:01:13 -0500 | [diff] [blame] | 221 | import com.android.internal.os.SomeArgs; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 222 | import com.android.internal.statusbar.NotificationVisibility; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 223 | import com.android.internal.util.ArrayUtils; |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 224 | import com.android.internal.util.CollectionUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 225 | import com.android.internal.util.DumpUtils; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 226 | import com.android.internal.util.FastXmlSerializer; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 227 | import com.android.internal.util.Preconditions; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 228 | import com.android.internal.util.XmlUtils; |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 229 | import com.android.internal.util.function.TriPredicate; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 230 | import com.android.server.DeviceIdleController; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 231 | import com.android.server.EventLogTags; |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 232 | import com.android.server.IoThread; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 233 | import com.android.server.LocalServices; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 234 | import com.android.server.SystemService; |
| 235 | import com.android.server.lights.Light; |
| 236 | import com.android.server.lights.LightsManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 237 | import com.android.server.notification.ManagedServices.ManagedServiceInfo; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 238 | import com.android.server.notification.ManagedServices.UserProfiles; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 239 | import com.android.server.pm.PackageManagerService; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 240 | import com.android.server.policy.PhoneWindowManager; |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 241 | import com.android.server.statusbar.StatusBarManagerInternal; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 242 | import com.android.server.uri.UriGrantsManagerInternal; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 243 | import com.android.server.wm.WindowManagerInternal; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 244 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 245 | import libcore.io.IoUtils; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 246 | |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 247 | import org.json.JSONException; |
| 248 | import org.json.JSONObject; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 249 | import org.xmlpull.v1.XmlPullParser; |
| 250 | import org.xmlpull.v1.XmlPullParserException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 251 | import org.xmlpull.v1.XmlSerializer; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 252 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 253 | import java.io.ByteArrayInputStream; |
| 254 | import java.io.ByteArrayOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 255 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | import java.io.FileDescriptor; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 257 | import java.io.FileNotFoundException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 258 | import java.io.FileOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 259 | import java.io.IOException; |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 260 | import java.io.InputStream; |
| 261 | import java.io.OutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 263 | import java.nio.charset.StandardCharsets; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 264 | import java.util.ArrayDeque; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | import java.util.ArrayList; |
Michael Wright | bc4d0d9 | 2017-03-23 18:57:57 +0000 | [diff] [blame] | 266 | import java.util.Arrays; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 267 | import java.util.Iterator; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 268 | import java.util.List; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 269 | import java.util.Map.Entry; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 270 | import java.util.Objects; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 271 | import java.util.Set; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 272 | import java.util.concurrent.Executor; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 273 | import java.util.concurrent.TimeUnit; |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 274 | import java.util.function.BiConsumer; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 275 | |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 276 | /** {@hide} */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 277 | public class NotificationManagerService extends SystemService { |
| 278 | static final String TAG = "NotificationService"; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 279 | static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Selim Cinek | 4041249 | 2015-12-08 18:03:22 -0800 | [diff] [blame] | 280 | public static final boolean ENABLE_CHILD_NOTIFICATIONS |
| 281 | = SystemProperties.getBoolean("debug.child_notifs", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 283 | static final boolean DEBUG_INTERRUPTIVENESS = SystemProperties.getBoolean( |
| 284 | "debug.notification.interruptiveness", false); |
| 285 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 286 | static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 287 | static final float DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE = 5f; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 288 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | // message codes |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 290 | static final int MESSAGE_DURATION_REACHED = 2; |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 291 | // 3: removed to a different handler |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 292 | static final int MESSAGE_SEND_RANKING_UPDATE = 4; |
| 293 | static final int MESSAGE_LISTENER_HINTS_CHANGED = 5; |
| 294 | static final int MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED = 6; |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 295 | static final int MESSAGE_FINISH_TOKEN_TIMEOUT = 7; |
Julia Reynolds | b040559 | 2018-11-26 17:01:13 -0500 | [diff] [blame] | 296 | static final int MESSAGE_ON_PACKAGE_CHANGED = 8; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 297 | |
| 298 | // ranking thread messages |
| 299 | private static final int MESSAGE_RECONSIDER_RANKING = 1000; |
| 300 | private static final int MESSAGE_RANKING_SORT = 1001; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 302 | static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 303 | static final int SHORT_DELAY = 2000; // 2 seconds |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 304 | |
Robert Carr | 3406d46 | 2018-03-15 16:19:07 -0700 | [diff] [blame] | 305 | // 1 second past the ANR timeout. |
| 306 | static final int FINISH_TOKEN_TIMEOUT = 11 * 1000; |
| 307 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 308 | static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 309 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 310 | static final long SNOOZE_UNTIL_UNSPECIFIED = -1; |
| 311 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 312 | 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] | 313 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 314 | static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION; |
Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 315 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 316 | static final boolean ENABLE_BLOCKED_TOASTS = true; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 317 | |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 318 | static final String[] DEFAULT_ALLOWED_ADJUSTMENTS = new String[] { |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 319 | Adjustment.KEY_CONTEXTUAL_ACTIONS, |
Julia Reynolds | 6650004 | 2019-04-19 11:23:55 -0400 | [diff] [blame] | 320 | Adjustment.KEY_TEXT_REPLIES}; |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 321 | |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 322 | static final String[] NON_BLOCKABLE_DEFAULT_ROLES = new String[] { |
| 323 | RoleManager.ROLE_DIALER, |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 324 | RoleManager.ROLE_EMERGENCY |
| 325 | }; |
| 326 | |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 327 | // When #matchesCallFilter is called from the ringer, wait at most |
| 328 | // 3s to resolve the contacts. This timeout is required since |
| 329 | // ContactsProvider might take a long time to start up. |
| 330 | // |
| 331 | // Return STARRED_CONTACT when the timeout is hit in order to avoid |
| 332 | // missed calls in ZEN mode "Important". |
| 333 | static final int MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS = 3000; |
| 334 | static final float MATCHES_CALL_FILTER_TIMEOUT_AFFINITY = |
| 335 | ValidateNotificationPeople.STARRED_CONTACT; |
| 336 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 337 | /** notification_enqueue status value for a newly enqueued notification. */ |
| 338 | private static final int EVENTLOG_ENQUEUE_STATUS_NEW = 0; |
| 339 | |
| 340 | /** notification_enqueue status value for an existing notification. */ |
| 341 | private static final int EVENTLOG_ENQUEUE_STATUS_UPDATE = 1; |
| 342 | |
| 343 | /** notification_enqueue status value for an ignored notification. */ |
| 344 | private static final int EVENTLOG_ENQUEUE_STATUS_IGNORED = 2; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 345 | private static final long MIN_PACKAGE_OVERRATE_LOG_INTERVAL = 5000; // milliseconds |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 346 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 347 | private static final long DELAY_FOR_ASSISTANT_TIME = 100; |
| 348 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 349 | private static final String ACTION_NOTIFICATION_TIMEOUT = |
| 350 | NotificationManagerService.class.getSimpleName() + ".TIMEOUT"; |
| 351 | private static final int REQUEST_CODE_TIMEOUT = 1; |
| 352 | private static final String SCHEME_TIMEOUT = "timeout"; |
| 353 | private static final String EXTRA_KEY = "key"; |
| 354 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 355 | private IActivityManager mAm; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 356 | private ActivityManager mActivityManager; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 357 | private IPackageManager mPackageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 358 | private PackageManager mPackageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 359 | AudioManager mAudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 360 | AudioManagerInternal mAudioManagerInternal; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 361 | @Nullable StatusBarManagerInternal mStatusBar; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 362 | Vibrator mVibrator; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 363 | private WindowManagerInternal mWindowManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 364 | private AlarmManager mAlarmManager; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 365 | private ICompanionDeviceManager mCompanionManager; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 366 | private AccessibilityManager mAccessibilityManager; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 367 | private IDeviceIdleController mDeviceIdleController; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 368 | private IUriGrantsManager mUgm; |
| 369 | private UriGrantsManagerInternal mUgmInternal; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 370 | private RoleObserver mRoleObserver; |
| 371 | private UserManager mUm; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 372 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | final IBinder mForegroundToken = new Binder(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 374 | private WorkerHandler mHandler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 375 | private final HandlerThread mRankingThread = new HandlerThread("ranker", |
| 376 | Process.THREAD_PRIORITY_BACKGROUND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 378 | private Light mNotificationLight; |
| 379 | Light mAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 380 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 381 | private long[] mFallbackVibrationPattern; |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 382 | private boolean mUseAttentionLight; |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 383 | boolean mHasLight = true; |
| 384 | boolean mLightEnabled; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 385 | boolean mSystemReady; |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 386 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 387 | private boolean mDisableNotificationEffects; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 388 | private int mCallState; |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 389 | private String mSoundNotificationKey; |
| 390 | private String mVibrateNotificationKey; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 392 | private final SparseArray<ArraySet<ComponentName>> mListenersDisablingEffects = |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 393 | new SparseArray<>(); |
| 394 | private List<ComponentName> mEffectsSuppressors = new ArrayList<>(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 395 | private int mListenerHints; // right now, all hints are global |
John Spurlock | 8310410 | 2015-02-12 23:25:12 -0500 | [diff] [blame] | 396 | private int mInterruptionFilter = NotificationListenerService.INTERRUPTION_FILTER_UNKNOWN; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 397 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 398 | // for enabling and disabling notification pulse behavior |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 399 | boolean mScreenOn = true; |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 400 | protected boolean mInCall = false; |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 401 | boolean mNotificationPulseEnabled; |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 402 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 403 | private Uri mInCallNotificationUri; |
| 404 | private AudioAttributes mInCallNotificationAudioAttributes; |
| 405 | private float mInCallNotificationVolume; |
luochaojiang | 50e5273c | 2018-04-16 16:55:03 +0800 | [diff] [blame] | 406 | private Binder mCallNotificationToken = null; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 407 | |
Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 408 | // used as a mutex for access to all active notifications & listeners |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 409 | final Object mNotificationLock = new Object(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 410 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 411 | final ArrayList<NotificationRecord> mNotificationList = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 412 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 413 | final ArrayMap<String, NotificationRecord> mNotificationsByKey = new ArrayMap<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 414 | @GuardedBy("mNotificationLock") |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 415 | final ArrayList<NotificationRecord> mEnqueuedNotifications = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 416 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 417 | final ArrayMap<Integer, ArrayMap<String, String>> mAutobundledSummaries = new ArrayMap<>(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 418 | final ArrayList<ToastRecord> mToastQueue = new ArrayList<>(); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 419 | final ArrayMap<String, NotificationRecord> mSummaryByGroupKey = new ArrayMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 421 | // The last key in this list owns the hardware. |
| 422 | ArrayList<String> mLights = new ArrayList<>(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 423 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 424 | private AppOpsManager mAppOps; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 425 | private UsageStatsManagerInternal mAppUsageStats; |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 426 | private DevicePolicyManagerInternal mDpm; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 427 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 428 | private Archive mArchive; |
| 429 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 430 | // Persistent storage for notification policy |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 431 | private AtomicFile mPolicyFile; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 432 | |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 433 | private static final int DB_VERSION = 1; |
| 434 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 435 | private static final String TAG_NOTIFICATION_POLICY = "notification-policy"; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 436 | private static final String ATTR_VERSION = "version"; |
| 437 | |
Zimuzo | b3b9c26 | 2018-10-31 11:54:20 +0000 | [diff] [blame] | 438 | private static final String LOCKSCREEN_ALLOW_SECURE_NOTIFICATIONS_TAG = |
| 439 | "allow-secure-notifications-on-lockscreen"; |
| 440 | private static final String LOCKSCREEN_ALLOW_SECURE_NOTIFICATIONS_VALUE = "value"; |
| 441 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 442 | private RankingHelper mRankingHelper; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 443 | private PreferencesHelper mPreferencesHelper; |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 444 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 445 | private final UserProfiles mUserProfiles = new UserProfiles(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 446 | private NotificationListeners mListeners; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 447 | private NotificationAssistants mAssistants; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 448 | private ConditionProviders mConditionProviders; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 449 | private NotificationUsageStats mUsageStats; |
Selim Cinek | 0336296 | 2018-11-20 16:19:12 -0800 | [diff] [blame] | 450 | private boolean mLockScreenAllowSecureNotifications = true; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 451 | |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 452 | private static final int MY_UID = Process.myUid(); |
| 453 | private static final int MY_PID = Process.myPid(); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 454 | private static final IBinder WHITELIST_TOKEN = new Binder(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 455 | private RankingHandler mRankingHandler; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 456 | private long mLastOverRateLogTime; |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 457 | private float mMaxPackageEnqueueRate = DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE; |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 458 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 459 | private SnoozeHelper mSnoozeHelper; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 460 | private GroupHelper mGroupHelper; |
Adora Zhang | 48dd614a8 | 2018-06-25 19:18:41 -0700 | [diff] [blame] | 461 | private int mAutoGroupAtCount; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 462 | private boolean mIsTelevision; |
Adora Zhang | 963328f | 2018-11-15 18:17:19 -0800 | [diff] [blame] | 463 | private boolean mIsAutomotive; |
Qiao (Adora) Zhang | 47b553c | 2019-04-15 20:42:57 +0000 | [diff] [blame] | 464 | private boolean mNotificationEffectsEnabledForAutomotive; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 465 | |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 466 | private MetricsLogger mMetricsLogger; |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 467 | private TriPredicate<String, Integer, String> mAllowedManagedServicePackages; |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 468 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 469 | private static class Archive { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 470 | final int mBufferSize; |
| 471 | final ArrayDeque<StatusBarNotification> mBuffer; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 472 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 473 | public Archive(int size) { |
| 474 | mBufferSize = size; |
| 475 | mBuffer = new ArrayDeque<StatusBarNotification>(mBufferSize); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 476 | } |
Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 477 | |
Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 478 | public String toString() { |
| 479 | final StringBuilder sb = new StringBuilder(); |
| 480 | final int N = mBuffer.size(); |
| 481 | sb.append("Archive ("); |
| 482 | sb.append(N); |
| 483 | sb.append(" notification"); |
| 484 | sb.append((N==1)?")":"s)"); |
| 485 | return sb.toString(); |
| 486 | } |
| 487 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 488 | public void record(StatusBarNotification nr) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 489 | if (mBuffer.size() == mBufferSize) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 490 | mBuffer.removeFirst(); |
| 491 | } |
Daniel Sandler | 26b81d5 | 2013-05-20 20:56:43 -0400 | [diff] [blame] | 492 | |
| 493 | // We don't want to store the heavy bits of the notification in the archive, |
| 494 | // but other clients in the system process might be using the object, so we |
| 495 | // store a (lightened) copy. |
| 496 | mBuffer.addLast(nr.cloneLight()); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 497 | } |
| 498 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 499 | public Iterator<StatusBarNotification> descendingIterator() { |
| 500 | return mBuffer.descendingIterator(); |
| 501 | } |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 502 | |
| 503 | public StatusBarNotification[] getArray(int count) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 504 | if (count == 0) count = mBufferSize; |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 505 | final StatusBarNotification[] a |
| 506 | = new StatusBarNotification[Math.min(count, mBuffer.size())]; |
| 507 | Iterator<StatusBarNotification> iter = descendingIterator(); |
| 508 | int i=0; |
| 509 | while (iter.hasNext() && i < count) { |
| 510 | a[i++] = iter.next(); |
| 511 | } |
| 512 | return a; |
| 513 | } |
| 514 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 515 | } |
| 516 | |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 517 | protected void readDefaultApprovedServices(int userId) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 518 | String defaultListenerAccess = getContext().getResources().getString( |
| 519 | com.android.internal.R.string.config_defaultListenerAccessPackages); |
| 520 | if (defaultListenerAccess != null) { |
| 521 | for (String whitelisted : |
| 522 | defaultListenerAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR)) { |
| 523 | // Gather all notification listener components for candidate pkgs. |
| 524 | Set<ComponentName> approvedListeners = |
| 525 | mListeners.queryPackageForServices(whitelisted, |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 526 | MATCH_DIRECT_BOOT_AWARE |
| 527 | | MATCH_DIRECT_BOOT_UNAWARE, userId); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 528 | for (ComponentName cn : approvedListeners) { |
| 529 | try { |
| 530 | getBinderService().setNotificationListenerAccessGrantedForUser(cn, |
| 531 | userId, true); |
| 532 | } catch (RemoteException e) { |
| 533 | e.printStackTrace(); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 538 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 539 | String defaultDndAccess = getContext().getResources().getString( |
| 540 | com.android.internal.R.string.config_defaultDndAccessPackages); |
Edward Savage-Jones | 36a8942 | 2018-07-19 12:23:58 +0200 | [diff] [blame] | 541 | if (defaultDndAccess != null) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 542 | for (String whitelisted : |
| 543 | defaultDndAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR)) { |
| 544 | try { |
| 545 | getBinderService().setNotificationPolicyAccessGranted(whitelisted, true); |
| 546 | } catch (RemoteException e) { |
| 547 | e.printStackTrace(); |
| 548 | } |
| 549 | } |
| 550 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 551 | |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 552 | setDefaultAssistantForUser(userId); |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 553 | } |
| 554 | |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 555 | protected void setDefaultAssistantForUser(int userId) { |
| 556 | List<ComponentName> validAssistants = new ArrayList<>( |
| 557 | mAssistants.queryPackageForServices( |
| 558 | null, MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE, userId)); |
| 559 | |
| 560 | List<String> candidateStrs = new ArrayList<>(); |
| 561 | candidateStrs.add(DeviceConfig.getProperty( |
| 562 | DeviceConfig.NAMESPACE_SYSTEMUI, |
| 563 | SystemUiDeviceConfigFlags.NAS_DEFAULT_SERVICE)); |
| 564 | candidateStrs.add(getContext().getResources().getString( |
| 565 | com.android.internal.R.string.config_defaultAssistantAccessComponent)); |
| 566 | |
| 567 | for (String candidateStr : candidateStrs) { |
| 568 | if (TextUtils.isEmpty(candidateStr)) { |
| 569 | continue; |
| 570 | } |
| 571 | ComponentName candidate = ComponentName.unflattenFromString(candidateStr); |
| 572 | if (candidate != null && validAssistants.contains(candidate)) { |
| 573 | setNotificationAssistantAccessGrantedForUserInternal(candidate, userId, true); |
| 574 | Slog.d(TAG, String.format("Set default NAS to be %s in %d", candidateStr, userId)); |
| 575 | return; |
| 576 | } else { |
| 577 | Slog.w(TAG, "Invalid default NAS config is found: " + candidateStr); |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 578 | } |
| 579 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 580 | } |
| 581 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 582 | void readPolicyXml(InputStream stream, boolean forRestore, int userId) |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 583 | throws XmlPullParserException, NumberFormatException, IOException { |
| 584 | final XmlPullParser parser = Xml.newPullParser(); |
| 585 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 586 | XmlUtils.beginDocument(parser, TAG_NOTIFICATION_POLICY); |
| 587 | boolean migratedManagedServices = false; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 588 | boolean ineligibleForManagedServices = forRestore && mUm.isManagedProfile(userId); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 589 | int outerDepth = parser.getDepth(); |
| 590 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 591 | if (ZenModeConfig.ZEN_TAG.equals(parser.getName())) { |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 592 | mZenModeHelper.readXml(parser, forRestore, userId); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 593 | } else if (PreferencesHelper.TAG_RANKING.equals(parser.getName())){ |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 594 | mPreferencesHelper.readXml(parser, forRestore, userId); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 595 | } |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 596 | if (mListeners.getConfig().xmlTag.equals(parser.getName())) { |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 597 | if (ineligibleForManagedServices) { |
| 598 | continue; |
| 599 | } |
| 600 | mListeners.readXml(parser, mAllowedManagedServicePackages, forRestore, userId); |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 601 | migratedManagedServices = true; |
| 602 | } else if (mAssistants.getConfig().xmlTag.equals(parser.getName())) { |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 603 | if (ineligibleForManagedServices) { |
| 604 | continue; |
| 605 | } |
| 606 | mAssistants.readXml(parser, mAllowedManagedServicePackages, forRestore, userId); |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 607 | migratedManagedServices = true; |
| 608 | } else if (mConditionProviders.getConfig().xmlTag.equals(parser.getName())) { |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 609 | if (ineligibleForManagedServices) { |
| 610 | continue; |
| 611 | } |
| 612 | mConditionProviders.readXml( |
| 613 | parser, mAllowedManagedServicePackages, forRestore, userId); |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 614 | migratedManagedServices = true; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 615 | } |
Zimuzo | b3b9c26 | 2018-10-31 11:54:20 +0000 | [diff] [blame] | 616 | if (LOCKSCREEN_ALLOW_SECURE_NOTIFICATIONS_TAG.equals(parser.getName())) { |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 617 | if (forRestore && userId != UserHandle.USER_SYSTEM) { |
| 618 | continue; |
| 619 | } |
Zimuzo | b3b9c26 | 2018-10-31 11:54:20 +0000 | [diff] [blame] | 620 | mLockScreenAllowSecureNotifications = |
| 621 | safeBoolean(parser.getAttributeValue(null, |
| 622 | LOCKSCREEN_ALLOW_SECURE_NOTIFICATIONS_VALUE), true); |
| 623 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 624 | } |
| 625 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 626 | if (!migratedManagedServices) { |
| 627 | mListeners.migrateToXml(); |
| 628 | mAssistants.migrateToXml(); |
| 629 | mConditionProviders.migrateToXml(); |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 630 | handleSavePolicyFile(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 631 | } |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 632 | |
| 633 | mAssistants.resetDefaultAssistantsIfNecessary(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 634 | } |
| 635 | |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 636 | @VisibleForTesting |
| 637 | protected void loadPolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 638 | if (DBG) Slog.d(TAG, "loadPolicyFile"); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 639 | synchronized (mPolicyFile) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 640 | InputStream infile = null; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 641 | try { |
| 642 | infile = mPolicyFile.openRead(); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 643 | readPolicyXml(infile, false /*forRestore*/, UserHandle.USER_ALL); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 644 | } catch (FileNotFoundException e) { |
| 645 | // No data yet |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 646 | // Load default managed services approvals |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 647 | readDefaultApprovedServices(USER_SYSTEM); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 648 | } catch (IOException e) { |
| 649 | Log.wtf(TAG, "Unable to read notification policy", e); |
| 650 | } catch (NumberFormatException e) { |
| 651 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 652 | } catch (XmlPullParserException e) { |
| 653 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 654 | } finally { |
| 655 | IoUtils.closeQuietly(infile); |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 660 | @VisibleForTesting |
| 661 | protected void handleSavePolicyFile() { |
| 662 | IoThread.getHandler().post(() -> { |
| 663 | if (DBG) Slog.d(TAG, "handleSavePolicyFile"); |
| 664 | synchronized (mPolicyFile) { |
| 665 | final FileOutputStream stream; |
| 666 | try { |
| 667 | stream = mPolicyFile.startWrite(); |
| 668 | } catch (IOException e) { |
| 669 | Slog.w(TAG, "Failed to save policy file", e); |
| 670 | return; |
| 671 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 672 | |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 673 | try { |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 674 | writePolicyXml(stream, false /*forBackup*/, UserHandle.USER_ALL); |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 675 | mPolicyFile.finishWrite(stream); |
| 676 | } catch (IOException e) { |
| 677 | Slog.w(TAG, "Failed to save policy file, restoring backup", e); |
| 678 | mPolicyFile.failWrite(stream); |
| 679 | } |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 680 | } |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 681 | BackupManager.dataChanged(getContext().getPackageName()); |
| 682 | }); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 683 | } |
| 684 | |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 685 | private void writePolicyXml(OutputStream stream, boolean forBackup, int userId) |
| 686 | throws IOException { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 687 | final XmlSerializer out = new FastXmlSerializer(); |
| 688 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
| 689 | out.startDocument(null, true); |
| 690 | out.startTag(null, TAG_NOTIFICATION_POLICY); |
| 691 | out.attribute(null, ATTR_VERSION, Integer.toString(DB_VERSION)); |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 692 | mZenModeHelper.writeXml(out, forBackup, null, userId); |
| 693 | mPreferencesHelper.writeXml(out, forBackup, userId); |
| 694 | mListeners.writeXml(out, forBackup, userId); |
| 695 | mAssistants.writeXml(out, forBackup, userId); |
| 696 | mConditionProviders.writeXml(out, forBackup, userId); |
| 697 | if (!forBackup || userId == UserHandle.USER_SYSTEM) { |
| 698 | writeSecureNotificationsPolicy(out); |
| 699 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 700 | out.endTag(null, TAG_NOTIFICATION_POLICY); |
| 701 | out.endDocument(); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 702 | } |
| 703 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | private static final class ToastRecord |
| 705 | { |
| 706 | final int pid; |
| 707 | final String pkg; |
Beverly Tai | a7ed0ab | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 708 | final ITransientNotification callback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 709 | int duration; |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 710 | int displayId; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 711 | Binder token; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 713 | ToastRecord(int pid, String pkg, ITransientNotification callback, int duration, |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 714 | Binder token, int displayId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 | this.pid = pid; |
| 716 | this.pkg = pkg; |
| 717 | this.callback = callback; |
| 718 | this.duration = duration; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 719 | this.token = token; |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 720 | this.displayId = displayId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | void update(int duration) { |
| 724 | this.duration = duration; |
| 725 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 726 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 727 | void dump(PrintWriter pw, String prefix, DumpFilter filter) { |
| 728 | if (filter != null && !filter.matches(pkg)) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | pw.println(prefix + this); |
| 730 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 731 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 732 | @Override |
| 733 | public final String toString() |
| 734 | { |
| 735 | return "ToastRecord{" |
| 736 | + Integer.toHexString(System.identityHashCode(this)) |
| 737 | + " pkg=" + pkg |
| 738 | + " callback=" + callback |
| 739 | + " duration=" + duration; |
| 740 | } |
| 741 | } |
| 742 | |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 743 | @VisibleForTesting |
| 744 | final NotificationDelegate mNotificationDelegate = new NotificationDelegate() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 745 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 746 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 747 | public void onSetDisabled(int status) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 748 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 749 | mDisableNotificationEffects = |
| 750 | (status & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 751 | if (disableNotificationEffects(null) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 752 | // cancel whatever's going on |
| 753 | long identity = Binder.clearCallingIdentity(); |
| 754 | try { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 755 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 756 | if (player != null) { |
| 757 | player.stopAsync(); |
| 758 | } |
| 759 | } catch (RemoteException e) { |
| 760 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 761 | Binder.restoreCallingIdentity(identity); |
| 762 | } |
| 763 | |
| 764 | identity = Binder.clearCallingIdentity(); |
| 765 | try { |
| 766 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 767 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 768 | Binder.restoreCallingIdentity(identity); |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 774 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 775 | public void onClearAll(int callingUid, int callingPid, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 776 | synchronized (mNotificationLock) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 777 | cancelAllLocked(callingUid, callingPid, userId, REASON_CANCEL_ALL, null, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 778 | /*includeCurrentProfiles*/ true); |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 779 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | } |
| 781 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 782 | @Override |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 783 | public void onNotificationClick(int callingUid, int callingPid, String key, |
| 784 | NotificationVisibility nv) { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 785 | exitIdle(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 786 | synchronized (mNotificationLock) { |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 787 | NotificationRecord r = mNotificationsByKey.get(key); |
| 788 | if (r == null) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 789 | Slog.w(TAG, "No notification with key: " + key); |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 790 | return; |
| 791 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 792 | final long now = System.currentTimeMillis(); |
Julia Reynolds | 3dfdde0 | 2018-10-08 09:17:56 -0400 | [diff] [blame] | 793 | MetricsLogger.action(r.getItemLogMaker() |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 794 | .setType(MetricsEvent.TYPE_ACTION) |
| 795 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, nv.rank) |
| 796 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, nv.count)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 797 | EventLogTags.writeNotificationClicked(key, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 798 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now), |
| 799 | nv.rank, nv.count); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 800 | |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 801 | StatusBarNotification sbn = r.sbn; |
| 802 | cancelNotification(callingUid, callingPid, sbn.getPackageName(), sbn.getTag(), |
| 803 | sbn.getId(), Notification.FLAG_AUTO_CANCEL, |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 804 | FLAG_FOREGROUND_SERVICE, false, r.getUserId(), |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 805 | REASON_CLICK, nv.rank, nv.count, null); |
| 806 | nv.recycle(); |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 807 | reportUserInteraction(r); |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 808 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | } |
| 810 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 811 | @Override |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 812 | public void onNotificationActionClick(int callingUid, int callingPid, String key, |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 813 | int actionIndex, Notification.Action action, NotificationVisibility nv, |
| 814 | boolean generatedByAssistant) { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 815 | exitIdle(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 816 | synchronized (mNotificationLock) { |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 817 | NotificationRecord r = mNotificationsByKey.get(key); |
| 818 | if (r == null) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 819 | Slog.w(TAG, "No notification with key: " + key); |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 820 | return; |
| 821 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 822 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 823 | MetricsLogger.action(r.getLogMaker(now) |
| 824 | .setCategory(MetricsEvent.NOTIFICATION_ITEM_ACTION) |
| 825 | .setType(MetricsEvent.TYPE_ACTION) |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 826 | .setSubtype(actionIndex) |
| 827 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, nv.rank) |
Gustav Sennton | d25a64d | 2018-12-07 10:58:39 +0000 | [diff] [blame] | 828 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, nv.count) |
| 829 | .addTaggedData(MetricsEvent.NOTIFICATION_ACTION_IS_SMART, |
Gustav Sennton | 005d7a0 | 2019-01-04 13:41:32 +0000 | [diff] [blame] | 830 | action.isContextual() ? 1 : 0) |
Gustav Sennton | d25a64d | 2018-12-07 10:58:39 +0000 | [diff] [blame] | 831 | .addTaggedData( |
| 832 | MetricsEvent.NOTIFICATION_SMART_SUGGESTION_ASSISTANT_GENERATED, |
Gustav Sennton | 7299196 | 2019-01-28 21:09:54 +0000 | [diff] [blame] | 833 | generatedByAssistant ? 1 : 0) |
| 834 | .addTaggedData(MetricsEvent.NOTIFICATION_LOCATION, |
| 835 | nv.location.toMetricsEventEnum())); |
| 836 | |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 837 | EventLogTags.writeNotificationActionClicked(key, actionIndex, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 838 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now), |
| 839 | nv.rank, nv.count); |
| 840 | nv.recycle(); |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 841 | reportUserInteraction(r); |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 842 | mAssistants.notifyAssistantActionClicked( |
| 843 | r.sbn, actionIndex, action, generatedByAssistant); |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | |
| 847 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 848 | public void onNotificationClear(int callingUid, int callingPid, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 849 | String pkg, String tag, int id, int userId, String key, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 850 | @NotificationStats.DismissalSurface int dismissalSurface, |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 851 | @NotificationStats.DismissalSentiment int dismissalSentiment, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 852 | NotificationVisibility nv) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 853 | synchronized (mNotificationLock) { |
| 854 | NotificationRecord r = mNotificationsByKey.get(key); |
| 855 | if (r != null) { |
| 856 | r.recordDismissalSurface(dismissalSurface); |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 857 | r.recordDismissalSentiment(dismissalSentiment); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 858 | } |
| 859 | } |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 860 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 861 | FLAG_ONGOING_EVENT | FLAG_FOREGROUND_SERVICE, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 862 | true, userId, REASON_CANCEL, nv.rank, nv.count,null); |
| 863 | nv.recycle(); |
Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 864 | } |
| 865 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 866 | @Override |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 867 | public void onPanelRevealed(boolean clearEffects, int items) { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 868 | MetricsLogger.visible(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Chris Wren | 621933f | 2017-06-14 15:59:03 -0400 | [diff] [blame] | 869 | MetricsLogger.histogram(getContext(), "note_load", items); |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 870 | EventLogTags.writeNotificationPanelRevealed(items); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 871 | if (clearEffects) { |
| 872 | clearEffects(); |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | @Override |
| 877 | public void onPanelHidden() { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 878 | MetricsLogger.hidden(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 879 | EventLogTags.writeNotificationPanelHidden(); |
| 880 | } |
| 881 | |
| 882 | @Override |
| 883 | public void clearEffects() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 884 | synchronized (mNotificationLock) { |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 885 | if (DBG) Slog.d(TAG, "clearEffects"); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 886 | clearSoundLocked(); |
| 887 | clearVibrateLocked(); |
| 888 | clearLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | } |
| 890 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 891 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 892 | @Override |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 893 | public void onNotificationError(int callingUid, int callingPid, String pkg, String tag, |
| 894 | int id, int uid, int initialPid, String message, int userId) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 895 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, 0, false, userId, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 896 | REASON_ERROR, null); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 897 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 898 | |
| 899 | @Override |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 900 | public void onNotificationVisibilityChanged(NotificationVisibility[] newlyVisibleKeys, |
| 901 | NotificationVisibility[] noLongerVisibleKeys) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 902 | synchronized (mNotificationLock) { |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 903 | for (NotificationVisibility nv : newlyVisibleKeys) { |
| 904 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 905 | if (r == null) continue; |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 906 | if (!r.isSeen()) { |
| 907 | // Report to usage stats that notification was made visible |
| 908 | if (DBG) Slog.d(TAG, "Marking notification as visible " + nv.key); |
| 909 | reportSeen(r); |
| 910 | } |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 911 | r.setVisibility(true, nv.rank, nv.count); |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 912 | boolean isHun = (nv.location |
| 913 | == NotificationVisibility.NotificationLocation.LOCATION_FIRST_HEADS_UP); |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 914 | // hasBeenVisiblyExpanded must be called after updating the expansion state of |
| 915 | // the NotificationRecord to ensure the expansion state is up-to-date. |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 916 | if (isHun || r.hasBeenVisiblyExpanded()) { |
| 917 | logSmartSuggestionsVisible(r, nv.location.toMetricsEventEnum()); |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 918 | } |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 919 | maybeRecordInterruptionLocked(r); |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 920 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 921 | } |
| 922 | // Note that we might receive this event after notifications |
| 923 | // have already left the system, e.g. after dismissing from the |
| 924 | // shade. Hence not finding notifications in |
| 925 | // mNotificationsByKey is not an exceptional condition. |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 926 | for (NotificationVisibility nv : noLongerVisibleKeys) { |
| 927 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 928 | if (r == null) continue; |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 929 | r.setVisibility(false, nv.rank, nv.count); |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 930 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 931 | } |
| 932 | } |
Christoph Studer | 92b389d | 2014-04-01 18:44:40 +0200 | [diff] [blame] | 933 | } |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 934 | |
| 935 | @Override |
| 936 | public void onNotificationExpansionChanged(String key, |
Gustav Sennton | a8e38aa | 2019-01-22 14:55:39 +0000 | [diff] [blame] | 937 | boolean userAction, boolean expanded, int notificationLocation) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 938 | synchronized (mNotificationLock) { |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 939 | NotificationRecord r = mNotificationsByKey.get(key); |
| 940 | if (r != null) { |
| 941 | r.stats.onExpansionChanged(userAction, expanded); |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 942 | // hasBeenVisiblyExpanded must be called after updating the expansion state of |
| 943 | // the NotificationRecord to ensure the expansion state is up-to-date. |
| 944 | if (r.hasBeenVisiblyExpanded()) { |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 945 | logSmartSuggestionsVisible(r, notificationLocation); |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 946 | } |
Chris Wren | f734271 | 2017-09-14 10:55:55 -0400 | [diff] [blame] | 947 | if (userAction) { |
Julia Reynolds | 3dfdde0 | 2018-10-08 09:17:56 -0400 | [diff] [blame] | 948 | MetricsLogger.action(r.getItemLogMaker() |
Chris Wren | f734271 | 2017-09-14 10:55:55 -0400 | [diff] [blame] | 949 | .setType(expanded ? MetricsEvent.TYPE_DETAIL |
| 950 | : MetricsEvent.TYPE_COLLAPSE)); |
Chris Wren | 377ac6d | 2017-09-12 14:15:23 -0400 | [diff] [blame] | 951 | } |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 952 | if (expanded && userAction) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 953 | r.recordExpanded(); |
Esteban Talavera | 917a71d | 2018-11-13 07:55:08 +0000 | [diff] [blame] | 954 | reportUserInteraction(r); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 955 | } |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 956 | mAssistants.notifyAssistantExpansionChangedLocked(r.sbn, userAction, expanded); |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 960 | |
| 961 | @Override |
| 962 | public void onNotificationDirectReplied(String key) { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 963 | exitIdle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 964 | synchronized (mNotificationLock) { |
| 965 | NotificationRecord r = mNotificationsByKey.get(key); |
| 966 | if (r != null) { |
| 967 | r.recordDirectReplied(); |
Esteban Talavera | f9c53b6 | 2018-11-14 18:20:29 +0000 | [diff] [blame] | 968 | mMetricsLogger.write(r.getLogMaker() |
| 969 | .setCategory(MetricsEvent.NOTIFICATION_DIRECT_REPLY_ACTION) |
| 970 | .setType(MetricsEvent.TYPE_ACTION)); |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 971 | reportUserInteraction(r); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 972 | mAssistants.notifyAssistantNotificationDirectReplyLocked(r.sbn); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | @Override |
Gustav Sennton | d25a64d | 2018-12-07 10:58:39 +0000 | [diff] [blame] | 978 | public void onNotificationSmartSuggestionsAdded(String key, int smartReplyCount, |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 979 | int smartActionCount, boolean generatedByAssistant, boolean editBeforeSending) { |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 980 | synchronized (mNotificationLock) { |
| 981 | NotificationRecord r = mNotificationsByKey.get(key); |
| 982 | if (r != null) { |
Gustav Sennton | d25a64d | 2018-12-07 10:58:39 +0000 | [diff] [blame] | 983 | r.setNumSmartRepliesAdded(smartReplyCount); |
| 984 | r.setNumSmartActionsAdded(smartActionCount); |
| 985 | r.setSuggestionsGeneratedByAssistant(generatedByAssistant); |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 986 | r.setEditChoicesBeforeSending(editBeforeSending); |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | @Override |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 992 | public void onNotificationSmartReplySent(String key, int replyIndex, CharSequence reply, |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 993 | int notificationLocation, boolean modifiedBeforeSending) { |
Gustav Sennton | d25a64d | 2018-12-07 10:58:39 +0000 | [diff] [blame] | 994 | |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 995 | synchronized (mNotificationLock) { |
| 996 | NotificationRecord r = mNotificationsByKey.get(key); |
| 997 | if (r != null) { |
| 998 | LogMaker logMaker = r.getLogMaker() |
| 999 | .setCategory(MetricsEvent.SMART_REPLY_ACTION) |
Gustav Sennton | 13edb49 | 2019-01-28 21:40:04 +0000 | [diff] [blame] | 1000 | .setSubtype(replyIndex) |
| 1001 | .addTaggedData( |
| 1002 | MetricsEvent.NOTIFICATION_SMART_SUGGESTION_ASSISTANT_GENERATED, |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 1003 | r.getSuggestionsGeneratedByAssistant() ? 1 : 0) |
Gustav Sennton | 13edb49 | 2019-01-28 21:40:04 +0000 | [diff] [blame] | 1004 | .addTaggedData(MetricsEvent.NOTIFICATION_LOCATION, |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 1005 | notificationLocation) |
| 1006 | .addTaggedData( |
| 1007 | MetricsEvent.NOTIFICATION_SMART_REPLY_EDIT_BEFORE_SENDING, |
| 1008 | r.getEditChoicesBeforeSending() ? 1 : 0) |
| 1009 | .addTaggedData( |
| 1010 | MetricsEvent.NOTIFICATION_SMART_REPLY_MODIFIED_BEFORE_SENDING, |
| 1011 | modifiedBeforeSending ? 1 : 0); |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 1012 | mMetricsLogger.write(logMaker); |
| 1013 | // Treat clicking on a smart reply as a user interaction. |
| 1014 | reportUserInteraction(r); |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 1015 | mAssistants.notifyAssistantSuggestedReplySent( |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 1016 | r.sbn, reply, r.getSuggestionsGeneratedByAssistant()); |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | @Override |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 1022 | public void onNotificationSettingsViewed(String key) { |
| 1023 | synchronized (mNotificationLock) { |
| 1024 | NotificationRecord r = mNotificationsByKey.get(key); |
| 1025 | if (r != null) { |
| 1026 | r.recordViewedSettings(); |
| 1027 | } |
| 1028 | } |
| 1029 | } |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 1030 | |
| 1031 | @Override |
| 1032 | public void onNotificationBubbleChanged(String key, boolean isBubble) { |
| 1033 | synchronized (mNotificationLock) { |
| 1034 | NotificationRecord r = mNotificationsByKey.get(key); |
| 1035 | if (r != null) { |
| 1036 | final StatusBarNotification n = r.sbn; |
| 1037 | final int callingUid = n.getUid(); |
| 1038 | final String pkg = n.getPackageName(); |
Mady Mellor | 66efd5e | 2019-05-15 13:38:11 -0700 | [diff] [blame] | 1039 | final boolean wasBubble = r.getNotification().isBubbleNotification(); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 1040 | if (isBubble && isNotificationAppropriateToBubble(r, pkg, callingUid, |
| 1041 | null /* oldEntry */)) { |
| 1042 | r.getNotification().flags |= FLAG_BUBBLE; |
| 1043 | } else { |
| 1044 | r.getNotification().flags &= ~FLAG_BUBBLE; |
| 1045 | } |
Mady Mellor | 66efd5e | 2019-05-15 13:38:11 -0700 | [diff] [blame] | 1046 | if (wasBubble != r.getNotification().isBubbleNotification()) { |
| 1047 | // Add the "alert only once" flag so that the notification won't HUN |
| 1048 | // unnecessarily just because the bubble flag was changed. |
| 1049 | r.getNotification().flags |= FLAG_ONLY_ALERT_ONCE; |
| 1050 | mListeners.notifyPostedLocked(r, r); |
| 1051 | } |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 1052 | } |
| 1053 | } |
| 1054 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | }; |
| 1056 | |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 1057 | @VisibleForTesting |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 1058 | void logSmartSuggestionsVisible(NotificationRecord r, int notificationLocation) { |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 1059 | // If the newly visible notification has smart suggestions |
| 1060 | // then log that the user has seen them. |
| 1061 | if ((r.getNumSmartRepliesAdded() > 0 || r.getNumSmartActionsAdded() > 0) |
| 1062 | && !r.hasSeenSmartReplies()) { |
| 1063 | r.setSeenSmartReplies(true); |
| 1064 | LogMaker logMaker = r.getLogMaker() |
| 1065 | .setCategory(MetricsEvent.SMART_REPLY_VISIBLE) |
| 1066 | .addTaggedData(MetricsEvent.NOTIFICATION_SMART_REPLY_COUNT, |
| 1067 | r.getNumSmartRepliesAdded()) |
| 1068 | .addTaggedData(MetricsEvent.NOTIFICATION_SMART_ACTION_COUNT, |
| 1069 | r.getNumSmartActionsAdded()) |
| 1070 | .addTaggedData( |
| 1071 | MetricsEvent.NOTIFICATION_SMART_SUGGESTION_ASSISTANT_GENERATED, |
Gustav Sennton | c7d0d32 | 2019-01-07 15:36:41 +0000 | [diff] [blame] | 1072 | r.getSuggestionsGeneratedByAssistant() ? 1 : 0) |
| 1073 | // The fields in the NotificationVisibility.NotificationLocation enum map |
| 1074 | // directly to the fields in the MetricsEvent.NotificationLocation enum. |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 1075 | .addTaggedData(MetricsEvent.NOTIFICATION_LOCATION, notificationLocation) |
| 1076 | .addTaggedData( |
| 1077 | MetricsEvent.NOTIFICATION_SMART_REPLY_EDIT_BEFORE_SENDING, |
| 1078 | r.getEditChoicesBeforeSending() ? 1 : 0); |
Gustav Sennton | 44dc588 | 2018-12-13 14:38:50 +0000 | [diff] [blame] | 1079 | mMetricsLogger.write(logMaker); |
| 1080 | } |
| 1081 | } |
| 1082 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1083 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1084 | private void clearSoundLocked() { |
| 1085 | mSoundNotificationKey = null; |
| 1086 | long identity = Binder.clearCallingIdentity(); |
| 1087 | try { |
| 1088 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 1089 | if (player != null) { |
| 1090 | player.stopAsync(); |
| 1091 | } |
| 1092 | } catch (RemoteException e) { |
| 1093 | } finally { |
| 1094 | Binder.restoreCallingIdentity(identity); |
| 1095 | } |
| 1096 | } |
| 1097 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1098 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1099 | private void clearVibrateLocked() { |
| 1100 | mVibrateNotificationKey = null; |
| 1101 | long identity = Binder.clearCallingIdentity(); |
| 1102 | try { |
| 1103 | mVibrator.cancel(); |
| 1104 | } finally { |
| 1105 | Binder.restoreCallingIdentity(identity); |
| 1106 | } |
| 1107 | } |
| 1108 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1109 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1110 | private void clearLightsLocked() { |
| 1111 | // light |
| 1112 | mLights.clear(); |
| 1113 | updateLightsLocked(); |
| 1114 | } |
| 1115 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1116 | protected final BroadcastReceiver mLocaleChangeReceiver = new BroadcastReceiver() { |
| 1117 | @Override |
| 1118 | public void onReceive(Context context, Intent intent) { |
| 1119 | if (Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) { |
Beverly | 70dcd00 | 2018-03-29 17:09:16 -0400 | [diff] [blame] | 1120 | // update system notification channels |
| 1121 | SystemNotificationChannels.createAll(context); |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1122 | mZenModeHelper.updateDefaultZenRules(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1123 | mPreferencesHelper.onLocaleChanged(context, ActivityManager.getCurrentUser()); |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | }; |
| 1127 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1128 | private final BroadcastReceiver mRestoreReceiver = new BroadcastReceiver() { |
| 1129 | @Override |
| 1130 | public void onReceive(Context context, Intent intent) { |
| 1131 | if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) { |
| 1132 | try { |
| 1133 | String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME); |
| 1134 | String newValue = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE); |
Michal Karpinski | 6135a26 | 2017-08-11 10:45:58 +0100 | [diff] [blame] | 1135 | int restoredFromSdkInt = intent.getIntExtra( |
| 1136 | Intent.EXTRA_SETTING_RESTORED_FROM_SDK_INT, 0); |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 1137 | mListeners.onSettingRestored( |
| 1138 | element, newValue, restoredFromSdkInt, getSendingUserId()); |
| 1139 | mConditionProviders.onSettingRestored( |
| 1140 | element, newValue, restoredFromSdkInt, getSendingUserId()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1141 | } catch (Exception e) { |
| 1142 | Slog.wtf(TAG, "Cannot restore managed services from settings", e); |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | }; |
| 1147 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1148 | private final BroadcastReceiver mNotificationTimeoutReceiver = new BroadcastReceiver() { |
| 1149 | @Override |
| 1150 | public void onReceive(Context context, Intent intent) { |
| 1151 | String action = intent.getAction(); |
| 1152 | if (action == null) { |
| 1153 | return; |
| 1154 | } |
| 1155 | if (ACTION_NOTIFICATION_TIMEOUT.equals(action)) { |
| 1156 | final NotificationRecord record; |
| 1157 | synchronized (mNotificationLock) { |
| 1158 | record = findNotificationByKeyLocked(intent.getStringExtra(EXTRA_KEY)); |
| 1159 | } |
| 1160 | if (record != null) { |
| 1161 | cancelNotification(record.sbn.getUid(), record.sbn.getInitialPid(), |
| 1162 | record.sbn.getPackageName(), record.sbn.getTag(), |
| 1163 | record.sbn.getId(), 0, |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1164 | FLAG_FOREGROUND_SERVICE, true, record.getUserId(), |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1165 | REASON_TIMEOUT, null); |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | }; |
| 1170 | |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1171 | private final BroadcastReceiver mPackageIntentReceiver = new BroadcastReceiver() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1172 | @Override |
| 1173 | public void onReceive(Context context, Intent intent) { |
| 1174 | String action = intent.getAction(); |
Dianne Hackborn | 29cd7f1 | 2015-01-08 10:37:05 -0800 | [diff] [blame] | 1175 | if (action == null) { |
| 1176 | return; |
| 1177 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1178 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1179 | boolean queryRestart = false; |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 1180 | boolean queryRemove = false; |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1181 | boolean packageChanged = false; |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 1182 | boolean cancelNotifications = true; |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1183 | boolean hideNotifications = false; |
| 1184 | boolean unhideNotifications = false; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1185 | int reason = REASON_PACKAGE_CHANGED; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1186 | |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1187 | if (action.equals(Intent.ACTION_PACKAGE_ADDED) |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 1188 | || (queryRemove=action.equals(Intent.ACTION_PACKAGE_REMOVED)) |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1189 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1190 | || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED)) |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1191 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1192 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE) |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1193 | || action.equals(Intent.ACTION_PACKAGES_SUSPENDED) |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 1194 | || action.equals(Intent.ACTION_PACKAGES_UNSUSPENDED) |
| 1195 | || action.equals(Intent.ACTION_DISTRACTING_PACKAGES_CHANGED)) { |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1196 | int changeUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 1197 | UserHandle.USER_ALL); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1198 | String pkgList[] = null; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1199 | int uidList[] = null; |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 1200 | boolean removingPackage = queryRemove && |
| 1201 | !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 1202 | if (DBG) Slog.i(TAG, "action=" + action + " removing=" + removingPackage); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1203 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1204 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1205 | uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1206 | } else if (action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
| 1207 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Julia Reynolds | 4bc42b9 | 2018-08-14 11:27:35 -0400 | [diff] [blame] | 1208 | uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1209 | cancelNotifications = false; |
| 1210 | hideNotifications = true; |
| 1211 | } else if (action.equals(Intent.ACTION_PACKAGES_UNSUSPENDED)) { |
| 1212 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Julia Reynolds | 4bc42b9 | 2018-08-14 11:27:35 -0400 | [diff] [blame] | 1213 | uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1214 | cancelNotifications = false; |
| 1215 | unhideNotifications = true; |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 1216 | } else if (action.equals(Intent.ACTION_DISTRACTING_PACKAGES_CHANGED)) { |
| 1217 | final int distractionRestrictions = |
| 1218 | intent.getIntExtra(Intent.EXTRA_DISTRACTION_RESTRICTIONS, |
| 1219 | PackageManager.RESTRICTION_NONE); |
| 1220 | if ((distractionRestrictions |
| 1221 | & PackageManager.RESTRICTION_HIDE_NOTIFICATIONS) != 0) { |
| 1222 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 1223 | uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
| 1224 | cancelNotifications = false; |
| 1225 | hideNotifications = true; |
| 1226 | } else { |
| 1227 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 1228 | uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
| 1229 | cancelNotifications = false; |
| 1230 | unhideNotifications = true; |
| 1231 | } |
| 1232 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1233 | } else if (queryRestart) { |
| 1234 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1235 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1236 | } else { |
| 1237 | Uri uri = intent.getData(); |
| 1238 | if (uri == null) { |
| 1239 | return; |
| 1240 | } |
| 1241 | String pkgName = uri.getSchemeSpecificPart(); |
| 1242 | if (pkgName == null) { |
| 1243 | return; |
| 1244 | } |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1245 | if (packageChanged) { |
| 1246 | // We cancel notifications for packages which have just been disabled |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 1247 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1248 | final int enabled = mPackageManager.getApplicationEnabledSetting( |
| 1249 | pkgName, |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1250 | changeUserId != UserHandle.USER_ALL ? changeUserId : |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1251 | USER_SYSTEM); |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 1252 | if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 1253 | || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 1254 | cancelNotifications = false; |
| 1255 | } |
| 1256 | } catch (IllegalArgumentException e) { |
| 1257 | // Package doesn't exist; probably racing with uninstall. |
| 1258 | // cancelNotifications is already true, so nothing to do here. |
| 1259 | if (DBG) { |
| 1260 | Slog.i(TAG, "Exception trying to look up app enabled setting", e); |
| 1261 | } |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1262 | } catch (RemoteException e) { |
| 1263 | // Failed to talk to PackageManagerService Should never happen! |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1264 | } |
| 1265 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1266 | pkgList = new String[]{pkgName}; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1267 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1268 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1269 | if (pkgList != null && (pkgList.length > 0)) { |
| 1270 | for (String pkgName : pkgList) { |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 1271 | if (cancelNotifications) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1272 | cancelAllNotificationsInt(MY_UID, MY_PID, pkgName, null, 0, 0, |
| 1273 | !queryRestart, changeUserId, reason, null); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1274 | } else if (hideNotifications) { |
| 1275 | hideNotificationsForPackages(pkgList); |
| 1276 | } else if (unhideNotifications) { |
| 1277 | unhideNotificationsForPackages(pkgList); |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 1278 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1279 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1280 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1281 | } |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 1282 | |
Julia Reynolds | b040559 | 2018-11-26 17:01:13 -0500 | [diff] [blame] | 1283 | mHandler.scheduleOnPackageChanged(removingPackage, changeUserId, pkgList, uidList); |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 1284 | handleSavePolicyFile(); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | }; |
| 1288 | |
| 1289 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 1290 | @Override |
| 1291 | public void onReceive(Context context, Intent intent) { |
| 1292 | String action = intent.getAction(); |
| 1293 | |
| 1294 | if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1295 | // Keep track of screen on/off state, but do not turn off the notification light |
| 1296 | // until user passes through the lock screen or views the notification. |
| 1297 | mScreenOn = true; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 1298 | updateNotificationPulse(); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1299 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 1300 | mScreenOn = false; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 1301 | updateNotificationPulse(); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1302 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
John Spurlock | 5d2eeb1 | 2014-01-16 10:46:36 -0500 | [diff] [blame] | 1303 | mInCall = TelephonyManager.EXTRA_STATE_OFFHOOK |
| 1304 | .equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE)); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1305 | updateNotificationPulse(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1306 | } else if (action.equals(Intent.ACTION_USER_STOPPED)) { |
| 1307 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 1308 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1309 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1310 | REASON_USER_STOPPED, null); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1311 | } |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1312 | } else if (action.equals(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) { |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 1313 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1314 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1315 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1316 | REASON_PROFILE_TURNED_OFF, null); |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 1317 | } |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1318 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { |
| 1319 | // turn off LED when user passes through lock screen |
| 1320 | mNotificationLight.turnOff(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1321 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Julia Reynolds | 4ff9749 | 2018-09-24 14:53:54 -0400 | [diff] [blame] | 1322 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1323 | mUserProfiles.updateCache(context); |
Julia Reynolds | 4ff9749 | 2018-09-24 14:53:54 -0400 | [diff] [blame] | 1324 | if (!mUserProfiles.isManagedProfile(userId)) { |
| 1325 | // reload per-user settings |
| 1326 | mSettingsObserver.update(null); |
| 1327 | // Refresh managed services |
| 1328 | mConditionProviders.onUserSwitched(userId); |
| 1329 | mListeners.onUserSwitched(userId); |
| 1330 | mZenModeHelper.onUserSwitched(userId); |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 1331 | mPreferencesHelper.onUserSwitched(userId); |
Julia Reynolds | 4ff9749 | 2018-09-24 14:53:54 -0400 | [diff] [blame] | 1332 | } |
| 1333 | // assistant is the only thing that cares about managed profiles specifically |
| 1334 | mAssistants.onUserSwitched(userId); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1335 | } else if (action.equals(Intent.ACTION_USER_ADDED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1336 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
| 1337 | if (userId != USER_NULL) { |
| 1338 | mUserProfiles.updateCache(context); |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 1339 | if (!mUserProfiles.isManagedProfile(userId)) { |
| 1340 | readDefaultApprovedServices(userId); |
| 1341 | } |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1342 | } |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1343 | } else if (action.equals(Intent.ACTION_USER_REMOVED)) { |
Julia Reynolds | 4ff9749 | 2018-09-24 14:53:54 -0400 | [diff] [blame] | 1344 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1345 | mUserProfiles.updateCache(context); |
Julia Reynolds | 4ff9749 | 2018-09-24 14:53:54 -0400 | [diff] [blame] | 1346 | mZenModeHelper.onUserRemoved(userId); |
| 1347 | mPreferencesHelper.onUserRemoved(userId); |
| 1348 | mListeners.onUserRemoved(userId); |
| 1349 | mConditionProviders.onUserRemoved(userId); |
| 1350 | mAssistants.onUserRemoved(userId); |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 1351 | handleSavePolicyFile(); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1352 | } else if (action.equals(Intent.ACTION_USER_UNLOCKED)) { |
Julia Reynolds | 4ff9749 | 2018-09-24 14:53:54 -0400 | [diff] [blame] | 1353 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
| 1354 | mUserProfiles.updateCache(context); |
| 1355 | mAssistants.onUserUnlocked(userId); |
| 1356 | if (!mUserProfiles.isManagedProfile(userId)) { |
| 1357 | mConditionProviders.onUserUnlocked(userId); |
| 1358 | mListeners.onUserUnlocked(userId); |
| 1359 | mZenModeHelper.onUserUnlocked(userId); |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 1360 | mPreferencesHelper.onUserUnlocked(userId); |
Julia Reynolds | 4ff9749 | 2018-09-24 14:53:54 -0400 | [diff] [blame] | 1361 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1362 | } |
| 1363 | } |
| 1364 | }; |
| 1365 | |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 1366 | private final class SettingsObserver extends ContentObserver { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1367 | private final Uri NOTIFICATION_BADGING_URI |
| 1368 | = Settings.Secure.getUriFor(Settings.Secure.NOTIFICATION_BADGING); |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 1369 | private final Uri NOTIFICATION_BUBBLES_URI |
| 1370 | = Settings.Secure.getUriFor(Settings.Secure.NOTIFICATION_BUBBLES); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1371 | private final Uri NOTIFICATION_LIGHT_PULSE_URI |
| 1372 | = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1373 | private final Uri NOTIFICATION_RATE_LIMIT_URI |
| 1374 | = Settings.Global.getUriFor(Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1375 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1376 | SettingsObserver(Handler handler) { |
| 1377 | super(handler); |
| 1378 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1379 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1380 | void observe() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1381 | ContentResolver resolver = getContext().getContentResolver(); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1382 | resolver.registerContentObserver(NOTIFICATION_BADGING_URI, |
| 1383 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1384 | resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1385 | false, this, UserHandle.USER_ALL); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1386 | resolver.registerContentObserver(NOTIFICATION_RATE_LIMIT_URI, |
| 1387 | false, this, UserHandle.USER_ALL); |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 1388 | resolver.registerContentObserver(NOTIFICATION_BUBBLES_URI, |
| 1389 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1390 | update(null); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1393 | @Override public void onChange(boolean selfChange, Uri uri) { |
| 1394 | update(uri); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1395 | } |
| 1396 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1397 | public void update(Uri uri) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1398 | ContentResolver resolver = getContext().getContentResolver(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1399 | if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { |
zhoulei | 7e37697 | 2017-05-17 18:41:25 +0800 | [diff] [blame] | 1400 | boolean pulseEnabled = Settings.System.getIntForUser(resolver, |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 1401 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0, UserHandle.USER_CURRENT) |
| 1402 | != 0; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1403 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 1404 | mNotificationPulseEnabled = pulseEnabled; |
| 1405 | updateNotificationPulse(); |
| 1406 | } |
| 1407 | } |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1408 | if (uri == null || NOTIFICATION_RATE_LIMIT_URI.equals(uri)) { |
| 1409 | mMaxPackageEnqueueRate = Settings.Global.getFloat(resolver, |
| 1410 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, mMaxPackageEnqueueRate); |
| 1411 | } |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1412 | if (uri == null || NOTIFICATION_BADGING_URI.equals(uri)) { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1413 | mPreferencesHelper.updateBadgingEnabled(); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1414 | } |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 1415 | if (uri == null || NOTIFICATION_BUBBLES_URI.equals(uri)) { |
| 1416 | mPreferencesHelper.updateBubblesEnabled(); |
| 1417 | } |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1418 | } |
| 1419 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1420 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1421 | private SettingsObserver mSettingsObserver; |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1422 | protected ZenModeHelper mZenModeHelper; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1423 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1424 | static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) { |
| 1425 | int[] ar = r.getIntArray(resid); |
| 1426 | if (ar == null) { |
| 1427 | return def; |
| 1428 | } |
| 1429 | final int len = ar.length > maxlen ? maxlen : ar.length; |
| 1430 | long[] out = new long[len]; |
| 1431 | for (int i=0; i<len; i++) { |
| 1432 | out[i] = ar[i]; |
| 1433 | } |
| 1434 | return out; |
| 1435 | } |
| 1436 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 1437 | public NotificationManagerService(Context context) { |
| 1438 | super(context); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 1439 | Notification.processWhitelistToken = WHITELIST_TOKEN; |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 1440 | } |
| 1441 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1442 | // TODO - replace these methods with a single VisibleForTesting constructor |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1443 | @VisibleForTesting |
| 1444 | void setAudioManager(AudioManager audioMananger) { |
| 1445 | mAudioManager = audioMananger; |
| 1446 | } |
| 1447 | |
| 1448 | @VisibleForTesting |
Julia Reynolds | db7081e | 2019-01-03 14:35:38 -0500 | [diff] [blame] | 1449 | void setHints(int hints) { |
| 1450 | mListenerHints = hints; |
| 1451 | } |
| 1452 | |
| 1453 | @VisibleForTesting |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1454 | void setVibrator(Vibrator vibrator) { |
| 1455 | mVibrator = vibrator; |
| 1456 | } |
| 1457 | |
| 1458 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1459 | void setLights(Light light) { |
| 1460 | mNotificationLight = light; |
| 1461 | mAttentionLight = light; |
Julia Reynolds | 033a412 | 2017-01-31 16:50:38 -0500 | [diff] [blame] | 1462 | mNotificationPulseEnabled = true; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | @VisibleForTesting |
| 1466 | void setScreenOn(boolean on) { |
| 1467 | mScreenOn = on; |
| 1468 | } |
| 1469 | |
| 1470 | @VisibleForTesting |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1471 | int getNotificationRecordCount() { |
| 1472 | synchronized (mNotificationLock) { |
| 1473 | int count = mNotificationList.size() + mNotificationsByKey.size() |
| 1474 | + mSummaryByGroupKey.size() + mEnqueuedNotifications.size(); |
| 1475 | // subtract duplicates |
| 1476 | for (NotificationRecord posted : mNotificationList) { |
| 1477 | if (mNotificationsByKey.containsKey(posted.getKey())) { |
| 1478 | count--; |
| 1479 | } |
| 1480 | if (posted.sbn.isGroup() && posted.getNotification().isGroupSummary()) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1481 | count--; |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | return count; |
| 1486 | } |
| 1487 | } |
| 1488 | |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 1489 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1490 | void clearNotifications() { |
| 1491 | mEnqueuedNotifications.clear(); |
| 1492 | mNotificationList.clear(); |
| 1493 | mNotificationsByKey.clear(); |
| 1494 | mSummaryByGroupKey.clear(); |
| 1495 | } |
| 1496 | |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1497 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1498 | void addNotification(NotificationRecord r) { |
| 1499 | mNotificationList.add(r); |
| 1500 | mNotificationsByKey.put(r.sbn.getKey(), r); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1501 | if (r.sbn.isGroup()) { |
| 1502 | mSummaryByGroupKey.put(r.getGroupKey(), r); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | @VisibleForTesting |
| 1507 | void addEnqueuedNotification(NotificationRecord r) { |
| 1508 | mEnqueuedNotifications.add(r); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | @VisibleForTesting |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1512 | NotificationRecord getNotificationRecord(String key) { |
| 1513 | return mNotificationsByKey.get(key); |
| 1514 | } |
| 1515 | |
| 1516 | |
| 1517 | @VisibleForTesting |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1518 | void setSystemReady(boolean systemReady) { |
| 1519 | mSystemReady = systemReady; |
| 1520 | } |
| 1521 | |
| 1522 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1523 | void setHandler(WorkerHandler handler) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1524 | mHandler = handler; |
| 1525 | } |
| 1526 | |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1527 | @VisibleForTesting |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 1528 | void setFallbackVibrationPattern(long[] vibrationPattern) { |
| 1529 | mFallbackVibrationPattern = vibrationPattern; |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1530 | } |
| 1531 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1532 | @VisibleForTesting |
| 1533 | void setPackageManager(IPackageManager packageManager) { |
| 1534 | mPackageManager = packageManager; |
| 1535 | } |
| 1536 | |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1537 | @VisibleForTesting |
| 1538 | void setRankingHelper(RankingHelper rankingHelper) { |
| 1539 | mRankingHelper = rankingHelper; |
| 1540 | } |
| 1541 | |
| 1542 | @VisibleForTesting |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1543 | void setPreferencesHelper(PreferencesHelper prefHelper) { mPreferencesHelper = prefHelper; } |
| 1544 | |
| 1545 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1546 | void setRankingHandler(RankingHandler rankingHandler) { |
| 1547 | mRankingHandler = rankingHandler; |
| 1548 | } |
| 1549 | |
| 1550 | @VisibleForTesting |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 1551 | void setZenHelper(ZenModeHelper zenHelper) { |
| 1552 | mZenModeHelper = zenHelper; |
| 1553 | } |
| 1554 | |
| 1555 | @VisibleForTesting |
Adora Zhang | 963328f | 2018-11-15 18:17:19 -0800 | [diff] [blame] | 1556 | void setIsAutomotive(boolean isAutomotive) { |
| 1557 | mIsAutomotive = isAutomotive; |
| 1558 | } |
| 1559 | |
| 1560 | @VisibleForTesting |
Qiao (Adora) Zhang | 47b553c | 2019-04-15 20:42:57 +0000 | [diff] [blame] | 1561 | void setNotificationEffectsEnabledForAutomotive(boolean isEnabled) { |
| 1562 | mNotificationEffectsEnabledForAutomotive = isEnabled; |
| 1563 | } |
| 1564 | |
| 1565 | @VisibleForTesting |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1566 | void setIsTelevision(boolean isTelevision) { |
| 1567 | mIsTelevision = isTelevision; |
| 1568 | } |
| 1569 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 1570 | @VisibleForTesting |
| 1571 | void setUsageStats(NotificationUsageStats us) { |
| 1572 | mUsageStats = us; |
| 1573 | } |
| 1574 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 1575 | @VisibleForTesting |
| 1576 | void setAccessibilityManager(AccessibilityManager am) { |
| 1577 | mAccessibilityManager = am; |
| 1578 | } |
| 1579 | |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1580 | // 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] | 1581 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1582 | void init(Looper looper, IPackageManager packageManager, |
| 1583 | PackageManager packageManagerClient, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1584 | LightsManager lightsManager, NotificationListeners notificationListeners, |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1585 | NotificationAssistants notificationAssistants, ConditionProviders conditionProviders, |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1586 | ICompanionDeviceManager companionManager, SnoozeHelper snoozeHelper, |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1587 | NotificationUsageStats usageStats, AtomicFile policyFile, |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 1588 | ActivityManager activityManager, GroupHelper groupHelper, IActivityManager am, |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 1589 | UsageStatsManagerInternal appUsageStats, DevicePolicyManagerInternal dpm, |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 1590 | IUriGrantsManager ugm, UriGrantsManagerInternal ugmInternal, AppOpsManager appOps, |
| 1591 | UserManager userManager) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1592 | Resources resources = getContext().getResources(); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1593 | mMaxPackageEnqueueRate = Settings.Global.getFloat(getContext().getContentResolver(), |
| 1594 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, |
| 1595 | DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE); |
| 1596 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 1597 | mAccessibilityManager = |
| 1598 | (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1599 | mAm = am; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 1600 | mUgm = ugm; |
| 1601 | mUgmInternal = ugmInternal; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1602 | mPackageManager = packageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1603 | mPackageManagerClient = packageManagerClient; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1604 | mAppOps = appOps; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1605 | mVibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE); |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 1606 | mAppUsageStats = appUsageStats; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1607 | mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1608 | mCompanionManager = companionManager; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1609 | mActivityManager = activityManager; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 1610 | mDeviceIdleController = IDeviceIdleController.Stub.asInterface( |
| 1611 | ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER)); |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 1612 | mDpm = dpm; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 1613 | mUm = userManager; |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 1614 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1615 | mHandler = new WorkerHandler(looper); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1616 | mRankingThread.start(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1617 | String[] extractorNames; |
| 1618 | try { |
| 1619 | extractorNames = resources.getStringArray(R.array.config_notificationSignalExtractors); |
| 1620 | } catch (Resources.NotFoundException e) { |
| 1621 | extractorNames = new String[0]; |
| 1622 | } |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1623 | mUsageStats = usageStats; |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 1624 | mMetricsLogger = new MetricsLogger(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1625 | mRankingHandler = new RankingHandlerWorker(mRankingThread.getLooper()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1626 | mConditionProviders = conditionProviders; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1627 | mZenModeHelper = new ZenModeHelper(getContext(), mHandler.getLooper(), mConditionProviders); |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 1628 | mZenModeHelper.addCallback(new ZenModeHelper.Callback() { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1629 | @Override |
| 1630 | public void onConfigChanged() { |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 1631 | handleSavePolicyFile(); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1632 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1633 | |
| 1634 | @Override |
| 1635 | void onZenModeChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1636 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED); |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1637 | getContext().sendBroadcastAsUser( |
Jason Monk | 6350674 | 2015-12-16 12:06:51 -0500 | [diff] [blame] | 1638 | new Intent(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL) |
| 1639 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT), |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1640 | UserHandle.ALL, android.Manifest.permission.MANAGE_NOTIFICATIONS); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1641 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1642 | updateInterruptionFilterLocked(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1643 | } |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 1644 | mRankingHandler.requestSort(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1645 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1646 | |
| 1647 | @Override |
| 1648 | void onPolicyChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1649 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED); |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 1650 | mRankingHandler.requestSort(); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1651 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1652 | }); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1653 | mPreferencesHelper = new PreferencesHelper(getContext(), |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 1654 | mPackageManagerClient, |
| 1655 | mRankingHandler, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1656 | mZenModeHelper); |
| 1657 | mRankingHelper = new RankingHelper(getContext(), |
| 1658 | mRankingHandler, |
| 1659 | mPreferencesHelper, |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 1660 | mZenModeHelper, |
| 1661 | mUsageStats, |
| 1662 | extractorNames); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1663 | mSnoozeHelper = snoozeHelper; |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1664 | mGroupHelper = groupHelper; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 1665 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1666 | // This is a ManagedServices object that keeps track of the listeners. |
| 1667 | mListeners = notificationListeners; |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1668 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 1669 | // This is a MangedServices object that keeps track of the assistant. |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1670 | mAssistants = notificationAssistants; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1671 | |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 1672 | // Needs to be set before loadPolicyFile |
| 1673 | mAllowedManagedServicePackages = this::canUseManagedServices; |
| 1674 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1675 | mPolicyFile = policyFile; |
| 1676 | loadPolicyFile(); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1677 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1678 | mStatusBar = getLocalService(StatusBarManagerInternal.class); |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 1679 | if (mStatusBar != null) { |
| 1680 | mStatusBar.setNotificationDelegate(mNotificationDelegate); |
| 1681 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1683 | mNotificationLight = lightsManager.getLight(LightsManager.LIGHT_ID_NOTIFICATIONS); |
| 1684 | mAttentionLight = lightsManager.getLight(LightsManager.LIGHT_ID_ATTENTION); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1685 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1686 | mFallbackVibrationPattern = getLongArray(resources, |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 1687 | R.array.config_notificationFallbackVibePattern, |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1688 | VIBRATE_PATTERN_MAXLEN, |
| 1689 | DEFAULT_VIBRATE_PATTERN); |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 1690 | mInCallNotificationUri = Uri.parse("file://" + |
| 1691 | resources.getString(R.string.config_inCallNotificationSound)); |
| 1692 | mInCallNotificationAudioAttributes = new AudioAttributes.Builder() |
| 1693 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 1694 | .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 1695 | .build(); |
| 1696 | mInCallNotificationVolume = resources.getFloat(R.dimen.config_inCallNotificationVolume); |
| 1697 | |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 1698 | mUseAttentionLight = resources.getBoolean(R.bool.config_useAttentionLight); |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 1699 | mHasLight = |
| 1700 | resources.getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed); |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 1701 | |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1702 | // Don't start allowing notifications until the setup wizard has run once. |
| 1703 | // After that, including subsequent boots, init with notifications turned on. |
| 1704 | // This works on the first boot because the setup wizard will toggle this |
| 1705 | // 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] | 1706 | if (0 == Settings.Global.getInt(getContext().getContentResolver(), |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1707 | Settings.Global.DEVICE_PROVISIONED, 0)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1708 | mDisableNotificationEffects = true; |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1709 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1710 | mZenModeHelper.initZenMode(); |
John Spurlock | f370177 | 2015-02-12 13:29:37 -0500 | [diff] [blame] | 1711 | mInterruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1712 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1713 | mUserProfiles.updateCache(getContext()); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 1714 | listenForCallState(); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1715 | |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1716 | mSettingsObserver = new SettingsObserver(mHandler); |
| 1717 | |
| 1718 | mArchive = new Archive(resources.getInteger( |
| 1719 | R.integer.config_notificationServiceArchiveSize)); |
| 1720 | |
| 1721 | mIsTelevision = mPackageManagerClient.hasSystemFeature(FEATURE_LEANBACK) |
| 1722 | || mPackageManagerClient.hasSystemFeature(FEATURE_TELEVISION); |
Adora Zhang | 963328f | 2018-11-15 18:17:19 -0800 | [diff] [blame] | 1723 | |
| 1724 | mIsAutomotive = |
| 1725 | mPackageManagerClient.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0); |
Qiao (Adora) Zhang | 47b553c | 2019-04-15 20:42:57 +0000 | [diff] [blame] | 1726 | mNotificationEffectsEnabledForAutomotive = |
| 1727 | resources.getBoolean(R.bool.config_enableServerNotificationEffectsForAutomotive); |
Julia Reynolds | 413ba84 | 2019-01-11 10:38:08 -0500 | [diff] [blame] | 1728 | |
| 1729 | mPreferencesHelper.lockChannelsForOEM(getContext().getResources().getStringArray( |
| 1730 | com.android.internal.R.array.config_nonBlockableNotificationPackages)); |
Julia Reynolds | e99db5a | 2019-04-16 12:50:04 -0400 | [diff] [blame] | 1731 | |
| 1732 | mZenModeHelper.setPriorityOnlyDndExemptPackages(getContext().getResources().getStringArray( |
| 1733 | com.android.internal.R.array.config_priorityOnlyDndExemptPackages)); |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | @Override |
| 1737 | public void onStart() { |
| 1738 | SnoozeHelper snoozeHelper = new SnoozeHelper(getContext(), new SnoozeHelper.Callback() { |
| 1739 | @Override |
| 1740 | public void repost(int userId, NotificationRecord r) { |
| 1741 | try { |
| 1742 | if (DBG) { |
| 1743 | Slog.d(TAG, "Reposting " + r.getKey()); |
| 1744 | } |
| 1745 | enqueueNotificationInternal(r.sbn.getPackageName(), r.sbn.getOpPkg(), |
| 1746 | r.sbn.getUid(), r.sbn.getInitialPid(), r.sbn.getTag(), r.sbn.getId(), |
| 1747 | r.sbn.getNotification(), userId); |
| 1748 | } catch (Exception e) { |
| 1749 | Slog.e(TAG, "Cannot un-snooze notification", e); |
| 1750 | } |
| 1751 | } |
| 1752 | }, mUserProfiles); |
| 1753 | |
| 1754 | final File systemDir = new File(Environment.getDataDirectory(), "system"); |
| 1755 | |
| 1756 | init(Looper.myLooper(), |
| 1757 | AppGlobals.getPackageManager(), getContext().getPackageManager(), |
| 1758 | getLocalService(LightsManager.class), |
| 1759 | new NotificationListeners(AppGlobals.getPackageManager()), |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 1760 | new NotificationAssistants(getContext(), mNotificationLock, mUserProfiles, |
| 1761 | AppGlobals.getPackageManager()), |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1762 | new ConditionProviders(getContext(), mUserProfiles, AppGlobals.getPackageManager()), |
| 1763 | null, snoozeHelper, new NotificationUsageStats(getContext()), |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 1764 | new AtomicFile(new File( |
| 1765 | systemDir, "notification_policy.xml"), "notification-policy"), |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1766 | (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE), |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 1767 | getGroupHelper(), ActivityManager.getService(), |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 1768 | LocalServices.getService(UsageStatsManagerInternal.class), |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 1769 | LocalServices.getService(DevicePolicyManagerInternal.class), |
| 1770 | UriGrantsManager.getService(), |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1771 | LocalServices.getService(UriGrantsManagerInternal.class), |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 1772 | (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE), |
| 1773 | getContext().getSystemService(UserManager.class)); |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1774 | |
Mike Lockwood | 35e16bf | 2010-11-30 19:53:36 -0500 | [diff] [blame] | 1775 | // register for various Intents |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1776 | IntentFilter filter = new IntentFilter(); |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1777 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 1778 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1779 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1780 | filter.addAction(Intent.ACTION_USER_PRESENT); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1781 | filter.addAction(Intent.ACTION_USER_STOPPED); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1782 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1783 | filter.addAction(Intent.ACTION_USER_ADDED); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1784 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1785 | filter.addAction(Intent.ACTION_USER_UNLOCKED); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1786 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1787 | getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter, null, null); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1788 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1789 | IntentFilter pkgFilter = new IntentFilter(); |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1790 | pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1791 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
Daniel Sandler | aac0eb0 | 2011-08-06 22:51:56 -0400 | [diff] [blame] | 1792 | pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1793 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 1794 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 1795 | pkgFilter.addDataScheme("package"); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1796 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, pkgFilter, null, |
| 1797 | null); |
| 1798 | |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1799 | IntentFilter suspendedPkgFilter = new IntentFilter(); |
| 1800 | suspendedPkgFilter.addAction(Intent.ACTION_PACKAGES_SUSPENDED); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1801 | suspendedPkgFilter.addAction(Intent.ACTION_PACKAGES_UNSUSPENDED); |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 1802 | suspendedPkgFilter.addAction(Intent.ACTION_DISTRACTING_PACKAGES_CHANGED); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1803 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, |
| 1804 | suspendedPkgFilter, null, null); |
| 1805 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1806 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1807 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, sdFilter, null, |
| 1808 | null); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1809 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1810 | IntentFilter timeoutFilter = new IntentFilter(ACTION_NOTIFICATION_TIMEOUT); |
| 1811 | timeoutFilter.addDataScheme(SCHEME_TIMEOUT); |
| 1812 | getContext().registerReceiver(mNotificationTimeoutReceiver, timeoutFilter); |
| 1813 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1814 | IntentFilter settingsRestoredFilter = new IntentFilter(Intent.ACTION_SETTING_RESTORED); |
| 1815 | getContext().registerReceiver(mRestoreReceiver, settingsRestoredFilter); |
| 1816 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1817 | IntentFilter localeChangedFilter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED); |
| 1818 | getContext().registerReceiver(mLocaleChangeReceiver, localeChangedFilter); |
| 1819 | |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 1820 | publishBinderService(Context.NOTIFICATION_SERVICE, mService, /* allowIsolated= */ false, |
| 1821 | DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1822 | publishLocalService(NotificationManagerInternal.class, mInternalService); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1823 | } |
| 1824 | |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 1825 | private void registerDeviceConfigChange() { |
Matt Pape | 15769e2 | 2019-04-19 12:31:24 -0700 | [diff] [blame] | 1826 | DeviceConfig.addOnPropertiesChangedListener( |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 1827 | DeviceConfig.NAMESPACE_SYSTEMUI, |
| 1828 | getContext().getMainExecutor(), |
Matt Pape | 15769e2 | 2019-04-19 12:31:24 -0700 | [diff] [blame] | 1829 | (properties) -> { |
| 1830 | if (!DeviceConfig.NAMESPACE_SYSTEMUI.equals(properties.getNamespace())) { |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 1831 | return; |
| 1832 | } |
Matt Pape | 15769e2 | 2019-04-19 12:31:24 -0700 | [diff] [blame] | 1833 | if (properties.getKeyset() |
| 1834 | .contains(SystemUiDeviceConfigFlags.NAS_DEFAULT_SERVICE)) { |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 1835 | mAssistants.resetDefaultAssistantsIfNecessary(); |
| 1836 | } |
| 1837 | }); |
| 1838 | } |
| 1839 | |
| 1840 | |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1841 | private GroupHelper getGroupHelper() { |
Adora Zhang | 48dd614a8 | 2018-06-25 19:18:41 -0700 | [diff] [blame] | 1842 | mAutoGroupAtCount = |
| 1843 | getContext().getResources().getInteger(R.integer.config_autoGroupAtCount); |
| 1844 | return new GroupHelper(mAutoGroupAtCount, new GroupHelper.Callback() { |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1845 | @Override |
| 1846 | public void addAutoGroup(String key) { |
| 1847 | synchronized (mNotificationLock) { |
| 1848 | addAutogroupKeyLocked(key); |
| 1849 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | @Override |
| 1853 | public void removeAutoGroup(String key) { |
| 1854 | synchronized (mNotificationLock) { |
| 1855 | removeAutogroupKeyLocked(key); |
| 1856 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | @Override |
| 1860 | public void addAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 1861 | createAutoGroupSummary(userId, pkg, triggeringKey); |
| 1862 | } |
| 1863 | |
| 1864 | @Override |
| 1865 | public void removeAutoGroupSummary(int userId, String pkg) { |
| 1866 | synchronized (mNotificationLock) { |
| 1867 | clearAutogroupSummaryLocked(userId, pkg); |
| 1868 | } |
| 1869 | } |
| 1870 | }); |
| 1871 | } |
| 1872 | |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1873 | private void sendRegisteredOnlyBroadcast(String action) { |
Julia Reynolds | 4c456dd | 2019-01-07 12:22:00 -0500 | [diff] [blame] | 1874 | Intent intent = new Intent(action); |
| 1875 | getContext().sendBroadcastAsUser(intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
| 1876 | UserHandle.ALL, null); |
| 1877 | // explicitly send the broadcast to all DND packages, even if they aren't currently running |
| 1878 | intent.setFlags(0); |
| 1879 | final Set<String> dndApprovedPackages = mConditionProviders.getAllowedPackages(); |
| 1880 | for (String pkg : dndApprovedPackages) { |
| 1881 | intent.setPackage(pkg); |
| 1882 | getContext().sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1883 | } |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1884 | } |
| 1885 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1886 | @Override |
| 1887 | public void onBootPhase(int phase) { |
| 1888 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 1889 | // no beeping until we're basically done booting |
| 1890 | mSystemReady = true; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1891 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1892 | // Grab our optional AudioService |
| 1893 | mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1894 | mAudioManagerInternal = getLocalService(AudioManagerInternal.class); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1895 | mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1896 | mZenModeHelper.onSystemReady(); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 1897 | mRoleObserver = new RoleObserver(getContext().getSystemService(RoleManager.class), |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 1898 | mPackageManager, getContext().getMainExecutor()); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 1899 | mRoleObserver.init(); |
Adam Lesinski | a6db4ab | 2014-03-24 12:31:45 -0700 | [diff] [blame] | 1900 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 1901 | // This observer will force an update when observe is called, causing us to |
| 1902 | // bind to listener services. |
| 1903 | mSettingsObserver.observe(); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1904 | mListeners.onBootPhaseAppsCanStart(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1905 | mAssistants.onBootPhaseAppsCanStart(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1906 | mConditionProviders.onBootPhaseAppsCanStart(); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 1907 | registerDeviceConfigChange(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1911 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1912 | private void updateListenerHintsLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1913 | final int hints = calculateHints(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1914 | if (hints == mListenerHints) return; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1915 | ZenLog.traceListenerHintsChanged(mListenerHints, hints, mEffectsSuppressors.size()); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1916 | mListenerHints = hints; |
| 1917 | scheduleListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 1918 | } |
| 1919 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1920 | @GuardedBy("mNotificationLock") |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1921 | private void updateEffectsSuppressorLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1922 | final long updatedSuppressedEffects = calculateSuppressedEffects(); |
| 1923 | if (updatedSuppressedEffects == mZenModeHelper.getSuppressedEffects()) return; |
| 1924 | final List<ComponentName> suppressors = getSuppressors(); |
| 1925 | ZenLog.traceEffectsSuppressorChanged(mEffectsSuppressors, suppressors, updatedSuppressedEffects); |
| 1926 | mEffectsSuppressors = suppressors; |
| 1927 | mZenModeHelper.setSuppressedEffects(updatedSuppressedEffects); |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1928 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1929 | } |
| 1930 | |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 1931 | private void exitIdle() { |
| 1932 | try { |
| 1933 | if (mDeviceIdleController != null) { |
| 1934 | mDeviceIdleController.exitIdle("notification interaction"); |
| 1935 | } |
| 1936 | } catch (RemoteException e) { |
| 1937 | } |
| 1938 | } |
| 1939 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1940 | private void updateNotificationChannelInt(String pkg, int uid, NotificationChannel channel, |
| 1941 | boolean fromListener) { |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1942 | if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) { |
| 1943 | // cancel |
| 1944 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channel.getId(), 0, 0, true, |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1945 | UserHandle.getUserId(uid), REASON_CHANNEL_BANNED, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1946 | null); |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1947 | if (isUidSystemOrPhone(uid)) { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1948 | IntArray profileIds = mUserProfiles.getCurrentProfileIds(); |
| 1949 | int N = profileIds.size(); |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1950 | for (int i = 0; i < N; i++) { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1951 | int profileId = profileIds.get(i); |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1952 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channel.getId(), 0, 0, true, |
| 1953 | profileId, REASON_CHANNEL_BANNED, |
| 1954 | null); |
| 1955 | } |
| 1956 | } |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1957 | } |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1958 | final NotificationChannel preUpdate = |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1959 | mPreferencesHelper.getNotificationChannel(pkg, uid, channel.getId(), true); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1960 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1961 | mPreferencesHelper.updateNotificationChannel(pkg, uid, channel, true); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1962 | maybeNotifyChannelOwner(pkg, uid, preUpdate, channel); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1963 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1964 | if (!fromListener) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1965 | final NotificationChannel modifiedChannel = |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1966 | mPreferencesHelper.getNotificationChannel(pkg, uid, channel.getId(), false); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1967 | mListeners.notifyNotificationChannelChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1968 | pkg, UserHandle.getUserHandleForUid(uid), |
| 1969 | modifiedChannel, NOTIFICATION_CHANNEL_OR_GROUP_UPDATED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1970 | } |
| 1971 | |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 1972 | handleSavePolicyFile(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1973 | } |
| 1974 | |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1975 | private void maybeNotifyChannelOwner(String pkg, int uid, NotificationChannel preUpdate, |
| 1976 | NotificationChannel update) { |
| 1977 | try { |
| 1978 | if ((preUpdate.getImportance() == IMPORTANCE_NONE |
| 1979 | && update.getImportance() != IMPORTANCE_NONE) |
| 1980 | || (preUpdate.getImportance() != IMPORTANCE_NONE |
| 1981 | && update.getImportance() == IMPORTANCE_NONE)) { |
| 1982 | getContext().sendBroadcastAsUser( |
| 1983 | new Intent(ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED) |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 1984 | .putExtra(NotificationManager.EXTRA_NOTIFICATION_CHANNEL_ID, |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1985 | update.getId()) |
| 1986 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, |
| 1987 | update.getImportance() == IMPORTANCE_NONE) |
| 1988 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 1989 | .setPackage(pkg), |
| 1990 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 1991 | } |
| 1992 | } catch (SecurityException e) { |
| 1993 | Slog.w(TAG, "Can't notify app about channel change", e); |
| 1994 | } |
| 1995 | } |
| 1996 | |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1997 | private void createNotificationChannelGroup(String pkg, int uid, NotificationChannelGroup group, |
| 1998 | boolean fromApp, boolean fromListener) { |
| 1999 | Preconditions.checkNotNull(group); |
| 2000 | Preconditions.checkNotNull(pkg); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2001 | |
| 2002 | final NotificationChannelGroup preUpdate = |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2003 | mPreferencesHelper.getNotificationChannelGroup(group.getId(), pkg, uid); |
| 2004 | mPreferencesHelper.createNotificationChannelGroup(pkg, uid, group, |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2005 | fromApp); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2006 | if (!fromApp) { |
| 2007 | maybeNotifyChannelGroupOwner(pkg, uid, preUpdate, group); |
| 2008 | } |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2009 | if (!fromListener) { |
| 2010 | mListeners.notifyNotificationChannelGroupChanged(pkg, |
| 2011 | UserHandle.of(UserHandle.getCallingUserId()), group, |
| 2012 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
| 2013 | } |
| 2014 | } |
| 2015 | |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2016 | private void maybeNotifyChannelGroupOwner(String pkg, int uid, |
| 2017 | NotificationChannelGroup preUpdate, NotificationChannelGroup update) { |
| 2018 | try { |
| 2019 | if (preUpdate.isBlocked() != update.isBlocked()) { |
| 2020 | getContext().sendBroadcastAsUser( |
| 2021 | new Intent(ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED) |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 2022 | .putExtra(NotificationManager.EXTRA_NOTIFICATION_CHANNEL_GROUP_ID, |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2023 | update.getId()) |
| 2024 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, |
| 2025 | update.isBlocked()) |
| 2026 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 2027 | .setPackage(pkg), |
| 2028 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 2029 | } |
| 2030 | } catch (SecurityException e) { |
| 2031 | Slog.w(TAG, "Can't notify app about group change", e); |
| 2032 | } |
| 2033 | } |
| 2034 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2035 | private ArrayList<ComponentName> getSuppressors() { |
| 2036 | ArrayList<ComponentName> names = new ArrayList<ComponentName>(); |
| 2037 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 2038 | ArraySet<ComponentName> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2039 | |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 2040 | for (ComponentName info : serviceInfoList) { |
| 2041 | names.add(info); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2042 | } |
| 2043 | } |
| 2044 | |
| 2045 | return names; |
| 2046 | } |
| 2047 | |
| 2048 | private boolean removeDisabledHints(ManagedServiceInfo info) { |
| 2049 | return removeDisabledHints(info, 0); |
| 2050 | } |
| 2051 | |
| 2052 | private boolean removeDisabledHints(ManagedServiceInfo info, int hints) { |
| 2053 | boolean removed = false; |
| 2054 | |
| 2055 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 2056 | final int hint = mListenersDisablingEffects.keyAt(i); |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 2057 | final ArraySet<ComponentName> listeners = mListenersDisablingEffects.valueAt(i); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2058 | |
| 2059 | if (hints == 0 || (hint & hints) == hint) { |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 2060 | removed |= listeners.remove(info.component); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2061 | } |
| 2062 | } |
| 2063 | |
| 2064 | return removed; |
| 2065 | } |
| 2066 | |
| 2067 | private void addDisabledHints(ManagedServiceInfo info, int hints) { |
| 2068 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 2069 | addDisabledHint(info, HINT_HOST_DISABLE_EFFECTS); |
| 2070 | } |
| 2071 | |
| 2072 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 2073 | addDisabledHint(info, HINT_HOST_DISABLE_NOTIFICATION_EFFECTS); |
| 2074 | } |
| 2075 | |
| 2076 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 2077 | addDisabledHint(info, HINT_HOST_DISABLE_CALL_EFFECTS); |
| 2078 | } |
| 2079 | } |
| 2080 | |
| 2081 | private void addDisabledHint(ManagedServiceInfo info, int hint) { |
| 2082 | if (mListenersDisablingEffects.indexOfKey(hint) < 0) { |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 2083 | mListenersDisablingEffects.put(hint, new ArraySet<>()); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2084 | } |
| 2085 | |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 2086 | ArraySet<ComponentName> hintListeners = mListenersDisablingEffects.get(hint); |
| 2087 | hintListeners.add(info.component); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | private int calculateHints() { |
| 2091 | int hints = 0; |
| 2092 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 2093 | int hint = mListenersDisablingEffects.keyAt(i); |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 2094 | ArraySet<ComponentName> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2095 | |
| 2096 | if (!serviceInfoList.isEmpty()) { |
| 2097 | hints |= hint; |
| 2098 | } |
| 2099 | } |
| 2100 | |
| 2101 | return hints; |
| 2102 | } |
| 2103 | |
| 2104 | private long calculateSuppressedEffects() { |
| 2105 | int hints = calculateHints(); |
| 2106 | long suppressedEffects = 0; |
| 2107 | |
| 2108 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 2109 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_ALL; |
| 2110 | } |
| 2111 | |
| 2112 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 2113 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_NOTIFICATIONS; |
| 2114 | } |
| 2115 | |
| 2116 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 2117 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_CALLS; |
| 2118 | } |
| 2119 | |
| 2120 | return suppressedEffects; |
| 2121 | } |
| 2122 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2123 | @GuardedBy("mNotificationLock") |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2124 | private void updateInterruptionFilterLocked() { |
| 2125 | int interruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
| 2126 | if (interruptionFilter == mInterruptionFilter) return; |
| 2127 | mInterruptionFilter = interruptionFilter; |
| 2128 | scheduleInterruptionFilterChanged(interruptionFilter); |
| 2129 | } |
| 2130 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 2131 | @VisibleForTesting |
| 2132 | INotificationManager getBinderService() { |
| 2133 | return INotificationManager.Stub.asInterface(mService); |
| 2134 | } |
| 2135 | |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 2136 | /** |
| 2137 | * Report to usage stats that the notification was seen. |
| 2138 | * @param r notification record |
| 2139 | */ |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 2140 | @GuardedBy("mNotificationLock") |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 2141 | protected void reportSeen(NotificationRecord r) { |
Julia Reynolds | 9533413 | 2018-12-19 11:15:35 -0500 | [diff] [blame] | 2142 | if (!r.isProxied()) { |
| 2143 | mAppUsageStats.reportEvent(r.sbn.getPackageName(), |
| 2144 | getRealUserId(r.sbn.getUserId()), |
| 2145 | UsageEvents.Event.NOTIFICATION_SEEN); |
| 2146 | } |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 2147 | } |
| 2148 | |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 2149 | protected int calculateSuppressedVisualEffects(Policy incomingPolicy, Policy currPolicy, |
| 2150 | int targetSdkVersion) { |
| 2151 | if (incomingPolicy.suppressedVisualEffects == SUPPRESSED_EFFECTS_UNSET) { |
| 2152 | return incomingPolicy.suppressedVisualEffects; |
| 2153 | } |
| 2154 | final int[] effectsIntroducedInP = { |
| 2155 | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT, |
| 2156 | SUPPRESSED_EFFECT_LIGHTS, |
| 2157 | SUPPRESSED_EFFECT_PEEK, |
| 2158 | SUPPRESSED_EFFECT_STATUS_BAR, |
| 2159 | SUPPRESSED_EFFECT_BADGE, |
| 2160 | SUPPRESSED_EFFECT_AMBIENT, |
| 2161 | SUPPRESSED_EFFECT_NOTIFICATION_LIST |
| 2162 | }; |
| 2163 | |
| 2164 | int newSuppressedVisualEffects = incomingPolicy.suppressedVisualEffects; |
Jeff Sharkey | aa1a911 | 2018-04-10 15:18:12 -0600 | [diff] [blame] | 2165 | if (targetSdkVersion < Build.VERSION_CODES.P) { |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 2166 | // unset higher order bits introduced in P, maintain the user's higher order bits |
| 2167 | for (int i = 0; i < effectsIntroducedInP.length ; i++) { |
| 2168 | newSuppressedVisualEffects &= ~effectsIntroducedInP[i]; |
| 2169 | newSuppressedVisualEffects |= |
| 2170 | (currPolicy.suppressedVisualEffects & effectsIntroducedInP[i]); |
| 2171 | } |
| 2172 | // set higher order bits according to lower order bits |
| 2173 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_OFF) != 0) { |
| 2174 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_LIGHTS; |
| 2175 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 2176 | } |
| 2177 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_ON) != 0) { |
| 2178 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_PEEK; |
| 2179 | } |
| 2180 | } else { |
| 2181 | boolean hasNewEffects = (newSuppressedVisualEffects |
| 2182 | - SUPPRESSED_EFFECT_SCREEN_ON - SUPPRESSED_EFFECT_SCREEN_OFF) > 0; |
| 2183 | // if any of the new effects introduced in P are set |
| 2184 | if (hasNewEffects) { |
| 2185 | // clear out the deprecated effects |
| 2186 | newSuppressedVisualEffects &= ~ (SUPPRESSED_EFFECT_SCREEN_ON |
| 2187 | | SUPPRESSED_EFFECT_SCREEN_OFF); |
| 2188 | |
| 2189 | // set the deprecated effects according to the new more specific effects |
| 2190 | if ((newSuppressedVisualEffects & Policy.SUPPRESSED_EFFECT_PEEK) != 0) { |
| 2191 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_SCREEN_ON; |
| 2192 | } |
| 2193 | if ((newSuppressedVisualEffects & Policy.SUPPRESSED_EFFECT_LIGHTS) != 0 |
| 2194 | && (newSuppressedVisualEffects |
| 2195 | & Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT) != 0 |
| 2196 | && (newSuppressedVisualEffects |
| 2197 | & Policy.SUPPRESSED_EFFECT_AMBIENT) != 0) { |
| 2198 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_SCREEN_OFF; |
| 2199 | } |
| 2200 | } else { |
| 2201 | // set higher order bits according to lower order bits |
| 2202 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_OFF) != 0) { |
| 2203 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_LIGHTS; |
| 2204 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 2205 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_AMBIENT; |
| 2206 | } |
| 2207 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_ON) != 0) { |
| 2208 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_PEEK; |
| 2209 | } |
| 2210 | } |
| 2211 | } |
| 2212 | |
| 2213 | return newSuppressedVisualEffects; |
| 2214 | } |
| 2215 | |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 2216 | @GuardedBy("mNotificationLock") |
| 2217 | protected void maybeRecordInterruptionLocked(NotificationRecord r) { |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 2218 | if (r.isInterruptive() && !r.hasRecordedInterruption()) { |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 2219 | mAppUsageStats.reportInterruptiveNotification(r.sbn.getPackageName(), |
| 2220 | r.getChannel().getId(), |
| 2221 | getRealUserId(r.sbn.getUserId())); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 2222 | r.setRecordedInterruption(true); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 2226 | /** |
Esteban Talavera | 917a71d | 2018-11-13 07:55:08 +0000 | [diff] [blame] | 2227 | * Report to usage stats that the user interacted with the notification. |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 2228 | * @param r notification record |
| 2229 | */ |
| 2230 | protected void reportUserInteraction(NotificationRecord r) { |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 2231 | mAppUsageStats.reportEvent(r.sbn.getPackageName(), |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 2232 | getRealUserId(r.sbn.getUserId()), |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 2233 | UsageEvents.Event.USER_INTERACTION); |
| 2234 | } |
| 2235 | |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 2236 | private int getRealUserId(int userId) { |
| 2237 | return userId == UserHandle.USER_ALL ? UserHandle.USER_SYSTEM : userId; |
| 2238 | } |
| 2239 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 2240 | @VisibleForTesting |
| 2241 | NotificationManagerInternal getInternalService() { |
| 2242 | return mInternalService; |
| 2243 | } |
| 2244 | |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 2245 | @VisibleForTesting |
| 2246 | final IBinder mService = new INotificationManager.Stub() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2247 | // Toasts |
| 2248 | // ============================================================================ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2249 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2250 | @Override |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 2251 | public void enqueueToast(String pkg, ITransientNotification callback, int duration, |
| 2252 | int displayId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2253 | { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2254 | if (DBG) { |
| 2255 | Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 2256 | + " duration=" + duration + " displayId=" + displayId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2257 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2258 | |
| 2259 | if (pkg == null || callback == null) { |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 2260 | Slog.e(TAG, "Not enqueuing toast. pkg=" + pkg + " callback=" + callback); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2261 | return ; |
| 2262 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2263 | |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 2264 | final int callingUid = Binder.getCallingUid(); |
| 2265 | final boolean isSystemToast = isCallerSystemOrPhone() |
| 2266 | || PackageManagerService.PLATFORM_PACKAGE_NAME.equals(pkg); |
| 2267 | final boolean isPackageSuspended = isPackageSuspendedForUser(pkg, callingUid); |
| 2268 | final boolean notificationsDisabledForPackage = !areNotificationsEnabledForPackage(pkg, |
| 2269 | callingUid); |
| 2270 | |
| 2271 | long callingIdentity = Binder.clearCallingIdentity(); |
| 2272 | try { |
| 2273 | final boolean appIsForeground = mActivityManager.getUidImportance(callingUid) |
| 2274 | == IMPORTANCE_FOREGROUND; |
| 2275 | if (ENABLE_BLOCKED_TOASTS && !isSystemToast && ((notificationsDisabledForPackage |
| 2276 | && !appIsForeground) || isPackageSuspended)) { |
| 2277 | Slog.e(TAG, "Suppressing toast from package " + pkg |
| 2278 | + (isPackageSuspended ? " due to package suspended." |
| 2279 | : " by user request.")); |
| 2280 | return; |
| 2281 | } |
| 2282 | } finally { |
| 2283 | Binder.restoreCallingIdentity(callingIdentity); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | synchronized (mToastQueue) { |
| 2287 | int callingPid = Binder.getCallingPid(); |
| 2288 | long callingId = Binder.clearCallingIdentity(); |
| 2289 | try { |
| 2290 | ToastRecord record; |
Beverly Tai | a7ed0ab | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 2291 | int index = indexOfToastLocked(pkg, callback); |
| 2292 | // If it's already in the queue, we update it in place, we don't |
| 2293 | // move it to the end of the queue. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2294 | if (index >= 0) { |
| 2295 | record = mToastQueue.get(index); |
| 2296 | record.update(duration); |
| 2297 | } else { |
Beverly Tai | a7ed0ab | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 2298 | // Limit the number of toasts that any given package except the android |
| 2299 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| 2300 | if (!isSystemToast) { |
| 2301 | int count = 0; |
| 2302 | final int N = mToastQueue.size(); |
| 2303 | for (int i=0; i<N; i++) { |
| 2304 | final ToastRecord r = mToastQueue.get(i); |
| 2305 | if (r.pkg.equals(pkg)) { |
| 2306 | count++; |
| 2307 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 2308 | Slog.e(TAG, "Package has already posted " + count |
| 2309 | + " toasts. Not showing more. Package=" + pkg); |
| 2310 | return; |
| 2311 | } |
| 2312 | } |
| 2313 | } |
| 2314 | } |
| 2315 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 2316 | Binder token = new Binder(); |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 2317 | mWindowManagerInternal.addWindowToken(token, TYPE_TOAST, displayId); |
| 2318 | record = new ToastRecord(callingPid, pkg, callback, duration, token, |
| 2319 | displayId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2320 | mToastQueue.add(record); |
| 2321 | index = mToastQueue.size() - 1; |
Beverly Tai | a7ed0ab | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 2322 | keepProcessAliveIfNeededLocked(callingPid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2323 | } |
| 2324 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 2325 | // new or just been updated. Call back and tell it to show itself. |
| 2326 | // If the callback fails, this will remove it from the list, so don't |
| 2327 | // assume that it's valid after this. |
| 2328 | if (index == 0) { |
| 2329 | showNextToastLocked(); |
| 2330 | } |
| 2331 | } finally { |
| 2332 | Binder.restoreCallingIdentity(callingId); |
| 2333 | } |
| 2334 | } |
| 2335 | } |
| 2336 | |
| 2337 | @Override |
| 2338 | public void cancelToast(String pkg, ITransientNotification callback) { |
| 2339 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| 2340 | |
| 2341 | if (pkg == null || callback == null) { |
| 2342 | Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback); |
| 2343 | return ; |
| 2344 | } |
| 2345 | |
| 2346 | synchronized (mToastQueue) { |
| 2347 | long callingId = Binder.clearCallingIdentity(); |
| 2348 | try { |
| 2349 | int index = indexOfToastLocked(pkg, callback); |
| 2350 | if (index >= 0) { |
| 2351 | cancelToastLocked(index); |
| 2352 | } else { |
| 2353 | Slog.w(TAG, "Toast already cancelled. pkg=" + pkg |
| 2354 | + " callback=" + callback); |
| 2355 | } |
| 2356 | } finally { |
| 2357 | Binder.restoreCallingIdentity(callingId); |
| 2358 | } |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | @Override |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 2363 | public void finishToken(String pkg, ITransientNotification callback) { |
| 2364 | synchronized (mToastQueue) { |
| 2365 | long callingId = Binder.clearCallingIdentity(); |
| 2366 | try { |
| 2367 | int index = indexOfToastLocked(pkg, callback); |
| 2368 | if (index >= 0) { |
| 2369 | ToastRecord record = mToastQueue.get(index); |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 2370 | finishTokenLocked(record.token, record.displayId); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 2371 | } else { |
| 2372 | Slog.w(TAG, "Toast already killed. pkg=" + pkg |
| 2373 | + " callback=" + callback); |
| 2374 | } |
| 2375 | } finally { |
| 2376 | Binder.restoreCallingIdentity(callingId); |
| 2377 | } |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 2382 | public void enqueueNotificationWithTag(String pkg, String opPkg, String tag, int id, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 2383 | Notification notification, int userId) throws RemoteException { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 2384 | enqueueNotificationInternal(pkg, opPkg, Binder.getCallingUid(), |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 2385 | Binder.getCallingPid(), tag, id, notification, userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | @Override |
| 2389 | public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2390 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2391 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2392 | Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2393 | // Don't allow client applications to cancel foreground service notis or autobundled |
| 2394 | // summaries. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 2395 | final int mustNotHaveFlags = isCallingUidSystem() ? 0 : |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 2396 | (FLAG_FOREGROUND_SERVICE | FLAG_AUTOGROUP_SUMMARY); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2397 | cancelNotification(Binder.getCallingUid(), Binder.getCallingPid(), pkg, tag, id, 0, |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 2398 | mustNotHaveFlags, false, userId, REASON_APP_CANCEL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | @Override |
| 2402 | public void cancelAllNotifications(String pkg, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2403 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2404 | |
| 2405 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2406 | Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg); |
| 2407 | |
| 2408 | // Calling from user space, don't allow the canceling of actively |
| 2409 | // running foreground services. |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2410 | cancelAllNotificationsInt(Binder.getCallingUid(), Binder.getCallingPid(), |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 2411 | pkg, null, 0, FLAG_FOREGROUND_SERVICE, true, userId, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 2412 | REASON_APP_CANCEL_ALL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | @Override |
| 2416 | public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) { |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 2417 | enforceSystemOrSystemUI("setNotificationsEnabledForPackage"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2418 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2419 | mPreferencesHelper.setEnabled(pkg, uid, enabled); |
Howard Ro | 8b56f75 | 2018-08-07 15:44:25 -0700 | [diff] [blame] | 2420 | mMetricsLogger.write(new LogMaker(MetricsEvent.ACTION_BAN_APP_NOTES) |
| 2421 | .setType(MetricsEvent.TYPE_ACTION) |
| 2422 | .setPackageName(pkg) |
| 2423 | .setSubtype(enabled ? 1 : 0)); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 2424 | // 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] | 2425 | if (!enabled) { |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 2426 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, null, 0, 0, true, |
| 2427 | UserHandle.getUserId(uid), REASON_PACKAGE_BANNED, null); |
| 2428 | } |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 2429 | |
| 2430 | try { |
| 2431 | getContext().sendBroadcastAsUser( |
| 2432 | new Intent(ACTION_APP_BLOCK_STATE_CHANGED) |
| 2433 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, !enabled) |
| 2434 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 2435 | .setPackage(pkg), |
| 2436 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 2437 | } catch (SecurityException e) { |
| 2438 | Slog.w(TAG, "Can't notify app about app block change", e); |
| 2439 | } |
| 2440 | |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 2441 | handleSavePolicyFile(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | /** |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 2445 | * Updates the enabled state for notifications for the given package (and uid). |
| 2446 | * Additionally, this method marks the app importance as locked by the user, which means |
| 2447 | * that notifications from the app will <b>not</b> be considered for showing a |
| 2448 | * blocking helper. |
| 2449 | * |
| 2450 | * @param pkg package that owns the notifications to update |
| 2451 | * @param uid uid of the app providing notifications |
| 2452 | * @param enabled whether notifications should be enabled for the app |
| 2453 | * |
| 2454 | * @see #setNotificationsEnabledForPackage(String, int, boolean) |
| 2455 | */ |
| 2456 | @Override |
| 2457 | public void setNotificationsEnabledWithImportanceLockForPackage( |
| 2458 | String pkg, int uid, boolean enabled) { |
| 2459 | setNotificationsEnabledForPackage(pkg, uid, enabled); |
| 2460 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2461 | mPreferencesHelper.setAppImportanceLocked(pkg, uid); |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2465 | * Use this when you just want to know if notifications are OK for this package. |
| 2466 | */ |
| 2467 | @Override |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 2468 | public boolean areNotificationsEnabled(String pkg) { |
| 2469 | return areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()); |
| 2470 | } |
| 2471 | |
| 2472 | /** |
| 2473 | * Use this when you just want to know if notifications are OK for this package. |
| 2474 | */ |
| 2475 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2476 | public boolean areNotificationsEnabledForPackage(String pkg, int uid) { |
Evan Laird | 47dc454 | 2019-04-24 15:10:52 -0400 | [diff] [blame] | 2477 | enforceSystemOrSystemUIOrSamePackage(pkg, |
| 2478 | "Caller not system or systemui or same package"); |
Julia Reynolds | 657d164 | 2019-03-27 12:15:57 -0400 | [diff] [blame] | 2479 | if (UserHandle.getCallingUserId() != UserHandle.getUserId(uid)) { |
| 2480 | getContext().enforceCallingPermission( |
| 2481 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 2482 | "canNotifyAsPackage for uid " + uid); |
| 2483 | } |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 2484 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2485 | return mPreferencesHelper.getImportance(pkg, uid) != IMPORTANCE_NONE; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2486 | } |
| 2487 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 2488 | @Override |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 2489 | public boolean areBubblesAllowed(String pkg) { |
| 2490 | return areBubblesAllowedForPackage(pkg, Binder.getCallingUid()); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | @Override |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 2494 | public boolean areBubblesAllowedForPackage(String pkg, int uid) { |
| 2495 | enforceSystemOrSystemUIOrSamePackage(pkg, |
| 2496 | "Caller not system or systemui or same package"); |
Julia Reynolds | 2f7592d | 2019-03-27 12:17:23 -0400 | [diff] [blame] | 2497 | |
| 2498 | if (UserHandle.getCallingUserId() != UserHandle.getUserId(uid)) { |
| 2499 | getContext().enforceCallingPermission( |
| 2500 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 2501 | "canNotifyAsPackage for uid " + uid); |
| 2502 | } |
| 2503 | |
Mady Mellor | 9db685a | 2019-01-23 13:23:37 -0800 | [diff] [blame] | 2504 | return mPreferencesHelper.areBubblesAllowed(pkg, uid); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2505 | } |
| 2506 | |
| 2507 | @Override |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 2508 | public void setBubblesAllowed(String pkg, int uid, boolean allowed) { |
Mady Mellor | 9db685a | 2019-01-23 13:23:37 -0800 | [diff] [blame] | 2509 | enforceSystemOrSystemUI("Caller not system or systemui"); |
Mady Mellor | c39b4ae | 2019-01-09 17:11:37 -0800 | [diff] [blame] | 2510 | mPreferencesHelper.setBubblesAllowed(pkg, uid, allowed); |
Julia Reynolds | 33ab8a0 | 2018-12-17 16:19:52 -0500 | [diff] [blame] | 2511 | handleSavePolicyFile(); |
| 2512 | } |
| 2513 | |
| 2514 | @Override |
Mady Mellor | 9db685a | 2019-01-23 13:23:37 -0800 | [diff] [blame] | 2515 | public boolean hasUserApprovedBubblesForPackage(String pkg, int uid) { |
| 2516 | enforceSystemOrSystemUI("Caller not system or systemui"); |
| 2517 | int lockedFields = mPreferencesHelper.getAppLockedFields(pkg, uid); |
| 2518 | return (lockedFields & PreferencesHelper.LockableAppFields.USER_LOCKED_BUBBLE) != 0; |
| 2519 | } |
| 2520 | |
| 2521 | @Override |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 2522 | public boolean shouldHideSilentStatusIcons(String callingPkg) { |
| 2523 | checkCallerIsSameApp(callingPkg); |
| 2524 | |
| 2525 | if (isCallerSystemOrPhone() |
| 2526 | || mListeners.isListenerPackage(callingPkg)) { |
| 2527 | return mPreferencesHelper.shouldHideSilentStatusIcons(); |
| 2528 | } else { |
| 2529 | throw new SecurityException("Only available for notification listeners"); |
| 2530 | } |
| 2531 | } |
| 2532 | |
| 2533 | @Override |
| 2534 | public void setHideSilentStatusIcons(boolean hide) { |
| 2535 | checkCallerIsSystem(); |
| 2536 | |
| 2537 | mPreferencesHelper.setHideSilentStatusIcons(hide); |
| 2538 | handleSavePolicyFile(); |
| 2539 | |
| 2540 | mListeners.onStatusBarIconsBehaviorChanged(hide); |
| 2541 | } |
| 2542 | |
| 2543 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 2544 | public int getPackageImportance(String pkg) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 2545 | checkCallerIsSystemOrSameApp(pkg); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2546 | return mPreferencesHelper.getImportance(pkg, Binder.getCallingUid()); |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 2547 | } |
| 2548 | |
| 2549 | @Override |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2550 | public boolean canShowBadge(String pkg, int uid) { |
| 2551 | checkCallerIsSystem(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2552 | return mPreferencesHelper.canShowBadge(pkg, uid); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | @Override |
| 2556 | public void setShowBadge(String pkg, int uid, boolean showBadge) { |
| 2557 | checkCallerIsSystem(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2558 | mPreferencesHelper.setShowBadge(pkg, uid, showBadge); |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 2559 | handleSavePolicyFile(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | @Override |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2563 | public void setNotificationDelegate(String callingPkg, String delegate) { |
| 2564 | checkCallerIsSameApp(callingPkg); |
| 2565 | final int callingUid = Binder.getCallingUid(); |
| 2566 | UserHandle user = UserHandle.getUserHandleForUid(callingUid); |
Julia Reynolds | cbc45e7 | 2019-03-07 12:31:52 -0500 | [diff] [blame] | 2567 | if (delegate == null) { |
| 2568 | mPreferencesHelper.revokeNotificationDelegate(callingPkg, Binder.getCallingUid()); |
| 2569 | handleSavePolicyFile(); |
| 2570 | } else { |
| 2571 | try { |
| 2572 | ApplicationInfo info = |
| 2573 | mPackageManager.getApplicationInfo(delegate, |
| 2574 | MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE, |
| 2575 | user.getIdentifier()); |
| 2576 | if (info != null) { |
| 2577 | mPreferencesHelper.setNotificationDelegate( |
| 2578 | callingPkg, callingUid, delegate, info.uid); |
| 2579 | handleSavePolicyFile(); |
| 2580 | } |
| 2581 | } catch (RemoteException e) { |
| 2582 | e.rethrowFromSystemServer(); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2583 | } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | @Override |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2588 | public String getNotificationDelegate(String callingPkg) { |
| 2589 | // callable by Settings also |
| 2590 | checkCallerIsSystemOrSameApp(callingPkg); |
| 2591 | return mPreferencesHelper.getNotificationDelegate(callingPkg, Binder.getCallingUid()); |
| 2592 | } |
| 2593 | |
| 2594 | @Override |
Julia Reynolds | 7a6d07a | 2019-03-18 11:31:56 -0400 | [diff] [blame] | 2595 | public boolean canNotifyAsPackage(String callingPkg, String targetPkg, int userId) { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2596 | checkCallerIsSameApp(callingPkg); |
| 2597 | final int callingUid = Binder.getCallingUid(); |
| 2598 | UserHandle user = UserHandle.getUserHandleForUid(callingUid); |
Julia Reynolds | 7a6d07a | 2019-03-18 11:31:56 -0400 | [diff] [blame] | 2599 | if (user.getIdentifier() != userId) { |
| 2600 | getContext().enforceCallingPermission( |
| 2601 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 2602 | "canNotifyAsPackage for user " + userId); |
| 2603 | } |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 2604 | if (callingPkg.equals(targetPkg)) { |
| 2605 | return true; |
| 2606 | } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2607 | try { |
| 2608 | ApplicationInfo info = |
| 2609 | mPackageManager.getApplicationInfo(targetPkg, |
| 2610 | MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE, |
Julia Reynolds | 7a6d07a | 2019-03-18 11:31:56 -0400 | [diff] [blame] | 2611 | userId); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2612 | if (info != null) { |
| 2613 | return mPreferencesHelper.isDelegateAllowed( |
| 2614 | targetPkg, info.uid, callingPkg, callingUid); |
| 2615 | } |
| 2616 | } catch (RemoteException e) { |
| 2617 | // :( |
| 2618 | } |
| 2619 | return false; |
| 2620 | } |
| 2621 | |
| 2622 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2623 | public void updateNotificationChannelGroupForPackage(String pkg, int uid, |
| 2624 | NotificationChannelGroup group) throws RemoteException { |
| 2625 | enforceSystemOrSystemUI("Caller not system or systemui"); |
| 2626 | createNotificationChannelGroup(pkg, uid, group, false, false); |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 2627 | handleSavePolicyFile(); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2628 | } |
| 2629 | |
| 2630 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2631 | public void createNotificationChannelGroups(String pkg, |
| 2632 | ParceledListSlice channelGroupList) throws RemoteException { |
| 2633 | checkCallerIsSystemOrSameApp(pkg); |
| 2634 | List<NotificationChannelGroup> groups = channelGroupList.getList(); |
| 2635 | final int groupSize = groups.size(); |
| 2636 | for (int i = 0; i < groupSize; i++) { |
| 2637 | final NotificationChannelGroup group = groups.get(i); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2638 | createNotificationChannelGroup(pkg, Binder.getCallingUid(), group, true, false); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2639 | } |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 2640 | handleSavePolicyFile(); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2641 | } |
| 2642 | |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 2643 | private void createNotificationChannelsImpl(String pkg, int uid, |
| 2644 | ParceledListSlice channelsList) { |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 2645 | List<NotificationChannel> channels = channelsList.getList(); |
| 2646 | final int channelsSize = channels.size(); |
| 2647 | for (int i = 0; i < channelsSize; i++) { |
| 2648 | final NotificationChannel channel = channels.get(i); |
| 2649 | Preconditions.checkNotNull(channel, "channel in list is null"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2650 | mPreferencesHelper.createNotificationChannel(pkg, uid, channel, |
Julia Reynolds | 1fe1094 | 2018-03-28 12:46:51 -0400 | [diff] [blame] | 2651 | true /* fromTargetApp */, mConditionProviders.isPackageOrComponentAllowed( |
| 2652 | pkg, UserHandle.getUserId(uid))); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2653 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2654 | UserHandle.getUserHandleForUid(uid), |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2655 | mPreferencesHelper.getNotificationChannel(pkg, uid, channel.getId(), false), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2656 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 2657 | } |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 2658 | handleSavePolicyFile(); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | @Override |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 2662 | public void createNotificationChannels(String pkg, |
| 2663 | ParceledListSlice channelsList) throws RemoteException { |
| 2664 | checkCallerIsSystemOrSameApp(pkg); |
| 2665 | createNotificationChannelsImpl(pkg, Binder.getCallingUid(), channelsList); |
| 2666 | } |
| 2667 | |
| 2668 | @Override |
| 2669 | public void createNotificationChannelsForPackage(String pkg, int uid, |
| 2670 | ParceledListSlice channelsList) throws RemoteException { |
| 2671 | checkCallerIsSystem(); |
| 2672 | createNotificationChannelsImpl(pkg, uid, channelsList); |
| 2673 | } |
| 2674 | |
| 2675 | @Override |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 2676 | public NotificationChannel getNotificationChannel(String callingPkg, int userId, |
| 2677 | String targetPkg, String channelId) { |
| 2678 | if (canNotifyAsPackage(callingPkg, targetPkg, userId) |
| 2679 | || isCallingUidSystem()) { |
| 2680 | int targetUid = -1; |
| 2681 | try { |
| 2682 | targetUid = mPackageManagerClient.getPackageUidAsUser(targetPkg, userId); |
| 2683 | } catch (NameNotFoundException e) { |
| 2684 | /* ignore */ |
| 2685 | } |
| 2686 | return mPreferencesHelper.getNotificationChannel( |
| 2687 | targetPkg, targetUid, channelId, false /* includeDeleted */); |
| 2688 | } |
| 2689 | throw new SecurityException("Pkg " + callingPkg |
| 2690 | + " cannot read channels for " + targetPkg + " in " + userId); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2691 | } |
| 2692 | |
| 2693 | @Override |
| 2694 | public NotificationChannel getNotificationChannelForPackage(String pkg, int uid, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2695 | String channelId, boolean includeDeleted) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2696 | checkCallerIsSystem(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2697 | return mPreferencesHelper.getNotificationChannel(pkg, uid, channelId, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | @Override |
| 2701 | public void deleteNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2702 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2703 | final int callingUid = Binder.getCallingUid(); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2704 | if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 2705 | throw new IllegalArgumentException("Cannot delete default channel"); |
| 2706 | } |
| 2707 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channelId, 0, 0, true, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2708 | UserHandle.getUserId(callingUid), REASON_CHANNEL_BANNED, null); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2709 | mPreferencesHelper.deleteNotificationChannel(pkg, callingUid, channelId); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2710 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2711 | UserHandle.getUserHandleForUid(callingUid), |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2712 | mPreferencesHelper.getNotificationChannel(pkg, callingUid, channelId, true), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2713 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 2714 | handleSavePolicyFile(); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | @Override |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2718 | public NotificationChannelGroup getNotificationChannelGroup(String pkg, String groupId) { |
| 2719 | checkCallerIsSystemOrSameApp(pkg); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2720 | return mPreferencesHelper.getNotificationChannelGroupWithChannels( |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2721 | pkg, Binder.getCallingUid(), groupId, false); |
| 2722 | } |
| 2723 | |
| 2724 | @Override |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2725 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroups( |
| 2726 | String pkg) { |
| 2727 | checkCallerIsSystemOrSameApp(pkg); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2728 | return mPreferencesHelper.getNotificationChannelGroups( |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 2729 | pkg, Binder.getCallingUid(), false, false, true); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2730 | } |
| 2731 | |
| 2732 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2733 | public void deleteNotificationChannelGroup(String pkg, String groupId) { |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2734 | checkCallerIsSystemOrSameApp(pkg); |
| 2735 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2736 | final int callingUid = Binder.getCallingUid(); |
| 2737 | NotificationChannelGroup groupToDelete = |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2738 | mPreferencesHelper.getNotificationChannelGroup(groupId, pkg, callingUid); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2739 | if (groupToDelete != null) { |
| 2740 | List<NotificationChannel> deletedChannels = |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2741 | mPreferencesHelper.deleteNotificationChannelGroup(pkg, callingUid, groupId); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2742 | for (int i = 0; i < deletedChannels.size(); i++) { |
| 2743 | final NotificationChannel deletedChannel = deletedChannels.get(i); |
| 2744 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, deletedChannel.getId(), 0, 0, |
| 2745 | true, |
| 2746 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, |
| 2747 | null); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2748 | mListeners.notifyNotificationChannelChanged(pkg, |
| 2749 | UserHandle.getUserHandleForUid(callingUid), |
| 2750 | deletedChannel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2751 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
| 2752 | } |
| 2753 | mListeners.notifyNotificationChannelGroupChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2754 | pkg, UserHandle.getUserHandleForUid(callingUid), groupToDelete, |
| 2755 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 2756 | handleSavePolicyFile(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2757 | } |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2761 | public void updateNotificationChannelForPackage(String pkg, int uid, |
| 2762 | NotificationChannel channel) { |
Geoffrey Pitsch | 4dd5006 | 2016-12-06 16:41:22 -0500 | [diff] [blame] | 2763 | enforceSystemOrSystemUI("Caller not system or systemui"); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2764 | Preconditions.checkNotNull(channel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2765 | updateNotificationChannelInt(pkg, uid, channel, false); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | @Override |
| 2769 | public ParceledListSlice<NotificationChannel> getNotificationChannelsForPackage(String pkg, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2770 | int uid, boolean includeDeleted) { |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2771 | enforceSystemOrSystemUI("getNotificationChannelsForPackage"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2772 | return mPreferencesHelper.getNotificationChannels(pkg, uid, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2773 | } |
| 2774 | |
| 2775 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2776 | public int getNumNotificationChannelsForPackage(String pkg, int uid, |
| 2777 | boolean includeDeleted) { |
| 2778 | enforceSystemOrSystemUI("getNumNotificationChannelsForPackage"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2779 | return mPreferencesHelper.getNotificationChannels(pkg, uid, includeDeleted) |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2780 | .getList().size(); |
| 2781 | } |
| 2782 | |
| 2783 | @Override |
Julia Reynolds | 17717f5 | 2017-05-09 11:46:06 -0400 | [diff] [blame] | 2784 | public boolean onlyHasDefaultChannel(String pkg, int uid) { |
| 2785 | enforceSystemOrSystemUI("onlyHasDefaultChannel"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2786 | return mPreferencesHelper.onlyHasDefaultChannel(pkg, uid); |
Julia Reynolds | 17717f5 | 2017-05-09 11:46:06 -0400 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | @Override |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 2790 | public int getDeletedChannelCount(String pkg, int uid) { |
| 2791 | enforceSystemOrSystemUI("getDeletedChannelCount"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2792 | return mPreferencesHelper.getDeletedChannelCount(pkg, uid); |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | @Override |
Julia Reynolds | f2e499d | 2018-03-30 10:36:42 -0400 | [diff] [blame] | 2796 | public int getBlockedChannelCount(String pkg, int uid) { |
| 2797 | enforceSystemOrSystemUI("getBlockedChannelCount"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2798 | return mPreferencesHelper.getBlockedChannelCount(pkg, uid); |
Julia Reynolds | f2e499d | 2018-03-30 10:36:42 -0400 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2802 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroupsForPackage( |
| 2803 | String pkg, int uid, boolean includeDeleted) { |
Evan Laird | 47dc454 | 2019-04-24 15:10:52 -0400 | [diff] [blame] | 2804 | enforceSystemOrSystemUI("getNotificationChannelGroupsForPackage"); |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 2805 | return mPreferencesHelper.getNotificationChannelGroups( |
| 2806 | pkg, uid, includeDeleted, true, false); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2807 | } |
| 2808 | |
| 2809 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2810 | public NotificationChannelGroup getPopulatedNotificationChannelGroupForPackage( |
| 2811 | String pkg, int uid, String groupId, boolean includeDeleted) { |
| 2812 | enforceSystemOrSystemUI("getPopulatedNotificationChannelGroupForPackage"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2813 | return mPreferencesHelper.getNotificationChannelGroupWithChannels( |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2814 | pkg, uid, groupId, includeDeleted); |
| 2815 | } |
| 2816 | |
| 2817 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2818 | public NotificationChannelGroup getNotificationChannelGroupForPackage( |
| 2819 | String groupId, String pkg, int uid) { |
| 2820 | enforceSystemOrSystemUI("getNotificationChannelGroupForPackage"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2821 | return mPreferencesHelper.getNotificationChannelGroup(groupId, pkg, uid); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2822 | } |
| 2823 | |
| 2824 | @Override |
Julia Reynolds | b4a9e9c | 2019-03-20 15:46:08 -0400 | [diff] [blame] | 2825 | public ParceledListSlice<NotificationChannel> getNotificationChannels( |
| 2826 | String callingPkg, String targetPkg, int userId) { |
| 2827 | if (canNotifyAsPackage(callingPkg, targetPkg, userId) |
| 2828 | || isCallingUidSystem()) { |
| 2829 | int targetUid = -1; |
| 2830 | try { |
| 2831 | targetUid = mPackageManagerClient.getPackageUidAsUser(targetPkg, userId); |
| 2832 | } catch (NameNotFoundException e) { |
| 2833 | /* ignore */ |
| 2834 | } |
| 2835 | return mPreferencesHelper.getNotificationChannels( |
| 2836 | targetPkg, targetUid, false /* includeDeleted */); |
| 2837 | } |
| 2838 | throw new SecurityException("Pkg " + callingPkg |
| 2839 | + " cannot read channels for " + targetPkg + " in " + userId); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2840 | } |
| 2841 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2842 | @Override |
Julia Reynolds | e273f08 | 2018-04-12 13:48:49 -0400 | [diff] [blame] | 2843 | public int getBlockedAppCount(int userId) { |
| 2844 | checkCallerIsSystem(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2845 | return mPreferencesHelper.getBlockedAppCount(userId); |
Julia Reynolds | e273f08 | 2018-04-12 13:48:49 -0400 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | @Override |
Beverly | 0479cde2 | 2018-11-09 11:05:34 -0500 | [diff] [blame] | 2849 | public int getAppsBypassingDndCount(int userId) { |
| 2850 | checkCallerIsSystem(); |
| 2851 | return mPreferencesHelper.getAppsBypassingDndCount(userId); |
| 2852 | } |
| 2853 | |
| 2854 | @Override |
| 2855 | public ParceledListSlice<NotificationChannel> getNotificationChannelsBypassingDnd( |
| 2856 | String pkg, int userId) { |
| 2857 | checkCallerIsSystem(); |
| 2858 | return mPreferencesHelper.getNotificationChannelsBypassingDnd(pkg, userId); |
| 2859 | } |
| 2860 | |
| 2861 | @Override |
Beverly | 86d076f | 2018-04-17 14:44:52 -0400 | [diff] [blame] | 2862 | public boolean areChannelsBypassingDnd() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2863 | return mPreferencesHelper.areChannelsBypassingDnd(); |
Beverly | 86d076f | 2018-04-17 14:44:52 -0400 | [diff] [blame] | 2864 | } |
| 2865 | |
| 2866 | @Override |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2867 | public void clearData(String packageName, int uid, boolean fromApp) throws RemoteException { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2868 | checkCallerIsSystem(); |
| 2869 | |
| 2870 | // Cancel posted notifications |
| 2871 | cancelAllNotificationsInt(MY_UID, MY_PID, packageName, null, 0, 0, true, |
| 2872 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, null); |
| 2873 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2874 | final String[] packages = new String[] {packageName}; |
| 2875 | final int[] uids = new int[] {uid}; |
| 2876 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2877 | // Listener & assistant |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2878 | mListeners.onPackagesChanged(true, packages, uids); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2879 | mAssistants.onPackagesChanged(true, packages, uids); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2880 | |
| 2881 | // Zen |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2882 | mConditionProviders.onPackagesChanged(true, packages, uids); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2883 | |
Julia Reynolds | 67c1e96 | 2019-01-04 14:01:10 -0500 | [diff] [blame] | 2884 | // Snoozing |
| 2885 | mSnoozeHelper.clearData(UserHandle.getUserId(uid), packageName); |
| 2886 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2887 | // Reset notification preferences |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2888 | if (!fromApp) { |
Julia Reynolds | 7af51c5 | 2019-04-19 11:08:27 -0400 | [diff] [blame] | 2889 | mPreferencesHelper.clearData(packageName, uid); |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2890 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2891 | |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 2892 | handleSavePolicyFile(); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2893 | } |
| 2894 | |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 2895 | @Override |
Julia Reynolds | 088c448 | 2019-04-10 12:43:27 -0400 | [diff] [blame] | 2896 | public List<String> getAllowedAssistantAdjustments(String pkg) { |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 2897 | checkCallerIsSystemOrSameApp(pkg); |
| 2898 | |
| 2899 | if (!isCallerSystemOrPhone() |
| 2900 | && !mAssistants.isPackageAllowed(pkg, UserHandle.getCallingUserId())) { |
| 2901 | throw new SecurityException("Not currently an assistant"); |
| 2902 | } |
| 2903 | |
Julia Reynolds | 088c448 | 2019-04-10 12:43:27 -0400 | [diff] [blame] | 2904 | return mAssistants.getAllowedAssistantAdjustments(); |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | @Override |
Julia Reynolds | 088c448 | 2019-04-10 12:43:27 -0400 | [diff] [blame] | 2908 | public void allowAssistantAdjustment(String adjustmentType) { |
Julia Reynolds | dc6adc6 | 2019-04-08 10:35:40 -0400 | [diff] [blame] | 2909 | checkCallerIsSystemOrSystemUiOrShell(); |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 2910 | mAssistants.allowAdjustmentType(adjustmentType); |
| 2911 | |
| 2912 | handleSavePolicyFile(); |
| 2913 | } |
| 2914 | |
| 2915 | @Override |
Julia Reynolds | 088c448 | 2019-04-10 12:43:27 -0400 | [diff] [blame] | 2916 | public void disallowAssistantAdjustment(String adjustmentType) { |
Julia Reynolds | dc6adc6 | 2019-04-08 10:35:40 -0400 | [diff] [blame] | 2917 | checkCallerIsSystemOrSystemUiOrShell(); |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 2918 | mAssistants.disallowAdjustmentType(adjustmentType); |
| 2919 | |
| 2920 | handleSavePolicyFile(); |
| 2921 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2922 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2923 | /** |
| 2924 | * System-only API for getting a list of current (i.e. not cleared) notifications. |
| 2925 | * |
| 2926 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2927 | * @returns A list of all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2928 | */ |
| 2929 | @Override |
| 2930 | public StatusBarNotification[] getActiveNotifications(String callingPkg) { |
| 2931 | // enforce() will ensure the calling uid has the correct permission |
| 2932 | getContext().enforceCallingOrSelfPermission( |
| 2933 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 2934 | "NotificationManagerService.getActiveNotifications"); |
| 2935 | |
| 2936 | StatusBarNotification[] tmp = null; |
| 2937 | int uid = Binder.getCallingUid(); |
| 2938 | |
| 2939 | // noteOp will check to make sure the callingPkg matches the uid |
| 2940 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 2941 | == AppOpsManager.MODE_ALLOWED) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2942 | synchronized (mNotificationLock) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2943 | tmp = new StatusBarNotification[mNotificationList.size()]; |
| 2944 | final int N = mNotificationList.size(); |
| 2945 | for (int i=0; i<N; i++) { |
| 2946 | tmp[i] = mNotificationList.get(i).sbn; |
| 2947 | } |
| 2948 | } |
| 2949 | } |
| 2950 | return tmp; |
| 2951 | } |
| 2952 | |
| 2953 | /** |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2954 | * Public API for getting a list of current notifications for the calling package/uid. |
| 2955 | * |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 2956 | * Note that since notification posting is done asynchronously, this will not return |
| 2957 | * notifications that are in the process of being posted. |
| 2958 | * |
Julia Reynolds | d12392c | 2018-12-17 14:06:04 -0500 | [diff] [blame] | 2959 | * From {@link Build.VERSION_CODES#Q}, will also return notifications you've posted as |
| 2960 | * an app's notification delegate via |
| 2961 | * {@link NotificationManager#notifyAsPackage(String, String, int, Notification)}. |
| 2962 | * |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2963 | * @returns A list of all the package's notifications, in natural order. |
| 2964 | */ |
| 2965 | @Override |
| 2966 | public ParceledListSlice<StatusBarNotification> getAppActiveNotifications(String pkg, |
| 2967 | int incomingUserId) { |
| 2968 | checkCallerIsSystemOrSameApp(pkg); |
| 2969 | int userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2970 | Binder.getCallingUid(), incomingUserId, true, false, |
| 2971 | "getAppActiveNotifications", pkg); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2972 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2973 | final ArrayMap<String, StatusBarNotification> map |
| 2974 | = new ArrayMap<>(mNotificationList.size() + mEnqueuedNotifications.size()); |
Erik Wolsheimer | 2242b4d | 2015-11-24 13:22:04 -0800 | [diff] [blame] | 2975 | final int N = mNotificationList.size(); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2976 | for (int i = 0; i < N; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2977 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 2978 | mNotificationList.get(i).sbn); |
| 2979 | if (sbn != null) { |
| 2980 | map.put(sbn.getKey(), sbn); |
| 2981 | } |
| 2982 | } |
| 2983 | for(NotificationRecord snoozed: mSnoozeHelper.getSnoozed(userId, pkg)) { |
| 2984 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, snoozed.sbn); |
| 2985 | if (sbn != null) { |
| 2986 | map.put(sbn.getKey(), sbn); |
| 2987 | } |
| 2988 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2989 | final int M = mEnqueuedNotifications.size(); |
| 2990 | for (int i = 0; i < M; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2991 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 2992 | mEnqueuedNotifications.get(i).sbn); |
| 2993 | if (sbn != null) { |
| 2994 | map.put(sbn.getKey(), sbn); // pending update overwrites existing post here |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2995 | } |
| 2996 | } |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2997 | final ArrayList<StatusBarNotification> list = new ArrayList<>(map.size()); |
| 2998 | list.addAll(map.values()); |
| 2999 | return new ParceledListSlice<StatusBarNotification>(list); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 3000 | } |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 3001 | } |
| 3002 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3003 | private StatusBarNotification sanitizeSbn(String pkg, int userId, |
| 3004 | StatusBarNotification sbn) { |
Julia Reynolds | d12392c | 2018-12-17 14:06:04 -0500 | [diff] [blame] | 3005 | if (sbn.getUserId() == userId) { |
| 3006 | if (sbn.getPackageName().equals(pkg) || sbn.getOpPkg().equals(pkg)) { |
| 3007 | // We could pass back a cloneLight() but clients might get confused and |
| 3008 | // try to send this thing back to notify() again, which would not work |
| 3009 | // very well. |
| 3010 | return new StatusBarNotification( |
| 3011 | sbn.getPackageName(), |
| 3012 | sbn.getOpPkg(), |
| 3013 | sbn.getId(), sbn.getTag(), sbn.getUid(), sbn.getInitialPid(), |
| 3014 | sbn.getNotification().clone(), |
| 3015 | sbn.getUser(), sbn.getOverrideGroupKey(), sbn.getPostTime()); |
| 3016 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3017 | } |
| 3018 | return null; |
| 3019 | } |
| 3020 | |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 3021 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3022 | * System-only API for getting a list of recent (cleared, no longer shown) notifications. |
| 3023 | * |
| 3024 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
| 3025 | */ |
| 3026 | @Override |
| 3027 | public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) { |
| 3028 | // enforce() will ensure the calling uid has the correct permission |
| 3029 | getContext().enforceCallingOrSelfPermission( |
| 3030 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 3031 | "NotificationManagerService.getHistoricalNotifications"); |
| 3032 | |
| 3033 | StatusBarNotification[] tmp = null; |
| 3034 | int uid = Binder.getCallingUid(); |
| 3035 | |
| 3036 | // noteOp will check to make sure the callingPkg matches the uid |
| 3037 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 3038 | == AppOpsManager.MODE_ALLOWED) { |
| 3039 | synchronized (mArchive) { |
| 3040 | tmp = mArchive.getArray(count); |
| 3041 | } |
| 3042 | } |
| 3043 | return tmp; |
| 3044 | } |
| 3045 | |
| 3046 | /** |
| 3047 | * Register a listener binder directly with the notification manager. |
| 3048 | * |
| 3049 | * Only works with system callers. Apps should extend |
| 3050 | * {@link android.service.notification.NotificationListenerService}. |
| 3051 | */ |
| 3052 | @Override |
| 3053 | public void registerListener(final INotificationListener listener, |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 3054 | final ComponentName component, final int userid) { |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 3055 | enforceSystemOrSystemUI("INotificationManager.registerListener"); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 3056 | mListeners.registerService(listener, component, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | /** |
| 3060 | * Remove a listener binder directly |
| 3061 | */ |
| 3062 | @Override |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 3063 | public void unregisterListener(INotificationListener token, int userid) { |
Chris Wren | b7c8109 | 2016-03-10 11:41:10 -0500 | [diff] [blame] | 3064 | mListeners.unregisterService(token, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | /** |
| 3068 | * Allow an INotificationListener to simulate a "clear all" operation. |
| 3069 | * |
| 3070 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications} |
| 3071 | * |
| 3072 | * @param token The binder for the listener, to check that the caller is allowed |
| 3073 | */ |
| 3074 | @Override |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 3075 | public void cancelNotificationsFromListener(INotificationListener token, String[] keys) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 3076 | final int callingUid = Binder.getCallingUid(); |
| 3077 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3078 | long identity = Binder.clearCallingIdentity(); |
| 3079 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3080 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3081 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 3082 | |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 3083 | if (keys != null) { |
| 3084 | final int N = keys.length; |
| 3085 | for (int i = 0; i < N; i++) { |
| 3086 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 3087 | if (r == null) continue; |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3088 | final int userId = r.sbn.getUserId(); |
| 3089 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 3090 | !mUserProfiles.isCurrentProfile(userId)) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3091 | throw new SecurityException("Disallowed call from listener: " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3092 | + info.service); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3093 | } |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 3094 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 3095 | r.sbn.getPackageName(), r.sbn.getTag(), r.sbn.getId(), |
| 3096 | userId); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 3097 | } |
| 3098 | } else { |
| 3099 | cancelAllLocked(callingUid, callingPid, info.userid, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3100 | REASON_LISTENER_CANCEL_ALL, info, info.supportsProfiles()); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 3101 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 3102 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3103 | } finally { |
| 3104 | Binder.restoreCallingIdentity(identity); |
| 3105 | } |
| 3106 | } |
| 3107 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 3108 | /** |
| 3109 | * Handle request from an approved listener to re-enable itself. |
| 3110 | * |
| 3111 | * @param component The componenet to be re-enabled, caller must match package. |
| 3112 | */ |
| 3113 | @Override |
| 3114 | public void requestBindListener(ComponentName component) { |
| 3115 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 3116 | long identity = Binder.clearCallingIdentity(); |
| 3117 | try { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3118 | ManagedServices manager = |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3119 | mAssistants.isComponentEnabledForCurrentProfiles(component) |
| 3120 | ? mAssistants |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 3121 | : mListeners; |
| 3122 | manager.setComponentState(component, true); |
| 3123 | } finally { |
| 3124 | Binder.restoreCallingIdentity(identity); |
| 3125 | } |
| 3126 | } |
| 3127 | |
| 3128 | @Override |
| 3129 | public void requestUnbindListener(INotificationListener token) { |
| 3130 | long identity = Binder.clearCallingIdentity(); |
| 3131 | try { |
| 3132 | // allow bound services to disable themselves |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3133 | synchronized (mNotificationLock) { |
| 3134 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 3135 | info.getOwner().setComponentState(info.component, false); |
| 3136 | } |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 3137 | } finally { |
| 3138 | Binder.restoreCallingIdentity(identity); |
| 3139 | } |
| 3140 | } |
| 3141 | |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 3142 | @Override |
| 3143 | public void setNotificationsShownFromListener(INotificationListener token, String[] keys) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 3144 | long identity = Binder.clearCallingIdentity(); |
| 3145 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3146 | synchronized (mNotificationLock) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 3147 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 3148 | if (keys == null) { |
| 3149 | return; |
| 3150 | } |
| 3151 | ArrayList<NotificationRecord> seen = new ArrayList<>(); |
| 3152 | final int n = keys.length; |
| 3153 | for (int i = 0; i < n; i++) { |
| 3154 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
| 3155 | if (r == null) continue; |
| 3156 | final int userId = r.sbn.getUserId(); |
| 3157 | if (userId != info.userid && userId != UserHandle.USER_ALL |
| 3158 | && !mUserProfiles.isCurrentProfile(userId)) { |
| 3159 | throw new SecurityException("Disallowed call from listener: " |
| 3160 | + info.service); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 3161 | } |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 3162 | seen.add(r); |
| 3163 | if (!r.isSeen()) { |
| 3164 | if (DBG) Slog.d(TAG, "Marking notification as seen " + keys[i]); |
| 3165 | reportSeen(r); |
| 3166 | r.setSeen(); |
| 3167 | maybeRecordInterruptionLocked(r); |
| 3168 | } |
| 3169 | } |
| 3170 | if (!seen.isEmpty()) { |
| 3171 | mAssistants.onNotificationsSeenLocked(seen); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 3172 | } |
| 3173 | } |
| 3174 | } finally { |
| 3175 | Binder.restoreCallingIdentity(identity); |
| 3176 | } |
| 3177 | } |
| 3178 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3179 | /** |
| 3180 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 3181 | * |
| 3182 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 3183 | * |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 3184 | * @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] | 3185 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3186 | @GuardedBy("mNotificationLock") |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3187 | private void cancelNotificationFromListenerLocked(ManagedServiceInfo info, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3188 | int callingUid, int callingPid, String pkg, String tag, int id, int userId) { |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 3189 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 3190 | FLAG_ONGOING_EVENT | FLAG_FOREGROUND_SERVICE | FLAG_BUBBLE, |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 3191 | true, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3192 | userId, REASON_LISTENER_CANCEL, info); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 3193 | } |
| 3194 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3195 | /** |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 3196 | * Allow an INotificationListener to snooze a single notification until a context. |
| 3197 | * |
| 3198 | * @param token The binder for the listener, to check that the caller is allowed |
| 3199 | */ |
| 3200 | @Override |
| 3201 | public void snoozeNotificationUntilContextFromListener(INotificationListener token, |
| 3202 | String key, String snoozeCriterionId) { |
| 3203 | long identity = Binder.clearCallingIdentity(); |
| 3204 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3205 | synchronized (mNotificationLock) { |
| 3206 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 3207 | snoozeNotificationInt(key, SNOOZE_UNTIL_UNSPECIFIED, snoozeCriterionId, info); |
| 3208 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 3209 | } finally { |
| 3210 | Binder.restoreCallingIdentity(identity); |
| 3211 | } |
| 3212 | } |
| 3213 | |
| 3214 | /** |
| 3215 | * Allow an INotificationListener to snooze a single notification until a time. |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3216 | * |
| 3217 | * @param token The binder for the listener, to check that the caller is allowed |
| 3218 | */ |
| 3219 | @Override |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 3220 | public void snoozeNotificationUntilFromListener(INotificationListener token, String key, |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 3221 | long duration) { |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3222 | long identity = Binder.clearCallingIdentity(); |
| 3223 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3224 | synchronized (mNotificationLock) { |
| 3225 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 3226 | snoozeNotificationInt(key, duration, null, info); |
| 3227 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3228 | } finally { |
| 3229 | Binder.restoreCallingIdentity(identity); |
| 3230 | } |
| 3231 | } |
| 3232 | |
| 3233 | /** |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 3234 | * Allows the notification assistant to un-snooze a single notification. |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 3235 | * |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 3236 | * @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] | 3237 | */ |
| 3238 | @Override |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 3239 | public void unsnoozeNotificationFromAssistant(INotificationListener token, String key) { |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 3240 | long identity = Binder.clearCallingIdentity(); |
| 3241 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3242 | synchronized (mNotificationLock) { |
| 3243 | final ManagedServiceInfo info = |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3244 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3245 | unsnoozeNotificationInt(key, info); |
| 3246 | } |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 3247 | } finally { |
| 3248 | Binder.restoreCallingIdentity(identity); |
| 3249 | } |
| 3250 | } |
| 3251 | |
| 3252 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3253 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 3254 | * |
| 3255 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 3256 | * |
| 3257 | * @param token The binder for the listener, to check that the caller is allowed |
| 3258 | */ |
| 3259 | @Override |
| 3260 | public void cancelNotificationFromListener(INotificationListener token, String pkg, |
| 3261 | String tag, int id) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 3262 | final int callingUid = Binder.getCallingUid(); |
| 3263 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3264 | long identity = Binder.clearCallingIdentity(); |
| 3265 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3266 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3267 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3268 | if (info.supportsProfiles()) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 3269 | Slog.e(TAG, "Ignoring deprecated cancelNotification(pkg, tag, id) " |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3270 | + "from " + info.component |
| 3271 | + " use cancelNotification(key) instead."); |
| 3272 | } else { |
| 3273 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 3274 | pkg, tag, id, info.userid); |
| 3275 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 3276 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3277 | } finally { |
| 3278 | Binder.restoreCallingIdentity(identity); |
| 3279 | } |
| 3280 | } |
| 3281 | |
| 3282 | /** |
| 3283 | * Allow an INotificationListener to request the list of outstanding notifications seen by |
| 3284 | * the current user. Useful when starting up, after which point the listener callbacks |
| 3285 | * should be used. |
| 3286 | * |
| 3287 | * @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] | 3288 | * @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] | 3289 | * @returns The return value will contain the notifications specified in keys, in that |
| 3290 | * order, or if keys is null, all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3291 | */ |
| 3292 | @Override |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 3293 | public ParceledListSlice<StatusBarNotification> getActiveNotificationsFromListener( |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 3294 | INotificationListener token, String[] keys, int trim) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3295 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3296 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 3297 | final boolean getKeys = keys != null; |
| 3298 | final int N = getKeys ? keys.length : mNotificationList.size(); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 3299 | final ArrayList<StatusBarNotification> list |
| 3300 | = new ArrayList<StatusBarNotification>(N); |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 3301 | for (int i=0; i<N; i++) { |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 3302 | final NotificationRecord r = getKeys |
| 3303 | ? mNotificationsByKey.get(keys[i]) |
| 3304 | : mNotificationList.get(i); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 3305 | if (r == null) continue; |
| 3306 | StatusBarNotification sbn = r.sbn; |
| 3307 | if (!isVisibleToListener(sbn, info)) continue; |
| 3308 | StatusBarNotification sbnToSend = |
| 3309 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 3310 | list.add(sbnToSend); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3311 | } |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 3312 | return new ParceledListSlice<StatusBarNotification>(list); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3313 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3314 | } |
| 3315 | |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 3316 | /** |
| 3317 | * Allow an INotificationListener to request the list of outstanding snoozed notifications |
| 3318 | * seen by the current user. Useful when starting up, after which point the listener |
| 3319 | * callbacks should be used. |
| 3320 | * |
| 3321 | * @param token The binder for the listener, to check that the caller is allowed |
| 3322 | * @returns The return value will contain the notifications specified in keys, in that |
| 3323 | * order, or if keys is null, all the notifications, in natural order. |
| 3324 | */ |
| 3325 | @Override |
| 3326 | public ParceledListSlice<StatusBarNotification> getSnoozedNotificationsFromListener( |
| 3327 | INotificationListener token, int trim) { |
| 3328 | synchronized (mNotificationLock) { |
| 3329 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 3330 | List<NotificationRecord> snoozedRecords = mSnoozeHelper.getSnoozed(); |
| 3331 | final int N = snoozedRecords.size(); |
| 3332 | final ArrayList<StatusBarNotification> list = new ArrayList<>(N); |
| 3333 | for (int i=0; i < N; i++) { |
| 3334 | final NotificationRecord r = snoozedRecords.get(i); |
| 3335 | if (r == null) continue; |
| 3336 | StatusBarNotification sbn = r.sbn; |
| 3337 | if (!isVisibleToListener(sbn, info)) continue; |
| 3338 | StatusBarNotification sbnToSend = |
| 3339 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 3340 | list.add(sbnToSend); |
| 3341 | } |
| 3342 | return new ParceledListSlice<>(list); |
| 3343 | } |
| 3344 | } |
| 3345 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3346 | @Override |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 3347 | public void clearRequestedListenerHints(INotificationListener token) { |
| 3348 | final long identity = Binder.clearCallingIdentity(); |
| 3349 | try { |
| 3350 | synchronized (mNotificationLock) { |
| 3351 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 3352 | removeDisabledHints(info); |
| 3353 | updateListenerHintsLocked(); |
| 3354 | updateEffectsSuppressorLocked(); |
| 3355 | } |
| 3356 | } finally { |
| 3357 | Binder.restoreCallingIdentity(identity); |
| 3358 | } |
| 3359 | } |
| 3360 | |
| 3361 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3362 | public void requestHintsFromListener(INotificationListener token, int hints) { |
| 3363 | final long identity = Binder.clearCallingIdentity(); |
| 3364 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3365 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3366 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 3367 | final int disableEffectsMask = HINT_HOST_DISABLE_EFFECTS |
| 3368 | | HINT_HOST_DISABLE_NOTIFICATION_EFFECTS |
| 3369 | | HINT_HOST_DISABLE_CALL_EFFECTS; |
| 3370 | final boolean disableEffects = (hints & disableEffectsMask) != 0; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3371 | if (disableEffects) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 3372 | addDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3373 | } else { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 3374 | removeDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3375 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3376 | updateListenerHintsLocked(); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 3377 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3378 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3379 | } finally { |
| 3380 | Binder.restoreCallingIdentity(identity); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3381 | } |
| 3382 | } |
| 3383 | |
| 3384 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3385 | public int getHintsFromListener(INotificationListener token) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3386 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3387 | return mListenerHints; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3388 | } |
| 3389 | } |
| 3390 | |
| 3391 | @Override |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 3392 | public void requestInterruptionFilterFromListener(INotificationListener token, |
| 3393 | int interruptionFilter) throws RemoteException { |
| 3394 | final long identity = Binder.clearCallingIdentity(); |
| 3395 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3396 | synchronized (mNotificationLock) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3397 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 3398 | mZenModeHelper.requestFromListener(info.component, interruptionFilter); |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 3399 | updateInterruptionFilterLocked(); |
| 3400 | } |
| 3401 | } finally { |
| 3402 | Binder.restoreCallingIdentity(identity); |
| 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | @Override |
| 3407 | public int getInterruptionFilterFromListener(INotificationListener token) |
| 3408 | throws RemoteException { |
| 3409 | synchronized (mNotificationLight) { |
| 3410 | return mInterruptionFilter; |
| 3411 | } |
| 3412 | } |
| 3413 | |
| 3414 | @Override |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 3415 | public void setOnNotificationPostedTrimFromListener(INotificationListener token, int trim) |
| 3416 | throws RemoteException { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3417 | synchronized (mNotificationLock) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 3418 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 3419 | if (info == null) return; |
| 3420 | mListeners.setOnNotificationPostedTrimLocked(info, trim); |
| 3421 | } |
| 3422 | } |
| 3423 | |
| 3424 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 3425 | public int getZenMode() { |
| 3426 | return mZenModeHelper.getZenMode(); |
| 3427 | } |
| 3428 | |
| 3429 | @Override |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 3430 | public ZenModeConfig getZenModeConfig() { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 3431 | enforceSystemOrSystemUI("INotificationManager.getZenModeConfig"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 3432 | return mZenModeHelper.getConfig(); |
| 3433 | } |
| 3434 | |
| 3435 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 3436 | public void setZenMode(int mode, Uri conditionId, String reason) throws RemoteException { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 3437 | enforceSystemOrSystemUI("INotificationManager.setZenMode"); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 3438 | final long identity = Binder.clearCallingIdentity(); |
| 3439 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 3440 | mZenModeHelper.setManualZenMode(mode, conditionId, null, reason); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 3441 | } finally { |
| 3442 | Binder.restoreCallingIdentity(identity); |
| 3443 | } |
| 3444 | } |
| 3445 | |
| 3446 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 3447 | public List<ZenModeConfig.ZenRule> getZenRules() throws RemoteException { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3448 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRules"); |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 3449 | return mZenModeHelper.getZenRules(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3453 | public AutomaticZenRule getAutomaticZenRule(String id) throws RemoteException { |
| 3454 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3455 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRule"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3456 | return mZenModeHelper.getAutomaticZenRule(id); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3457 | } |
| 3458 | |
| 3459 | @Override |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 3460 | public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3461 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 3462 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 3463 | if (automaticZenRule.getOwner() == null |
| 3464 | && automaticZenRule.getConfigurationActivity() == null) { |
| 3465 | throw new NullPointerException( |
| 3466 | "Rule must have a conditionproviderservice and/or configuration activity"); |
| 3467 | } |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3468 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
Julia Reynolds | a94365d | 2019-04-09 10:48:43 -0400 | [diff] [blame] | 3469 | if (automaticZenRule.getZenPolicy() != null |
| 3470 | && automaticZenRule.getInterruptionFilter() != INTERRUPTION_FILTER_PRIORITY) { |
| 3471 | throw new IllegalArgumentException("ZenPolicy is only applicable to " |
| 3472 | + "INTERRUPTION_FILTER_PRIORITY filters"); |
| 3473 | } |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3474 | enforcePolicyAccess(Binder.getCallingUid(), "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3475 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3476 | return mZenModeHelper.addAutomaticZenRule(automaticZenRule, |
| 3477 | "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3478 | } |
| 3479 | |
| 3480 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 3481 | public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3482 | throws RemoteException { |
| 3483 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 3484 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 3485 | if (automaticZenRule.getOwner() == null |
| 3486 | && automaticZenRule.getConfigurationActivity() == null) { |
| 3487 | throw new NullPointerException( |
| 3488 | "Rule must have a conditionproviderservice and/or configuration activity"); |
| 3489 | } |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3490 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
| 3491 | enforcePolicyAccess(Binder.getCallingUid(), "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3492 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 3493 | return mZenModeHelper.updateAutomaticZenRule(id, automaticZenRule, |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3494 | "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3498 | public boolean removeAutomaticZenRule(String id) throws RemoteException { |
| 3499 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3500 | // Verify that they can modify zen rules. |
| 3501 | enforcePolicyAccess(Binder.getCallingUid(), "removeAutomaticZenRule"); |
| 3502 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3503 | return mZenModeHelper.removeAutomaticZenRule(id, "removeAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3504 | } |
| 3505 | |
| 3506 | @Override |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 3507 | public boolean removeAutomaticZenRules(String packageName) throws RemoteException { |
| 3508 | Preconditions.checkNotNull(packageName, "Package name is null"); |
| 3509 | enforceSystemOrSystemUI("removeAutomaticZenRules"); |
| 3510 | |
| 3511 | return mZenModeHelper.removeAutomaticZenRules(packageName, "removeAutomaticZenRules"); |
| 3512 | } |
| 3513 | |
| 3514 | @Override |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 3515 | public int getRuleInstanceCount(ComponentName owner) throws RemoteException { |
| 3516 | Preconditions.checkNotNull(owner, "Owner is null"); |
| 3517 | enforceSystemOrSystemUI("getRuleInstanceCount"); |
| 3518 | |
| 3519 | return mZenModeHelper.getCurrentInstanceCount(owner); |
| 3520 | } |
| 3521 | |
| 3522 | @Override |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 3523 | public void setAutomaticZenRuleState(String id, Condition condition) { |
| 3524 | Preconditions.checkNotNull(id, "id is null"); |
| 3525 | Preconditions.checkNotNull(condition, "Condition is null"); |
| 3526 | |
| 3527 | enforcePolicyAccess(Binder.getCallingUid(), "setAutomaticZenRuleState"); |
| 3528 | |
| 3529 | mZenModeHelper.setAutomaticZenRuleState(id, condition); |
| 3530 | } |
| 3531 | |
| 3532 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3533 | public void setInterruptionFilter(String pkg, int filter) throws RemoteException { |
| 3534 | enforcePolicyAccess(pkg, "setInterruptionFilter"); |
| 3535 | final int zen = NotificationManager.zenModeFromInterruptionFilter(filter, -1); |
| 3536 | if (zen == -1) throw new IllegalArgumentException("Invalid filter: " + filter); |
| 3537 | final long identity = Binder.clearCallingIdentity(); |
| 3538 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 3539 | mZenModeHelper.setManualZenMode(zen, null, pkg, "setInterruptionFilter"); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3540 | } finally { |
| 3541 | Binder.restoreCallingIdentity(identity); |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | @Override |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 3546 | public void notifyConditions(final String pkg, IConditionProvider provider, |
| 3547 | final Condition[] conditions) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3548 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 3549 | checkCallerIsSystemOrSameApp(pkg); |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 3550 | mHandler.post(new Runnable() { |
| 3551 | @Override |
| 3552 | public void run() { |
| 3553 | mConditionProviders.notifyConditions(pkg, info, conditions); |
| 3554 | } |
| 3555 | }); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3556 | } |
| 3557 | |
Julia Reynolds | 38e6ca4 | 2016-08-08 08:38:09 -0400 | [diff] [blame] | 3558 | @Override |
| 3559 | public void requestUnbindProvider(IConditionProvider provider) { |
| 3560 | long identity = Binder.clearCallingIdentity(); |
| 3561 | try { |
| 3562 | // allow bound services to disable themselves |
| 3563 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 3564 | info.getOwner().setComponentState(info.component, false); |
| 3565 | } finally { |
| 3566 | Binder.restoreCallingIdentity(identity); |
| 3567 | } |
| 3568 | } |
| 3569 | |
| 3570 | @Override |
| 3571 | public void requestBindProvider(ComponentName component) { |
| 3572 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 3573 | long identity = Binder.clearCallingIdentity(); |
| 3574 | try { |
| 3575 | mConditionProviders.setComponentState(component, true); |
| 3576 | } finally { |
| 3577 | Binder.restoreCallingIdentity(identity); |
| 3578 | } |
| 3579 | } |
| 3580 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3581 | private void enforceSystemOrSystemUI(String message) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3582 | if (isCallerSystemOrPhone()) return; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3583 | getContext().enforceCallingPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 3584 | message); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3585 | } |
| 3586 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 3587 | private void enforceSystemOrSystemUIOrSamePackage(String pkg, String message) { |
| 3588 | try { |
| 3589 | checkCallerIsSystemOrSameApp(pkg); |
| 3590 | } catch (SecurityException e) { |
| 3591 | getContext().enforceCallingPermission( |
| 3592 | android.Manifest.permission.STATUS_BAR_SERVICE, |
| 3593 | message); |
| 3594 | } |
| 3595 | } |
| 3596 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3597 | private void enforcePolicyAccess(int uid, String method) { |
| 3598 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 3599 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 3600 | return; |
| 3601 | } |
| 3602 | boolean accessAllowed = false; |
Julia Reynolds | 4214da9 | 2019-04-10 15:04:06 -0400 | [diff] [blame] | 3603 | String[] packages = mPackageManagerClient.getPackagesForUid(uid); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3604 | final int packageCount = packages.length; |
| 3605 | for (int i = 0; i < packageCount; i++) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3606 | if (mConditionProviders.isPackageOrComponentAllowed( |
| 3607 | packages[i], UserHandle.getUserId(uid))) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3608 | accessAllowed = true; |
| 3609 | } |
| 3610 | } |
| 3611 | if (!accessAllowed) { |
| 3612 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 3613 | throw new SecurityException("Notification policy access denied"); |
| 3614 | } |
| 3615 | } |
| 3616 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3617 | private void enforcePolicyAccess(String pkg, String method) { |
Julia Reynolds | 6ee2617 | 2015-09-28 11:34:48 -0400 | [diff] [blame] | 3618 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 3619 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 3620 | return; |
| 3621 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 3622 | checkCallerIsSameApp(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3623 | if (!checkPolicyAccess(pkg)) { |
| 3624 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 3625 | throw new SecurityException("Notification policy access denied"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3626 | } |
| 3627 | } |
| 3628 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3629 | private boolean checkPackagePolicyAccess(String pkg) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3630 | return mConditionProviders.isPackageOrComponentAllowed( |
| 3631 | pkg, getCallingUserHandle().getIdentifier()); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | private boolean checkPolicyAccess(String pkg) { |
Julia Reynolds | 0867b3a | 2016-03-30 17:29:54 -0400 | [diff] [blame] | 3635 | try { |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 3636 | int uid = getContext().getPackageManager().getPackageUidAsUser(pkg, |
| 3637 | UserHandle.getCallingUserId()); |
Julia Reynolds | 0867b3a | 2016-03-30 17:29:54 -0400 | [diff] [blame] | 3638 | if (PackageManager.PERMISSION_GRANTED == ActivityManager.checkComponentPermission( |
| 3639 | android.Manifest.permission.MANAGE_NOTIFICATIONS, uid, |
| 3640 | -1, true)) { |
| 3641 | return true; |
| 3642 | } |
| 3643 | } catch (NameNotFoundException e) { |
| 3644 | return false; |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 3645 | } |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 3646 | return checkPackagePolicyAccess(pkg) |
| 3647 | || mListeners.isComponentEnabledForPackage(pkg) |
| 3648 | || (mDpm != null && |
| 3649 | mDpm.isActiveAdminWithPolicy(Binder.getCallingUid(), |
| 3650 | DeviceAdminInfo.USES_POLICY_PROFILE_OWNER)); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3651 | } |
| 3652 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3653 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3654 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 3655 | if (!DumpUtils.checkDumpAndUsageStatsPermission(getContext(), TAG, pw)) return; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3656 | final DumpFilter filter = DumpFilter.parseFromArguments(args); |
Makoto Onuki | bbb4b22 | 2018-06-25 16:01:02 -0700 | [diff] [blame] | 3657 | final long token = Binder.clearCallingIdentity(); |
| 3658 | try { |
| 3659 | if (filter.stats) { |
| 3660 | dumpJson(pw, filter); |
| 3661 | } else if (filter.proto) { |
| 3662 | dumpProto(fd, filter); |
| 3663 | } else if (filter.criticalPriority) { |
| 3664 | dumpNotificationRecords(pw, filter); |
| 3665 | } else { |
| 3666 | dumpImpl(pw, filter); |
| 3667 | } |
| 3668 | } finally { |
| 3669 | Binder.restoreCallingIdentity(token); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3670 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3671 | } |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 3672 | |
| 3673 | @Override |
| 3674 | public ComponentName getEffectsSuppressor() { |
Bryce Lee | ba3d895 | 2016-04-12 12:39:15 -0700 | [diff] [blame] | 3675 | return !mEffectsSuppressors.isEmpty() ? mEffectsSuppressors.get(0) : null; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 3676 | } |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 3677 | |
| 3678 | @Override |
| 3679 | public boolean matchesCallFilter(Bundle extras) { |
| 3680 | enforceSystemOrSystemUI("INotificationManager.matchesCallFilter"); |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 3681 | return mZenModeHelper.matchesCallFilter( |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3682 | Binder.getCallingUserHandle(), |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 3683 | extras, |
| 3684 | mRankingHelper.findExtractor(ValidateNotificationPeople.class), |
| 3685 | MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS, |
| 3686 | MATCHES_CALL_FILTER_TIMEOUT_AFFINITY); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 3687 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 3688 | |
| 3689 | @Override |
| 3690 | public boolean isSystemConditionProviderEnabled(String path) { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 3691 | enforceSystemOrSystemUI("INotificationManager.isSystemConditionProviderEnabled"); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 3692 | return mConditionProviders.isSystemProviderEnabled(path); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 3693 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3694 | |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 3695 | // Backup/restore interface |
| 3696 | @Override |
| 3697 | public byte[] getBackupPayload(int user) { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3698 | checkCallerIsSystem(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3699 | if (DBG) Slog.d(TAG, "getBackupPayload u=" + user); |
Julia Reynolds | 8a613f8 | 2018-12-13 14:25:13 -0500 | [diff] [blame] | 3700 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 3701 | try { |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 3702 | writePolicyXml(baos, true /*forBackup*/, user); |
Julia Reynolds | 8a613f8 | 2018-12-13 14:25:13 -0500 | [diff] [blame] | 3703 | return baos.toByteArray(); |
| 3704 | } catch (IOException e) { |
| 3705 | Slog.w(TAG, "getBackupPayload: error writing payload for user " + user, e); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3706 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 3707 | return null; |
| 3708 | } |
| 3709 | |
| 3710 | @Override |
| 3711 | public void applyRestore(byte[] payload, int user) { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3712 | checkCallerIsSystem(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3713 | if (DBG) Slog.d(TAG, "applyRestore u=" + user + " payload=" |
| 3714 | + (payload != null ? new String(payload, StandardCharsets.UTF_8) : null)); |
| 3715 | if (payload == null) { |
| 3716 | Slog.w(TAG, "applyRestore: no payload to restore for user " + user); |
| 3717 | return; |
| 3718 | } |
Julia Reynolds | 8a613f8 | 2018-12-13 14:25:13 -0500 | [diff] [blame] | 3719 | final ByteArrayInputStream bais = new ByteArrayInputStream(payload); |
| 3720 | try { |
Annie Meng | 8b646fd | 2019-02-01 18:46:42 +0000 | [diff] [blame] | 3721 | readPolicyXml(bais, true /*forRestore*/, user); |
Julia Reynolds | 8a613f8 | 2018-12-13 14:25:13 -0500 | [diff] [blame] | 3722 | handleSavePolicyFile(); |
| 3723 | } catch (NumberFormatException | XmlPullParserException | IOException e) { |
| 3724 | Slog.w(TAG, "applyRestore: error reading payload", e); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3725 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 3726 | } |
| 3727 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3728 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3729 | public boolean isNotificationPolicyAccessGranted(String pkg) { |
| 3730 | return checkPolicyAccess(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3731 | } |
| 3732 | |
| 3733 | @Override |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 3734 | public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {; |
| 3735 | enforceSystemOrSystemUIOrSamePackage(pkg, |
| 3736 | "request policy access status for another package"); |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 3737 | return checkPolicyAccess(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3738 | } |
| 3739 | |
| 3740 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3741 | public void setNotificationPolicyAccessGranted(String pkg, boolean granted) |
| 3742 | throws RemoteException { |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3743 | setNotificationPolicyAccessGrantedForUser( |
| 3744 | pkg, getCallingUserHandle().getIdentifier(), granted); |
| 3745 | } |
| 3746 | |
| 3747 | @Override |
| 3748 | public void setNotificationPolicyAccessGrantedForUser( |
| 3749 | String pkg, int userId, boolean granted) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3750 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3751 | final long identity = Binder.clearCallingIdentity(); |
| 3752 | try { |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 3753 | if (mAllowedManagedServicePackages.test( |
| 3754 | pkg, userId, mConditionProviders.getRequiredPermission())) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3755 | mConditionProviders.setPackageOrComponentEnabled( |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3756 | pkg, userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3757 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3758 | getContext().sendBroadcastAsUser(new Intent( |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 3759 | ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3760 | .setPackage(pkg) |
Julia Reynolds | 99f7207 | 2019-01-14 09:35:22 -0500 | [diff] [blame] | 3761 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3762 | UserHandle.of(userId), null); |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 3763 | handleSavePolicyFile(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3764 | } |
| 3765 | } finally { |
| 3766 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3767 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3768 | } |
| 3769 | |
| 3770 | @Override |
| 3771 | public Policy getNotificationPolicy(String pkg) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3772 | final long identity = Binder.clearCallingIdentity(); |
| 3773 | try { |
| 3774 | return mZenModeHelper.getNotificationPolicy(); |
| 3775 | } finally { |
| 3776 | Binder.restoreCallingIdentity(identity); |
| 3777 | } |
| 3778 | } |
| 3779 | |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 3780 | @Override |
| 3781 | public Policy getConsolidatedNotificationPolicy() { |
| 3782 | final long identity = Binder.clearCallingIdentity(); |
| 3783 | try { |
| 3784 | return mZenModeHelper.getConsolidatedNotificationPolicy(); |
| 3785 | } finally { |
| 3786 | Binder.restoreCallingIdentity(identity); |
| 3787 | } |
| 3788 | } |
| 3789 | |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3790 | /** |
| 3791 | * Sets the notification policy. Apps that target API levels below |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3792 | * {@link android.os.Build.VERSION_CODES#P} cannot change user-designated values to |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3793 | * allow or disallow {@link Policy#PRIORITY_CATEGORY_ALARMS}, |
| 3794 | * {@link Policy#PRIORITY_CATEGORY_SYSTEM} and |
| 3795 | * {@link Policy#PRIORITY_CATEGORY_MEDIA} from bypassing dnd |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3796 | */ |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3797 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3798 | public void setNotificationPolicy(String pkg, Policy policy) { |
| 3799 | enforcePolicyAccess(pkg, "setNotificationPolicy"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3800 | final long identity = Binder.clearCallingIdentity(); |
| 3801 | try { |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3802 | final ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(pkg, |
| 3803 | 0, UserHandle.getUserId(MY_UID)); |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3804 | Policy currPolicy = mZenModeHelper.getNotificationPolicy(); |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3805 | |
Jeff Sharkey | aa1a911 | 2018-04-10 15:18:12 -0600 | [diff] [blame] | 3806 | if (applicationInfo.targetSdkVersion < Build.VERSION_CODES.P) { |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3807 | int priorityCategories = policy.priorityCategories; |
| 3808 | // ignore alarm and media values from new policy |
| 3809 | priorityCategories &= ~Policy.PRIORITY_CATEGORY_ALARMS; |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3810 | priorityCategories &= ~Policy.PRIORITY_CATEGORY_MEDIA; |
| 3811 | priorityCategories &= ~Policy.PRIORITY_CATEGORY_SYSTEM; |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3812 | // use user-designated values |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3813 | priorityCategories |= currPolicy.priorityCategories |
| 3814 | & Policy.PRIORITY_CATEGORY_ALARMS; |
| 3815 | priorityCategories |= currPolicy.priorityCategories |
| 3816 | & Policy.PRIORITY_CATEGORY_MEDIA; |
| 3817 | priorityCategories |= currPolicy.priorityCategories |
| 3818 | & Policy.PRIORITY_CATEGORY_SYSTEM; |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3819 | |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3820 | policy = new Policy(priorityCategories, |
| 3821 | policy.priorityCallSenders, policy.priorityMessageSenders, |
| 3822 | policy.suppressedVisualEffects); |
| 3823 | } |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3824 | int newVisualEffects = calculateSuppressedVisualEffects( |
| 3825 | policy, currPolicy, applicationInfo.targetSdkVersion); |
| 3826 | policy = new Policy(policy.priorityCategories, |
| 3827 | policy.priorityCallSenders, policy.priorityMessageSenders, |
| 3828 | newVisualEffects); |
Beverly | 5e07322 | 2018-03-08 10:36:25 -0500 | [diff] [blame] | 3829 | ZenLog.traceSetNotificationPolicy(pkg, applicationInfo.targetSdkVersion, policy); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3830 | mZenModeHelper.setNotificationPolicy(policy); |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3831 | } catch (RemoteException e) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3832 | } finally { |
| 3833 | Binder.restoreCallingIdentity(identity); |
| 3834 | } |
| 3835 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3836 | |
| 3837 | @Override |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3838 | public List<String> getEnabledNotificationListenerPackages() { |
| 3839 | checkCallerIsSystem(); |
| 3840 | return mListeners.getAllowedPackages(getCallingUserHandle().getIdentifier()); |
| 3841 | } |
| 3842 | |
| 3843 | @Override |
| 3844 | public List<ComponentName> getEnabledNotificationListeners(int userId) { |
| 3845 | checkCallerIsSystem(); |
| 3846 | return mListeners.getAllowedComponents(userId); |
| 3847 | } |
| 3848 | |
| 3849 | @Override |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 3850 | public ComponentName getAllowedNotificationAssistantForUser(int userId) { |
Julia Reynolds | dc6adc6 | 2019-04-08 10:35:40 -0400 | [diff] [blame] | 3851 | checkCallerIsSystemOrSystemUiOrShell(); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 3852 | List<ComponentName> allowedComponents = mAssistants.getAllowedComponents(userId); |
| 3853 | if (allowedComponents.size() > 1) { |
| 3854 | throw new IllegalStateException( |
| 3855 | "At most one NotificationAssistant: " + allowedComponents.size()); |
| 3856 | } |
| 3857 | return CollectionUtils.firstOrNull(allowedComponents); |
| 3858 | } |
| 3859 | |
| 3860 | @Override |
| 3861 | public ComponentName getAllowedNotificationAssistant() { |
| 3862 | return getAllowedNotificationAssistantForUser(getCallingUserHandle().getIdentifier()); |
| 3863 | } |
| 3864 | |
| 3865 | @Override |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3866 | public boolean isNotificationListenerAccessGranted(ComponentName listener) { |
| 3867 | Preconditions.checkNotNull(listener); |
| 3868 | checkCallerIsSystemOrSameApp(listener.getPackageName()); |
| 3869 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 3870 | getCallingUserHandle().getIdentifier()); |
| 3871 | } |
| 3872 | |
| 3873 | @Override |
| 3874 | public boolean isNotificationListenerAccessGrantedForUser(ComponentName listener, |
| 3875 | int userId) { |
| 3876 | Preconditions.checkNotNull(listener); |
| 3877 | checkCallerIsSystem(); |
| 3878 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 3879 | userId); |
| 3880 | } |
| 3881 | |
| 3882 | @Override |
| 3883 | public boolean isNotificationAssistantAccessGranted(ComponentName assistant) { |
| 3884 | Preconditions.checkNotNull(assistant); |
| 3885 | checkCallerIsSystemOrSameApp(assistant.getPackageName()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3886 | return mAssistants.isPackageOrComponentAllowed(assistant.flattenToString(), |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3887 | getCallingUserHandle().getIdentifier()); |
| 3888 | } |
| 3889 | |
| 3890 | @Override |
| 3891 | public void setNotificationListenerAccessGranted(ComponentName listener, |
| 3892 | boolean granted) throws RemoteException { |
| 3893 | setNotificationListenerAccessGrantedForUser( |
| 3894 | listener, getCallingUserHandle().getIdentifier(), granted); |
| 3895 | } |
| 3896 | |
| 3897 | @Override |
| 3898 | public void setNotificationAssistantAccessGranted(ComponentName assistant, |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 3899 | boolean granted) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3900 | setNotificationAssistantAccessGrantedForUser( |
| 3901 | assistant, getCallingUserHandle().getIdentifier(), granted); |
| 3902 | } |
| 3903 | |
| 3904 | @Override |
| 3905 | public void setNotificationListenerAccessGrantedForUser(ComponentName listener, int userId, |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 3906 | boolean granted) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3907 | Preconditions.checkNotNull(listener); |
Julia Reynolds | 0d21764 | 2017-08-11 11:26:04 -0400 | [diff] [blame] | 3908 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3909 | final long identity = Binder.clearCallingIdentity(); |
| 3910 | try { |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 3911 | if (mAllowedManagedServicePackages.test( |
| 3912 | listener.getPackageName(), userId, mListeners.getRequiredPermission())) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3913 | mConditionProviders.setPackageOrComponentEnabled(listener.flattenToString(), |
| 3914 | userId, false, granted); |
| 3915 | mListeners.setPackageOrComponentEnabled(listener.flattenToString(), |
| 3916 | userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3917 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3918 | getContext().sendBroadcastAsUser(new Intent( |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 3919 | ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3920 | .setPackage(listener.getPackageName()) |
| 3921 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3922 | UserHandle.of(userId), null); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3923 | |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 3924 | handleSavePolicyFile(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3925 | } |
| 3926 | } finally { |
| 3927 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3928 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | @Override |
| 3932 | public void setNotificationAssistantAccessGrantedForUser(ComponentName assistant, |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 3933 | int userId, boolean granted) { |
Julia Reynolds | dc6adc6 | 2019-04-08 10:35:40 -0400 | [diff] [blame] | 3934 | checkCallerIsSystemOrSystemUiOrShell(); |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 3935 | for (UserInfo ui : mUm.getEnabledProfiles(userId)) { |
| 3936 | mAssistants.setUserSet(ui.id, true); |
| 3937 | } |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3938 | final long identity = Binder.clearCallingIdentity(); |
| 3939 | try { |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 3940 | setNotificationAssistantAccessGrantedForUserInternal(assistant, userId, granted); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3941 | } finally { |
| 3942 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3943 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3944 | } |
| 3945 | |
| 3946 | @Override |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3947 | public void applyEnqueuedAdjustmentFromAssistant(INotificationListener token, |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3948 | Adjustment adjustment) { |
| 3949 | boolean foundEnqueued = false; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3950 | final long identity = Binder.clearCallingIdentity(); |
| 3951 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3952 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3953 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3954 | int N = mEnqueuedNotifications.size(); |
| 3955 | for (int i = 0; i < N; i++) { |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3956 | final NotificationRecord r = mEnqueuedNotifications.get(i); |
| 3957 | if (Objects.equals(adjustment.getKey(), r.getKey()) |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3958 | && Objects.equals(adjustment.getUser(), r.getUserId()) |
| 3959 | && mAssistants.isSameUser(token, r.getUserId())) { |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3960 | applyAdjustment(r, adjustment); |
| 3961 | r.applyAdjustments(); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 3962 | // importance is checked at the beginning of the |
| 3963 | // PostNotificationRunnable, before the signal extractors are run, so |
| 3964 | // calculate the final importance here |
| 3965 | r.calculateImportance(); |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3966 | foundEnqueued = true; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3967 | break; |
| 3968 | } |
| 3969 | } |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3970 | if (!foundEnqueued) { |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3971 | applyAdjustmentFromAssistant(token, adjustment); |
| 3972 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3973 | } |
| 3974 | } finally { |
| 3975 | Binder.restoreCallingIdentity(identity); |
| 3976 | } |
| 3977 | } |
| 3978 | |
| 3979 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 3980 | public void applyAdjustmentFromAssistant(INotificationListener token, |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3981 | Adjustment adjustment) { |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3982 | List<Adjustment> adjustments = new ArrayList<>(); |
| 3983 | adjustments.add(adjustment); |
| 3984 | applyAdjustmentsFromAssistant(token, adjustments); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3985 | } |
| 3986 | |
| 3987 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 3988 | public void applyAdjustmentsFromAssistant(INotificationListener token, |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3989 | List<Adjustment> adjustments) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3990 | |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 3991 | boolean needsSort = false; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3992 | final long identity = Binder.clearCallingIdentity(); |
| 3993 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3994 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3995 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3996 | for (Adjustment adjustment : adjustments) { |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3997 | NotificationRecord r = mNotificationsByKey.get(adjustment.getKey()); |
| 3998 | if (r != null && mAssistants.isSameUser(token, r.getUserId())) { |
| 3999 | applyAdjustment(r, adjustment); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 4000 | // If the assistant has blocked the notification, cancel it |
| 4001 | // This will trigger a sort, so we don't have to explicitly ask for |
| 4002 | // one here. |
| 4003 | if (adjustment.getSignals().containsKey(Adjustment.KEY_IMPORTANCE) |
| 4004 | && adjustment.getSignals().getInt(Adjustment.KEY_IMPORTANCE) |
| 4005 | == IMPORTANCE_NONE) { |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 4006 | cancelNotificationsFromListener(token, new String[]{r.getKey()}); |
| 4007 | } else { |
| 4008 | needsSort = true; |
| 4009 | } |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 4010 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4011 | } |
| 4012 | } |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 4013 | if (needsSort) { |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 4014 | mRankingHandler.requestSort(); |
| 4015 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4016 | } finally { |
| 4017 | Binder.restoreCallingIdentity(identity); |
| 4018 | } |
| 4019 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4020 | |
| 4021 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 4022 | public void updateNotificationChannelGroupFromPrivilegedListener( |
| 4023 | INotificationListener token, String pkg, UserHandle user, |
| 4024 | NotificationChannelGroup group) throws RemoteException { |
| 4025 | Preconditions.checkNotNull(user); |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 4026 | verifyPrivilegedListener(token, user, false); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 4027 | createNotificationChannelGroup( |
| 4028 | pkg, getUidForPackageAndUser(pkg, user), group, false, true); |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 4029 | handleSavePolicyFile(); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 4030 | } |
| 4031 | |
| 4032 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4033 | public void updateNotificationChannelFromPrivilegedListener(INotificationListener token, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 4034 | String pkg, UserHandle user, NotificationChannel channel) throws RemoteException { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4035 | Preconditions.checkNotNull(channel); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 4036 | Preconditions.checkNotNull(pkg); |
| 4037 | Preconditions.checkNotNull(user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4038 | |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 4039 | verifyPrivilegedListener(token, user, false); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 4040 | updateNotificationChannelInt(pkg, getUidForPackageAndUser(pkg, user), channel, true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4041 | } |
| 4042 | |
| 4043 | @Override |
| 4044 | public ParceledListSlice<NotificationChannel> getNotificationChannelsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 4045 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 4046 | Preconditions.checkNotNull(pkg); |
| 4047 | Preconditions.checkNotNull(user); |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 4048 | verifyPrivilegedListener(token, user, true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4049 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 4050 | return mPreferencesHelper.getNotificationChannels(pkg, getUidForPackageAndUser(pkg, user), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 4051 | false /* includeDeleted */); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | @Override |
| 4055 | public ParceledListSlice<NotificationChannelGroup> |
| 4056 | getNotificationChannelGroupsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 4057 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 4058 | Preconditions.checkNotNull(pkg); |
| 4059 | Preconditions.checkNotNull(user); |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 4060 | verifyPrivilegedListener(token, user, true); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 4061 | |
| 4062 | List<NotificationChannelGroup> groups = new ArrayList<>(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 4063 | groups.addAll(mPreferencesHelper.getNotificationChannelGroups( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 4064 | pkg, getUidForPackageAndUser(pkg, user))); |
| 4065 | return new ParceledListSlice<>(groups); |
| 4066 | } |
| 4067 | |
Zimuzo | b3b9c26 | 2018-10-31 11:54:20 +0000 | [diff] [blame] | 4068 | @Override |
| 4069 | public void setPrivateNotificationsAllowed(boolean allow) { |
| 4070 | if (PackageManager.PERMISSION_GRANTED |
| 4071 | != getContext().checkCallingPermission( |
| 4072 | permission.CONTROL_KEYGUARD_SECURE_NOTIFICATIONS)) { |
| 4073 | throw new SecurityException( |
| 4074 | "Requires CONTROL_KEYGUARD_SECURE_NOTIFICATIONS permission"); |
| 4075 | } |
| 4076 | if (allow != mLockScreenAllowSecureNotifications) { |
| 4077 | mLockScreenAllowSecureNotifications = allow; |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 4078 | handleSavePolicyFile(); |
Zimuzo | b3b9c26 | 2018-10-31 11:54:20 +0000 | [diff] [blame] | 4079 | } |
| 4080 | } |
| 4081 | |
| 4082 | @Override |
| 4083 | public boolean getPrivateNotificationsAllowed() { |
| 4084 | if (PackageManager.PERMISSION_GRANTED |
| 4085 | != getContext().checkCallingPermission( |
| 4086 | permission.CONTROL_KEYGUARD_SECURE_NOTIFICATIONS)) { |
| 4087 | throw new SecurityException( |
| 4088 | "Requires CONTROL_KEYGUARD_SECURE_NOTIFICATIONS permission"); |
| 4089 | } |
| 4090 | return mLockScreenAllowSecureNotifications; |
| 4091 | } |
| 4092 | |
Julia Reynolds | b40cd09 | 2019-01-25 09:35:02 -0500 | [diff] [blame] | 4093 | @Override |
| 4094 | public boolean isPackagePaused(String pkg) { |
| 4095 | Preconditions.checkNotNull(pkg); |
| 4096 | checkCallerIsSameApp(pkg); |
| 4097 | |
| 4098 | boolean isPaused; |
| 4099 | |
| 4100 | final PackageManagerInternal pmi = LocalServices.getService( |
| 4101 | PackageManagerInternal.class); |
| 4102 | int flags = pmi.getDistractingPackageRestrictions( |
| 4103 | pkg, Binder.getCallingUserHandle().getIdentifier()); |
| 4104 | isPaused = ((flags & PackageManager.RESTRICTION_HIDE_NOTIFICATIONS) != 0); |
| 4105 | |
| 4106 | isPaused |= isPackageSuspendedForUser(pkg, Binder.getCallingUid()); |
| 4107 | |
| 4108 | return isPaused; |
| 4109 | } |
| 4110 | |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 4111 | private void verifyPrivilegedListener(INotificationListener token, UserHandle user, |
| 4112 | boolean assistantAllowed) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 4113 | ManagedServiceInfo info; |
| 4114 | synchronized (mNotificationLock) { |
| 4115 | info = mListeners.checkServiceTokenLocked(token); |
| 4116 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 4117 | if (!hasCompanionDevice(info)) { |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 4118 | synchronized (mNotificationLock) { |
| 4119 | if (!assistantAllowed || !mAssistants.isServiceTokenValidLocked(info.service)) { |
| 4120 | throw new SecurityException(info + " does not have access"); |
| 4121 | } |
| 4122 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 4123 | } |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 4124 | if (!info.enabledAndUserMatches(user.getIdentifier())) { |
| 4125 | throw new SecurityException(info + " does not have access"); |
| 4126 | } |
| 4127 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4128 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 4129 | private int getUidForPackageAndUser(String pkg, UserHandle user) throws RemoteException { |
| 4130 | int uid = 0; |
| 4131 | long identity = Binder.clearCallingIdentity(); |
| 4132 | try { |
| 4133 | uid = mPackageManager.getPackageUid(pkg, 0, user.getIdentifier()); |
| 4134 | } finally { |
| 4135 | Binder.restoreCallingIdentity(identity); |
| 4136 | } |
| 4137 | return uid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4138 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 4139 | |
| 4140 | @Override |
| 4141 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 4142 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) |
| 4143 | throws RemoteException { |
Dan Sandler | 7647f1d | 2018-11-26 09:56:26 -0500 | [diff] [blame] | 4144 | new NotificationShellCmd(NotificationManagerService.this) |
| 4145 | .exec(this, in, out, err, args, callback, resultReceiver); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 4146 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 4147 | }; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 4148 | |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 4149 | @VisibleForTesting |
| 4150 | protected void setNotificationAssistantAccessGrantedForUserInternal( |
Julia Reynolds | 4afe264 | 2019-05-01 08:42:24 -0400 | [diff] [blame] | 4151 | ComponentName assistant, int baseUserId, boolean granted) { |
| 4152 | List<UserInfo> users = mUm.getEnabledProfiles(baseUserId); |
| 4153 | if (users != null) { |
| 4154 | for (UserInfo user : users) { |
| 4155 | int userId = user.id; |
| 4156 | if (assistant == null) { |
| 4157 | ComponentName allowedAssistant = CollectionUtils.firstOrNull( |
| 4158 | mAssistants.getAllowedComponents(userId)); |
| 4159 | if (allowedAssistant != null) { |
| 4160 | setNotificationAssistantAccessGrantedForUserInternal( |
| 4161 | allowedAssistant, userId, false); |
| 4162 | } |
| 4163 | continue; |
| 4164 | } |
| 4165 | if (!granted || mAllowedManagedServicePackages.test(assistant.getPackageName(), |
| 4166 | userId, mAssistants.getRequiredPermission())) { |
| 4167 | mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(), |
| 4168 | userId, false, granted); |
| 4169 | mAssistants.setPackageOrComponentEnabled(assistant.flattenToString(), |
| 4170 | userId, true, granted); |
| 4171 | |
| 4172 | getContext().sendBroadcastAsUser( |
| 4173 | new Intent(ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 4174 | .setPackage(assistant.getPackageName()) |
| 4175 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
| 4176 | UserHandle.of(userId), null); |
| 4177 | |
| 4178 | handleSavePolicyFile(); |
| 4179 | } |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 4180 | } |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 4181 | } |
| 4182 | } |
| 4183 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4184 | private void applyAdjustment(NotificationRecord r, Adjustment adjustment) { |
| 4185 | if (r == null) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4186 | return; |
| 4187 | } |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 4188 | if (adjustment.getSignals() != null) { |
| 4189 | final Bundle adjustments = adjustment.getSignals(); |
| 4190 | Bundle.setDefusable(adjustments, true); |
| 4191 | List<String> toRemove = new ArrayList<>(); |
| 4192 | for (String potentialKey : adjustments.keySet()) { |
| 4193 | if (!mAssistants.isAdjustmentAllowed(potentialKey)) { |
| 4194 | toRemove.add(potentialKey); |
| 4195 | } |
Julia Reynolds | 418a8ff | 2019-03-21 10:45:10 -0400 | [diff] [blame] | 4196 | } |
Julia Reynolds | c7dcdc2 | 2019-03-25 10:26:14 -0400 | [diff] [blame] | 4197 | for (String removeKey : toRemove) { |
| 4198 | adjustments.remove(removeKey); |
| 4199 | } |
| 4200 | r.addAdjustment(adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4201 | } |
| 4202 | } |
| 4203 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4204 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4205 | void addAutogroupKeyLocked(String key) { |
| 4206 | NotificationRecord r = mNotificationsByKey.get(key); |
| 4207 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4208 | return; |
| 4209 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 4210 | if (r.sbn.getOverrideGroupKey() == null) { |
| 4211 | addAutoGroupAdjustment(r, GroupHelper.AUTOGROUP_KEY); |
| 4212 | EventLogTags.writeNotificationAutogrouped(key); |
| 4213 | mRankingHandler.requestSort(); |
| 4214 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4215 | } |
| 4216 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4217 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4218 | void removeAutogroupKeyLocked(String key) { |
| 4219 | NotificationRecord r = mNotificationsByKey.get(key); |
| 4220 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4221 | return; |
| 4222 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 4223 | if (r.sbn.getOverrideGroupKey() != null) { |
| 4224 | addAutoGroupAdjustment(r, null); |
| 4225 | EventLogTags.writeNotificationUnautogrouped(key); |
| 4226 | mRankingHandler.requestSort(); |
| 4227 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | private void addAutoGroupAdjustment(NotificationRecord r, String overrideGroupKey) { |
| 4231 | Bundle signals = new Bundle(); |
| 4232 | signals.putString(Adjustment.KEY_GROUP_KEY, overrideGroupKey); |
| 4233 | Adjustment adjustment = |
| 4234 | new Adjustment(r.sbn.getPackageName(), r.getKey(), signals, "", r.sbn.getUserId()); |
| 4235 | r.addAdjustment(adjustment); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | // Clears the 'fake' auto-group summary. |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4239 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4240 | private void clearAutogroupSummaryLocked(int userId, String pkg) { |
| 4241 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 4242 | if (summaries != null && summaries.containsKey(pkg)) { |
| 4243 | // Clear summary. |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4244 | final NotificationRecord removed = findNotificationByKeyLocked(summaries.remove(pkg)); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4245 | if (removed != null) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4246 | boolean wasPosted = removeFromNotificationListsLocked(removed); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4247 | cancelNotificationLocked(removed, false, REASON_UNAUTOBUNDLED, wasPosted, null); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4248 | } |
| 4249 | } |
| 4250 | } |
| 4251 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 4252 | @GuardedBy("mNotificationLock") |
| 4253 | private boolean hasAutoGroupSummaryLocked(StatusBarNotification sbn) { |
| 4254 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(sbn.getUserId()); |
| 4255 | return summaries != null && summaries.containsKey(sbn.getPackageName()); |
| 4256 | } |
| 4257 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4258 | // 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] | 4259 | private void createAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 4260 | NotificationRecord summaryRecord = null; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4261 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4262 | NotificationRecord notificationRecord = mNotificationsByKey.get(triggeringKey); |
| 4263 | if (notificationRecord == null) { |
| 4264 | // The notification could have been cancelled again already. A successive |
| 4265 | // adjustment will post a summary if needed. |
| 4266 | return; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4267 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4268 | final StatusBarNotification adjustedSbn = notificationRecord.sbn; |
| 4269 | userId = adjustedSbn.getUser().getIdentifier(); |
| 4270 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 4271 | if (summaries == null) { |
| 4272 | summaries = new ArrayMap<>(); |
| 4273 | } |
| 4274 | mAutobundledSummaries.put(userId, summaries); |
| 4275 | if (!summaries.containsKey(pkg)) { |
| 4276 | // Add summary |
| 4277 | final ApplicationInfo appInfo = |
| 4278 | adjustedSbn.getNotification().extras.getParcelable( |
| 4279 | Notification.EXTRA_BUILDER_APPLICATION_INFO); |
| 4280 | final Bundle extras = new Bundle(); |
| 4281 | extras.putParcelable(Notification.EXTRA_BUILDER_APPLICATION_INFO, appInfo); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 4282 | final String channelId = notificationRecord.getChannel().getId(); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4283 | final Notification summaryNotification = |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 4284 | new Notification.Builder(getContext(), channelId) |
| 4285 | .setSmallIcon(adjustedSbn.getNotification().getSmallIcon()) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4286 | .setGroupSummary(true) |
Julia Reynolds | 9d5786e | 2017-04-28 10:26:32 -0400 | [diff] [blame] | 4287 | .setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4288 | .setGroup(GroupHelper.AUTOGROUP_KEY) |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 4289 | .setFlag(FLAG_AUTOGROUP_SUMMARY, true) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4290 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 4291 | .setColor(adjustedSbn.getNotification().color) |
| 4292 | .setLocalOnly(true) |
| 4293 | .build(); |
| 4294 | summaryNotification.extras.putAll(extras); |
| 4295 | Intent appIntent = getContext().getPackageManager().getLaunchIntentForPackage(pkg); |
| 4296 | if (appIntent != null) { |
| 4297 | summaryNotification.contentIntent = PendingIntent.getActivityAsUser( |
| 4298 | getContext(), 0, appIntent, 0, null, UserHandle.of(userId)); |
| 4299 | } |
| 4300 | final StatusBarNotification summarySbn = |
| 4301 | new StatusBarNotification(adjustedSbn.getPackageName(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 4302 | adjustedSbn.getOpPkg(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 4303 | Integer.MAX_VALUE, |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4304 | GroupHelper.AUTOGROUP_KEY, adjustedSbn.getUid(), |
| 4305 | adjustedSbn.getInitialPid(), summaryNotification, |
| 4306 | adjustedSbn.getUser(), GroupHelper.AUTOGROUP_KEY, |
| 4307 | System.currentTimeMillis()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4308 | summaryRecord = new NotificationRecord(getContext(), summarySbn, |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 4309 | notificationRecord.getChannel()); |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 4310 | summaryRecord.setIsAppImportanceLocked( |
| 4311 | notificationRecord.getIsAppImportanceLocked()); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4312 | summaries.put(pkg, summarySbn.getKey()); |
| 4313 | } |
| 4314 | } |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4315 | if (summaryRecord != null && checkDisqualifyingFeatures(userId, MY_UID, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 4316 | summaryRecord.sbn.getId(), summaryRecord.sbn.getTag(), summaryRecord, true)) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4317 | mHandler.post(new EnqueueNotificationRunnable(userId, summaryRecord)); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4318 | } |
| 4319 | } |
| 4320 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 4321 | private String disableNotificationEffects(NotificationRecord record) { |
| 4322 | if (mDisableNotificationEffects) { |
| 4323 | return "booleanState"; |
| 4324 | } |
| 4325 | if ((mListenerHints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 4326 | return "listenerHints"; |
| 4327 | } |
Julia Reynolds | db7081e | 2019-01-03 14:35:38 -0500 | [diff] [blame] | 4328 | if (record != null && record.getAudioAttributes() != null) { |
| 4329 | if ((mListenerHints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 4330 | if (record.getAudioAttributes().getUsage() |
| 4331 | != AudioAttributes.USAGE_VOICE_COMMUNICATION) { |
| 4332 | return "listenerNoti"; |
| 4333 | } |
| 4334 | } |
| 4335 | if ((mListenerHints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 4336 | if (record.getAudioAttributes().getUsage() |
| 4337 | == AudioAttributes.USAGE_VOICE_COMMUNICATION) { |
| 4338 | return "listenerCall"; |
| 4339 | } |
| 4340 | } |
| 4341 | } |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 4342 | if (mCallState != TelephonyManager.CALL_STATE_IDLE && !mZenModeHelper.isCall(record)) { |
| 4343 | return "callState"; |
| 4344 | } |
| 4345 | return null; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 4346 | }; |
| 4347 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 4348 | private void dumpJson(PrintWriter pw, @NonNull DumpFilter filter) { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 4349 | JSONObject dump = new JSONObject(); |
| 4350 | try { |
| 4351 | dump.put("service", "Notification Manager"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 4352 | dump.put("bans", mPreferencesHelper.dumpBansJson(filter)); |
| 4353 | dump.put("ranking", mPreferencesHelper.dumpJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 4354 | dump.put("stats", mUsageStats.dumpJson(filter)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 4355 | dump.put("channels", mPreferencesHelper.dumpChannelsJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 4356 | } catch (JSONException e) { |
| 4357 | e.printStackTrace(); |
| 4358 | } |
| 4359 | pw.println(dump); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4360 | } |
| 4361 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 4362 | private void dumpProto(FileDescriptor fd, @NonNull DumpFilter filter) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 4363 | final ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 4364 | synchronized (mNotificationLock) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 4365 | int N = mNotificationList.size(); |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 4366 | for (int i = 0; i < N; i++) { |
| 4367 | final NotificationRecord nr = mNotificationList.get(i); |
| 4368 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 4369 | nr.dump(proto, NotificationServiceDumpProto.RECORDS, filter.redact, |
| 4370 | NotificationRecordProto.POSTED); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 4371 | } |
| 4372 | N = mEnqueuedNotifications.size(); |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 4373 | for (int i = 0; i < N; i++) { |
| 4374 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 4375 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 4376 | nr.dump(proto, NotificationServiceDumpProto.RECORDS, filter.redact, |
| 4377 | NotificationRecordProto.ENQUEUED); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 4378 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4379 | List<NotificationRecord> snoozed = mSnoozeHelper.getSnoozed(); |
| 4380 | N = snoozed.size(); |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 4381 | for (int i = 0; i < N; i++) { |
| 4382 | final NotificationRecord nr = snoozed.get(i); |
| 4383 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 4384 | nr.dump(proto, NotificationServiceDumpProto.RECORDS, filter.redact, |
| 4385 | NotificationRecordProto.SNOOZED); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4386 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4387 | |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 4388 | long zenLog = proto.start(NotificationServiceDumpProto.ZEN); |
| 4389 | mZenModeHelper.dump(proto); |
| 4390 | for (ComponentName suppressor : mEffectsSuppressors) { |
Kweku Adams | 9954633 | 2018-01-24 17:03:50 -0800 | [diff] [blame] | 4391 | suppressor.writeToProto(proto, ZenModeProto.SUPPRESSORS); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 4392 | } |
| 4393 | proto.end(zenLog); |
| 4394 | |
| 4395 | long listenersToken = proto.start(NotificationServiceDumpProto.NOTIFICATION_LISTENERS); |
| 4396 | mListeners.dump(proto, filter); |
| 4397 | proto.end(listenersToken); |
| 4398 | |
| 4399 | proto.write(NotificationServiceDumpProto.LISTENER_HINTS, mListenerHints); |
| 4400 | |
| 4401 | for (int i = 0; i < mListenersDisablingEffects.size(); ++i) { |
| 4402 | long effectsToken = proto.start( |
| 4403 | NotificationServiceDumpProto.LISTENERS_DISABLING_EFFECTS); |
| 4404 | |
| 4405 | proto.write( |
| 4406 | ListenersDisablingEffectsProto.HINT, mListenersDisablingEffects.keyAt(i)); |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 4407 | final ArraySet<ComponentName> listeners = |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 4408 | mListenersDisablingEffects.valueAt(i); |
| 4409 | for (int j = 0; j < listeners.size(); j++) { |
Kweku Adams | f216953 | 2018-12-20 04:15:49 -0800 | [diff] [blame] | 4410 | final ComponentName componentName = listeners.valueAt(j); |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 4411 | componentName.writeToProto(proto, |
| 4412 | ListenersDisablingEffectsProto.LISTENER_COMPONENTS); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | proto.end(effectsToken); |
| 4416 | } |
| 4417 | |
| 4418 | long assistantsToken = proto.start( |
| 4419 | NotificationServiceDumpProto.NOTIFICATION_ASSISTANTS); |
| 4420 | mAssistants.dump(proto, filter); |
| 4421 | proto.end(assistantsToken); |
| 4422 | |
| 4423 | long conditionsToken = proto.start(NotificationServiceDumpProto.CONDITION_PROVIDERS); |
| 4424 | mConditionProviders.dump(proto, filter); |
| 4425 | proto.end(conditionsToken); |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 4426 | |
| 4427 | long rankingToken = proto.start(NotificationServiceDumpProto.RANKING_CONFIG); |
| 4428 | mRankingHelper.dump(proto, filter); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 4429 | mPreferencesHelper.dump(proto, filter); |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 4430 | proto.end(rankingToken); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4431 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4432 | |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 4433 | proto.flush(); |
| 4434 | } |
| 4435 | |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 4436 | private void dumpNotificationRecords(PrintWriter pw, @NonNull DumpFilter filter) { |
| 4437 | synchronized (mNotificationLock) { |
| 4438 | int N; |
| 4439 | N = mNotificationList.size(); |
| 4440 | if (N > 0) { |
| 4441 | pw.println(" Notification List:"); |
| 4442 | for (int i = 0; i < N; i++) { |
| 4443 | final NotificationRecord nr = mNotificationList.get(i); |
| 4444 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 4445 | nr.dump(pw, " ", getContext(), filter.redact); |
| 4446 | } |
| 4447 | pw.println(" "); |
| 4448 | } |
| 4449 | } |
| 4450 | } |
| 4451 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 4452 | void dumpImpl(PrintWriter pw, @NonNull DumpFilter filter) { |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 4453 | pw.print("Current Notification Manager state"); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 4454 | if (filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4455 | pw.print(" (filtered to "); pw.print(filter); pw.print(")"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 4456 | } |
| 4457 | pw.println(':'); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4458 | int N; |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 4459 | final boolean zenOnly = filter.filtered && filter.zen; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4460 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4461 | if (!zenOnly) { |
| 4462 | synchronized (mToastQueue) { |
| 4463 | N = mToastQueue.size(); |
| 4464 | if (N > 0) { |
| 4465 | pw.println(" Toast Queue:"); |
| 4466 | for (int i=0; i<N; i++) { |
| 4467 | mToastQueue.get(i).dump(pw, " ", filter); |
| 4468 | } |
| 4469 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4470 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4471 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4472 | } |
| 4473 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4474 | synchronized (mNotificationLock) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4475 | if (!zenOnly) { |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 4476 | // Priority filters are only set when called via bugreport. If set |
| 4477 | // skip sections that are part of the critical section. |
| 4478 | if (!filter.normalPriority) { |
| 4479 | dumpNotificationRecords(pw, filter); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4480 | } |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 4481 | if (!filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4482 | N = mLights.size(); |
| 4483 | if (N > 0) { |
| 4484 | pw.println(" Lights List:"); |
| 4485 | for (int i=0; i<N; i++) { |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 4486 | if (i == N - 1) { |
| 4487 | pw.print(" > "); |
| 4488 | } else { |
| 4489 | pw.print(" "); |
| 4490 | } |
| 4491 | pw.println(mLights.get(i)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4492 | } |
| 4493 | pw.println(" "); |
| 4494 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 4495 | pw.println(" mUseAttentionLight=" + mUseAttentionLight); |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 4496 | pw.println(" mHasLight=" + mHasLight); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 4497 | pw.println(" mNotificationPulseEnabled=" + mNotificationPulseEnabled); |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 4498 | pw.println(" mSoundNotificationKey=" + mSoundNotificationKey); |
| 4499 | pw.println(" mVibrateNotificationKey=" + mVibrateNotificationKey); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4500 | pw.println(" mDisableNotificationEffects=" + mDisableNotificationEffects); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 4501 | pw.println(" mCallState=" + callStateToString(mCallState)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4502 | pw.println(" mSystemReady=" + mSystemReady); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 4503 | pw.println(" mMaxPackageEnqueueRate=" + mMaxPackageEnqueueRate); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4504 | } |
| 4505 | pw.println(" mArchive=" + mArchive.toString()); |
| 4506 | Iterator<StatusBarNotification> iter = mArchive.descendingIterator(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4507 | int j=0; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4508 | while (iter.hasNext()) { |
| 4509 | final StatusBarNotification sbn = iter.next(); |
| 4510 | if (filter != null && !filter.matches(sbn)) continue; |
| 4511 | pw.println(" " + sbn); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4512 | if (++j >= 5) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4513 | if (iter.hasNext()) pw.println(" ..."); |
| 4514 | break; |
| 4515 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4516 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4517 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4518 | if (!zenOnly) { |
| 4519 | N = mEnqueuedNotifications.size(); |
| 4520 | if (N > 0) { |
| 4521 | pw.println(" Enqueued Notification List:"); |
| 4522 | for (int i = 0; i < N; i++) { |
| 4523 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 4524 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 4525 | nr.dump(pw, " ", getContext(), filter.redact); |
| 4526 | } |
| 4527 | pw.println(" "); |
| 4528 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4529 | |
| 4530 | mSnoozeHelper.dump(pw, filter); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4531 | } |
| 4532 | } |
| 4533 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4534 | if (!zenOnly) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4535 | pw.println("\n Ranking Config:"); |
| 4536 | mRankingHelper.dump(pw, " ", filter); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4537 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 4538 | pw.println("\n Notification Preferences:"); |
| 4539 | mPreferencesHelper.dump(pw, " ", filter); |
| 4540 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4541 | pw.println("\n Notification listeners:"); |
| 4542 | mListeners.dump(pw, filter); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4543 | pw.print(" mListenerHints: "); pw.println(mListenerHints); |
| 4544 | pw.print(" mListenersDisablingEffects: ("); |
| 4545 | N = mListenersDisablingEffects.size(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4546 | for (int i = 0; i < N; i++) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 4547 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 4548 | if (i > 0) pw.print(';'); |
| 4549 | pw.print("hint[" + hint + "]:"); |
| 4550 | |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 4551 | final ArraySet<ComponentName> listeners = mListenersDisablingEffects.valueAt(i); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 4552 | final int listenerSize = listeners.size(); |
| 4553 | |
| 4554 | for (int j = 0; j < listenerSize; j++) { |
Kweku Adams | f216953 | 2018-12-20 04:15:49 -0800 | [diff] [blame] | 4555 | if (j > 0) pw.print(','); |
| 4556 | final ComponentName listener = listeners.valueAt(j); |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 4557 | if (listener != null) { |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 4558 | pw.print(listener); |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 4559 | } |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 4560 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4561 | } |
| 4562 | pw.println(')'); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 4563 | pw.println("\n Notification assistant services:"); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4564 | mAssistants.dump(pw, filter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4565 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 4566 | |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4567 | if (!filter.filtered || zenOnly) { |
| 4568 | pw.println("\n Zen Mode:"); |
| 4569 | pw.print(" mInterruptionFilter="); pw.println(mInterruptionFilter); |
| 4570 | mZenModeHelper.dump(pw, " "); |
| 4571 | |
| 4572 | pw.println("\n Zen Log:"); |
| 4573 | ZenLog.dump(pw, " "); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 4574 | } |
| 4575 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 4576 | pw.println("\n Condition providers:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 4577 | mConditionProviders.dump(pw, filter); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4578 | |
| 4579 | pw.println("\n Group summaries:"); |
| 4580 | for (Entry<String, NotificationRecord> entry : mSummaryByGroupKey.entrySet()) { |
| 4581 | NotificationRecord r = entry.getValue(); |
| 4582 | pw.println(" " + entry.getKey() + " -> " + r.getKey()); |
| 4583 | if (mNotificationsByKey.get(r.getKey()) != r) { |
| 4584 | pw.println("!!!!!!LEAK: Record not found in mNotificationsByKey."); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 4585 | r.dump(pw, " ", getContext(), filter.redact); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4586 | } |
| 4587 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4588 | |
| 4589 | if (!zenOnly) { |
| 4590 | pw.println("\n Usage Stats:"); |
| 4591 | mUsageStats.dump(pw, " ", filter); |
| 4592 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4593 | } |
| 4594 | } |
| 4595 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4596 | /** |
| 4597 | * The private API only accessible to the system process. |
| 4598 | */ |
| 4599 | private final NotificationManagerInternal mInternalService = new NotificationManagerInternal() { |
| 4600 | @Override |
Julia Reynolds | f3de8aa | 2017-09-29 15:52:37 -0400 | [diff] [blame] | 4601 | public NotificationChannel getNotificationChannel(String pkg, int uid, String |
| 4602 | channelId) { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 4603 | return mPreferencesHelper.getNotificationChannel(pkg, uid, channelId, false); |
Julia Reynolds | f3de8aa | 2017-09-29 15:52:37 -0400 | [diff] [blame] | 4604 | } |
| 4605 | |
| 4606 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 4607 | public void enqueueNotification(String pkg, String opPkg, int callingUid, int callingPid, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 4608 | String tag, int id, Notification notification, int userId) { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 4609 | enqueueNotificationInternal(pkg, opPkg, callingUid, callingPid, tag, id, notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 4610 | userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4611 | } |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 4612 | |
| 4613 | @Override |
| 4614 | public void removeForegroundServiceFlagFromNotification(String pkg, int notificationId, |
| 4615 | int userId) { |
| 4616 | checkCallerIsSystem(); |
Julia Reynolds | 564273f | 2018-09-13 15:53:11 -0400 | [diff] [blame] | 4617 | mHandler.post(() -> { |
| 4618 | synchronized (mNotificationLock) { |
| 4619 | // strip flag from all enqueued notifications. listeners will be informed |
| 4620 | // in post runnable. |
| 4621 | List<NotificationRecord> enqueued = findNotificationsByListLocked( |
| 4622 | mEnqueuedNotifications, pkg, null, notificationId, userId); |
| 4623 | for (int i = 0; i < enqueued.size(); i++) { |
| 4624 | removeForegroundServiceFlagLocked(enqueued.get(i)); |
| 4625 | } |
| 4626 | |
| 4627 | // if posted notification exists, strip its flag and tell listeners |
| 4628 | NotificationRecord r = findNotificationByListLocked( |
| 4629 | mNotificationList, pkg, null, notificationId, userId); |
| 4630 | if (r != null) { |
| 4631 | removeForegroundServiceFlagLocked(r); |
| 4632 | mRankingHelper.sort(mNotificationList); |
| 4633 | mListeners.notifyPostedLocked(r, r); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4634 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 4635 | } |
| 4636 | }); |
| 4637 | } |
| 4638 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4639 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 564273f | 2018-09-13 15:53:11 -0400 | [diff] [blame] | 4640 | private void removeForegroundServiceFlagLocked(NotificationRecord r) { |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 4641 | if (r == null) { |
| 4642 | return; |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 4643 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 4644 | StatusBarNotification sbn = r.sbn; |
| 4645 | // NoMan adds flags FLAG_NO_CLEAR and FLAG_ONGOING_EVENT when it sees |
| 4646 | // FLAG_FOREGROUND_SERVICE. Hence it's not enough to remove |
| 4647 | // FLAG_FOREGROUND_SERVICE, we have to revert to the flags we received |
| 4648 | // initially *and* force remove FLAG_FOREGROUND_SERVICE. |
| 4649 | sbn.getNotification().flags = |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4650 | (r.mOriginalFlags & ~FLAG_FOREGROUND_SERVICE); |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 4651 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4652 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4653 | |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 4654 | void enqueueNotificationInternal(final String pkg, final String opPkg, final int callingUid, |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 4655 | final int callingPid, final String tag, final int id, final Notification notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 4656 | int incomingUserId) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 4657 | if (DBG) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4658 | Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id |
| 4659 | + " notification=" + notification); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 4660 | } |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4661 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4662 | if (pkg == null || notification == null) { |
| 4663 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 4664 | + " id=" + id + " notification=" + notification); |
| 4665 | } |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4666 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4667 | final int userId = ActivityManager.handleIncomingUser(callingPid, |
| 4668 | callingUid, incomingUserId, true, false, "enqueueNotification", pkg); |
| 4669 | final UserHandle user = UserHandle.of(userId); |
| 4670 | |
| 4671 | // Can throw a SecurityException if the calling uid doesn't have permission to post |
| 4672 | // as "pkg" |
| 4673 | final int notificationUid = resolveNotificationUid(opPkg, pkg, callingUid, userId); |
| 4674 | |
| 4675 | checkRestrictedCategories(notification); |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4676 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4677 | // Fix the notification as best we can. |
| 4678 | try { |
Julia Reynolds | 47fd15f | 2018-11-28 10:16:00 -0500 | [diff] [blame] | 4679 | fixNotification(notification, pkg, userId); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 4680 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4681 | } catch (NameNotFoundException e) { |
| 4682 | Slog.e(TAG, "Cannot create a context for sending app", e); |
| 4683 | return; |
| 4684 | } |
| 4685 | |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 4686 | mUsageStats.registerEnqueuedByApp(pkg); |
| 4687 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4688 | // setup local book-keeping |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 4689 | String channelId = notification.getChannelId(); |
| 4690 | if (mIsTelevision && (new Notification.TvExtender(notification)).getChannelId() != null) { |
| 4691 | channelId = (new Notification.TvExtender(notification)).getChannelId(); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 4692 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 4693 | final NotificationChannel channel = mPreferencesHelper.getNotificationChannel(pkg, |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4694 | notificationUid, channelId, false /* includeDeleted */); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 4695 | if (channel == null) { |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 4696 | final String noChannelStr = "No Channel found for " |
| 4697 | + "pkg=" + pkg |
| 4698 | + ", channelId=" + channelId |
Julia Reynolds | f26eb91 | 2017-05-22 15:47:06 -0400 | [diff] [blame] | 4699 | + ", id=" + id |
| 4700 | + ", tag=" + tag |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 4701 | + ", opPkg=" + opPkg |
| 4702 | + ", callingUid=" + callingUid |
| 4703 | + ", userId=" + userId |
| 4704 | + ", incomingUserId=" + incomingUserId |
| 4705 | + ", notificationUid=" + notificationUid |
| 4706 | + ", notification=" + notification; |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 4707 | Slog.e(TAG, noChannelStr); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 4708 | boolean appNotificationsOff = mPreferencesHelper.getImportance(pkg, notificationUid) |
Beverly | 5d4564b | 2018-04-10 20:09:23 -0400 | [diff] [blame] | 4709 | == NotificationManager.IMPORTANCE_NONE; |
| 4710 | |
| 4711 | if (!appNotificationsOff) { |
| 4712 | doChannelWarningToast("Developer warning for package \"" + pkg + "\"\n" + |
| 4713 | "Failed to post notification on channel \"" + channelId + "\"\n" + |
| 4714 | "See log for more details"); |
| 4715 | } |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 4716 | return; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 4717 | } |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 4718 | |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 4719 | final StatusBarNotification n = new StatusBarNotification( |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4720 | pkg, opPkg, id, tag, notificationUid, callingPid, notification, |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 4721 | user, null, System.currentTimeMillis()); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 4722 | final NotificationRecord r = new NotificationRecord(getContext(), n, channel); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 4723 | r.setIsAppImportanceLocked(mPreferencesHelper.getIsAppImportanceLocked(pkg, callingUid)); |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 4724 | |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 4725 | if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 4726 | final boolean fgServiceShown = channel.isFgServiceShown(); |
| 4727 | if (((channel.getUserLockedFields() & NotificationChannel.USER_LOCKED_IMPORTANCE) == 0 |
| 4728 | || !fgServiceShown) |
| 4729 | && (r.getImportance() == IMPORTANCE_MIN |
| 4730 | || r.getImportance() == IMPORTANCE_NONE)) { |
| 4731 | // Increase the importance of foreground service notifications unless the user had |
| 4732 | // an opinion otherwise (and the channel hasn't yet shown a fg service). |
| 4733 | if (TextUtils.isEmpty(channelId) |
| 4734 | || NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 4735 | r.setSystemImportance(IMPORTANCE_LOW); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 4736 | } else { |
| 4737 | channel.setImportance(IMPORTANCE_LOW); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 4738 | r.setSystemImportance(IMPORTANCE_LOW); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 4739 | if (!fgServiceShown) { |
| 4740 | channel.unlockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); |
| 4741 | channel.setFgServiceShown(true); |
| 4742 | } |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 4743 | mPreferencesHelper.updateNotificationChannel( |
| 4744 | pkg, notificationUid, channel, false); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 4745 | r.updateNotificationChannel(channel); |
| 4746 | } |
| 4747 | } else if (!fgServiceShown && !TextUtils.isEmpty(channelId) |
| 4748 | && !NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 4749 | channel.setFgServiceShown(true); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 4750 | r.updateNotificationChannel(channel); |
| 4751 | } |
| 4752 | } |
| 4753 | |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 4754 | if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r, |
| 4755 | r.sbn.getOverrideGroupKey() != null)) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4756 | return; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 4757 | } |
| 4758 | |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 4759 | // Whitelist pending intents. |
| 4760 | if (notification.allPendingIntents != null) { |
| 4761 | final int intentCount = notification.allPendingIntents.size(); |
| 4762 | if (intentCount > 0) { |
| 4763 | final ActivityManagerInternal am = LocalServices |
| 4764 | .getService(ActivityManagerInternal.class); |
| 4765 | final long duration = LocalServices.getService( |
| 4766 | DeviceIdleController.LocalService.class).getNotificationWhitelistDuration(); |
| 4767 | for (int i = 0; i < intentCount; i++) { |
| 4768 | PendingIntent pendingIntent = notification.allPendingIntents.valueAt(i); |
| 4769 | if (pendingIntent != null) { |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 4770 | am.setPendingIntentWhitelistDuration(pendingIntent.getTarget(), |
| 4771 | WHITELIST_TOKEN, duration); |
Michal Karpinski | ac116df | 2018-12-10 17:51:42 +0000 | [diff] [blame] | 4772 | am.setPendingIntentAllowBgActivityStarts(pendingIntent.getTarget(), |
Michal Karpinski | c8aa91b | 2019-01-10 16:45:59 +0000 | [diff] [blame] | 4773 | WHITELIST_TOKEN, (FLAG_ACTIVITY_SENDER | FLAG_BROADCAST_SENDER |
| 4774 | | FLAG_SERVICE_SENDER)); |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 4775 | } |
| 4776 | } |
| 4777 | } |
| 4778 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 4779 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4780 | mHandler.post(new EnqueueNotificationRunnable(userId, r)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4781 | } |
| 4782 | |
Julia Reynolds | 47fd15f | 2018-11-28 10:16:00 -0500 | [diff] [blame] | 4783 | @VisibleForTesting |
| 4784 | protected void fixNotification(Notification notification, String pkg, int userId) |
| 4785 | throws NameNotFoundException { |
| 4786 | final ApplicationInfo ai = mPackageManagerClient.getApplicationInfoAsUser( |
| 4787 | pkg, PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 4788 | (userId == UserHandle.USER_ALL) ? USER_SYSTEM : userId); |
| 4789 | Notification.addFieldsFromContext(ai, notification); |
| 4790 | |
| 4791 | int canColorize = mPackageManagerClient.checkPermission( |
| 4792 | android.Manifest.permission.USE_COLORIZED_NOTIFICATIONS, pkg); |
| 4793 | if (canColorize == PERMISSION_GRANTED) { |
| 4794 | notification.flags |= Notification.FLAG_CAN_COLORIZE; |
| 4795 | } else { |
| 4796 | notification.flags &= ~Notification.FLAG_CAN_COLORIZE; |
| 4797 | } |
| 4798 | |
Takamasa Kuramitsu | 3dd7b2d | 2018-12-14 20:46:02 +0900 | [diff] [blame] | 4799 | if (notification.fullScreenIntent != null && ai.targetSdkVersion >= Build.VERSION_CODES.Q) { |
Julia Reynolds | 47fd15f | 2018-11-28 10:16:00 -0500 | [diff] [blame] | 4800 | int fullscreenIntentPermission = mPackageManagerClient.checkPermission( |
| 4801 | android.Manifest.permission.USE_FULL_SCREEN_INTENT, pkg); |
| 4802 | if (fullscreenIntentPermission != PERMISSION_GRANTED) { |
| 4803 | notification.fullScreenIntent = null; |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 4804 | Slog.w(TAG, "Package " + pkg + |
Julia Reynolds | 47fd15f | 2018-11-28 10:16:00 -0500 | [diff] [blame] | 4805 | ": Use of fullScreenIntent requires the USE_FULL_SCREEN_INTENT permission"); |
| 4806 | } |
| 4807 | } |
| 4808 | } |
| 4809 | |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4810 | /** |
| 4811 | * Updates the flags for this notification to reflect whether it is a bubble or not. |
| 4812 | */ |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4813 | private void flagNotificationForBubbles(NotificationRecord r, String pkg, int userId, |
| 4814 | NotificationRecord oldRecord) { |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4815 | Notification notification = r.getNotification(); |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 4816 | if (isNotificationAppropriateToBubble(r, pkg, userId, oldRecord)) { |
| 4817 | notification.flags |= FLAG_BUBBLE; |
| 4818 | } else { |
| 4819 | notification.flags &= ~FLAG_BUBBLE; |
| 4820 | } |
| 4821 | } |
| 4822 | |
| 4823 | /** |
| 4824 | * @return whether the provided notification record is allowed to be represented as a bubble. |
| 4825 | */ |
| 4826 | private boolean isNotificationAppropriateToBubble(NotificationRecord r, String pkg, int userId, |
| 4827 | NotificationRecord oldRecord) { |
| 4828 | Notification notification = r.getNotification(); |
Mady Mellor | ca0c24c | 2019-05-16 16:14:32 -0700 | [diff] [blame] | 4829 | Notification.BubbleMetadata metadata = notification.getBubbleMetadata(); |
| 4830 | boolean intentCanBubble = metadata != null |
| 4831 | && canLaunchInActivityView(getContext(), metadata.getIntent(), pkg); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4832 | |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 4833 | // Does the app want to bubble & is able to bubble |
Mady Mellor | ca0c24c | 2019-05-16 16:14:32 -0700 | [diff] [blame] | 4834 | boolean canBubble = intentCanBubble |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4835 | && mPreferencesHelper.areBubblesAllowed(pkg, userId) |
Mady Mellor | c682034 | 2019-05-20 12:04:36 -0700 | [diff] [blame] | 4836 | && mPreferencesHelper.bubblesEnabled(r.sbn.getUser()) |
Mady Mellor | 5c11a2e | 2019-04-25 17:26:15 -0700 | [diff] [blame] | 4837 | && r.getChannel().canBubble() |
| 4838 | && !mActivityManager.isLowRamDevice(); |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4839 | |
| 4840 | // Is the app in the foreground? |
| 4841 | final boolean appIsForeground = |
| 4842 | mActivityManager.getPackageImportance(pkg) == IMPORTANCE_FOREGROUND; |
| 4843 | |
| 4844 | // Is the notification something we'd allow to bubble? |
| 4845 | // A call with a foreground service + person |
| 4846 | ArrayList<Person> peopleList = notification.extras != null |
| 4847 | ? notification.extras.getParcelableArrayList(Notification.EXTRA_PEOPLE_LIST) |
| 4848 | : null; |
| 4849 | boolean isForegroundCall = CATEGORY_CALL.equals(notification.category) |
| 4850 | && (notification.flags & FLAG_FOREGROUND_SERVICE) != 0; |
Mady Mellor | a10448e | 2019-04-26 13:50:58 -0700 | [diff] [blame] | 4851 | // OR message style (which always has a person) with any remote input |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4852 | Class<? extends Notification.Style> style = notification.getNotificationStyle(); |
| 4853 | boolean isMessageStyle = Notification.MessagingStyle.class.equals(style); |
Mady Mellor | a10448e | 2019-04-26 13:50:58 -0700 | [diff] [blame] | 4854 | boolean notificationAppropriateToBubble = |
| 4855 | (isMessageStyle && hasValidRemoteInput(notification)) |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4856 | || (peopleList != null && !peopleList.isEmpty() && isForegroundCall); |
Mady Mellor | a10448e | 2019-04-26 13:50:58 -0700 | [diff] [blame] | 4857 | |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 4858 | // OR something that was previously a bubble & still exists |
| 4859 | boolean bubbleUpdate = oldRecord != null |
| 4860 | && (oldRecord.getNotification().flags & FLAG_BUBBLE) != 0; |
Mady Mellor | a54e9fa | 2019-04-18 13:26:18 -0700 | [diff] [blame] | 4861 | return canBubble && (notificationAppropriateToBubble || appIsForeground || bubbleUpdate); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 4862 | } |
| 4863 | |
Mady Mellor | a10448e | 2019-04-26 13:50:58 -0700 | [diff] [blame] | 4864 | private boolean hasValidRemoteInput(Notification n) { |
| 4865 | // Also check for inline reply |
| 4866 | Notification.Action[] actions = n.actions; |
| 4867 | if (actions != null) { |
| 4868 | // Get the remote inputs |
| 4869 | for (int i = 0; i < actions.length; i++) { |
| 4870 | Notification.Action action = actions[i]; |
| 4871 | RemoteInput[] inputs = action.getRemoteInputs(); |
| 4872 | if (inputs != null && inputs.length > 0) { |
| 4873 | return true; |
| 4874 | } |
| 4875 | } |
| 4876 | } |
| 4877 | return false; |
| 4878 | } |
| 4879 | |
Mady Mellor | ca0c24c | 2019-05-16 16:14:32 -0700 | [diff] [blame] | 4880 | /** |
| 4881 | * Whether an intent is properly configured to display in an {@link android.app.ActivityView}. |
| 4882 | * |
| 4883 | * @param context the context to use. |
| 4884 | * @param pendingIntent the pending intent of the bubble. |
| 4885 | * @param packageName the notification package name for this bubble. |
| 4886 | */ |
| 4887 | // Keep checks in sync with BubbleController#canLaunchInActivityView. |
| 4888 | @VisibleForTesting |
| 4889 | protected boolean canLaunchInActivityView(Context context, PendingIntent pendingIntent, |
| 4890 | String packageName) { |
| 4891 | if (pendingIntent == null) { |
| 4892 | Log.w(TAG, "Unable to create bubble -- no intent"); |
| 4893 | return false; |
| 4894 | } |
| 4895 | |
| 4896 | // Need escalated privileges to get the intent. |
| 4897 | final long token = Binder.clearCallingIdentity(); |
| 4898 | Intent intent; |
| 4899 | try { |
| 4900 | intent = pendingIntent.getIntent(); |
| 4901 | } finally { |
| 4902 | Binder.restoreCallingIdentity(token); |
| 4903 | } |
| 4904 | |
| 4905 | ActivityInfo info = intent != null |
| 4906 | ? intent.resolveActivityInfo(context.getPackageManager(), 0) |
| 4907 | : null; |
| 4908 | if (info == null) { |
| 4909 | StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName, |
| 4910 | BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING); |
| 4911 | Log.w(TAG, "Unable to send as bubble -- couldn't find activity info for intent: " |
| 4912 | + intent); |
| 4913 | return false; |
| 4914 | } |
| 4915 | if (!ActivityInfo.isResizeableMode(info.resizeMode)) { |
| 4916 | StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName, |
| 4917 | BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE); |
| 4918 | Log.w(TAG, "Unable to send as bubble -- activity is not resizable for intent: " |
| 4919 | + intent); |
| 4920 | return false; |
| 4921 | } |
| 4922 | if (info.documentLaunchMode != DOCUMENT_LAUNCH_ALWAYS) { |
| 4923 | StatsLog.write(StatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, packageName, |
| 4924 | BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__DOCUMENT_LAUNCH_NOT_ALWAYS); |
| 4925 | Log.w(TAG, "Unable to send as bubble -- activity is not documentLaunchMode=always " |
| 4926 | + "for intent: " + intent); |
| 4927 | return false; |
| 4928 | } |
| 4929 | if ((info.flags & ActivityInfo.FLAG_ALLOW_EMBEDDED) == 0) { |
| 4930 | Log.w(TAG, "Unable to send as bubble -- activity is not embeddable for intent: " |
| 4931 | + intent); |
| 4932 | return false; |
| 4933 | } |
| 4934 | return true; |
| 4935 | } |
| 4936 | |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 4937 | private void doChannelWarningToast(CharSequence toastText) { |
Julia Reynolds | bba26b1 | 2018-10-11 09:21:11 -0400 | [diff] [blame] | 4938 | Binder.withCleanCallingIdentity(() -> { |
| 4939 | final int defaultWarningEnabled = Build.IS_DEBUGGABLE ? 1 : 0; |
| 4940 | final boolean warningEnabled = Settings.Global.getInt(getContext().getContentResolver(), |
| 4941 | Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, defaultWarningEnabled) != 0; |
| 4942 | if (warningEnabled) { |
| 4943 | Toast toast = Toast.makeText(getContext(), mHandler.getLooper(), toastText, |
| 4944 | Toast.LENGTH_SHORT); |
| 4945 | toast.show(); |
| 4946 | } |
| 4947 | }); |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 4948 | } |
| 4949 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4950 | @VisibleForTesting |
| 4951 | int resolveNotificationUid(String callingPkg, String targetPkg, |
| 4952 | int callingUid, int userId) { |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4953 | if (userId == UserHandle.USER_ALL) { |
| 4954 | userId = USER_SYSTEM; |
| 4955 | } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4956 | // posted from app A on behalf of app A |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4957 | if (isCallerSameApp(targetPkg, callingUid, userId) |
Julia Reynolds | ecc1b57 | 2018-10-01 16:19:24 -0400 | [diff] [blame] | 4958 | && (TextUtils.equals(callingPkg, targetPkg) |
| 4959 | || isCallerSameApp(callingPkg, callingUid, userId))) { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4960 | return callingUid; |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4961 | } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4962 | |
| 4963 | int targetUid = -1; |
| 4964 | try { |
| 4965 | targetUid = mPackageManagerClient.getPackageUidAsUser(targetPkg, userId); |
| 4966 | } catch (NameNotFoundException e) { |
| 4967 | /* ignore */ |
| 4968 | } |
| 4969 | // posted from app A on behalf of app B |
| 4970 | if (targetUid != -1 && (isCallerAndroid(callingPkg, callingUid) |
| 4971 | || mPreferencesHelper.isDelegateAllowed( |
| 4972 | targetPkg, targetUid, callingPkg, callingUid))) { |
| 4973 | return targetUid; |
| 4974 | } |
| 4975 | |
Julia Reynolds | ecc1b57 | 2018-10-01 16:19:24 -0400 | [diff] [blame] | 4976 | throw new SecurityException("Caller " + callingPkg + ":" + callingUid |
| 4977 | + " cannot post for pkg " + targetPkg + " in user " + userId); |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4978 | } |
| 4979 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4980 | /** |
| 4981 | * Checks if a notification can be posted. checks rate limiter, snooze helper, and blocking. |
| 4982 | * |
| 4983 | * Has side effects. |
| 4984 | */ |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 4985 | private boolean checkDisqualifyingFeatures(int userId, int uid, int id, String tag, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 4986 | NotificationRecord r, boolean isAutogroup) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4987 | final String pkg = r.sbn.getPackageName(); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4988 | final boolean isSystemNotification = |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 4989 | isUidSystemOrPhone(uid) || ("android".equals(pkg)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4990 | final boolean isNotificationFromListener = mListeners.isListenerPackage(pkg); |
| 4991 | |
| 4992 | // Limit the number of notifications that any given package except the android |
| 4993 | // package or a registered listener can enqueue. Prevents DOS attacks and deals with leaks. |
| 4994 | if (!isSystemNotification && !isNotificationFromListener) { |
| 4995 | synchronized (mNotificationLock) { |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 4996 | final int callingUid = Binder.getCallingUid(); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4997 | if (mNotificationsByKey.get(r.sbn.getKey()) == null |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 4998 | && isCallerInstantApp(callingUid, userId)) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 4999 | // Ephemeral apps have some special constraints for notifications. |
| 5000 | // They are not allowed to create new notifications however they are allowed to |
| 5001 | // update notifications created by the system (e.g. a foreground service |
| 5002 | // notification). |
| 5003 | throw new SecurityException("Instant app " + pkg |
| 5004 | + " cannot create notifications"); |
| 5005 | } |
| 5006 | |
| 5007 | // rate limit updates that aren't completed progress notifications |
| 5008 | if (mNotificationsByKey.get(r.sbn.getKey()) != null |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 5009 | && !r.getNotification().hasCompletedProgress() |
| 5010 | && !isAutogroup) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 5011 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 5012 | final float appEnqueueRate = mUsageStats.getAppEnqueueRate(pkg); |
| 5013 | if (appEnqueueRate > mMaxPackageEnqueueRate) { |
| 5014 | mUsageStats.registerOverRateQuota(pkg); |
| 5015 | final long now = SystemClock.elapsedRealtime(); |
| 5016 | if ((now - mLastOverRateLogTime) > MIN_PACKAGE_OVERRATE_LOG_INTERVAL) { |
| 5017 | Slog.e(TAG, "Package enqueue rate is " + appEnqueueRate |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 5018 | + ". Shedding " + r.sbn.getKey() + ". package=" + pkg); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 5019 | mLastOverRateLogTime = now; |
| 5020 | } |
| 5021 | return false; |
| 5022 | } |
| 5023 | } |
| 5024 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 5025 | // limit the number of outstanding notificationrecords an app can have |
| 5026 | int count = getNotificationCountLocked(pkg, userId, id, tag); |
| 5027 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 5028 | mUsageStats.registerOverCountQuota(pkg); |
| 5029 | Slog.e(TAG, "Package has already posted or enqueued " + count |
| 5030 | + " notifications. Not showing more. package=" + pkg); |
| 5031 | return false; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 5032 | } |
| 5033 | } |
| 5034 | } |
| 5035 | |
| 5036 | // snoozed apps |
| 5037 | if (mSnoozeHelper.isSnoozed(userId, pkg, r.getKey())) { |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 5038 | MetricsLogger.action(r.getLogMaker() |
| 5039 | .setType(MetricsProto.MetricsEvent.TYPE_UPDATE) |
| 5040 | .setCategory(MetricsProto.MetricsEvent.NOTIFICATION_SNOOZED)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 5041 | if (DBG) { |
| 5042 | Slog.d(TAG, "Ignored enqueue for snoozed notification " + r.getKey()); |
| 5043 | } |
| 5044 | mSnoozeHelper.update(userId, r); |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 5045 | handleSavePolicyFile(); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 5046 | return false; |
| 5047 | } |
| 5048 | |
| 5049 | |
| 5050 | // blocked apps |
| 5051 | if (isBlocked(r, mUsageStats)) { |
| 5052 | return false; |
| 5053 | } |
| 5054 | |
| 5055 | return true; |
| 5056 | } |
| 5057 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 5058 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 5059 | protected int getNotificationCountLocked(String pkg, int userId, int excludedId, |
| 5060 | String excludedTag) { |
| 5061 | int count = 0; |
| 5062 | final int N = mNotificationList.size(); |
| 5063 | for (int i = 0; i < N; i++) { |
| 5064 | final NotificationRecord existing = mNotificationList.get(i); |
| 5065 | if (existing.sbn.getPackageName().equals(pkg) |
| 5066 | && existing.sbn.getUserId() == userId) { |
| 5067 | if (existing.sbn.getId() == excludedId |
| 5068 | && TextUtils.equals(existing.sbn.getTag(), excludedTag)) { |
| 5069 | continue; |
| 5070 | } |
| 5071 | count++; |
| 5072 | } |
| 5073 | } |
| 5074 | final int M = mEnqueuedNotifications.size(); |
| 5075 | for (int i = 0; i < M; i++) { |
| 5076 | final NotificationRecord existing = mEnqueuedNotifications.get(i); |
| 5077 | if (existing.sbn.getPackageName().equals(pkg) |
| 5078 | && existing.sbn.getUserId() == userId) { |
| 5079 | count++; |
| 5080 | } |
| 5081 | } |
| 5082 | return count; |
| 5083 | } |
| 5084 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 5085 | protected boolean isBlocked(NotificationRecord r, NotificationUsageStats usageStats) { |
Beverly | 3c707b4 | 2018-09-14 09:49:07 -0400 | [diff] [blame] | 5086 | if (isBlocked(r)) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 5087 | Slog.e(TAG, "Suppressing notification from package by user request."); |
| 5088 | usageStats.registerBlocked(r); |
Beverly | 3c707b4 | 2018-09-14 09:49:07 -0400 | [diff] [blame] | 5089 | return true; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 5090 | } |
Beverly | 3c707b4 | 2018-09-14 09:49:07 -0400 | [diff] [blame] | 5091 | return false; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 5092 | } |
| 5093 | |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 5094 | private boolean isBlocked(NotificationRecord r) { |
| 5095 | final String pkg = r.sbn.getPackageName(); |
| 5096 | final int callingUid = r.sbn.getUid(); |
| 5097 | return mPreferencesHelper.isGroupBlocked(pkg, callingUid, r.getChannel().getGroup()) |
| 5098 | || mPreferencesHelper.getImportance(pkg, callingUid) |
| 5099 | == NotificationManager.IMPORTANCE_NONE |
| 5100 | || r.getImportance() == NotificationManager.IMPORTANCE_NONE; |
| 5101 | } |
| 5102 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5103 | protected class SnoozeNotificationRunnable implements Runnable { |
| 5104 | private final String mKey; |
| 5105 | private final long mDuration; |
| 5106 | private final String mSnoozeCriterionId; |
| 5107 | |
| 5108 | SnoozeNotificationRunnable(String key, long duration, String snoozeCriterionId) { |
| 5109 | mKey = key; |
| 5110 | mDuration = duration; |
| 5111 | mSnoozeCriterionId = snoozeCriterionId; |
| 5112 | } |
| 5113 | |
| 5114 | @Override |
| 5115 | public void run() { |
| 5116 | synchronized (mNotificationLock) { |
| 5117 | final NotificationRecord r = findNotificationByKeyLocked(mKey); |
| 5118 | if (r != null) { |
| 5119 | snoozeLocked(r); |
| 5120 | } |
| 5121 | } |
| 5122 | } |
| 5123 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5124 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5125 | void snoozeLocked(NotificationRecord r) { |
| 5126 | if (r.sbn.isGroup()) { |
| 5127 | final List<NotificationRecord> groupNotifications = findGroupNotificationsLocked( |
| 5128 | r.sbn.getPackageName(), r.sbn.getGroupKey(), r.sbn.getUserId()); |
| 5129 | if (r.getNotification().isGroupSummary()) { |
| 5130 | // snooze summary and all children |
| 5131 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 5132 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 5133 | } |
| 5134 | } else { |
| 5135 | // if there is a valid summary for this group, and we are snoozing the only |
| 5136 | // child, also snooze the summary |
| 5137 | if (mSummaryByGroupKey.containsKey(r.sbn.getGroupKey())) { |
| 5138 | if (groupNotifications.size() != 2) { |
| 5139 | snoozeNotificationLocked(r); |
| 5140 | } else { |
| 5141 | // snooze summary and the one child |
| 5142 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 5143 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 5144 | } |
| 5145 | } |
| 5146 | } else { |
| 5147 | snoozeNotificationLocked(r); |
| 5148 | } |
| 5149 | } |
| 5150 | } else { |
| 5151 | // just snooze the one notification |
| 5152 | snoozeNotificationLocked(r); |
| 5153 | } |
| 5154 | } |
| 5155 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5156 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5157 | void snoozeNotificationLocked(NotificationRecord r) { |
| 5158 | MetricsLogger.action(r.getLogMaker() |
| 5159 | .setCategory(MetricsEvent.NOTIFICATION_SNOOZED) |
| 5160 | .setType(MetricsEvent.TYPE_CLOSE) |
Chris Wren | 21a2e72 | 2017-10-02 17:44:53 -0400 | [diff] [blame] | 5161 | .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_SNOOZE_DURATION_MS, |
| 5162 | mDuration) |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5163 | .addTaggedData(MetricsEvent.NOTIFICATION_SNOOZED_CRITERIA, |
| 5164 | mSnoozeCriterionId == null ? 0 : 1)); |
Esteban Talavera | 5d60389 | 2018-11-15 10:55:24 +0000 | [diff] [blame] | 5165 | reportUserInteraction(r); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5166 | boolean wasPosted = removeFromNotificationListsLocked(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5167 | cancelNotificationLocked(r, false, REASON_SNOOZED, wasPosted, null); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5168 | updateLightsLocked(); |
| 5169 | if (mSnoozeCriterionId != null) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5170 | mAssistants.notifyAssistantSnoozedLocked(r.sbn, mSnoozeCriterionId); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5171 | mSnoozeHelper.snooze(r); |
| 5172 | } else { |
| 5173 | mSnoozeHelper.snooze(r, mDuration); |
| 5174 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5175 | r.recordSnoozed(); |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 5176 | handleSavePolicyFile(); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5177 | } |
| 5178 | } |
| 5179 | |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 5180 | protected class CancelNotificationRunnable implements Runnable { |
| 5181 | private final int mCallingUid; |
| 5182 | private final int mCallingPid; |
| 5183 | private final String mPkg; |
| 5184 | private final String mTag; |
| 5185 | private final int mId; |
| 5186 | private final int mMustHaveFlags; |
| 5187 | private final int mMustNotHaveFlags; |
| 5188 | private final boolean mSendDelete; |
| 5189 | private final int mUserId; |
| 5190 | private final int mReason; |
| 5191 | private final int mRank; |
| 5192 | private final int mCount; |
| 5193 | private final ManagedServiceInfo mListener; |
| 5194 | |
| 5195 | CancelNotificationRunnable(final int callingUid, final int callingPid, |
| 5196 | final String pkg, final String tag, final int id, |
| 5197 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
| 5198 | final int userId, final int reason, int rank, int count, |
| 5199 | final ManagedServiceInfo listener) { |
| 5200 | this.mCallingUid = callingUid; |
| 5201 | this.mCallingPid = callingPid; |
| 5202 | this.mPkg = pkg; |
| 5203 | this.mTag = tag; |
| 5204 | this.mId = id; |
| 5205 | this.mMustHaveFlags = mustHaveFlags; |
| 5206 | this.mMustNotHaveFlags = mustNotHaveFlags; |
| 5207 | this.mSendDelete = sendDelete; |
| 5208 | this.mUserId = userId; |
| 5209 | this.mReason = reason; |
| 5210 | this.mRank = rank; |
| 5211 | this.mCount = count; |
| 5212 | this.mListener = listener; |
| 5213 | } |
| 5214 | |
| 5215 | @Override |
| 5216 | public void run() { |
| 5217 | String listenerName = mListener == null ? null : mListener.component.toShortString(); |
| 5218 | if (DBG) { |
| 5219 | EventLogTags.writeNotificationCancel(mCallingUid, mCallingPid, mPkg, mId, mTag, |
| 5220 | mUserId, mMustHaveFlags, mMustNotHaveFlags, mReason, listenerName); |
| 5221 | } |
| 5222 | |
| 5223 | synchronized (mNotificationLock) { |
| 5224 | // Look for the notification, searching both the posted and enqueued lists. |
| 5225 | NotificationRecord r = findNotificationLocked(mPkg, mTag, mId, mUserId); |
| 5226 | if (r != null) { |
| 5227 | // The notification was found, check if it should be removed. |
| 5228 | |
| 5229 | // Ideally we'd do this in the caller of this method. However, that would |
| 5230 | // require the caller to also find the notification. |
| 5231 | if (mReason == REASON_CLICK) { |
| 5232 | mUsageStats.registerClickedByUser(r); |
| 5233 | } |
| 5234 | |
| 5235 | if ((r.getNotification().flags & mMustHaveFlags) != mMustHaveFlags) { |
| 5236 | return; |
| 5237 | } |
| 5238 | if ((r.getNotification().flags & mMustNotHaveFlags) != 0) { |
| 5239 | return; |
| 5240 | } |
| 5241 | |
| 5242 | // Cancel the notification. |
| 5243 | boolean wasPosted = removeFromNotificationListsLocked(r); |
| 5244 | cancelNotificationLocked( |
| 5245 | r, mSendDelete, mReason, mRank, mCount, wasPosted, listenerName); |
| 5246 | cancelGroupChildrenLocked(r, mCallingUid, mCallingPid, listenerName, |
| 5247 | mSendDelete, null); |
| 5248 | updateLightsLocked(); |
| 5249 | } else { |
| 5250 | // No notification was found, assume that it is snoozed and cancel it. |
| 5251 | if (mReason != REASON_SNOOZED) { |
| 5252 | final boolean wasSnoozed = mSnoozeHelper.cancel(mUserId, mPkg, mTag, mId); |
| 5253 | if (wasSnoozed) { |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 5254 | handleSavePolicyFile(); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 5255 | } |
| 5256 | } |
| 5257 | } |
| 5258 | } |
| 5259 | } |
| 5260 | } |
| 5261 | |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 5262 | protected class EnqueueNotificationRunnable implements Runnable { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5263 | private final NotificationRecord r; |
| 5264 | private final int userId; |
| 5265 | |
| 5266 | EnqueueNotificationRunnable(int userId, NotificationRecord r) { |
| 5267 | this.userId = userId; |
| 5268 | this.r = r; |
| 5269 | }; |
| 5270 | |
| 5271 | @Override |
| 5272 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5273 | synchronized (mNotificationLock) { |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 5274 | mEnqueuedNotifications.add(r); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 5275 | scheduleTimeoutLocked(r); |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 5276 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 5277 | final StatusBarNotification n = r.sbn; |
| 5278 | if (DBG) Slog.d(TAG, "EnqueueNotificationRunnable.run for: " + n.getKey()); |
| 5279 | NotificationRecord old = mNotificationsByKey.get(n.getKey()); |
| 5280 | if (old != null) { |
| 5281 | // Retain ranking information from previous record |
| 5282 | r.copyRankingInformation(old); |
| 5283 | } |
| 5284 | |
| 5285 | final int callingUid = n.getUid(); |
| 5286 | final int callingPid = n.getInitialPid(); |
| 5287 | final Notification notification = n.getNotification(); |
| 5288 | final String pkg = n.getPackageName(); |
| 5289 | final int id = n.getId(); |
| 5290 | final String tag = n.getTag(); |
| 5291 | |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5292 | // We need to fix the notification up a little for bubbles |
Mady Mellor | be79796 | 2019-04-01 16:04:24 -0700 | [diff] [blame] | 5293 | flagNotificationForBubbles(r, pkg, callingUid, old); |
Mady Mellor | 7eb18ef | 2019-03-27 14:03:46 -0700 | [diff] [blame] | 5294 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 5295 | // Handle grouped notifications and bail out early if we |
| 5296 | // can to avoid extracting signals. |
| 5297 | handleGroupedNotificationLocked(r, old, callingUid, callingPid); |
| 5298 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5299 | // if this is a group child, unsnooze parent summary |
| 5300 | if (n.isGroup() && notification.isGroupChild()) { |
| 5301 | mSnoozeHelper.repostGroupSummary(pkg, r.getUserId(), n.getGroupKey()); |
| 5302 | } |
| 5303 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 5304 | // This conditional is a dirty hack to limit the logging done on |
| 5305 | // behalf of the download manager without affecting other apps. |
| 5306 | if (!pkg.equals("com.android.providers.downloads") |
| 5307 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| 5308 | int enqueueStatus = EVENTLOG_ENQUEUE_STATUS_NEW; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 5309 | if (old != null) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 5310 | enqueueStatus = EVENTLOG_ENQUEUE_STATUS_UPDATE; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 5311 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 5312 | EventLogTags.writeNotificationEnqueue(callingUid, callingPid, |
| 5313 | pkg, id, tag, userId, notification.toString(), |
| 5314 | enqueueStatus); |
| 5315 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 5316 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 5317 | // tell the assistant service about the notification |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5318 | if (mAssistants.isEnabled()) { |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 5319 | mAssistants.onNotificationEnqueuedLocked(r); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 5320 | mHandler.postDelayed(new PostNotificationRunnable(r.getKey()), |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 5321 | DELAY_FOR_ASSISTANT_TIME); |
| 5322 | } else { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 5323 | mHandler.post(new PostNotificationRunnable(r.getKey())); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 5324 | } |
| 5325 | } |
| 5326 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 5327 | } |
| 5328 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 5329 | @GuardedBy("mNotificationLock") |
| 5330 | private boolean isPackageSuspendedLocked(NotificationRecord r) { |
| 5331 | final String pkg = r.sbn.getPackageName(); |
| 5332 | final int callingUid = r.sbn.getUid(); |
| 5333 | |
| 5334 | return isPackageSuspendedForUser(pkg, callingUid); |
| 5335 | } |
| 5336 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 5337 | protected class PostNotificationRunnable implements Runnable { |
| 5338 | private final String key; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 5339 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 5340 | PostNotificationRunnable(String key) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 5341 | this.key = key; |
| 5342 | } |
| 5343 | |
| 5344 | @Override |
| 5345 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5346 | synchronized (mNotificationLock) { |
| 5347 | try { |
| 5348 | NotificationRecord r = null; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 5349 | int N = mEnqueuedNotifications.size(); |
| 5350 | for (int i = 0; i < N; i++) { |
| 5351 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 5352 | if (Objects.equals(key, enqueued.getKey())) { |
| 5353 | r = enqueued; |
| 5354 | break; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 5355 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 5356 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5357 | if (r == null) { |
| 5358 | Slog.i(TAG, "Cannot find enqueued record for key: " + key); |
| 5359 | return; |
| 5360 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 5361 | |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 5362 | if (isBlocked(r)) { |
| 5363 | Slog.i(TAG, "notification blocked by assistant request"); |
| 5364 | return; |
| 5365 | } |
| 5366 | |
Beverly | 3c707b4 | 2018-09-14 09:49:07 -0400 | [diff] [blame] | 5367 | final boolean isPackageSuspended = isPackageSuspendedLocked(r); |
| 5368 | r.setHidden(isPackageSuspended); |
| 5369 | if (isPackageSuspended) { |
| 5370 | mUsageStats.registerSuspendedByAdmin(r); |
| 5371 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 5372 | NotificationRecord old = mNotificationsByKey.get(key); |
| 5373 | final StatusBarNotification n = r.sbn; |
| 5374 | final Notification notification = n.getNotification(); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 5375 | int index = indexOfNotificationLocked(n.getKey()); |
| 5376 | if (index < 0) { |
| 5377 | mNotificationList.add(r); |
| 5378 | mUsageStats.registerPostedByApp(r); |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 5379 | r.setInterruptive(isVisuallyInterruptive(null, r)); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 5380 | } else { |
| 5381 | old = mNotificationList.get(index); |
| 5382 | mNotificationList.set(index, r); |
| 5383 | mUsageStats.registerUpdatedByApp(r, old); |
| 5384 | // Make sure we don't lose the foreground service state. |
| 5385 | notification.flags |= |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 5386 | old.getNotification().flags & FLAG_FOREGROUND_SERVICE; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 5387 | r.isUpdate = true; |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 5388 | r.setTextChanged(isVisuallyInterruptive(old, r)); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 5389 | } |
| 5390 | |
| 5391 | mNotificationsByKey.put(n.getKey(), r); |
| 5392 | |
| 5393 | // Ensure if this is a foreground service that the proper additional |
| 5394 | // flags are set. |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 5395 | if ((notification.flags & FLAG_FOREGROUND_SERVICE) != 0) { |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 5396 | notification.flags |= FLAG_ONGOING_EVENT |
| 5397 | | FLAG_NO_CLEAR; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 5398 | } |
| 5399 | |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 5400 | mRankingHelper.extractSignals(r); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 5401 | mRankingHelper.sort(mNotificationList); |
| 5402 | |
Gus Prevas | a322649 | 2018-10-23 11:10:09 -0400 | [diff] [blame] | 5403 | if (!r.isHidden()) { |
| 5404 | buzzBeepBlinkLocked(r); |
| 5405 | } |
| 5406 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 5407 | if (notification.getSmallIcon() != null) { |
| 5408 | StatusBarNotification oldSbn = (old != null) ? old.sbn : null; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 5409 | mListeners.notifyPostedLocked(r, old); |
Brad Stenning | d2e7a97 | 2018-10-01 09:08:42 -0700 | [diff] [blame] | 5410 | if ((oldSbn == null || !Objects.equals(oldSbn.getGroup(), n.getGroup())) |
| 5411 | && !isCritical(r)) { |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 5412 | mHandler.post(new Runnable() { |
| 5413 | @Override |
| 5414 | public void run() { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 5415 | mGroupHelper.onNotificationPosted( |
| 5416 | n, hasAutoGroupSummaryLocked(n)); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 5417 | } |
| 5418 | }); |
| 5419 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 5420 | } else { |
| 5421 | Slog.e(TAG, "Not posting notification without small icon: " + notification); |
| 5422 | if (old != null && !old.isCanceled) { |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 5423 | mListeners.notifyRemovedLocked(r, |
Julia Reynolds | 3207e2f | 2018-12-20 09:39:53 -0500 | [diff] [blame] | 5424 | NotificationListenerService.REASON_ERROR, r.getStats()); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 5425 | mHandler.post(new Runnable() { |
| 5426 | @Override |
| 5427 | public void run() { |
| 5428 | mGroupHelper.onNotificationRemoved(n); |
| 5429 | } |
| 5430 | }); |
| 5431 | } |
| 5432 | // ATTENTION: in a future release we will bail out here |
| 5433 | // so that we do not play sounds, show lights, etc. for invalid |
| 5434 | // notifications |
| 5435 | Slog.e(TAG, "WARNING: In a future release this will crash the app: " |
| 5436 | + n.getPackageName()); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5437 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5438 | |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 5439 | maybeRecordInterruptionLocked(r); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5440 | } finally { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 5441 | int N = mEnqueuedNotifications.size(); |
| 5442 | for (int i = 0; i < N; i++) { |
| 5443 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 5444 | if (Objects.equals(key, enqueued.getKey())) { |
| 5445 | mEnqueuedNotifications.remove(i); |
| 5446 | break; |
| 5447 | } |
| 5448 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 5449 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5450 | } |
| 5451 | } |
| 5452 | } |
| 5453 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5454 | /** |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 5455 | * If the notification differs enough visually, consider it a new interruptive notification. |
| 5456 | */ |
| 5457 | @GuardedBy("mNotificationLock") |
| 5458 | @VisibleForTesting |
| 5459 | protected boolean isVisuallyInterruptive(NotificationRecord old, NotificationRecord r) { |
Julia Reynolds | 760fa76 | 2018-06-19 15:39:23 -0400 | [diff] [blame] | 5460 | // Ignore summary updates because we don't display most of the information. |
| 5461 | if (r.sbn.isGroup() && r.sbn.getNotification().isGroupSummary()) { |
| 5462 | if (DEBUG_INTERRUPTIVENESS) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5463 | Slog.v(TAG, "INTERRUPTIVENESS: " |
Julia Reynolds | 760fa76 | 2018-06-19 15:39:23 -0400 | [diff] [blame] | 5464 | + r.getKey() + " is not interruptive: summary"); |
| 5465 | } |
| 5466 | return false; |
| 5467 | } |
| 5468 | |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5469 | if (old == null) { |
| 5470 | if (DEBUG_INTERRUPTIVENESS) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5471 | Slog.v(TAG, "INTERRUPTIVENESS: " |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5472 | + r.getKey() + " is interruptive: new notification"); |
| 5473 | } |
| 5474 | return true; |
| 5475 | } |
| 5476 | |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 5477 | if (r == null) { |
| 5478 | if (DEBUG_INTERRUPTIVENESS) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5479 | Slog.v(TAG, "INTERRUPTIVENESS: " |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 5480 | + r.getKey() + " is not interruptive: null"); |
| 5481 | } |
| 5482 | return false; |
| 5483 | } |
| 5484 | |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 5485 | Notification oldN = old.sbn.getNotification(); |
| 5486 | Notification newN = r.sbn.getNotification(); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5487 | |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 5488 | if (oldN.extras == null || newN.extras == null) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5489 | if (DEBUG_INTERRUPTIVENESS) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5490 | Slog.v(TAG, "INTERRUPTIVENESS: " |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5491 | + r.getKey() + " is not interruptive: no extras"); |
| 5492 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 5493 | return false; |
| 5494 | } |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 5495 | |
| 5496 | // Ignore visual interruptions from foreground services because users |
| 5497 | // consider them one 'session'. Count them for everything else. |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 5498 | if ((r.sbn.getNotification().flags & FLAG_FOREGROUND_SERVICE) != 0) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5499 | if (DEBUG_INTERRUPTIVENESS) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5500 | Slog.v(TAG, "INTERRUPTIVENESS: " |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5501 | + r.getKey() + " is not interruptive: foreground service"); |
| 5502 | } |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 5503 | return false; |
| 5504 | } |
| 5505 | |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5506 | final String oldTitle = String.valueOf(oldN.extras.get(Notification.EXTRA_TITLE)); |
| 5507 | final String newTitle = String.valueOf(newN.extras.get(Notification.EXTRA_TITLE)); |
| 5508 | if (!Objects.equals(oldTitle, newTitle)) { |
| 5509 | if (DEBUG_INTERRUPTIVENESS) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5510 | Slog.v(TAG, "INTERRUPTIVENESS: " |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5511 | + r.getKey() + " is interruptive: changed title"); |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5512 | Slog.v(TAG, "INTERRUPTIVENESS: " + String.format(" old title: %s (%s@0x%08x)", |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5513 | oldTitle, oldTitle.getClass(), oldTitle.hashCode())); |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5514 | Slog.v(TAG, "INTERRUPTIVENESS: " + String.format(" new title: %s (%s@0x%08x)", |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5515 | newTitle, newTitle.getClass(), newTitle.hashCode())); |
| 5516 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 5517 | return true; |
| 5518 | } |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5519 | // Do not compare Spannables (will always return false); compare unstyled Strings |
| 5520 | final String oldText = String.valueOf(oldN.extras.get(Notification.EXTRA_TEXT)); |
| 5521 | final String newText = String.valueOf(newN.extras.get(Notification.EXTRA_TEXT)); |
| 5522 | if (!Objects.equals(oldText, newText)) { |
| 5523 | if (DEBUG_INTERRUPTIVENESS) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5524 | Slog.v(TAG, "INTERRUPTIVENESS: " |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5525 | + r.getKey() + " is interruptive: changed text"); |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5526 | Slog.v(TAG, "INTERRUPTIVENESS: " + String.format(" old text: %s (%s@0x%08x)", |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5527 | oldText, oldText.getClass(), oldText.hashCode())); |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5528 | Slog.v(TAG, "INTERRUPTIVENESS: " + String.format(" new text: %s (%s@0x%08x)", |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5529 | newText, newText.getClass(), newText.hashCode())); |
| 5530 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 5531 | return true; |
| 5532 | } |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5533 | if (oldN.hasCompletedProgress() != newN.hasCompletedProgress()) { |
| 5534 | if (DEBUG_INTERRUPTIVENESS) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5535 | Slog.v(TAG, "INTERRUPTIVENESS: " |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5536 | + r.getKey() + " is interruptive: completed progress"); |
| 5537 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 5538 | return true; |
| 5539 | } |
| 5540 | // Actions |
| 5541 | if (Notification.areActionsVisiblyDifferent(oldN, newN)) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5542 | if (DEBUG_INTERRUPTIVENESS) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5543 | Slog.v(TAG, "INTERRUPTIVENESS: " |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5544 | + r.getKey() + " is interruptive: changed actions"); |
| 5545 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 5546 | return true; |
| 5547 | } |
| 5548 | |
| 5549 | try { |
| 5550 | Notification.Builder oldB = Notification.Builder.recoverBuilder(getContext(), oldN); |
| 5551 | Notification.Builder newB = Notification.Builder.recoverBuilder(getContext(), newN); |
| 5552 | |
| 5553 | // Style based comparisons |
| 5554 | if (Notification.areStyledNotificationsVisiblyDifferent(oldB, newB)) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5555 | if (DEBUG_INTERRUPTIVENESS) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5556 | Slog.v(TAG, "INTERRUPTIVENESS: " |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5557 | + r.getKey() + " is interruptive: styles differ"); |
| 5558 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 5559 | return true; |
| 5560 | } |
| 5561 | |
| 5562 | // Remote views |
| 5563 | if (Notification.areRemoteViewsChanged(oldB, newB)) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5564 | if (DEBUG_INTERRUPTIVENESS) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5565 | Slog.v(TAG, "INTERRUPTIVENESS: " |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5566 | + r.getKey() + " is interruptive: remoteviews differ"); |
| 5567 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 5568 | return true; |
| 5569 | } |
| 5570 | } catch (Exception e) { |
| 5571 | Slog.w(TAG, "error recovering builder", e); |
| 5572 | } |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 5573 | |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 5574 | return false; |
| 5575 | } |
| 5576 | |
| 5577 | /** |
Brad Stenning | d2e7a97 | 2018-10-01 09:08:42 -0700 | [diff] [blame] | 5578 | * Check if the notification is classified as critical. |
| 5579 | * |
| 5580 | * @param record the record to test for criticality |
| 5581 | * @return {@code true} if notification is considered critical |
| 5582 | * |
| 5583 | * @see CriticalNotificationExtractor for criteria |
| 5584 | */ |
| 5585 | private boolean isCritical(NotificationRecord record) { |
| 5586 | // 0 is the most critical |
| 5587 | return record.getCriticality() < CriticalNotificationExtractor.NORMAL; |
| 5588 | } |
| 5589 | |
| 5590 | /** |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5591 | * Ensures that grouped notification receive their special treatment. |
| 5592 | * |
| 5593 | * <p>Cancels group children if the new notification causes a group to lose |
| 5594 | * its summary.</p> |
| 5595 | * |
| 5596 | * <p>Updates mSummaryByGroupKey.</p> |
| 5597 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5598 | @GuardedBy("mNotificationLock") |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5599 | private void handleGroupedNotificationLocked(NotificationRecord r, NotificationRecord old, |
| 5600 | int callingUid, int callingPid) { |
| 5601 | StatusBarNotification sbn = r.sbn; |
| 5602 | Notification n = sbn.getNotification(); |
Selim Cinek | 5b03ce9 | 2016-05-18 15:16:58 -0700 | [diff] [blame] | 5603 | if (n.isGroupSummary() && !sbn.isAppGroup()) { |
| 5604 | // notifications without a group shouldn't be a summary, otherwise autobundling can |
| 5605 | // lead to bugs |
| 5606 | n.flags &= ~Notification.FLAG_GROUP_SUMMARY; |
| 5607 | } |
| 5608 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5609 | String group = sbn.getGroupKey(); |
| 5610 | boolean isSummary = n.isGroupSummary(); |
| 5611 | |
| 5612 | Notification oldN = old != null ? old.sbn.getNotification() : null; |
| 5613 | String oldGroup = old != null ? old.sbn.getGroupKey() : null; |
| 5614 | boolean oldIsSummary = old != null && oldN.isGroupSummary(); |
| 5615 | |
| 5616 | if (oldIsSummary) { |
| 5617 | NotificationRecord removedSummary = mSummaryByGroupKey.remove(oldGroup); |
| 5618 | if (removedSummary != old) { |
| 5619 | String removedKey = |
| 5620 | removedSummary != null ? removedSummary.getKey() : "<null>"; |
| 5621 | Slog.w(TAG, "Removed summary didn't match old notification: old=" + old.getKey() + |
| 5622 | ", removed=" + removedKey); |
| 5623 | } |
| 5624 | } |
| 5625 | if (isSummary) { |
| 5626 | mSummaryByGroupKey.put(group, r); |
| 5627 | } |
| 5628 | |
| 5629 | // Clear out group children of the old notification if the update |
| 5630 | // causes the group summary to go away. This happens when the old |
| 5631 | // notification was a summary and the new one isn't, or when the old |
| 5632 | // notification was a summary and its group key changed. |
| 5633 | if (oldIsSummary && (!isSummary || !oldGroup.equals(group))) { |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5634 | cancelGroupChildrenLocked(old, callingUid, callingPid, null, false /* sendDelete */, |
| 5635 | null); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5636 | } |
| 5637 | } |
| 5638 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5639 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5640 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 5641 | void scheduleTimeoutLocked(NotificationRecord record) { |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 5642 | if (record.getNotification().getTimeoutAfter() > 0) { |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 5643 | final PendingIntent pi = PendingIntent.getBroadcast(getContext(), |
| 5644 | REQUEST_CODE_TIMEOUT, |
| 5645 | new Intent(ACTION_NOTIFICATION_TIMEOUT) |
| 5646 | .setData(new Uri.Builder().scheme(SCHEME_TIMEOUT) |
| 5647 | .appendPath(record.getKey()).build()) |
| 5648 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 5649 | .putExtra(EXTRA_KEY, record.getKey()), |
| 5650 | PendingIntent.FLAG_UPDATE_CURRENT); |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 5651 | mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 5652 | SystemClock.elapsedRealtime() + record.getNotification().getTimeoutAfter(), pi); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 5653 | } |
| 5654 | } |
| 5655 | |
| 5656 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5657 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5658 | void buzzBeepBlinkLocked(NotificationRecord record) { |
Qiao (Adora) Zhang | 47b553c | 2019-04-15 20:42:57 +0000 | [diff] [blame] | 5659 | if (mIsAutomotive && !mNotificationEffectsEnabledForAutomotive) { |
| 5660 | return; |
| 5661 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 5662 | boolean buzz = false; |
| 5663 | boolean beep = false; |
| 5664 | boolean blink = false; |
| 5665 | |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5666 | final Notification notification = record.sbn.getNotification(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5667 | final String key = record.getKey(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5668 | |
| 5669 | // Should this notification make noise, vibe, or use the LED? |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 5670 | final boolean aboveThreshold = |
Adora Zhang | 963328f | 2018-11-15 18:17:19 -0800 | [diff] [blame] | 5671 | mIsAutomotive |
| 5672 | ? record.getImportance() > NotificationManager.IMPORTANCE_DEFAULT |
| 5673 | : record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5674 | |
| 5675 | // Remember if this notification already owns the notification channels. |
| 5676 | boolean wasBeep = key != null && key.equals(mSoundNotificationKey); |
| 5677 | boolean wasBuzz = key != null && key.equals(mVibrateNotificationKey); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5678 | // These are set inside the conditional if the notification is allowed to make noise. |
| 5679 | boolean hasValidVibrate = false; |
| 5680 | boolean hasValidSound = false; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 5681 | boolean sentAccessibilityEvent = false; |
| 5682 | // If the notification will appear in the status bar, it should send an accessibility |
| 5683 | // event |
| 5684 | if (!record.isUpdate && record.getImportance() > IMPORTANCE_MIN) { |
| 5685 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
| 5686 | sentAccessibilityEvent = true; |
| 5687 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5688 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5689 | if (aboveThreshold && isNotificationForCurrentUser(record)) { |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 5690 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5691 | if (mSystemReady && mAudioManager != null) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5692 | Uri soundUri = record.getSound(); |
| 5693 | hasValidSound = soundUri != null && !Uri.EMPTY.equals(soundUri); |
| 5694 | long[] vibration = record.getVibration(); |
| 5695 | // Demote sound to vibration if vibration missing & phone in vibration mode. |
| 5696 | if (vibration == null |
| 5697 | && hasValidSound |
| 5698 | && (mAudioManager.getRingerModeInternal() |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 5699 | == AudioManager.RINGER_MODE_VIBRATE) |
| 5700 | && mAudioManager.getStreamVolume( |
| 5701 | AudioAttributes.toLegacyStreamType(record.getAudioAttributes())) == 0) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5702 | vibration = mFallbackVibrationPattern; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5703 | } |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5704 | hasValidVibrate = vibration != null; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 5705 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5706 | boolean hasAudibleAlert = hasValidSound || hasValidVibrate; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5707 | if (hasAudibleAlert && !shouldMuteNotificationLocked(record)) { |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 5708 | if (!sentAccessibilityEvent) { |
| 5709 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
| 5710 | sentAccessibilityEvent = true; |
| 5711 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5712 | if (DBG) Slog.v(TAG, "Interrupting!"); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5713 | if (hasValidSound) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5714 | if (mInCall) { |
| 5715 | playInCallNotification(); |
| 5716 | beep = true; |
| 5717 | } else { |
| 5718 | beep = playSound(record, soundUri); |
| 5719 | } |
Seungho Lee | 55102a8 | 2018-12-09 19:27:36 +0900 | [diff] [blame] | 5720 | if(beep) { |
| 5721 | mSoundNotificationKey = key; |
| 5722 | } |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5723 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5724 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5725 | final boolean ringerModeSilent = |
| 5726 | mAudioManager.getRingerModeInternal() |
| 5727 | == AudioManager.RINGER_MODE_SILENT; |
| 5728 | if (!mInCall && hasValidVibrate && !ringerModeSilent) { |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 5729 | buzz = playVibration(record, vibration, hasValidSound); |
Seungho Lee | 55102a8 | 2018-12-09 19:27:36 +0900 | [diff] [blame] | 5730 | if(buzz) { |
| 5731 | mVibrateNotificationKey = key; |
| 5732 | } |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5733 | } |
Tyler Gunn | 48f8627 | 2018-07-03 12:38:49 -0700 | [diff] [blame] | 5734 | } else if ((record.getFlags() & Notification.FLAG_INSISTENT) != 0) { |
| 5735 | hasValidSound = false; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5736 | } |
| 5737 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5738 | } |
| 5739 | // If a notification is updated to remove the actively playing sound or vibrate, |
| 5740 | // cancel that feedback now |
| 5741 | if (wasBeep && !hasValidSound) { |
| 5742 | clearSoundLocked(); |
| 5743 | } |
| 5744 | if (wasBuzz && !hasValidVibrate) { |
| 5745 | clearVibrateLocked(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5746 | } |
| 5747 | |
| 5748 | // light |
| 5749 | // release the light |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5750 | boolean wasShowLights = mLights.remove(key); |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 5751 | if (canShowLightsLocked(record, aboveThreshold)) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5752 | mLights.add(key); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5753 | updateLightsLocked(); |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 5754 | if (mUseAttentionLight) { |
| 5755 | mAttentionLight.pulse(); |
| 5756 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 5757 | blink = true; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5758 | } else if (wasShowLights) { |
| 5759 | updateLightsLocked(); |
| 5760 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 5761 | if (buzz || beep || blink) { |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 5762 | // Ignore summary updates because we don't display most of the information. |
| 5763 | if (record.sbn.isGroup() && record.sbn.getNotification().isGroupSummary()) { |
| 5764 | if (DEBUG_INTERRUPTIVENESS) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5765 | Slog.v(TAG, "INTERRUPTIVENESS: " |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 5766 | + record.getKey() + " is not interruptive: summary"); |
| 5767 | } |
| 5768 | } else { |
| 5769 | if (DEBUG_INTERRUPTIVENESS) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 5770 | Slog.v(TAG, "INTERRUPTIVENESS: " |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 5771 | + record.getKey() + " is interruptive: alerted"); |
| 5772 | } |
| 5773 | record.setInterruptive(true); |
| 5774 | } |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 5775 | MetricsLogger.action(record.getLogMaker() |
| 5776 | .setCategory(MetricsEvent.NOTIFICATION_ALERT) |
| 5777 | .setType(MetricsEvent.TYPE_OPEN) |
| 5778 | .setSubtype((buzz ? 1 : 0) | (beep ? 2 : 0) | (blink ? 4 : 0))); |
| 5779 | EventLogTags.writeNotificationAlert(key, buzz ? 1 : 0, beep ? 1 : 0, blink ? 1 : 0); |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 5780 | } |
Gus Prevas | a322649 | 2018-10-23 11:10:09 -0400 | [diff] [blame] | 5781 | record.setAudiblyAlerted(buzz || beep); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5782 | } |
| 5783 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5784 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 5785 | boolean canShowLightsLocked(final NotificationRecord record, boolean aboveThreshold) { |
| 5786 | // device lacks light |
| 5787 | if (!mHasLight) { |
| 5788 | return false; |
| 5789 | } |
| 5790 | // user turned lights off globally |
| 5791 | if (!mNotificationPulseEnabled) { |
| 5792 | return false; |
| 5793 | } |
| 5794 | // the notification/channel has no light |
| 5795 | if (record.getLight() == null) { |
| 5796 | return false; |
| 5797 | } |
| 5798 | // unimportant notification |
| 5799 | if (!aboveThreshold) { |
| 5800 | return false; |
| 5801 | } |
| 5802 | // suppressed due to DND |
| 5803 | if ((record.getSuppressedVisualEffects() & SUPPRESSED_EFFECT_LIGHTS) != 0) { |
| 5804 | return false; |
| 5805 | } |
| 5806 | // Suppressed because it's a silent update |
| 5807 | final Notification notification = record.getNotification(); |
Mady Mellor | 66efd5e | 2019-05-15 13:38:11 -0700 | [diff] [blame] | 5808 | if (record.isUpdate && (notification.flags & FLAG_ONLY_ALERT_ONCE) != 0) { |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 5809 | return false; |
| 5810 | } |
| 5811 | // Suppressed because another notification in its group handles alerting |
| 5812 | if (record.sbn.isGroup() && record.getNotification().suppressAlertingDueToGrouping()) { |
| 5813 | return false; |
| 5814 | } |
| 5815 | // not if in call or the screen's on |
| 5816 | if (mInCall || mScreenOn) { |
| 5817 | return false; |
| 5818 | } |
| 5819 | |
| 5820 | return true; |
| 5821 | } |
| 5822 | |
| 5823 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 5824 | boolean shouldMuteNotificationLocked(final NotificationRecord record) { |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5825 | // Suppressed because it's a silent update |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 5826 | final Notification notification = record.getNotification(); |
Mady Mellor | 66efd5e | 2019-05-15 13:38:11 -0700 | [diff] [blame] | 5827 | if (record.isUpdate && (notification.flags & FLAG_ONLY_ALERT_ONCE) != 0) { |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 5828 | return true; |
| 5829 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5830 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5831 | // muted by listener |
| 5832 | final String disableEffects = disableNotificationEffects(record); |
| 5833 | if (disableEffects != null) { |
| 5834 | ZenLog.traceDisableEffects(record, disableEffects); |
| 5835 | return true; |
| 5836 | } |
| 5837 | |
| 5838 | // suppressed due to DND |
| 5839 | if (record.isIntercepted()) { |
| 5840 | return true; |
| 5841 | } |
| 5842 | |
| 5843 | // Suppressed because another notification in its group handles alerting |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 5844 | if (record.sbn.isGroup()) { |
Julia Reynolds | 79dfdd6 | 2018-04-17 15:36:33 -0400 | [diff] [blame] | 5845 | if (notification.suppressAlertingDueToGrouping()) { |
| 5846 | return true; |
| 5847 | } |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 5848 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5849 | |
Julia Reynolds | 65b85cf | 2017-07-20 09:19:20 -0400 | [diff] [blame] | 5850 | // Suppressed for being too recently noisy |
| 5851 | final String pkg = record.sbn.getPackageName(); |
| 5852 | if (mUsageStats.isAlertRateLimited(pkg)) { |
| 5853 | Slog.e(TAG, "Muting recently noisy " + record.getKey()); |
| 5854 | return true; |
| 5855 | } |
| 5856 | |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 5857 | return false; |
| 5858 | } |
| 5859 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 5860 | private boolean playSound(final NotificationRecord record, Uri soundUri) { |
| 5861 | boolean looping = (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
Jean-Michel Trivi | ec2bb18 | 2018-03-23 18:04:00 -0700 | [diff] [blame] | 5862 | // play notifications if there is no user of exclusive audio focus |
| 5863 | // and the stream volume is not 0 (non-zero volume implies not silenced by SILENT or |
| 5864 | // VIBRATE ringer mode) |
| 5865 | if (!mAudioManager.isAudioFocusExclusive() |
| 5866 | && (mAudioManager.getStreamVolume( |
| 5867 | AudioAttributes.toLegacyStreamType(record.getAudioAttributes())) != 0)) { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 5868 | final long identity = Binder.clearCallingIdentity(); |
| 5869 | try { |
| 5870 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 5871 | if (player != null) { |
| 5872 | if (DBG) Slog.v(TAG, "Playing sound " + soundUri |
| 5873 | + " with attributes " + record.getAudioAttributes()); |
| 5874 | player.playAsync(soundUri, record.sbn.getUser(), looping, |
| 5875 | record.getAudioAttributes()); |
| 5876 | return true; |
| 5877 | } |
| 5878 | } catch (RemoteException e) { |
| 5879 | } finally { |
| 5880 | Binder.restoreCallingIdentity(identity); |
| 5881 | } |
| 5882 | } |
| 5883 | return false; |
| 5884 | } |
| 5885 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 5886 | private boolean playVibration(final NotificationRecord record, long[] vibration, |
| 5887 | boolean delayVibForSound) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 5888 | // Escalate privileges so we can use the vibrator even if the |
| 5889 | // notifying app does not have the VIBRATE permission. |
| 5890 | long identity = Binder.clearCallingIdentity(); |
| 5891 | try { |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 5892 | final VibrationEffect effect; |
| 5893 | try { |
| 5894 | final boolean insistent = |
| 5895 | (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 5896 | effect = VibrationEffect.createWaveform( |
| 5897 | vibration, insistent ? 0 : -1 /*repeatIndex*/); |
| 5898 | } catch (IllegalArgumentException e) { |
| 5899 | Slog.e(TAG, "Error creating vibration waveform with pattern: " + |
| 5900 | Arrays.toString(vibration)); |
| 5901 | return false; |
| 5902 | } |
| 5903 | if (delayVibForSound) { |
| 5904 | new Thread(() -> { |
| 5905 | // delay the vibration by the same amount as the notification sound |
| 5906 | final int waitMs = mAudioManager.getFocusRampTimeMs( |
| 5907 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 5908 | record.getAudioAttributes()); |
| 5909 | if (DBG) Slog.v(TAG, "Delaying vibration by " + waitMs + "ms"); |
| 5910 | try { |
| 5911 | Thread.sleep(waitMs); |
| 5912 | } catch (InterruptedException e) { } |
Seungho Lee | de93388 | 2018-10-31 21:49:09 +0900 | [diff] [blame] | 5913 | |
| 5914 | // Notifications might be canceled before it actually vibrates due to waitMs, |
| 5915 | // so need to check the notification still valide for vibrate. |
| 5916 | synchronized (mNotificationLock) { |
| 5917 | if (mNotificationsByKey.get(record.getKey()) != null) { |
| 5918 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 5919 | effect, "Notification (delayed)", record.getAudioAttributes()); |
| 5920 | } else { |
| 5921 | Slog.e(TAG, "No vibration for canceled notification : " + record.getKey()); |
| 5922 | } |
| 5923 | } |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 5924 | }).start(); |
| 5925 | } else { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 5926 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getPackageName(), |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 5927 | effect, "Notification", record.getAudioAttributes()); |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 5928 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 5929 | return true; |
| 5930 | } finally{ |
| 5931 | Binder.restoreCallingIdentity(identity); |
| 5932 | } |
| 5933 | } |
| 5934 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5935 | private boolean isNotificationForCurrentUser(NotificationRecord record) { |
| 5936 | final int currentUser; |
| 5937 | final long token = Binder.clearCallingIdentity(); |
| 5938 | try { |
| 5939 | currentUser = ActivityManager.getCurrentUser(); |
| 5940 | } finally { |
| 5941 | Binder.restoreCallingIdentity(token); |
| 5942 | } |
| 5943 | return (record.getUserId() == UserHandle.USER_ALL || |
| 5944 | record.getUserId() == currentUser || |
| 5945 | mUserProfiles.isCurrentProfile(record.getUserId())); |
| 5946 | } |
| 5947 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 5948 | protected void playInCallNotification() { |
Beverly | 28c3d16 | 2018-06-28 11:37:53 -0400 | [diff] [blame] | 5949 | if (mAudioManager.getRingerModeInternal() == AudioManager.RINGER_MODE_NORMAL |
| 5950 | && Settings.Secure.getInt(getContext().getContentResolver(), |
| 5951 | Settings.Secure.IN_CALL_NOTIFICATION_ENABLED, 1) != 0) { |
| 5952 | new Thread() { |
| 5953 | @Override |
| 5954 | public void run() { |
| 5955 | final long identity = Binder.clearCallingIdentity(); |
| 5956 | try { |
| 5957 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 5958 | if (player != null) { |
| 5959 | if (mCallNotificationToken != null) { |
| 5960 | player.stop(mCallNotificationToken); |
| 5961 | } |
| 5962 | mCallNotificationToken = new Binder(); |
| 5963 | player.play(mCallNotificationToken, mInCallNotificationUri, |
| 5964 | mInCallNotificationAudioAttributes, |
| 5965 | mInCallNotificationVolume, false); |
luochaojiang | 50e5273c | 2018-04-16 16:55:03 +0800 | [diff] [blame] | 5966 | } |
Beverly | 28c3d16 | 2018-06-28 11:37:53 -0400 | [diff] [blame] | 5967 | } catch (RemoteException e) { |
| 5968 | } finally { |
| 5969 | Binder.restoreCallingIdentity(identity); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 5970 | } |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 5971 | } |
Beverly | 28c3d16 | 2018-06-28 11:37:53 -0400 | [diff] [blame] | 5972 | }.start(); |
| 5973 | } |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 5974 | } |
| 5975 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5976 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5977 | void showNextToastLocked() { |
| 5978 | ToastRecord record = mToastQueue.get(0); |
| 5979 | while (record != null) { |
| 5980 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| 5981 | try { |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5982 | record.callback.show(record.token); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5983 | scheduleDurationReachedLocked(record); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5984 | return; |
| 5985 | } catch (RemoteException e) { |
| 5986 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| 5987 | + " in package " + record.pkg); |
| 5988 | // remove it from the list and let the process die |
| 5989 | int index = mToastQueue.indexOf(record); |
| 5990 | if (index >= 0) { |
| 5991 | mToastQueue.remove(index); |
| 5992 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5993 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5994 | if (mToastQueue.size() > 0) { |
| 5995 | record = mToastQueue.get(0); |
| 5996 | } else { |
| 5997 | record = null; |
| 5998 | } |
| 5999 | } |
| 6000 | } |
| 6001 | } |
| 6002 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6003 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6004 | void cancelToastLocked(int index) { |
| 6005 | ToastRecord record = mToastQueue.get(index); |
| 6006 | try { |
| 6007 | record.callback.hide(); |
| 6008 | } catch (RemoteException e) { |
| 6009 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| 6010 | + " in package " + record.pkg); |
| 6011 | // don't worry about this, we're about to remove it from |
| 6012 | // the list anyway |
| 6013 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 6014 | |
| 6015 | ToastRecord lastToast = mToastQueue.remove(index); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 6016 | |
| 6017 | mWindowManagerInternal.removeWindowToken(lastToast.token, false /* removeWindows */, |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 6018 | lastToast.displayId); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 6019 | // We passed 'false' for 'removeWindows' so that the client has time to stop |
| 6020 | // rendering (as hide above is a one-way message), otherwise we could crash |
| 6021 | // a client which was actively using a surface made from the token. However |
| 6022 | // we need to schedule a timeout to make sure the token is eventually killed |
| 6023 | // one way or another. |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 6024 | scheduleKillTokenTimeout(lastToast); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 6025 | |
| 6026 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6027 | if (mToastQueue.size() > 0) { |
| 6028 | // Show the next one. If the callback fails, this will remove |
| 6029 | // it from the list, so don't assume that the list hasn't changed |
| 6030 | // after this point. |
| 6031 | showNextToastLocked(); |
| 6032 | } |
| 6033 | } |
| 6034 | |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 6035 | void finishTokenLocked(IBinder t, int displayId) { |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 6036 | mHandler.removeCallbacksAndMessages(t); |
| 6037 | // We pass 'true' for 'removeWindows' to let the WindowManager destroy any |
| 6038 | // remaining surfaces as either the client has called finishToken indicating |
| 6039 | // it has successfully removed the views, or the client has timed out |
| 6040 | // at which point anything goes. |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 6041 | mWindowManagerInternal.removeWindowToken(t, true /* removeWindows */, displayId); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 6042 | } |
| 6043 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6044 | @GuardedBy("mToastQueue") |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 6045 | private void scheduleDurationReachedLocked(ToastRecord r) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6046 | { |
| 6047 | mHandler.removeCallbacksAndMessages(r); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 6048 | Message m = Message.obtain(mHandler, MESSAGE_DURATION_REACHED, r); |
Rhed Jao | 406d3a2 | 2018-11-30 19:28:58 +0800 | [diff] [blame] | 6049 | int delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY; |
| 6050 | // Accessibility users may need longer timeout duration. This api compares original delay |
| 6051 | // with user's preference and return longer one. It returns original delay if there's no |
| 6052 | // preference. |
| 6053 | delay = mAccessibilityManager.getRecommendedTimeoutMillis(delay, |
| 6054 | AccessibilityManager.FLAG_CONTENT_TEXT); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6055 | mHandler.sendMessageDelayed(m, delay); |
| 6056 | } |
| 6057 | |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 6058 | private void handleDurationReached(ToastRecord record) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6059 | { |
| 6060 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| 6061 | synchronized (mToastQueue) { |
| 6062 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 6063 | if (index >= 0) { |
| 6064 | cancelToastLocked(index); |
| 6065 | } |
| 6066 | } |
| 6067 | } |
| 6068 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6069 | @GuardedBy("mToastQueue") |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 6070 | private void scheduleKillTokenTimeout(ToastRecord r) |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 6071 | { |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 6072 | mHandler.removeCallbacksAndMessages(r); |
| 6073 | Message m = Message.obtain(mHandler, MESSAGE_FINISH_TOKEN_TIMEOUT, r); |
Robert Carr | 3406d46 | 2018-03-15 16:19:07 -0700 | [diff] [blame] | 6074 | mHandler.sendMessageDelayed(m, FINISH_TOKEN_TIMEOUT); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 6075 | } |
| 6076 | |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 6077 | private void handleKillTokenTimeout(ToastRecord record) |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 6078 | { |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 6079 | if (DBG) Slog.d(TAG, "Kill Token Timeout token=" + record.token); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 6080 | synchronized (mToastQueue) { |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 6081 | finishTokenLocked(record.token, record.displayId); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 6082 | } |
| 6083 | } |
| 6084 | |
| 6085 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6086 | int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 6087 | { |
| 6088 | IBinder cbak = callback.asBinder(); |
| 6089 | ArrayList<ToastRecord> list = mToastQueue; |
| 6090 | int len = list.size(); |
| 6091 | for (int i=0; i<len; i++) { |
| 6092 | ToastRecord r = list.get(i); |
Beverly Tai | a7ed0ab | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 6093 | if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6094 | return i; |
| 6095 | } |
| 6096 | } |
| 6097 | return -1; |
| 6098 | } |
| 6099 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6100 | @GuardedBy("mToastQueue") |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 6101 | void keepProcessAliveIfNeededLocked(int pid) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6102 | { |
| 6103 | int toastCount = 0; // toasts from this pid |
| 6104 | ArrayList<ToastRecord> list = mToastQueue; |
| 6105 | int N = list.size(); |
| 6106 | for (int i=0; i<N; i++) { |
| 6107 | ToastRecord r = list.get(i); |
| 6108 | if (r.pid == pid) { |
| 6109 | toastCount++; |
| 6110 | } |
| 6111 | } |
| 6112 | try { |
Dianne Hackborn | f965f40 | 2017-05-04 23:27:23 -0700 | [diff] [blame] | 6113 | mAm.setProcessImportant(mForegroundToken, pid, toastCount > 0, "toast"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6114 | } catch (RemoteException e) { |
| 6115 | // Shouldn't happen. |
| 6116 | } |
| 6117 | } |
| 6118 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6119 | private void handleRankingReconsideration(Message message) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 6120 | if (!(message.obj instanceof RankingReconsideration)) return; |
| 6121 | RankingReconsideration recon = (RankingReconsideration) message.obj; |
| 6122 | recon.run(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6123 | boolean changed; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6124 | synchronized (mNotificationLock) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 6125 | final NotificationRecord record = mNotificationsByKey.get(recon.getKey()); |
| 6126 | if (record == null) { |
| 6127 | return; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6128 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6129 | int indexBefore = findNotificationRecordIndexLocked(record); |
| 6130 | boolean interceptBefore = record.isIntercepted(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6131 | int visibilityBefore = record.getPackageVisibilityOverride(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 6132 | recon.applyChangesLocked(record); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6133 | applyZenModeLocked(record); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 6134 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6135 | int indexAfter = findNotificationRecordIndexLocked(record); |
| 6136 | boolean interceptAfter = record.isIntercepted(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6137 | int visibilityAfter = record.getPackageVisibilityOverride(); |
| 6138 | changed = indexBefore != indexAfter || interceptBefore != interceptAfter |
| 6139 | || visibilityBefore != visibilityAfter; |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 6140 | if (interceptBefore && !interceptAfter |
Julia Reynolds | d673007 | 2019-01-04 12:52:52 -0500 | [diff] [blame] | 6141 | && record.isNewEnoughForAlerting(System.currentTimeMillis())) { |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 6142 | buzzBeepBlinkLocked(record); |
| 6143 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6144 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6145 | if (changed) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6146 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 6147 | } |
| 6148 | } |
| 6149 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6150 | void handleRankingSort() { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 6151 | if (mRankingHelper == null) return; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6152 | synchronized (mNotificationLock) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 6153 | final int N = mNotificationList.size(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6154 | // Any field that can change via one of the extractors needs to be added here. |
| 6155 | ArrayList<String> orderBefore = new ArrayList<>(N); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6156 | int[] visibilities = new int[N]; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 6157 | boolean[] showBadges = new boolean[N]; |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 6158 | boolean[] allowBubbles = new boolean[N]; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6159 | ArrayList<NotificationChannel> channelBefore = new ArrayList<>(N); |
| 6160 | ArrayList<String> groupKeyBefore = new ArrayList<>(N); |
| 6161 | ArrayList<ArrayList<String>> overridePeopleBefore = new ArrayList<>(N); |
| 6162 | ArrayList<ArrayList<SnoozeCriterion>> snoozeCriteriaBefore = new ArrayList<>(N); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6163 | ArrayList<Integer> userSentimentBefore = new ArrayList<>(N); |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 6164 | ArrayList<Integer> suppressVisuallyBefore = new ArrayList<>(N); |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 6165 | ArrayList<ArrayList<Notification.Action>> systemSmartActionsBefore = new ArrayList<>(N); |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 6166 | ArrayList<ArrayList<CharSequence>> smartRepliesBefore = new ArrayList<>(N); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 6167 | int[] importancesBefore = new int[N]; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 6168 | for (int i = 0; i < N; i++) { |
| 6169 | final NotificationRecord r = mNotificationList.get(i); |
| 6170 | orderBefore.add(r.getKey()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6171 | visibilities[i] = r.getPackageVisibilityOverride(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 6172 | showBadges[i] = r.canShowBadge(); |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 6173 | allowBubbles[i] = r.canBubble(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6174 | channelBefore.add(r.getChannel()); |
| 6175 | groupKeyBefore.add(r.getGroupKey()); |
| 6176 | overridePeopleBefore.add(r.getPeopleOverride()); |
| 6177 | snoozeCriteriaBefore.add(r.getSnoozeCriteria()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6178 | userSentimentBefore.add(r.getUserSentiment()); |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 6179 | suppressVisuallyBefore.add(r.getSuppressedVisualEffects()); |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 6180 | systemSmartActionsBefore.add(r.getSystemGeneratedSmartActions()); |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 6181 | smartRepliesBefore.add(r.getSmartReplies()); |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 6182 | importancesBefore[i] = r.getImportance(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 6183 | mRankingHelper.extractSignals(r); |
| 6184 | } |
Chris Wren | 19a02b0 | 2015-12-22 10:34:22 -0500 | [diff] [blame] | 6185 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 6186 | for (int i = 0; i < N; i++) { |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6187 | final NotificationRecord r = mNotificationList.get(i); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6188 | if (!orderBefore.get(i).equals(r.getKey()) |
Julia Reynolds | 6976669 | 2016-02-01 15:35:08 -0500 | [diff] [blame] | 6189 | || visibilities[i] != r.getPackageVisibilityOverride() |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6190 | || showBadges[i] != r.canShowBadge() |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 6191 | || allowBubbles[i] != r.canBubble() |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6192 | || !Objects.equals(channelBefore.get(i), r.getChannel()) |
| 6193 | || !Objects.equals(groupKeyBefore.get(i), r.getGroupKey()) |
| 6194 | || !Objects.equals(overridePeopleBefore.get(i), r.getPeopleOverride()) |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6195 | || !Objects.equals(snoozeCriteriaBefore.get(i), r.getSnoozeCriteria()) |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 6196 | || !Objects.equals(userSentimentBefore.get(i), r.getUserSentiment()) |
| 6197 | || !Objects.equals(suppressVisuallyBefore.get(i), |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 6198 | r.getSuppressedVisualEffects()) |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 6199 | || !Objects.equals(systemSmartActionsBefore.get(i), |
| 6200 | r.getSystemGeneratedSmartActions()) |
Julia Reynolds | 27c0a96 | 2018-12-10 12:37:28 -0500 | [diff] [blame] | 6201 | || !Objects.equals(smartRepliesBefore.get(i), r.getSmartReplies()) |
| 6202 | || importancesBefore[i] != r.getImportance()) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6203 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 6204 | return; |
| 6205 | } |
| 6206 | } |
| 6207 | } |
| 6208 | } |
| 6209 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6210 | @GuardedBy("mNotificationLock") |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 6211 | private void recordCallerLocked(NotificationRecord record) { |
| 6212 | if (mZenModeHelper.isCall(record)) { |
| 6213 | mZenModeHelper.recordCaller(record); |
| 6214 | } |
| 6215 | } |
| 6216 | |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 6217 | // let zen mode evaluate this record |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6218 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6219 | private void applyZenModeLocked(NotificationRecord record) { |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 6220 | record.setIntercepted(mZenModeHelper.shouldIntercept(record)); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 6221 | if (record.isIntercepted()) { |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 6222 | record.setSuppressedVisualEffects( |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 6223 | mZenModeHelper.getConsolidatedNotificationPolicy().suppressedVisualEffects); |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 6224 | } else { |
| 6225 | record.setSuppressedVisualEffects(0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 6226 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6227 | } |
| 6228 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6229 | @GuardedBy("mNotificationLock") |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 6230 | private int findNotificationRecordIndexLocked(NotificationRecord target) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 6231 | return mRankingHelper.indexOf(mNotificationList, target); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6232 | } |
| 6233 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6234 | private void handleSendRankingUpdate() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6235 | synchronized (mNotificationLock) { |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6236 | mListeners.notifyRankingUpdateLocked(null); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6237 | } |
| 6238 | } |
| 6239 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6240 | private void scheduleListenerHintsChanged(int state) { |
| 6241 | mHandler.removeMessages(MESSAGE_LISTENER_HINTS_CHANGED); |
| 6242 | mHandler.obtainMessage(MESSAGE_LISTENER_HINTS_CHANGED, state, 0).sendToTarget(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6243 | } |
| 6244 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 6245 | private void scheduleInterruptionFilterChanged(int listenerInterruptionFilter) { |
| 6246 | mHandler.removeMessages(MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED); |
| 6247 | mHandler.obtainMessage( |
| 6248 | MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED, |
| 6249 | listenerInterruptionFilter, |
| 6250 | 0).sendToTarget(); |
| 6251 | } |
| 6252 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6253 | private void handleListenerHintsChanged(int hints) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6254 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6255 | mListeners.notifyListenerHintsChangedLocked(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6256 | } |
| 6257 | } |
| 6258 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 6259 | private void handleListenerInterruptionFilterChanged(int interruptionFilter) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6260 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 6261 | mListeners.notifyInterruptionFilterChanged(interruptionFilter); |
| 6262 | } |
| 6263 | } |
| 6264 | |
Julia Reynolds | b040559 | 2018-11-26 17:01:13 -0500 | [diff] [blame] | 6265 | private void handleOnPackageChanged(boolean removingPackage, int changeUserId, |
| 6266 | String[] pkgList, int[] uidList) { |
| 6267 | mListeners.onPackagesChanged(removingPackage, pkgList, uidList); |
| 6268 | mAssistants.onPackagesChanged(removingPackage, pkgList, uidList); |
| 6269 | mConditionProviders.onPackagesChanged(removingPackage, pkgList, uidList); |
| 6270 | mPreferencesHelper.onPackagesChanged( |
| 6271 | removingPackage, changeUserId, pkgList, uidList); |
| 6272 | handleSavePolicyFile(); |
| 6273 | } |
| 6274 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6275 | protected class WorkerHandler extends Handler |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6276 | { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6277 | public WorkerHandler(Looper looper) { |
| 6278 | super(looper); |
| 6279 | } |
| 6280 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6281 | @Override |
| 6282 | public void handleMessage(Message msg) |
| 6283 | { |
| 6284 | switch (msg.what) |
| 6285 | { |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 6286 | case MESSAGE_DURATION_REACHED: |
Julia Reynolds | b040559 | 2018-11-26 17:01:13 -0500 | [diff] [blame] | 6287 | handleDurationReached((ToastRecord) msg.obj); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 6288 | break; |
| 6289 | case MESSAGE_FINISH_TOKEN_TIMEOUT: |
Julia Reynolds | b040559 | 2018-11-26 17:01:13 -0500 | [diff] [blame] | 6290 | handleKillTokenTimeout((ToastRecord) msg.obj); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6291 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6292 | case MESSAGE_SEND_RANKING_UPDATE: |
| 6293 | handleSendRankingUpdate(); |
| 6294 | break; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6295 | case MESSAGE_LISTENER_HINTS_CHANGED: |
| 6296 | handleListenerHintsChanged(msg.arg1); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6297 | break; |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 6298 | case MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED: |
| 6299 | handleListenerInterruptionFilterChanged(msg.arg1); |
| 6300 | break; |
Julia Reynolds | b040559 | 2018-11-26 17:01:13 -0500 | [diff] [blame] | 6301 | case MESSAGE_ON_PACKAGE_CHANGED: |
| 6302 | SomeArgs args = (SomeArgs) msg.obj; |
| 6303 | handleOnPackageChanged((boolean) args.arg1, args.argi1, (String[]) args.arg2, |
| 6304 | (int[]) args.arg3); |
| 6305 | args.recycle(); |
| 6306 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6307 | } |
| 6308 | } |
| 6309 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6310 | protected void scheduleSendRankingUpdate() { |
| 6311 | if (!hasMessages(MESSAGE_SEND_RANKING_UPDATE)) { |
| 6312 | Message m = Message.obtain(this, MESSAGE_SEND_RANKING_UPDATE); |
| 6313 | sendMessage(m); |
| 6314 | } |
| 6315 | } |
| 6316 | |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 6317 | protected void scheduleCancelNotification(CancelNotificationRunnable cancelRunnable) { |
| 6318 | if (!hasCallbacks(cancelRunnable)) { |
| 6319 | sendMessage(Message.obtain(this, cancelRunnable)); |
| 6320 | } |
| 6321 | } |
Julia Reynolds | b040559 | 2018-11-26 17:01:13 -0500 | [diff] [blame] | 6322 | |
| 6323 | protected void scheduleOnPackageChanged(boolean removingPackage, int changeUserId, |
| 6324 | String[] pkgList, int[] uidList) { |
| 6325 | SomeArgs args = SomeArgs.obtain(); |
| 6326 | args.arg1 = removingPackage; |
| 6327 | args.argi1 = changeUserId; |
| 6328 | args.arg2 = pkgList; |
| 6329 | args.arg3 = uidList; |
| 6330 | sendMessage(Message.obtain(this, MESSAGE_ON_PACKAGE_CHANGED, args)); |
| 6331 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6332 | } |
| 6333 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6334 | private final class RankingHandlerWorker extends Handler implements RankingHandler |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6335 | { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6336 | public RankingHandlerWorker(Looper looper) { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6337 | super(looper); |
| 6338 | } |
| 6339 | |
| 6340 | @Override |
| 6341 | public void handleMessage(Message msg) { |
| 6342 | switch (msg.what) { |
| 6343 | case MESSAGE_RECONSIDER_RANKING: |
| 6344 | handleRankingReconsideration(msg); |
| 6345 | break; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6346 | case MESSAGE_RANKING_SORT: |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6347 | handleRankingSort(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 6348 | break; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6349 | } |
| 6350 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6351 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6352 | public void requestSort() { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6353 | removeMessages(MESSAGE_RANKING_SORT); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 6354 | Message msg = Message.obtain(); |
| 6355 | msg.what = MESSAGE_RANKING_SORT; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 6356 | sendMessage(msg); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6357 | } |
| 6358 | |
| 6359 | public void requestReconsideration(RankingReconsideration recon) { |
| 6360 | Message m = Message.obtain(this, |
| 6361 | NotificationManagerService.MESSAGE_RECONSIDER_RANKING, recon); |
| 6362 | long delay = recon.getDelay(TimeUnit.MILLISECONDS); |
| 6363 | sendMessageDelayed(m, delay); |
| 6364 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6365 | } |
| 6366 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6367 | // Notifications |
| 6368 | // ============================================================================ |
| 6369 | static int clamp(int x, int low, int high) { |
| 6370 | return (x < low) ? low : ((x > high) ? high : x); |
| 6371 | } |
| 6372 | |
| 6373 | void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
Eugene Susla | d4128ec | 2017-12-04 19:48:41 +0000 | [diff] [blame] | 6374 | if (!mAccessibilityManager.isEnabled()) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 6375 | return; |
| 6376 | } |
| 6377 | |
| 6378 | AccessibilityEvent event = |
| 6379 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 6380 | event.setPackageName(packageName); |
| 6381 | event.setClassName(Notification.class.getName()); |
| 6382 | event.setParcelableData(notification); |
| 6383 | CharSequence tickerText = notification.tickerText; |
| 6384 | if (!TextUtils.isEmpty(tickerText)) { |
| 6385 | event.getText().add(tickerText); |
| 6386 | } |
| 6387 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 6388 | mAccessibilityManager.sendAccessibilityEvent(event); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 6389 | } |
| 6390 | |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6391 | /** |
| 6392 | * Removes all NotificationsRecords with the same key as the given notification record |
| 6393 | * from both lists. Do not call this method while iterating over either list. |
| 6394 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6395 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6396 | private boolean removeFromNotificationListsLocked(NotificationRecord r) { |
| 6397 | // Remove from both lists, either list could have a separate Record for what is |
| 6398 | // effectively the same notification. |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6399 | boolean wasPosted = false; |
| 6400 | NotificationRecord recordInList = null; |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6401 | if ((recordInList = findNotificationByListLocked(mNotificationList, r.getKey())) |
| 6402 | != null) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6403 | mNotificationList.remove(recordInList); |
| 6404 | mNotificationsByKey.remove(recordInList.sbn.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6405 | wasPosted = true; |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6406 | } |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 6407 | while ((recordInList = findNotificationByListLocked(mEnqueuedNotifications, r.getKey())) |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6408 | != null) { |
| 6409 | mEnqueuedNotifications.remove(recordInList); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6410 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6411 | return wasPosted; |
| 6412 | } |
| 6413 | |
| 6414 | @GuardedBy("mNotificationLock") |
| 6415 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason, |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 6416 | boolean wasPosted, String listenerName) { |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 6417 | cancelNotificationLocked(r, sendDelete, reason, -1, -1, wasPosted, listenerName); |
| 6418 | } |
| 6419 | |
| 6420 | @GuardedBy("mNotificationLock") |
| 6421 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason, |
| 6422 | int rank, int count, boolean wasPosted, String listenerName) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6423 | final String canceledKey = r.getKey(); |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 6424 | |
| 6425 | // Record caller. |
| 6426 | recordCallerLocked(r); |
| 6427 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6428 | if (r.getStats().getDismissalSurface() == NotificationStats.DISMISSAL_NOT_DISMISSED) { |
| 6429 | r.recordDismissalSurface(NotificationStats.DISMISSAL_OTHER); |
| 6430 | } |
| 6431 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 6432 | // tell the app |
| 6433 | if (sendDelete) { |
Michal Karpinski | d207589 | 2019-04-17 16:01:10 +0100 | [diff] [blame] | 6434 | final PendingIntent deleteIntent = r.getNotification().deleteIntent; |
| 6435 | if (deleteIntent != null) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 6436 | try { |
Michal Karpinski | d207589 | 2019-04-17 16:01:10 +0100 | [diff] [blame] | 6437 | // make sure deleteIntent cannot be used to start activities from background |
| 6438 | LocalServices.getService(ActivityManagerInternal.class) |
| 6439 | .clearPendingIntentAllowBgActivityStarts(deleteIntent.getTarget(), |
| 6440 | WHITELIST_TOKEN); |
| 6441 | deleteIntent.send(); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 6442 | } catch (PendingIntent.CanceledException ex) { |
| 6443 | // do nothing - there's no relevant way to recover, and |
| 6444 | // no reason to let this propagate |
Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 6445 | Slog.w(TAG, "canceled PendingIntent for " + r.sbn.getPackageName(), ex); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 6446 | } |
| 6447 | } |
| 6448 | } |
| 6449 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6450 | // Only cancel these if this notification actually got to be posted. |
| 6451 | if (wasPosted) { |
| 6452 | // status bar |
| 6453 | if (r.getNotification().getSmallIcon() != null) { |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 6454 | if (reason != REASON_SNOOZED) { |
| 6455 | r.isCanceled = true; |
| 6456 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6457 | mListeners.notifyRemovedLocked(r, reason, r.getStats()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6458 | mHandler.post(new Runnable() { |
| 6459 | @Override |
| 6460 | public void run() { |
| 6461 | mGroupHelper.onNotificationRemoved(r.sbn); |
| 6462 | } |
| 6463 | }); |
| 6464 | } |
| 6465 | |
| 6466 | // sound |
| 6467 | if (canceledKey.equals(mSoundNotificationKey)) { |
| 6468 | mSoundNotificationKey = null; |
| 6469 | final long identity = Binder.clearCallingIdentity(); |
| 6470 | try { |
| 6471 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 6472 | if (player != null) { |
| 6473 | player.stopAsync(); |
| 6474 | } |
| 6475 | } catch (RemoteException e) { |
| 6476 | } finally { |
| 6477 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 6478 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6479 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6480 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6481 | // vibrate |
| 6482 | if (canceledKey.equals(mVibrateNotificationKey)) { |
| 6483 | mVibrateNotificationKey = null; |
| 6484 | long identity = Binder.clearCallingIdentity(); |
| 6485 | try { |
| 6486 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 6487 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6488 | finally { |
| 6489 | Binder.restoreCallingIdentity(identity); |
| 6490 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6491 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6492 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6493 | // light |
| 6494 | mLights.remove(canceledKey); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6495 | } |
| 6496 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 6497 | // Record usage stats |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 6498 | // TODO: add unbundling stats? |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 6499 | switch (reason) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 6500 | case REASON_CANCEL: |
| 6501 | case REASON_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 6502 | case REASON_LISTENER_CANCEL: |
| 6503 | case REASON_LISTENER_CANCEL_ALL: |
| 6504 | mUsageStats.registerDismissedByUser(r); |
| 6505 | break; |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 6506 | case REASON_APP_CANCEL: |
| 6507 | case REASON_APP_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 6508 | mUsageStats.registerRemovedByApp(r); |
| 6509 | break; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 6510 | } |
| 6511 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 6512 | String groupKey = r.getGroupKey(); |
| 6513 | NotificationRecord groupSummary = mSummaryByGroupKey.get(groupKey); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6514 | if (groupSummary != null && groupSummary.getKey().equals(canceledKey)) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 6515 | mSummaryByGroupKey.remove(groupKey); |
| 6516 | } |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 6517 | final ArrayMap<String, String> summaries = mAutobundledSummaries.get(r.sbn.getUserId()); |
| 6518 | if (summaries != null && r.sbn.getKey().equals(summaries.get(r.sbn.getPackageName()))) { |
| 6519 | summaries.remove(r.sbn.getPackageName()); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 6520 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6521 | |
Daniel Sandler | 23d7c70 | 2013-03-07 16:32:06 -0500 | [diff] [blame] | 6522 | // Save it for users of getHistoricalNotifications() |
| 6523 | mArchive.record(r.sbn); |
Christoph Studer | 81e5b5f | 2014-10-22 17:19:56 +0200 | [diff] [blame] | 6524 | |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 6525 | final long now = System.currentTimeMillis(); |
Julia Reynolds | 3dfdde0 | 2018-10-08 09:17:56 -0400 | [diff] [blame] | 6526 | final LogMaker logMaker = r.getItemLogMaker() |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 6527 | .setType(MetricsEvent.TYPE_DISMISS) |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 6528 | .setSubtype(reason); |
| 6529 | if (rank != -1 && count != -1) { |
| 6530 | logMaker.addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, rank) |
| 6531 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, count); |
| 6532 | } |
| 6533 | MetricsLogger.action(logMaker); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 6534 | EventLogTags.writeNotificationCanceled(canceledKey, reason, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 6535 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now), |
| 6536 | rank, count, listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6537 | } |
| 6538 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6539 | @VisibleForTesting |
| 6540 | void updateUriPermissions(@Nullable NotificationRecord newRecord, |
| 6541 | @Nullable NotificationRecord oldRecord, String targetPkg, int targetUserId) { |
| 6542 | final String key = (newRecord != null) ? newRecord.getKey() : oldRecord.getKey(); |
| 6543 | if (DBG) Slog.d(TAG, key + ": updating permissions"); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 6544 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6545 | final ArraySet<Uri> newUris = (newRecord != null) ? newRecord.getGrantableUris() : null; |
| 6546 | final ArraySet<Uri> oldUris = (oldRecord != null) ? oldRecord.getGrantableUris() : null; |
| 6547 | |
| 6548 | // Shortcut when no Uris involved |
| 6549 | if (newUris == null && oldUris == null) { |
| 6550 | return; |
| 6551 | } |
| 6552 | |
| 6553 | // Inherit any existing owner |
| 6554 | IBinder permissionOwner = null; |
| 6555 | if (newRecord != null && permissionOwner == null) { |
| 6556 | permissionOwner = newRecord.permissionOwner; |
| 6557 | } |
| 6558 | if (oldRecord != null && permissionOwner == null) { |
| 6559 | permissionOwner = oldRecord.permissionOwner; |
| 6560 | } |
| 6561 | |
| 6562 | // If we have Uris to grant, but no owner yet, go create one |
| 6563 | if (newUris != null && permissionOwner == null) { |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 6564 | if (DBG) Slog.d(TAG, key + ": creating owner"); |
| 6565 | permissionOwner = mUgmInternal.newUriPermissionOwner("NOTIF:" + key); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6566 | } |
| 6567 | |
| 6568 | // If we have no Uris to grant, but an existing owner, go destroy it |
| 6569 | if (newUris == null && permissionOwner != null) { |
| 6570 | final long ident = Binder.clearCallingIdentity(); |
| 6571 | try { |
| 6572 | if (DBG) Slog.d(TAG, key + ": destroying owner"); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 6573 | mUgmInternal.revokeUriPermissionFromOwner(permissionOwner, null, ~0, |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6574 | UserHandle.getUserId(oldRecord.getUid())); |
| 6575 | permissionOwner = null; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6576 | } finally { |
| 6577 | Binder.restoreCallingIdentity(ident); |
| 6578 | } |
| 6579 | } |
| 6580 | |
| 6581 | // Grant access to new Uris |
| 6582 | if (newUris != null && permissionOwner != null) { |
| 6583 | for (int i = 0; i < newUris.size(); i++) { |
| 6584 | final Uri uri = newUris.valueAt(i); |
| 6585 | if (oldUris == null || !oldUris.contains(uri)) { |
| 6586 | if (DBG) Slog.d(TAG, key + ": granting " + uri); |
| 6587 | grantUriPermission(permissionOwner, uri, newRecord.getUid(), targetPkg, |
| 6588 | targetUserId); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 6589 | } |
| 6590 | } |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6591 | } |
| 6592 | |
| 6593 | // Revoke access to old Uris |
| 6594 | if (oldUris != null && permissionOwner != null) { |
| 6595 | for (int i = 0; i < oldUris.size(); i++) { |
| 6596 | final Uri uri = oldUris.valueAt(i); |
| 6597 | if (newUris == null || !newUris.contains(uri)) { |
| 6598 | if (DBG) Slog.d(TAG, key + ": revoking " + uri); |
| 6599 | revokeUriPermission(permissionOwner, uri, oldRecord.getUid()); |
| 6600 | } |
| 6601 | } |
| 6602 | } |
| 6603 | |
| 6604 | if (newRecord != null) { |
| 6605 | newRecord.permissionOwner = permissionOwner; |
| 6606 | } |
| 6607 | } |
| 6608 | |
| 6609 | private void grantUriPermission(IBinder owner, Uri uri, int sourceUid, String targetPkg, |
| 6610 | int targetUserId) { |
| 6611 | if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return; |
| 6612 | |
| 6613 | final long ident = Binder.clearCallingIdentity(); |
| 6614 | try { |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 6615 | mUgm.grantUriPermissionFromOwner(owner, sourceUid, targetPkg, |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6616 | ContentProvider.getUriWithoutUserId(uri), |
| 6617 | Intent.FLAG_GRANT_READ_URI_PERMISSION, |
| 6618 | ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid)), |
| 6619 | targetUserId); |
| 6620 | } catch (RemoteException ignored) { |
| 6621 | // Ignored because we're in same process |
| 6622 | } finally { |
| 6623 | Binder.restoreCallingIdentity(ident); |
| 6624 | } |
| 6625 | } |
| 6626 | |
| 6627 | private void revokeUriPermission(IBinder owner, Uri uri, int sourceUid) { |
| 6628 | if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return; |
| 6629 | |
| 6630 | final long ident = Binder.clearCallingIdentity(); |
| 6631 | try { |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 6632 | mUgmInternal.revokeUriPermissionFromOwner( |
| 6633 | owner, |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6634 | ContentProvider.getUriWithoutUserId(uri), |
| 6635 | Intent.FLAG_GRANT_READ_URI_PERMISSION, |
| 6636 | ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid))); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 6637 | } finally { |
| 6638 | Binder.restoreCallingIdentity(ident); |
| 6639 | } |
| 6640 | } |
| 6641 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6642 | /** |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 6643 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 6644 | * and none of the {@code mustNotHaveFlags}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6645 | */ |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 6646 | void cancelNotification(final int callingUid, final int callingPid, |
| 6647 | final String pkg, final String tag, final int id, |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 6648 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6649 | final int userId, final int reason, final ManagedServiceInfo listener) { |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 6650 | cancelNotification(callingUid, callingPid, pkg, tag, id, mustHaveFlags, mustNotHaveFlags, |
| 6651 | sendDelete, userId, reason, -1 /* rank */, -1 /* count */, listener); |
| 6652 | } |
| 6653 | |
| 6654 | /** |
| 6655 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
| 6656 | * and none of the {@code mustNotHaveFlags}. |
| 6657 | */ |
| 6658 | void cancelNotification(final int callingUid, final int callingPid, |
| 6659 | final String pkg, final String tag, final int id, |
| 6660 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 6661 | final int userId, final int reason, int rank, int count, |
| 6662 | final ManagedServiceInfo listener) { |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 6663 | // In enqueueNotificationInternal notifications are added by scheduling the |
| 6664 | // work on the worker handler. Hence, we also schedule the cancel on this |
| 6665 | // handler to avoid a scenario where an add notification call followed by a |
| 6666 | // remove notification call ends up in not removing the notification. |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 6667 | mHandler.scheduleCancelNotification(new CancelNotificationRunnable(callingUid, callingPid, |
| 6668 | pkg, tag, id, mustHaveFlags, mustNotHaveFlags, sendDelete, userId, reason, rank, |
| 6669 | count, listener)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6670 | } |
| 6671 | |
| 6672 | /** |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 6673 | * Determine whether the userId applies to the notification in question, either because |
| 6674 | * they match exactly, or one of them is USER_ALL (which is treated as a wildcard). |
| 6675 | */ |
| 6676 | private boolean notificationMatchesUserId(NotificationRecord r, int userId) { |
| 6677 | return |
| 6678 | // looking for USER_ALL notifications? match everything |
| 6679 | userId == UserHandle.USER_ALL |
| 6680 | // a notification sent to USER_ALL matches any query |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 6681 | || r.getUserId() == UserHandle.USER_ALL |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 6682 | // an exact user match |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 6683 | || r.getUserId() == userId; |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 6684 | } |
| 6685 | |
| 6686 | /** |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6687 | * Determine whether the userId applies to the notification in question, either because |
| 6688 | * 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] | 6689 | * because it matches one of the users profiles. |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6690 | */ |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 6691 | private boolean notificationMatchesCurrentProfiles(NotificationRecord r, int userId) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6692 | return notificationMatchesUserId(r, userId) |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 6693 | || mUserProfiles.isCurrentProfile(r.getUserId()); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6694 | } |
| 6695 | |
| 6696 | /** |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 6697 | * 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] | 6698 | * {@code mustHaveFlags}. |
| 6699 | */ |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6700 | void cancelAllNotificationsInt(int callingUid, int callingPid, String pkg, String channelId, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 6701 | int mustHaveFlags, int mustNotHaveFlags, boolean doit, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6702 | ManagedServiceInfo listener) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6703 | mHandler.post(new Runnable() { |
| 6704 | @Override |
| 6705 | public void run() { |
| 6706 | String listenerName = listener == null ? null : listener.component.toShortString(); |
| 6707 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 6708 | pkg, userId, mustHaveFlags, mustNotHaveFlags, reason, |
| 6709 | listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6710 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6711 | // Why does this parameter exist? Do we actually want to execute the above if doit |
| 6712 | // is false? |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 6713 | if (!doit) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6714 | return; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 6715 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6716 | |
| 6717 | synchronized (mNotificationLock) { |
| 6718 | FlagChecker flagChecker = (int flags) -> { |
| 6719 | if ((flags & mustHaveFlags) != mustHaveFlags) { |
| 6720 | return false; |
| 6721 | } |
| 6722 | if ((flags & mustNotHaveFlags) != 0) { |
| 6723 | return false; |
| 6724 | } |
| 6725 | return true; |
| 6726 | }; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6727 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 6728 | pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, flagChecker, |
| 6729 | false /*includeCurrentProfiles*/, userId, false /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6730 | listenerName, true /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6731 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 6732 | callingPid, pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, |
| 6733 | flagChecker, false /*includeCurrentProfiles*/, userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6734 | false /*sendDelete*/, reason, listenerName, false /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6735 | mSnoozeHelper.cancel(userId, pkg); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 6736 | } |
| 6737 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6738 | }); |
| 6739 | } |
| 6740 | |
| 6741 | private interface FlagChecker { |
| 6742 | // Returns false if these flags do not pass the defined flag test. |
| 6743 | public boolean apply(int flags); |
| 6744 | } |
| 6745 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6746 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6747 | private void cancelAllNotificationsByListLocked(ArrayList<NotificationRecord> notificationList, |
| 6748 | int callingUid, int callingPid, String pkg, boolean nullPkgIndicatesUserSwitch, |
| 6749 | String channelId, FlagChecker flagChecker, boolean includeCurrentProfiles, int userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6750 | boolean sendDelete, int reason, String listenerName, boolean wasPosted) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6751 | ArrayList<NotificationRecord> canceledNotifications = null; |
| 6752 | for (int i = notificationList.size() - 1; i >= 0; --i) { |
| 6753 | NotificationRecord r = notificationList.get(i); |
| 6754 | if (includeCurrentProfiles) { |
| 6755 | if (!notificationMatchesCurrentProfiles(r, userId)) { |
| 6756 | continue; |
| 6757 | } |
| 6758 | } else if (!notificationMatchesUserId(r, userId)) { |
| 6759 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6760 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6761 | // Don't remove notifications to all, if there's no package name specified |
| 6762 | if (nullPkgIndicatesUserSwitch && pkg == null && r.getUserId() == UserHandle.USER_ALL) { |
| 6763 | continue; |
| 6764 | } |
| 6765 | if (!flagChecker.apply(r.getFlags())) { |
| 6766 | continue; |
| 6767 | } |
| 6768 | if (pkg != null && !r.sbn.getPackageName().equals(pkg)) { |
| 6769 | continue; |
| 6770 | } |
| 6771 | if (channelId != null && !channelId.equals(r.getChannel().getId())) { |
| 6772 | continue; |
| 6773 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6774 | if (canceledNotifications == null) { |
| 6775 | canceledNotifications = new ArrayList<>(); |
| 6776 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6777 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 6778 | mNotificationsByKey.remove(r.getKey()); |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 6779 | r.recordDismissalSentiment(NotificationStats.DISMISS_SENTIMENT_NEUTRAL); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6780 | canceledNotifications.add(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 6781 | cancelNotificationLocked(r, sendDelete, reason, wasPosted, listenerName); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6782 | } |
| 6783 | if (canceledNotifications != null) { |
| 6784 | final int M = canceledNotifications.size(); |
| 6785 | for (int i = 0; i < M; i++) { |
| 6786 | cancelGroupChildrenLocked(canceledNotifications.get(i), callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 6787 | listenerName, false /* sendDelete */, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6788 | } |
| 6789 | updateLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6790 | } |
| 6791 | } |
| 6792 | |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 6793 | void snoozeNotificationInt(String key, long duration, String snoozeCriterionId, |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6794 | ManagedServiceInfo listener) { |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6795 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 6796 | if (duration <= 0 && snoozeCriterionId == null || key == null) { |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 6797 | return; |
| 6798 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 6799 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6800 | if (DBG) { |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 6801 | Slog.d(TAG, String.format("snooze event(%s, %d, %s, %s)", key, duration, |
| 6802 | snoozeCriterionId, listenerName)); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6803 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6804 | // Needs to post so that it can cancel notifications not yet enqueued. |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 6805 | mHandler.post(new SnoozeNotificationRunnable(key, duration, snoozeCriterionId)); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 6806 | } |
| 6807 | |
| 6808 | void unsnoozeNotificationInt(String key, ManagedServiceInfo listener) { |
| 6809 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 6810 | if (DBG) { |
| 6811 | Slog.d(TAG, String.format("unsnooze event(%s, %s)", key, listenerName)); |
| 6812 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6813 | mSnoozeHelper.repost(key); |
Julia Reynolds | b62dad4 | 2018-11-26 16:33:02 -0500 | [diff] [blame] | 6814 | handleSavePolicyFile(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 6815 | } |
| 6816 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6817 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 350159c | 2014-03-27 11:15:11 -0700 | [diff] [blame] | 6818 | void cancelAllLocked(int callingUid, int callingPid, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6819 | ManagedServiceInfo listener, boolean includeCurrentProfiles) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6820 | mHandler.post(new Runnable() { |
| 6821 | @Override |
| 6822 | public void run() { |
| 6823 | synchronized (mNotificationLock) { |
| 6824 | String listenerName = |
| 6825 | listener == null ? null : listener.component.toShortString(); |
| 6826 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 6827 | null, userId, 0, 0, reason, listenerName); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 6828 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6829 | FlagChecker flagChecker = (int flags) -> { |
Mady Mellor | 49b1bf1 | 2019-03-29 12:00:02 -0700 | [diff] [blame] | 6830 | int flagsToCheck = FLAG_ONGOING_EVENT | FLAG_NO_CLEAR; |
| 6831 | if (REASON_LISTENER_CANCEL_ALL == reason) { |
| 6832 | flagsToCheck |= FLAG_BUBBLE; |
| 6833 | } |
| 6834 | if ((flags & flagsToCheck) != 0) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6835 | return false; |
| 6836 | } |
| 6837 | return true; |
| 6838 | }; |
| 6839 | |
| 6840 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 6841 | null, false /*nullPkgIndicatesUserSwitch*/, null, flagChecker, |
| 6842 | includeCurrentProfiles, userId, true /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6843 | listenerName, true); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6844 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 6845 | callingPid, null, false /*nullPkgIndicatesUserSwitch*/, null, |
| 6846 | flagChecker, includeCurrentProfiles, userId, true /*sendDelete*/, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6847 | reason, listenerName, false); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6848 | mSnoozeHelper.cancel(userId, includeCurrentProfiles); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6849 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6850 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6851 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6852 | } |
| 6853 | |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 6854 | // Warning: The caller is responsible for invoking updateLightsLocked(). |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6855 | @GuardedBy("mNotificationLock") |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 6856 | private void cancelGroupChildrenLocked(NotificationRecord r, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 6857 | String listenerName, boolean sendDelete, FlagChecker flagChecker) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 6858 | Notification n = r.getNotification(); |
Christoph Studer | 3f31f5d | 2014-07-31 16:55:32 +0200 | [diff] [blame] | 6859 | if (!n.isGroupSummary()) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 6860 | return; |
| 6861 | } |
| 6862 | |
| 6863 | String pkg = r.sbn.getPackageName(); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 6864 | |
| 6865 | if (pkg == null) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 6866 | if (DBG) Slog.e(TAG, "No package for group summary: " + r.getKey()); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 6867 | return; |
| 6868 | } |
| 6869 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6870 | cancelGroupChildrenByListLocked(mNotificationList, r, callingUid, callingPid, listenerName, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 6871 | sendDelete, true, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6872 | cancelGroupChildrenByListLocked(mEnqueuedNotifications, r, callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 6873 | listenerName, sendDelete, false, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6874 | } |
| 6875 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6876 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6877 | private void cancelGroupChildrenByListLocked(ArrayList<NotificationRecord> notificationList, |
| 6878 | NotificationRecord parentNotification, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 6879 | String listenerName, boolean sendDelete, boolean wasPosted, FlagChecker flagChecker) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6880 | final String pkg = parentNotification.sbn.getPackageName(); |
| 6881 | final int userId = parentNotification.getUserId(); |
| 6882 | final int reason = REASON_GROUP_SUMMARY_CANCELED; |
| 6883 | for (int i = notificationList.size() - 1; i >= 0; i--) { |
| 6884 | final NotificationRecord childR = notificationList.get(i); |
| 6885 | final StatusBarNotification childSbn = childR.sbn; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 6886 | if ((childSbn.isGroup() && !childSbn.getNotification().isGroupSummary()) && |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6887 | childR.getGroupKey().equals(parentNotification.getGroupKey()) |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 6888 | && (childR.getFlags() & FLAG_FOREGROUND_SERVICE) == 0 |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 6889 | && (flagChecker == null || flagChecker.apply(childR.getFlags()))) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 6890 | EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, childSbn.getId(), |
| 6891 | childSbn.getTag(), userId, 0, 0, reason, listenerName); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6892 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 6893 | mNotificationsByKey.remove(childR.getKey()); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 6894 | cancelNotificationLocked(childR, sendDelete, reason, wasPosted, listenerName); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 6895 | } |
| 6896 | } |
| 6897 | } |
| 6898 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6899 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6900 | void updateLightsLocked() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6901 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 6902 | // handle notification lights |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 6903 | NotificationRecord ledNotification = null; |
| 6904 | while (ledNotification == null && !mLights.isEmpty()) { |
| 6905 | final String owner = mLights.get(mLights.size() - 1); |
| 6906 | ledNotification = mNotificationsByKey.get(owner); |
| 6907 | if (ledNotification == null) { |
| 6908 | Slog.wtfStack(TAG, "LED Notification does not exist: " + owner); |
| 6909 | mLights.remove(owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6910 | } |
| 6911 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 6912 | |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 6913 | // 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] | 6914 | if (ledNotification == null || mInCall || mScreenOn) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 6915 | mNotificationLight.turnOff(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 6916 | } else { |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 6917 | NotificationRecord.Light light = ledNotification.getLight(); |
| 6918 | if (light != null && mNotificationPulseEnabled) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 6919 | // pulse repeatedly |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 6920 | mNotificationLight.setFlashing(light.color, Light.LIGHT_FLASH_TIMED, |
| 6921 | light.onMs, light.offMs); |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 6922 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 6923 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6924 | } |
| 6925 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6926 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 6927 | @NonNull List<NotificationRecord> findGroupNotificationsLocked(String pkg, |
| 6928 | String groupKey, int userId) { |
| 6929 | List<NotificationRecord> records = new ArrayList<>(); |
| 6930 | records.addAll(findGroupNotificationByListLocked(mNotificationList, pkg, groupKey, userId)); |
| 6931 | records.addAll( |
| 6932 | findGroupNotificationByListLocked(mEnqueuedNotifications, pkg, groupKey, userId)); |
| 6933 | return records; |
| 6934 | } |
| 6935 | |
| 6936 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6937 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 6938 | private @NonNull List<NotificationRecord> findGroupNotificationByListLocked( |
| 6939 | ArrayList<NotificationRecord> list, String pkg, String groupKey, int userId) { |
| 6940 | List<NotificationRecord> records = new ArrayList<>(); |
| 6941 | final int len = list.size(); |
| 6942 | for (int i = 0; i < len; i++) { |
| 6943 | NotificationRecord r = list.get(i); |
| 6944 | if (notificationMatchesUserId(r, userId) && r.getGroupKey().equals(groupKey) |
| 6945 | && r.sbn.getPackageName().equals(pkg)) { |
| 6946 | records.add(r); |
| 6947 | } |
| 6948 | } |
| 6949 | return records; |
| 6950 | } |
| 6951 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6952 | // Searches both enqueued and posted notifications by key. |
| 6953 | // TODO: need to combine a bunch of these getters with slightly different behavior. |
| 6954 | // TODO: Should enqueuing just add to mNotificationsByKey instead? |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6955 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6956 | private NotificationRecord findNotificationByKeyLocked(String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6957 | NotificationRecord r; |
| 6958 | if ((r = findNotificationByListLocked(mNotificationList, key)) != null) { |
| 6959 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6960 | } |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6961 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, key)) != null) { |
| 6962 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6963 | } |
| 6964 | return null; |
| 6965 | } |
| 6966 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6967 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 6968 | NotificationRecord findNotificationLocked(String pkg, String tag, int id, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6969 | NotificationRecord r; |
| 6970 | if ((r = findNotificationByListLocked(mNotificationList, pkg, tag, id, userId)) != null) { |
| 6971 | return r; |
| 6972 | } |
| 6973 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, pkg, tag, id, userId)) |
| 6974 | != null) { |
| 6975 | return r; |
| 6976 | } |
| 6977 | return null; |
| 6978 | } |
| 6979 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6980 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6981 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6982 | String pkg, String tag, int id, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6983 | final int len = list.size(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6984 | for (int i = 0; i < len; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6985 | NotificationRecord r = list.get(i); |
Vladimir Marko | 2526f33 | 2013-09-11 11:13:55 +0100 | [diff] [blame] | 6986 | if (notificationMatchesUserId(r, userId) && r.sbn.getId() == id && |
| 6987 | TextUtils.equals(r.sbn.getTag(), tag) && r.sbn.getPackageName().equals(pkg)) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6988 | return r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6989 | } |
| 6990 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6991 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6992 | } |
| 6993 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6994 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 564273f | 2018-09-13 15:53:11 -0400 | [diff] [blame] | 6995 | private List<NotificationRecord> findNotificationsByListLocked( |
| 6996 | ArrayList<NotificationRecord> list, String pkg, String tag, int id, int userId) { |
| 6997 | List<NotificationRecord> matching = new ArrayList<>(); |
| 6998 | final int len = list.size(); |
| 6999 | for (int i = 0; i < len; i++) { |
| 7000 | NotificationRecord r = list.get(i); |
| 7001 | if (notificationMatchesUserId(r, userId) && r.sbn.getId() == id && |
| 7002 | TextUtils.equals(r.sbn.getTag(), tag) && r.sbn.getPackageName().equals(pkg)) { |
| 7003 | matching.add(r); |
| 7004 | } |
| 7005 | } |
| 7006 | return matching; |
| 7007 | } |
| 7008 | |
| 7009 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 7010 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 7011 | String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 7012 | final int N = list.size(); |
| 7013 | for (int i = 0; i < N; i++) { |
| 7014 | if (key.equals(list.get(i).getKey())) { |
| 7015 | return list.get(i); |
| 7016 | } |
| 7017 | } |
| 7018 | return null; |
| 7019 | } |
| 7020 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 7021 | @GuardedBy("mNotificationLock") |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 7022 | int indexOfNotificationLocked(String key) { |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 7023 | final int N = mNotificationList.size(); |
| 7024 | for (int i = 0; i < N; i++) { |
| 7025 | if (key.equals(mNotificationList.get(i).getKey())) { |
| 7026 | return i; |
| 7027 | } |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 7028 | } |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 7029 | return -1; |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 7030 | } |
| 7031 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7032 | @VisibleForTesting |
| 7033 | protected void hideNotificationsForPackages(String[] pkgs) { |
| 7034 | synchronized (mNotificationLock) { |
| 7035 | List<String> pkgList = Arrays.asList(pkgs); |
| 7036 | List<NotificationRecord> changedNotifications = new ArrayList<>(); |
| 7037 | int numNotifications = mNotificationList.size(); |
| 7038 | for (int i = 0; i < numNotifications; i++) { |
| 7039 | NotificationRecord rec = mNotificationList.get(i); |
| 7040 | if (pkgList.contains(rec.sbn.getPackageName())) { |
| 7041 | rec.setHidden(true); |
| 7042 | changedNotifications.add(rec); |
| 7043 | } |
| 7044 | } |
| 7045 | |
| 7046 | mListeners.notifyHiddenLocked(changedNotifications); |
| 7047 | } |
| 7048 | } |
| 7049 | |
| 7050 | @VisibleForTesting |
| 7051 | protected void unhideNotificationsForPackages(String[] pkgs) { |
| 7052 | synchronized (mNotificationLock) { |
| 7053 | List<String> pkgList = Arrays.asList(pkgs); |
| 7054 | List<NotificationRecord> changedNotifications = new ArrayList<>(); |
| 7055 | int numNotifications = mNotificationList.size(); |
| 7056 | for (int i = 0; i < numNotifications; i++) { |
| 7057 | NotificationRecord rec = mNotificationList.get(i); |
| 7058 | if (pkgList.contains(rec.sbn.getPackageName())) { |
| 7059 | rec.setHidden(false); |
| 7060 | changedNotifications.add(rec); |
| 7061 | } |
| 7062 | } |
| 7063 | |
| 7064 | mListeners.notifyUnhiddenLocked(changedNotifications); |
| 7065 | } |
| 7066 | } |
| 7067 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 7068 | private void updateNotificationPulse() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 7069 | synchronized (mNotificationLock) { |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 7070 | updateLightsLocked(); |
| 7071 | } |
| 7072 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 7073 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 7074 | protected boolean isCallingUidSystem() { |
| 7075 | final int uid = Binder.getCallingUid(); |
| 7076 | return uid == Process.SYSTEM_UID; |
| 7077 | } |
| 7078 | |
| 7079 | protected boolean isUidSystemOrPhone(int uid) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7080 | final int appid = UserHandle.getAppId(uid); |
| 7081 | return (appid == Process.SYSTEM_UID || appid == Process.PHONE_UID || uid == 0); |
| 7082 | } |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 7083 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 7084 | // TODO: Most calls should probably move to isCallerSystem. |
| 7085 | protected boolean isCallerSystemOrPhone() { |
| 7086 | return isUidSystemOrPhone(Binder.getCallingUid()); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7087 | } |
| 7088 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7089 | private void checkCallerIsSystemOrShell() { |
| 7090 | if (Binder.getCallingUid() == Process.SHELL_UID) { |
| 7091 | return; |
| 7092 | } |
| 7093 | checkCallerIsSystem(); |
| 7094 | } |
| 7095 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7096 | private void checkCallerIsSystem() { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 7097 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7098 | return; |
| 7099 | } |
| 7100 | throw new SecurityException("Disallowed call for uid " + Binder.getCallingUid()); |
| 7101 | } |
| 7102 | |
Julia Reynolds | dc6adc6 | 2019-04-08 10:35:40 -0400 | [diff] [blame] | 7103 | private void checkCallerIsSystemOrSystemUiOrShell() { |
| 7104 | if (Binder.getCallingUid() == Process.SHELL_UID) { |
| 7105 | return; |
| 7106 | } |
| 7107 | if (isCallerSystemOrPhone()) { |
| 7108 | return; |
| 7109 | } |
| 7110 | getContext().enforceCallingPermission(android.Manifest.permission.STATUS_BAR_SERVICE, null); |
| 7111 | } |
| 7112 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 7113 | private void checkCallerIsSystemOrSameApp(String pkg) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 7114 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7115 | return; |
| 7116 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 7117 | checkCallerIsSameApp(pkg); |
| 7118 | } |
| 7119 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 7120 | private boolean isCallerAndroid(String callingPkg, int uid) { |
| 7121 | return isUidSystemOrPhone(uid) && callingPkg != null |
| 7122 | && PackageManagerService.PLATFORM_PACKAGE_NAME.equals(callingPkg); |
| 7123 | } |
| 7124 | |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 7125 | /** |
| 7126 | * Check if the notification is of a category type that is restricted to system use only, |
| 7127 | * if so throw SecurityException |
| 7128 | */ |
| 7129 | private void checkRestrictedCategories(final Notification notification) { |
| 7130 | try { |
| 7131 | if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) { |
| 7132 | return; |
| 7133 | } |
| 7134 | } catch (RemoteException re) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 7135 | if (DBG) Slog.e(TAG, "Unable to confirm if it's safe to skip category " |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 7136 | + "restrictions check thus the check will be done anyway"); |
| 7137 | } |
| 7138 | if (Notification.CATEGORY_CAR_EMERGENCY.equals(notification.category) |
| 7139 | || Notification.CATEGORY_CAR_WARNING.equals(notification.category) |
| 7140 | || Notification.CATEGORY_CAR_INFORMATION.equals(notification.category)) { |
| 7141 | checkCallerIsSystem(); |
| 7142 | } |
| 7143 | } |
| 7144 | |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 7145 | @VisibleForTesting |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 7146 | boolean isCallerInstantApp(int callingUid, int userId) { |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 7147 | // System is always allowed to act for ephemeral apps. |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 7148 | if (isUidSystemOrPhone(callingUid)) { |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 7149 | return false; |
| 7150 | } |
| 7151 | |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 7152 | try { |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 7153 | final String[] pkgs = mPackageManager.getPackagesForUid(callingUid); |
| 7154 | if (pkgs == null) { |
| 7155 | throw new SecurityException("Unknown uid " + callingUid); |
| 7156 | } |
| 7157 | final String pkg = pkgs[0]; |
| 7158 | mAppOps.checkPackage(callingUid, pkg); |
| 7159 | |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 7160 | ApplicationInfo ai = mPackageManager.getApplicationInfo(pkg, 0, userId); |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 7161 | if (ai == null) { |
| 7162 | throw new SecurityException("Unknown package " + pkg); |
| 7163 | } |
| 7164 | return ai.isInstantApp(); |
| 7165 | } catch (RemoteException re) { |
Julia Reynolds | 268647a | 2018-10-25 16:54:27 -0400 | [diff] [blame] | 7166 | throw new SecurityException("Unknown uid " + callingUid, re); |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 7167 | } |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 7168 | } |
| 7169 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 7170 | private void checkCallerIsSameApp(String pkg) { |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 7171 | checkCallerIsSameApp(pkg, Binder.getCallingUid(), UserHandle.getCallingUserId()); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 7172 | } |
| 7173 | |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 7174 | private void checkCallerIsSameApp(String pkg, int uid, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7175 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 7176 | ApplicationInfo ai = mPackageManager.getApplicationInfo( |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 7177 | pkg, 0, userId); |
Dan Sandler | 09afc2e | 2014-07-18 14:29:20 -0400 | [diff] [blame] | 7178 | if (ai == null) { |
| 7179 | throw new SecurityException("Unknown package " + pkg); |
| 7180 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7181 | if (!UserHandle.isSameApp(ai.uid, uid)) { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 7182 | throw new SecurityException("Calling uid " + uid + " gave package " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7183 | + pkg + " which is owned by uid " + ai.uid); |
| 7184 | } |
| 7185 | } catch (RemoteException re) { |
| 7186 | throw new SecurityException("Unknown package " + pkg + "\n" + re); |
| 7187 | } |
| 7188 | } |
| 7189 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 7190 | private boolean isCallerSameApp(String pkg) { |
| 7191 | try { |
| 7192 | checkCallerIsSameApp(pkg); |
| 7193 | return true; |
| 7194 | } catch (SecurityException e) { |
| 7195 | return false; |
| 7196 | } |
| 7197 | } |
| 7198 | |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 7199 | private boolean isCallerSameApp(String pkg, int uid, int userId) { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 7200 | try { |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 7201 | checkCallerIsSameApp(pkg, uid, userId); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 7202 | return true; |
| 7203 | } catch (SecurityException e) { |
| 7204 | return false; |
| 7205 | } |
| 7206 | } |
| 7207 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 7208 | private static String callStateToString(int state) { |
| 7209 | switch (state) { |
| 7210 | case TelephonyManager.CALL_STATE_IDLE: return "CALL_STATE_IDLE"; |
| 7211 | case TelephonyManager.CALL_STATE_RINGING: return "CALL_STATE_RINGING"; |
| 7212 | case TelephonyManager.CALL_STATE_OFFHOOK: return "CALL_STATE_OFFHOOK"; |
| 7213 | default: return "CALL_STATE_UNKNOWN_" + state; |
| 7214 | } |
| 7215 | } |
| 7216 | |
| 7217 | private void listenForCallState() { |
| 7218 | TelephonyManager.from(getContext()).listen(new PhoneStateListener() { |
| 7219 | @Override |
| 7220 | public void onCallStateChanged(int state, String incomingNumber) { |
| 7221 | if (mCallState == state) return; |
| 7222 | if (DBG) Slog.d(TAG, "Call state changed: " + callStateToString(state)); |
| 7223 | mCallState = state; |
| 7224 | } |
| 7225 | }, PhoneStateListener.LISTEN_CALL_STATE); |
| 7226 | } |
| 7227 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7228 | /** |
| 7229 | * Generates a NotificationRankingUpdate from 'sbns', considering only |
| 7230 | * notifications visible to the given listener. |
| 7231 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 7232 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 7233 | private NotificationRankingUpdate makeRankingUpdateLocked(ManagedServiceInfo info) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 7234 | final int N = mNotificationList.size(); |
| 7235 | ArrayList<String> keys = new ArrayList<String>(N); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 7236 | ArrayList<String> interceptedKeys = new ArrayList<String>(N); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 7237 | ArrayList<Integer> importance = new ArrayList<>(N); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 7238 | Bundle overrideGroupKeys = new Bundle(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 7239 | Bundle visibilityOverrides = new Bundle(); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 7240 | Bundle suppressedVisualEffects = new Bundle(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 7241 | Bundle explanation = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 7242 | Bundle channels = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 7243 | Bundle overridePeople = new Bundle(); |
| 7244 | Bundle snoozeCriteria = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 7245 | Bundle showBadge = new Bundle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 7246 | Bundle userSentiment = new Bundle(); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7247 | Bundle hidden = new Bundle(); |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 7248 | Bundle systemGeneratedSmartActions = new Bundle(); |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 7249 | Bundle smartReplies = new Bundle(); |
Gus Prevas | 7306b90 | 2018-12-11 10:57:06 -0500 | [diff] [blame] | 7250 | Bundle lastAudiblyAlerted = new Bundle(); |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 7251 | Bundle noisy = new Bundle(); |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 7252 | ArrayList<Boolean> canBubble = new ArrayList<>(N); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 7253 | for (int i = 0; i < N; i++) { |
| 7254 | NotificationRecord record = mNotificationList.get(i); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 7255 | if (!isVisibleToListener(record.sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7256 | continue; |
| 7257 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 7258 | final String key = record.sbn.getKey(); |
| 7259 | keys.add(key); |
| 7260 | importance.add(record.getImportance()); |
| 7261 | if (record.getImportanceExplanation() != null) { |
| 7262 | explanation.putCharSequence(key, record.getImportanceExplanation()); |
| 7263 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 7264 | if (record.isIntercepted()) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 7265 | interceptedKeys.add(key); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 7266 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7267 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 7268 | suppressedVisualEffects.putInt(key, record.getSuppressedVisualEffects()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 7269 | if (record.getPackageVisibilityOverride() |
| 7270 | != NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 7271 | visibilityOverrides.putInt(key, record.getPackageVisibilityOverride()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 7272 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 7273 | overrideGroupKeys.putString(key, record.sbn.getOverrideGroupKey()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 7274 | channels.putParcelable(key, record.getChannel()); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 7275 | overridePeople.putStringArrayList(key, record.getPeopleOverride()); |
| 7276 | snoozeCriteria.putParcelableArrayList(key, record.getSnoozeCriteria()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 7277 | showBadge.putBoolean(key, record.canShowBadge()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 7278 | userSentiment.putInt(key, record.getUserSentiment()); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7279 | hidden.putBoolean(key, record.isHidden()); |
Gustav Sennton | 1463d83 | 2018-11-06 16:12:48 +0000 | [diff] [blame] | 7280 | systemGeneratedSmartActions.putParcelableArrayList(key, |
| 7281 | record.getSystemGeneratedSmartActions()); |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 7282 | smartReplies.putCharSequenceArrayList(key, record.getSmartReplies()); |
Gus Prevas | 7306b90 | 2018-12-11 10:57:06 -0500 | [diff] [blame] | 7283 | lastAudiblyAlerted.putLong(key, record.getLastAudiblyAlertedMs()); |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame] | 7284 | noisy.putBoolean(key, record.getSound() != null || record.getVibration() != null); |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 7285 | canBubble.add(record.canBubble()); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7286 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 7287 | final int M = keys.size(); |
| 7288 | String[] keysAr = keys.toArray(new String[M]); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 7289 | String[] interceptedKeysAr = interceptedKeys.toArray(new String[interceptedKeys.size()]); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 7290 | int[] importanceAr = new int[M]; |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 7291 | boolean[] canBubbleAr = new boolean[M]; |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 7292 | for (int i = 0; i < M; i++) { |
| 7293 | importanceAr[i] = importance.get(i); |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 7294 | canBubbleAr[i] = canBubble.get(i); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 7295 | } |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 7296 | return new NotificationRankingUpdate(keysAr, interceptedKeysAr, visibilityOverrides, |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 7297 | suppressedVisualEffects, importanceAr, explanation, overrideGroupKeys, |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 7298 | channels, overridePeople, snoozeCriteria, showBadge, userSentiment, hidden, |
Julia Reynolds | 4509ce7 | 2019-01-31 13:12:43 -0500 | [diff] [blame] | 7299 | systemGeneratedSmartActions, smartReplies, lastAudiblyAlerted, noisy, |
| 7300 | canBubbleAr); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7301 | } |
| 7302 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 7303 | boolean hasCompanionDevice(ManagedServiceInfo info) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7304 | if (mCompanionManager == null) { |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 7305 | mCompanionManager = getCompanionManager(); |
| 7306 | } |
| 7307 | // Companion mgr doesn't exist on all device types |
| 7308 | if (mCompanionManager == null) { |
| 7309 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7310 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 7311 | long identity = Binder.clearCallingIdentity(); |
| 7312 | try { |
| 7313 | List<String> associations = mCompanionManager.getAssociations( |
| 7314 | info.component.getPackageName(), info.userid); |
| 7315 | if (!ArrayUtils.isEmpty(associations)) { |
| 7316 | return true; |
| 7317 | } |
| 7318 | } catch (SecurityException se) { |
| 7319 | // Not a privileged listener |
| 7320 | } catch (RemoteException re) { |
| 7321 | Slog.e(TAG, "Cannot reach companion device service", re); |
| 7322 | } catch (Exception e) { |
| 7323 | Slog.e(TAG, "Cannot verify listener " + info, e); |
| 7324 | } finally { |
| 7325 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7326 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 7327 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7328 | } |
| 7329 | |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 7330 | protected ICompanionDeviceManager getCompanionManager() { |
| 7331 | return ICompanionDeviceManager.Stub.asInterface( |
| 7332 | ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE)); |
| 7333 | } |
| 7334 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 7335 | private boolean isVisibleToListener(StatusBarNotification sbn, ManagedServiceInfo listener) { |
| 7336 | if (!listener.enabledAndUserMatches(sbn.getUserId())) { |
| 7337 | return false; |
| 7338 | } |
Justin Koh | 8d11a5a | 2014-08-04 18:29:49 -0700 | [diff] [blame] | 7339 | // TODO: remove this for older listeners. |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 7340 | return true; |
| 7341 | } |
| 7342 | |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 7343 | private boolean isPackageSuspendedForUser(String pkg, int uid) { |
Beverly | 2be7a05 | 2018-03-27 11:37:58 -0400 | [diff] [blame] | 7344 | final long identity = Binder.clearCallingIdentity(); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 7345 | int userId = UserHandle.getUserId(uid); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 7346 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 7347 | return mPackageManager.isPackageSuspendedForUser(pkg, userId); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 7348 | } catch (RemoteException re) { |
| 7349 | throw new SecurityException("Could not talk to package manager service"); |
Andrei Stingaceanu | efc4a34 | 2016-03-22 14:43:01 +0000 | [diff] [blame] | 7350 | } catch (IllegalArgumentException ex) { |
| 7351 | // Package not found. |
| 7352 | return false; |
Beverly | 2be7a05 | 2018-03-27 11:37:58 -0400 | [diff] [blame] | 7353 | } finally { |
| 7354 | Binder.restoreCallingIdentity(identity); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 7355 | } |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 7356 | } |
| 7357 | |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 7358 | @VisibleForTesting |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 7359 | boolean canUseManagedServices(String pkg, Integer userId, String requiredPermission) { |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 7360 | boolean canUseManagedServices = !mActivityManager.isLowRamDevice() |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 7361 | || mPackageManagerClient.hasSystemFeature(PackageManager.FEATURE_WATCH); |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 7362 | |
| 7363 | for (String whitelisted : getContext().getResources().getStringArray( |
| 7364 | R.array.config_allowedManagedServicesOnLowRamDevices)) { |
| 7365 | if (whitelisted.equals(pkg)) { |
| 7366 | canUseManagedServices = true; |
| 7367 | } |
| 7368 | } |
| 7369 | |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 7370 | if (requiredPermission != null) { |
| 7371 | try { |
| 7372 | if (mPackageManager.checkPermission(requiredPermission, pkg, userId) |
| 7373 | != PackageManager.PERMISSION_GRANTED) { |
| 7374 | canUseManagedServices = false; |
| 7375 | } |
| 7376 | } catch (RemoteException e) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 7377 | Slog.e(TAG, "can't talk to pm", e); |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 7378 | } |
| 7379 | } |
| 7380 | |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 7381 | return canUseManagedServices; |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 7382 | } |
| 7383 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 7384 | private class TrimCache { |
| 7385 | StatusBarNotification heavy; |
| 7386 | StatusBarNotification sbnClone; |
| 7387 | StatusBarNotification sbnCloneLight; |
| 7388 | |
| 7389 | TrimCache(StatusBarNotification sbn) { |
| 7390 | heavy = sbn; |
| 7391 | } |
| 7392 | |
| 7393 | StatusBarNotification ForListener(ManagedServiceInfo info) { |
| 7394 | if (mListeners.getOnNotificationPostedTrim(info) == TRIM_LIGHT) { |
| 7395 | if (sbnCloneLight == null) { |
| 7396 | sbnCloneLight = heavy.cloneLight(); |
| 7397 | } |
| 7398 | return sbnCloneLight; |
| 7399 | } else { |
| 7400 | if (sbnClone == null) { |
| 7401 | sbnClone = heavy.clone(); |
| 7402 | } |
| 7403 | return sbnClone; |
| 7404 | } |
| 7405 | } |
| 7406 | } |
| 7407 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 7408 | public class NotificationAssistants extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7409 | static final String TAG_ENABLED_NOTIFICATION_ASSISTANTS = "enabled_assistants"; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 7410 | |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 7411 | private static final String ATT_USER_SET = "user_set"; |
Julia Reynolds | 54c5fd1 | 2019-05-20 12:56:27 -0400 | [diff] [blame] | 7412 | private static final String TAG_ALLOWED_ADJUSTMENT_TYPES = "q_allowed_adjustments"; |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 7413 | private static final String ATT_TYPES = "types"; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 7414 | |
| 7415 | private final Object mLock = new Object(); |
| 7416 | |
| 7417 | @GuardedBy("mLock") |
| 7418 | private ArrayMap<Integer, Boolean> mUserSetMap = new ArrayMap<>(); |
Julia Reynolds | dc6adc6 | 2019-04-08 10:35:40 -0400 | [diff] [blame] | 7419 | private Set<String> mAllowedAdjustments = new ArraySet<>(); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 7420 | |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 7421 | public NotificationAssistants(Context context, Object lock, UserProfiles up, |
| 7422 | IPackageManager pm) { |
| 7423 | super(context, lock, up, pm); |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 7424 | |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 7425 | // Add all default allowed adjustment types. Will be overwritten by values in xml, |
| 7426 | // if they exist |
| 7427 | for (int i = 0; i < DEFAULT_ALLOWED_ADJUSTMENTS.length; i++) { |
| 7428 | mAllowedAdjustments.add(DEFAULT_ALLOWED_ADJUSTMENTS[i]); |
| 7429 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 7430 | } |
| 7431 | |
| 7432 | @Override |
| 7433 | protected Config getConfig() { |
| 7434 | Config c = new Config(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 7435 | c.caption = "notification assistant"; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 7436 | c.serviceInterface = NotificationAssistantService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 7437 | c.xmlTag = TAG_ENABLED_NOTIFICATION_ASSISTANTS; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 7438 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_ASSISTANT; |
| 7439 | c.bindPermission = Manifest.permission.BIND_NOTIFICATION_ASSISTANT_SERVICE; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 7440 | c.settingsAction = Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS; |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 7441 | c.clientLabel = R.string.notification_ranker_binding_label; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 7442 | return c; |
| 7443 | } |
| 7444 | |
| 7445 | @Override |
| 7446 | protected IInterface asInterface(IBinder binder) { |
| 7447 | return INotificationListener.Stub.asInterface(binder); |
| 7448 | } |
| 7449 | |
| 7450 | @Override |
| 7451 | protected boolean checkType(IInterface service) { |
| 7452 | return service instanceof INotificationListener; |
| 7453 | } |
| 7454 | |
| 7455 | @Override |
| 7456 | protected void onServiceAdded(ManagedServiceInfo info) { |
| 7457 | mListeners.registerGuestService(info); |
| 7458 | } |
| 7459 | |
| 7460 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 7461 | @GuardedBy("mNotificationLock") |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 7462 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
| 7463 | mListeners.unregisterService(removed.service, removed.userid); |
| 7464 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 7465 | |
Julia Reynolds | ef934fd | 2018-02-01 14:39:17 -0500 | [diff] [blame] | 7466 | @Override |
| 7467 | public void onUserUnlocked(int user) { |
| 7468 | if (DEBUG) Slog.d(TAG, "onUserUnlocked u=" + user); |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 7469 | // force rebind the assistant, as it might be keeping its own state in user locked |
| 7470 | // storage |
| 7471 | rebindServices(true, user); |
Julia Reynolds | ef934fd | 2018-02-01 14:39:17 -0500 | [diff] [blame] | 7472 | } |
| 7473 | |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 7474 | @Override |
| 7475 | protected String getRequiredPermission() { |
Julia Reynolds | cbc45e7 | 2019-03-07 12:31:52 -0500 | [diff] [blame] | 7476 | // only signature/privileged apps can be bound. |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 7477 | return android.Manifest.permission.REQUEST_NOTIFICATION_ASSISTANT_SERVICE; |
| 7478 | } |
| 7479 | |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 7480 | @Override |
| 7481 | protected void writeExtraXmlTags(XmlSerializer out) throws IOException { |
| 7482 | synchronized (mLock) { |
| 7483 | out.startTag(null, TAG_ALLOWED_ADJUSTMENT_TYPES); |
| 7484 | out.attribute(null, ATT_TYPES, TextUtils.join(",", mAllowedAdjustments)); |
| 7485 | out.endTag(null, TAG_ALLOWED_ADJUSTMENT_TYPES); |
| 7486 | } |
| 7487 | } |
| 7488 | |
| 7489 | @Override |
| 7490 | protected void readExtraTag(String tag, XmlPullParser parser) throws IOException { |
| 7491 | if (TAG_ALLOWED_ADJUSTMENT_TYPES.equals(tag)) { |
| 7492 | final String types = XmlUtils.readStringAttribute(parser, ATT_TYPES); |
Julia Reynolds | 25692c4 | 2019-05-03 15:01:24 -0400 | [diff] [blame] | 7493 | synchronized (mLock) { |
| 7494 | mAllowedAdjustments.clear(); |
| 7495 | if (!TextUtils.isEmpty(types)) { |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 7496 | mAllowedAdjustments.addAll(Arrays.asList(types.split(","))); |
| 7497 | } |
| 7498 | } |
| 7499 | } |
| 7500 | } |
| 7501 | |
| 7502 | protected void allowAdjustmentType(String type) { |
| 7503 | synchronized (mLock) { |
| 7504 | mAllowedAdjustments.add(type); |
| 7505 | } |
Julia Reynolds | dc6adc6 | 2019-04-08 10:35:40 -0400 | [diff] [blame] | 7506 | for (final ManagedServiceInfo info : NotificationAssistants.this.getServices()) { |
| 7507 | mHandler.post(() -> notifyCapabilitiesChanged(info)); |
| 7508 | } |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 7509 | } |
| 7510 | |
| 7511 | protected void disallowAdjustmentType(String type) { |
| 7512 | synchronized (mLock) { |
| 7513 | mAllowedAdjustments.remove(type); |
| 7514 | } |
Julia Reynolds | dc6adc6 | 2019-04-08 10:35:40 -0400 | [diff] [blame] | 7515 | for (final ManagedServiceInfo info : NotificationAssistants.this.getServices()) { |
| 7516 | mHandler.post(() -> notifyCapabilitiesChanged(info)); |
| 7517 | } |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 7518 | } |
| 7519 | |
Julia Reynolds | 088c448 | 2019-04-10 12:43:27 -0400 | [diff] [blame] | 7520 | protected List<String> getAllowedAssistantAdjustments() { |
Julia Reynolds | ad6dd35 | 2019-03-07 16:46:22 -0500 | [diff] [blame] | 7521 | synchronized (mLock) { |
| 7522 | List<String> types = new ArrayList<>(); |
| 7523 | types.addAll(mAllowedAdjustments); |
| 7524 | return types; |
| 7525 | } |
| 7526 | } |
| 7527 | |
Julia Reynolds | 418a8ff | 2019-03-21 10:45:10 -0400 | [diff] [blame] | 7528 | protected boolean isAdjustmentAllowed(String type) { |
| 7529 | synchronized (mLock) { |
| 7530 | return mAllowedAdjustments.contains(type); |
| 7531 | } |
| 7532 | } |
| 7533 | |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 7534 | protected void onNotificationsSeenLocked(ArrayList<NotificationRecord> records) { |
| 7535 | // There should be only one, but it's a list, so while we enforce |
| 7536 | // singularity elsewhere, we keep it general here, to avoid surprises. |
| 7537 | for (final ManagedServiceInfo info : NotificationAssistants.this.getServices()) { |
| 7538 | ArrayList<String> keys = new ArrayList<>(records.size()); |
| 7539 | for (NotificationRecord r : records) { |
| 7540 | boolean sbnVisible = isVisibleToListener(r.sbn, info) |
| 7541 | && info.isSameUser(r.getUserId()); |
| 7542 | if (sbnVisible) { |
| 7543 | keys.add(r.getKey()); |
| 7544 | } |
| 7545 | } |
| 7546 | |
| 7547 | if (!keys.isEmpty()) { |
| 7548 | mHandler.post(() -> notifySeen(info, keys)); |
| 7549 | } |
| 7550 | } |
| 7551 | } |
| 7552 | |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 7553 | boolean hasUserSet(int userId) { |
| 7554 | synchronized (mLock) { |
| 7555 | return mUserSetMap.getOrDefault(userId, false); |
| 7556 | } |
| 7557 | } |
| 7558 | |
| 7559 | void setUserSet(int userId, boolean set) { |
| 7560 | synchronized (mLock) { |
| 7561 | mUserSetMap.put(userId, set); |
| 7562 | } |
| 7563 | } |
| 7564 | |
| 7565 | @Override |
| 7566 | protected void writeExtraAttributes(XmlSerializer out, int userId) throws IOException { |
| 7567 | out.attribute(null, ATT_USER_SET, Boolean.toString(hasUserSet(userId))); |
| 7568 | } |
| 7569 | |
| 7570 | @Override |
| 7571 | protected void readExtraAttributes(String tag, XmlPullParser parser, int userId) |
| 7572 | throws IOException { |
| 7573 | boolean userSet = XmlUtils.readBooleanAttribute(parser, ATT_USER_SET, false); |
| 7574 | setUserSet(userId, userSet); |
| 7575 | } |
| 7576 | |
Julia Reynolds | dc6adc6 | 2019-04-08 10:35:40 -0400 | [diff] [blame] | 7577 | private void notifyCapabilitiesChanged(final ManagedServiceInfo info) { |
| 7578 | final INotificationListener assistant = (INotificationListener) info.service; |
| 7579 | try { |
Julia Reynolds | 088c448 | 2019-04-10 12:43:27 -0400 | [diff] [blame] | 7580 | assistant.onAllowedAdjustmentsChanged(); |
Julia Reynolds | dc6adc6 | 2019-04-08 10:35:40 -0400 | [diff] [blame] | 7581 | } catch (RemoteException ex) { |
| 7582 | Slog.e(TAG, "unable to notify assistant (capabilities): " + assistant, ex); |
| 7583 | } |
| 7584 | } |
| 7585 | |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 7586 | private void notifySeen(final ManagedServiceInfo info, |
| 7587 | final ArrayList<String> keys) { |
| 7588 | final INotificationListener assistant = (INotificationListener) info.service; |
| 7589 | try { |
| 7590 | assistant.onNotificationsSeen(keys); |
| 7591 | } catch (RemoteException ex) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 7592 | Slog.e(TAG, "unable to notify assistant (seen): " + assistant, ex); |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 7593 | } |
| 7594 | } |
| 7595 | |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 7596 | @GuardedBy("mNotificationLock") |
| 7597 | private void onNotificationEnqueuedLocked(final NotificationRecord r) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 7598 | final boolean debug = isVerboseLogEnabled(); |
| 7599 | if (debug) { |
| 7600 | Slog.v(TAG, "onNotificationEnqueuedLocked() called with: r = [" + r + "]"); |
| 7601 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 7602 | final StatusBarNotification sbn = r.sbn; |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 7603 | notifyAssistantLocked( |
| 7604 | sbn, |
| 7605 | true /* sameUserOnly */, |
| 7606 | (assistant, sbnHolder) -> { |
| 7607 | try { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 7608 | if (debug) { |
| 7609 | Slog.v(TAG, |
| 7610 | "calling onNotificationEnqueuedWithChannel " + sbnHolder); |
| 7611 | } |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 7612 | assistant.onNotificationEnqueuedWithChannel(sbnHolder, r.getChannel()); |
| 7613 | } catch (RemoteException ex) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 7614 | Slog.e(TAG, "unable to notify assistant (enqueued): " + assistant, ex); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 7615 | } |
| 7616 | }); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 7617 | } |
| 7618 | |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 7619 | @GuardedBy("mNotificationLock") |
| 7620 | void notifyAssistantExpansionChangedLocked( |
| 7621 | final StatusBarNotification sbn, |
| 7622 | final boolean isUserAction, |
| 7623 | final boolean isExpanded) { |
| 7624 | final String key = sbn.getKey(); |
| 7625 | notifyAssistantLocked( |
| 7626 | sbn, |
| 7627 | false /* sameUserOnly */, |
| 7628 | (assistant, sbnHolder) -> { |
| 7629 | try { |
| 7630 | assistant.onNotificationExpansionChanged(key, isUserAction, isExpanded); |
| 7631 | } catch (RemoteException ex) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 7632 | Slog.e(TAG, "unable to notify assistant (expanded): " + assistant, ex); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 7633 | } |
| 7634 | }); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 7635 | } |
| 7636 | |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 7637 | @GuardedBy("mNotificationLock") |
| 7638 | void notifyAssistantNotificationDirectReplyLocked( |
| 7639 | final StatusBarNotification sbn) { |
| 7640 | final String key = sbn.getKey(); |
| 7641 | notifyAssistantLocked( |
| 7642 | sbn, |
| 7643 | false /* sameUserOnly */, |
| 7644 | (assistant, sbnHolder) -> { |
| 7645 | try { |
| 7646 | assistant.onNotificationDirectReply(key); |
| 7647 | } catch (RemoteException ex) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 7648 | Slog.e(TAG, "unable to notify assistant (expanded): " + assistant, ex); |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 7649 | } |
| 7650 | }); |
| 7651 | } |
| 7652 | |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 7653 | @GuardedBy("mNotificationLock") |
| 7654 | void notifyAssistantSuggestedReplySent( |
| 7655 | final StatusBarNotification sbn, CharSequence reply, boolean generatedByAssistant) { |
| 7656 | final String key = sbn.getKey(); |
| 7657 | notifyAssistantLocked( |
| 7658 | sbn, |
| 7659 | false /* sameUserOnly */, |
| 7660 | (assistant, sbnHolder) -> { |
| 7661 | try { |
| 7662 | assistant.onSuggestedReplySent( |
| 7663 | key, |
| 7664 | reply, |
| 7665 | generatedByAssistant |
| 7666 | ? NotificationAssistantService.SOURCE_FROM_ASSISTANT |
| 7667 | : NotificationAssistantService.SOURCE_FROM_APP); |
| 7668 | } catch (RemoteException ex) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 7669 | Slog.e(TAG, "unable to notify assistant (snoozed): " + assistant, ex); |
Tony Mak | 2999670 | 2018-11-26 16:23:34 +0000 | [diff] [blame] | 7670 | } |
| 7671 | }); |
| 7672 | } |
| 7673 | |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 7674 | @GuardedBy("mNotificationLock") |
| 7675 | void notifyAssistantActionClicked( |
| 7676 | final StatusBarNotification sbn, int actionIndex, Notification.Action action, |
| 7677 | boolean generatedByAssistant) { |
| 7678 | final String key = sbn.getKey(); |
| 7679 | notifyAssistantLocked( |
| 7680 | sbn, |
| 7681 | false /* sameUserOnly */, |
| 7682 | (assistant, sbnHolder) -> { |
| 7683 | try { |
| 7684 | assistant.onActionClicked( |
| 7685 | key, |
| 7686 | action, |
| 7687 | generatedByAssistant |
| 7688 | ? NotificationAssistantService.SOURCE_FROM_ASSISTANT |
| 7689 | : NotificationAssistantService.SOURCE_FROM_APP); |
| 7690 | } catch (RemoteException ex) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 7691 | Slog.e(TAG, "unable to notify assistant (snoozed): " + assistant, ex); |
Tony Mak | 7d4b3a5 | 2018-11-27 17:29:36 +0000 | [diff] [blame] | 7692 | } |
| 7693 | }); |
| 7694 | } |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 7695 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 7696 | /** |
| 7697 | * asynchronously notify the assistant that a notification has been snoozed until a |
| 7698 | * context |
| 7699 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 7700 | @GuardedBy("mNotificationLock") |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 7701 | private void notifyAssistantSnoozedLocked( |
| 7702 | final StatusBarNotification sbn, final String snoozeCriterionId) { |
| 7703 | notifyAssistantLocked( |
| 7704 | sbn, |
| 7705 | false /* sameUserOnly */, |
| 7706 | (assistant, sbnHolder) -> { |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 7707 | try { |
| 7708 | assistant.onNotificationSnoozedUntilContext( |
| 7709 | sbnHolder, snoozeCriterionId); |
| 7710 | } catch (RemoteException ex) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 7711 | Slog.e(TAG, "unable to notify assistant (snoozed): " + assistant, ex); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 7712 | } |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 7713 | }); |
| 7714 | } |
| 7715 | |
| 7716 | /** |
| 7717 | * Notifies the assistant something about the specified notification, only assistant |
| 7718 | * that is visible to the notification will be notified. |
| 7719 | * |
| 7720 | * @param sbn the notification object that the update is about. |
| 7721 | * @param sameUserOnly should the update be sent to the assistant in the same user only. |
| 7722 | * @param callback the callback that provides the assistant to be notified, executed |
| 7723 | * in WorkerHandler. |
| 7724 | */ |
| 7725 | @GuardedBy("mNotificationLock") |
| 7726 | private void notifyAssistantLocked( |
| 7727 | final StatusBarNotification sbn, |
| 7728 | boolean sameUserOnly, |
| 7729 | BiConsumer<INotificationListener, StatusBarNotificationHolder> callback) { |
| 7730 | TrimCache trimCache = new TrimCache(sbn); |
| 7731 | // There should be only one, but it's a list, so while we enforce |
| 7732 | // singularity elsewhere, we keep it general here, to avoid surprises. |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 7733 | |
| 7734 | final boolean debug = isVerboseLogEnabled(); |
| 7735 | if (debug) { |
| 7736 | Slog.v(TAG, |
| 7737 | "notifyAssistantLocked() called with: sbn = [" + sbn + "], sameUserOnly = [" |
| 7738 | + sameUserOnly + "], callback = [" + callback + "]"); |
| 7739 | } |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 7740 | for (final ManagedServiceInfo info : NotificationAssistants.this.getServices()) { |
| 7741 | boolean sbnVisible = isVisibleToListener(sbn, info) |
| 7742 | && (!sameUserOnly || info.isSameUser(sbn.getUserId())); |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 7743 | if (debug) { |
| 7744 | Slog.v(TAG, "notifyAssistantLocked info=" + info + " snbVisible=" + sbnVisible); |
| 7745 | } |
Tony Mak | eda84a7 | 2018-11-19 17:01:32 +0000 | [diff] [blame] | 7746 | if (!sbnVisible) { |
| 7747 | continue; |
| 7748 | } |
| 7749 | final INotificationListener assistant = (INotificationListener) info.service; |
| 7750 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
| 7751 | final StatusBarNotificationHolder sbnHolder = |
| 7752 | new StatusBarNotificationHolder(sbnToPost); |
| 7753 | mHandler.post(() -> callback.accept(assistant, sbnHolder)); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 7754 | } |
| 7755 | } |
| 7756 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 7757 | public boolean isEnabled() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 7758 | return !getServices().isEmpty(); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 7759 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 7760 | |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 7761 | protected void resetDefaultAssistantsIfNecessary() { |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 7762 | final List<UserInfo> activeUsers = mUm.getUsers(true); |
| 7763 | for (UserInfo userInfo : activeUsers) { |
| 7764 | int userId = userInfo.getUserHandle().getIdentifier(); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 7765 | if (!hasUserSet(userId)) { |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 7766 | Slog.d(TAG, "Approving default notification assistant for user " + userId); |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 7767 | setDefaultAssistantForUser(userId); |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 7768 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 7769 | } |
| 7770 | } |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 7771 | |
| 7772 | @Override |
| 7773 | protected void setPackageOrComponentEnabled(String pkgOrComponent, int userId, |
| 7774 | boolean isPrimary, boolean enabled) { |
| 7775 | // Ensures that only one component is enabled at a time |
| 7776 | if (enabled) { |
| 7777 | List<ComponentName> allowedComponents = getAllowedComponents(userId); |
| 7778 | if (!allowedComponents.isEmpty()) { |
| 7779 | ComponentName currentComponent = CollectionUtils.firstOrNull(allowedComponents); |
| 7780 | if (currentComponent.flattenToString().equals(pkgOrComponent)) return; |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 7781 | setNotificationAssistantAccessGrantedForUserInternal( |
| 7782 | currentComponent, userId, false); |
Fabian Kozynski | d942566 | 2019-01-29 13:08:30 -0500 | [diff] [blame] | 7783 | } |
| 7784 | } |
| 7785 | super.setPackageOrComponentEnabled(pkgOrComponent, userId, isPrimary, enabled); |
| 7786 | } |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 7787 | |
| 7788 | @Override |
| 7789 | public void dump(PrintWriter pw, DumpFilter filter) { |
| 7790 | super.dump(pw, filter); |
| 7791 | pw.println(" Has user set:"); |
| 7792 | synchronized (mLock) { |
| 7793 | Set<Integer> userIds = mUserSetMap.keySet(); |
| 7794 | for (int userId : userIds) { |
| 7795 | pw.println(" userId=" + userId + " value=" + mUserSetMap.get(userId)); |
| 7796 | } |
| 7797 | } |
| 7798 | } |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 7799 | |
| 7800 | private boolean isVerboseLogEnabled() { |
| 7801 | return Log.isLoggable("notification_assistant", Log.VERBOSE); |
| 7802 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 7803 | } |
| 7804 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7805 | public class NotificationListeners extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7806 | static final String TAG_ENABLED_NOTIFICATION_LISTENERS = "enabled_listeners"; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7807 | |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 7808 | private final ArraySet<ManagedServiceInfo> mLightTrimListeners = new ArraySet<>(); |
| 7809 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7810 | public NotificationListeners(IPackageManager pm) { |
| 7811 | super(getContext(), mNotificationLock, mUserProfiles, pm); |
| 7812 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7813 | } |
| 7814 | |
| 7815 | @Override |
Amith Yamasani | e5bfeee | 2018-09-05 18:52:35 -0700 | [diff] [blame] | 7816 | protected int getBindFlags() { |
| 7817 | // Most of the same flags as the base, but also add BIND_ADJUST_BELOW_PERCEPTIBLE |
| 7818 | // because too many 3P apps could be kept in memory as notification listeners and |
| 7819 | // cause extreme memory pressure. |
| 7820 | // TODO: Change the binding lifecycle of NotificationListeners to avoid this situation. |
| 7821 | return BIND_AUTO_CREATE | BIND_FOREGROUND_SERVICE |
| 7822 | | BIND_ADJUST_BELOW_PERCEPTIBLE | BIND_ALLOW_WHITELIST_MANAGEMENT; |
| 7823 | } |
| 7824 | |
| 7825 | @Override |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7826 | protected Config getConfig() { |
| 7827 | Config c = new Config(); |
| 7828 | c.caption = "notification listener"; |
| 7829 | c.serviceInterface = NotificationListenerService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 7830 | c.xmlTag = TAG_ENABLED_NOTIFICATION_LISTENERS; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7831 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS; |
| 7832 | c.bindPermission = android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE; |
| 7833 | c.settingsAction = Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS; |
| 7834 | c.clientLabel = R.string.notification_listener_binding_label; |
| 7835 | return c; |
| 7836 | } |
| 7837 | |
| 7838 | @Override |
| 7839 | protected IInterface asInterface(IBinder binder) { |
| 7840 | return INotificationListener.Stub.asInterface(binder); |
| 7841 | } |
| 7842 | |
| 7843 | @Override |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 7844 | protected boolean checkType(IInterface service) { |
| 7845 | return service instanceof INotificationListener; |
| 7846 | } |
| 7847 | |
| 7848 | @Override |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 7849 | public void onServiceAdded(ManagedServiceInfo info) { |
| 7850 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 7851 | final NotificationRankingUpdate update; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 7852 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 7853 | update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7854 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7855 | try { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 7856 | listener.onListenerConnected(update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7857 | } catch (RemoteException e) { |
| 7858 | // we tried |
| 7859 | } |
| 7860 | } |
| 7861 | |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 7862 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 7863 | @GuardedBy("mNotificationLock") |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 7864 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 7865 | if (removeDisabledHints(removed)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 7866 | updateListenerHintsLocked(); |
Christoph Studer | 0d6ef4b | 2014-12-02 15:00:48 +0100 | [diff] [blame] | 7867 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 7868 | } |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 7869 | mLightTrimListeners.remove(removed); |
| 7870 | } |
| 7871 | |
Julia Reynolds | d0ceefa | 2019-03-03 16:10:52 -0500 | [diff] [blame] | 7872 | @Override |
| 7873 | protected String getRequiredPermission() { |
| 7874 | return null; |
| 7875 | } |
| 7876 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 7877 | @GuardedBy("mNotificationLock") |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 7878 | public void setOnNotificationPostedTrimLocked(ManagedServiceInfo info, int trim) { |
| 7879 | if (trim == TRIM_LIGHT) { |
| 7880 | mLightTrimListeners.add(info); |
| 7881 | } else { |
| 7882 | mLightTrimListeners.remove(info); |
| 7883 | } |
| 7884 | } |
| 7885 | |
| 7886 | public int getOnNotificationPostedTrim(ManagedServiceInfo info) { |
| 7887 | return mLightTrimListeners.contains(info) ? TRIM_LIGHT : TRIM_FULL; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 7888 | } |
| 7889 | |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 7890 | public void onStatusBarIconsBehaviorChanged(boolean hideSilentStatusIcons) { |
| 7891 | for (final ManagedServiceInfo info : getServices()) { |
| 7892 | mHandler.post(() -> { |
| 7893 | final INotificationListener listener = (INotificationListener) info.service; |
| 7894 | try { |
| 7895 | listener.onStatusBarIconsBehaviorChanged(hideSilentStatusIcons); |
| 7896 | } catch (RemoteException ex) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 7897 | Slog.e(TAG, "unable to notify listener " |
Julia Reynolds | 12ad7ca | 2019-01-28 09:29:16 -0500 | [diff] [blame] | 7898 | + "(hideSilentStatusIcons): " + listener, ex); |
| 7899 | } |
| 7900 | }); |
| 7901 | } |
| 7902 | } |
| 7903 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7904 | /** |
| 7905 | * asynchronously notify all listeners about a new notification |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 7906 | * |
| 7907 | * <p> |
| 7908 | * Also takes care of removing a notification that has been visible to a listener before, |
| 7909 | * but isn't anymore. |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7910 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 7911 | @GuardedBy("mNotificationLock") |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 7912 | public void notifyPostedLocked(NotificationRecord r, NotificationRecord old) { |
| 7913 | notifyPostedLocked(r, old, true); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7914 | } |
| 7915 | |
| 7916 | /** |
| 7917 | * @param notifyAllListeners notifies all listeners if true, else only notifies listeners |
| 7918 | * targetting <= O_MR1 |
| 7919 | */ |
| 7920 | @GuardedBy("mNotificationLock") |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 7921 | private void notifyPostedLocked(NotificationRecord r, NotificationRecord old, |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7922 | boolean notifyAllListeners) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 7923 | // Lazily initialized snapshots of the notification. |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 7924 | StatusBarNotification sbn = r.sbn; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 7925 | StatusBarNotification oldSbn = (old != null) ? old.sbn : null; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 7926 | TrimCache trimCache = new TrimCache(sbn); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 7927 | |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 7928 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 7929 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 7930 | boolean oldSbnVisible = oldSbn != null ? isVisibleToListener(oldSbn, info) : false; |
| 7931 | // This notification hasn't been and still isn't visible -> ignore. |
| 7932 | if (!oldSbnVisible && !sbnVisible) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7933 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 7934 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7935 | // If the notification is hidden, don't notifyPosted listeners targeting < P. |
| 7936 | // Instead, those listeners will receive notifyPosted when the notification is |
| 7937 | // unhidden. |
| 7938 | if (r.isHidden() && info.targetSdkVersion < Build.VERSION_CODES.P) { |
| 7939 | continue; |
| 7940 | } |
| 7941 | |
| 7942 | // If we shouldn't notify all listeners, this means the hidden state of |
| 7943 | // a notification was changed. Don't notifyPosted listeners targeting >= P. |
| 7944 | // Instead, those listeners will receive notifyRankingUpdate. |
| 7945 | if (!notifyAllListeners && info.targetSdkVersion >= Build.VERSION_CODES.P) { |
| 7946 | continue; |
| 7947 | } |
| 7948 | |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 7949 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 7950 | |
| 7951 | // This notification became invisible -> remove the old one. |
| 7952 | if (oldSbnVisible && !sbnVisible) { |
| 7953 | final StatusBarNotification oldSbnLightClone = oldSbn.cloneLight(); |
| 7954 | mHandler.post(new Runnable() { |
| 7955 | @Override |
| 7956 | public void run() { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 7957 | notifyRemoved( |
| 7958 | info, oldSbnLightClone, update, null, REASON_USER_STOPPED); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 7959 | } |
| 7960 | }); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7961 | continue; |
| 7962 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 7963 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 7964 | // Grant access before listener is notified |
| 7965 | final int targetUserId = (info.userid == UserHandle.USER_ALL) |
| 7966 | ? UserHandle.USER_SYSTEM : info.userid; |
| 7967 | updateUriPermissions(r, old, info.component.getPackageName(), targetUserId); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 7968 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 7969 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7970 | mHandler.post(new Runnable() { |
| 7971 | @Override |
| 7972 | public void run() { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 7973 | notifyPosted(info, sbnToPost, update); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7974 | } |
| 7975 | }); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 7976 | } |
| 7977 | } |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 7978 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7979 | /** |
| 7980 | * asynchronously notify all listeners about a removed notification |
| 7981 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 7982 | @GuardedBy("mNotificationLock") |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7983 | public void notifyRemovedLocked(NotificationRecord r, int reason, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 7984 | NotificationStats notificationStats) { |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7985 | final StatusBarNotification sbn = r.sbn; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 7986 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7987 | // make a copy in case changes are made to the underlying Notification object |
| 7988 | // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the |
| 7989 | // notification |
| 7990 | final StatusBarNotification sbnLight = sbn.cloneLight(); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 7991 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 7992 | if (!isVisibleToListener(sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7993 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 7994 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7995 | |
| 7996 | // don't notifyRemoved for listeners targeting < P |
| 7997 | // if not for reason package suspended |
| 7998 | if (r.isHidden() && reason != REASON_PACKAGE_SUSPENDED |
| 7999 | && info.targetSdkVersion < Build.VERSION_CODES.P) { |
| 8000 | continue; |
| 8001 | } |
| 8002 | |
| 8003 | // don't notifyRemoved for listeners targeting >= P |
| 8004 | // if the reason is package suspended |
| 8005 | if (reason == REASON_PACKAGE_SUSPENDED |
| 8006 | && info.targetSdkVersion >= Build.VERSION_CODES.P) { |
| 8007 | continue; |
| 8008 | } |
| 8009 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 8010 | // Only assistants can get stats |
| 8011 | final NotificationStats stats = mAssistants.isServiceTokenValidLocked(info.service) |
| 8012 | ? notificationStats : null; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 8013 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 8014 | mHandler.post(new Runnable() { |
| 8015 | @Override |
| 8016 | public void run() { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 8017 | notifyRemoved(info, sbnLight, update, stats, reason); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 8018 | } |
| 8019 | }); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 8020 | } |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 8021 | |
| 8022 | // Revoke access after all listeners have been updated |
| 8023 | mHandler.post(() -> { |
| 8024 | updateUriPermissions(null, r, null, UserHandle.USER_SYSTEM); |
| 8025 | }); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 8026 | } |
| 8027 | |
| 8028 | /** |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 8029 | * Asynchronously notify all listeners about a reordering of notifications |
| 8030 | * unless changedHiddenNotifications is populated. |
| 8031 | * If changedHiddenNotifications is populated, there was a change in the hidden state |
| 8032 | * of the notifications. In this case, we only send updates to listeners that |
| 8033 | * target >= P. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 8034 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 8035 | @GuardedBy("mNotificationLock") |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 8036 | public void notifyRankingUpdateLocked(List<NotificationRecord> changedHiddenNotifications) { |
| 8037 | boolean isHiddenRankingUpdate = changedHiddenNotifications != null |
| 8038 | && changedHiddenNotifications.size() > 0; |
| 8039 | |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 8040 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 8041 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 8042 | continue; |
| 8043 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 8044 | |
| 8045 | boolean notifyThisListener = false; |
| 8046 | if (isHiddenRankingUpdate && serviceInfo.targetSdkVersion >= |
| 8047 | Build.VERSION_CODES.P) { |
| 8048 | for (NotificationRecord rec : changedHiddenNotifications) { |
| 8049 | if (isVisibleToListener(rec.sbn, serviceInfo)) { |
| 8050 | notifyThisListener = true; |
| 8051 | break; |
| 8052 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 8053 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 8054 | } |
| 8055 | |
| 8056 | if (notifyThisListener || !isHiddenRankingUpdate) { |
| 8057 | final NotificationRankingUpdate update = makeRankingUpdateLocked( |
| 8058 | serviceInfo); |
| 8059 | |
| 8060 | mHandler.post(new Runnable() { |
| 8061 | @Override |
| 8062 | public void run() { |
| 8063 | notifyRankingUpdate(serviceInfo, update); |
| 8064 | } |
| 8065 | }); |
| 8066 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 8067 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 8068 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 8069 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 8070 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 8071 | public void notifyListenerHintsChangedLocked(final int hints) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 8072 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 8073 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 8074 | continue; |
| 8075 | } |
| 8076 | mHandler.post(new Runnable() { |
| 8077 | @Override |
| 8078 | public void run() { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 8079 | notifyListenerHintsChanged(serviceInfo, hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 8080 | } |
| 8081 | }); |
| 8082 | } |
| 8083 | } |
| 8084 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 8085 | /** |
| 8086 | * asynchronously notify relevant listeners their notification is hidden |
| 8087 | * NotificationListenerServices that target P+: |
| 8088 | * NotificationListenerService#notifyRankingUpdateLocked() |
| 8089 | * NotificationListenerServices that target <= P: |
| 8090 | * NotificationListenerService#notifyRemovedLocked() with REASON_PACKAGE_SUSPENDED. |
| 8091 | */ |
| 8092 | @GuardedBy("mNotificationLock") |
| 8093 | public void notifyHiddenLocked(List<NotificationRecord> changedNotifications) { |
| 8094 | if (changedNotifications == null || changedNotifications.size() == 0) { |
| 8095 | return; |
| 8096 | } |
| 8097 | |
| 8098 | notifyRankingUpdateLocked(changedNotifications); |
| 8099 | |
| 8100 | // for listeners that target < P, notifyRemoveLocked |
| 8101 | int numChangedNotifications = changedNotifications.size(); |
| 8102 | for (int i = 0; i < numChangedNotifications; i++) { |
| 8103 | NotificationRecord rec = changedNotifications.get(i); |
| 8104 | mListeners.notifyRemovedLocked(rec, REASON_PACKAGE_SUSPENDED, rec.getStats()); |
| 8105 | } |
| 8106 | } |
| 8107 | |
| 8108 | /** |
| 8109 | * asynchronously notify relevant listeners their notification is unhidden |
| 8110 | * NotificationListenerServices that target P+: |
| 8111 | * NotificationListenerService#notifyRankingUpdateLocked() |
| 8112 | * NotificationListenerServices that target <= P: |
| 8113 | * NotificationListeners#notifyPostedLocked() |
| 8114 | */ |
| 8115 | @GuardedBy("mNotificationLock") |
| 8116 | public void notifyUnhiddenLocked(List<NotificationRecord> changedNotifications) { |
| 8117 | if (changedNotifications == null || changedNotifications.size() == 0) { |
| 8118 | return; |
| 8119 | } |
| 8120 | |
| 8121 | notifyRankingUpdateLocked(changedNotifications); |
| 8122 | |
| 8123 | // for listeners that target < P, notifyPostedLocked |
| 8124 | int numChangedNotifications = changedNotifications.size(); |
| 8125 | for (int i = 0; i < numChangedNotifications; i++) { |
| 8126 | NotificationRecord rec = changedNotifications.get(i); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 8127 | mListeners.notifyPostedLocked(rec, rec, false); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 8128 | } |
| 8129 | } |
| 8130 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 8131 | public void notifyInterruptionFilterChanged(final int interruptionFilter) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 8132 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 8133 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 8134 | continue; |
| 8135 | } |
| 8136 | mHandler.post(new Runnable() { |
| 8137 | @Override |
| 8138 | public void run() { |
| 8139 | notifyInterruptionFilterChanged(serviceInfo, interruptionFilter); |
| 8140 | } |
| 8141 | }); |
| 8142 | } |
| 8143 | } |
| 8144 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 8145 | protected void notifyNotificationChannelChanged(final String pkg, final UserHandle user, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 8146 | final NotificationChannel channel, final int modificationType) { |
| 8147 | if (channel == null) { |
| 8148 | return; |
| 8149 | } |
| 8150 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 8151 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 8152 | continue; |
| 8153 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 8154 | |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 8155 | BackgroundThread.getHandler().post(() -> { |
| 8156 | if (hasCompanionDevice(serviceInfo)) { |
| 8157 | notifyNotificationChannelChanged( |
| 8158 | serviceInfo, pkg, user, channel, modificationType); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 8159 | } |
| 8160 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 8161 | } |
| 8162 | } |
| 8163 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 8164 | protected void notifyNotificationChannelGroupChanged( |
| 8165 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
| 8166 | final int modificationType) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 8167 | if (group == null) { |
| 8168 | return; |
| 8169 | } |
| 8170 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 8171 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 8172 | continue; |
| 8173 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 8174 | |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 8175 | BackgroundThread.getHandler().post(() -> { |
| 8176 | if (hasCompanionDevice(serviceInfo)) { |
| 8177 | notifyNotificationChannelGroupChanged( |
| 8178 | serviceInfo, pkg, user, group, modificationType); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 8179 | } |
| 8180 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 8181 | } |
| 8182 | } |
| 8183 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 8184 | private void notifyPosted(final ManagedServiceInfo info, |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 8185 | final StatusBarNotification sbn, NotificationRankingUpdate rankingUpdate) { |
Julia Reynolds | a75c752 | 2017-03-21 17:34:25 -0400 | [diff] [blame] | 8186 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 8187 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 8188 | try { |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 8189 | listener.onNotificationPosted(sbnHolder, rankingUpdate); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 8190 | } catch (RemoteException ex) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 8191 | Slog.e(TAG, "unable to notify listener (posted): " + listener, ex); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 8192 | } |
| 8193 | } |
| 8194 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 8195 | private void notifyRemoved(ManagedServiceInfo info, StatusBarNotification sbn, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 8196 | NotificationRankingUpdate rankingUpdate, NotificationStats stats, int reason) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 8197 | if (!info.enabledAndUserMatches(sbn.getUserId())) { |
| 8198 | return; |
| 8199 | } |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 8200 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 8201 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 8202 | try { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 8203 | listener.onNotificationRemoved(sbnHolder, rankingUpdate, stats, reason); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 8204 | } catch (RemoteException ex) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 8205 | Slog.e(TAG, "unable to notify listener (removed): " + listener, ex); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 8206 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 8207 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 8208 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 8209 | private void notifyRankingUpdate(ManagedServiceInfo info, |
| 8210 | NotificationRankingUpdate rankingUpdate) { |
| 8211 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 8212 | try { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 8213 | listener.onNotificationRankingUpdate(rankingUpdate); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 8214 | } catch (RemoteException ex) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 8215 | Slog.e(TAG, "unable to notify listener (ranking update): " + listener, ex); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 8216 | } |
| 8217 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 8218 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 8219 | private void notifyListenerHintsChanged(ManagedServiceInfo info, int hints) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 8220 | final INotificationListener listener = (INotificationListener) info.service; |
| 8221 | try { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 8222 | listener.onListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 8223 | } catch (RemoteException ex) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 8224 | Slog.e(TAG, "unable to notify listener (listener hints): " + listener, ex); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 8225 | } |
| 8226 | } |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 8227 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 8228 | private void notifyInterruptionFilterChanged(ManagedServiceInfo info, |
| 8229 | int interruptionFilter) { |
| 8230 | final INotificationListener listener = (INotificationListener) info.service; |
| 8231 | try { |
| 8232 | listener.onInterruptionFilterChanged(interruptionFilter); |
| 8233 | } catch (RemoteException ex) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 8234 | Slog.e(TAG, "unable to notify listener (interruption filter): " + listener, ex); |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 8235 | } |
| 8236 | } |
| 8237 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 8238 | void notifyNotificationChannelChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 8239 | final String pkg, final UserHandle user, final NotificationChannel channel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 8240 | final int modificationType) { |
| 8241 | final INotificationListener listener = (INotificationListener) info.service; |
| 8242 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 8243 | listener.onNotificationChannelModification(pkg, user, channel, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 8244 | } catch (RemoteException ex) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 8245 | Slog.e(TAG, "unable to notify listener (channel changed): " + listener, ex); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 8246 | } |
| 8247 | } |
| 8248 | |
| 8249 | private void notifyNotificationChannelGroupChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 8250 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 8251 | final int modificationType) { |
| 8252 | final INotificationListener listener = (INotificationListener) info.service; |
| 8253 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 8254 | listener.onNotificationChannelGroupModification(pkg, user, group, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 8255 | } catch (RemoteException ex) { |
Tony Mak | 180a9c4 | 2019-03-08 13:33:08 +0000 | [diff] [blame] | 8256 | Slog.e(TAG, "unable to notify listener (channel group changed): " + listener, ex); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 8257 | } |
| 8258 | } |
| 8259 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 8260 | public boolean isListenerPackage(String packageName) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 8261 | if (packageName == null) { |
| 8262 | return false; |
| 8263 | } |
| 8264 | // TODO: clean up locking object later |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 8265 | synchronized (mNotificationLock) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 8266 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 8267 | if (packageName.equals(serviceInfo.component.getPackageName())) { |
| 8268 | return true; |
| 8269 | } |
| 8270 | } |
| 8271 | } |
| 8272 | return false; |
| 8273 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 8274 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 8275 | |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 8276 | class RoleObserver implements OnRoleHoldersChangedListener { |
| 8277 | // Role name : user id : list of approved packages |
| 8278 | private ArrayMap<String, ArrayMap<Integer, ArraySet<String>>> mNonBlockableDefaultApps; |
| 8279 | |
| 8280 | private final RoleManager mRm; |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 8281 | private final IPackageManager mPm; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 8282 | private final Executor mExecutor; |
| 8283 | |
| 8284 | RoleObserver(@NonNull RoleManager roleManager, |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 8285 | @NonNull IPackageManager pkgMgr, |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 8286 | @NonNull @CallbackExecutor Executor executor) { |
| 8287 | mRm = roleManager; |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 8288 | mPm = pkgMgr; |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 8289 | mExecutor = executor; |
| 8290 | } |
| 8291 | |
| 8292 | public void init() { |
| 8293 | List<UserInfo> users = mUm.getUsers(); |
| 8294 | mNonBlockableDefaultApps = new ArrayMap<>(); |
| 8295 | for (int i = 0; i < NON_BLOCKABLE_DEFAULT_ROLES.length; i++) { |
| 8296 | final ArrayMap<Integer, ArraySet<String>> userToApprovedList = new ArrayMap<>(); |
| 8297 | mNonBlockableDefaultApps.put(NON_BLOCKABLE_DEFAULT_ROLES[i], userToApprovedList); |
| 8298 | for (int j = 0; j < users.size(); j++) { |
| 8299 | Integer userId = users.get(j).getUserHandle().getIdentifier(); |
| 8300 | ArraySet<String> approvedForUserId = new ArraySet<>(mRm.getRoleHoldersAsUser( |
| 8301 | NON_BLOCKABLE_DEFAULT_ROLES[i], UserHandle.of(userId))); |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 8302 | ArraySet<Pair<String, Integer>> approvedAppUids = new ArraySet<>(); |
| 8303 | for (String pkg : approvedForUserId) { |
| 8304 | approvedAppUids.add(new Pair(pkg, getUidForPackage(pkg, userId))); |
| 8305 | } |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 8306 | userToApprovedList.put(userId, approvedForUserId); |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 8307 | mPreferencesHelper.updateDefaultApps(userId, null, approvedAppUids); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 8308 | } |
| 8309 | } |
| 8310 | |
| 8311 | mRm.addOnRoleHoldersChangedListenerAsUser(mExecutor, this, UserHandle.ALL); |
| 8312 | } |
| 8313 | |
| 8314 | @VisibleForTesting |
| 8315 | public boolean isApprovedPackageForRoleForUser(String role, String pkg, int userId) { |
| 8316 | return mNonBlockableDefaultApps.get(role).get(userId).contains(pkg); |
| 8317 | } |
| 8318 | |
| 8319 | /** |
| 8320 | * Convert the assistant-role holder into settings. The rest of the system uses the |
| 8321 | * settings. |
| 8322 | * |
| 8323 | * @param roleName the name of the role whose holders are changed |
| 8324 | * @param user the user for this role holder change |
| 8325 | */ |
| 8326 | @Override |
| 8327 | public void onRoleHoldersChanged(@NonNull String roleName, @NonNull UserHandle user) { |
| 8328 | // we only care about a couple of the roles they'll tell us about |
| 8329 | boolean relevantChange = false; |
| 8330 | for (int i = 0; i < NON_BLOCKABLE_DEFAULT_ROLES.length; i++) { |
| 8331 | if (NON_BLOCKABLE_DEFAULT_ROLES[i].equals(roleName)) { |
| 8332 | relevantChange = true; |
| 8333 | break; |
| 8334 | } |
| 8335 | } |
| 8336 | |
| 8337 | if (!relevantChange) { |
| 8338 | return; |
| 8339 | } |
| 8340 | |
| 8341 | ArraySet<String> roleHolders = new ArraySet<>(mRm.getRoleHoldersAsUser(roleName, user)); |
| 8342 | |
| 8343 | // find the diff |
| 8344 | ArrayMap<Integer, ArraySet<String>> prevApprovedForRole = |
| 8345 | mNonBlockableDefaultApps.getOrDefault(roleName, new ArrayMap<>()); |
| 8346 | ArraySet<String> previouslyApproved = |
| 8347 | prevApprovedForRole.getOrDefault(user.getIdentifier(), new ArraySet<>()); |
| 8348 | |
| 8349 | ArraySet<String> toRemove = new ArraySet<>(); |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 8350 | ArraySet<Pair<String, Integer>> toAdd = new ArraySet<>(); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 8351 | |
| 8352 | for (String previous : previouslyApproved) { |
| 8353 | if (!roleHolders.contains(previous)) { |
| 8354 | toRemove.add(previous); |
| 8355 | } |
| 8356 | } |
| 8357 | for (String nowApproved : roleHolders) { |
| 8358 | if (!previouslyApproved.contains(nowApproved)) { |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 8359 | toAdd.add(new Pair(nowApproved, |
| 8360 | getUidForPackage(nowApproved, user.getIdentifier()))); |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 8361 | } |
| 8362 | } |
| 8363 | |
| 8364 | // store newly approved apps |
| 8365 | prevApprovedForRole.put(user.getIdentifier(), roleHolders); |
| 8366 | mNonBlockableDefaultApps.put(roleName, prevApprovedForRole); |
| 8367 | |
| 8368 | // update what apps can be blocked |
| 8369 | mPreferencesHelper.updateDefaultApps(user.getIdentifier(), toRemove, toAdd); |
| 8370 | |
| 8371 | // RoleManager is the source of truth for this data so we don't need to trigger a |
| 8372 | // write of the notification policy xml for this change |
| 8373 | } |
Julia Reynolds | e7ca31b | 2019-04-25 15:41:47 -0400 | [diff] [blame] | 8374 | |
| 8375 | private int getUidForPackage(String pkg, int userId) { |
| 8376 | try { |
| 8377 | return mPm.getPackageUid(pkg, MATCH_ALL, userId); |
| 8378 | } catch (RemoteException e) { |
| 8379 | Slog.e(TAG, "role manager has bad default " + pkg + " " + userId); |
| 8380 | } |
| 8381 | return -1; |
| 8382 | } |
Julia Reynolds | 0c24500 | 2019-03-27 16:10:11 -0400 | [diff] [blame] | 8383 | } |
| 8384 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 8385 | public static final class DumpFilter { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 8386 | public boolean filtered = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 8387 | public String pkgFilter; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 8388 | public boolean zen; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 8389 | public long since; |
| 8390 | public boolean stats; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 8391 | public boolean redact = true; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 8392 | public boolean proto = false; |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 8393 | public boolean criticalPriority = false; |
| 8394 | public boolean normalPriority = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 8395 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 8396 | @NonNull |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 8397 | public static DumpFilter parseFromArguments(String[] args) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 8398 | final DumpFilter filter = new DumpFilter(); |
| 8399 | for (int ai = 0; ai < args.length; ai++) { |
| 8400 | final String a = args[ai]; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 8401 | if ("--proto".equals(a)) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 8402 | filter.proto = true; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 8403 | } else if ("--noredact".equals(a) || "--reveal".equals(a)) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 8404 | filter.redact = false; |
| 8405 | } else if ("p".equals(a) || "pkg".equals(a) || "--package".equals(a)) { |
| 8406 | if (ai < args.length-1) { |
| 8407 | ai++; |
| 8408 | filter.pkgFilter = args[ai].trim().toLowerCase(); |
| 8409 | if (filter.pkgFilter.isEmpty()) { |
| 8410 | filter.pkgFilter = null; |
| 8411 | } else { |
| 8412 | filter.filtered = true; |
| 8413 | } |
| 8414 | } |
| 8415 | } else if ("--zen".equals(a) || "zen".equals(a)) { |
| 8416 | filter.filtered = true; |
| 8417 | filter.zen = true; |
| 8418 | } else if ("--stats".equals(a)) { |
| 8419 | filter.stats = true; |
| 8420 | if (ai < args.length-1) { |
| 8421 | ai++; |
Tobias Thierer | 28532d0 | 2016-04-21 14:52:10 +0100 | [diff] [blame] | 8422 | filter.since = Long.parseLong(args[ai]); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 8423 | } else { |
| 8424 | filter.since = 0; |
| 8425 | } |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 8426 | } else if (PRIORITY_ARG.equals(a)) { |
| 8427 | // Bugreport will call the service twice with priority arguments, first to dump |
| 8428 | // critical sections and then non critical ones. Set approriate filters |
| 8429 | // to generate the desired data. |
| 8430 | if (ai < args.length - 1) { |
| 8431 | ai++; |
| 8432 | switch (args[ai]) { |
| 8433 | case PRIORITY_ARG_CRITICAL: |
| 8434 | filter.criticalPriority = true; |
| 8435 | break; |
| 8436 | case PRIORITY_ARG_NORMAL: |
| 8437 | filter.normalPriority = true; |
| 8438 | break; |
| 8439 | } |
| 8440 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 8441 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 8442 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 8443 | return filter; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 8444 | } |
| 8445 | |
| 8446 | public boolean matches(StatusBarNotification sbn) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 8447 | if (!filtered) return true; |
| 8448 | return zen ? true : sbn != null |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 8449 | && (matches(sbn.getPackageName()) || matches(sbn.getOpPkg())); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 8450 | } |
| 8451 | |
| 8452 | public boolean matches(ComponentName component) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 8453 | if (!filtered) return true; |
| 8454 | return zen ? true : component != null && matches(component.getPackageName()); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 8455 | } |
| 8456 | |
| 8457 | public boolean matches(String pkg) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 8458 | if (!filtered) return true; |
| 8459 | return zen ? true : pkg != null && pkg.toLowerCase().contains(pkgFilter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 8460 | } |
| 8461 | |
| 8462 | @Override |
| 8463 | public String toString() { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 8464 | return stats ? "stats" : zen ? "zen" : ('\'' + pkgFilter + '\''); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 8465 | } |
| 8466 | } |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 8467 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 8468 | @VisibleForTesting |
Tony Mak | 9a3c1f1 | 2019-03-04 16:04:42 +0000 | [diff] [blame] | 8469 | void resetAssistantUserSet(int userId) { |
| 8470 | mAssistants.setUserSet(userId, false); |
| 8471 | handleSavePolicyFile(); |
| 8472 | } |
| 8473 | |
| 8474 | @VisibleForTesting |
| 8475 | @Nullable |
| 8476 | ComponentName getApprovedAssistant(int userId) { |
| 8477 | List<ComponentName> allowedComponents = mAssistants.getAllowedComponents(userId); |
| 8478 | return CollectionUtils.firstOrNull(allowedComponents); |
| 8479 | } |
| 8480 | |
| 8481 | @VisibleForTesting |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 8482 | protected void simulatePackageSuspendBroadcast(boolean suspend, String pkg) { |
| 8483 | // only use for testing: mimic receive broadcast that package is (un)suspended |
| 8484 | // but does not actually (un)suspend the package |
| 8485 | final Bundle extras = new Bundle(); |
| 8486 | extras.putStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST, |
| 8487 | new String[]{pkg}); |
| 8488 | |
| 8489 | final String action = suspend ? Intent.ACTION_PACKAGES_SUSPENDED |
Beverly | 3c707b4 | 2018-09-14 09:49:07 -0400 | [diff] [blame] | 8490 | : Intent.ACTION_PACKAGES_UNSUSPENDED; |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 8491 | final Intent intent = new Intent(action); |
| 8492 | intent.putExtras(extras); |
| 8493 | |
| 8494 | mPackageIntentReceiver.onReceive(getContext(), intent); |
| 8495 | } |
| 8496 | |
Julia Reynolds | 0e5a343 | 2019-01-17 09:40:46 -0500 | [diff] [blame] | 8497 | @VisibleForTesting |
| 8498 | protected void simulatePackageDistractionBroadcast(int flag, String[] pkgs) { |
| 8499 | // only use for testing: mimic receive broadcast that package is (un)distracting |
| 8500 | // but does not actually register that info with packagemanager |
| 8501 | final Bundle extras = new Bundle(); |
| 8502 | extras.putStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST, pkgs); |
| 8503 | extras.putInt(Intent.EXTRA_DISTRACTION_RESTRICTIONS, flag); |
| 8504 | |
| 8505 | final Intent intent = new Intent(Intent.ACTION_DISTRACTING_PACKAGES_CHANGED); |
| 8506 | intent.putExtras(extras); |
| 8507 | |
| 8508 | mPackageIntentReceiver.onReceive(getContext(), intent); |
| 8509 | } |
| 8510 | |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 8511 | /** |
| 8512 | * Wrapper for a StatusBarNotification object that allows transfer across a oneway |
| 8513 | * binder without sending large amounts of data over a oneway transaction. |
| 8514 | */ |
| 8515 | private static final class StatusBarNotificationHolder |
| 8516 | extends IStatusBarNotificationHolder.Stub { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 8517 | private StatusBarNotification mValue; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 8518 | |
| 8519 | public StatusBarNotificationHolder(StatusBarNotification value) { |
| 8520 | mValue = value; |
| 8521 | } |
| 8522 | |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 8523 | /** 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] | 8524 | @Override |
| 8525 | public StatusBarNotification get() { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 8526 | StatusBarNotification value = mValue; |
| 8527 | mValue = null; |
| 8528 | return value; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 8529 | } |
| 8530 | } |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 8531 | |
Zimuzo | b3b9c26 | 2018-10-31 11:54:20 +0000 | [diff] [blame] | 8532 | private void writeSecureNotificationsPolicy(XmlSerializer out) throws IOException { |
| 8533 | out.startTag(null, LOCKSCREEN_ALLOW_SECURE_NOTIFICATIONS_TAG); |
| 8534 | out.attribute(null, LOCKSCREEN_ALLOW_SECURE_NOTIFICATIONS_VALUE, |
| 8535 | Boolean.toString(mLockScreenAllowSecureNotifications)); |
| 8536 | out.endTag(null, LOCKSCREEN_ALLOW_SECURE_NOTIFICATIONS_TAG); |
| 8537 | } |
| 8538 | |
| 8539 | private static boolean safeBoolean(String val, boolean defValue) { |
| 8540 | if (TextUtils.isEmpty(val)) return defValue; |
| 8541 | return Boolean.parseBoolean(val); |
| 8542 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8543 | } |