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; |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 20 | import static android.app.Notification.FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 21 | import static android.app.NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 22 | import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED; |
| 23 | import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 24 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 25 | import static android.app.NotificationManager.IMPORTANCE_MIN; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 26 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 27 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECTS_UNSET; |
| 28 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT; |
| 29 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE; |
| 30 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 31 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS; |
| 32 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST; |
| 33 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK; |
| 34 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF; |
| 35 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON; |
| 36 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_STATUS_BAR; |
Amith Yamasani | e5bfeee | 2018-09-05 18:52:35 -0700 | [diff] [blame] | 37 | import static android.content.Context.BIND_ADJUST_BELOW_PERCEPTIBLE; |
| 38 | import static android.content.Context.BIND_ALLOW_WHITELIST_MANAGEMENT; |
| 39 | import static android.content.Context.BIND_AUTO_CREATE; |
| 40 | import static android.content.Context.BIND_FOREGROUND_SERVICE; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 41 | import static android.content.pm.PackageManager.FEATURE_LEANBACK; |
| 42 | import static android.content.pm.PackageManager.FEATURE_TELEVISION; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 43 | import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_AWARE; |
| 44 | import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE; |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 45 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 46 | import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_CRITICAL; |
| 47 | import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_NORMAL; |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 48 | import static android.os.UserHandle.USER_NULL; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 49 | import static android.os.UserHandle.USER_SYSTEM; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 50 | import static android.service.notification.NotificationListenerService |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 51 | .HINT_HOST_DISABLE_CALL_EFFECTS; |
| 52 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_EFFECTS; |
| 53 | import static android.service.notification.NotificationListenerService |
| 54 | .HINT_HOST_DISABLE_NOTIFICATION_EFFECTS; |
| 55 | import static android.service.notification.NotificationListenerService |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 56 | .NOTIFICATION_CHANNEL_OR_GROUP_ADDED; |
| 57 | import static android.service.notification.NotificationListenerService |
| 58 | .NOTIFICATION_CHANNEL_OR_GROUP_DELETED; |
| 59 | import static android.service.notification.NotificationListenerService |
| 60 | .NOTIFICATION_CHANNEL_OR_GROUP_UPDATED; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 61 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL; |
| 62 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL; |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 63 | import static android.service.notification.NotificationListenerService.REASON_CANCEL; |
| 64 | import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 65 | import static android.service.notification.NotificationListenerService.REASON_CHANNEL_BANNED; |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 66 | import static android.service.notification.NotificationListenerService.REASON_CLICK; |
| 67 | import static android.service.notification.NotificationListenerService.REASON_ERROR; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 68 | import static android.service.notification.NotificationListenerService |
| 69 | .REASON_GROUP_SUMMARY_CANCELED; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 70 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL; |
| 71 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL_ALL; |
| 72 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_BANNED; |
| 73 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_CHANGED; |
| 74 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_SUSPENDED; |
| 75 | import static android.service.notification.NotificationListenerService.REASON_PROFILE_TURNED_OFF; |
| 76 | import static android.service.notification.NotificationListenerService.REASON_SNOOZED; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 77 | import static android.service.notification.NotificationListenerService.REASON_TIMEOUT; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 78 | import static android.service.notification.NotificationListenerService.REASON_UNAUTOBUNDLED; |
| 79 | import static android.service.notification.NotificationListenerService.REASON_USER_STOPPED; |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 80 | import static android.service.notification.NotificationListenerService.TRIM_FULL; |
| 81 | import static android.service.notification.NotificationListenerService.TRIM_LIGHT; |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 82 | import static android.view.WindowManager.LayoutParams.TYPE_TOAST; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 83 | |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 84 | import static com.android.server.utils.PriorityDump.PRIORITY_ARG; |
| 85 | import static com.android.server.utils.PriorityDump.PRIORITY_ARG_CRITICAL; |
| 86 | import static com.android.server.utils.PriorityDump.PRIORITY_ARG_NORMAL; |
| 87 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 88 | import android.Manifest; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 89 | import android.annotation.NonNull; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 90 | import android.annotation.Nullable; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 91 | import android.app.ActivityManager; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 92 | import android.app.ActivityManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 93 | import android.app.AlarmManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 94 | import android.app.AppGlobals; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 95 | import android.app.AppOpsManager; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 96 | import android.app.AutomaticZenRule; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | import android.app.IActivityManager; |
| 98 | import android.app.INotificationManager; |
| 99 | import android.app.ITransientNotification; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 100 | import android.app.IUriGrantsManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | import android.app.Notification; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 102 | import android.app.NotificationChannel; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 103 | import android.app.NotificationChannelGroup; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 104 | import android.app.NotificationManager; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 105 | import android.app.NotificationManager.Policy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | import android.app.PendingIntent; |
| 107 | import android.app.StatusBarManager; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 108 | import android.app.UriGrantsManager; |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 109 | import android.app.admin.DeviceAdminInfo; |
| 110 | import android.app.admin.DevicePolicyManagerInternal; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 111 | import android.app.backup.BackupManager; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 112 | import android.app.usage.UsageEvents; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 113 | import android.app.usage.UsageStatsManagerInternal; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 114 | import android.companion.ICompanionDeviceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | import android.content.BroadcastReceiver; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 116 | import android.content.ComponentName; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 117 | import android.content.ContentProvider; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 118 | import android.content.ContentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | import android.content.Context; |
| 120 | import android.content.Intent; |
| 121 | import android.content.IntentFilter; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 122 | import android.content.pm.ApplicationInfo; |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 123 | import android.content.pm.IPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | import android.content.pm.PackageManager; |
| 125 | import android.content.pm.PackageManager.NameNotFoundException; |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 126 | import android.content.pm.ParceledListSlice; |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 127 | import android.content.pm.UserInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | import android.content.res.Resources; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 129 | import android.database.ContentObserver; |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 130 | import android.media.AudioAttributes; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 131 | import android.media.AudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 132 | import android.media.AudioManagerInternal; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 133 | import android.media.IRingtonePlayer; |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 134 | import android.metrics.LogMaker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | import android.os.Binder; |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 137 | import android.os.Build; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 138 | import android.os.Bundle; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 139 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | import android.os.Handler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 141 | import android.os.HandlerThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | import android.os.IBinder; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 143 | import android.os.IDeviceIdleController; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 144 | import android.os.IInterface; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 145 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | import android.os.Message; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 147 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 148 | import android.os.RemoteException; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 149 | import android.os.ResultReceiver; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 150 | import android.os.ServiceManager; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 151 | import android.os.ShellCallback; |
| 152 | import android.os.ShellCommand; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 153 | import android.os.SystemClock; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 154 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 155 | import android.os.UserHandle; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 156 | import android.os.VibrationEffect; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 157 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | import android.provider.Settings; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 159 | import android.service.notification.Adjustment; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 160 | import android.service.notification.Condition; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 161 | import android.service.notification.IConditionProvider; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 162 | import android.service.notification.INotificationListener; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 163 | import android.service.notification.IStatusBarNotificationHolder; |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 164 | import android.service.notification.ListenersDisablingEffectsProto; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 165 | import android.service.notification.NotificationAssistantService; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 166 | import android.service.notification.NotificationListenerService; |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 167 | import android.service.notification.NotificationRankingUpdate; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 168 | import android.service.notification.NotificationRecordProto; |
| 169 | import android.service.notification.NotificationServiceDumpProto; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 170 | import android.service.notification.NotificationStats; |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 171 | import android.service.notification.NotifyingApp; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 172 | import android.service.notification.SnoozeCriterion; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 173 | import android.service.notification.StatusBarNotification; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 174 | import android.service.notification.ZenModeConfig; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 175 | import android.service.notification.ZenModeProto; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 176 | import android.telephony.PhoneStateListener; |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 177 | import android.telephony.TelephonyManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 178 | import android.text.TextUtils; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 179 | import android.util.ArrayMap; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 180 | import android.util.ArraySet; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 181 | import android.util.AtomicFile; |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 182 | import android.util.IntArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | import android.util.Log; |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 184 | import android.util.Slog; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 185 | import android.util.SparseArray; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 186 | import android.util.Xml; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 187 | import android.util.proto.ProtoOutputStream; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 188 | import android.view.accessibility.AccessibilityEvent; |
| 189 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | import android.widget.Toast; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 191 | |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 192 | import com.android.internal.R; |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 193 | import com.android.internal.annotations.GuardedBy; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 194 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 195 | import com.android.internal.logging.MetricsLogger; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 196 | import com.android.internal.logging.nano.MetricsProto; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 197 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Beverly | 70dcd00 | 2018-03-29 17:09:16 -0400 | [diff] [blame] | 198 | import com.android.internal.notification.SystemNotificationChannels; |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 199 | import com.android.internal.os.BackgroundThread; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 200 | import com.android.internal.statusbar.NotificationVisibility; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 201 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 202 | import com.android.internal.util.DumpUtils; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 203 | import com.android.internal.util.FastXmlSerializer; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 204 | import com.android.internal.util.Preconditions; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 205 | import com.android.internal.util.XmlUtils; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 206 | import com.android.server.DeviceIdleController; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 207 | import com.android.server.EventLogTags; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 208 | import com.android.server.LocalServices; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 209 | import com.android.server.SystemService; |
| 210 | import com.android.server.lights.Light; |
| 211 | import com.android.server.lights.LightsManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 212 | import com.android.server.notification.ManagedServices.ManagedServiceInfo; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 213 | import com.android.server.notification.ManagedServices.UserProfiles; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 214 | import com.android.server.pm.PackageManagerService; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 215 | import com.android.server.policy.PhoneWindowManager; |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 216 | import com.android.server.statusbar.StatusBarManagerInternal; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 217 | import com.android.server.uri.UriGrantsManagerInternal; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 218 | import com.android.server.wm.WindowManagerInternal; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 219 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 220 | import libcore.io.IoUtils; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 221 | |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 222 | import org.json.JSONException; |
| 223 | import org.json.JSONObject; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 224 | import org.xmlpull.v1.XmlPullParser; |
| 225 | import org.xmlpull.v1.XmlPullParserException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 226 | import org.xmlpull.v1.XmlSerializer; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 227 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 228 | import java.io.ByteArrayInputStream; |
| 229 | import java.io.ByteArrayOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 230 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | import java.io.FileDescriptor; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 232 | import java.io.FileNotFoundException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 233 | import java.io.FileOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 234 | import java.io.IOException; |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 235 | import java.io.InputStream; |
| 236 | import java.io.OutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 238 | import java.nio.charset.StandardCharsets; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 239 | import java.util.ArrayDeque; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | import java.util.ArrayList; |
Michael Wright | bc4d0d9 | 2017-03-23 18:57:57 +0000 | [diff] [blame] | 241 | import java.util.Arrays; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 242 | import java.util.Iterator; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 243 | import java.util.List; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 244 | import java.util.Map.Entry; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 245 | import java.util.Objects; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 246 | import java.util.Set; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 247 | import java.util.concurrent.TimeUnit; |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 248 | import java.util.function.Predicate; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 249 | |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 250 | /** {@hide} */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 251 | public class NotificationManagerService extends SystemService { |
| 252 | static final String TAG = "NotificationService"; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 253 | static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Selim Cinek | 4041249 | 2015-12-08 18:03:22 -0800 | [diff] [blame] | 254 | public static final boolean ENABLE_CHILD_NOTIFICATIONS |
| 255 | = SystemProperties.getBoolean("debug.child_notifs", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 257 | static final boolean DEBUG_INTERRUPTIVENESS = SystemProperties.getBoolean( |
| 258 | "debug.notification.interruptiveness", false); |
| 259 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 260 | static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 261 | static final float DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE = 5f; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | // message codes |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 264 | static final int MESSAGE_DURATION_REACHED = 2; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 265 | static final int MESSAGE_SAVE_POLICY_FILE = 3; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 266 | static final int MESSAGE_SEND_RANKING_UPDATE = 4; |
| 267 | static final int MESSAGE_LISTENER_HINTS_CHANGED = 5; |
| 268 | static final int MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED = 6; |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 269 | static final int MESSAGE_FINISH_TOKEN_TIMEOUT = 7; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 270 | |
| 271 | // ranking thread messages |
| 272 | private static final int MESSAGE_RECONSIDER_RANKING = 1000; |
| 273 | private static final int MESSAGE_RANKING_SORT = 1001; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 275 | static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 276 | static final int SHORT_DELAY = 2000; // 2 seconds |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 277 | |
Robert Carr | 3406d46 | 2018-03-15 16:19:07 -0700 | [diff] [blame] | 278 | // 1 second past the ANR timeout. |
| 279 | static final int FINISH_TOKEN_TIMEOUT = 11 * 1000; |
| 280 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 281 | static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 282 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 283 | static final long SNOOZE_UNTIL_UNSPECIFIED = -1; |
| 284 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 285 | 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] | 286 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 287 | static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION; |
Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 288 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 289 | static final boolean ENABLE_BLOCKED_TOASTS = true; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 290 | |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 291 | // When #matchesCallFilter is called from the ringer, wait at most |
| 292 | // 3s to resolve the contacts. This timeout is required since |
| 293 | // ContactsProvider might take a long time to start up. |
| 294 | // |
| 295 | // Return STARRED_CONTACT when the timeout is hit in order to avoid |
| 296 | // missed calls in ZEN mode "Important". |
| 297 | static final int MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS = 3000; |
| 298 | static final float MATCHES_CALL_FILTER_TIMEOUT_AFFINITY = |
| 299 | ValidateNotificationPeople.STARRED_CONTACT; |
| 300 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 301 | /** notification_enqueue status value for a newly enqueued notification. */ |
| 302 | private static final int EVENTLOG_ENQUEUE_STATUS_NEW = 0; |
| 303 | |
| 304 | /** notification_enqueue status value for an existing notification. */ |
| 305 | private static final int EVENTLOG_ENQUEUE_STATUS_UPDATE = 1; |
| 306 | |
| 307 | /** notification_enqueue status value for an ignored notification. */ |
| 308 | private static final int EVENTLOG_ENQUEUE_STATUS_IGNORED = 2; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 309 | private static final long MIN_PACKAGE_OVERRATE_LOG_INTERVAL = 5000; // milliseconds |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 310 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 311 | private static final long DELAY_FOR_ASSISTANT_TIME = 100; |
| 312 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 313 | private static final String ACTION_NOTIFICATION_TIMEOUT = |
| 314 | NotificationManagerService.class.getSimpleName() + ".TIMEOUT"; |
| 315 | private static final int REQUEST_CODE_TIMEOUT = 1; |
| 316 | private static final String SCHEME_TIMEOUT = "timeout"; |
| 317 | private static final String EXTRA_KEY = "key"; |
| 318 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 319 | private IActivityManager mAm; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 320 | private ActivityManager mActivityManager; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 321 | private IPackageManager mPackageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 322 | private PackageManager mPackageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 323 | AudioManager mAudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 324 | AudioManagerInternal mAudioManagerInternal; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 325 | @Nullable StatusBarManagerInternal mStatusBar; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 326 | Vibrator mVibrator; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 327 | private WindowManagerInternal mWindowManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 328 | private AlarmManager mAlarmManager; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 329 | private ICompanionDeviceManager mCompanionManager; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 330 | private AccessibilityManager mAccessibilityManager; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 331 | private IDeviceIdleController mDeviceIdleController; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 332 | private IUriGrantsManager mUgm; |
| 333 | private UriGrantsManagerInternal mUgmInternal; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 334 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | final IBinder mForegroundToken = new Binder(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 336 | private WorkerHandler mHandler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 337 | private final HandlerThread mRankingThread = new HandlerThread("ranker", |
| 338 | Process.THREAD_PRIORITY_BACKGROUND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 340 | private Light mNotificationLight; |
| 341 | Light mAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 342 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 343 | private long[] mFallbackVibrationPattern; |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 344 | private boolean mUseAttentionLight; |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 345 | boolean mHasLight = true; |
| 346 | boolean mLightEnabled; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 347 | boolean mSystemReady; |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 348 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 349 | private boolean mDisableNotificationEffects; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 350 | private int mCallState; |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 351 | private String mSoundNotificationKey; |
| 352 | private String mVibrateNotificationKey; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 354 | private final SparseArray<ArraySet<ComponentName>> mListenersDisablingEffects = |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 355 | new SparseArray<>(); |
| 356 | private List<ComponentName> mEffectsSuppressors = new ArrayList<>(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 357 | private int mListenerHints; // right now, all hints are global |
John Spurlock | 8310410 | 2015-02-12 23:25:12 -0500 | [diff] [blame] | 358 | private int mInterruptionFilter = NotificationListenerService.INTERRUPTION_FILTER_UNKNOWN; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 359 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 360 | // for enabling and disabling notification pulse behavior |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 361 | boolean mScreenOn = true; |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 362 | protected boolean mInCall = false; |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 363 | boolean mNotificationPulseEnabled; |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 364 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 365 | private Uri mInCallNotificationUri; |
| 366 | private AudioAttributes mInCallNotificationAudioAttributes; |
| 367 | private float mInCallNotificationVolume; |
luochaojiang | 50e5273c | 2018-04-16 16:55:03 +0800 | [diff] [blame] | 368 | private Binder mCallNotificationToken = null; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 369 | |
Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 370 | // used as a mutex for access to all active notifications & listeners |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 371 | final Object mNotificationLock = new Object(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 372 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 373 | final ArrayList<NotificationRecord> mNotificationList = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 374 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 375 | final ArrayMap<String, NotificationRecord> mNotificationsByKey = new ArrayMap<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 376 | @GuardedBy("mNotificationLock") |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 377 | final ArrayList<NotificationRecord> mEnqueuedNotifications = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 378 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 379 | final ArrayMap<Integer, ArrayMap<String, String>> mAutobundledSummaries = new ArrayMap<>(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 380 | final ArrayList<ToastRecord> mToastQueue = new ArrayList<>(); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 381 | final ArrayMap<String, NotificationRecord> mSummaryByGroupKey = new ArrayMap<>(); |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 382 | final ArrayMap<Integer, ArrayList<NotifyingApp>> mRecentApps = new ArrayMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 384 | // The last key in this list owns the hardware. |
| 385 | ArrayList<String> mLights = new ArrayList<>(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 386 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 387 | private AppOpsManager mAppOps; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 388 | private UsageStatsManagerInternal mAppUsageStats; |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 389 | private DevicePolicyManagerInternal mDpm; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 390 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 391 | private Archive mArchive; |
| 392 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 393 | // Persistent storage for notification policy |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 394 | private AtomicFile mPolicyFile; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 395 | |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 396 | private static final int DB_VERSION = 1; |
| 397 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 398 | private static final String TAG_NOTIFICATION_POLICY = "notification-policy"; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 399 | private static final String ATTR_VERSION = "version"; |
| 400 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 401 | private RankingHelper mRankingHelper; |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 402 | private PreferencesHelper mPreferencesHelper; |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 403 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 404 | private final UserProfiles mUserProfiles = new UserProfiles(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 405 | private NotificationListeners mListeners; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 406 | private NotificationAssistants mAssistants; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 407 | private ConditionProviders mConditionProviders; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 408 | private NotificationUsageStats mUsageStats; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 409 | |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 410 | private static final int MY_UID = Process.myUid(); |
| 411 | private static final int MY_PID = Process.myPid(); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 412 | private static final IBinder WHITELIST_TOKEN = new Binder(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 413 | private RankingHandler mRankingHandler; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 414 | private long mLastOverRateLogTime; |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 415 | private float mMaxPackageEnqueueRate = DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE; |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 416 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 417 | private SnoozeHelper mSnoozeHelper; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 418 | private GroupHelper mGroupHelper; |
Adora Zhang | 48dd614a8 | 2018-06-25 19:18:41 -0700 | [diff] [blame] | 419 | private int mAutoGroupAtCount; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 420 | private boolean mIsTelevision; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 421 | |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 422 | private MetricsLogger mMetricsLogger; |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 423 | private Predicate<String> mAllowedManagedServicePackages; |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 424 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 425 | private static class Archive { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 426 | final int mBufferSize; |
| 427 | final ArrayDeque<StatusBarNotification> mBuffer; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 428 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 429 | public Archive(int size) { |
| 430 | mBufferSize = size; |
| 431 | mBuffer = new ArrayDeque<StatusBarNotification>(mBufferSize); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 432 | } |
Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 433 | |
Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 434 | public String toString() { |
| 435 | final StringBuilder sb = new StringBuilder(); |
| 436 | final int N = mBuffer.size(); |
| 437 | sb.append("Archive ("); |
| 438 | sb.append(N); |
| 439 | sb.append(" notification"); |
| 440 | sb.append((N==1)?")":"s)"); |
| 441 | return sb.toString(); |
| 442 | } |
| 443 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 444 | public void record(StatusBarNotification nr) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 445 | if (mBuffer.size() == mBufferSize) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 446 | mBuffer.removeFirst(); |
| 447 | } |
Daniel Sandler | 26b81d5 | 2013-05-20 20:56:43 -0400 | [diff] [blame] | 448 | |
| 449 | // We don't want to store the heavy bits of the notification in the archive, |
| 450 | // but other clients in the system process might be using the object, so we |
| 451 | // store a (lightened) copy. |
| 452 | mBuffer.addLast(nr.cloneLight()); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 453 | } |
| 454 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 455 | public Iterator<StatusBarNotification> descendingIterator() { |
| 456 | return mBuffer.descendingIterator(); |
| 457 | } |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 458 | |
| 459 | public StatusBarNotification[] getArray(int count) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 460 | if (count == 0) count = mBufferSize; |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 461 | final StatusBarNotification[] a |
| 462 | = new StatusBarNotification[Math.min(count, mBuffer.size())]; |
| 463 | Iterator<StatusBarNotification> iter = descendingIterator(); |
| 464 | int i=0; |
| 465 | while (iter.hasNext() && i < count) { |
| 466 | a[i++] = iter.next(); |
| 467 | } |
| 468 | return a; |
| 469 | } |
| 470 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 471 | } |
| 472 | |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 473 | protected void readDefaultApprovedServices(int userId) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 474 | String defaultListenerAccess = getContext().getResources().getString( |
| 475 | com.android.internal.R.string.config_defaultListenerAccessPackages); |
| 476 | if (defaultListenerAccess != null) { |
| 477 | for (String whitelisted : |
| 478 | defaultListenerAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR)) { |
| 479 | // Gather all notification listener components for candidate pkgs. |
| 480 | Set<ComponentName> approvedListeners = |
| 481 | mListeners.queryPackageForServices(whitelisted, |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 482 | MATCH_DIRECT_BOOT_AWARE |
| 483 | | MATCH_DIRECT_BOOT_UNAWARE, userId); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 484 | for (ComponentName cn : approvedListeners) { |
| 485 | try { |
| 486 | getBinderService().setNotificationListenerAccessGrantedForUser(cn, |
| 487 | userId, true); |
| 488 | } catch (RemoteException e) { |
| 489 | e.printStackTrace(); |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 494 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 495 | String defaultDndAccess = getContext().getResources().getString( |
| 496 | com.android.internal.R.string.config_defaultDndAccessPackages); |
Edward Savage-Jones | 36a8942 | 2018-07-19 12:23:58 +0200 | [diff] [blame] | 497 | if (defaultDndAccess != null) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 498 | for (String whitelisted : |
| 499 | defaultDndAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR)) { |
| 500 | try { |
| 501 | getBinderService().setNotificationPolicyAccessGranted(whitelisted, true); |
| 502 | } catch (RemoteException e) { |
| 503 | e.printStackTrace(); |
| 504 | } |
| 505 | } |
| 506 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 507 | |
| 508 | readDefaultAssistant(userId); |
| 509 | } |
| 510 | |
| 511 | protected void readDefaultAssistant(int userId) { |
| 512 | String defaultAssistantAccess = getContext().getResources().getString( |
| 513 | com.android.internal.R.string.config_defaultAssistantAccessPackage); |
| 514 | if (defaultAssistantAccess != null) { |
| 515 | // Gather all notification assistant components for candidate pkg. There should |
| 516 | // only be one |
| 517 | Set<ComponentName> approvedAssistants = |
| 518 | mAssistants.queryPackageForServices(defaultAssistantAccess, |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 519 | MATCH_DIRECT_BOOT_AWARE |
| 520 | | MATCH_DIRECT_BOOT_UNAWARE, userId); |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 521 | for (ComponentName cn : approvedAssistants) { |
| 522 | try { |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 523 | getBinderService().setNotificationAssistantAccessGrantedForUser( |
| 524 | cn, userId, true); |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 525 | } catch (RemoteException e) { |
| 526 | e.printStackTrace(); |
| 527 | } |
| 528 | } |
| 529 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 530 | } |
| 531 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 532 | void readPolicyXml(InputStream stream, boolean forRestore) |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 533 | throws XmlPullParserException, NumberFormatException, IOException { |
| 534 | final XmlPullParser parser = Xml.newPullParser(); |
| 535 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 536 | XmlUtils.beginDocument(parser, TAG_NOTIFICATION_POLICY); |
| 537 | boolean migratedManagedServices = false; |
| 538 | int outerDepth = parser.getDepth(); |
| 539 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 540 | if (ZenModeConfig.ZEN_TAG.equals(parser.getName())) { |
| 541 | mZenModeHelper.readXml(parser, forRestore); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 542 | } else if (PreferencesHelper.TAG_RANKING.equals(parser.getName())){ |
| 543 | mPreferencesHelper.readXml(parser, forRestore); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 544 | } |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 545 | if (mListeners.getConfig().xmlTag.equals(parser.getName())) { |
| 546 | mListeners.readXml(parser, mAllowedManagedServicePackages); |
| 547 | migratedManagedServices = true; |
| 548 | } else if (mAssistants.getConfig().xmlTag.equals(parser.getName())) { |
| 549 | mAssistants.readXml(parser, mAllowedManagedServicePackages); |
| 550 | migratedManagedServices = true; |
| 551 | } else if (mConditionProviders.getConfig().xmlTag.equals(parser.getName())) { |
| 552 | mConditionProviders.readXml(parser, mAllowedManagedServicePackages); |
| 553 | migratedManagedServices = true; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 554 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 555 | } |
| 556 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 557 | if (!migratedManagedServices) { |
| 558 | mListeners.migrateToXml(); |
| 559 | mAssistants.migrateToXml(); |
| 560 | mConditionProviders.migrateToXml(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 561 | savePolicyFile(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 562 | } |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 563 | |
| 564 | mAssistants.ensureAssistant(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 565 | } |
| 566 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 567 | private void loadPolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 568 | if (DBG) Slog.d(TAG, "loadPolicyFile"); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 569 | synchronized (mPolicyFile) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 570 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 571 | InputStream infile = null; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 572 | try { |
| 573 | infile = mPolicyFile.openRead(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 574 | readPolicyXml(infile, false /*forRestore*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 575 | } catch (FileNotFoundException e) { |
| 576 | // No data yet |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 577 | // Load default managed services approvals |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 578 | readDefaultApprovedServices(USER_SYSTEM); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 579 | } catch (IOException e) { |
| 580 | Log.wtf(TAG, "Unable to read notification policy", e); |
| 581 | } catch (NumberFormatException e) { |
| 582 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 583 | } catch (XmlPullParserException e) { |
| 584 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 585 | } finally { |
| 586 | IoUtils.closeQuietly(infile); |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | public void savePolicyFile() { |
| 592 | mHandler.removeMessages(MESSAGE_SAVE_POLICY_FILE); |
| 593 | mHandler.sendEmptyMessage(MESSAGE_SAVE_POLICY_FILE); |
| 594 | } |
| 595 | |
| 596 | private void handleSavePolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 597 | if (DBG) Slog.d(TAG, "handleSavePolicyFile"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 598 | synchronized (mPolicyFile) { |
| 599 | final FileOutputStream stream; |
| 600 | try { |
| 601 | stream = mPolicyFile.startWrite(); |
| 602 | } catch (IOException e) { |
| 603 | Slog.w(TAG, "Failed to save policy file", e); |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | try { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 608 | writePolicyXml(stream, false /*forBackup*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 609 | mPolicyFile.finishWrite(stream); |
| 610 | } catch (IOException e) { |
| 611 | Slog.w(TAG, "Failed to save policy file, restoring backup", e); |
| 612 | mPolicyFile.failWrite(stream); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 613 | } |
| 614 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 615 | BackupManager.dataChanged(getContext().getPackageName()); |
| 616 | } |
| 617 | |
| 618 | private void writePolicyXml(OutputStream stream, boolean forBackup) throws IOException { |
| 619 | final XmlSerializer out = new FastXmlSerializer(); |
| 620 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
| 621 | out.startDocument(null, true); |
| 622 | out.startTag(null, TAG_NOTIFICATION_POLICY); |
| 623 | out.attribute(null, ATTR_VERSION, Integer.toString(DB_VERSION)); |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 624 | mZenModeHelper.writeXml(out, forBackup, null); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 625 | mPreferencesHelper.writeXml(out, forBackup); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 626 | mListeners.writeXml(out, forBackup); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 627 | mAssistants.writeXml(out, forBackup); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 628 | mConditionProviders.writeXml(out, forBackup); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 629 | out.endTag(null, TAG_NOTIFICATION_POLICY); |
| 630 | out.endDocument(); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 631 | } |
| 632 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | private static final class ToastRecord |
| 634 | { |
| 635 | final int pid; |
| 636 | final String pkg; |
Beverly Tai | a7ed0ab | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 637 | final ITransientNotification callback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | int duration; |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 639 | int displayId; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 640 | Binder token; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 642 | ToastRecord(int pid, String pkg, ITransientNotification callback, int duration, |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 643 | Binder token, int displayId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | this.pid = pid; |
| 645 | this.pkg = pkg; |
| 646 | this.callback = callback; |
| 647 | this.duration = duration; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 648 | this.token = token; |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 649 | this.displayId = displayId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | void update(int duration) { |
| 653 | this.duration = duration; |
| 654 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 655 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 656 | void dump(PrintWriter pw, String prefix, DumpFilter filter) { |
| 657 | if (filter != null && !filter.matches(pkg)) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | pw.println(prefix + this); |
| 659 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 660 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | @Override |
| 662 | public final String toString() |
| 663 | { |
| 664 | return "ToastRecord{" |
| 665 | + Integer.toHexString(System.identityHashCode(this)) |
| 666 | + " pkg=" + pkg |
| 667 | + " callback=" + callback |
| 668 | + " duration=" + duration; |
| 669 | } |
| 670 | } |
| 671 | |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 672 | @VisibleForTesting |
| 673 | final NotificationDelegate mNotificationDelegate = new NotificationDelegate() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 674 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 675 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | public void onSetDisabled(int status) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 677 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 678 | mDisableNotificationEffects = |
| 679 | (status & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 680 | if (disableNotificationEffects(null) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | // cancel whatever's going on |
| 682 | long identity = Binder.clearCallingIdentity(); |
| 683 | try { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 684 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 685 | if (player != null) { |
| 686 | player.stopAsync(); |
| 687 | } |
| 688 | } catch (RemoteException e) { |
| 689 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | Binder.restoreCallingIdentity(identity); |
| 691 | } |
| 692 | |
| 693 | identity = Binder.clearCallingIdentity(); |
| 694 | try { |
| 695 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 696 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | Binder.restoreCallingIdentity(identity); |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 703 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 704 | public void onClearAll(int callingUid, int callingPid, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 705 | synchronized (mNotificationLock) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 706 | cancelAllLocked(callingUid, callingPid, userId, REASON_CANCEL_ALL, null, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 707 | /*includeCurrentProfiles*/ true); |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 708 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 709 | } |
| 710 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 711 | @Override |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 712 | public void onNotificationClick(int callingUid, int callingPid, String key, |
| 713 | NotificationVisibility nv) { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 714 | exitIdle(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 715 | synchronized (mNotificationLock) { |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 716 | NotificationRecord r = mNotificationsByKey.get(key); |
| 717 | if (r == null) { |
| 718 | Log.w(TAG, "No notification with key: " + key); |
| 719 | return; |
| 720 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 721 | final long now = System.currentTimeMillis(); |
Julia Reynolds | 3dfdde0 | 2018-10-08 09:17:56 -0400 | [diff] [blame] | 722 | MetricsLogger.action(r.getItemLogMaker() |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 723 | .setType(MetricsEvent.TYPE_ACTION) |
| 724 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, nv.rank) |
| 725 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, nv.count)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 726 | EventLogTags.writeNotificationClicked(key, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 727 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now), |
| 728 | nv.rank, nv.count); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 729 | |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 730 | StatusBarNotification sbn = r.sbn; |
| 731 | cancelNotification(callingUid, callingPid, sbn.getPackageName(), sbn.getTag(), |
| 732 | sbn.getId(), Notification.FLAG_AUTO_CANCEL, |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 733 | FLAG_FOREGROUND_SERVICE, false, r.getUserId(), |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 734 | REASON_CLICK, nv.rank, nv.count, null); |
| 735 | nv.recycle(); |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 736 | reportUserInteraction(r); |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 737 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | } |
| 739 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 740 | @Override |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 741 | public void onNotificationActionClick(int callingUid, int callingPid, String key, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 742 | int actionIndex, NotificationVisibility nv) { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 743 | exitIdle(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 744 | synchronized (mNotificationLock) { |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 745 | NotificationRecord r = mNotificationsByKey.get(key); |
| 746 | if (r == null) { |
| 747 | Log.w(TAG, "No notification with key: " + key); |
| 748 | return; |
| 749 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 750 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 751 | MetricsLogger.action(r.getLogMaker(now) |
| 752 | .setCategory(MetricsEvent.NOTIFICATION_ITEM_ACTION) |
| 753 | .setType(MetricsEvent.TYPE_ACTION) |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 754 | .setSubtype(actionIndex) |
| 755 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, nv.rank) |
| 756 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, nv.count)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 757 | EventLogTags.writeNotificationActionClicked(key, actionIndex, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 758 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now), |
| 759 | nv.rank, nv.count); |
| 760 | nv.recycle(); |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 761 | reportUserInteraction(r); |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
| 765 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 766 | public void onNotificationClear(int callingUid, int callingPid, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 767 | String pkg, String tag, int id, int userId, String key, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 768 | @NotificationStats.DismissalSurface int dismissalSurface, |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 769 | @NotificationStats.DismissalSentiment int dismissalSentiment, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 770 | NotificationVisibility nv) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 771 | synchronized (mNotificationLock) { |
| 772 | NotificationRecord r = mNotificationsByKey.get(key); |
| 773 | if (r != null) { |
| 774 | r.recordDismissalSurface(dismissalSurface); |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 775 | r.recordDismissalSentiment(dismissalSentiment); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 776 | } |
| 777 | } |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 778 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 779 | Notification.FLAG_ONGOING_EVENT | FLAG_FOREGROUND_SERVICE, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 780 | true, userId, REASON_CANCEL, nv.rank, nv.count,null); |
| 781 | nv.recycle(); |
Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 782 | } |
| 783 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 784 | @Override |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 785 | public void onPanelRevealed(boolean clearEffects, int items) { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 786 | MetricsLogger.visible(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Chris Wren | 621933f | 2017-06-14 15:59:03 -0400 | [diff] [blame] | 787 | MetricsLogger.histogram(getContext(), "note_load", items); |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 788 | EventLogTags.writeNotificationPanelRevealed(items); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 789 | if (clearEffects) { |
| 790 | clearEffects(); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | @Override |
| 795 | public void onPanelHidden() { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 796 | MetricsLogger.hidden(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 797 | EventLogTags.writeNotificationPanelHidden(); |
| 798 | } |
| 799 | |
| 800 | @Override |
| 801 | public void clearEffects() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 802 | synchronized (mNotificationLock) { |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 803 | if (DBG) Slog.d(TAG, "clearEffects"); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 804 | clearSoundLocked(); |
| 805 | clearVibrateLocked(); |
| 806 | clearLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | } |
| 808 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 809 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 810 | @Override |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 811 | public void onNotificationError(int callingUid, int callingPid, String pkg, String tag, |
| 812 | int id, int uid, int initialPid, String message, int userId) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 813 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, 0, false, userId, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 814 | REASON_ERROR, null); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 815 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 816 | |
| 817 | @Override |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 818 | public void onNotificationVisibilityChanged(NotificationVisibility[] newlyVisibleKeys, |
| 819 | NotificationVisibility[] noLongerVisibleKeys) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 820 | synchronized (mNotificationLock) { |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 821 | for (NotificationVisibility nv : newlyVisibleKeys) { |
| 822 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 823 | if (r == null) continue; |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 824 | if (!r.isSeen()) { |
| 825 | // Report to usage stats that notification was made visible |
| 826 | if (DBG) Slog.d(TAG, "Marking notification as visible " + nv.key); |
| 827 | reportSeen(r); |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 828 | |
| 829 | // If the newly visible notification has smart replies |
| 830 | // then log that the user has seen them. |
| 831 | if (r.getNumSmartRepliesAdded() > 0 |
| 832 | && !r.hasSeenSmartReplies()) { |
| 833 | r.setSeenSmartReplies(true); |
| 834 | LogMaker logMaker = r.getLogMaker() |
| 835 | .setCategory(MetricsEvent.SMART_REPLY_VISIBLE) |
| 836 | .addTaggedData(MetricsEvent.NOTIFICATION_SMART_REPLY_COUNT, |
| 837 | r.getNumSmartRepliesAdded()); |
| 838 | mMetricsLogger.write(logMaker); |
| 839 | } |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 840 | } |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 841 | r.setVisibility(true, nv.rank, nv.count); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 842 | maybeRecordInterruptionLocked(r); |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 843 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 844 | } |
| 845 | // Note that we might receive this event after notifications |
| 846 | // have already left the system, e.g. after dismissing from the |
| 847 | // shade. Hence not finding notifications in |
| 848 | // mNotificationsByKey is not an exceptional condition. |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 849 | for (NotificationVisibility nv : noLongerVisibleKeys) { |
| 850 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 851 | if (r == null) continue; |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 852 | r.setVisibility(false, nv.rank, nv.count); |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 853 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 854 | } |
| 855 | } |
Christoph Studer | 92b389d | 2014-04-01 18:44:40 +0200 | [diff] [blame] | 856 | } |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 857 | |
| 858 | @Override |
| 859 | public void onNotificationExpansionChanged(String key, |
| 860 | boolean userAction, boolean expanded) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 861 | synchronized (mNotificationLock) { |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 862 | NotificationRecord r = mNotificationsByKey.get(key); |
| 863 | if (r != null) { |
| 864 | r.stats.onExpansionChanged(userAction, expanded); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 865 | final long now = System.currentTimeMillis(); |
Chris Wren | f734271 | 2017-09-14 10:55:55 -0400 | [diff] [blame] | 866 | if (userAction) { |
Julia Reynolds | 3dfdde0 | 2018-10-08 09:17:56 -0400 | [diff] [blame] | 867 | MetricsLogger.action(r.getItemLogMaker() |
Chris Wren | f734271 | 2017-09-14 10:55:55 -0400 | [diff] [blame] | 868 | .setType(expanded ? MetricsEvent.TYPE_DETAIL |
| 869 | : MetricsEvent.TYPE_COLLAPSE)); |
Chris Wren | 377ac6d | 2017-09-12 14:15:23 -0400 | [diff] [blame] | 870 | } |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 871 | if (expanded && userAction) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 872 | r.recordExpanded(); |
| 873 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 874 | EventLogTags.writeNotificationExpansion(key, |
| 875 | userAction ? 1 : 0, expanded ? 1 : 0, |
| 876 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 880 | |
| 881 | @Override |
| 882 | public void onNotificationDirectReplied(String key) { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 883 | exitIdle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 884 | synchronized (mNotificationLock) { |
| 885 | NotificationRecord r = mNotificationsByKey.get(key); |
| 886 | if (r != null) { |
| 887 | r.recordDirectReplied(); |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 888 | reportUserInteraction(r); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | @Override |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 894 | public void onNotificationSmartRepliesAdded(String key, int replyCount) { |
| 895 | synchronized (mNotificationLock) { |
| 896 | NotificationRecord r = mNotificationsByKey.get(key); |
| 897 | if (r != null) { |
| 898 | r.setNumSmartRepliesAdded(replyCount); |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | @Override |
| 904 | public void onNotificationSmartReplySent(String key, int replyIndex) { |
| 905 | synchronized (mNotificationLock) { |
| 906 | NotificationRecord r = mNotificationsByKey.get(key); |
| 907 | if (r != null) { |
| 908 | LogMaker logMaker = r.getLogMaker() |
| 909 | .setCategory(MetricsEvent.SMART_REPLY_ACTION) |
| 910 | .setSubtype(replyIndex); |
| 911 | mMetricsLogger.write(logMaker); |
| 912 | // Treat clicking on a smart reply as a user interaction. |
| 913 | reportUserInteraction(r); |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | @Override |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 919 | public void onNotificationSettingsViewed(String key) { |
| 920 | synchronized (mNotificationLock) { |
| 921 | NotificationRecord r = mNotificationsByKey.get(key); |
| 922 | if (r != null) { |
| 923 | r.recordViewedSettings(); |
| 924 | } |
| 925 | } |
| 926 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | }; |
| 928 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 929 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 930 | private void clearSoundLocked() { |
| 931 | mSoundNotificationKey = null; |
| 932 | long identity = Binder.clearCallingIdentity(); |
| 933 | try { |
| 934 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 935 | if (player != null) { |
| 936 | player.stopAsync(); |
| 937 | } |
| 938 | } catch (RemoteException e) { |
| 939 | } finally { |
| 940 | Binder.restoreCallingIdentity(identity); |
| 941 | } |
| 942 | } |
| 943 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 944 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 945 | private void clearVibrateLocked() { |
| 946 | mVibrateNotificationKey = null; |
| 947 | long identity = Binder.clearCallingIdentity(); |
| 948 | try { |
| 949 | mVibrator.cancel(); |
| 950 | } finally { |
| 951 | Binder.restoreCallingIdentity(identity); |
| 952 | } |
| 953 | } |
| 954 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 955 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 956 | private void clearLightsLocked() { |
| 957 | // light |
| 958 | mLights.clear(); |
| 959 | updateLightsLocked(); |
| 960 | } |
| 961 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 962 | protected final BroadcastReceiver mLocaleChangeReceiver = new BroadcastReceiver() { |
| 963 | @Override |
| 964 | public void onReceive(Context context, Intent intent) { |
| 965 | if (Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) { |
Beverly | 70dcd00 | 2018-03-29 17:09:16 -0400 | [diff] [blame] | 966 | // update system notification channels |
| 967 | SystemNotificationChannels.createAll(context); |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 968 | mZenModeHelper.updateDefaultZenRules(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 969 | mPreferencesHelper.onLocaleChanged(context, ActivityManager.getCurrentUser()); |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | }; |
| 973 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 974 | private final BroadcastReceiver mRestoreReceiver = new BroadcastReceiver() { |
| 975 | @Override |
| 976 | public void onReceive(Context context, Intent intent) { |
| 977 | if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) { |
| 978 | try { |
| 979 | String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME); |
| 980 | String newValue = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE); |
Michal Karpinski | 6135a26 | 2017-08-11 10:45:58 +0100 | [diff] [blame] | 981 | int restoredFromSdkInt = intent.getIntExtra( |
| 982 | Intent.EXTRA_SETTING_RESTORED_FROM_SDK_INT, 0); |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 983 | mListeners.onSettingRestored( |
| 984 | element, newValue, restoredFromSdkInt, getSendingUserId()); |
| 985 | mConditionProviders.onSettingRestored( |
| 986 | element, newValue, restoredFromSdkInt, getSendingUserId()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 987 | } catch (Exception e) { |
| 988 | Slog.wtf(TAG, "Cannot restore managed services from settings", e); |
| 989 | } |
| 990 | } |
| 991 | } |
| 992 | }; |
| 993 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 994 | private final BroadcastReceiver mNotificationTimeoutReceiver = new BroadcastReceiver() { |
| 995 | @Override |
| 996 | public void onReceive(Context context, Intent intent) { |
| 997 | String action = intent.getAction(); |
| 998 | if (action == null) { |
| 999 | return; |
| 1000 | } |
| 1001 | if (ACTION_NOTIFICATION_TIMEOUT.equals(action)) { |
| 1002 | final NotificationRecord record; |
| 1003 | synchronized (mNotificationLock) { |
| 1004 | record = findNotificationByKeyLocked(intent.getStringExtra(EXTRA_KEY)); |
| 1005 | } |
| 1006 | if (record != null) { |
| 1007 | cancelNotification(record.sbn.getUid(), record.sbn.getInitialPid(), |
| 1008 | record.sbn.getPackageName(), record.sbn.getTag(), |
| 1009 | record.sbn.getId(), 0, |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 1010 | FLAG_FOREGROUND_SERVICE, true, record.getUserId(), |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1011 | REASON_TIMEOUT, null); |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | }; |
| 1016 | |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1017 | private final BroadcastReceiver mPackageIntentReceiver = new BroadcastReceiver() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | @Override |
| 1019 | public void onReceive(Context context, Intent intent) { |
| 1020 | String action = intent.getAction(); |
Dianne Hackborn | 29cd7f1 | 2015-01-08 10:37:05 -0800 | [diff] [blame] | 1021 | if (action == null) { |
| 1022 | return; |
| 1023 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1025 | boolean queryRestart = false; |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 1026 | boolean queryRemove = false; |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1027 | boolean packageChanged = false; |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 1028 | boolean cancelNotifications = true; |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1029 | boolean hideNotifications = false; |
| 1030 | boolean unhideNotifications = false; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1031 | int reason = REASON_PACKAGE_CHANGED; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1032 | |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1033 | if (action.equals(Intent.ACTION_PACKAGE_ADDED) |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 1034 | || (queryRemove=action.equals(Intent.ACTION_PACKAGE_REMOVED)) |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1035 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1036 | || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED)) |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1037 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1038 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE) |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1039 | || action.equals(Intent.ACTION_PACKAGES_SUSPENDED) |
| 1040 | || action.equals(Intent.ACTION_PACKAGES_UNSUSPENDED)) { |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1041 | int changeUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 1042 | UserHandle.USER_ALL); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1043 | String pkgList[] = null; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1044 | int uidList[] = null; |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 1045 | boolean removingPackage = queryRemove && |
| 1046 | !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 1047 | if (DBG) Slog.i(TAG, "action=" + action + " removing=" + removingPackage); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1048 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1049 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1050 | uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1051 | } else if (action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
| 1052 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Julia Reynolds | 4bc42b9 | 2018-08-14 11:27:35 -0400 | [diff] [blame] | 1053 | uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1054 | cancelNotifications = false; |
| 1055 | hideNotifications = true; |
| 1056 | } else if (action.equals(Intent.ACTION_PACKAGES_UNSUSPENDED)) { |
| 1057 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Julia Reynolds | 4bc42b9 | 2018-08-14 11:27:35 -0400 | [diff] [blame] | 1058 | uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1059 | cancelNotifications = false; |
| 1060 | unhideNotifications = true; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1061 | } else if (queryRestart) { |
| 1062 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1063 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1064 | } else { |
| 1065 | Uri uri = intent.getData(); |
| 1066 | if (uri == null) { |
| 1067 | return; |
| 1068 | } |
| 1069 | String pkgName = uri.getSchemeSpecificPart(); |
| 1070 | if (pkgName == null) { |
| 1071 | return; |
| 1072 | } |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1073 | if (packageChanged) { |
| 1074 | // We cancel notifications for packages which have just been disabled |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 1075 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1076 | final int enabled = mPackageManager.getApplicationEnabledSetting( |
| 1077 | pkgName, |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1078 | changeUserId != UserHandle.USER_ALL ? changeUserId : |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1079 | USER_SYSTEM); |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 1080 | if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 1081 | || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 1082 | cancelNotifications = false; |
| 1083 | } |
| 1084 | } catch (IllegalArgumentException e) { |
| 1085 | // Package doesn't exist; probably racing with uninstall. |
| 1086 | // cancelNotifications is already true, so nothing to do here. |
| 1087 | if (DBG) { |
| 1088 | Slog.i(TAG, "Exception trying to look up app enabled setting", e); |
| 1089 | } |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1090 | } catch (RemoteException e) { |
| 1091 | // Failed to talk to PackageManagerService Should never happen! |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1092 | } |
| 1093 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1094 | pkgList = new String[]{pkgName}; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1095 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1096 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1097 | if (pkgList != null && (pkgList.length > 0)) { |
| 1098 | for (String pkgName : pkgList) { |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 1099 | if (cancelNotifications) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1100 | cancelAllNotificationsInt(MY_UID, MY_PID, pkgName, null, 0, 0, |
| 1101 | !queryRestart, changeUserId, reason, null); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1102 | } else if (hideNotifications) { |
| 1103 | hideNotificationsForPackages(pkgList); |
| 1104 | } else if (unhideNotifications) { |
| 1105 | unhideNotificationsForPackages(pkgList); |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 1106 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1107 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1108 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1109 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1110 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1111 | mListeners.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1112 | mAssistants.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1113 | mConditionProviders.onPackagesChanged(removingPackage, pkgList, uidList); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1114 | mPreferencesHelper.onPackagesChanged( |
| 1115 | removingPackage, changeUserId, pkgList, uidList); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1116 | savePolicyFile(); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | }; |
| 1120 | |
| 1121 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 1122 | @Override |
| 1123 | public void onReceive(Context context, Intent intent) { |
| 1124 | String action = intent.getAction(); |
| 1125 | |
| 1126 | if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1127 | // Keep track of screen on/off state, but do not turn off the notification light |
| 1128 | // until user passes through the lock screen or views the notification. |
| 1129 | mScreenOn = true; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 1130 | updateNotificationPulse(); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1131 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 1132 | mScreenOn = false; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 1133 | updateNotificationPulse(); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1134 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
John Spurlock | 5d2eeb1 | 2014-01-16 10:46:36 -0500 | [diff] [blame] | 1135 | mInCall = TelephonyManager.EXTRA_STATE_OFFHOOK |
| 1136 | .equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE)); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1137 | updateNotificationPulse(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1138 | } else if (action.equals(Intent.ACTION_USER_STOPPED)) { |
| 1139 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 1140 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1141 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1142 | REASON_USER_STOPPED, null); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1143 | } |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1144 | } else if (action.equals(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) { |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 1145 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1146 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1147 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1148 | REASON_PROFILE_TURNED_OFF, null); |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 1149 | } |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1150 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { |
| 1151 | // turn off LED when user passes through lock screen |
| 1152 | mNotificationLight.turnOff(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1153 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Julia Reynolds | 4ff9749 | 2018-09-24 14:53:54 -0400 | [diff] [blame] | 1154 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1155 | mUserProfiles.updateCache(context); |
Julia Reynolds | 4ff9749 | 2018-09-24 14:53:54 -0400 | [diff] [blame] | 1156 | if (!mUserProfiles.isManagedProfile(userId)) { |
| 1157 | // reload per-user settings |
| 1158 | mSettingsObserver.update(null); |
| 1159 | // Refresh managed services |
| 1160 | mConditionProviders.onUserSwitched(userId); |
| 1161 | mListeners.onUserSwitched(userId); |
| 1162 | mZenModeHelper.onUserSwitched(userId); |
| 1163 | } |
| 1164 | // assistant is the only thing that cares about managed profiles specifically |
| 1165 | mAssistants.onUserSwitched(userId); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1166 | } else if (action.equals(Intent.ACTION_USER_ADDED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1167 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
| 1168 | if (userId != USER_NULL) { |
| 1169 | mUserProfiles.updateCache(context); |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 1170 | if (!mUserProfiles.isManagedProfile(userId)) { |
| 1171 | readDefaultApprovedServices(userId); |
| 1172 | } |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1173 | } |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1174 | } else if (action.equals(Intent.ACTION_USER_REMOVED)) { |
Julia Reynolds | 4ff9749 | 2018-09-24 14:53:54 -0400 | [diff] [blame] | 1175 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1176 | mUserProfiles.updateCache(context); |
Julia Reynolds | 4ff9749 | 2018-09-24 14:53:54 -0400 | [diff] [blame] | 1177 | mZenModeHelper.onUserRemoved(userId); |
| 1178 | mPreferencesHelper.onUserRemoved(userId); |
| 1179 | mListeners.onUserRemoved(userId); |
| 1180 | mConditionProviders.onUserRemoved(userId); |
| 1181 | mAssistants.onUserRemoved(userId); |
Julia Reynolds | 2e9bf5f | 2017-05-03 13:23:30 -0400 | [diff] [blame] | 1182 | savePolicyFile(); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1183 | } else if (action.equals(Intent.ACTION_USER_UNLOCKED)) { |
Julia Reynolds | 4ff9749 | 2018-09-24 14:53:54 -0400 | [diff] [blame] | 1184 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
| 1185 | mUserProfiles.updateCache(context); |
| 1186 | mAssistants.onUserUnlocked(userId); |
| 1187 | if (!mUserProfiles.isManagedProfile(userId)) { |
| 1188 | mConditionProviders.onUserUnlocked(userId); |
| 1189 | mListeners.onUserUnlocked(userId); |
| 1190 | mZenModeHelper.onUserUnlocked(userId); |
| 1191 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | }; |
| 1195 | |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 1196 | private final class SettingsObserver extends ContentObserver { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1197 | private final Uri NOTIFICATION_BADGING_URI |
| 1198 | = Settings.Secure.getUriFor(Settings.Secure.NOTIFICATION_BADGING); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1199 | private final Uri NOTIFICATION_LIGHT_PULSE_URI |
| 1200 | = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1201 | private final Uri NOTIFICATION_RATE_LIMIT_URI |
| 1202 | = Settings.Global.getUriFor(Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1203 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1204 | SettingsObserver(Handler handler) { |
| 1205 | super(handler); |
| 1206 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1207 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1208 | void observe() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1209 | ContentResolver resolver = getContext().getContentResolver(); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1210 | resolver.registerContentObserver(NOTIFICATION_BADGING_URI, |
| 1211 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1212 | resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1213 | false, this, UserHandle.USER_ALL); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1214 | resolver.registerContentObserver(NOTIFICATION_RATE_LIMIT_URI, |
| 1215 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1216 | update(null); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1219 | @Override public void onChange(boolean selfChange, Uri uri) { |
| 1220 | update(uri); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1223 | public void update(Uri uri) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1224 | ContentResolver resolver = getContext().getContentResolver(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1225 | if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { |
zhoulei | 7e37697 | 2017-05-17 18:41:25 +0800 | [diff] [blame] | 1226 | boolean pulseEnabled = Settings.System.getIntForUser(resolver, |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 1227 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0, UserHandle.USER_CURRENT) |
| 1228 | != 0; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1229 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 1230 | mNotificationPulseEnabled = pulseEnabled; |
| 1231 | updateNotificationPulse(); |
| 1232 | } |
| 1233 | } |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1234 | if (uri == null || NOTIFICATION_RATE_LIMIT_URI.equals(uri)) { |
| 1235 | mMaxPackageEnqueueRate = Settings.Global.getFloat(resolver, |
| 1236 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, mMaxPackageEnqueueRate); |
| 1237 | } |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1238 | if (uri == null || NOTIFICATION_BADGING_URI.equals(uri)) { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1239 | mPreferencesHelper.updateBadgingEnabled(); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1240 | } |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1241 | } |
| 1242 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1243 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1244 | private SettingsObserver mSettingsObserver; |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1245 | protected ZenModeHelper mZenModeHelper; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1246 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1247 | static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) { |
| 1248 | int[] ar = r.getIntArray(resid); |
| 1249 | if (ar == null) { |
| 1250 | return def; |
| 1251 | } |
| 1252 | final int len = ar.length > maxlen ? maxlen : ar.length; |
| 1253 | long[] out = new long[len]; |
| 1254 | for (int i=0; i<len; i++) { |
| 1255 | out[i] = ar[i]; |
| 1256 | } |
| 1257 | return out; |
| 1258 | } |
| 1259 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 1260 | public NotificationManagerService(Context context) { |
| 1261 | super(context); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 1262 | Notification.processWhitelistToken = WHITELIST_TOKEN; |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 1263 | } |
| 1264 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1265 | // TODO - replace these methods with a single VisibleForTesting constructor |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1266 | @VisibleForTesting |
| 1267 | void setAudioManager(AudioManager audioMananger) { |
| 1268 | mAudioManager = audioMananger; |
| 1269 | } |
| 1270 | |
| 1271 | @VisibleForTesting |
| 1272 | void setVibrator(Vibrator vibrator) { |
| 1273 | mVibrator = vibrator; |
| 1274 | } |
| 1275 | |
| 1276 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1277 | void setLights(Light light) { |
| 1278 | mNotificationLight = light; |
| 1279 | mAttentionLight = light; |
Julia Reynolds | 033a412 | 2017-01-31 16:50:38 -0500 | [diff] [blame] | 1280 | mNotificationPulseEnabled = true; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | @VisibleForTesting |
| 1284 | void setScreenOn(boolean on) { |
| 1285 | mScreenOn = on; |
| 1286 | } |
| 1287 | |
| 1288 | @VisibleForTesting |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1289 | int getNotificationRecordCount() { |
| 1290 | synchronized (mNotificationLock) { |
| 1291 | int count = mNotificationList.size() + mNotificationsByKey.size() |
| 1292 | + mSummaryByGroupKey.size() + mEnqueuedNotifications.size(); |
| 1293 | // subtract duplicates |
| 1294 | for (NotificationRecord posted : mNotificationList) { |
| 1295 | if (mNotificationsByKey.containsKey(posted.getKey())) { |
| 1296 | count--; |
| 1297 | } |
| 1298 | if (posted.sbn.isGroup() && posted.getNotification().isGroupSummary()) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1299 | count--; |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | return count; |
| 1304 | } |
| 1305 | } |
| 1306 | |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 1307 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1308 | void clearNotifications() { |
| 1309 | mEnqueuedNotifications.clear(); |
| 1310 | mNotificationList.clear(); |
| 1311 | mNotificationsByKey.clear(); |
| 1312 | mSummaryByGroupKey.clear(); |
| 1313 | } |
| 1314 | |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1315 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1316 | void addNotification(NotificationRecord r) { |
| 1317 | mNotificationList.add(r); |
| 1318 | mNotificationsByKey.put(r.sbn.getKey(), r); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1319 | if (r.sbn.isGroup()) { |
| 1320 | mSummaryByGroupKey.put(r.getGroupKey(), r); |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | @VisibleForTesting |
| 1325 | void addEnqueuedNotification(NotificationRecord r) { |
| 1326 | mEnqueuedNotifications.add(r); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | @VisibleForTesting |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1330 | NotificationRecord getNotificationRecord(String key) { |
| 1331 | return mNotificationsByKey.get(key); |
| 1332 | } |
| 1333 | |
| 1334 | |
| 1335 | @VisibleForTesting |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1336 | void setSystemReady(boolean systemReady) { |
| 1337 | mSystemReady = systemReady; |
| 1338 | } |
| 1339 | |
| 1340 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1341 | void setHandler(WorkerHandler handler) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1342 | mHandler = handler; |
| 1343 | } |
| 1344 | |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1345 | @VisibleForTesting |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 1346 | void setFallbackVibrationPattern(long[] vibrationPattern) { |
| 1347 | mFallbackVibrationPattern = vibrationPattern; |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1348 | } |
| 1349 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1350 | @VisibleForTesting |
| 1351 | void setPackageManager(IPackageManager packageManager) { |
| 1352 | mPackageManager = packageManager; |
| 1353 | } |
| 1354 | |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1355 | @VisibleForTesting |
| 1356 | void setRankingHelper(RankingHelper rankingHelper) { |
| 1357 | mRankingHelper = rankingHelper; |
| 1358 | } |
| 1359 | |
| 1360 | @VisibleForTesting |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1361 | void setPreferencesHelper(PreferencesHelper prefHelper) { mPreferencesHelper = prefHelper; } |
| 1362 | |
| 1363 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1364 | void setRankingHandler(RankingHandler rankingHandler) { |
| 1365 | mRankingHandler = rankingHandler; |
| 1366 | } |
| 1367 | |
| 1368 | @VisibleForTesting |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1369 | void setIsTelevision(boolean isTelevision) { |
| 1370 | mIsTelevision = isTelevision; |
| 1371 | } |
| 1372 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 1373 | @VisibleForTesting |
| 1374 | void setUsageStats(NotificationUsageStats us) { |
| 1375 | mUsageStats = us; |
| 1376 | } |
| 1377 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 1378 | @VisibleForTesting |
| 1379 | void setAccessibilityManager(AccessibilityManager am) { |
| 1380 | mAccessibilityManager = am; |
| 1381 | } |
| 1382 | |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1383 | // 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] | 1384 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1385 | void init(Looper looper, IPackageManager packageManager, |
| 1386 | PackageManager packageManagerClient, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1387 | LightsManager lightsManager, NotificationListeners notificationListeners, |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1388 | NotificationAssistants notificationAssistants, ConditionProviders conditionProviders, |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1389 | ICompanionDeviceManager companionManager, SnoozeHelper snoozeHelper, |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1390 | NotificationUsageStats usageStats, AtomicFile policyFile, |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 1391 | ActivityManager activityManager, GroupHelper groupHelper, IActivityManager am, |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 1392 | UsageStatsManagerInternal appUsageStats, DevicePolicyManagerInternal dpm, |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1393 | IUriGrantsManager ugm, UriGrantsManagerInternal ugmInternal, AppOpsManager appOps) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1394 | Resources resources = getContext().getResources(); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1395 | mMaxPackageEnqueueRate = Settings.Global.getFloat(getContext().getContentResolver(), |
| 1396 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, |
| 1397 | DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE); |
| 1398 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 1399 | mAccessibilityManager = |
| 1400 | (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1401 | mAm = am; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 1402 | mUgm = ugm; |
| 1403 | mUgmInternal = ugmInternal; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1404 | mPackageManager = packageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1405 | mPackageManagerClient = packageManagerClient; |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1406 | mAppOps = appOps; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1407 | mVibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE); |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 1408 | mAppUsageStats = appUsageStats; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1409 | mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1410 | mCompanionManager = companionManager; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1411 | mActivityManager = activityManager; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 1412 | mDeviceIdleController = IDeviceIdleController.Stub.asInterface( |
| 1413 | ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER)); |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 1414 | mDpm = dpm; |
| 1415 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1416 | mHandler = new WorkerHandler(looper); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1417 | mRankingThread.start(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1418 | String[] extractorNames; |
| 1419 | try { |
| 1420 | extractorNames = resources.getStringArray(R.array.config_notificationSignalExtractors); |
| 1421 | } catch (Resources.NotFoundException e) { |
| 1422 | extractorNames = new String[0]; |
| 1423 | } |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1424 | mUsageStats = usageStats; |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 1425 | mMetricsLogger = new MetricsLogger(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1426 | mRankingHandler = new RankingHandlerWorker(mRankingThread.getLooper()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1427 | mConditionProviders = conditionProviders; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1428 | mZenModeHelper = new ZenModeHelper(getContext(), mHandler.getLooper(), mConditionProviders); |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 1429 | mZenModeHelper.addCallback(new ZenModeHelper.Callback() { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1430 | @Override |
| 1431 | public void onConfigChanged() { |
| 1432 | savePolicyFile(); |
| 1433 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1434 | |
| 1435 | @Override |
| 1436 | void onZenModeChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1437 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED); |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1438 | getContext().sendBroadcastAsUser( |
Jason Monk | 6350674 | 2015-12-16 12:06:51 -0500 | [diff] [blame] | 1439 | new Intent(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL) |
| 1440 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT), |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1441 | UserHandle.ALL, android.Manifest.permission.MANAGE_NOTIFICATIONS); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1442 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1443 | updateInterruptionFilterLocked(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1444 | } |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 1445 | mRankingHandler.requestSort(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1446 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1447 | |
| 1448 | @Override |
| 1449 | void onPolicyChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1450 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED); |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 1451 | mRankingHandler.requestSort(); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1452 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1453 | }); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1454 | mPreferencesHelper = new PreferencesHelper(getContext(), |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 1455 | mPackageManagerClient, |
| 1456 | mRankingHandler, |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1457 | mZenModeHelper); |
| 1458 | mRankingHelper = new RankingHelper(getContext(), |
| 1459 | mRankingHandler, |
| 1460 | mPreferencesHelper, |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 1461 | mZenModeHelper, |
| 1462 | mUsageStats, |
| 1463 | extractorNames); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1464 | mSnoozeHelper = snoozeHelper; |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1465 | mGroupHelper = groupHelper; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 1466 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1467 | // This is a ManagedServices object that keeps track of the listeners. |
| 1468 | mListeners = notificationListeners; |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1469 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 1470 | // This is a MangedServices object that keeps track of the assistant. |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1471 | mAssistants = notificationAssistants; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1472 | |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 1473 | // Needs to be set before loadPolicyFile |
| 1474 | mAllowedManagedServicePackages = this::canUseManagedServices; |
| 1475 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1476 | mPolicyFile = policyFile; |
| 1477 | loadPolicyFile(); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1478 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1479 | mStatusBar = getLocalService(StatusBarManagerInternal.class); |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 1480 | if (mStatusBar != null) { |
| 1481 | mStatusBar.setNotificationDelegate(mNotificationDelegate); |
| 1482 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1483 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1484 | mNotificationLight = lightsManager.getLight(LightsManager.LIGHT_ID_NOTIFICATIONS); |
| 1485 | mAttentionLight = lightsManager.getLight(LightsManager.LIGHT_ID_ATTENTION); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1486 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1487 | mFallbackVibrationPattern = getLongArray(resources, |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 1488 | R.array.config_notificationFallbackVibePattern, |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1489 | VIBRATE_PATTERN_MAXLEN, |
| 1490 | DEFAULT_VIBRATE_PATTERN); |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 1491 | mInCallNotificationUri = Uri.parse("file://" + |
| 1492 | resources.getString(R.string.config_inCallNotificationSound)); |
| 1493 | mInCallNotificationAudioAttributes = new AudioAttributes.Builder() |
| 1494 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 1495 | .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 1496 | .build(); |
| 1497 | mInCallNotificationVolume = resources.getFloat(R.dimen.config_inCallNotificationVolume); |
| 1498 | |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 1499 | mUseAttentionLight = resources.getBoolean(R.bool.config_useAttentionLight); |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 1500 | mHasLight = |
| 1501 | resources.getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed); |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 1502 | |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1503 | // Don't start allowing notifications until the setup wizard has run once. |
| 1504 | // After that, including subsequent boots, init with notifications turned on. |
| 1505 | // This works on the first boot because the setup wizard will toggle this |
| 1506 | // 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] | 1507 | if (0 == Settings.Global.getInt(getContext().getContentResolver(), |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1508 | Settings.Global.DEVICE_PROVISIONED, 0)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1509 | mDisableNotificationEffects = true; |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1510 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1511 | mZenModeHelper.initZenMode(); |
John Spurlock | f370177 | 2015-02-12 13:29:37 -0500 | [diff] [blame] | 1512 | mInterruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1513 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1514 | mUserProfiles.updateCache(getContext()); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 1515 | listenForCallState(); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1516 | |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1517 | mSettingsObserver = new SettingsObserver(mHandler); |
| 1518 | |
| 1519 | mArchive = new Archive(resources.getInteger( |
| 1520 | R.integer.config_notificationServiceArchiveSize)); |
| 1521 | |
| 1522 | mIsTelevision = mPackageManagerClient.hasSystemFeature(FEATURE_LEANBACK) |
| 1523 | || mPackageManagerClient.hasSystemFeature(FEATURE_TELEVISION); |
| 1524 | } |
| 1525 | |
| 1526 | @Override |
| 1527 | public void onStart() { |
| 1528 | SnoozeHelper snoozeHelper = new SnoozeHelper(getContext(), new SnoozeHelper.Callback() { |
| 1529 | @Override |
| 1530 | public void repost(int userId, NotificationRecord r) { |
| 1531 | try { |
| 1532 | if (DBG) { |
| 1533 | Slog.d(TAG, "Reposting " + r.getKey()); |
| 1534 | } |
| 1535 | enqueueNotificationInternal(r.sbn.getPackageName(), r.sbn.getOpPkg(), |
| 1536 | r.sbn.getUid(), r.sbn.getInitialPid(), r.sbn.getTag(), r.sbn.getId(), |
| 1537 | r.sbn.getNotification(), userId); |
| 1538 | } catch (Exception e) { |
| 1539 | Slog.e(TAG, "Cannot un-snooze notification", e); |
| 1540 | } |
| 1541 | } |
| 1542 | }, mUserProfiles); |
| 1543 | |
| 1544 | final File systemDir = new File(Environment.getDataDirectory(), "system"); |
| 1545 | |
| 1546 | init(Looper.myLooper(), |
| 1547 | AppGlobals.getPackageManager(), getContext().getPackageManager(), |
| 1548 | getLocalService(LightsManager.class), |
| 1549 | new NotificationListeners(AppGlobals.getPackageManager()), |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 1550 | new NotificationAssistants(getContext(), mNotificationLock, mUserProfiles, |
| 1551 | AppGlobals.getPackageManager()), |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1552 | new ConditionProviders(getContext(), mUserProfiles, AppGlobals.getPackageManager()), |
| 1553 | null, snoozeHelper, new NotificationUsageStats(getContext()), |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 1554 | new AtomicFile(new File(systemDir, "notification_policy.xml"), "notification-policy"), |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1555 | (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE), |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 1556 | getGroupHelper(), ActivityManager.getService(), |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 1557 | LocalServices.getService(UsageStatsManagerInternal.class), |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 1558 | LocalServices.getService(DevicePolicyManagerInternal.class), |
| 1559 | UriGrantsManager.getService(), |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 1560 | LocalServices.getService(UriGrantsManagerInternal.class), |
| 1561 | (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE)); |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1562 | |
Mike Lockwood | 35e16bf | 2010-11-30 19:53:36 -0500 | [diff] [blame] | 1563 | // register for various Intents |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1564 | IntentFilter filter = new IntentFilter(); |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1565 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 1566 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1567 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1568 | filter.addAction(Intent.ACTION_USER_PRESENT); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1569 | filter.addAction(Intent.ACTION_USER_STOPPED); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1570 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1571 | filter.addAction(Intent.ACTION_USER_ADDED); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1572 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1573 | filter.addAction(Intent.ACTION_USER_UNLOCKED); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1574 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1575 | getContext().registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter, null, null); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1576 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1577 | IntentFilter pkgFilter = new IntentFilter(); |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1578 | pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1579 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
Daniel Sandler | aac0eb0 | 2011-08-06 22:51:56 -0400 | [diff] [blame] | 1580 | pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1581 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 1582 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 1583 | pkgFilter.addDataScheme("package"); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1584 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, pkgFilter, null, |
| 1585 | null); |
| 1586 | |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1587 | IntentFilter suspendedPkgFilter = new IntentFilter(); |
| 1588 | suspendedPkgFilter.addAction(Intent.ACTION_PACKAGES_SUSPENDED); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1589 | suspendedPkgFilter.addAction(Intent.ACTION_PACKAGES_UNSUSPENDED); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1590 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, |
| 1591 | suspendedPkgFilter, null, null); |
| 1592 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1593 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1594 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, sdFilter, null, |
| 1595 | null); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1596 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1597 | IntentFilter timeoutFilter = new IntentFilter(ACTION_NOTIFICATION_TIMEOUT); |
| 1598 | timeoutFilter.addDataScheme(SCHEME_TIMEOUT); |
| 1599 | getContext().registerReceiver(mNotificationTimeoutReceiver, timeoutFilter); |
| 1600 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1601 | IntentFilter settingsRestoredFilter = new IntentFilter(Intent.ACTION_SETTING_RESTORED); |
| 1602 | getContext().registerReceiver(mRestoreReceiver, settingsRestoredFilter); |
| 1603 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1604 | IntentFilter localeChangedFilter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED); |
| 1605 | getContext().registerReceiver(mLocaleChangeReceiver, localeChangedFilter); |
| 1606 | |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 1607 | publishBinderService(Context.NOTIFICATION_SERVICE, mService, /* allowIsolated= */ false, |
| 1608 | DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1609 | publishLocalService(NotificationManagerInternal.class, mInternalService); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1610 | } |
| 1611 | |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1612 | private GroupHelper getGroupHelper() { |
Adora Zhang | 48dd614a8 | 2018-06-25 19:18:41 -0700 | [diff] [blame] | 1613 | mAutoGroupAtCount = |
| 1614 | getContext().getResources().getInteger(R.integer.config_autoGroupAtCount); |
| 1615 | return new GroupHelper(mAutoGroupAtCount, new GroupHelper.Callback() { |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1616 | @Override |
| 1617 | public void addAutoGroup(String key) { |
| 1618 | synchronized (mNotificationLock) { |
| 1619 | addAutogroupKeyLocked(key); |
| 1620 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | @Override |
| 1624 | public void removeAutoGroup(String key) { |
| 1625 | synchronized (mNotificationLock) { |
| 1626 | removeAutogroupKeyLocked(key); |
| 1627 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | @Override |
| 1631 | public void addAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 1632 | createAutoGroupSummary(userId, pkg, triggeringKey); |
| 1633 | } |
| 1634 | |
| 1635 | @Override |
| 1636 | public void removeAutoGroupSummary(int userId, String pkg) { |
| 1637 | synchronized (mNotificationLock) { |
| 1638 | clearAutogroupSummaryLocked(userId, pkg); |
| 1639 | } |
| 1640 | } |
| 1641 | }); |
| 1642 | } |
| 1643 | |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1644 | private void sendRegisteredOnlyBroadcast(String action) { |
| 1645 | getContext().sendBroadcastAsUser(new Intent(action) |
| 1646 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), UserHandle.ALL, null); |
| 1647 | } |
| 1648 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1649 | @Override |
| 1650 | public void onBootPhase(int phase) { |
| 1651 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 1652 | // no beeping until we're basically done booting |
| 1653 | mSystemReady = true; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1654 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1655 | // Grab our optional AudioService |
| 1656 | mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1657 | mAudioManagerInternal = getLocalService(AudioManagerInternal.class); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1658 | mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1659 | mZenModeHelper.onSystemReady(); |
Adam Lesinski | a6db4ab | 2014-03-24 12:31:45 -0700 | [diff] [blame] | 1660 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 1661 | // This observer will force an update when observe is called, causing us to |
| 1662 | // bind to listener services. |
| 1663 | mSettingsObserver.observe(); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1664 | mListeners.onBootPhaseAppsCanStart(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1665 | mAssistants.onBootPhaseAppsCanStart(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1666 | mConditionProviders.onBootPhaseAppsCanStart(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1667 | } |
| 1668 | } |
| 1669 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1670 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1671 | private void updateListenerHintsLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1672 | final int hints = calculateHints(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1673 | if (hints == mListenerHints) return; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1674 | ZenLog.traceListenerHintsChanged(mListenerHints, hints, mEffectsSuppressors.size()); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1675 | mListenerHints = hints; |
| 1676 | scheduleListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 1677 | } |
| 1678 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1679 | @GuardedBy("mNotificationLock") |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1680 | private void updateEffectsSuppressorLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1681 | final long updatedSuppressedEffects = calculateSuppressedEffects(); |
| 1682 | if (updatedSuppressedEffects == mZenModeHelper.getSuppressedEffects()) return; |
| 1683 | final List<ComponentName> suppressors = getSuppressors(); |
| 1684 | ZenLog.traceEffectsSuppressorChanged(mEffectsSuppressors, suppressors, updatedSuppressedEffects); |
| 1685 | mEffectsSuppressors = suppressors; |
| 1686 | mZenModeHelper.setSuppressedEffects(updatedSuppressedEffects); |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1687 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1688 | } |
| 1689 | |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 1690 | private void exitIdle() { |
| 1691 | try { |
| 1692 | if (mDeviceIdleController != null) { |
| 1693 | mDeviceIdleController.exitIdle("notification interaction"); |
| 1694 | } |
| 1695 | } catch (RemoteException e) { |
| 1696 | } |
| 1697 | } |
| 1698 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1699 | private void updateNotificationChannelInt(String pkg, int uid, NotificationChannel channel, |
| 1700 | boolean fromListener) { |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1701 | if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) { |
| 1702 | // cancel |
| 1703 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channel.getId(), 0, 0, true, |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1704 | UserHandle.getUserId(uid), REASON_CHANNEL_BANNED, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1705 | null); |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1706 | if (isUidSystemOrPhone(uid)) { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1707 | IntArray profileIds = mUserProfiles.getCurrentProfileIds(); |
| 1708 | int N = profileIds.size(); |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1709 | for (int i = 0; i < N; i++) { |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 1710 | int profileId = profileIds.get(i); |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1711 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channel.getId(), 0, 0, true, |
| 1712 | profileId, REASON_CHANNEL_BANNED, |
| 1713 | null); |
| 1714 | } |
| 1715 | } |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1716 | } |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1717 | final NotificationChannel preUpdate = |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1718 | mPreferencesHelper.getNotificationChannel(pkg, uid, channel.getId(), true); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1719 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1720 | mPreferencesHelper.updateNotificationChannel(pkg, uid, channel, true); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1721 | maybeNotifyChannelOwner(pkg, uid, preUpdate, channel); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1722 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1723 | if (!fromListener) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1724 | final NotificationChannel modifiedChannel = |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1725 | mPreferencesHelper.getNotificationChannel(pkg, uid, channel.getId(), false); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1726 | mListeners.notifyNotificationChannelChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1727 | pkg, UserHandle.getUserHandleForUid(uid), |
| 1728 | modifiedChannel, NOTIFICATION_CHANNEL_OR_GROUP_UPDATED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1729 | } |
| 1730 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1731 | savePolicyFile(); |
| 1732 | } |
| 1733 | |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1734 | private void maybeNotifyChannelOwner(String pkg, int uid, NotificationChannel preUpdate, |
| 1735 | NotificationChannel update) { |
| 1736 | try { |
| 1737 | if ((preUpdate.getImportance() == IMPORTANCE_NONE |
| 1738 | && update.getImportance() != IMPORTANCE_NONE) |
| 1739 | || (preUpdate.getImportance() != IMPORTANCE_NONE |
| 1740 | && update.getImportance() == IMPORTANCE_NONE)) { |
| 1741 | getContext().sendBroadcastAsUser( |
| 1742 | new Intent(ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED) |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 1743 | .putExtra(NotificationManager.EXTRA_NOTIFICATION_CHANNEL_ID, |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1744 | update.getId()) |
| 1745 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, |
| 1746 | update.getImportance() == IMPORTANCE_NONE) |
| 1747 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 1748 | .setPackage(pkg), |
| 1749 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 1750 | } |
| 1751 | } catch (SecurityException e) { |
| 1752 | Slog.w(TAG, "Can't notify app about channel change", e); |
| 1753 | } |
| 1754 | } |
| 1755 | |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1756 | private void createNotificationChannelGroup(String pkg, int uid, NotificationChannelGroup group, |
| 1757 | boolean fromApp, boolean fromListener) { |
| 1758 | Preconditions.checkNotNull(group); |
| 1759 | Preconditions.checkNotNull(pkg); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1760 | |
| 1761 | final NotificationChannelGroup preUpdate = |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 1762 | mPreferencesHelper.getNotificationChannelGroup(group.getId(), pkg, uid); |
| 1763 | mPreferencesHelper.createNotificationChannelGroup(pkg, uid, group, |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1764 | fromApp); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1765 | if (!fromApp) { |
| 1766 | maybeNotifyChannelGroupOwner(pkg, uid, preUpdate, group); |
| 1767 | } |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1768 | if (!fromListener) { |
| 1769 | mListeners.notifyNotificationChannelGroupChanged(pkg, |
| 1770 | UserHandle.of(UserHandle.getCallingUserId()), group, |
| 1771 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
| 1772 | } |
| 1773 | } |
| 1774 | |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1775 | private void maybeNotifyChannelGroupOwner(String pkg, int uid, |
| 1776 | NotificationChannelGroup preUpdate, NotificationChannelGroup update) { |
| 1777 | try { |
| 1778 | if (preUpdate.isBlocked() != update.isBlocked()) { |
| 1779 | getContext().sendBroadcastAsUser( |
| 1780 | new Intent(ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED) |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 1781 | .putExtra(NotificationManager.EXTRA_NOTIFICATION_CHANNEL_GROUP_ID, |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1782 | update.getId()) |
| 1783 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, |
| 1784 | update.isBlocked()) |
| 1785 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 1786 | .setPackage(pkg), |
| 1787 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 1788 | } |
| 1789 | } catch (SecurityException e) { |
| 1790 | Slog.w(TAG, "Can't notify app about group change", e); |
| 1791 | } |
| 1792 | } |
| 1793 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1794 | private ArrayList<ComponentName> getSuppressors() { |
| 1795 | ArrayList<ComponentName> names = new ArrayList<ComponentName>(); |
| 1796 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 1797 | ArraySet<ComponentName> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1798 | |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 1799 | for (ComponentName info : serviceInfoList) { |
| 1800 | names.add(info); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | return names; |
| 1805 | } |
| 1806 | |
| 1807 | private boolean removeDisabledHints(ManagedServiceInfo info) { |
| 1808 | return removeDisabledHints(info, 0); |
| 1809 | } |
| 1810 | |
| 1811 | private boolean removeDisabledHints(ManagedServiceInfo info, int hints) { |
| 1812 | boolean removed = false; |
| 1813 | |
| 1814 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1815 | final int hint = mListenersDisablingEffects.keyAt(i); |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 1816 | final ArraySet<ComponentName> listeners = mListenersDisablingEffects.valueAt(i); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1817 | |
| 1818 | if (hints == 0 || (hint & hints) == hint) { |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 1819 | removed |= listeners.remove(info.component); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1820 | } |
| 1821 | } |
| 1822 | |
| 1823 | return removed; |
| 1824 | } |
| 1825 | |
| 1826 | private void addDisabledHints(ManagedServiceInfo info, int hints) { |
| 1827 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1828 | addDisabledHint(info, HINT_HOST_DISABLE_EFFECTS); |
| 1829 | } |
| 1830 | |
| 1831 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1832 | addDisabledHint(info, HINT_HOST_DISABLE_NOTIFICATION_EFFECTS); |
| 1833 | } |
| 1834 | |
| 1835 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1836 | addDisabledHint(info, HINT_HOST_DISABLE_CALL_EFFECTS); |
| 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | private void addDisabledHint(ManagedServiceInfo info, int hint) { |
| 1841 | if (mListenersDisablingEffects.indexOfKey(hint) < 0) { |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 1842 | mListenersDisablingEffects.put(hint, new ArraySet<>()); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 1845 | ArraySet<ComponentName> hintListeners = mListenersDisablingEffects.get(hint); |
| 1846 | hintListeners.add(info.component); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | private int calculateHints() { |
| 1850 | int hints = 0; |
| 1851 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1852 | int hint = mListenersDisablingEffects.keyAt(i); |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 1853 | ArraySet<ComponentName> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1854 | |
| 1855 | if (!serviceInfoList.isEmpty()) { |
| 1856 | hints |= hint; |
| 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | return hints; |
| 1861 | } |
| 1862 | |
| 1863 | private long calculateSuppressedEffects() { |
| 1864 | int hints = calculateHints(); |
| 1865 | long suppressedEffects = 0; |
| 1866 | |
| 1867 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1868 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_ALL; |
| 1869 | } |
| 1870 | |
| 1871 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1872 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_NOTIFICATIONS; |
| 1873 | } |
| 1874 | |
| 1875 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1876 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_CALLS; |
| 1877 | } |
| 1878 | |
| 1879 | return suppressedEffects; |
| 1880 | } |
| 1881 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1882 | @GuardedBy("mNotificationLock") |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1883 | private void updateInterruptionFilterLocked() { |
| 1884 | int interruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
| 1885 | if (interruptionFilter == mInterruptionFilter) return; |
| 1886 | mInterruptionFilter = interruptionFilter; |
| 1887 | scheduleInterruptionFilterChanged(interruptionFilter); |
| 1888 | } |
| 1889 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1890 | @VisibleForTesting |
| 1891 | INotificationManager getBinderService() { |
| 1892 | return INotificationManager.Stub.asInterface(mService); |
| 1893 | } |
| 1894 | |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1895 | /** |
| 1896 | * Report to usage stats that the notification was seen. |
| 1897 | * @param r notification record |
| 1898 | */ |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1899 | @GuardedBy("mNotificationLock") |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 1900 | protected void reportSeen(NotificationRecord r) { |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 1901 | mAppUsageStats.reportEvent(r.sbn.getPackageName(), |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1902 | getRealUserId(r.sbn.getUserId()), |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 1903 | UsageEvents.Event.NOTIFICATION_SEEN); |
| 1904 | } |
| 1905 | |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 1906 | protected int calculateSuppressedVisualEffects(Policy incomingPolicy, Policy currPolicy, |
| 1907 | int targetSdkVersion) { |
| 1908 | if (incomingPolicy.suppressedVisualEffects == SUPPRESSED_EFFECTS_UNSET) { |
| 1909 | return incomingPolicy.suppressedVisualEffects; |
| 1910 | } |
| 1911 | final int[] effectsIntroducedInP = { |
| 1912 | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT, |
| 1913 | SUPPRESSED_EFFECT_LIGHTS, |
| 1914 | SUPPRESSED_EFFECT_PEEK, |
| 1915 | SUPPRESSED_EFFECT_STATUS_BAR, |
| 1916 | SUPPRESSED_EFFECT_BADGE, |
| 1917 | SUPPRESSED_EFFECT_AMBIENT, |
| 1918 | SUPPRESSED_EFFECT_NOTIFICATION_LIST |
| 1919 | }; |
| 1920 | |
| 1921 | int newSuppressedVisualEffects = incomingPolicy.suppressedVisualEffects; |
Jeff Sharkey | aa1a911 | 2018-04-10 15:18:12 -0600 | [diff] [blame] | 1922 | if (targetSdkVersion < Build.VERSION_CODES.P) { |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 1923 | // unset higher order bits introduced in P, maintain the user's higher order bits |
| 1924 | for (int i = 0; i < effectsIntroducedInP.length ; i++) { |
| 1925 | newSuppressedVisualEffects &= ~effectsIntroducedInP[i]; |
| 1926 | newSuppressedVisualEffects |= |
| 1927 | (currPolicy.suppressedVisualEffects & effectsIntroducedInP[i]); |
| 1928 | } |
| 1929 | // set higher order bits according to lower order bits |
| 1930 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_OFF) != 0) { |
| 1931 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_LIGHTS; |
| 1932 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 1933 | } |
| 1934 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_ON) != 0) { |
| 1935 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_PEEK; |
| 1936 | } |
| 1937 | } else { |
| 1938 | boolean hasNewEffects = (newSuppressedVisualEffects |
| 1939 | - SUPPRESSED_EFFECT_SCREEN_ON - SUPPRESSED_EFFECT_SCREEN_OFF) > 0; |
| 1940 | // if any of the new effects introduced in P are set |
| 1941 | if (hasNewEffects) { |
| 1942 | // clear out the deprecated effects |
| 1943 | newSuppressedVisualEffects &= ~ (SUPPRESSED_EFFECT_SCREEN_ON |
| 1944 | | SUPPRESSED_EFFECT_SCREEN_OFF); |
| 1945 | |
| 1946 | // set the deprecated effects according to the new more specific effects |
| 1947 | if ((newSuppressedVisualEffects & Policy.SUPPRESSED_EFFECT_PEEK) != 0) { |
| 1948 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_SCREEN_ON; |
| 1949 | } |
| 1950 | if ((newSuppressedVisualEffects & Policy.SUPPRESSED_EFFECT_LIGHTS) != 0 |
| 1951 | && (newSuppressedVisualEffects |
| 1952 | & Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT) != 0 |
| 1953 | && (newSuppressedVisualEffects |
| 1954 | & Policy.SUPPRESSED_EFFECT_AMBIENT) != 0) { |
| 1955 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_SCREEN_OFF; |
| 1956 | } |
| 1957 | } else { |
| 1958 | // set higher order bits according to lower order bits |
| 1959 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_OFF) != 0) { |
| 1960 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_LIGHTS; |
| 1961 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 1962 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_AMBIENT; |
| 1963 | } |
| 1964 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_ON) != 0) { |
| 1965 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_PEEK; |
| 1966 | } |
| 1967 | } |
| 1968 | } |
| 1969 | |
| 1970 | return newSuppressedVisualEffects; |
| 1971 | } |
| 1972 | |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1973 | @GuardedBy("mNotificationLock") |
| 1974 | protected void maybeRecordInterruptionLocked(NotificationRecord r) { |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 1975 | if (r.isInterruptive() && !r.hasRecordedInterruption()) { |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1976 | mAppUsageStats.reportInterruptiveNotification(r.sbn.getPackageName(), |
| 1977 | r.getChannel().getId(), |
| 1978 | getRealUserId(r.sbn.getUserId())); |
Julia Reynolds | ad7d713 | 2018-03-21 16:05:00 -0400 | [diff] [blame] | 1979 | logRecentLocked(r); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 1980 | r.setRecordedInterruption(true); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1981 | } |
| 1982 | } |
| 1983 | |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1984 | /** |
| 1985 | * Report to usage stats that the notification was clicked. |
| 1986 | * @param r notification record |
| 1987 | */ |
| 1988 | protected void reportUserInteraction(NotificationRecord r) { |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1989 | mAppUsageStats.reportEvent(r.sbn.getPackageName(), |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1990 | getRealUserId(r.sbn.getUserId()), |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1991 | UsageEvents.Event.USER_INTERACTION); |
| 1992 | } |
| 1993 | |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1994 | private int getRealUserId(int userId) { |
| 1995 | return userId == UserHandle.USER_ALL ? UserHandle.USER_SYSTEM : userId; |
| 1996 | } |
| 1997 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1998 | @VisibleForTesting |
| 1999 | NotificationManagerInternal getInternalService() { |
| 2000 | return mInternalService; |
| 2001 | } |
| 2002 | |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 2003 | @VisibleForTesting |
| 2004 | final IBinder mService = new INotificationManager.Stub() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2005 | // Toasts |
| 2006 | // ============================================================================ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2007 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2008 | @Override |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 2009 | public void enqueueToast(String pkg, ITransientNotification callback, int duration, |
| 2010 | int displayId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2011 | { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2012 | if (DBG) { |
| 2013 | Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 2014 | + " duration=" + duration + " displayId=" + displayId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2015 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2016 | |
| 2017 | if (pkg == null || callback == null) { |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 2018 | Slog.e(TAG, "Not enqueuing toast. pkg=" + pkg + " callback=" + callback); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2019 | return ; |
| 2020 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2021 | |
Beverly | 58b2453 | 2018-10-02 09:08:23 -0400 | [diff] [blame] | 2022 | final int callingUid = Binder.getCallingUid(); |
| 2023 | final boolean isSystemToast = isCallerSystemOrPhone() |
| 2024 | || PackageManagerService.PLATFORM_PACKAGE_NAME.equals(pkg); |
| 2025 | final boolean isPackageSuspended = isPackageSuspendedForUser(pkg, callingUid); |
| 2026 | final boolean notificationsDisabledForPackage = !areNotificationsEnabledForPackage(pkg, |
| 2027 | callingUid); |
| 2028 | |
| 2029 | long callingIdentity = Binder.clearCallingIdentity(); |
| 2030 | try { |
| 2031 | final boolean appIsForeground = mActivityManager.getUidImportance(callingUid) |
| 2032 | == IMPORTANCE_FOREGROUND; |
| 2033 | if (ENABLE_BLOCKED_TOASTS && !isSystemToast && ((notificationsDisabledForPackage |
| 2034 | && !appIsForeground) || isPackageSuspended)) { |
| 2035 | Slog.e(TAG, "Suppressing toast from package " + pkg |
| 2036 | + (isPackageSuspended ? " due to package suspended." |
| 2037 | : " by user request.")); |
| 2038 | return; |
| 2039 | } |
| 2040 | } finally { |
| 2041 | Binder.restoreCallingIdentity(callingIdentity); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | synchronized (mToastQueue) { |
| 2045 | int callingPid = Binder.getCallingPid(); |
| 2046 | long callingId = Binder.clearCallingIdentity(); |
| 2047 | try { |
| 2048 | ToastRecord record; |
Beverly Tai | a7ed0ab | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 2049 | int index = indexOfToastLocked(pkg, callback); |
| 2050 | // If it's already in the queue, we update it in place, we don't |
| 2051 | // move it to the end of the queue. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2052 | if (index >= 0) { |
| 2053 | record = mToastQueue.get(index); |
| 2054 | record.update(duration); |
| 2055 | } else { |
Beverly Tai | a7ed0ab | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 2056 | // Limit the number of toasts that any given package except the android |
| 2057 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| 2058 | if (!isSystemToast) { |
| 2059 | int count = 0; |
| 2060 | final int N = mToastQueue.size(); |
| 2061 | for (int i=0; i<N; i++) { |
| 2062 | final ToastRecord r = mToastQueue.get(i); |
| 2063 | if (r.pkg.equals(pkg)) { |
| 2064 | count++; |
| 2065 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 2066 | Slog.e(TAG, "Package has already posted " + count |
| 2067 | + " toasts. Not showing more. Package=" + pkg); |
| 2068 | return; |
| 2069 | } |
| 2070 | } |
| 2071 | } |
| 2072 | } |
| 2073 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 2074 | Binder token = new Binder(); |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 2075 | mWindowManagerInternal.addWindowToken(token, TYPE_TOAST, displayId); |
| 2076 | record = new ToastRecord(callingPid, pkg, callback, duration, token, |
| 2077 | displayId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2078 | mToastQueue.add(record); |
| 2079 | index = mToastQueue.size() - 1; |
Beverly Tai | a7ed0ab | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 2080 | keepProcessAliveIfNeededLocked(callingPid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2081 | } |
| 2082 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 2083 | // new or just been updated. Call back and tell it to show itself. |
| 2084 | // If the callback fails, this will remove it from the list, so don't |
| 2085 | // assume that it's valid after this. |
| 2086 | if (index == 0) { |
| 2087 | showNextToastLocked(); |
| 2088 | } |
| 2089 | } finally { |
| 2090 | Binder.restoreCallingIdentity(callingId); |
| 2091 | } |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | @Override |
| 2096 | public void cancelToast(String pkg, ITransientNotification callback) { |
| 2097 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| 2098 | |
| 2099 | if (pkg == null || callback == null) { |
| 2100 | Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback); |
| 2101 | return ; |
| 2102 | } |
| 2103 | |
| 2104 | synchronized (mToastQueue) { |
| 2105 | long callingId = Binder.clearCallingIdentity(); |
| 2106 | try { |
| 2107 | int index = indexOfToastLocked(pkg, callback); |
| 2108 | if (index >= 0) { |
| 2109 | cancelToastLocked(index); |
| 2110 | } else { |
| 2111 | Slog.w(TAG, "Toast already cancelled. pkg=" + pkg |
| 2112 | + " callback=" + callback); |
| 2113 | } |
| 2114 | } finally { |
| 2115 | Binder.restoreCallingIdentity(callingId); |
| 2116 | } |
| 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | @Override |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 2121 | public void finishToken(String pkg, ITransientNotification callback) { |
| 2122 | synchronized (mToastQueue) { |
| 2123 | long callingId = Binder.clearCallingIdentity(); |
| 2124 | try { |
| 2125 | int index = indexOfToastLocked(pkg, callback); |
| 2126 | if (index >= 0) { |
| 2127 | ToastRecord record = mToastQueue.get(index); |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 2128 | finishTokenLocked(record.token, record.displayId); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 2129 | } else { |
| 2130 | Slog.w(TAG, "Toast already killed. pkg=" + pkg |
| 2131 | + " callback=" + callback); |
| 2132 | } |
| 2133 | } finally { |
| 2134 | Binder.restoreCallingIdentity(callingId); |
| 2135 | } |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 2140 | public void enqueueNotificationWithTag(String pkg, String opPkg, String tag, int id, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 2141 | Notification notification, int userId) throws RemoteException { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 2142 | enqueueNotificationInternal(pkg, opPkg, Binder.getCallingUid(), |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 2143 | Binder.getCallingPid(), tag, id, notification, userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | @Override |
| 2147 | public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2148 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2149 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2150 | Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2151 | // Don't allow client applications to cancel foreground service notis or autobundled |
| 2152 | // summaries. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 2153 | final int mustNotHaveFlags = isCallingUidSystem() ? 0 : |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 2154 | (FLAG_FOREGROUND_SERVICE | Notification.FLAG_AUTOGROUP_SUMMARY); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2155 | cancelNotification(Binder.getCallingUid(), Binder.getCallingPid(), pkg, tag, id, 0, |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 2156 | mustNotHaveFlags, false, userId, REASON_APP_CANCEL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | @Override |
| 2160 | public void cancelAllNotifications(String pkg, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2161 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2162 | |
| 2163 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2164 | Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg); |
| 2165 | |
| 2166 | // Calling from user space, don't allow the canceling of actively |
| 2167 | // running foreground services. |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2168 | cancelAllNotificationsInt(Binder.getCallingUid(), Binder.getCallingPid(), |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 2169 | pkg, null, 0, FLAG_FOREGROUND_SERVICE, true, userId, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 2170 | REASON_APP_CANCEL_ALL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | @Override |
| 2174 | public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) { |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 2175 | enforceSystemOrSystemUI("setNotificationsEnabledForPackage"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2176 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2177 | mPreferencesHelper.setEnabled(pkg, uid, enabled); |
Howard Ro | 8b56f75 | 2018-08-07 15:44:25 -0700 | [diff] [blame] | 2178 | mMetricsLogger.write(new LogMaker(MetricsEvent.ACTION_BAN_APP_NOTES) |
| 2179 | .setType(MetricsEvent.TYPE_ACTION) |
| 2180 | .setPackageName(pkg) |
| 2181 | .setSubtype(enabled ? 1 : 0)); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 2182 | // 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] | 2183 | if (!enabled) { |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 2184 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, null, 0, 0, true, |
| 2185 | UserHandle.getUserId(uid), REASON_PACKAGE_BANNED, null); |
| 2186 | } |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 2187 | |
| 2188 | try { |
| 2189 | getContext().sendBroadcastAsUser( |
| 2190 | new Intent(ACTION_APP_BLOCK_STATE_CHANGED) |
| 2191 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, !enabled) |
| 2192 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 2193 | .setPackage(pkg), |
| 2194 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 2195 | } catch (SecurityException e) { |
| 2196 | Slog.w(TAG, "Can't notify app about app block change", e); |
| 2197 | } |
| 2198 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 2199 | savePolicyFile(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | /** |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 2203 | * Updates the enabled state for notifications for the given package (and uid). |
| 2204 | * Additionally, this method marks the app importance as locked by the user, which means |
| 2205 | * that notifications from the app will <b>not</b> be considered for showing a |
| 2206 | * blocking helper. |
| 2207 | * |
| 2208 | * @param pkg package that owns the notifications to update |
| 2209 | * @param uid uid of the app providing notifications |
| 2210 | * @param enabled whether notifications should be enabled for the app |
| 2211 | * |
| 2212 | * @see #setNotificationsEnabledForPackage(String, int, boolean) |
| 2213 | */ |
| 2214 | @Override |
| 2215 | public void setNotificationsEnabledWithImportanceLockForPackage( |
| 2216 | String pkg, int uid, boolean enabled) { |
| 2217 | setNotificationsEnabledForPackage(pkg, uid, enabled); |
| 2218 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2219 | mPreferencesHelper.setAppImportanceLocked(pkg, uid); |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2223 | * Use this when you just want to know if notifications are OK for this package. |
| 2224 | */ |
| 2225 | @Override |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 2226 | public boolean areNotificationsEnabled(String pkg) { |
| 2227 | return areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()); |
| 2228 | } |
| 2229 | |
| 2230 | /** |
| 2231 | * Use this when you just want to know if notifications are OK for this package. |
| 2232 | */ |
| 2233 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2234 | public boolean areNotificationsEnabledForPackage(String pkg, int uid) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 2235 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 2236 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2237 | return mPreferencesHelper.getImportance(pkg, uid) != IMPORTANCE_NONE; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2238 | } |
| 2239 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 2240 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 2241 | public int getPackageImportance(String pkg) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 2242 | checkCallerIsSystemOrSameApp(pkg); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2243 | return mPreferencesHelper.getImportance(pkg, Binder.getCallingUid()); |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | @Override |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2247 | public boolean canShowBadge(String pkg, int uid) { |
| 2248 | checkCallerIsSystem(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2249 | return mPreferencesHelper.canShowBadge(pkg, uid); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | @Override |
| 2253 | public void setShowBadge(String pkg, int uid, boolean showBadge) { |
| 2254 | checkCallerIsSystem(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2255 | mPreferencesHelper.setShowBadge(pkg, uid, showBadge); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2256 | savePolicyFile(); |
| 2257 | } |
| 2258 | |
| 2259 | @Override |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 2260 | public void setNotificationDelegate(String callingPkg, String delegate) { |
| 2261 | checkCallerIsSameApp(callingPkg); |
| 2262 | final int callingUid = Binder.getCallingUid(); |
| 2263 | UserHandle user = UserHandle.getUserHandleForUid(callingUid); |
| 2264 | try { |
| 2265 | ApplicationInfo info = |
| 2266 | mPackageManager.getApplicationInfo(delegate, |
| 2267 | MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE, |
| 2268 | user.getIdentifier()); |
| 2269 | if (info != null) { |
| 2270 | mPreferencesHelper.setNotificationDelegate( |
| 2271 | callingPkg, callingUid, delegate, info.uid); |
| 2272 | savePolicyFile(); |
| 2273 | } |
| 2274 | } catch (RemoteException e) { |
| 2275 | // :( |
| 2276 | } |
| 2277 | } |
| 2278 | |
| 2279 | @Override |
| 2280 | public void revokeNotificationDelegate(String callingPkg) { |
| 2281 | checkCallerIsSameApp(callingPkg); |
| 2282 | mPreferencesHelper.revokeNotificationDelegate(callingPkg, Binder.getCallingUid()); |
| 2283 | savePolicyFile(); |
| 2284 | } |
| 2285 | |
| 2286 | @Override |
| 2287 | public String getNotificationDelegate(String callingPkg) { |
| 2288 | // callable by Settings also |
| 2289 | checkCallerIsSystemOrSameApp(callingPkg); |
| 2290 | return mPreferencesHelper.getNotificationDelegate(callingPkg, Binder.getCallingUid()); |
| 2291 | } |
| 2292 | |
| 2293 | @Override |
| 2294 | public boolean canNotifyAsPackage(String callingPkg, String targetPkg) { |
| 2295 | checkCallerIsSameApp(callingPkg); |
| 2296 | final int callingUid = Binder.getCallingUid(); |
| 2297 | UserHandle user = UserHandle.getUserHandleForUid(callingUid); |
| 2298 | try { |
| 2299 | ApplicationInfo info = |
| 2300 | mPackageManager.getApplicationInfo(targetPkg, |
| 2301 | MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE, |
| 2302 | user.getIdentifier()); |
| 2303 | if (info != null) { |
| 2304 | return mPreferencesHelper.isDelegateAllowed( |
| 2305 | targetPkg, info.uid, callingPkg, callingUid); |
| 2306 | } |
| 2307 | } catch (RemoteException e) { |
| 2308 | // :( |
| 2309 | } |
| 2310 | return false; |
| 2311 | } |
| 2312 | |
| 2313 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2314 | public void updateNotificationChannelGroupForPackage(String pkg, int uid, |
| 2315 | NotificationChannelGroup group) throws RemoteException { |
| 2316 | enforceSystemOrSystemUI("Caller not system or systemui"); |
| 2317 | createNotificationChannelGroup(pkg, uid, group, false, false); |
| 2318 | savePolicyFile(); |
| 2319 | } |
| 2320 | |
| 2321 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2322 | public void createNotificationChannelGroups(String pkg, |
| 2323 | ParceledListSlice channelGroupList) throws RemoteException { |
| 2324 | checkCallerIsSystemOrSameApp(pkg); |
| 2325 | List<NotificationChannelGroup> groups = channelGroupList.getList(); |
| 2326 | final int groupSize = groups.size(); |
| 2327 | for (int i = 0; i < groupSize; i++) { |
| 2328 | final NotificationChannelGroup group = groups.get(i); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2329 | createNotificationChannelGroup(pkg, Binder.getCallingUid(), group, true, false); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2330 | } |
| 2331 | savePolicyFile(); |
| 2332 | } |
| 2333 | |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 2334 | private void createNotificationChannelsImpl(String pkg, int uid, |
| 2335 | ParceledListSlice channelsList) { |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 2336 | List<NotificationChannel> channels = channelsList.getList(); |
| 2337 | final int channelsSize = channels.size(); |
| 2338 | for (int i = 0; i < channelsSize; i++) { |
| 2339 | final NotificationChannel channel = channels.get(i); |
| 2340 | Preconditions.checkNotNull(channel, "channel in list is null"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2341 | mPreferencesHelper.createNotificationChannel(pkg, uid, channel, |
Julia Reynolds | 1fe1094 | 2018-03-28 12:46:51 -0400 | [diff] [blame] | 2342 | true /* fromTargetApp */, mConditionProviders.isPackageOrComponentAllowed( |
| 2343 | pkg, UserHandle.getUserId(uid))); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2344 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2345 | UserHandle.getUserHandleForUid(uid), |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2346 | mPreferencesHelper.getNotificationChannel(pkg, uid, channel.getId(), false), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2347 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 2348 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2349 | savePolicyFile(); |
| 2350 | } |
| 2351 | |
| 2352 | @Override |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 2353 | public void createNotificationChannels(String pkg, |
| 2354 | ParceledListSlice channelsList) throws RemoteException { |
| 2355 | checkCallerIsSystemOrSameApp(pkg); |
| 2356 | createNotificationChannelsImpl(pkg, Binder.getCallingUid(), channelsList); |
| 2357 | } |
| 2358 | |
| 2359 | @Override |
| 2360 | public void createNotificationChannelsForPackage(String pkg, int uid, |
| 2361 | ParceledListSlice channelsList) throws RemoteException { |
| 2362 | checkCallerIsSystem(); |
| 2363 | createNotificationChannelsImpl(pkg, uid, channelsList); |
| 2364 | } |
| 2365 | |
| 2366 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2367 | public NotificationChannel getNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2368 | checkCallerIsSystemOrSameApp(pkg); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2369 | return mPreferencesHelper.getNotificationChannel( |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2370 | pkg, Binder.getCallingUid(), channelId, false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | @Override |
| 2374 | public NotificationChannel getNotificationChannelForPackage(String pkg, int uid, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2375 | String channelId, boolean includeDeleted) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2376 | checkCallerIsSystem(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2377 | return mPreferencesHelper.getNotificationChannel(pkg, uid, channelId, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | @Override |
| 2381 | public void deleteNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2382 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2383 | final int callingUid = Binder.getCallingUid(); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2384 | if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 2385 | throw new IllegalArgumentException("Cannot delete default channel"); |
| 2386 | } |
| 2387 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channelId, 0, 0, true, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2388 | UserHandle.getUserId(callingUid), REASON_CHANNEL_BANNED, null); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2389 | mPreferencesHelper.deleteNotificationChannel(pkg, callingUid, channelId); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2390 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2391 | UserHandle.getUserHandleForUid(callingUid), |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2392 | mPreferencesHelper.getNotificationChannel(pkg, callingUid, channelId, true), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2393 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2394 | savePolicyFile(); |
| 2395 | } |
| 2396 | |
| 2397 | @Override |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2398 | public NotificationChannelGroup getNotificationChannelGroup(String pkg, String groupId) { |
| 2399 | checkCallerIsSystemOrSameApp(pkg); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2400 | return mPreferencesHelper.getNotificationChannelGroupWithChannels( |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2401 | pkg, Binder.getCallingUid(), groupId, false); |
| 2402 | } |
| 2403 | |
| 2404 | @Override |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2405 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroups( |
| 2406 | String pkg) { |
| 2407 | checkCallerIsSystemOrSameApp(pkg); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2408 | return mPreferencesHelper.getNotificationChannelGroups( |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 2409 | pkg, Binder.getCallingUid(), false, false, true); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2413 | public void deleteNotificationChannelGroup(String pkg, String groupId) { |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2414 | checkCallerIsSystemOrSameApp(pkg); |
| 2415 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2416 | final int callingUid = Binder.getCallingUid(); |
| 2417 | NotificationChannelGroup groupToDelete = |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2418 | mPreferencesHelper.getNotificationChannelGroup(groupId, pkg, callingUid); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2419 | if (groupToDelete != null) { |
| 2420 | List<NotificationChannel> deletedChannels = |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2421 | mPreferencesHelper.deleteNotificationChannelGroup(pkg, callingUid, groupId); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2422 | for (int i = 0; i < deletedChannels.size(); i++) { |
| 2423 | final NotificationChannel deletedChannel = deletedChannels.get(i); |
| 2424 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, deletedChannel.getId(), 0, 0, |
| 2425 | true, |
| 2426 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, |
| 2427 | null); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2428 | mListeners.notifyNotificationChannelChanged(pkg, |
| 2429 | UserHandle.getUserHandleForUid(callingUid), |
| 2430 | deletedChannel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2431 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
| 2432 | } |
| 2433 | mListeners.notifyNotificationChannelGroupChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2434 | pkg, UserHandle.getUserHandleForUid(callingUid), groupToDelete, |
| 2435 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2436 | savePolicyFile(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2437 | } |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2441 | public void updateNotificationChannelForPackage(String pkg, int uid, |
| 2442 | NotificationChannel channel) { |
Geoffrey Pitsch | 4dd5006 | 2016-12-06 16:41:22 -0500 | [diff] [blame] | 2443 | enforceSystemOrSystemUI("Caller not system or systemui"); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2444 | Preconditions.checkNotNull(channel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2445 | updateNotificationChannelInt(pkg, uid, channel, false); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | @Override |
| 2449 | public ParceledListSlice<NotificationChannel> getNotificationChannelsForPackage(String pkg, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2450 | int uid, boolean includeDeleted) { |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2451 | enforceSystemOrSystemUI("getNotificationChannelsForPackage"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2452 | return mPreferencesHelper.getNotificationChannels(pkg, uid, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2453 | } |
| 2454 | |
| 2455 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2456 | public int getNumNotificationChannelsForPackage(String pkg, int uid, |
| 2457 | boolean includeDeleted) { |
| 2458 | enforceSystemOrSystemUI("getNumNotificationChannelsForPackage"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2459 | return mPreferencesHelper.getNotificationChannels(pkg, uid, includeDeleted) |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2460 | .getList().size(); |
| 2461 | } |
| 2462 | |
| 2463 | @Override |
Julia Reynolds | 17717f5 | 2017-05-09 11:46:06 -0400 | [diff] [blame] | 2464 | public boolean onlyHasDefaultChannel(String pkg, int uid) { |
| 2465 | enforceSystemOrSystemUI("onlyHasDefaultChannel"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2466 | return mPreferencesHelper.onlyHasDefaultChannel(pkg, uid); |
Julia Reynolds | 17717f5 | 2017-05-09 11:46:06 -0400 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | @Override |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 2470 | public int getDeletedChannelCount(String pkg, int uid) { |
| 2471 | enforceSystemOrSystemUI("getDeletedChannelCount"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2472 | return mPreferencesHelper.getDeletedChannelCount(pkg, uid); |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | @Override |
Julia Reynolds | f2e499d | 2018-03-30 10:36:42 -0400 | [diff] [blame] | 2476 | public int getBlockedChannelCount(String pkg, int uid) { |
| 2477 | enforceSystemOrSystemUI("getBlockedChannelCount"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2478 | return mPreferencesHelper.getBlockedChannelCount(pkg, uid); |
Julia Reynolds | f2e499d | 2018-03-30 10:36:42 -0400 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2482 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroupsForPackage( |
| 2483 | String pkg, int uid, boolean includeDeleted) { |
| 2484 | checkCallerIsSystem(); |
Julia Reynolds | 13ed28b | 2018-09-21 15:20:13 -0400 | [diff] [blame] | 2485 | return mPreferencesHelper.getNotificationChannelGroups( |
| 2486 | pkg, uid, includeDeleted, true, false); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2490 | public NotificationChannelGroup getPopulatedNotificationChannelGroupForPackage( |
| 2491 | String pkg, int uid, String groupId, boolean includeDeleted) { |
| 2492 | enforceSystemOrSystemUI("getPopulatedNotificationChannelGroupForPackage"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2493 | return mPreferencesHelper.getNotificationChannelGroupWithChannels( |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2494 | pkg, uid, groupId, includeDeleted); |
| 2495 | } |
| 2496 | |
| 2497 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2498 | public NotificationChannelGroup getNotificationChannelGroupForPackage( |
| 2499 | String groupId, String pkg, int uid) { |
| 2500 | enforceSystemOrSystemUI("getNotificationChannelGroupForPackage"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2501 | return mPreferencesHelper.getNotificationChannelGroup(groupId, pkg, uid); |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2505 | public ParceledListSlice<NotificationChannel> getNotificationChannels(String pkg) { |
| 2506 | checkCallerIsSystemOrSameApp(pkg); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2507 | return mPreferencesHelper.getNotificationChannels( |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2508 | pkg, Binder.getCallingUid(), false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2509 | } |
| 2510 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2511 | @Override |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 2512 | public ParceledListSlice<NotifyingApp> getRecentNotifyingAppsForUser(int userId) { |
| 2513 | checkCallerIsSystem(); |
| 2514 | synchronized (mNotificationLock) { |
| 2515 | List<NotifyingApp> apps = new ArrayList<>( |
| 2516 | mRecentApps.getOrDefault(userId, new ArrayList<>())); |
| 2517 | return new ParceledListSlice<>(apps); |
| 2518 | } |
| 2519 | } |
| 2520 | |
| 2521 | @Override |
Julia Reynolds | e273f08 | 2018-04-12 13:48:49 -0400 | [diff] [blame] | 2522 | public int getBlockedAppCount(int userId) { |
| 2523 | checkCallerIsSystem(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2524 | return mPreferencesHelper.getBlockedAppCount(userId); |
Julia Reynolds | e273f08 | 2018-04-12 13:48:49 -0400 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | @Override |
Beverly | 86d076f | 2018-04-17 14:44:52 -0400 | [diff] [blame] | 2528 | public boolean areChannelsBypassingDnd() { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2529 | return mPreferencesHelper.areChannelsBypassingDnd(); |
Beverly | 86d076f | 2018-04-17 14:44:52 -0400 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | @Override |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2533 | public void clearData(String packageName, int uid, boolean fromApp) throws RemoteException { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2534 | checkCallerIsSystem(); |
| 2535 | |
| 2536 | // Cancel posted notifications |
| 2537 | cancelAllNotificationsInt(MY_UID, MY_PID, packageName, null, 0, 0, true, |
| 2538 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, null); |
| 2539 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2540 | final String[] packages = new String[] {packageName}; |
| 2541 | final int[] uids = new int[] {uid}; |
| 2542 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2543 | // Listener & assistant |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2544 | mListeners.onPackagesChanged(true, packages, uids); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2545 | mAssistants.onPackagesChanged(true, packages, uids); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2546 | |
| 2547 | // Zen |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2548 | mConditionProviders.onPackagesChanged(true, packages, uids); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2549 | |
| 2550 | // Reset notification preferences |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2551 | if (!fromApp) { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 2552 | mPreferencesHelper.onPackagesChanged( |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2553 | true, UserHandle.getCallingUserId(), packages, uids); |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2554 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2555 | |
| 2556 | savePolicyFile(); |
| 2557 | } |
| 2558 | |
| 2559 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2560 | /** |
| 2561 | * System-only API for getting a list of current (i.e. not cleared) notifications. |
| 2562 | * |
| 2563 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2564 | * @returns A list of all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2565 | */ |
| 2566 | @Override |
| 2567 | public StatusBarNotification[] getActiveNotifications(String callingPkg) { |
| 2568 | // enforce() will ensure the calling uid has the correct permission |
| 2569 | getContext().enforceCallingOrSelfPermission( |
| 2570 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 2571 | "NotificationManagerService.getActiveNotifications"); |
| 2572 | |
| 2573 | StatusBarNotification[] tmp = null; |
| 2574 | int uid = Binder.getCallingUid(); |
| 2575 | |
| 2576 | // noteOp will check to make sure the callingPkg matches the uid |
| 2577 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 2578 | == AppOpsManager.MODE_ALLOWED) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2579 | synchronized (mNotificationLock) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2580 | tmp = new StatusBarNotification[mNotificationList.size()]; |
| 2581 | final int N = mNotificationList.size(); |
| 2582 | for (int i=0; i<N; i++) { |
| 2583 | tmp[i] = mNotificationList.get(i).sbn; |
| 2584 | } |
| 2585 | } |
| 2586 | } |
| 2587 | return tmp; |
| 2588 | } |
| 2589 | |
| 2590 | /** |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2591 | * Public API for getting a list of current notifications for the calling package/uid. |
| 2592 | * |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 2593 | * Note that since notification posting is done asynchronously, this will not return |
| 2594 | * notifications that are in the process of being posted. |
| 2595 | * |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2596 | * @returns A list of all the package's notifications, in natural order. |
| 2597 | */ |
| 2598 | @Override |
| 2599 | public ParceledListSlice<StatusBarNotification> getAppActiveNotifications(String pkg, |
| 2600 | int incomingUserId) { |
| 2601 | checkCallerIsSystemOrSameApp(pkg); |
| 2602 | int userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2603 | Binder.getCallingUid(), incomingUserId, true, false, |
| 2604 | "getAppActiveNotifications", pkg); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2605 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2606 | final ArrayMap<String, StatusBarNotification> map |
| 2607 | = new ArrayMap<>(mNotificationList.size() + mEnqueuedNotifications.size()); |
Erik Wolsheimer | 2242b4d | 2015-11-24 13:22:04 -0800 | [diff] [blame] | 2608 | final int N = mNotificationList.size(); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2609 | for (int i = 0; i < N; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2610 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 2611 | mNotificationList.get(i).sbn); |
| 2612 | if (sbn != null) { |
| 2613 | map.put(sbn.getKey(), sbn); |
| 2614 | } |
| 2615 | } |
| 2616 | for(NotificationRecord snoozed: mSnoozeHelper.getSnoozed(userId, pkg)) { |
| 2617 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, snoozed.sbn); |
| 2618 | if (sbn != null) { |
| 2619 | map.put(sbn.getKey(), sbn); |
| 2620 | } |
| 2621 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2622 | final int M = mEnqueuedNotifications.size(); |
| 2623 | for (int i = 0; i < M; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2624 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 2625 | mEnqueuedNotifications.get(i).sbn); |
| 2626 | if (sbn != null) { |
| 2627 | map.put(sbn.getKey(), sbn); // pending update overwrites existing post here |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2628 | } |
| 2629 | } |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2630 | final ArrayList<StatusBarNotification> list = new ArrayList<>(map.size()); |
| 2631 | list.addAll(map.values()); |
| 2632 | return new ParceledListSlice<StatusBarNotification>(list); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2633 | } |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2634 | } |
| 2635 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2636 | private StatusBarNotification sanitizeSbn(String pkg, int userId, |
| 2637 | StatusBarNotification sbn) { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 2638 | if (sbn.getPackageName().equals(pkg) && sbn.getUserId() == userId) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2639 | // We could pass back a cloneLight() but clients might get confused and |
| 2640 | // try to send this thing back to notify() again, which would not work |
| 2641 | // very well. |
| 2642 | return new StatusBarNotification( |
| 2643 | sbn.getPackageName(), |
| 2644 | sbn.getOpPkg(), |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2645 | sbn.getId(), sbn.getTag(), sbn.getUid(), sbn.getInitialPid(), |
| 2646 | sbn.getNotification().clone(), |
| 2647 | sbn.getUser(), sbn.getOverrideGroupKey(), sbn.getPostTime()); |
| 2648 | } |
| 2649 | return null; |
| 2650 | } |
| 2651 | |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2652 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2653 | * System-only API for getting a list of recent (cleared, no longer shown) notifications. |
| 2654 | * |
| 2655 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
| 2656 | */ |
| 2657 | @Override |
| 2658 | public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) { |
| 2659 | // enforce() will ensure the calling uid has the correct permission |
| 2660 | getContext().enforceCallingOrSelfPermission( |
| 2661 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 2662 | "NotificationManagerService.getHistoricalNotifications"); |
| 2663 | |
| 2664 | StatusBarNotification[] tmp = null; |
| 2665 | int uid = Binder.getCallingUid(); |
| 2666 | |
| 2667 | // noteOp will check to make sure the callingPkg matches the uid |
| 2668 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 2669 | == AppOpsManager.MODE_ALLOWED) { |
| 2670 | synchronized (mArchive) { |
| 2671 | tmp = mArchive.getArray(count); |
| 2672 | } |
| 2673 | } |
| 2674 | return tmp; |
| 2675 | } |
| 2676 | |
| 2677 | /** |
| 2678 | * Register a listener binder directly with the notification manager. |
| 2679 | * |
| 2680 | * Only works with system callers. Apps should extend |
| 2681 | * {@link android.service.notification.NotificationListenerService}. |
| 2682 | */ |
| 2683 | @Override |
| 2684 | public void registerListener(final INotificationListener listener, |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 2685 | final ComponentName component, final int userid) { |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 2686 | enforceSystemOrSystemUI("INotificationManager.registerListener"); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 2687 | mListeners.registerService(listener, component, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | /** |
| 2691 | * Remove a listener binder directly |
| 2692 | */ |
| 2693 | @Override |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 2694 | public void unregisterListener(INotificationListener token, int userid) { |
Chris Wren | b7c8109 | 2016-03-10 11:41:10 -0500 | [diff] [blame] | 2695 | mListeners.unregisterService(token, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2696 | } |
| 2697 | |
| 2698 | /** |
| 2699 | * Allow an INotificationListener to simulate a "clear all" operation. |
| 2700 | * |
| 2701 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications} |
| 2702 | * |
| 2703 | * @param token The binder for the listener, to check that the caller is allowed |
| 2704 | */ |
| 2705 | @Override |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2706 | public void cancelNotificationsFromListener(INotificationListener token, String[] keys) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2707 | final int callingUid = Binder.getCallingUid(); |
| 2708 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2709 | long identity = Binder.clearCallingIdentity(); |
| 2710 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2711 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2712 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 2713 | |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2714 | if (keys != null) { |
| 2715 | final int N = keys.length; |
| 2716 | for (int i = 0; i < N; i++) { |
| 2717 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 2718 | if (r == null) continue; |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2719 | final int userId = r.sbn.getUserId(); |
| 2720 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 2721 | !mUserProfiles.isCurrentProfile(userId)) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2722 | throw new SecurityException("Disallowed call from listener: " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2723 | + info.service); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2724 | } |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 2725 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2726 | r.sbn.getPackageName(), r.sbn.getTag(), r.sbn.getId(), |
| 2727 | userId); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2728 | } |
| 2729 | } else { |
| 2730 | cancelAllLocked(callingUid, callingPid, info.userid, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2731 | REASON_LISTENER_CANCEL_ALL, info, info.supportsProfiles()); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2732 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2733 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2734 | } finally { |
| 2735 | Binder.restoreCallingIdentity(identity); |
| 2736 | } |
| 2737 | } |
| 2738 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2739 | /** |
| 2740 | * Handle request from an approved listener to re-enable itself. |
| 2741 | * |
| 2742 | * @param component The componenet to be re-enabled, caller must match package. |
| 2743 | */ |
| 2744 | @Override |
| 2745 | public void requestBindListener(ComponentName component) { |
| 2746 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 2747 | long identity = Binder.clearCallingIdentity(); |
| 2748 | try { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2749 | ManagedServices manager = |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2750 | mAssistants.isComponentEnabledForCurrentProfiles(component) |
| 2751 | ? mAssistants |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2752 | : mListeners; |
| 2753 | manager.setComponentState(component, true); |
| 2754 | } finally { |
| 2755 | Binder.restoreCallingIdentity(identity); |
| 2756 | } |
| 2757 | } |
| 2758 | |
| 2759 | @Override |
| 2760 | public void requestUnbindListener(INotificationListener token) { |
| 2761 | long identity = Binder.clearCallingIdentity(); |
| 2762 | try { |
| 2763 | // allow bound services to disable themselves |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2764 | synchronized (mNotificationLock) { |
| 2765 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2766 | info.getOwner().setComponentState(info.component, false); |
| 2767 | } |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2768 | } finally { |
| 2769 | Binder.restoreCallingIdentity(identity); |
| 2770 | } |
| 2771 | } |
| 2772 | |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2773 | @Override |
| 2774 | public void setNotificationsShownFromListener(INotificationListener token, String[] keys) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2775 | long identity = Binder.clearCallingIdentity(); |
| 2776 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2777 | synchronized (mNotificationLock) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2778 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 2779 | if (keys == null) { |
| 2780 | return; |
| 2781 | } |
| 2782 | ArrayList<NotificationRecord> seen = new ArrayList<>(); |
| 2783 | final int n = keys.length; |
| 2784 | for (int i = 0; i < n; i++) { |
| 2785 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
| 2786 | if (r == null) continue; |
| 2787 | final int userId = r.sbn.getUserId(); |
| 2788 | if (userId != info.userid && userId != UserHandle.USER_ALL |
| 2789 | && !mUserProfiles.isCurrentProfile(userId)) { |
| 2790 | throw new SecurityException("Disallowed call from listener: " |
| 2791 | + info.service); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2792 | } |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 2793 | seen.add(r); |
| 2794 | if (!r.isSeen()) { |
| 2795 | if (DBG) Slog.d(TAG, "Marking notification as seen " + keys[i]); |
| 2796 | reportSeen(r); |
| 2797 | r.setSeen(); |
| 2798 | maybeRecordInterruptionLocked(r); |
| 2799 | } |
| 2800 | } |
| 2801 | if (!seen.isEmpty()) { |
| 2802 | mAssistants.onNotificationsSeenLocked(seen); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2803 | } |
| 2804 | } |
| 2805 | } finally { |
| 2806 | Binder.restoreCallingIdentity(identity); |
| 2807 | } |
| 2808 | } |
| 2809 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2810 | /** |
| 2811 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2812 | * |
| 2813 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2814 | * |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2815 | * @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] | 2816 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2817 | @GuardedBy("mNotificationLock") |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2818 | private void cancelNotificationFromListenerLocked(ManagedServiceInfo info, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2819 | int callingUid, int callingPid, String pkg, String tag, int id, int userId) { |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2820 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 2821 | Notification.FLAG_ONGOING_EVENT | FLAG_FOREGROUND_SERVICE, |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2822 | true, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2823 | userId, REASON_LISTENER_CANCEL, info); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2824 | } |
| 2825 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2826 | /** |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2827 | * Allow an INotificationListener to snooze a single notification until a context. |
| 2828 | * |
| 2829 | * @param token The binder for the listener, to check that the caller is allowed |
| 2830 | */ |
| 2831 | @Override |
| 2832 | public void snoozeNotificationUntilContextFromListener(INotificationListener token, |
| 2833 | String key, String snoozeCriterionId) { |
| 2834 | long identity = Binder.clearCallingIdentity(); |
| 2835 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2836 | synchronized (mNotificationLock) { |
| 2837 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2838 | snoozeNotificationInt(key, SNOOZE_UNTIL_UNSPECIFIED, snoozeCriterionId, info); |
| 2839 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2840 | } finally { |
| 2841 | Binder.restoreCallingIdentity(identity); |
| 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | /** |
| 2846 | * Allow an INotificationListener to snooze a single notification until a time. |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2847 | * |
| 2848 | * @param token The binder for the listener, to check that the caller is allowed |
| 2849 | */ |
| 2850 | @Override |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2851 | public void snoozeNotificationUntilFromListener(INotificationListener token, String key, |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 2852 | long duration) { |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2853 | long identity = Binder.clearCallingIdentity(); |
| 2854 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2855 | synchronized (mNotificationLock) { |
| 2856 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2857 | snoozeNotificationInt(key, duration, null, info); |
| 2858 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2859 | } finally { |
| 2860 | Binder.restoreCallingIdentity(identity); |
| 2861 | } |
| 2862 | } |
| 2863 | |
| 2864 | /** |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2865 | * Allows the notification assistant to un-snooze a single notification. |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2866 | * |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2867 | * @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] | 2868 | */ |
| 2869 | @Override |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2870 | public void unsnoozeNotificationFromAssistant(INotificationListener token, String key) { |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2871 | long identity = Binder.clearCallingIdentity(); |
| 2872 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2873 | synchronized (mNotificationLock) { |
| 2874 | final ManagedServiceInfo info = |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2875 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2876 | unsnoozeNotificationInt(key, info); |
| 2877 | } |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2878 | } finally { |
| 2879 | Binder.restoreCallingIdentity(identity); |
| 2880 | } |
| 2881 | } |
| 2882 | |
| 2883 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2884 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2885 | * |
| 2886 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2887 | * |
| 2888 | * @param token The binder for the listener, to check that the caller is allowed |
| 2889 | */ |
| 2890 | @Override |
| 2891 | public void cancelNotificationFromListener(INotificationListener token, String pkg, |
| 2892 | String tag, int id) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2893 | final int callingUid = Binder.getCallingUid(); |
| 2894 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2895 | long identity = Binder.clearCallingIdentity(); |
| 2896 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2897 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2898 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2899 | if (info.supportsProfiles()) { |
| 2900 | Log.e(TAG, "Ignoring deprecated cancelNotification(pkg, tag, id) " |
| 2901 | + "from " + info.component |
| 2902 | + " use cancelNotification(key) instead."); |
| 2903 | } else { |
| 2904 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2905 | pkg, tag, id, info.userid); |
| 2906 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2907 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2908 | } finally { |
| 2909 | Binder.restoreCallingIdentity(identity); |
| 2910 | } |
| 2911 | } |
| 2912 | |
| 2913 | /** |
| 2914 | * Allow an INotificationListener to request the list of outstanding notifications seen by |
| 2915 | * the current user. Useful when starting up, after which point the listener callbacks |
| 2916 | * should be used. |
| 2917 | * |
| 2918 | * @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] | 2919 | * @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] | 2920 | * @returns The return value will contain the notifications specified in keys, in that |
| 2921 | * order, or if keys is null, all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2922 | */ |
| 2923 | @Override |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2924 | public ParceledListSlice<StatusBarNotification> getActiveNotificationsFromListener( |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2925 | INotificationListener token, String[] keys, int trim) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2926 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2927 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2928 | final boolean getKeys = keys != null; |
| 2929 | final int N = getKeys ? keys.length : mNotificationList.size(); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2930 | final ArrayList<StatusBarNotification> list |
| 2931 | = new ArrayList<StatusBarNotification>(N); |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2932 | for (int i=0; i<N; i++) { |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2933 | final NotificationRecord r = getKeys |
| 2934 | ? mNotificationsByKey.get(keys[i]) |
| 2935 | : mNotificationList.get(i); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2936 | if (r == null) continue; |
| 2937 | StatusBarNotification sbn = r.sbn; |
| 2938 | if (!isVisibleToListener(sbn, info)) continue; |
| 2939 | StatusBarNotification sbnToSend = |
| 2940 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2941 | list.add(sbnToSend); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2942 | } |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2943 | return new ParceledListSlice<StatusBarNotification>(list); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2944 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2945 | } |
| 2946 | |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2947 | /** |
| 2948 | * Allow an INotificationListener to request the list of outstanding snoozed notifications |
| 2949 | * seen by the current user. Useful when starting up, after which point the listener |
| 2950 | * callbacks should be used. |
| 2951 | * |
| 2952 | * @param token The binder for the listener, to check that the caller is allowed |
| 2953 | * @returns The return value will contain the notifications specified in keys, in that |
| 2954 | * order, or if keys is null, all the notifications, in natural order. |
| 2955 | */ |
| 2956 | @Override |
| 2957 | public ParceledListSlice<StatusBarNotification> getSnoozedNotificationsFromListener( |
| 2958 | INotificationListener token, int trim) { |
| 2959 | synchronized (mNotificationLock) { |
| 2960 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2961 | List<NotificationRecord> snoozedRecords = mSnoozeHelper.getSnoozed(); |
| 2962 | final int N = snoozedRecords.size(); |
| 2963 | final ArrayList<StatusBarNotification> list = new ArrayList<>(N); |
| 2964 | for (int i=0; i < N; i++) { |
| 2965 | final NotificationRecord r = snoozedRecords.get(i); |
| 2966 | if (r == null) continue; |
| 2967 | StatusBarNotification sbn = r.sbn; |
| 2968 | if (!isVisibleToListener(sbn, info)) continue; |
| 2969 | StatusBarNotification sbnToSend = |
| 2970 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2971 | list.add(sbnToSend); |
| 2972 | } |
| 2973 | return new ParceledListSlice<>(list); |
| 2974 | } |
| 2975 | } |
| 2976 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2977 | @Override |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 2978 | public void clearRequestedListenerHints(INotificationListener token) { |
| 2979 | final long identity = Binder.clearCallingIdentity(); |
| 2980 | try { |
| 2981 | synchronized (mNotificationLock) { |
| 2982 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2983 | removeDisabledHints(info); |
| 2984 | updateListenerHintsLocked(); |
| 2985 | updateEffectsSuppressorLocked(); |
| 2986 | } |
| 2987 | } finally { |
| 2988 | Binder.restoreCallingIdentity(identity); |
| 2989 | } |
| 2990 | } |
| 2991 | |
| 2992 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2993 | public void requestHintsFromListener(INotificationListener token, int hints) { |
| 2994 | final long identity = Binder.clearCallingIdentity(); |
| 2995 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2996 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2997 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2998 | final int disableEffectsMask = HINT_HOST_DISABLE_EFFECTS |
| 2999 | | HINT_HOST_DISABLE_NOTIFICATION_EFFECTS |
| 3000 | | HINT_HOST_DISABLE_CALL_EFFECTS; |
| 3001 | final boolean disableEffects = (hints & disableEffectsMask) != 0; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3002 | if (disableEffects) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 3003 | addDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3004 | } else { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 3005 | removeDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3006 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3007 | updateListenerHintsLocked(); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 3008 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3009 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3010 | } finally { |
| 3011 | Binder.restoreCallingIdentity(identity); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3016 | public int getHintsFromListener(INotificationListener token) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3017 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3018 | return mListenerHints; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3019 | } |
| 3020 | } |
| 3021 | |
| 3022 | @Override |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 3023 | public void requestInterruptionFilterFromListener(INotificationListener token, |
| 3024 | int interruptionFilter) throws RemoteException { |
| 3025 | final long identity = Binder.clearCallingIdentity(); |
| 3026 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3027 | synchronized (mNotificationLock) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 3028 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 3029 | mZenModeHelper.requestFromListener(info.component, interruptionFilter); |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 3030 | updateInterruptionFilterLocked(); |
| 3031 | } |
| 3032 | } finally { |
| 3033 | Binder.restoreCallingIdentity(identity); |
| 3034 | } |
| 3035 | } |
| 3036 | |
| 3037 | @Override |
| 3038 | public int getInterruptionFilterFromListener(INotificationListener token) |
| 3039 | throws RemoteException { |
| 3040 | synchronized (mNotificationLight) { |
| 3041 | return mInterruptionFilter; |
| 3042 | } |
| 3043 | } |
| 3044 | |
| 3045 | @Override |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 3046 | public void setOnNotificationPostedTrimFromListener(INotificationListener token, int trim) |
| 3047 | throws RemoteException { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3048 | synchronized (mNotificationLock) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 3049 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 3050 | if (info == null) return; |
| 3051 | mListeners.setOnNotificationPostedTrimLocked(info, trim); |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 3056 | public int getZenMode() { |
| 3057 | return mZenModeHelper.getZenMode(); |
| 3058 | } |
| 3059 | |
| 3060 | @Override |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 3061 | public ZenModeConfig getZenModeConfig() { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 3062 | enforceSystemOrSystemUI("INotificationManager.getZenModeConfig"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 3063 | return mZenModeHelper.getConfig(); |
| 3064 | } |
| 3065 | |
| 3066 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 3067 | public void setZenMode(int mode, Uri conditionId, String reason) throws RemoteException { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 3068 | enforceSystemOrSystemUI("INotificationManager.setZenMode"); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 3069 | final long identity = Binder.clearCallingIdentity(); |
| 3070 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 3071 | mZenModeHelper.setManualZenMode(mode, conditionId, null, reason); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 3072 | } finally { |
| 3073 | Binder.restoreCallingIdentity(identity); |
| 3074 | } |
| 3075 | } |
| 3076 | |
| 3077 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 3078 | public List<ZenModeConfig.ZenRule> getZenRules() throws RemoteException { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3079 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRules"); |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 3080 | return mZenModeHelper.getZenRules(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3081 | } |
| 3082 | |
| 3083 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3084 | public AutomaticZenRule getAutomaticZenRule(String id) throws RemoteException { |
| 3085 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3086 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRule"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3087 | return mZenModeHelper.getAutomaticZenRule(id); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3088 | } |
| 3089 | |
| 3090 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 3091 | public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3092 | throws RemoteException { |
| 3093 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 3094 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 3095 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 3096 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3097 | enforcePolicyAccess(Binder.getCallingUid(), "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3098 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3099 | return mZenModeHelper.addAutomaticZenRule(automaticZenRule, |
| 3100 | "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 3104 | public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3105 | throws RemoteException { |
| 3106 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 3107 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 3108 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 3109 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
| 3110 | enforcePolicyAccess(Binder.getCallingUid(), "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3111 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 3112 | return mZenModeHelper.updateAutomaticZenRule(id, automaticZenRule, |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3113 | "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3117 | public boolean removeAutomaticZenRule(String id) throws RemoteException { |
| 3118 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3119 | // Verify that they can modify zen rules. |
| 3120 | enforcePolicyAccess(Binder.getCallingUid(), "removeAutomaticZenRule"); |
| 3121 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 3122 | return mZenModeHelper.removeAutomaticZenRule(id, "removeAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | @Override |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 3126 | public boolean removeAutomaticZenRules(String packageName) throws RemoteException { |
| 3127 | Preconditions.checkNotNull(packageName, "Package name is null"); |
| 3128 | enforceSystemOrSystemUI("removeAutomaticZenRules"); |
| 3129 | |
| 3130 | return mZenModeHelper.removeAutomaticZenRules(packageName, "removeAutomaticZenRules"); |
| 3131 | } |
| 3132 | |
| 3133 | @Override |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 3134 | public int getRuleInstanceCount(ComponentName owner) throws RemoteException { |
| 3135 | Preconditions.checkNotNull(owner, "Owner is null"); |
| 3136 | enforceSystemOrSystemUI("getRuleInstanceCount"); |
| 3137 | |
| 3138 | return mZenModeHelper.getCurrentInstanceCount(owner); |
| 3139 | } |
| 3140 | |
| 3141 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3142 | public void setInterruptionFilter(String pkg, int filter) throws RemoteException { |
| 3143 | enforcePolicyAccess(pkg, "setInterruptionFilter"); |
| 3144 | final int zen = NotificationManager.zenModeFromInterruptionFilter(filter, -1); |
| 3145 | if (zen == -1) throw new IllegalArgumentException("Invalid filter: " + filter); |
| 3146 | final long identity = Binder.clearCallingIdentity(); |
| 3147 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 3148 | mZenModeHelper.setManualZenMode(zen, null, pkg, "setInterruptionFilter"); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3149 | } finally { |
| 3150 | Binder.restoreCallingIdentity(identity); |
| 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | @Override |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 3155 | public void notifyConditions(final String pkg, IConditionProvider provider, |
| 3156 | final Condition[] conditions) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3157 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 3158 | checkCallerIsSystemOrSameApp(pkg); |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 3159 | mHandler.post(new Runnable() { |
| 3160 | @Override |
| 3161 | public void run() { |
| 3162 | mConditionProviders.notifyConditions(pkg, info, conditions); |
| 3163 | } |
| 3164 | }); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3165 | } |
| 3166 | |
Julia Reynolds | 38e6ca4 | 2016-08-08 08:38:09 -0400 | [diff] [blame] | 3167 | @Override |
| 3168 | public void requestUnbindProvider(IConditionProvider provider) { |
| 3169 | long identity = Binder.clearCallingIdentity(); |
| 3170 | try { |
| 3171 | // allow bound services to disable themselves |
| 3172 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 3173 | info.getOwner().setComponentState(info.component, false); |
| 3174 | } finally { |
| 3175 | Binder.restoreCallingIdentity(identity); |
| 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | @Override |
| 3180 | public void requestBindProvider(ComponentName component) { |
| 3181 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 3182 | long identity = Binder.clearCallingIdentity(); |
| 3183 | try { |
| 3184 | mConditionProviders.setComponentState(component, true); |
| 3185 | } finally { |
| 3186 | Binder.restoreCallingIdentity(identity); |
| 3187 | } |
| 3188 | } |
| 3189 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3190 | private void enforceSystemOrSystemUI(String message) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3191 | if (isCallerSystemOrPhone()) return; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3192 | getContext().enforceCallingPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 3193 | message); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3194 | } |
| 3195 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 3196 | private void enforceSystemOrSystemUIOrSamePackage(String pkg, String message) { |
| 3197 | try { |
| 3198 | checkCallerIsSystemOrSameApp(pkg); |
| 3199 | } catch (SecurityException e) { |
| 3200 | getContext().enforceCallingPermission( |
| 3201 | android.Manifest.permission.STATUS_BAR_SERVICE, |
| 3202 | message); |
| 3203 | } |
| 3204 | } |
| 3205 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3206 | private void enforcePolicyAccess(int uid, String method) { |
| 3207 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 3208 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 3209 | return; |
| 3210 | } |
| 3211 | boolean accessAllowed = false; |
| 3212 | String[] packages = getContext().getPackageManager().getPackagesForUid(uid); |
| 3213 | final int packageCount = packages.length; |
| 3214 | for (int i = 0; i < packageCount; i++) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3215 | if (mConditionProviders.isPackageOrComponentAllowed( |
| 3216 | packages[i], UserHandle.getUserId(uid))) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3217 | accessAllowed = true; |
| 3218 | } |
| 3219 | } |
| 3220 | if (!accessAllowed) { |
| 3221 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 3222 | throw new SecurityException("Notification policy access denied"); |
| 3223 | } |
| 3224 | } |
| 3225 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3226 | private void enforcePolicyAccess(String pkg, String method) { |
Julia Reynolds | 6ee2617 | 2015-09-28 11:34:48 -0400 | [diff] [blame] | 3227 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 3228 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 3229 | return; |
| 3230 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 3231 | checkCallerIsSameApp(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3232 | if (!checkPolicyAccess(pkg)) { |
| 3233 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 3234 | throw new SecurityException("Notification policy access denied"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3235 | } |
| 3236 | } |
| 3237 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3238 | private boolean checkPackagePolicyAccess(String pkg) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3239 | return mConditionProviders.isPackageOrComponentAllowed( |
| 3240 | pkg, getCallingUserHandle().getIdentifier()); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3241 | } |
| 3242 | |
| 3243 | private boolean checkPolicyAccess(String pkg) { |
Julia Reynolds | 0867b3a | 2016-03-30 17:29:54 -0400 | [diff] [blame] | 3244 | try { |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 3245 | int uid = getContext().getPackageManager().getPackageUidAsUser(pkg, |
| 3246 | UserHandle.getCallingUserId()); |
Julia Reynolds | 0867b3a | 2016-03-30 17:29:54 -0400 | [diff] [blame] | 3247 | if (PackageManager.PERMISSION_GRANTED == ActivityManager.checkComponentPermission( |
| 3248 | android.Manifest.permission.MANAGE_NOTIFICATIONS, uid, |
| 3249 | -1, true)) { |
| 3250 | return true; |
| 3251 | } |
| 3252 | } catch (NameNotFoundException e) { |
| 3253 | return false; |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 3254 | } |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 3255 | return checkPackagePolicyAccess(pkg) |
| 3256 | || mListeners.isComponentEnabledForPackage(pkg) |
| 3257 | || (mDpm != null && |
| 3258 | mDpm.isActiveAdminWithPolicy(Binder.getCallingUid(), |
| 3259 | DeviceAdminInfo.USES_POLICY_PROFILE_OWNER)); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3260 | } |
| 3261 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3262 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3263 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 3264 | if (!DumpUtils.checkDumpAndUsageStatsPermission(getContext(), TAG, pw)) return; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3265 | final DumpFilter filter = DumpFilter.parseFromArguments(args); |
Makoto Onuki | bbb4b22 | 2018-06-25 16:01:02 -0700 | [diff] [blame] | 3266 | final long token = Binder.clearCallingIdentity(); |
| 3267 | try { |
| 3268 | if (filter.stats) { |
| 3269 | dumpJson(pw, filter); |
| 3270 | } else if (filter.proto) { |
| 3271 | dumpProto(fd, filter); |
| 3272 | } else if (filter.criticalPriority) { |
| 3273 | dumpNotificationRecords(pw, filter); |
| 3274 | } else { |
| 3275 | dumpImpl(pw, filter); |
| 3276 | } |
| 3277 | } finally { |
| 3278 | Binder.restoreCallingIdentity(token); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3279 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3280 | } |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 3281 | |
| 3282 | @Override |
| 3283 | public ComponentName getEffectsSuppressor() { |
Bryce Lee | ba3d895 | 2016-04-12 12:39:15 -0700 | [diff] [blame] | 3284 | return !mEffectsSuppressors.isEmpty() ? mEffectsSuppressors.get(0) : null; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 3285 | } |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 3286 | |
| 3287 | @Override |
| 3288 | public boolean matchesCallFilter(Bundle extras) { |
| 3289 | enforceSystemOrSystemUI("INotificationManager.matchesCallFilter"); |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 3290 | return mZenModeHelper.matchesCallFilter( |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3291 | Binder.getCallingUserHandle(), |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 3292 | extras, |
| 3293 | mRankingHelper.findExtractor(ValidateNotificationPeople.class), |
| 3294 | MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS, |
| 3295 | MATCHES_CALL_FILTER_TIMEOUT_AFFINITY); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 3296 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 3297 | |
| 3298 | @Override |
| 3299 | public boolean isSystemConditionProviderEnabled(String path) { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 3300 | enforceSystemOrSystemUI("INotificationManager.isSystemConditionProviderEnabled"); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 3301 | return mConditionProviders.isSystemProviderEnabled(path); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 3302 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3303 | |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 3304 | // Backup/restore interface |
| 3305 | @Override |
| 3306 | public byte[] getBackupPayload(int user) { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3307 | checkCallerIsSystem(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3308 | if (DBG) Slog.d(TAG, "getBackupPayload u=" + user); |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 3309 | //TODO: http://b/22388012 |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3310 | if (user != USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3311 | Slog.w(TAG, "getBackupPayload: cannot backup policy for user " + user); |
| 3312 | return null; |
| 3313 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 3314 | synchronized(mPolicyFile) { |
| 3315 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 3316 | try { |
| 3317 | writePolicyXml(baos, true /*forBackup*/); |
| 3318 | return baos.toByteArray(); |
| 3319 | } catch (IOException e) { |
| 3320 | Slog.w(TAG, "getBackupPayload: error writing payload for user " + user, e); |
| 3321 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3322 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 3323 | return null; |
| 3324 | } |
| 3325 | |
| 3326 | @Override |
| 3327 | public void applyRestore(byte[] payload, int user) { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3328 | checkCallerIsSystem(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3329 | if (DBG) Slog.d(TAG, "applyRestore u=" + user + " payload=" |
| 3330 | + (payload != null ? new String(payload, StandardCharsets.UTF_8) : null)); |
| 3331 | if (payload == null) { |
| 3332 | Slog.w(TAG, "applyRestore: no payload to restore for user " + user); |
| 3333 | return; |
| 3334 | } |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 3335 | //TODO: http://b/22388012 |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3336 | if (user != USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3337 | Slog.w(TAG, "applyRestore: cannot restore policy for user " + user); |
| 3338 | return; |
| 3339 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 3340 | synchronized(mPolicyFile) { |
| 3341 | final ByteArrayInputStream bais = new ByteArrayInputStream(payload); |
| 3342 | try { |
| 3343 | readPolicyXml(bais, true /*forRestore*/); |
| 3344 | savePolicyFile(); |
| 3345 | } catch (NumberFormatException | XmlPullParserException | IOException e) { |
| 3346 | Slog.w(TAG, "applyRestore: error reading payload", e); |
| 3347 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3348 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 3349 | } |
| 3350 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3351 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3352 | public boolean isNotificationPolicyAccessGranted(String pkg) { |
| 3353 | return checkPolicyAccess(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3354 | } |
| 3355 | |
| 3356 | @Override |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 3357 | public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {; |
| 3358 | enforceSystemOrSystemUIOrSamePackage(pkg, |
| 3359 | "request policy access status for another package"); |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 3360 | return checkPolicyAccess(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3364 | public void setNotificationPolicyAccessGranted(String pkg, boolean granted) |
| 3365 | throws RemoteException { |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3366 | setNotificationPolicyAccessGrantedForUser( |
| 3367 | pkg, getCallingUserHandle().getIdentifier(), granted); |
| 3368 | } |
| 3369 | |
| 3370 | @Override |
| 3371 | public void setNotificationPolicyAccessGrantedForUser( |
| 3372 | String pkg, int userId, boolean granted) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3373 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3374 | final long identity = Binder.clearCallingIdentity(); |
| 3375 | try { |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3376 | if (mAllowedManagedServicePackages.test(pkg)) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3377 | mConditionProviders.setPackageOrComponentEnabled( |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3378 | pkg, userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3379 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3380 | getContext().sendBroadcastAsUser(new Intent( |
| 3381 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 3382 | .setPackage(pkg) |
| 3383 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3384 | UserHandle.of(userId), null); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3385 | savePolicyFile(); |
| 3386 | } |
| 3387 | } finally { |
| 3388 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3389 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3390 | } |
| 3391 | |
| 3392 | @Override |
| 3393 | public Policy getNotificationPolicy(String pkg) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3394 | final long identity = Binder.clearCallingIdentity(); |
| 3395 | try { |
| 3396 | return mZenModeHelper.getNotificationPolicy(); |
| 3397 | } finally { |
| 3398 | Binder.restoreCallingIdentity(identity); |
| 3399 | } |
| 3400 | } |
| 3401 | |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 3402 | @Override |
| 3403 | public Policy getConsolidatedNotificationPolicy() { |
| 3404 | final long identity = Binder.clearCallingIdentity(); |
| 3405 | try { |
| 3406 | return mZenModeHelper.getConsolidatedNotificationPolicy(); |
| 3407 | } finally { |
| 3408 | Binder.restoreCallingIdentity(identity); |
| 3409 | } |
| 3410 | } |
| 3411 | |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3412 | /** |
| 3413 | * Sets the notification policy. Apps that target API levels below |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3414 | * {@link android.os.Build.VERSION_CODES#P} cannot change user-designated values to |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3415 | * allow or disallow {@link Policy#PRIORITY_CATEGORY_ALARMS}, |
| 3416 | * {@link Policy#PRIORITY_CATEGORY_SYSTEM} and |
| 3417 | * {@link Policy#PRIORITY_CATEGORY_MEDIA} from bypassing dnd |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3418 | */ |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3419 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3420 | public void setNotificationPolicy(String pkg, Policy policy) { |
| 3421 | enforcePolicyAccess(pkg, "setNotificationPolicy"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3422 | final long identity = Binder.clearCallingIdentity(); |
| 3423 | try { |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3424 | final ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(pkg, |
| 3425 | 0, UserHandle.getUserId(MY_UID)); |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3426 | Policy currPolicy = mZenModeHelper.getNotificationPolicy(); |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3427 | |
Jeff Sharkey | aa1a911 | 2018-04-10 15:18:12 -0600 | [diff] [blame] | 3428 | if (applicationInfo.targetSdkVersion < Build.VERSION_CODES.P) { |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3429 | int priorityCategories = policy.priorityCategories; |
| 3430 | // ignore alarm and media values from new policy |
| 3431 | priorityCategories &= ~Policy.PRIORITY_CATEGORY_ALARMS; |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3432 | priorityCategories &= ~Policy.PRIORITY_CATEGORY_MEDIA; |
| 3433 | priorityCategories &= ~Policy.PRIORITY_CATEGORY_SYSTEM; |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3434 | // use user-designated values |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3435 | priorityCategories |= currPolicy.priorityCategories |
| 3436 | & Policy.PRIORITY_CATEGORY_ALARMS; |
| 3437 | priorityCategories |= currPolicy.priorityCategories |
| 3438 | & Policy.PRIORITY_CATEGORY_MEDIA; |
| 3439 | priorityCategories |= currPolicy.priorityCategories |
| 3440 | & Policy.PRIORITY_CATEGORY_SYSTEM; |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3441 | |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3442 | policy = new Policy(priorityCategories, |
| 3443 | policy.priorityCallSenders, policy.priorityMessageSenders, |
| 3444 | policy.suppressedVisualEffects); |
| 3445 | } |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3446 | int newVisualEffects = calculateSuppressedVisualEffects( |
| 3447 | policy, currPolicy, applicationInfo.targetSdkVersion); |
| 3448 | policy = new Policy(policy.priorityCategories, |
| 3449 | policy.priorityCallSenders, policy.priorityMessageSenders, |
| 3450 | newVisualEffects); |
Beverly | 5e07322 | 2018-03-08 10:36:25 -0500 | [diff] [blame] | 3451 | ZenLog.traceSetNotificationPolicy(pkg, applicationInfo.targetSdkVersion, policy); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3452 | mZenModeHelper.setNotificationPolicy(policy); |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3453 | } catch (RemoteException e) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3454 | } finally { |
| 3455 | Binder.restoreCallingIdentity(identity); |
| 3456 | } |
| 3457 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3458 | |
| 3459 | @Override |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3460 | public List<String> getEnabledNotificationListenerPackages() { |
| 3461 | checkCallerIsSystem(); |
| 3462 | return mListeners.getAllowedPackages(getCallingUserHandle().getIdentifier()); |
| 3463 | } |
| 3464 | |
| 3465 | @Override |
| 3466 | public List<ComponentName> getEnabledNotificationListeners(int userId) { |
| 3467 | checkCallerIsSystem(); |
| 3468 | return mListeners.getAllowedComponents(userId); |
| 3469 | } |
| 3470 | |
| 3471 | @Override |
| 3472 | public boolean isNotificationListenerAccessGranted(ComponentName listener) { |
| 3473 | Preconditions.checkNotNull(listener); |
| 3474 | checkCallerIsSystemOrSameApp(listener.getPackageName()); |
| 3475 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 3476 | getCallingUserHandle().getIdentifier()); |
| 3477 | } |
| 3478 | |
| 3479 | @Override |
| 3480 | public boolean isNotificationListenerAccessGrantedForUser(ComponentName listener, |
| 3481 | int userId) { |
| 3482 | Preconditions.checkNotNull(listener); |
| 3483 | checkCallerIsSystem(); |
| 3484 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 3485 | userId); |
| 3486 | } |
| 3487 | |
| 3488 | @Override |
| 3489 | public boolean isNotificationAssistantAccessGranted(ComponentName assistant) { |
| 3490 | Preconditions.checkNotNull(assistant); |
| 3491 | checkCallerIsSystemOrSameApp(assistant.getPackageName()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3492 | return mAssistants.isPackageOrComponentAllowed(assistant.flattenToString(), |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3493 | getCallingUserHandle().getIdentifier()); |
| 3494 | } |
| 3495 | |
| 3496 | @Override |
| 3497 | public void setNotificationListenerAccessGranted(ComponentName listener, |
| 3498 | boolean granted) throws RemoteException { |
| 3499 | setNotificationListenerAccessGrantedForUser( |
| 3500 | listener, getCallingUserHandle().getIdentifier(), granted); |
| 3501 | } |
| 3502 | |
| 3503 | @Override |
| 3504 | public void setNotificationAssistantAccessGranted(ComponentName assistant, |
| 3505 | boolean granted) throws RemoteException { |
| 3506 | setNotificationAssistantAccessGrantedForUser( |
| 3507 | assistant, getCallingUserHandle().getIdentifier(), granted); |
| 3508 | } |
| 3509 | |
| 3510 | @Override |
| 3511 | public void setNotificationListenerAccessGrantedForUser(ComponentName listener, int userId, |
| 3512 | boolean granted) throws RemoteException { |
| 3513 | Preconditions.checkNotNull(listener); |
Julia Reynolds | 0d21764 | 2017-08-11 11:26:04 -0400 | [diff] [blame] | 3514 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3515 | final long identity = Binder.clearCallingIdentity(); |
| 3516 | try { |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3517 | if (mAllowedManagedServicePackages.test(listener.getPackageName())) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3518 | mConditionProviders.setPackageOrComponentEnabled(listener.flattenToString(), |
| 3519 | userId, false, granted); |
| 3520 | mListeners.setPackageOrComponentEnabled(listener.flattenToString(), |
| 3521 | userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3522 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3523 | getContext().sendBroadcastAsUser(new Intent( |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3524 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3525 | .setPackage(listener.getPackageName()) |
| 3526 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3527 | UserHandle.of(userId), null); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3528 | |
| 3529 | savePolicyFile(); |
| 3530 | } |
| 3531 | } finally { |
| 3532 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3533 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | @Override |
| 3537 | public void setNotificationAssistantAccessGrantedForUser(ComponentName assistant, |
| 3538 | int userId, boolean granted) throws RemoteException { |
| 3539 | Preconditions.checkNotNull(assistant); |
Julia Reynolds | 0d21764 | 2017-08-11 11:26:04 -0400 | [diff] [blame] | 3540 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3541 | final long identity = Binder.clearCallingIdentity(); |
| 3542 | try { |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3543 | if (mAllowedManagedServicePackages.test(assistant.getPackageName())) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3544 | mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(), |
| 3545 | userId, false, granted); |
| 3546 | mAssistants.setPackageOrComponentEnabled(assistant.flattenToString(), |
| 3547 | userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3548 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3549 | getContext().sendBroadcastAsUser(new Intent( |
| 3550 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 3551 | .setPackage(assistant.getPackageName()) |
| 3552 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3553 | UserHandle.of(userId), null); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3554 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3555 | savePolicyFile(); |
| 3556 | } |
| 3557 | } finally { |
| 3558 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3559 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3560 | } |
| 3561 | |
| 3562 | @Override |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3563 | public void applyEnqueuedAdjustmentFromAssistant(INotificationListener token, |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3564 | Adjustment adjustment) { |
| 3565 | boolean foundEnqueued = false; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3566 | final long identity = Binder.clearCallingIdentity(); |
| 3567 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3568 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3569 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3570 | int N = mEnqueuedNotifications.size(); |
| 3571 | for (int i = 0; i < N; i++) { |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3572 | final NotificationRecord r = mEnqueuedNotifications.get(i); |
| 3573 | if (Objects.equals(adjustment.getKey(), r.getKey()) |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3574 | && Objects.equals(adjustment.getUser(), r.getUserId()) |
| 3575 | && mAssistants.isSameUser(token, r.getUserId())) { |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3576 | applyAdjustment(r, adjustment); |
| 3577 | r.applyAdjustments(); |
| 3578 | foundEnqueued = true; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3579 | break; |
| 3580 | } |
| 3581 | } |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3582 | if (!foundEnqueued) { |
| 3583 | // adjustment arrived too late to apply to enqueued; apply to posted |
| 3584 | applyAdjustmentFromAssistant(token, adjustment); |
| 3585 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3586 | } |
| 3587 | } finally { |
| 3588 | Binder.restoreCallingIdentity(identity); |
| 3589 | } |
| 3590 | } |
| 3591 | |
| 3592 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 3593 | public void applyAdjustmentFromAssistant(INotificationListener token, |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3594 | Adjustment adjustment) { |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3595 | List<Adjustment> adjustments = new ArrayList<>(); |
| 3596 | adjustments.add(adjustment); |
| 3597 | applyAdjustmentsFromAssistant(token, adjustments); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3598 | } |
| 3599 | |
| 3600 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 3601 | public void applyAdjustmentsFromAssistant(INotificationListener token, |
Julia Reynolds | 666ccf0 | 2018-06-18 10:19:20 -0400 | [diff] [blame] | 3602 | List<Adjustment> adjustments) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3603 | |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 3604 | boolean needsSort = false; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3605 | final long identity = Binder.clearCallingIdentity(); |
| 3606 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3607 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3608 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3609 | for (Adjustment adjustment : adjustments) { |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3610 | NotificationRecord r = mNotificationsByKey.get(adjustment.getKey()); |
| 3611 | if (r != null && mAssistants.isSameUser(token, r.getUserId())) { |
| 3612 | applyAdjustment(r, adjustment); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 3613 | r.applyAdjustments(); |
| 3614 | if (r.getImportance() == IMPORTANCE_NONE) { |
| 3615 | cancelNotificationsFromListener(token, new String[]{r.getKey()}); |
| 3616 | } else { |
| 3617 | needsSort = true; |
| 3618 | } |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3619 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3620 | } |
| 3621 | } |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 3622 | if (needsSort) { |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 3623 | mRankingHandler.requestSort(); |
| 3624 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3625 | } finally { |
| 3626 | Binder.restoreCallingIdentity(identity); |
| 3627 | } |
| 3628 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3629 | |
| 3630 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 3631 | public void updateNotificationChannelGroupFromPrivilegedListener( |
| 3632 | INotificationListener token, String pkg, UserHandle user, |
| 3633 | NotificationChannelGroup group) throws RemoteException { |
| 3634 | Preconditions.checkNotNull(user); |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 3635 | verifyPrivilegedListener(token, user, false); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 3636 | createNotificationChannelGroup( |
| 3637 | pkg, getUidForPackageAndUser(pkg, user), group, false, true); |
| 3638 | savePolicyFile(); |
| 3639 | } |
| 3640 | |
| 3641 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3642 | public void updateNotificationChannelFromPrivilegedListener(INotificationListener token, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3643 | String pkg, UserHandle user, NotificationChannel channel) throws RemoteException { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3644 | Preconditions.checkNotNull(channel); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3645 | Preconditions.checkNotNull(pkg); |
| 3646 | Preconditions.checkNotNull(user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3647 | |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 3648 | verifyPrivilegedListener(token, user, false); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3649 | updateNotificationChannelInt(pkg, getUidForPackageAndUser(pkg, user), channel, true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3650 | } |
| 3651 | |
| 3652 | @Override |
| 3653 | public ParceledListSlice<NotificationChannel> getNotificationChannelsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3654 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 3655 | Preconditions.checkNotNull(pkg); |
| 3656 | Preconditions.checkNotNull(user); |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 3657 | verifyPrivilegedListener(token, user, true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3658 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 3659 | return mPreferencesHelper.getNotificationChannels(pkg, getUidForPackageAndUser(pkg, user), |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3660 | false /* includeDeleted */); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3661 | } |
| 3662 | |
| 3663 | @Override |
| 3664 | public ParceledListSlice<NotificationChannelGroup> |
| 3665 | getNotificationChannelGroupsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3666 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 3667 | Preconditions.checkNotNull(pkg); |
| 3668 | Preconditions.checkNotNull(user); |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 3669 | verifyPrivilegedListener(token, user, true); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3670 | |
| 3671 | List<NotificationChannelGroup> groups = new ArrayList<>(); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 3672 | groups.addAll(mPreferencesHelper.getNotificationChannelGroups( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3673 | pkg, getUidForPackageAndUser(pkg, user))); |
| 3674 | return new ParceledListSlice<>(groups); |
| 3675 | } |
| 3676 | |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 3677 | private void verifyPrivilegedListener(INotificationListener token, UserHandle user, |
| 3678 | boolean assistantAllowed) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3679 | ManagedServiceInfo info; |
| 3680 | synchronized (mNotificationLock) { |
| 3681 | info = mListeners.checkServiceTokenLocked(token); |
| 3682 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 3683 | if (!hasCompanionDevice(info)) { |
Julia Reynolds | 48a6ed9 | 2018-10-22 12:52:03 -0400 | [diff] [blame] | 3684 | synchronized (mNotificationLock) { |
| 3685 | if (!assistantAllowed || !mAssistants.isServiceTokenValidLocked(info.service)) { |
| 3686 | throw new SecurityException(info + " does not have access"); |
| 3687 | } |
| 3688 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 3689 | } |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3690 | if (!info.enabledAndUserMatches(user.getIdentifier())) { |
| 3691 | throw new SecurityException(info + " does not have access"); |
| 3692 | } |
| 3693 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3694 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3695 | private int getUidForPackageAndUser(String pkg, UserHandle user) throws RemoteException { |
| 3696 | int uid = 0; |
| 3697 | long identity = Binder.clearCallingIdentity(); |
| 3698 | try { |
| 3699 | uid = mPackageManager.getPackageUid(pkg, 0, user.getIdentifier()); |
| 3700 | } finally { |
| 3701 | Binder.restoreCallingIdentity(identity); |
| 3702 | } |
| 3703 | return uid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3704 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3705 | |
| 3706 | @Override |
| 3707 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 3708 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) |
| 3709 | throws RemoteException { |
| 3710 | new ShellCmd().exec(this, in, out, err, args, callback, resultReceiver); |
| 3711 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3712 | }; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 3713 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3714 | private void applyAdjustment(NotificationRecord r, Adjustment adjustment) { |
| 3715 | if (r == null) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3716 | return; |
| 3717 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3718 | if (adjustment.getSignals() != null) { |
| 3719 | Bundle.setDefusable(adjustment.getSignals(), true); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3720 | r.addAdjustment(adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3721 | } |
| 3722 | } |
| 3723 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3724 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3725 | void addAutogroupKeyLocked(String key) { |
| 3726 | NotificationRecord r = mNotificationsByKey.get(key); |
| 3727 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3728 | return; |
| 3729 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3730 | if (r.sbn.getOverrideGroupKey() == null) { |
| 3731 | addAutoGroupAdjustment(r, GroupHelper.AUTOGROUP_KEY); |
| 3732 | EventLogTags.writeNotificationAutogrouped(key); |
| 3733 | mRankingHandler.requestSort(); |
| 3734 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3735 | } |
| 3736 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3737 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3738 | void removeAutogroupKeyLocked(String key) { |
| 3739 | NotificationRecord r = mNotificationsByKey.get(key); |
| 3740 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3741 | return; |
| 3742 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3743 | if (r.sbn.getOverrideGroupKey() != null) { |
| 3744 | addAutoGroupAdjustment(r, null); |
| 3745 | EventLogTags.writeNotificationUnautogrouped(key); |
| 3746 | mRankingHandler.requestSort(); |
| 3747 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | private void addAutoGroupAdjustment(NotificationRecord r, String overrideGroupKey) { |
| 3751 | Bundle signals = new Bundle(); |
| 3752 | signals.putString(Adjustment.KEY_GROUP_KEY, overrideGroupKey); |
| 3753 | Adjustment adjustment = |
| 3754 | new Adjustment(r.sbn.getPackageName(), r.getKey(), signals, "", r.sbn.getUserId()); |
| 3755 | r.addAdjustment(adjustment); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3756 | } |
| 3757 | |
| 3758 | // Clears the 'fake' auto-group summary. |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3759 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3760 | private void clearAutogroupSummaryLocked(int userId, String pkg) { |
| 3761 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 3762 | if (summaries != null && summaries.containsKey(pkg)) { |
| 3763 | // Clear summary. |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3764 | final NotificationRecord removed = findNotificationByKeyLocked(summaries.remove(pkg)); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3765 | if (removed != null) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 3766 | boolean wasPosted = removeFromNotificationListsLocked(removed); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 3767 | cancelNotificationLocked(removed, false, REASON_UNAUTOBUNDLED, wasPosted, null); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3768 | } |
| 3769 | } |
| 3770 | } |
| 3771 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 3772 | @GuardedBy("mNotificationLock") |
| 3773 | private boolean hasAutoGroupSummaryLocked(StatusBarNotification sbn) { |
| 3774 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(sbn.getUserId()); |
| 3775 | return summaries != null && summaries.containsKey(sbn.getPackageName()); |
| 3776 | } |
| 3777 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3778 | // 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] | 3779 | private void createAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 3780 | NotificationRecord summaryRecord = null; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3781 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3782 | NotificationRecord notificationRecord = mNotificationsByKey.get(triggeringKey); |
| 3783 | if (notificationRecord == null) { |
| 3784 | // The notification could have been cancelled again already. A successive |
| 3785 | // adjustment will post a summary if needed. |
| 3786 | return; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3787 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3788 | final StatusBarNotification adjustedSbn = notificationRecord.sbn; |
| 3789 | userId = adjustedSbn.getUser().getIdentifier(); |
| 3790 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 3791 | if (summaries == null) { |
| 3792 | summaries = new ArrayMap<>(); |
| 3793 | } |
| 3794 | mAutobundledSummaries.put(userId, summaries); |
| 3795 | if (!summaries.containsKey(pkg)) { |
| 3796 | // Add summary |
| 3797 | final ApplicationInfo appInfo = |
| 3798 | adjustedSbn.getNotification().extras.getParcelable( |
| 3799 | Notification.EXTRA_BUILDER_APPLICATION_INFO); |
| 3800 | final Bundle extras = new Bundle(); |
| 3801 | extras.putParcelable(Notification.EXTRA_BUILDER_APPLICATION_INFO, appInfo); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3802 | final String channelId = notificationRecord.getChannel().getId(); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3803 | final Notification summaryNotification = |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3804 | new Notification.Builder(getContext(), channelId) |
| 3805 | .setSmallIcon(adjustedSbn.getNotification().getSmallIcon()) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3806 | .setGroupSummary(true) |
Julia Reynolds | 9d5786e | 2017-04-28 10:26:32 -0400 | [diff] [blame] | 3807 | .setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3808 | .setGroup(GroupHelper.AUTOGROUP_KEY) |
| 3809 | .setFlag(Notification.FLAG_AUTOGROUP_SUMMARY, true) |
| 3810 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 3811 | .setColor(adjustedSbn.getNotification().color) |
| 3812 | .setLocalOnly(true) |
| 3813 | .build(); |
| 3814 | summaryNotification.extras.putAll(extras); |
| 3815 | Intent appIntent = getContext().getPackageManager().getLaunchIntentForPackage(pkg); |
| 3816 | if (appIntent != null) { |
| 3817 | summaryNotification.contentIntent = PendingIntent.getActivityAsUser( |
| 3818 | getContext(), 0, appIntent, 0, null, UserHandle.of(userId)); |
| 3819 | } |
| 3820 | final StatusBarNotification summarySbn = |
| 3821 | new StatusBarNotification(adjustedSbn.getPackageName(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3822 | adjustedSbn.getOpPkg(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3823 | Integer.MAX_VALUE, |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3824 | GroupHelper.AUTOGROUP_KEY, adjustedSbn.getUid(), |
| 3825 | adjustedSbn.getInitialPid(), summaryNotification, |
| 3826 | adjustedSbn.getUser(), GroupHelper.AUTOGROUP_KEY, |
| 3827 | System.currentTimeMillis()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 3828 | summaryRecord = new NotificationRecord(getContext(), summarySbn, |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 3829 | notificationRecord.getChannel()); |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 3830 | summaryRecord.setIsAppImportanceLocked( |
| 3831 | notificationRecord.getIsAppImportanceLocked()); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3832 | summaries.put(pkg, summarySbn.getKey()); |
| 3833 | } |
| 3834 | } |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3835 | if (summaryRecord != null && checkDisqualifyingFeatures(userId, MY_UID, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3836 | summaryRecord.sbn.getId(), summaryRecord.sbn.getTag(), summaryRecord, true)) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3837 | mHandler.post(new EnqueueNotificationRunnable(userId, summaryRecord)); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3838 | } |
| 3839 | } |
| 3840 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3841 | private String disableNotificationEffects(NotificationRecord record) { |
| 3842 | if (mDisableNotificationEffects) { |
| 3843 | return "booleanState"; |
| 3844 | } |
| 3845 | if ((mListenerHints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 3846 | return "listenerHints"; |
| 3847 | } |
| 3848 | if (mCallState != TelephonyManager.CALL_STATE_IDLE && !mZenModeHelper.isCall(record)) { |
| 3849 | return "callState"; |
| 3850 | } |
| 3851 | return null; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3852 | }; |
| 3853 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3854 | private void dumpJson(PrintWriter pw, @NonNull DumpFilter filter) { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3855 | JSONObject dump = new JSONObject(); |
| 3856 | try { |
| 3857 | dump.put("service", "Notification Manager"); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 3858 | dump.put("bans", mPreferencesHelper.dumpBansJson(filter)); |
| 3859 | dump.put("ranking", mPreferencesHelper.dumpJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3860 | dump.put("stats", mUsageStats.dumpJson(filter)); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 3861 | dump.put("channels", mPreferencesHelper.dumpChannelsJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3862 | } catch (JSONException e) { |
| 3863 | e.printStackTrace(); |
| 3864 | } |
| 3865 | pw.println(dump); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3866 | } |
| 3867 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3868 | private void dumpProto(FileDescriptor fd, @NonNull DumpFilter filter) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3869 | final ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 3870 | synchronized (mNotificationLock) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3871 | int N = mNotificationList.size(); |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 3872 | for (int i = 0; i < N; i++) { |
| 3873 | final NotificationRecord nr = mNotificationList.get(i); |
| 3874 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3875 | nr.dump(proto, NotificationServiceDumpProto.RECORDS, filter.redact, |
| 3876 | NotificationRecordProto.POSTED); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3877 | } |
| 3878 | N = mEnqueuedNotifications.size(); |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 3879 | for (int i = 0; i < N; i++) { |
| 3880 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 3881 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3882 | nr.dump(proto, NotificationServiceDumpProto.RECORDS, filter.redact, |
| 3883 | NotificationRecordProto.ENQUEUED); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3884 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3885 | List<NotificationRecord> snoozed = mSnoozeHelper.getSnoozed(); |
| 3886 | N = snoozed.size(); |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 3887 | for (int i = 0; i < N; i++) { |
| 3888 | final NotificationRecord nr = snoozed.get(i); |
| 3889 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3890 | nr.dump(proto, NotificationServiceDumpProto.RECORDS, filter.redact, |
| 3891 | NotificationRecordProto.SNOOZED); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3892 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3893 | |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3894 | long zenLog = proto.start(NotificationServiceDumpProto.ZEN); |
| 3895 | mZenModeHelper.dump(proto); |
| 3896 | for (ComponentName suppressor : mEffectsSuppressors) { |
Kweku Adams | 9954633 | 2018-01-24 17:03:50 -0800 | [diff] [blame] | 3897 | suppressor.writeToProto(proto, ZenModeProto.SUPPRESSORS); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3898 | } |
| 3899 | proto.end(zenLog); |
| 3900 | |
| 3901 | long listenersToken = proto.start(NotificationServiceDumpProto.NOTIFICATION_LISTENERS); |
| 3902 | mListeners.dump(proto, filter); |
| 3903 | proto.end(listenersToken); |
| 3904 | |
| 3905 | proto.write(NotificationServiceDumpProto.LISTENER_HINTS, mListenerHints); |
| 3906 | |
| 3907 | for (int i = 0; i < mListenersDisablingEffects.size(); ++i) { |
| 3908 | long effectsToken = proto.start( |
| 3909 | NotificationServiceDumpProto.LISTENERS_DISABLING_EFFECTS); |
| 3910 | |
| 3911 | proto.write( |
| 3912 | ListenersDisablingEffectsProto.HINT, mListenersDisablingEffects.keyAt(i)); |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 3913 | final ArraySet<ComponentName> listeners = |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3914 | mListenersDisablingEffects.valueAt(i); |
| 3915 | for (int j = 0; j < listeners.size(); j++) { |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 3916 | final ComponentName componentName = listeners.valueAt(i); |
| 3917 | componentName.writeToProto(proto, |
| 3918 | ListenersDisablingEffectsProto.LISTENER_COMPONENTS); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3919 | } |
| 3920 | |
| 3921 | proto.end(effectsToken); |
| 3922 | } |
| 3923 | |
| 3924 | long assistantsToken = proto.start( |
| 3925 | NotificationServiceDumpProto.NOTIFICATION_ASSISTANTS); |
| 3926 | mAssistants.dump(proto, filter); |
| 3927 | proto.end(assistantsToken); |
| 3928 | |
| 3929 | long conditionsToken = proto.start(NotificationServiceDumpProto.CONDITION_PROVIDERS); |
| 3930 | mConditionProviders.dump(proto, filter); |
| 3931 | proto.end(conditionsToken); |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 3932 | |
| 3933 | long rankingToken = proto.start(NotificationServiceDumpProto.RANKING_CONFIG); |
| 3934 | mRankingHelper.dump(proto, filter); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 3935 | mPreferencesHelper.dump(proto, filter); |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 3936 | proto.end(rankingToken); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3937 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3938 | |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3939 | proto.flush(); |
| 3940 | } |
| 3941 | |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 3942 | private void dumpNotificationRecords(PrintWriter pw, @NonNull DumpFilter filter) { |
| 3943 | synchronized (mNotificationLock) { |
| 3944 | int N; |
| 3945 | N = mNotificationList.size(); |
| 3946 | if (N > 0) { |
| 3947 | pw.println(" Notification List:"); |
| 3948 | for (int i = 0; i < N; i++) { |
| 3949 | final NotificationRecord nr = mNotificationList.get(i); |
| 3950 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3951 | nr.dump(pw, " ", getContext(), filter.redact); |
| 3952 | } |
| 3953 | pw.println(" "); |
| 3954 | } |
| 3955 | } |
| 3956 | } |
| 3957 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3958 | void dumpImpl(PrintWriter pw, @NonNull DumpFilter filter) { |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3959 | pw.print("Current Notification Manager state"); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3960 | if (filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3961 | pw.print(" (filtered to "); pw.print(filter); pw.print(")"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3962 | } |
| 3963 | pw.println(':'); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3964 | int N; |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3965 | final boolean zenOnly = filter.filtered && filter.zen; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3966 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3967 | if (!zenOnly) { |
| 3968 | synchronized (mToastQueue) { |
| 3969 | N = mToastQueue.size(); |
| 3970 | if (N > 0) { |
| 3971 | pw.println(" Toast Queue:"); |
| 3972 | for (int i=0; i<N; i++) { |
| 3973 | mToastQueue.get(i).dump(pw, " ", filter); |
| 3974 | } |
| 3975 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3976 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3977 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3978 | } |
| 3979 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3980 | synchronized (mNotificationLock) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3981 | if (!zenOnly) { |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 3982 | // Priority filters are only set when called via bugreport. If set |
| 3983 | // skip sections that are part of the critical section. |
| 3984 | if (!filter.normalPriority) { |
| 3985 | dumpNotificationRecords(pw, filter); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3986 | } |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3987 | if (!filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3988 | N = mLights.size(); |
| 3989 | if (N > 0) { |
| 3990 | pw.println(" Lights List:"); |
| 3991 | for (int i=0; i<N; i++) { |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3992 | if (i == N - 1) { |
| 3993 | pw.print(" > "); |
| 3994 | } else { |
| 3995 | pw.print(" "); |
| 3996 | } |
| 3997 | pw.println(mLights.get(i)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3998 | } |
| 3999 | pw.println(" "); |
| 4000 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 4001 | pw.println(" mUseAttentionLight=" + mUseAttentionLight); |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 4002 | pw.println(" mHasLight=" + mHasLight); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 4003 | pw.println(" mNotificationPulseEnabled=" + mNotificationPulseEnabled); |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 4004 | pw.println(" mSoundNotificationKey=" + mSoundNotificationKey); |
| 4005 | pw.println(" mVibrateNotificationKey=" + mVibrateNotificationKey); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4006 | pw.println(" mDisableNotificationEffects=" + mDisableNotificationEffects); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 4007 | pw.println(" mCallState=" + callStateToString(mCallState)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4008 | pw.println(" mSystemReady=" + mSystemReady); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 4009 | pw.println(" mMaxPackageEnqueueRate=" + mMaxPackageEnqueueRate); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4010 | } |
| 4011 | pw.println(" mArchive=" + mArchive.toString()); |
| 4012 | Iterator<StatusBarNotification> iter = mArchive.descendingIterator(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4013 | int j=0; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4014 | while (iter.hasNext()) { |
| 4015 | final StatusBarNotification sbn = iter.next(); |
| 4016 | if (filter != null && !filter.matches(sbn)) continue; |
| 4017 | pw.println(" " + sbn); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4018 | if (++j >= 5) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4019 | if (iter.hasNext()) pw.println(" ..."); |
| 4020 | break; |
| 4021 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4022 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4023 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4024 | if (!zenOnly) { |
| 4025 | N = mEnqueuedNotifications.size(); |
| 4026 | if (N > 0) { |
| 4027 | pw.println(" Enqueued Notification List:"); |
| 4028 | for (int i = 0; i < N; i++) { |
| 4029 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 4030 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 4031 | nr.dump(pw, " ", getContext(), filter.redact); |
| 4032 | } |
| 4033 | pw.println(" "); |
| 4034 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4035 | |
| 4036 | mSnoozeHelper.dump(pw, filter); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4037 | } |
| 4038 | } |
| 4039 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4040 | if (!zenOnly) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4041 | pw.println("\n Ranking Config:"); |
| 4042 | mRankingHelper.dump(pw, " ", filter); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4043 | |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 4044 | pw.println("\n Notification Preferences:"); |
| 4045 | mPreferencesHelper.dump(pw, " ", filter); |
| 4046 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4047 | pw.println("\n Notification listeners:"); |
| 4048 | mListeners.dump(pw, filter); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4049 | pw.print(" mListenerHints: "); pw.println(mListenerHints); |
| 4050 | pw.print(" mListenersDisablingEffects: ("); |
| 4051 | N = mListenersDisablingEffects.size(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4052 | for (int i = 0; i < N; i++) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 4053 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 4054 | if (i > 0) pw.print(';'); |
| 4055 | pw.print("hint[" + hint + "]:"); |
| 4056 | |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 4057 | final ArraySet<ComponentName> listeners = mListenersDisablingEffects.valueAt(i); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 4058 | final int listenerSize = listeners.size(); |
| 4059 | |
| 4060 | for (int j = 0; j < listenerSize; j++) { |
| 4061 | if (i > 0) pw.print(','); |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 4062 | final ComponentName listener = listeners.valueAt(i); |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 4063 | if (listener != null) { |
Julia Reynolds | 4703bac | 2018-09-12 10:39:30 -0400 | [diff] [blame] | 4064 | pw.print(listener); |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 4065 | } |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 4066 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4067 | } |
| 4068 | pw.println(')'); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 4069 | pw.println("\n Notification assistant services:"); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4070 | mAssistants.dump(pw, filter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 4071 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 4072 | |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4073 | if (!filter.filtered || zenOnly) { |
| 4074 | pw.println("\n Zen Mode:"); |
| 4075 | pw.print(" mInterruptionFilter="); pw.println(mInterruptionFilter); |
| 4076 | mZenModeHelper.dump(pw, " "); |
| 4077 | |
| 4078 | pw.println("\n Zen Log:"); |
| 4079 | ZenLog.dump(pw, " "); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 4080 | } |
| 4081 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 4082 | pw.println("\n Condition providers:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 4083 | mConditionProviders.dump(pw, filter); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4084 | |
| 4085 | pw.println("\n Group summaries:"); |
| 4086 | for (Entry<String, NotificationRecord> entry : mSummaryByGroupKey.entrySet()) { |
| 4087 | NotificationRecord r = entry.getValue(); |
| 4088 | pw.println(" " + entry.getKey() + " -> " + r.getKey()); |
| 4089 | if (mNotificationsByKey.get(r.getKey()) != r) { |
| 4090 | pw.println("!!!!!!LEAK: Record not found in mNotificationsByKey."); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 4091 | r.dump(pw, " ", getContext(), filter.redact); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4092 | } |
| 4093 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4094 | |
| 4095 | if (!zenOnly) { |
| 4096 | pw.println("\n Usage Stats:"); |
| 4097 | mUsageStats.dump(pw, " ", filter); |
| 4098 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4099 | } |
| 4100 | } |
| 4101 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4102 | /** |
| 4103 | * The private API only accessible to the system process. |
| 4104 | */ |
| 4105 | private final NotificationManagerInternal mInternalService = new NotificationManagerInternal() { |
| 4106 | @Override |
Julia Reynolds | f3de8aa | 2017-09-29 15:52:37 -0400 | [diff] [blame] | 4107 | public NotificationChannel getNotificationChannel(String pkg, int uid, String |
| 4108 | channelId) { |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 4109 | return mPreferencesHelper.getNotificationChannel(pkg, uid, channelId, false); |
Julia Reynolds | f3de8aa | 2017-09-29 15:52:37 -0400 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 4113 | public void enqueueNotification(String pkg, String opPkg, int callingUid, int callingPid, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 4114 | String tag, int id, Notification notification, int userId) { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 4115 | enqueueNotificationInternal(pkg, opPkg, callingUid, callingPid, tag, id, notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 4116 | userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4117 | } |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 4118 | |
| 4119 | @Override |
| 4120 | public void removeForegroundServiceFlagFromNotification(String pkg, int notificationId, |
| 4121 | int userId) { |
| 4122 | checkCallerIsSystem(); |
Julia Reynolds | 564273f | 2018-09-13 15:53:11 -0400 | [diff] [blame] | 4123 | mHandler.post(() -> { |
| 4124 | synchronized (mNotificationLock) { |
| 4125 | // strip flag from all enqueued notifications. listeners will be informed |
| 4126 | // in post runnable. |
| 4127 | List<NotificationRecord> enqueued = findNotificationsByListLocked( |
| 4128 | mEnqueuedNotifications, pkg, null, notificationId, userId); |
| 4129 | for (int i = 0; i < enqueued.size(); i++) { |
| 4130 | removeForegroundServiceFlagLocked(enqueued.get(i)); |
| 4131 | } |
| 4132 | |
| 4133 | // if posted notification exists, strip its flag and tell listeners |
| 4134 | NotificationRecord r = findNotificationByListLocked( |
| 4135 | mNotificationList, pkg, null, notificationId, userId); |
| 4136 | if (r != null) { |
| 4137 | removeForegroundServiceFlagLocked(r); |
| 4138 | mRankingHelper.sort(mNotificationList); |
| 4139 | mListeners.notifyPostedLocked(r, r); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4140 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 4141 | } |
| 4142 | }); |
| 4143 | } |
| 4144 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4145 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 564273f | 2018-09-13 15:53:11 -0400 | [diff] [blame] | 4146 | private void removeForegroundServiceFlagLocked(NotificationRecord r) { |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 4147 | if (r == null) { |
| 4148 | return; |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 4149 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 4150 | StatusBarNotification sbn = r.sbn; |
| 4151 | // NoMan adds flags FLAG_NO_CLEAR and FLAG_ONGOING_EVENT when it sees |
| 4152 | // FLAG_FOREGROUND_SERVICE. Hence it's not enough to remove |
| 4153 | // FLAG_FOREGROUND_SERVICE, we have to revert to the flags we received |
| 4154 | // initially *and* force remove FLAG_FOREGROUND_SERVICE. |
| 4155 | sbn.getNotification().flags = |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4156 | (r.mOriginalFlags & ~FLAG_FOREGROUND_SERVICE); |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 4157 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4158 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4159 | |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 4160 | void enqueueNotificationInternal(final String pkg, final String opPkg, final int callingUid, |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 4161 | final int callingPid, final String tag, final int id, final Notification notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 4162 | int incomingUserId) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 4163 | if (DBG) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4164 | Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id |
| 4165 | + " notification=" + notification); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 4166 | } |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4167 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4168 | if (pkg == null || notification == null) { |
| 4169 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 4170 | + " id=" + id + " notification=" + notification); |
| 4171 | } |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4172 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4173 | final int userId = ActivityManager.handleIncomingUser(callingPid, |
| 4174 | callingUid, incomingUserId, true, false, "enqueueNotification", pkg); |
| 4175 | final UserHandle user = UserHandle.of(userId); |
| 4176 | |
| 4177 | // Can throw a SecurityException if the calling uid doesn't have permission to post |
| 4178 | // as "pkg" |
| 4179 | final int notificationUid = resolveNotificationUid(opPkg, pkg, callingUid, userId); |
| 4180 | |
| 4181 | checkRestrictedCategories(notification); |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4182 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4183 | // Fix the notification as best we can. |
| 4184 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4185 | final ApplicationInfo ai = mPackageManagerClient.getApplicationInfoAsUser( |
Jeff Sharkey | 012bc7b | 2016-04-11 16:30:27 -0600 | [diff] [blame] | 4186 | pkg, PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 4187 | (userId == UserHandle.USER_ALL) ? USER_SYSTEM : userId); |
Julia Reynolds | e071abd | 2017-03-22 10:52:11 -0400 | [diff] [blame] | 4188 | Notification.addFieldsFromContext(ai, notification); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 4189 | |
| 4190 | int canColorize = mPackageManagerClient.checkPermission( |
| 4191 | android.Manifest.permission.USE_COLORIZED_NOTIFICATIONS, pkg); |
| 4192 | if (canColorize == PERMISSION_GRANTED) { |
| 4193 | notification.flags |= Notification.FLAG_CAN_COLORIZE; |
| 4194 | } else { |
| 4195 | notification.flags &= ~Notification.FLAG_CAN_COLORIZE; |
| 4196 | } |
| 4197 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4198 | } catch (NameNotFoundException e) { |
| 4199 | Slog.e(TAG, "Cannot create a context for sending app", e); |
| 4200 | return; |
| 4201 | } |
| 4202 | |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 4203 | mUsageStats.registerEnqueuedByApp(pkg); |
| 4204 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4205 | // setup local book-keeping |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 4206 | String channelId = notification.getChannelId(); |
| 4207 | if (mIsTelevision && (new Notification.TvExtender(notification)).getChannelId() != null) { |
| 4208 | channelId = (new Notification.TvExtender(notification)).getChannelId(); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 4209 | } |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 4210 | final NotificationChannel channel = mPreferencesHelper.getNotificationChannel(pkg, |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4211 | notificationUid, channelId, false /* includeDeleted */); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 4212 | if (channel == null) { |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 4213 | final String noChannelStr = "No Channel found for " |
| 4214 | + "pkg=" + pkg |
| 4215 | + ", channelId=" + channelId |
Julia Reynolds | f26eb91 | 2017-05-22 15:47:06 -0400 | [diff] [blame] | 4216 | + ", id=" + id |
| 4217 | + ", tag=" + tag |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 4218 | + ", opPkg=" + opPkg |
| 4219 | + ", callingUid=" + callingUid |
| 4220 | + ", userId=" + userId |
| 4221 | + ", incomingUserId=" + incomingUserId |
| 4222 | + ", notificationUid=" + notificationUid |
| 4223 | + ", notification=" + notification; |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 4224 | Log.e(TAG, noChannelStr); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 4225 | boolean appNotificationsOff = mPreferencesHelper.getImportance(pkg, notificationUid) |
Beverly | 5d4564b | 2018-04-10 20:09:23 -0400 | [diff] [blame] | 4226 | == NotificationManager.IMPORTANCE_NONE; |
| 4227 | |
| 4228 | if (!appNotificationsOff) { |
| 4229 | doChannelWarningToast("Developer warning for package \"" + pkg + "\"\n" + |
| 4230 | "Failed to post notification on channel \"" + channelId + "\"\n" + |
| 4231 | "See log for more details"); |
| 4232 | } |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 4233 | return; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 4234 | } |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 4235 | |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 4236 | final StatusBarNotification n = new StatusBarNotification( |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4237 | pkg, opPkg, id, tag, notificationUid, callingPid, notification, |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 4238 | user, null, System.currentTimeMillis()); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 4239 | final NotificationRecord r = new NotificationRecord(getContext(), n, channel); |
Aaron Heuckroth | e5bec15 | 2018-07-09 16:26:09 -0400 | [diff] [blame] | 4240 | r.setIsAppImportanceLocked(mPreferencesHelper.getIsAppImportanceLocked(pkg, callingUid)); |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 4241 | |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 4242 | if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 4243 | final boolean fgServiceShown = channel.isFgServiceShown(); |
| 4244 | if (((channel.getUserLockedFields() & NotificationChannel.USER_LOCKED_IMPORTANCE) == 0 |
| 4245 | || !fgServiceShown) |
| 4246 | && (r.getImportance() == IMPORTANCE_MIN |
| 4247 | || r.getImportance() == IMPORTANCE_NONE)) { |
| 4248 | // Increase the importance of foreground service notifications unless the user had |
| 4249 | // an opinion otherwise (and the channel hasn't yet shown a fg service). |
| 4250 | if (TextUtils.isEmpty(channelId) |
| 4251 | || NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 4252 | r.setSystemImportance(IMPORTANCE_LOW); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 4253 | } else { |
| 4254 | channel.setImportance(IMPORTANCE_LOW); |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 4255 | r.setSystemImportance(IMPORTANCE_LOW); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 4256 | if (!fgServiceShown) { |
| 4257 | channel.unlockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); |
| 4258 | channel.setFgServiceShown(true); |
| 4259 | } |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 4260 | mPreferencesHelper.updateNotificationChannel( |
| 4261 | pkg, notificationUid, channel, false); |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 4262 | r.updateNotificationChannel(channel); |
| 4263 | } |
| 4264 | } else if (!fgServiceShown && !TextUtils.isEmpty(channelId) |
| 4265 | && !NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 4266 | channel.setFgServiceShown(true); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 4267 | r.updateNotificationChannel(channel); |
| 4268 | } |
| 4269 | } |
| 4270 | |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 4271 | if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r, |
| 4272 | r.sbn.getOverrideGroupKey() != null)) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4273 | return; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 4274 | } |
| 4275 | |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 4276 | // Whitelist pending intents. |
| 4277 | if (notification.allPendingIntents != null) { |
| 4278 | final int intentCount = notification.allPendingIntents.size(); |
| 4279 | if (intentCount > 0) { |
| 4280 | final ActivityManagerInternal am = LocalServices |
| 4281 | .getService(ActivityManagerInternal.class); |
| 4282 | final long duration = LocalServices.getService( |
| 4283 | DeviceIdleController.LocalService.class).getNotificationWhitelistDuration(); |
| 4284 | for (int i = 0; i < intentCount; i++) { |
| 4285 | PendingIntent pendingIntent = notification.allPendingIntents.valueAt(i); |
| 4286 | if (pendingIntent != null) { |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 4287 | am.setPendingIntentWhitelistDuration(pendingIntent.getTarget(), |
| 4288 | WHITELIST_TOKEN, duration); |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 4289 | } |
| 4290 | } |
| 4291 | } |
| 4292 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 4293 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4294 | mHandler.post(new EnqueueNotificationRunnable(userId, r)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4295 | } |
| 4296 | |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 4297 | private void doChannelWarningToast(CharSequence toastText) { |
Julia Reynolds | bba26b1 | 2018-10-11 09:21:11 -0400 | [diff] [blame] | 4298 | Binder.withCleanCallingIdentity(() -> { |
| 4299 | final int defaultWarningEnabled = Build.IS_DEBUGGABLE ? 1 : 0; |
| 4300 | final boolean warningEnabled = Settings.Global.getInt(getContext().getContentResolver(), |
| 4301 | Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, defaultWarningEnabled) != 0; |
| 4302 | if (warningEnabled) { |
| 4303 | Toast toast = Toast.makeText(getContext(), mHandler.getLooper(), toastText, |
| 4304 | Toast.LENGTH_SHORT); |
| 4305 | toast.show(); |
| 4306 | } |
| 4307 | }); |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 4308 | } |
| 4309 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4310 | @VisibleForTesting |
| 4311 | int resolveNotificationUid(String callingPkg, String targetPkg, |
| 4312 | int callingUid, int userId) { |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4313 | if (userId == UserHandle.USER_ALL) { |
| 4314 | userId = USER_SYSTEM; |
| 4315 | } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4316 | // posted from app A on behalf of app A |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 4317 | if (isCallerSameApp(targetPkg, callingUid, userId) |
Julia Reynolds | ecc1b57 | 2018-10-01 16:19:24 -0400 | [diff] [blame] | 4318 | && (TextUtils.equals(callingPkg, targetPkg) |
| 4319 | || isCallerSameApp(callingPkg, callingUid, userId))) { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4320 | return callingUid; |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4321 | } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4322 | |
| 4323 | int targetUid = -1; |
| 4324 | try { |
| 4325 | targetUid = mPackageManagerClient.getPackageUidAsUser(targetPkg, userId); |
| 4326 | } catch (NameNotFoundException e) { |
| 4327 | /* ignore */ |
| 4328 | } |
| 4329 | // posted from app A on behalf of app B |
| 4330 | if (targetUid != -1 && (isCallerAndroid(callingPkg, callingUid) |
| 4331 | || mPreferencesHelper.isDelegateAllowed( |
| 4332 | targetPkg, targetUid, callingPkg, callingUid))) { |
| 4333 | return targetUid; |
| 4334 | } |
| 4335 | |
Julia Reynolds | ecc1b57 | 2018-10-01 16:19:24 -0400 | [diff] [blame] | 4336 | throw new SecurityException("Caller " + callingPkg + ":" + callingUid |
| 4337 | + " cannot post for pkg " + targetPkg + " in user " + userId); |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4338 | } |
| 4339 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4340 | /** |
| 4341 | * Checks if a notification can be posted. checks rate limiter, snooze helper, and blocking. |
| 4342 | * |
| 4343 | * Has side effects. |
| 4344 | */ |
| 4345 | private boolean checkDisqualifyingFeatures(int userId, int callingUid, int id, String tag, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 4346 | NotificationRecord r, boolean isAutogroup) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4347 | final String pkg = r.sbn.getPackageName(); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4348 | final boolean isSystemNotification = |
| 4349 | isUidSystemOrPhone(callingUid) || ("android".equals(pkg)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4350 | final boolean isNotificationFromListener = mListeners.isListenerPackage(pkg); |
| 4351 | |
| 4352 | // Limit the number of notifications that any given package except the android |
| 4353 | // package or a registered listener can enqueue. Prevents DOS attacks and deals with leaks. |
| 4354 | if (!isSystemNotification && !isNotificationFromListener) { |
| 4355 | synchronized (mNotificationLock) { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 4356 | if (mNotificationsByKey.get(r.sbn.getKey()) == null |
Julia Reynolds | ecc1b57 | 2018-10-01 16:19:24 -0400 | [diff] [blame] | 4357 | && isCallerInstantApp(pkg, Binder.getCallingUid(), userId)) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 4358 | // Ephemeral apps have some special constraints for notifications. |
| 4359 | // They are not allowed to create new notifications however they are allowed to |
| 4360 | // update notifications created by the system (e.g. a foreground service |
| 4361 | // notification). |
| 4362 | throw new SecurityException("Instant app " + pkg |
| 4363 | + " cannot create notifications"); |
| 4364 | } |
| 4365 | |
| 4366 | // rate limit updates that aren't completed progress notifications |
| 4367 | if (mNotificationsByKey.get(r.sbn.getKey()) != null |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 4368 | && !r.getNotification().hasCompletedProgress() |
| 4369 | && !isAutogroup) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 4370 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4371 | final float appEnqueueRate = mUsageStats.getAppEnqueueRate(pkg); |
| 4372 | if (appEnqueueRate > mMaxPackageEnqueueRate) { |
| 4373 | mUsageStats.registerOverRateQuota(pkg); |
| 4374 | final long now = SystemClock.elapsedRealtime(); |
| 4375 | if ((now - mLastOverRateLogTime) > MIN_PACKAGE_OVERRATE_LOG_INTERVAL) { |
| 4376 | Slog.e(TAG, "Package enqueue rate is " + appEnqueueRate |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 4377 | + ". Shedding " + r.sbn.getKey() + ". package=" + pkg); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4378 | mLastOverRateLogTime = now; |
| 4379 | } |
| 4380 | return false; |
| 4381 | } |
| 4382 | } |
| 4383 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 4384 | // limit the number of outstanding notificationrecords an app can have |
| 4385 | int count = getNotificationCountLocked(pkg, userId, id, tag); |
| 4386 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 4387 | mUsageStats.registerOverCountQuota(pkg); |
| 4388 | Slog.e(TAG, "Package has already posted or enqueued " + count |
| 4389 | + " notifications. Not showing more. package=" + pkg); |
| 4390 | return false; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4391 | } |
| 4392 | } |
| 4393 | } |
| 4394 | |
| 4395 | // snoozed apps |
| 4396 | if (mSnoozeHelper.isSnoozed(userId, pkg, r.getKey())) { |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4397 | MetricsLogger.action(r.getLogMaker() |
| 4398 | .setType(MetricsProto.MetricsEvent.TYPE_UPDATE) |
| 4399 | .setCategory(MetricsProto.MetricsEvent.NOTIFICATION_SNOOZED)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4400 | if (DBG) { |
| 4401 | Slog.d(TAG, "Ignored enqueue for snoozed notification " + r.getKey()); |
| 4402 | } |
| 4403 | mSnoozeHelper.update(userId, r); |
| 4404 | savePolicyFile(); |
| 4405 | return false; |
| 4406 | } |
| 4407 | |
| 4408 | |
| 4409 | // blocked apps |
| 4410 | if (isBlocked(r, mUsageStats)) { |
| 4411 | return false; |
| 4412 | } |
| 4413 | |
| 4414 | return true; |
| 4415 | } |
| 4416 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 4417 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 4418 | protected int getNotificationCountLocked(String pkg, int userId, int excludedId, |
| 4419 | String excludedTag) { |
| 4420 | int count = 0; |
| 4421 | final int N = mNotificationList.size(); |
| 4422 | for (int i = 0; i < N; i++) { |
| 4423 | final NotificationRecord existing = mNotificationList.get(i); |
| 4424 | if (existing.sbn.getPackageName().equals(pkg) |
| 4425 | && existing.sbn.getUserId() == userId) { |
| 4426 | if (existing.sbn.getId() == excludedId |
| 4427 | && TextUtils.equals(existing.sbn.getTag(), excludedTag)) { |
| 4428 | continue; |
| 4429 | } |
| 4430 | count++; |
| 4431 | } |
| 4432 | } |
| 4433 | final int M = mEnqueuedNotifications.size(); |
| 4434 | for (int i = 0; i < M; i++) { |
| 4435 | final NotificationRecord existing = mEnqueuedNotifications.get(i); |
| 4436 | if (existing.sbn.getPackageName().equals(pkg) |
| 4437 | && existing.sbn.getUserId() == userId) { |
| 4438 | count++; |
| 4439 | } |
| 4440 | } |
| 4441 | return count; |
| 4442 | } |
| 4443 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4444 | protected boolean isBlocked(NotificationRecord r, NotificationUsageStats usageStats) { |
Beverly | 3c707b4 | 2018-09-14 09:49:07 -0400 | [diff] [blame] | 4445 | if (isBlocked(r)) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4446 | Slog.e(TAG, "Suppressing notification from package by user request."); |
| 4447 | usageStats.registerBlocked(r); |
Beverly | 3c707b4 | 2018-09-14 09:49:07 -0400 | [diff] [blame] | 4448 | return true; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4449 | } |
Beverly | 3c707b4 | 2018-09-14 09:49:07 -0400 | [diff] [blame] | 4450 | return false; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4451 | } |
| 4452 | |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 4453 | private boolean isBlocked(NotificationRecord r) { |
| 4454 | final String pkg = r.sbn.getPackageName(); |
| 4455 | final int callingUid = r.sbn.getUid(); |
| 4456 | return mPreferencesHelper.isGroupBlocked(pkg, callingUid, r.getChannel().getGroup()) |
| 4457 | || mPreferencesHelper.getImportance(pkg, callingUid) |
| 4458 | == NotificationManager.IMPORTANCE_NONE |
| 4459 | || r.getImportance() == NotificationManager.IMPORTANCE_NONE; |
| 4460 | } |
| 4461 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4462 | protected class SnoozeNotificationRunnable implements Runnable { |
| 4463 | private final String mKey; |
| 4464 | private final long mDuration; |
| 4465 | private final String mSnoozeCriterionId; |
| 4466 | |
| 4467 | SnoozeNotificationRunnable(String key, long duration, String snoozeCriterionId) { |
| 4468 | mKey = key; |
| 4469 | mDuration = duration; |
| 4470 | mSnoozeCriterionId = snoozeCriterionId; |
| 4471 | } |
| 4472 | |
| 4473 | @Override |
| 4474 | public void run() { |
| 4475 | synchronized (mNotificationLock) { |
| 4476 | final NotificationRecord r = findNotificationByKeyLocked(mKey); |
| 4477 | if (r != null) { |
| 4478 | snoozeLocked(r); |
| 4479 | } |
| 4480 | } |
| 4481 | } |
| 4482 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4483 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4484 | void snoozeLocked(NotificationRecord r) { |
| 4485 | if (r.sbn.isGroup()) { |
| 4486 | final List<NotificationRecord> groupNotifications = findGroupNotificationsLocked( |
| 4487 | r.sbn.getPackageName(), r.sbn.getGroupKey(), r.sbn.getUserId()); |
| 4488 | if (r.getNotification().isGroupSummary()) { |
| 4489 | // snooze summary and all children |
| 4490 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 4491 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 4492 | } |
| 4493 | } else { |
| 4494 | // if there is a valid summary for this group, and we are snoozing the only |
| 4495 | // child, also snooze the summary |
| 4496 | if (mSummaryByGroupKey.containsKey(r.sbn.getGroupKey())) { |
| 4497 | if (groupNotifications.size() != 2) { |
| 4498 | snoozeNotificationLocked(r); |
| 4499 | } else { |
| 4500 | // snooze summary and the one child |
| 4501 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 4502 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 4503 | } |
| 4504 | } |
| 4505 | } else { |
| 4506 | snoozeNotificationLocked(r); |
| 4507 | } |
| 4508 | } |
| 4509 | } else { |
| 4510 | // just snooze the one notification |
| 4511 | snoozeNotificationLocked(r); |
| 4512 | } |
| 4513 | } |
| 4514 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4515 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4516 | void snoozeNotificationLocked(NotificationRecord r) { |
| 4517 | MetricsLogger.action(r.getLogMaker() |
| 4518 | .setCategory(MetricsEvent.NOTIFICATION_SNOOZED) |
| 4519 | .setType(MetricsEvent.TYPE_CLOSE) |
Chris Wren | 21a2e72 | 2017-10-02 17:44:53 -0400 | [diff] [blame] | 4520 | .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_SNOOZE_DURATION_MS, |
| 4521 | mDuration) |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4522 | .addTaggedData(MetricsEvent.NOTIFICATION_SNOOZED_CRITERIA, |
| 4523 | mSnoozeCriterionId == null ? 0 : 1)); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4524 | boolean wasPosted = removeFromNotificationListsLocked(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4525 | cancelNotificationLocked(r, false, REASON_SNOOZED, wasPosted, null); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4526 | updateLightsLocked(); |
| 4527 | if (mSnoozeCriterionId != null) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4528 | mAssistants.notifyAssistantSnoozedLocked(r.sbn, mSnoozeCriterionId); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4529 | mSnoozeHelper.snooze(r); |
| 4530 | } else { |
| 4531 | mSnoozeHelper.snooze(r, mDuration); |
| 4532 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4533 | r.recordSnoozed(); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4534 | savePolicyFile(); |
| 4535 | } |
| 4536 | } |
| 4537 | |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 4538 | protected class CancelNotificationRunnable implements Runnable { |
| 4539 | private final int mCallingUid; |
| 4540 | private final int mCallingPid; |
| 4541 | private final String mPkg; |
| 4542 | private final String mTag; |
| 4543 | private final int mId; |
| 4544 | private final int mMustHaveFlags; |
| 4545 | private final int mMustNotHaveFlags; |
| 4546 | private final boolean mSendDelete; |
| 4547 | private final int mUserId; |
| 4548 | private final int mReason; |
| 4549 | private final int mRank; |
| 4550 | private final int mCount; |
| 4551 | private final ManagedServiceInfo mListener; |
| 4552 | |
| 4553 | CancelNotificationRunnable(final int callingUid, final int callingPid, |
| 4554 | final String pkg, final String tag, final int id, |
| 4555 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
| 4556 | final int userId, final int reason, int rank, int count, |
| 4557 | final ManagedServiceInfo listener) { |
| 4558 | this.mCallingUid = callingUid; |
| 4559 | this.mCallingPid = callingPid; |
| 4560 | this.mPkg = pkg; |
| 4561 | this.mTag = tag; |
| 4562 | this.mId = id; |
| 4563 | this.mMustHaveFlags = mustHaveFlags; |
| 4564 | this.mMustNotHaveFlags = mustNotHaveFlags; |
| 4565 | this.mSendDelete = sendDelete; |
| 4566 | this.mUserId = userId; |
| 4567 | this.mReason = reason; |
| 4568 | this.mRank = rank; |
| 4569 | this.mCount = count; |
| 4570 | this.mListener = listener; |
| 4571 | } |
| 4572 | |
| 4573 | @Override |
| 4574 | public void run() { |
| 4575 | String listenerName = mListener == null ? null : mListener.component.toShortString(); |
| 4576 | if (DBG) { |
| 4577 | EventLogTags.writeNotificationCancel(mCallingUid, mCallingPid, mPkg, mId, mTag, |
| 4578 | mUserId, mMustHaveFlags, mMustNotHaveFlags, mReason, listenerName); |
| 4579 | } |
| 4580 | |
| 4581 | synchronized (mNotificationLock) { |
| 4582 | // Look for the notification, searching both the posted and enqueued lists. |
| 4583 | NotificationRecord r = findNotificationLocked(mPkg, mTag, mId, mUserId); |
| 4584 | if (r != null) { |
| 4585 | // The notification was found, check if it should be removed. |
| 4586 | |
| 4587 | // Ideally we'd do this in the caller of this method. However, that would |
| 4588 | // require the caller to also find the notification. |
| 4589 | if (mReason == REASON_CLICK) { |
| 4590 | mUsageStats.registerClickedByUser(r); |
| 4591 | } |
| 4592 | |
| 4593 | if ((r.getNotification().flags & mMustHaveFlags) != mMustHaveFlags) { |
| 4594 | return; |
| 4595 | } |
| 4596 | if ((r.getNotification().flags & mMustNotHaveFlags) != 0) { |
| 4597 | return; |
| 4598 | } |
| 4599 | |
| 4600 | // Cancel the notification. |
| 4601 | boolean wasPosted = removeFromNotificationListsLocked(r); |
| 4602 | cancelNotificationLocked( |
| 4603 | r, mSendDelete, mReason, mRank, mCount, wasPosted, listenerName); |
| 4604 | cancelGroupChildrenLocked(r, mCallingUid, mCallingPid, listenerName, |
| 4605 | mSendDelete, null); |
| 4606 | updateLightsLocked(); |
| 4607 | } else { |
| 4608 | // No notification was found, assume that it is snoozed and cancel it. |
| 4609 | if (mReason != REASON_SNOOZED) { |
| 4610 | final boolean wasSnoozed = mSnoozeHelper.cancel(mUserId, mPkg, mTag, mId); |
| 4611 | if (wasSnoozed) { |
| 4612 | savePolicyFile(); |
| 4613 | } |
| 4614 | } |
| 4615 | } |
| 4616 | } |
| 4617 | } |
| 4618 | } |
| 4619 | |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 4620 | protected class EnqueueNotificationRunnable implements Runnable { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4621 | private final NotificationRecord r; |
| 4622 | private final int userId; |
| 4623 | |
| 4624 | EnqueueNotificationRunnable(int userId, NotificationRecord r) { |
| 4625 | this.userId = userId; |
| 4626 | this.r = r; |
| 4627 | }; |
| 4628 | |
| 4629 | @Override |
| 4630 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4631 | synchronized (mNotificationLock) { |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 4632 | mEnqueuedNotifications.add(r); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4633 | scheduleTimeoutLocked(r); |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 4634 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4635 | final StatusBarNotification n = r.sbn; |
| 4636 | if (DBG) Slog.d(TAG, "EnqueueNotificationRunnable.run for: " + n.getKey()); |
| 4637 | NotificationRecord old = mNotificationsByKey.get(n.getKey()); |
| 4638 | if (old != null) { |
| 4639 | // Retain ranking information from previous record |
| 4640 | r.copyRankingInformation(old); |
| 4641 | } |
| 4642 | |
| 4643 | final int callingUid = n.getUid(); |
| 4644 | final int callingPid = n.getInitialPid(); |
| 4645 | final Notification notification = n.getNotification(); |
| 4646 | final String pkg = n.getPackageName(); |
| 4647 | final int id = n.getId(); |
| 4648 | final String tag = n.getTag(); |
| 4649 | |
| 4650 | // Handle grouped notifications and bail out early if we |
| 4651 | // can to avoid extracting signals. |
| 4652 | handleGroupedNotificationLocked(r, old, callingUid, callingPid); |
| 4653 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4654 | // if this is a group child, unsnooze parent summary |
| 4655 | if (n.isGroup() && notification.isGroupChild()) { |
| 4656 | mSnoozeHelper.repostGroupSummary(pkg, r.getUserId(), n.getGroupKey()); |
| 4657 | } |
| 4658 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4659 | // This conditional is a dirty hack to limit the logging done on |
| 4660 | // behalf of the download manager without affecting other apps. |
| 4661 | if (!pkg.equals("com.android.providers.downloads") |
| 4662 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| 4663 | int enqueueStatus = EVENTLOG_ENQUEUE_STATUS_NEW; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4664 | if (old != null) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4665 | enqueueStatus = EVENTLOG_ENQUEUE_STATUS_UPDATE; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4666 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4667 | EventLogTags.writeNotificationEnqueue(callingUid, callingPid, |
| 4668 | pkg, id, tag, userId, notification.toString(), |
| 4669 | enqueueStatus); |
| 4670 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4671 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4672 | mRankingHelper.extractSignals(r); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4673 | // tell the assistant service about the notification |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4674 | if (mAssistants.isEnabled()) { |
| 4675 | mAssistants.onNotificationEnqueued(r); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4676 | mHandler.postDelayed(new PostNotificationRunnable(r.getKey()), |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4677 | DELAY_FOR_ASSISTANT_TIME); |
| 4678 | } else { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4679 | mHandler.post(new PostNotificationRunnable(r.getKey())); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4680 | } |
| 4681 | } |
| 4682 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4683 | } |
| 4684 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 4685 | @GuardedBy("mNotificationLock") |
| 4686 | private boolean isPackageSuspendedLocked(NotificationRecord r) { |
| 4687 | final String pkg = r.sbn.getPackageName(); |
| 4688 | final int callingUid = r.sbn.getUid(); |
| 4689 | |
| 4690 | return isPackageSuspendedForUser(pkg, callingUid); |
| 4691 | } |
| 4692 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4693 | protected class PostNotificationRunnable implements Runnable { |
| 4694 | private final String key; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4695 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4696 | PostNotificationRunnable(String key) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4697 | this.key = key; |
| 4698 | } |
| 4699 | |
| 4700 | @Override |
| 4701 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4702 | synchronized (mNotificationLock) { |
| 4703 | try { |
| 4704 | NotificationRecord r = null; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4705 | int N = mEnqueuedNotifications.size(); |
| 4706 | for (int i = 0; i < N; i++) { |
| 4707 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 4708 | if (Objects.equals(key, enqueued.getKey())) { |
| 4709 | r = enqueued; |
| 4710 | break; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4711 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4712 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4713 | if (r == null) { |
| 4714 | Slog.i(TAG, "Cannot find enqueued record for key: " + key); |
| 4715 | return; |
| 4716 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 4717 | |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 4718 | if (isBlocked(r)) { |
| 4719 | Slog.i(TAG, "notification blocked by assistant request"); |
| 4720 | return; |
| 4721 | } |
| 4722 | |
Beverly | 3c707b4 | 2018-09-14 09:49:07 -0400 | [diff] [blame] | 4723 | final boolean isPackageSuspended = isPackageSuspendedLocked(r); |
| 4724 | r.setHidden(isPackageSuspended); |
| 4725 | if (isPackageSuspended) { |
| 4726 | mUsageStats.registerSuspendedByAdmin(r); |
| 4727 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4728 | NotificationRecord old = mNotificationsByKey.get(key); |
| 4729 | final StatusBarNotification n = r.sbn; |
| 4730 | final Notification notification = n.getNotification(); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4731 | int index = indexOfNotificationLocked(n.getKey()); |
| 4732 | if (index < 0) { |
| 4733 | mNotificationList.add(r); |
| 4734 | mUsageStats.registerPostedByApp(r); |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 4735 | r.setInterruptive(isVisuallyInterruptive(null, r)); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4736 | } else { |
| 4737 | old = mNotificationList.get(index); |
| 4738 | mNotificationList.set(index, r); |
| 4739 | mUsageStats.registerUpdatedByApp(r, old); |
| 4740 | // Make sure we don't lose the foreground service state. |
| 4741 | notification.flags |= |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4742 | old.getNotification().flags & FLAG_FOREGROUND_SERVICE; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4743 | r.isUpdate = true; |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 4744 | r.setTextChanged(isVisuallyInterruptive(old, r)); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | mNotificationsByKey.put(n.getKey(), r); |
| 4748 | |
| 4749 | // Ensure if this is a foreground service that the proper additional |
| 4750 | // flags are set. |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4751 | if ((notification.flags & FLAG_FOREGROUND_SERVICE) != 0) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4752 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 4753 | | Notification.FLAG_NO_CLEAR; |
| 4754 | } |
| 4755 | |
| 4756 | applyZenModeLocked(r); |
| 4757 | mRankingHelper.sort(mNotificationList); |
| 4758 | |
Gus Prevas | a322649 | 2018-10-23 11:10:09 -0400 | [diff] [blame] | 4759 | if (!r.isHidden()) { |
| 4760 | buzzBeepBlinkLocked(r); |
| 4761 | } |
| 4762 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4763 | if (notification.getSmallIcon() != null) { |
| 4764 | StatusBarNotification oldSbn = (old != null) ? old.sbn : null; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 4765 | mListeners.notifyPostedLocked(r, old); |
Brad Stenning | d2e7a97 | 2018-10-01 09:08:42 -0700 | [diff] [blame] | 4766 | if ((oldSbn == null || !Objects.equals(oldSbn.getGroup(), n.getGroup())) |
| 4767 | && !isCritical(r)) { |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 4768 | mHandler.post(new Runnable() { |
| 4769 | @Override |
| 4770 | public void run() { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 4771 | mGroupHelper.onNotificationPosted( |
| 4772 | n, hasAutoGroupSummaryLocked(n)); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 4773 | } |
| 4774 | }); |
| 4775 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4776 | } else { |
| 4777 | Slog.e(TAG, "Not posting notification without small icon: " + notification); |
| 4778 | if (old != null && !old.isCanceled) { |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 4779 | mListeners.notifyRemovedLocked(r, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4780 | NotificationListenerService.REASON_ERROR, null); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4781 | mHandler.post(new Runnable() { |
| 4782 | @Override |
| 4783 | public void run() { |
| 4784 | mGroupHelper.onNotificationRemoved(n); |
| 4785 | } |
| 4786 | }); |
| 4787 | } |
| 4788 | // ATTENTION: in a future release we will bail out here |
| 4789 | // so that we do not play sounds, show lights, etc. for invalid |
| 4790 | // notifications |
| 4791 | Slog.e(TAG, "WARNING: In a future release this will crash the app: " |
| 4792 | + n.getPackageName()); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4793 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4794 | |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 4795 | maybeRecordInterruptionLocked(r); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4796 | } finally { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4797 | int N = mEnqueuedNotifications.size(); |
| 4798 | for (int i = 0; i < N; i++) { |
| 4799 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 4800 | if (Objects.equals(key, enqueued.getKey())) { |
| 4801 | mEnqueuedNotifications.remove(i); |
| 4802 | break; |
| 4803 | } |
| 4804 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4805 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4806 | } |
| 4807 | } |
| 4808 | } |
| 4809 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4810 | /** |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4811 | * If the notification differs enough visually, consider it a new interruptive notification. |
| 4812 | */ |
| 4813 | @GuardedBy("mNotificationLock") |
| 4814 | @VisibleForTesting |
| 4815 | protected boolean isVisuallyInterruptive(NotificationRecord old, NotificationRecord r) { |
Julia Reynolds | 760fa76 | 2018-06-19 15:39:23 -0400 | [diff] [blame] | 4816 | // Ignore summary updates because we don't display most of the information. |
| 4817 | if (r.sbn.isGroup() && r.sbn.getNotification().isGroupSummary()) { |
| 4818 | if (DEBUG_INTERRUPTIVENESS) { |
| 4819 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4820 | + r.getKey() + " is not interruptive: summary"); |
| 4821 | } |
| 4822 | return false; |
| 4823 | } |
| 4824 | |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4825 | if (old == null) { |
| 4826 | if (DEBUG_INTERRUPTIVENESS) { |
| 4827 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4828 | + r.getKey() + " is interruptive: new notification"); |
| 4829 | } |
| 4830 | return true; |
| 4831 | } |
| 4832 | |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 4833 | if (r == null) { |
| 4834 | if (DEBUG_INTERRUPTIVENESS) { |
| 4835 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4836 | + r.getKey() + " is not interruptive: null"); |
| 4837 | } |
| 4838 | return false; |
| 4839 | } |
| 4840 | |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4841 | Notification oldN = old.sbn.getNotification(); |
| 4842 | Notification newN = r.sbn.getNotification(); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4843 | |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4844 | if (oldN.extras == null || newN.extras == null) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4845 | if (DEBUG_INTERRUPTIVENESS) { |
| 4846 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4847 | + r.getKey() + " is not interruptive: no extras"); |
| 4848 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4849 | return false; |
| 4850 | } |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4851 | |
| 4852 | // Ignore visual interruptions from foreground services because users |
| 4853 | // consider them one 'session'. Count them for everything else. |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 4854 | if ((r.sbn.getNotification().flags & FLAG_FOREGROUND_SERVICE) != 0) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4855 | if (DEBUG_INTERRUPTIVENESS) { |
| 4856 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4857 | + r.getKey() + " is not interruptive: foreground service"); |
| 4858 | } |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4859 | return false; |
| 4860 | } |
| 4861 | |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4862 | final String oldTitle = String.valueOf(oldN.extras.get(Notification.EXTRA_TITLE)); |
| 4863 | final String newTitle = String.valueOf(newN.extras.get(Notification.EXTRA_TITLE)); |
| 4864 | if (!Objects.equals(oldTitle, newTitle)) { |
| 4865 | if (DEBUG_INTERRUPTIVENESS) { |
| 4866 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4867 | + r.getKey() + " is interruptive: changed title"); |
| 4868 | Log.v(TAG, "INTERRUPTIVENESS: " + String.format(" old title: %s (%s@0x%08x)", |
| 4869 | oldTitle, oldTitle.getClass(), oldTitle.hashCode())); |
| 4870 | Log.v(TAG, "INTERRUPTIVENESS: " + String.format(" new title: %s (%s@0x%08x)", |
| 4871 | newTitle, newTitle.getClass(), newTitle.hashCode())); |
| 4872 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4873 | return true; |
| 4874 | } |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4875 | // Do not compare Spannables (will always return false); compare unstyled Strings |
| 4876 | final String oldText = String.valueOf(oldN.extras.get(Notification.EXTRA_TEXT)); |
| 4877 | final String newText = String.valueOf(newN.extras.get(Notification.EXTRA_TEXT)); |
| 4878 | if (!Objects.equals(oldText, newText)) { |
| 4879 | if (DEBUG_INTERRUPTIVENESS) { |
| 4880 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4881 | + r.getKey() + " is interruptive: changed text"); |
| 4882 | Log.v(TAG, "INTERRUPTIVENESS: " + String.format(" old text: %s (%s@0x%08x)", |
| 4883 | oldText, oldText.getClass(), oldText.hashCode())); |
| 4884 | Log.v(TAG, "INTERRUPTIVENESS: " + String.format(" new text: %s (%s@0x%08x)", |
| 4885 | newText, newText.getClass(), newText.hashCode())); |
| 4886 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4887 | return true; |
| 4888 | } |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4889 | if (oldN.hasCompletedProgress() != newN.hasCompletedProgress()) { |
| 4890 | if (DEBUG_INTERRUPTIVENESS) { |
| 4891 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4892 | + r.getKey() + " is interruptive: completed progress"); |
| 4893 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4894 | return true; |
| 4895 | } |
| 4896 | // Actions |
| 4897 | if (Notification.areActionsVisiblyDifferent(oldN, newN)) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4898 | if (DEBUG_INTERRUPTIVENESS) { |
| 4899 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4900 | + r.getKey() + " is interruptive: changed actions"); |
| 4901 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4902 | return true; |
| 4903 | } |
| 4904 | |
| 4905 | try { |
| 4906 | Notification.Builder oldB = Notification.Builder.recoverBuilder(getContext(), oldN); |
| 4907 | Notification.Builder newB = Notification.Builder.recoverBuilder(getContext(), newN); |
| 4908 | |
| 4909 | // Style based comparisons |
| 4910 | if (Notification.areStyledNotificationsVisiblyDifferent(oldB, newB)) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4911 | if (DEBUG_INTERRUPTIVENESS) { |
| 4912 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4913 | + r.getKey() + " is interruptive: styles differ"); |
| 4914 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4915 | return true; |
| 4916 | } |
| 4917 | |
| 4918 | // Remote views |
| 4919 | if (Notification.areRemoteViewsChanged(oldB, newB)) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4920 | if (DEBUG_INTERRUPTIVENESS) { |
| 4921 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4922 | + r.getKey() + " is interruptive: remoteviews differ"); |
| 4923 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4924 | return true; |
| 4925 | } |
| 4926 | } catch (Exception e) { |
| 4927 | Slog.w(TAG, "error recovering builder", e); |
| 4928 | } |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4929 | |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4930 | return false; |
| 4931 | } |
| 4932 | |
| 4933 | /** |
Brad Stenning | d2e7a97 | 2018-10-01 09:08:42 -0700 | [diff] [blame] | 4934 | * Check if the notification is classified as critical. |
| 4935 | * |
| 4936 | * @param record the record to test for criticality |
| 4937 | * @return {@code true} if notification is considered critical |
| 4938 | * |
| 4939 | * @see CriticalNotificationExtractor for criteria |
| 4940 | */ |
| 4941 | private boolean isCritical(NotificationRecord record) { |
| 4942 | // 0 is the most critical |
| 4943 | return record.getCriticality() < CriticalNotificationExtractor.NORMAL; |
| 4944 | } |
| 4945 | |
| 4946 | /** |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 4947 | * Keeps the last 5 packages that have notified, by user. |
| 4948 | */ |
| 4949 | @GuardedBy("mNotificationLock") |
| 4950 | @VisibleForTesting |
| 4951 | protected void logRecentLocked(NotificationRecord r) { |
| 4952 | if (r.isUpdate) { |
| 4953 | return; |
| 4954 | } |
| 4955 | ArrayList<NotifyingApp> recentAppsForUser = |
| 4956 | mRecentApps.getOrDefault(r.getUser().getIdentifier(), new ArrayList<>(6)); |
| 4957 | NotifyingApp na = new NotifyingApp() |
| 4958 | .setPackage(r.sbn.getPackageName()) |
| 4959 | .setUid(r.sbn.getUid()) |
| 4960 | .setLastNotified(r.sbn.getPostTime()); |
| 4961 | // A new notification gets an app moved to the front of the list |
| 4962 | for (int i = recentAppsForUser.size() - 1; i >= 0; i--) { |
| 4963 | NotifyingApp naExisting = recentAppsForUser.get(i); |
| 4964 | if (na.getPackage().equals(naExisting.getPackage()) |
| 4965 | && na.getUid() == naExisting.getUid()) { |
| 4966 | recentAppsForUser.remove(i); |
| 4967 | break; |
| 4968 | } |
| 4969 | } |
| 4970 | // time is always increasing, so always add to the front of the list |
| 4971 | recentAppsForUser.add(0, na); |
| 4972 | if (recentAppsForUser.size() > 5) { |
| 4973 | recentAppsForUser.remove(recentAppsForUser.size() -1); |
| 4974 | } |
| 4975 | mRecentApps.put(r.getUser().getIdentifier(), recentAppsForUser); |
| 4976 | } |
| 4977 | |
| 4978 | /** |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4979 | * Ensures that grouped notification receive their special treatment. |
| 4980 | * |
| 4981 | * <p>Cancels group children if the new notification causes a group to lose |
| 4982 | * its summary.</p> |
| 4983 | * |
| 4984 | * <p>Updates mSummaryByGroupKey.</p> |
| 4985 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4986 | @GuardedBy("mNotificationLock") |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4987 | private void handleGroupedNotificationLocked(NotificationRecord r, NotificationRecord old, |
| 4988 | int callingUid, int callingPid) { |
| 4989 | StatusBarNotification sbn = r.sbn; |
| 4990 | Notification n = sbn.getNotification(); |
Selim Cinek | 5b03ce9 | 2016-05-18 15:16:58 -0700 | [diff] [blame] | 4991 | if (n.isGroupSummary() && !sbn.isAppGroup()) { |
| 4992 | // notifications without a group shouldn't be a summary, otherwise autobundling can |
| 4993 | // lead to bugs |
| 4994 | n.flags &= ~Notification.FLAG_GROUP_SUMMARY; |
| 4995 | } |
| 4996 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4997 | String group = sbn.getGroupKey(); |
| 4998 | boolean isSummary = n.isGroupSummary(); |
| 4999 | |
| 5000 | Notification oldN = old != null ? old.sbn.getNotification() : null; |
| 5001 | String oldGroup = old != null ? old.sbn.getGroupKey() : null; |
| 5002 | boolean oldIsSummary = old != null && oldN.isGroupSummary(); |
| 5003 | |
| 5004 | if (oldIsSummary) { |
| 5005 | NotificationRecord removedSummary = mSummaryByGroupKey.remove(oldGroup); |
| 5006 | if (removedSummary != old) { |
| 5007 | String removedKey = |
| 5008 | removedSummary != null ? removedSummary.getKey() : "<null>"; |
| 5009 | Slog.w(TAG, "Removed summary didn't match old notification: old=" + old.getKey() + |
| 5010 | ", removed=" + removedKey); |
| 5011 | } |
| 5012 | } |
| 5013 | if (isSummary) { |
| 5014 | mSummaryByGroupKey.put(group, r); |
| 5015 | } |
| 5016 | |
| 5017 | // Clear out group children of the old notification if the update |
| 5018 | // causes the group summary to go away. This happens when the old |
| 5019 | // notification was a summary and the new one isn't, or when the old |
| 5020 | // notification was a summary and its group key changed. |
| 5021 | if (oldIsSummary && (!isSummary || !oldGroup.equals(group))) { |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5022 | cancelGroupChildrenLocked(old, callingUid, callingPid, null, false /* sendDelete */, |
| 5023 | null); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5024 | } |
| 5025 | } |
| 5026 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5027 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5028 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 5029 | void scheduleTimeoutLocked(NotificationRecord record) { |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 5030 | if (record.getNotification().getTimeoutAfter() > 0) { |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 5031 | final PendingIntent pi = PendingIntent.getBroadcast(getContext(), |
| 5032 | REQUEST_CODE_TIMEOUT, |
| 5033 | new Intent(ACTION_NOTIFICATION_TIMEOUT) |
| 5034 | .setData(new Uri.Builder().scheme(SCHEME_TIMEOUT) |
| 5035 | .appendPath(record.getKey()).build()) |
| 5036 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 5037 | .putExtra(EXTRA_KEY, record.getKey()), |
| 5038 | PendingIntent.FLAG_UPDATE_CURRENT); |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 5039 | mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 5040 | SystemClock.elapsedRealtime() + record.getNotification().getTimeoutAfter(), pi); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 5041 | } |
| 5042 | } |
| 5043 | |
| 5044 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5045 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5046 | void buzzBeepBlinkLocked(NotificationRecord record) { |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 5047 | boolean buzz = false; |
| 5048 | boolean beep = false; |
| 5049 | boolean blink = false; |
| 5050 | |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5051 | final Notification notification = record.sbn.getNotification(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5052 | final String key = record.getKey(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5053 | |
| 5054 | // Should this notification make noise, vibe, or use the LED? |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 5055 | final boolean aboveThreshold = |
| 5056 | record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5057 | |
| 5058 | // Remember if this notification already owns the notification channels. |
| 5059 | boolean wasBeep = key != null && key.equals(mSoundNotificationKey); |
| 5060 | boolean wasBuzz = key != null && key.equals(mVibrateNotificationKey); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5061 | // These are set inside the conditional if the notification is allowed to make noise. |
| 5062 | boolean hasValidVibrate = false; |
| 5063 | boolean hasValidSound = false; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 5064 | boolean sentAccessibilityEvent = false; |
| 5065 | // If the notification will appear in the status bar, it should send an accessibility |
| 5066 | // event |
| 5067 | if (!record.isUpdate && record.getImportance() > IMPORTANCE_MIN) { |
| 5068 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
| 5069 | sentAccessibilityEvent = true; |
| 5070 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5071 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5072 | if (aboveThreshold && isNotificationForCurrentUser(record)) { |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 5073 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5074 | if (mSystemReady && mAudioManager != null) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5075 | Uri soundUri = record.getSound(); |
| 5076 | hasValidSound = soundUri != null && !Uri.EMPTY.equals(soundUri); |
| 5077 | long[] vibration = record.getVibration(); |
| 5078 | // Demote sound to vibration if vibration missing & phone in vibration mode. |
| 5079 | if (vibration == null |
| 5080 | && hasValidSound |
| 5081 | && (mAudioManager.getRingerModeInternal() |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 5082 | == AudioManager.RINGER_MODE_VIBRATE) |
| 5083 | && mAudioManager.getStreamVolume( |
| 5084 | AudioAttributes.toLegacyStreamType(record.getAudioAttributes())) == 0) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5085 | vibration = mFallbackVibrationPattern; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5086 | } |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5087 | hasValidVibrate = vibration != null; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 5088 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5089 | boolean hasAudibleAlert = hasValidSound || hasValidVibrate; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5090 | if (hasAudibleAlert && !shouldMuteNotificationLocked(record)) { |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 5091 | if (!sentAccessibilityEvent) { |
| 5092 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
| 5093 | sentAccessibilityEvent = true; |
| 5094 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5095 | if (DBG) Slog.v(TAG, "Interrupting!"); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5096 | if (hasValidSound) { |
| 5097 | mSoundNotificationKey = key; |
| 5098 | if (mInCall) { |
| 5099 | playInCallNotification(); |
| 5100 | beep = true; |
| 5101 | } else { |
| 5102 | beep = playSound(record, soundUri); |
| 5103 | } |
| 5104 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5105 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5106 | final boolean ringerModeSilent = |
| 5107 | mAudioManager.getRingerModeInternal() |
| 5108 | == AudioManager.RINGER_MODE_SILENT; |
| 5109 | if (!mInCall && hasValidVibrate && !ringerModeSilent) { |
| 5110 | mVibrateNotificationKey = key; |
| 5111 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 5112 | buzz = playVibration(record, vibration, hasValidSound); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5113 | } |
Tyler Gunn | 48f8627 | 2018-07-03 12:38:49 -0700 | [diff] [blame] | 5114 | } else if ((record.getFlags() & Notification.FLAG_INSISTENT) != 0) { |
| 5115 | hasValidSound = false; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5116 | } |
| 5117 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5118 | } |
| 5119 | // If a notification is updated to remove the actively playing sound or vibrate, |
| 5120 | // cancel that feedback now |
| 5121 | if (wasBeep && !hasValidSound) { |
| 5122 | clearSoundLocked(); |
| 5123 | } |
| 5124 | if (wasBuzz && !hasValidVibrate) { |
| 5125 | clearVibrateLocked(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5126 | } |
| 5127 | |
| 5128 | // light |
| 5129 | // release the light |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5130 | boolean wasShowLights = mLights.remove(key); |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 5131 | if (canShowLightsLocked(record, aboveThreshold)) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 5132 | mLights.add(key); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5133 | updateLightsLocked(); |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 5134 | if (mUseAttentionLight) { |
| 5135 | mAttentionLight.pulse(); |
| 5136 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 5137 | blink = true; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5138 | } else if (wasShowLights) { |
| 5139 | updateLightsLocked(); |
| 5140 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 5141 | if (buzz || beep || blink) { |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 5142 | // Ignore summary updates because we don't display most of the information. |
| 5143 | if (record.sbn.isGroup() && record.sbn.getNotification().isGroupSummary()) { |
| 5144 | if (DEBUG_INTERRUPTIVENESS) { |
| 5145 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 5146 | + record.getKey() + " is not interruptive: summary"); |
| 5147 | } |
| 5148 | } else { |
| 5149 | if (DEBUG_INTERRUPTIVENESS) { |
| 5150 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 5151 | + record.getKey() + " is interruptive: alerted"); |
| 5152 | } |
| 5153 | record.setInterruptive(true); |
| 5154 | } |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 5155 | MetricsLogger.action(record.getLogMaker() |
| 5156 | .setCategory(MetricsEvent.NOTIFICATION_ALERT) |
| 5157 | .setType(MetricsEvent.TYPE_OPEN) |
| 5158 | .setSubtype((buzz ? 1 : 0) | (beep ? 2 : 0) | (blink ? 4 : 0))); |
| 5159 | EventLogTags.writeNotificationAlert(key, buzz ? 1 : 0, beep ? 1 : 0, blink ? 1 : 0); |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 5160 | } |
Gus Prevas | a322649 | 2018-10-23 11:10:09 -0400 | [diff] [blame] | 5161 | record.setAudiblyAlerted(buzz || beep); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5162 | } |
| 5163 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5164 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 5165 | boolean canShowLightsLocked(final NotificationRecord record, boolean aboveThreshold) { |
| 5166 | // device lacks light |
| 5167 | if (!mHasLight) { |
| 5168 | return false; |
| 5169 | } |
| 5170 | // user turned lights off globally |
| 5171 | if (!mNotificationPulseEnabled) { |
| 5172 | return false; |
| 5173 | } |
| 5174 | // the notification/channel has no light |
| 5175 | if (record.getLight() == null) { |
| 5176 | return false; |
| 5177 | } |
| 5178 | // unimportant notification |
| 5179 | if (!aboveThreshold) { |
| 5180 | return false; |
| 5181 | } |
| 5182 | // suppressed due to DND |
| 5183 | if ((record.getSuppressedVisualEffects() & SUPPRESSED_EFFECT_LIGHTS) != 0) { |
| 5184 | return false; |
| 5185 | } |
| 5186 | // Suppressed because it's a silent update |
| 5187 | final Notification notification = record.getNotification(); |
| 5188 | if (record.isUpdate && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) { |
| 5189 | return false; |
| 5190 | } |
| 5191 | // Suppressed because another notification in its group handles alerting |
| 5192 | if (record.sbn.isGroup() && record.getNotification().suppressAlertingDueToGrouping()) { |
| 5193 | return false; |
| 5194 | } |
| 5195 | // not if in call or the screen's on |
| 5196 | if (mInCall || mScreenOn) { |
| 5197 | return false; |
| 5198 | } |
| 5199 | |
| 5200 | return true; |
| 5201 | } |
| 5202 | |
| 5203 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 5204 | boolean shouldMuteNotificationLocked(final NotificationRecord record) { |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5205 | // Suppressed because it's a silent update |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 5206 | final Notification notification = record.getNotification(); |
Julia Reynolds | 28149f6 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 5207 | if (record.isUpdate && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) { |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 5208 | return true; |
| 5209 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5210 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5211 | // muted by listener |
| 5212 | final String disableEffects = disableNotificationEffects(record); |
| 5213 | if (disableEffects != null) { |
| 5214 | ZenLog.traceDisableEffects(record, disableEffects); |
| 5215 | return true; |
| 5216 | } |
| 5217 | |
| 5218 | // suppressed due to DND |
| 5219 | if (record.isIntercepted()) { |
| 5220 | return true; |
| 5221 | } |
| 5222 | |
| 5223 | // Suppressed because another notification in its group handles alerting |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 5224 | if (record.sbn.isGroup()) { |
Julia Reynolds | 79dfdd6 | 2018-04-17 15:36:33 -0400 | [diff] [blame] | 5225 | if (notification.suppressAlertingDueToGrouping()) { |
| 5226 | return true; |
| 5227 | } |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 5228 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 5229 | |
Julia Reynolds | 65b85cf | 2017-07-20 09:19:20 -0400 | [diff] [blame] | 5230 | // Suppressed for being too recently noisy |
| 5231 | final String pkg = record.sbn.getPackageName(); |
| 5232 | if (mUsageStats.isAlertRateLimited(pkg)) { |
| 5233 | Slog.e(TAG, "Muting recently noisy " + record.getKey()); |
| 5234 | return true; |
| 5235 | } |
| 5236 | |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 5237 | return false; |
| 5238 | } |
| 5239 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 5240 | private boolean playSound(final NotificationRecord record, Uri soundUri) { |
| 5241 | boolean looping = (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
Jean-Michel Trivi | ec2bb18 | 2018-03-23 18:04:00 -0700 | [diff] [blame] | 5242 | // play notifications if there is no user of exclusive audio focus |
| 5243 | // and the stream volume is not 0 (non-zero volume implies not silenced by SILENT or |
| 5244 | // VIBRATE ringer mode) |
| 5245 | if (!mAudioManager.isAudioFocusExclusive() |
| 5246 | && (mAudioManager.getStreamVolume( |
| 5247 | AudioAttributes.toLegacyStreamType(record.getAudioAttributes())) != 0)) { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 5248 | final long identity = Binder.clearCallingIdentity(); |
| 5249 | try { |
| 5250 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 5251 | if (player != null) { |
| 5252 | if (DBG) Slog.v(TAG, "Playing sound " + soundUri |
| 5253 | + " with attributes " + record.getAudioAttributes()); |
| 5254 | player.playAsync(soundUri, record.sbn.getUser(), looping, |
| 5255 | record.getAudioAttributes()); |
| 5256 | return true; |
| 5257 | } |
| 5258 | } catch (RemoteException e) { |
| 5259 | } finally { |
| 5260 | Binder.restoreCallingIdentity(identity); |
| 5261 | } |
| 5262 | } |
| 5263 | return false; |
| 5264 | } |
| 5265 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 5266 | private boolean playVibration(final NotificationRecord record, long[] vibration, |
| 5267 | boolean delayVibForSound) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 5268 | // Escalate privileges so we can use the vibrator even if the |
| 5269 | // notifying app does not have the VIBRATE permission. |
| 5270 | long identity = Binder.clearCallingIdentity(); |
| 5271 | try { |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 5272 | final VibrationEffect effect; |
| 5273 | try { |
| 5274 | final boolean insistent = |
| 5275 | (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 5276 | effect = VibrationEffect.createWaveform( |
| 5277 | vibration, insistent ? 0 : -1 /*repeatIndex*/); |
| 5278 | } catch (IllegalArgumentException e) { |
| 5279 | Slog.e(TAG, "Error creating vibration waveform with pattern: " + |
| 5280 | Arrays.toString(vibration)); |
| 5281 | return false; |
| 5282 | } |
| 5283 | if (delayVibForSound) { |
| 5284 | new Thread(() -> { |
| 5285 | // delay the vibration by the same amount as the notification sound |
| 5286 | final int waitMs = mAudioManager.getFocusRampTimeMs( |
| 5287 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 5288 | record.getAudioAttributes()); |
| 5289 | if (DBG) Slog.v(TAG, "Delaying vibration by " + waitMs + "ms"); |
| 5290 | try { |
| 5291 | Thread.sleep(waitMs); |
| 5292 | } catch (InterruptedException e) { } |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 5293 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getPackageName(), |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 5294 | effect, "Notification (delayed)", record.getAudioAttributes()); |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 5295 | }).start(); |
| 5296 | } else { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 5297 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getPackageName(), |
Alexey Kuzmin | e1f06b8 | 2018-06-20 17:48:43 +0100 | [diff] [blame] | 5298 | effect, "Notification", record.getAudioAttributes()); |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 5299 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 5300 | return true; |
| 5301 | } finally{ |
| 5302 | Binder.restoreCallingIdentity(identity); |
| 5303 | } |
| 5304 | } |
| 5305 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5306 | private boolean isNotificationForCurrentUser(NotificationRecord record) { |
| 5307 | final int currentUser; |
| 5308 | final long token = Binder.clearCallingIdentity(); |
| 5309 | try { |
| 5310 | currentUser = ActivityManager.getCurrentUser(); |
| 5311 | } finally { |
| 5312 | Binder.restoreCallingIdentity(token); |
| 5313 | } |
| 5314 | return (record.getUserId() == UserHandle.USER_ALL || |
| 5315 | record.getUserId() == currentUser || |
| 5316 | mUserProfiles.isCurrentProfile(record.getUserId())); |
| 5317 | } |
| 5318 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 5319 | protected void playInCallNotification() { |
Beverly | 28c3d16 | 2018-06-28 11:37:53 -0400 | [diff] [blame] | 5320 | if (mAudioManager.getRingerModeInternal() == AudioManager.RINGER_MODE_NORMAL |
| 5321 | && Settings.Secure.getInt(getContext().getContentResolver(), |
| 5322 | Settings.Secure.IN_CALL_NOTIFICATION_ENABLED, 1) != 0) { |
| 5323 | new Thread() { |
| 5324 | @Override |
| 5325 | public void run() { |
| 5326 | final long identity = Binder.clearCallingIdentity(); |
| 5327 | try { |
| 5328 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 5329 | if (player != null) { |
| 5330 | if (mCallNotificationToken != null) { |
| 5331 | player.stop(mCallNotificationToken); |
| 5332 | } |
| 5333 | mCallNotificationToken = new Binder(); |
| 5334 | player.play(mCallNotificationToken, mInCallNotificationUri, |
| 5335 | mInCallNotificationAudioAttributes, |
| 5336 | mInCallNotificationVolume, false); |
luochaojiang | 50e5273c | 2018-04-16 16:55:03 +0800 | [diff] [blame] | 5337 | } |
Beverly | 28c3d16 | 2018-06-28 11:37:53 -0400 | [diff] [blame] | 5338 | } catch (RemoteException e) { |
| 5339 | } finally { |
| 5340 | Binder.restoreCallingIdentity(identity); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 5341 | } |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 5342 | } |
Beverly | 28c3d16 | 2018-06-28 11:37:53 -0400 | [diff] [blame] | 5343 | }.start(); |
| 5344 | } |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 5345 | } |
| 5346 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5347 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5348 | void showNextToastLocked() { |
| 5349 | ToastRecord record = mToastQueue.get(0); |
| 5350 | while (record != null) { |
| 5351 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| 5352 | try { |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5353 | record.callback.show(record.token); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5354 | scheduleDurationReachedLocked(record); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5355 | return; |
| 5356 | } catch (RemoteException e) { |
| 5357 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| 5358 | + " in package " + record.pkg); |
| 5359 | // remove it from the list and let the process die |
| 5360 | int index = mToastQueue.indexOf(record); |
| 5361 | if (index >= 0) { |
| 5362 | mToastQueue.remove(index); |
| 5363 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5364 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5365 | if (mToastQueue.size() > 0) { |
| 5366 | record = mToastQueue.get(0); |
| 5367 | } else { |
| 5368 | record = null; |
| 5369 | } |
| 5370 | } |
| 5371 | } |
| 5372 | } |
| 5373 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5374 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5375 | void cancelToastLocked(int index) { |
| 5376 | ToastRecord record = mToastQueue.get(index); |
| 5377 | try { |
| 5378 | record.callback.hide(); |
| 5379 | } catch (RemoteException e) { |
| 5380 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| 5381 | + " in package " + record.pkg); |
| 5382 | // don't worry about this, we're about to remove it from |
| 5383 | // the list anyway |
| 5384 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5385 | |
| 5386 | ToastRecord lastToast = mToastQueue.remove(index); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5387 | |
| 5388 | mWindowManagerInternal.removeWindowToken(lastToast.token, false /* removeWindows */, |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 5389 | lastToast.displayId); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5390 | // We passed 'false' for 'removeWindows' so that the client has time to stop |
| 5391 | // rendering (as hide above is a one-way message), otherwise we could crash |
| 5392 | // a client which was actively using a surface made from the token. However |
| 5393 | // we need to schedule a timeout to make sure the token is eventually killed |
| 5394 | // one way or another. |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 5395 | scheduleKillTokenTimeout(lastToast); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5396 | |
| 5397 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5398 | if (mToastQueue.size() > 0) { |
| 5399 | // Show the next one. If the callback fails, this will remove |
| 5400 | // it from the list, so don't assume that the list hasn't changed |
| 5401 | // after this point. |
| 5402 | showNextToastLocked(); |
| 5403 | } |
| 5404 | } |
| 5405 | |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 5406 | void finishTokenLocked(IBinder t, int displayId) { |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5407 | mHandler.removeCallbacksAndMessages(t); |
| 5408 | // We pass 'true' for 'removeWindows' to let the WindowManager destroy any |
| 5409 | // remaining surfaces as either the client has called finishToken indicating |
| 5410 | // it has successfully removed the views, or the client has timed out |
| 5411 | // at which point anything goes. |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 5412 | mWindowManagerInternal.removeWindowToken(t, true /* removeWindows */, displayId); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5413 | } |
| 5414 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5415 | @GuardedBy("mToastQueue") |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5416 | private void scheduleDurationReachedLocked(ToastRecord r) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5417 | { |
| 5418 | mHandler.removeCallbacksAndMessages(r); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5419 | Message m = Message.obtain(mHandler, MESSAGE_DURATION_REACHED, r); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5420 | long delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY; |
| 5421 | mHandler.sendMessageDelayed(m, delay); |
| 5422 | } |
| 5423 | |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5424 | private void handleDurationReached(ToastRecord record) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5425 | { |
| 5426 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| 5427 | synchronized (mToastQueue) { |
| 5428 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 5429 | if (index >= 0) { |
| 5430 | cancelToastLocked(index); |
| 5431 | } |
| 5432 | } |
| 5433 | } |
| 5434 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5435 | @GuardedBy("mToastQueue") |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 5436 | private void scheduleKillTokenTimeout(ToastRecord r) |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5437 | { |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 5438 | mHandler.removeCallbacksAndMessages(r); |
| 5439 | Message m = Message.obtain(mHandler, MESSAGE_FINISH_TOKEN_TIMEOUT, r); |
Robert Carr | 3406d46 | 2018-03-15 16:19:07 -0700 | [diff] [blame] | 5440 | mHandler.sendMessageDelayed(m, FINISH_TOKEN_TIMEOUT); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5441 | } |
| 5442 | |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 5443 | private void handleKillTokenTimeout(ToastRecord record) |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5444 | { |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 5445 | if (DBG) Slog.d(TAG, "Kill Token Timeout token=" + record.token); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5446 | synchronized (mToastQueue) { |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 5447 | finishTokenLocked(record.token, record.displayId); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5448 | } |
| 5449 | } |
| 5450 | |
| 5451 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5452 | int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 5453 | { |
| 5454 | IBinder cbak = callback.asBinder(); |
| 5455 | ArrayList<ToastRecord> list = mToastQueue; |
| 5456 | int len = list.size(); |
| 5457 | for (int i=0; i<len; i++) { |
| 5458 | ToastRecord r = list.get(i); |
Beverly Tai | a7ed0ab | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 5459 | if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5460 | return i; |
| 5461 | } |
| 5462 | } |
| 5463 | return -1; |
| 5464 | } |
| 5465 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5466 | @GuardedBy("mToastQueue") |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5467 | void keepProcessAliveIfNeededLocked(int pid) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5468 | { |
| 5469 | int toastCount = 0; // toasts from this pid |
| 5470 | ArrayList<ToastRecord> list = mToastQueue; |
| 5471 | int N = list.size(); |
| 5472 | for (int i=0; i<N; i++) { |
| 5473 | ToastRecord r = list.get(i); |
| 5474 | if (r.pid == pid) { |
| 5475 | toastCount++; |
| 5476 | } |
| 5477 | } |
| 5478 | try { |
Dianne Hackborn | f965f40 | 2017-05-04 23:27:23 -0700 | [diff] [blame] | 5479 | mAm.setProcessImportant(mForegroundToken, pid, toastCount > 0, "toast"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5480 | } catch (RemoteException e) { |
| 5481 | // Shouldn't happen. |
| 5482 | } |
| 5483 | } |
| 5484 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5485 | private void handleRankingReconsideration(Message message) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 5486 | if (!(message.obj instanceof RankingReconsideration)) return; |
| 5487 | RankingReconsideration recon = (RankingReconsideration) message.obj; |
| 5488 | recon.run(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5489 | boolean changed; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5490 | synchronized (mNotificationLock) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 5491 | final NotificationRecord record = mNotificationsByKey.get(recon.getKey()); |
| 5492 | if (record == null) { |
| 5493 | return; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5494 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5495 | int indexBefore = findNotificationRecordIndexLocked(record); |
| 5496 | boolean interceptBefore = record.isIntercepted(); |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 5497 | float contactAffinityBefore = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5498 | int visibilityBefore = record.getPackageVisibilityOverride(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 5499 | recon.applyChangesLocked(record); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5500 | applyZenModeLocked(record); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5501 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5502 | int indexAfter = findNotificationRecordIndexLocked(record); |
| 5503 | boolean interceptAfter = record.isIntercepted(); |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 5504 | float contactAffinityAfter = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5505 | int visibilityAfter = record.getPackageVisibilityOverride(); |
| 5506 | changed = indexBefore != indexAfter || interceptBefore != interceptAfter |
| 5507 | || visibilityBefore != visibilityAfter; |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 5508 | if (interceptBefore && !interceptAfter |
| 5509 | && Float.compare(contactAffinityBefore, contactAffinityAfter) != 0) { |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5510 | buzzBeepBlinkLocked(record); |
| 5511 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5512 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5513 | if (changed) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5514 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 5515 | } |
| 5516 | } |
| 5517 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5518 | void handleRankingSort() { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 5519 | if (mRankingHelper == null) return; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5520 | synchronized (mNotificationLock) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5521 | final int N = mNotificationList.size(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5522 | // Any field that can change via one of the extractors needs to be added here. |
| 5523 | ArrayList<String> orderBefore = new ArrayList<>(N); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5524 | int[] visibilities = new int[N]; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5525 | boolean[] showBadges = new boolean[N]; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5526 | ArrayList<NotificationChannel> channelBefore = new ArrayList<>(N); |
| 5527 | ArrayList<String> groupKeyBefore = new ArrayList<>(N); |
| 5528 | ArrayList<ArrayList<String>> overridePeopleBefore = new ArrayList<>(N); |
| 5529 | ArrayList<ArrayList<SnoozeCriterion>> snoozeCriteriaBefore = new ArrayList<>(N); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5530 | ArrayList<Integer> userSentimentBefore = new ArrayList<>(N); |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 5531 | ArrayList<Integer> suppressVisuallyBefore = new ArrayList<>(N); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 5532 | ArrayList<ArrayList<Notification.Action>> smartActionsBefore = new ArrayList<>(N); |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 5533 | ArrayList<ArrayList<CharSequence>> smartRepliesBefore = new ArrayList<>(N); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5534 | for (int i = 0; i < N; i++) { |
| 5535 | final NotificationRecord r = mNotificationList.get(i); |
| 5536 | orderBefore.add(r.getKey()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5537 | visibilities[i] = r.getPackageVisibilityOverride(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5538 | showBadges[i] = r.canShowBadge(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5539 | channelBefore.add(r.getChannel()); |
| 5540 | groupKeyBefore.add(r.getGroupKey()); |
| 5541 | overridePeopleBefore.add(r.getPeopleOverride()); |
| 5542 | snoozeCriteriaBefore.add(r.getSnoozeCriteria()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5543 | userSentimentBefore.add(r.getUserSentiment()); |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 5544 | suppressVisuallyBefore.add(r.getSuppressedVisualEffects()); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 5545 | smartActionsBefore.add(r.getSmartActions()); |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 5546 | smartRepliesBefore.add(r.getSmartReplies()); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5547 | mRankingHelper.extractSignals(r); |
| 5548 | } |
Chris Wren | 19a02b0 | 2015-12-22 10:34:22 -0500 | [diff] [blame] | 5549 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5550 | for (int i = 0; i < N; i++) { |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5551 | final NotificationRecord r = mNotificationList.get(i); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5552 | if (!orderBefore.get(i).equals(r.getKey()) |
Julia Reynolds | 6976669 | 2016-02-01 15:35:08 -0500 | [diff] [blame] | 5553 | || visibilities[i] != r.getPackageVisibilityOverride() |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5554 | || showBadges[i] != r.canShowBadge() |
| 5555 | || !Objects.equals(channelBefore.get(i), r.getChannel()) |
| 5556 | || !Objects.equals(groupKeyBefore.get(i), r.getGroupKey()) |
| 5557 | || !Objects.equals(overridePeopleBefore.get(i), r.getPeopleOverride()) |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5558 | || !Objects.equals(snoozeCriteriaBefore.get(i), r.getSnoozeCriteria()) |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 5559 | || !Objects.equals(userSentimentBefore.get(i), r.getUserSentiment()) |
| 5560 | || !Objects.equals(suppressVisuallyBefore.get(i), |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 5561 | r.getSuppressedVisualEffects()) |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 5562 | || !Objects.equals(smartActionsBefore.get(i), r.getSmartActions()) |
| 5563 | || !Objects.equals(smartRepliesBefore.get(i), r.getSmartReplies())) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5564 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5565 | return; |
| 5566 | } |
| 5567 | } |
| 5568 | } |
| 5569 | } |
| 5570 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5571 | @GuardedBy("mNotificationLock") |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 5572 | private void recordCallerLocked(NotificationRecord record) { |
| 5573 | if (mZenModeHelper.isCall(record)) { |
| 5574 | mZenModeHelper.recordCaller(record); |
| 5575 | } |
| 5576 | } |
| 5577 | |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 5578 | // let zen mode evaluate this record |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5579 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5580 | private void applyZenModeLocked(NotificationRecord record) { |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 5581 | record.setIntercepted(mZenModeHelper.shouldIntercept(record)); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 5582 | if (record.isIntercepted()) { |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 5583 | record.setSuppressedVisualEffects( |
Beverly | ff2df9b | 2018-10-10 16:54:10 -0400 | [diff] [blame] | 5584 | mZenModeHelper.getConsolidatedNotificationPolicy().suppressedVisualEffects); |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 5585 | } else { |
| 5586 | record.setSuppressedVisualEffects(0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 5587 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5588 | } |
| 5589 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5590 | @GuardedBy("mNotificationLock") |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 5591 | private int findNotificationRecordIndexLocked(NotificationRecord target) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5592 | return mRankingHelper.indexOf(mNotificationList, target); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5593 | } |
| 5594 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5595 | private void handleSendRankingUpdate() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5596 | synchronized (mNotificationLock) { |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 5597 | mListeners.notifyRankingUpdateLocked(null); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5598 | } |
| 5599 | } |
| 5600 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5601 | private void scheduleListenerHintsChanged(int state) { |
| 5602 | mHandler.removeMessages(MESSAGE_LISTENER_HINTS_CHANGED); |
| 5603 | mHandler.obtainMessage(MESSAGE_LISTENER_HINTS_CHANGED, state, 0).sendToTarget(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5604 | } |
| 5605 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5606 | private void scheduleInterruptionFilterChanged(int listenerInterruptionFilter) { |
| 5607 | mHandler.removeMessages(MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED); |
| 5608 | mHandler.obtainMessage( |
| 5609 | MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED, |
| 5610 | listenerInterruptionFilter, |
| 5611 | 0).sendToTarget(); |
| 5612 | } |
| 5613 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5614 | private void handleListenerHintsChanged(int hints) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5615 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5616 | mListeners.notifyListenerHintsChangedLocked(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5617 | } |
| 5618 | } |
| 5619 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5620 | private void handleListenerInterruptionFilterChanged(int interruptionFilter) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5621 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5622 | mListeners.notifyInterruptionFilterChanged(interruptionFilter); |
| 5623 | } |
| 5624 | } |
| 5625 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5626 | protected class WorkerHandler extends Handler |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5627 | { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5628 | public WorkerHandler(Looper looper) { |
| 5629 | super(looper); |
| 5630 | } |
| 5631 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5632 | @Override |
| 5633 | public void handleMessage(Message msg) |
| 5634 | { |
| 5635 | switch (msg.what) |
| 5636 | { |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5637 | case MESSAGE_DURATION_REACHED: |
| 5638 | handleDurationReached((ToastRecord)msg.obj); |
| 5639 | break; |
| 5640 | case MESSAGE_FINISH_TOKEN_TIMEOUT: |
Jeff Chang | 48ecef4 | 2018-08-09 16:31:59 +0800 | [diff] [blame] | 5641 | handleKillTokenTimeout((ToastRecord)msg.obj); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5642 | break; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 5643 | case MESSAGE_SAVE_POLICY_FILE: |
| 5644 | handleSavePolicyFile(); |
| 5645 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5646 | case MESSAGE_SEND_RANKING_UPDATE: |
| 5647 | handleSendRankingUpdate(); |
| 5648 | break; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5649 | case MESSAGE_LISTENER_HINTS_CHANGED: |
| 5650 | handleListenerHintsChanged(msg.arg1); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5651 | break; |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5652 | case MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED: |
| 5653 | handleListenerInterruptionFilterChanged(msg.arg1); |
| 5654 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5655 | } |
| 5656 | } |
| 5657 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5658 | protected void scheduleSendRankingUpdate() { |
| 5659 | if (!hasMessages(MESSAGE_SEND_RANKING_UPDATE)) { |
| 5660 | Message m = Message.obtain(this, MESSAGE_SEND_RANKING_UPDATE); |
| 5661 | sendMessage(m); |
| 5662 | } |
| 5663 | } |
| 5664 | |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 5665 | protected void scheduleCancelNotification(CancelNotificationRunnable cancelRunnable) { |
| 5666 | if (!hasCallbacks(cancelRunnable)) { |
| 5667 | sendMessage(Message.obtain(this, cancelRunnable)); |
| 5668 | } |
| 5669 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5670 | } |
| 5671 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5672 | private final class RankingHandlerWorker extends Handler implements RankingHandler |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5673 | { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5674 | public RankingHandlerWorker(Looper looper) { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5675 | super(looper); |
| 5676 | } |
| 5677 | |
| 5678 | @Override |
| 5679 | public void handleMessage(Message msg) { |
| 5680 | switch (msg.what) { |
| 5681 | case MESSAGE_RECONSIDER_RANKING: |
| 5682 | handleRankingReconsideration(msg); |
| 5683 | break; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5684 | case MESSAGE_RANKING_SORT: |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5685 | handleRankingSort(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5686 | break; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5687 | } |
| 5688 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5689 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5690 | public void requestSort() { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5691 | removeMessages(MESSAGE_RANKING_SORT); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5692 | Message msg = Message.obtain(); |
| 5693 | msg.what = MESSAGE_RANKING_SORT; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5694 | sendMessage(msg); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5695 | } |
| 5696 | |
| 5697 | public void requestReconsideration(RankingReconsideration recon) { |
| 5698 | Message m = Message.obtain(this, |
| 5699 | NotificationManagerService.MESSAGE_RECONSIDER_RANKING, recon); |
| 5700 | long delay = recon.getDelay(TimeUnit.MILLISECONDS); |
| 5701 | sendMessageDelayed(m, delay); |
| 5702 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5703 | } |
| 5704 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5705 | // Notifications |
| 5706 | // ============================================================================ |
| 5707 | static int clamp(int x, int low, int high) { |
| 5708 | return (x < low) ? low : ((x > high) ? high : x); |
| 5709 | } |
| 5710 | |
| 5711 | void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
Eugene Susla | d4128ec | 2017-12-04 19:48:41 +0000 | [diff] [blame] | 5712 | if (!mAccessibilityManager.isEnabled()) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 5713 | return; |
| 5714 | } |
| 5715 | |
| 5716 | AccessibilityEvent event = |
| 5717 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 5718 | event.setPackageName(packageName); |
| 5719 | event.setClassName(Notification.class.getName()); |
| 5720 | event.setParcelableData(notification); |
| 5721 | CharSequence tickerText = notification.tickerText; |
| 5722 | if (!TextUtils.isEmpty(tickerText)) { |
| 5723 | event.getText().add(tickerText); |
| 5724 | } |
| 5725 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 5726 | mAccessibilityManager.sendAccessibilityEvent(event); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 5727 | } |
| 5728 | |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5729 | /** |
| 5730 | * Removes all NotificationsRecords with the same key as the given notification record |
| 5731 | * from both lists. Do not call this method while iterating over either list. |
| 5732 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5733 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5734 | private boolean removeFromNotificationListsLocked(NotificationRecord r) { |
| 5735 | // Remove from both lists, either list could have a separate Record for what is |
| 5736 | // effectively the same notification. |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5737 | boolean wasPosted = false; |
| 5738 | NotificationRecord recordInList = null; |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5739 | if ((recordInList = findNotificationByListLocked(mNotificationList, r.getKey())) |
| 5740 | != null) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5741 | mNotificationList.remove(recordInList); |
| 5742 | mNotificationsByKey.remove(recordInList.sbn.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5743 | wasPosted = true; |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5744 | } |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5745 | while ((recordInList = findNotificationByListLocked(mEnqueuedNotifications, r.getKey())) |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5746 | != null) { |
| 5747 | mEnqueuedNotifications.remove(recordInList); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5748 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5749 | return wasPosted; |
| 5750 | } |
| 5751 | |
| 5752 | @GuardedBy("mNotificationLock") |
| 5753 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason, |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5754 | boolean wasPosted, String listenerName) { |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5755 | cancelNotificationLocked(r, sendDelete, reason, -1, -1, wasPosted, listenerName); |
| 5756 | } |
| 5757 | |
| 5758 | @GuardedBy("mNotificationLock") |
| 5759 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason, |
| 5760 | int rank, int count, boolean wasPosted, String listenerName) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5761 | final String canceledKey = r.getKey(); |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 5762 | |
| 5763 | // Record caller. |
| 5764 | recordCallerLocked(r); |
| 5765 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5766 | if (r.getStats().getDismissalSurface() == NotificationStats.DISMISSAL_NOT_DISMISSED) { |
| 5767 | r.recordDismissalSurface(NotificationStats.DISMISSAL_OTHER); |
| 5768 | } |
| 5769 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 5770 | // tell the app |
| 5771 | if (sendDelete) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 5772 | if (r.getNotification().deleteIntent != null) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 5773 | try { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 5774 | r.getNotification().deleteIntent.send(); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 5775 | } catch (PendingIntent.CanceledException ex) { |
| 5776 | // do nothing - there's no relevant way to recover, and |
| 5777 | // no reason to let this propagate |
Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 5778 | Slog.w(TAG, "canceled PendingIntent for " + r.sbn.getPackageName(), ex); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 5779 | } |
| 5780 | } |
| 5781 | } |
| 5782 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5783 | // Only cancel these if this notification actually got to be posted. |
| 5784 | if (wasPosted) { |
| 5785 | // status bar |
| 5786 | if (r.getNotification().getSmallIcon() != null) { |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 5787 | if (reason != REASON_SNOOZED) { |
| 5788 | r.isCanceled = true; |
| 5789 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 5790 | mListeners.notifyRemovedLocked(r, reason, r.getStats()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5791 | mHandler.post(new Runnable() { |
| 5792 | @Override |
| 5793 | public void run() { |
| 5794 | mGroupHelper.onNotificationRemoved(r.sbn); |
| 5795 | } |
| 5796 | }); |
| 5797 | } |
| 5798 | |
| 5799 | // sound |
| 5800 | if (canceledKey.equals(mSoundNotificationKey)) { |
| 5801 | mSoundNotificationKey = null; |
| 5802 | final long identity = Binder.clearCallingIdentity(); |
| 5803 | try { |
| 5804 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 5805 | if (player != null) { |
| 5806 | player.stopAsync(); |
| 5807 | } |
| 5808 | } catch (RemoteException e) { |
| 5809 | } finally { |
| 5810 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 5811 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5812 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5813 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5814 | // vibrate |
| 5815 | if (canceledKey.equals(mVibrateNotificationKey)) { |
| 5816 | mVibrateNotificationKey = null; |
| 5817 | long identity = Binder.clearCallingIdentity(); |
| 5818 | try { |
| 5819 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 5820 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5821 | finally { |
| 5822 | Binder.restoreCallingIdentity(identity); |
| 5823 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5824 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5825 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5826 | // light |
| 5827 | mLights.remove(canceledKey); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5828 | } |
| 5829 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5830 | // Record usage stats |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5831 | // TODO: add unbundling stats? |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5832 | switch (reason) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 5833 | case REASON_CANCEL: |
| 5834 | case REASON_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5835 | case REASON_LISTENER_CANCEL: |
| 5836 | case REASON_LISTENER_CANCEL_ALL: |
| 5837 | mUsageStats.registerDismissedByUser(r); |
| 5838 | break; |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 5839 | case REASON_APP_CANCEL: |
| 5840 | case REASON_APP_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5841 | mUsageStats.registerRemovedByApp(r); |
| 5842 | break; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5843 | } |
| 5844 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5845 | String groupKey = r.getGroupKey(); |
| 5846 | NotificationRecord groupSummary = mSummaryByGroupKey.get(groupKey); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5847 | if (groupSummary != null && groupSummary.getKey().equals(canceledKey)) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5848 | mSummaryByGroupKey.remove(groupKey); |
| 5849 | } |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 5850 | final ArrayMap<String, String> summaries = mAutobundledSummaries.get(r.sbn.getUserId()); |
| 5851 | if (summaries != null && r.sbn.getKey().equals(summaries.get(r.sbn.getPackageName()))) { |
| 5852 | summaries.remove(r.sbn.getPackageName()); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5853 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5854 | |
Daniel Sandler | 23d7c70 | 2013-03-07 16:32:06 -0500 | [diff] [blame] | 5855 | // Save it for users of getHistoricalNotifications() |
| 5856 | mArchive.record(r.sbn); |
Christoph Studer | 81e5b5f | 2014-10-22 17:19:56 +0200 | [diff] [blame] | 5857 | |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 5858 | final long now = System.currentTimeMillis(); |
Julia Reynolds | 3dfdde0 | 2018-10-08 09:17:56 -0400 | [diff] [blame] | 5859 | final LogMaker logMaker = r.getItemLogMaker() |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 5860 | .setType(MetricsEvent.TYPE_DISMISS) |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5861 | .setSubtype(reason); |
| 5862 | if (rank != -1 && count != -1) { |
| 5863 | logMaker.addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, rank) |
| 5864 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, count); |
| 5865 | } |
| 5866 | MetricsLogger.action(logMaker); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 5867 | EventLogTags.writeNotificationCanceled(canceledKey, reason, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5868 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now), |
| 5869 | rank, count, listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5870 | } |
| 5871 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 5872 | @VisibleForTesting |
| 5873 | void updateUriPermissions(@Nullable NotificationRecord newRecord, |
| 5874 | @Nullable NotificationRecord oldRecord, String targetPkg, int targetUserId) { |
| 5875 | final String key = (newRecord != null) ? newRecord.getKey() : oldRecord.getKey(); |
| 5876 | if (DBG) Slog.d(TAG, key + ": updating permissions"); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 5877 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 5878 | final ArraySet<Uri> newUris = (newRecord != null) ? newRecord.getGrantableUris() : null; |
| 5879 | final ArraySet<Uri> oldUris = (oldRecord != null) ? oldRecord.getGrantableUris() : null; |
| 5880 | |
| 5881 | // Shortcut when no Uris involved |
| 5882 | if (newUris == null && oldUris == null) { |
| 5883 | return; |
| 5884 | } |
| 5885 | |
| 5886 | // Inherit any existing owner |
| 5887 | IBinder permissionOwner = null; |
| 5888 | if (newRecord != null && permissionOwner == null) { |
| 5889 | permissionOwner = newRecord.permissionOwner; |
| 5890 | } |
| 5891 | if (oldRecord != null && permissionOwner == null) { |
| 5892 | permissionOwner = oldRecord.permissionOwner; |
| 5893 | } |
| 5894 | |
| 5895 | // If we have Uris to grant, but no owner yet, go create one |
| 5896 | if (newUris != null && permissionOwner == null) { |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 5897 | if (DBG) Slog.d(TAG, key + ": creating owner"); |
| 5898 | permissionOwner = mUgmInternal.newUriPermissionOwner("NOTIF:" + key); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 5899 | } |
| 5900 | |
| 5901 | // If we have no Uris to grant, but an existing owner, go destroy it |
| 5902 | if (newUris == null && permissionOwner != null) { |
| 5903 | final long ident = Binder.clearCallingIdentity(); |
| 5904 | try { |
| 5905 | if (DBG) Slog.d(TAG, key + ": destroying owner"); |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 5906 | mUgmInternal.revokeUriPermissionFromOwner(permissionOwner, null, ~0, |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 5907 | UserHandle.getUserId(oldRecord.getUid())); |
| 5908 | permissionOwner = null; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 5909 | } finally { |
| 5910 | Binder.restoreCallingIdentity(ident); |
| 5911 | } |
| 5912 | } |
| 5913 | |
| 5914 | // Grant access to new Uris |
| 5915 | if (newUris != null && permissionOwner != null) { |
| 5916 | for (int i = 0; i < newUris.size(); i++) { |
| 5917 | final Uri uri = newUris.valueAt(i); |
| 5918 | if (oldUris == null || !oldUris.contains(uri)) { |
| 5919 | if (DBG) Slog.d(TAG, key + ": granting " + uri); |
| 5920 | grantUriPermission(permissionOwner, uri, newRecord.getUid(), targetPkg, |
| 5921 | targetUserId); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 5922 | } |
| 5923 | } |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 5924 | } |
| 5925 | |
| 5926 | // Revoke access to old Uris |
| 5927 | if (oldUris != null && permissionOwner != null) { |
| 5928 | for (int i = 0; i < oldUris.size(); i++) { |
| 5929 | final Uri uri = oldUris.valueAt(i); |
| 5930 | if (newUris == null || !newUris.contains(uri)) { |
| 5931 | if (DBG) Slog.d(TAG, key + ": revoking " + uri); |
| 5932 | revokeUriPermission(permissionOwner, uri, oldRecord.getUid()); |
| 5933 | } |
| 5934 | } |
| 5935 | } |
| 5936 | |
| 5937 | if (newRecord != null) { |
| 5938 | newRecord.permissionOwner = permissionOwner; |
| 5939 | } |
| 5940 | } |
| 5941 | |
| 5942 | private void grantUriPermission(IBinder owner, Uri uri, int sourceUid, String targetPkg, |
| 5943 | int targetUserId) { |
| 5944 | if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return; |
| 5945 | |
| 5946 | final long ident = Binder.clearCallingIdentity(); |
| 5947 | try { |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 5948 | mUgm.grantUriPermissionFromOwner(owner, sourceUid, targetPkg, |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 5949 | ContentProvider.getUriWithoutUserId(uri), |
| 5950 | Intent.FLAG_GRANT_READ_URI_PERMISSION, |
| 5951 | ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid)), |
| 5952 | targetUserId); |
| 5953 | } catch (RemoteException ignored) { |
| 5954 | // Ignored because we're in same process |
| 5955 | } finally { |
| 5956 | Binder.restoreCallingIdentity(ident); |
| 5957 | } |
| 5958 | } |
| 5959 | |
| 5960 | private void revokeUriPermission(IBinder owner, Uri uri, int sourceUid) { |
| 5961 | if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return; |
| 5962 | |
| 5963 | final long ident = Binder.clearCallingIdentity(); |
| 5964 | try { |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 5965 | mUgmInternal.revokeUriPermissionFromOwner( |
| 5966 | owner, |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 5967 | ContentProvider.getUriWithoutUserId(uri), |
| 5968 | Intent.FLAG_GRANT_READ_URI_PERMISSION, |
| 5969 | ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid))); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 5970 | } finally { |
| 5971 | Binder.restoreCallingIdentity(ident); |
| 5972 | } |
| 5973 | } |
| 5974 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5975 | /** |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 5976 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5977 | * and none of the {@code mustNotHaveFlags}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5978 | */ |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 5979 | void cancelNotification(final int callingUid, final int callingPid, |
| 5980 | final String pkg, final String tag, final int id, |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5981 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5982 | final int userId, final int reason, final ManagedServiceInfo listener) { |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5983 | cancelNotification(callingUid, callingPid, pkg, tag, id, mustHaveFlags, mustNotHaveFlags, |
| 5984 | sendDelete, userId, reason, -1 /* rank */, -1 /* count */, listener); |
| 5985 | } |
| 5986 | |
| 5987 | /** |
| 5988 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
| 5989 | * and none of the {@code mustNotHaveFlags}. |
| 5990 | */ |
| 5991 | void cancelNotification(final int callingUid, final int callingPid, |
| 5992 | final String pkg, final String tag, final int id, |
| 5993 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 5994 | final int userId, final int reason, int rank, int count, |
| 5995 | final ManagedServiceInfo listener) { |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5996 | // In enqueueNotificationInternal notifications are added by scheduling the |
| 5997 | // work on the worker handler. Hence, we also schedule the cancel on this |
| 5998 | // handler to avoid a scenario where an add notification call followed by a |
| 5999 | // remove notification call ends up in not removing the notification. |
Julia Reynolds | efcdff4 | 2018-08-09 09:42:56 -0400 | [diff] [blame] | 6000 | mHandler.scheduleCancelNotification(new CancelNotificationRunnable(callingUid, callingPid, |
| 6001 | pkg, tag, id, mustHaveFlags, mustNotHaveFlags, sendDelete, userId, reason, rank, |
| 6002 | count, listener)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6003 | } |
| 6004 | |
| 6005 | /** |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 6006 | * Determine whether the userId applies to the notification in question, either because |
| 6007 | * they match exactly, or one of them is USER_ALL (which is treated as a wildcard). |
| 6008 | */ |
| 6009 | private boolean notificationMatchesUserId(NotificationRecord r, int userId) { |
| 6010 | return |
| 6011 | // looking for USER_ALL notifications? match everything |
| 6012 | userId == UserHandle.USER_ALL |
| 6013 | // a notification sent to USER_ALL matches any query |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 6014 | || r.getUserId() == UserHandle.USER_ALL |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 6015 | // an exact user match |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 6016 | || r.getUserId() == userId; |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 6017 | } |
| 6018 | |
| 6019 | /** |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6020 | * Determine whether the userId applies to the notification in question, either because |
| 6021 | * 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] | 6022 | * because it matches one of the users profiles. |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6023 | */ |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 6024 | private boolean notificationMatchesCurrentProfiles(NotificationRecord r, int userId) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6025 | return notificationMatchesUserId(r, userId) |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 6026 | || mUserProfiles.isCurrentProfile(r.getUserId()); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6027 | } |
| 6028 | |
| 6029 | /** |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 6030 | * 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] | 6031 | * {@code mustHaveFlags}. |
| 6032 | */ |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6033 | void cancelAllNotificationsInt(int callingUid, int callingPid, String pkg, String channelId, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 6034 | int mustHaveFlags, int mustNotHaveFlags, boolean doit, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6035 | ManagedServiceInfo listener) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6036 | mHandler.post(new Runnable() { |
| 6037 | @Override |
| 6038 | public void run() { |
| 6039 | String listenerName = listener == null ? null : listener.component.toShortString(); |
| 6040 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 6041 | pkg, userId, mustHaveFlags, mustNotHaveFlags, reason, |
| 6042 | listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6043 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6044 | // Why does this parameter exist? Do we actually want to execute the above if doit |
| 6045 | // is false? |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 6046 | if (!doit) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6047 | return; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 6048 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6049 | |
| 6050 | synchronized (mNotificationLock) { |
| 6051 | FlagChecker flagChecker = (int flags) -> { |
| 6052 | if ((flags & mustHaveFlags) != mustHaveFlags) { |
| 6053 | return false; |
| 6054 | } |
| 6055 | if ((flags & mustNotHaveFlags) != 0) { |
| 6056 | return false; |
| 6057 | } |
| 6058 | return true; |
| 6059 | }; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6060 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 6061 | pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, flagChecker, |
| 6062 | false /*includeCurrentProfiles*/, userId, false /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6063 | listenerName, true /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6064 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 6065 | callingPid, pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, |
| 6066 | flagChecker, false /*includeCurrentProfiles*/, userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6067 | false /*sendDelete*/, reason, listenerName, false /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6068 | mSnoozeHelper.cancel(userId, pkg); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 6069 | } |
| 6070 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6071 | }); |
| 6072 | } |
| 6073 | |
| 6074 | private interface FlagChecker { |
| 6075 | // Returns false if these flags do not pass the defined flag test. |
| 6076 | public boolean apply(int flags); |
| 6077 | } |
| 6078 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6079 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6080 | private void cancelAllNotificationsByListLocked(ArrayList<NotificationRecord> notificationList, |
| 6081 | int callingUid, int callingPid, String pkg, boolean nullPkgIndicatesUserSwitch, |
| 6082 | String channelId, FlagChecker flagChecker, boolean includeCurrentProfiles, int userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6083 | boolean sendDelete, int reason, String listenerName, boolean wasPosted) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6084 | ArrayList<NotificationRecord> canceledNotifications = null; |
| 6085 | for (int i = notificationList.size() - 1; i >= 0; --i) { |
| 6086 | NotificationRecord r = notificationList.get(i); |
| 6087 | if (includeCurrentProfiles) { |
| 6088 | if (!notificationMatchesCurrentProfiles(r, userId)) { |
| 6089 | continue; |
| 6090 | } |
| 6091 | } else if (!notificationMatchesUserId(r, userId)) { |
| 6092 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6093 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6094 | // Don't remove notifications to all, if there's no package name specified |
| 6095 | if (nullPkgIndicatesUserSwitch && pkg == null && r.getUserId() == UserHandle.USER_ALL) { |
| 6096 | continue; |
| 6097 | } |
| 6098 | if (!flagChecker.apply(r.getFlags())) { |
| 6099 | continue; |
| 6100 | } |
| 6101 | if (pkg != null && !r.sbn.getPackageName().equals(pkg)) { |
| 6102 | continue; |
| 6103 | } |
| 6104 | if (channelId != null && !channelId.equals(r.getChannel().getId())) { |
| 6105 | continue; |
| 6106 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6107 | if (canceledNotifications == null) { |
| 6108 | canceledNotifications = new ArrayList<>(); |
| 6109 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6110 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 6111 | mNotificationsByKey.remove(r.getKey()); |
Julia Reynolds | fd4099d | 2018-08-21 11:06:06 -0400 | [diff] [blame] | 6112 | r.recordDismissalSentiment(NotificationStats.DISMISS_SENTIMENT_NEUTRAL); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6113 | canceledNotifications.add(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 6114 | cancelNotificationLocked(r, sendDelete, reason, wasPosted, listenerName); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6115 | } |
| 6116 | if (canceledNotifications != null) { |
| 6117 | final int M = canceledNotifications.size(); |
| 6118 | for (int i = 0; i < M; i++) { |
| 6119 | cancelGroupChildrenLocked(canceledNotifications.get(i), callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 6120 | listenerName, false /* sendDelete */, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6121 | } |
| 6122 | updateLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6123 | } |
| 6124 | } |
| 6125 | |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 6126 | void snoozeNotificationInt(String key, long duration, String snoozeCriterionId, |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6127 | ManagedServiceInfo listener) { |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6128 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 6129 | if (duration <= 0 && snoozeCriterionId == null || key == null) { |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 6130 | return; |
| 6131 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 6132 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6133 | if (DBG) { |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 6134 | Slog.d(TAG, String.format("snooze event(%s, %d, %s, %s)", key, duration, |
| 6135 | snoozeCriterionId, listenerName)); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6136 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6137 | // Needs to post so that it can cancel notifications not yet enqueued. |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 6138 | mHandler.post(new SnoozeNotificationRunnable(key, duration, snoozeCriterionId)); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 6139 | } |
| 6140 | |
| 6141 | void unsnoozeNotificationInt(String key, ManagedServiceInfo listener) { |
| 6142 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 6143 | if (DBG) { |
| 6144 | Slog.d(TAG, String.format("unsnooze event(%s, %s)", key, listenerName)); |
| 6145 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6146 | mSnoozeHelper.repost(key); |
| 6147 | savePolicyFile(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 6148 | } |
| 6149 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6150 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 350159c | 2014-03-27 11:15:11 -0700 | [diff] [blame] | 6151 | void cancelAllLocked(int callingUid, int callingPid, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6152 | ManagedServiceInfo listener, boolean includeCurrentProfiles) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6153 | mHandler.post(new Runnable() { |
| 6154 | @Override |
| 6155 | public void run() { |
| 6156 | synchronized (mNotificationLock) { |
| 6157 | String listenerName = |
| 6158 | listener == null ? null : listener.component.toShortString(); |
| 6159 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 6160 | null, userId, 0, 0, reason, listenerName); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 6161 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6162 | FlagChecker flagChecker = (int flags) -> { |
| 6163 | if ((flags & (Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR)) |
| 6164 | != 0) { |
| 6165 | return false; |
| 6166 | } |
| 6167 | return true; |
| 6168 | }; |
| 6169 | |
| 6170 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 6171 | null, false /*nullPkgIndicatesUserSwitch*/, null, flagChecker, |
| 6172 | includeCurrentProfiles, userId, true /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6173 | listenerName, true); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6174 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 6175 | callingPid, null, false /*nullPkgIndicatesUserSwitch*/, null, |
| 6176 | flagChecker, includeCurrentProfiles, userId, true /*sendDelete*/, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6177 | reason, listenerName, false); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6178 | mSnoozeHelper.cancel(userId, includeCurrentProfiles); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6179 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6180 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6181 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6182 | } |
| 6183 | |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 6184 | // Warning: The caller is responsible for invoking updateLightsLocked(). |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6185 | @GuardedBy("mNotificationLock") |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 6186 | private void cancelGroupChildrenLocked(NotificationRecord r, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 6187 | String listenerName, boolean sendDelete, FlagChecker flagChecker) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 6188 | Notification n = r.getNotification(); |
Christoph Studer | 3f31f5d | 2014-07-31 16:55:32 +0200 | [diff] [blame] | 6189 | if (!n.isGroupSummary()) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 6190 | return; |
| 6191 | } |
| 6192 | |
| 6193 | String pkg = r.sbn.getPackageName(); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 6194 | |
| 6195 | if (pkg == null) { |
| 6196 | if (DBG) Log.e(TAG, "No package for group summary: " + r.getKey()); |
| 6197 | return; |
| 6198 | } |
| 6199 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6200 | cancelGroupChildrenByListLocked(mNotificationList, r, callingUid, callingPid, listenerName, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 6201 | sendDelete, true, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6202 | cancelGroupChildrenByListLocked(mEnqueuedNotifications, r, callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 6203 | listenerName, sendDelete, false, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6204 | } |
| 6205 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6206 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6207 | private void cancelGroupChildrenByListLocked(ArrayList<NotificationRecord> notificationList, |
| 6208 | NotificationRecord parentNotification, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 6209 | String listenerName, boolean sendDelete, boolean wasPosted, FlagChecker flagChecker) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6210 | final String pkg = parentNotification.sbn.getPackageName(); |
| 6211 | final int userId = parentNotification.getUserId(); |
| 6212 | final int reason = REASON_GROUP_SUMMARY_CANCELED; |
| 6213 | for (int i = notificationList.size() - 1; i >= 0; i--) { |
| 6214 | final NotificationRecord childR = notificationList.get(i); |
| 6215 | final StatusBarNotification childSbn = childR.sbn; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 6216 | if ((childSbn.isGroup() && !childSbn.getNotification().isGroupSummary()) && |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6217 | childR.getGroupKey().equals(parentNotification.getGroupKey()) |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 6218 | && (childR.getFlags() & FLAG_FOREGROUND_SERVICE) == 0 |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 6219 | && (flagChecker == null || flagChecker.apply(childR.getFlags()))) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 6220 | EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, childSbn.getId(), |
| 6221 | childSbn.getTag(), userId, 0, 0, reason, listenerName); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 6222 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 6223 | mNotificationsByKey.remove(childR.getKey()); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 6224 | cancelNotificationLocked(childR, sendDelete, reason, wasPosted, listenerName); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 6225 | } |
| 6226 | } |
| 6227 | } |
| 6228 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6229 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 6230 | void updateLightsLocked() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6231 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 6232 | // handle notification lights |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 6233 | NotificationRecord ledNotification = null; |
| 6234 | while (ledNotification == null && !mLights.isEmpty()) { |
| 6235 | final String owner = mLights.get(mLights.size() - 1); |
| 6236 | ledNotification = mNotificationsByKey.get(owner); |
| 6237 | if (ledNotification == null) { |
| 6238 | Slog.wtfStack(TAG, "LED Notification does not exist: " + owner); |
| 6239 | mLights.remove(owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6240 | } |
| 6241 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 6242 | |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 6243 | // 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] | 6244 | if (ledNotification == null || mInCall || mScreenOn) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 6245 | mNotificationLight.turnOff(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 6246 | } else { |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 6247 | NotificationRecord.Light light = ledNotification.getLight(); |
| 6248 | if (light != null && mNotificationPulseEnabled) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 6249 | // pulse repeatedly |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 6250 | mNotificationLight.setFlashing(light.color, Light.LIGHT_FLASH_TIMED, |
| 6251 | light.onMs, light.offMs); |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 6252 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 6253 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6254 | } |
| 6255 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6256 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 6257 | @NonNull List<NotificationRecord> findGroupNotificationsLocked(String pkg, |
| 6258 | String groupKey, int userId) { |
| 6259 | List<NotificationRecord> records = new ArrayList<>(); |
| 6260 | records.addAll(findGroupNotificationByListLocked(mNotificationList, pkg, groupKey, userId)); |
| 6261 | records.addAll( |
| 6262 | findGroupNotificationByListLocked(mEnqueuedNotifications, pkg, groupKey, userId)); |
| 6263 | return records; |
| 6264 | } |
| 6265 | |
| 6266 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6267 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 6268 | private @NonNull List<NotificationRecord> findGroupNotificationByListLocked( |
| 6269 | ArrayList<NotificationRecord> list, String pkg, String groupKey, int userId) { |
| 6270 | List<NotificationRecord> records = new ArrayList<>(); |
| 6271 | final int len = list.size(); |
| 6272 | for (int i = 0; i < len; i++) { |
| 6273 | NotificationRecord r = list.get(i); |
| 6274 | if (notificationMatchesUserId(r, userId) && r.getGroupKey().equals(groupKey) |
| 6275 | && r.sbn.getPackageName().equals(pkg)) { |
| 6276 | records.add(r); |
| 6277 | } |
| 6278 | } |
| 6279 | return records; |
| 6280 | } |
| 6281 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6282 | // Searches both enqueued and posted notifications by key. |
| 6283 | // TODO: need to combine a bunch of these getters with slightly different behavior. |
| 6284 | // TODO: Should enqueuing just add to mNotificationsByKey instead? |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6285 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6286 | private NotificationRecord findNotificationByKeyLocked(String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6287 | NotificationRecord r; |
| 6288 | if ((r = findNotificationByListLocked(mNotificationList, key)) != null) { |
| 6289 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6290 | } |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6291 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, key)) != null) { |
| 6292 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6293 | } |
| 6294 | return null; |
| 6295 | } |
| 6296 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6297 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 6298 | NotificationRecord findNotificationLocked(String pkg, String tag, int id, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6299 | NotificationRecord r; |
| 6300 | if ((r = findNotificationByListLocked(mNotificationList, pkg, tag, id, userId)) != null) { |
| 6301 | return r; |
| 6302 | } |
| 6303 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, pkg, tag, id, userId)) |
| 6304 | != null) { |
| 6305 | return r; |
| 6306 | } |
| 6307 | return null; |
| 6308 | } |
| 6309 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6310 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6311 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6312 | String pkg, String tag, int id, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6313 | final int len = list.size(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6314 | for (int i = 0; i < len; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6315 | NotificationRecord r = list.get(i); |
Vladimir Marko | 2526f33 | 2013-09-11 11:13:55 +0100 | [diff] [blame] | 6316 | if (notificationMatchesUserId(r, userId) && r.sbn.getId() == id && |
| 6317 | TextUtils.equals(r.sbn.getTag(), tag) && r.sbn.getPackageName().equals(pkg)) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6318 | return r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6319 | } |
| 6320 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6321 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6322 | } |
| 6323 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6324 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 564273f | 2018-09-13 15:53:11 -0400 | [diff] [blame] | 6325 | private List<NotificationRecord> findNotificationsByListLocked( |
| 6326 | ArrayList<NotificationRecord> list, String pkg, String tag, int id, int userId) { |
| 6327 | List<NotificationRecord> matching = new ArrayList<>(); |
| 6328 | final int len = list.size(); |
| 6329 | for (int i = 0; i < len; i++) { |
| 6330 | NotificationRecord r = list.get(i); |
| 6331 | if (notificationMatchesUserId(r, userId) && r.sbn.getId() == id && |
| 6332 | TextUtils.equals(r.sbn.getTag(), tag) && r.sbn.getPackageName().equals(pkg)) { |
| 6333 | matching.add(r); |
| 6334 | } |
| 6335 | } |
| 6336 | return matching; |
| 6337 | } |
| 6338 | |
| 6339 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6340 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6341 | String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6342 | final int N = list.size(); |
| 6343 | for (int i = 0; i < N; i++) { |
| 6344 | if (key.equals(list.get(i).getKey())) { |
| 6345 | return list.get(i); |
| 6346 | } |
| 6347 | } |
| 6348 | return null; |
| 6349 | } |
| 6350 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6351 | @GuardedBy("mNotificationLock") |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 6352 | int indexOfNotificationLocked(String key) { |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 6353 | final int N = mNotificationList.size(); |
| 6354 | for (int i = 0; i < N; i++) { |
| 6355 | if (key.equals(mNotificationList.get(i).getKey())) { |
| 6356 | return i; |
| 6357 | } |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 6358 | } |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 6359 | return -1; |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 6360 | } |
| 6361 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6362 | @VisibleForTesting |
| 6363 | protected void hideNotificationsForPackages(String[] pkgs) { |
| 6364 | synchronized (mNotificationLock) { |
| 6365 | List<String> pkgList = Arrays.asList(pkgs); |
| 6366 | List<NotificationRecord> changedNotifications = new ArrayList<>(); |
| 6367 | int numNotifications = mNotificationList.size(); |
| 6368 | for (int i = 0; i < numNotifications; i++) { |
| 6369 | NotificationRecord rec = mNotificationList.get(i); |
| 6370 | if (pkgList.contains(rec.sbn.getPackageName())) { |
| 6371 | rec.setHidden(true); |
| 6372 | changedNotifications.add(rec); |
| 6373 | } |
| 6374 | } |
| 6375 | |
| 6376 | mListeners.notifyHiddenLocked(changedNotifications); |
| 6377 | } |
| 6378 | } |
| 6379 | |
| 6380 | @VisibleForTesting |
| 6381 | protected void unhideNotificationsForPackages(String[] pkgs) { |
| 6382 | synchronized (mNotificationLock) { |
| 6383 | List<String> pkgList = Arrays.asList(pkgs); |
| 6384 | List<NotificationRecord> changedNotifications = new ArrayList<>(); |
| 6385 | int numNotifications = mNotificationList.size(); |
| 6386 | for (int i = 0; i < numNotifications; i++) { |
| 6387 | NotificationRecord rec = mNotificationList.get(i); |
| 6388 | if (pkgList.contains(rec.sbn.getPackageName())) { |
| 6389 | rec.setHidden(false); |
| 6390 | changedNotifications.add(rec); |
| 6391 | } |
| 6392 | } |
| 6393 | |
| 6394 | mListeners.notifyUnhiddenLocked(changedNotifications); |
| 6395 | } |
| 6396 | } |
| 6397 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 6398 | private void updateNotificationPulse() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6399 | synchronized (mNotificationLock) { |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 6400 | updateLightsLocked(); |
| 6401 | } |
| 6402 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 6403 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 6404 | protected boolean isCallingUidSystem() { |
| 6405 | final int uid = Binder.getCallingUid(); |
| 6406 | return uid == Process.SYSTEM_UID; |
| 6407 | } |
| 6408 | |
| 6409 | protected boolean isUidSystemOrPhone(int uid) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6410 | final int appid = UserHandle.getAppId(uid); |
| 6411 | return (appid == Process.SYSTEM_UID || appid == Process.PHONE_UID || uid == 0); |
| 6412 | } |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 6413 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 6414 | // TODO: Most calls should probably move to isCallerSystem. |
| 6415 | protected boolean isCallerSystemOrPhone() { |
| 6416 | return isUidSystemOrPhone(Binder.getCallingUid()); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6417 | } |
| 6418 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6419 | private void checkCallerIsSystemOrShell() { |
| 6420 | if (Binder.getCallingUid() == Process.SHELL_UID) { |
| 6421 | return; |
| 6422 | } |
| 6423 | checkCallerIsSystem(); |
| 6424 | } |
| 6425 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6426 | private void checkCallerIsSystem() { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 6427 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6428 | return; |
| 6429 | } |
| 6430 | throw new SecurityException("Disallowed call for uid " + Binder.getCallingUid()); |
| 6431 | } |
| 6432 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6433 | private void checkCallerIsSystemOrSameApp(String pkg) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 6434 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6435 | return; |
| 6436 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 6437 | checkCallerIsSameApp(pkg); |
| 6438 | } |
| 6439 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 6440 | private boolean isCallerAndroid(String callingPkg, int uid) { |
| 6441 | return isUidSystemOrPhone(uid) && callingPkg != null |
| 6442 | && PackageManagerService.PLATFORM_PACKAGE_NAME.equals(callingPkg); |
| 6443 | } |
| 6444 | |
Brad Stenning | 8c991ea | 2018-07-31 13:33:01 -0700 | [diff] [blame] | 6445 | /** |
| 6446 | * Check if the notification is of a category type that is restricted to system use only, |
| 6447 | * if so throw SecurityException |
| 6448 | */ |
| 6449 | private void checkRestrictedCategories(final Notification notification) { |
| 6450 | try { |
| 6451 | if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE, 0)) { |
| 6452 | return; |
| 6453 | } |
| 6454 | } catch (RemoteException re) { |
| 6455 | if (DBG) Log.e(TAG, "Unable to confirm if it's safe to skip category " |
| 6456 | + "restrictions check thus the check will be done anyway"); |
| 6457 | } |
| 6458 | if (Notification.CATEGORY_CAR_EMERGENCY.equals(notification.category) |
| 6459 | || Notification.CATEGORY_CAR_WARNING.equals(notification.category) |
| 6460 | || Notification.CATEGORY_CAR_INFORMATION.equals(notification.category)) { |
| 6461 | checkCallerIsSystem(); |
| 6462 | } |
| 6463 | } |
| 6464 | |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 6465 | @VisibleForTesting |
| 6466 | boolean isCallerInstantApp(String pkg, int callingUid, int userId) { |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 6467 | // System is always allowed to act for ephemeral apps. |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 6468 | if (isUidSystemOrPhone(callingUid)) { |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 6469 | return false; |
| 6470 | } |
| 6471 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 6472 | mAppOps.checkPackage(callingUid, pkg); |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 6473 | |
| 6474 | try { |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 6475 | ApplicationInfo ai = mPackageManager.getApplicationInfo(pkg, 0, userId); |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 6476 | if (ai == null) { |
| 6477 | throw new SecurityException("Unknown package " + pkg); |
| 6478 | } |
| 6479 | return ai.isInstantApp(); |
| 6480 | } catch (RemoteException re) { |
| 6481 | throw new SecurityException("Unknown package " + pkg, re); |
| 6482 | } |
| 6483 | |
| 6484 | } |
| 6485 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6486 | private void checkCallerIsSameApp(String pkg) { |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 6487 | checkCallerIsSameApp(pkg, Binder.getCallingUid(), UserHandle.getCallingUserId()); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 6488 | } |
| 6489 | |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 6490 | private void checkCallerIsSameApp(String pkg, int uid, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6491 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6492 | ApplicationInfo ai = mPackageManager.getApplicationInfo( |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 6493 | pkg, 0, userId); |
Dan Sandler | 09afc2e | 2014-07-18 14:29:20 -0400 | [diff] [blame] | 6494 | if (ai == null) { |
| 6495 | throw new SecurityException("Unknown package " + pkg); |
| 6496 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6497 | if (!UserHandle.isSameApp(ai.uid, uid)) { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6498 | throw new SecurityException("Calling uid " + uid + " gave package " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6499 | + pkg + " which is owned by uid " + ai.uid); |
| 6500 | } |
| 6501 | } catch (RemoteException re) { |
| 6502 | throw new SecurityException("Unknown package " + pkg + "\n" + re); |
| 6503 | } |
| 6504 | } |
| 6505 | |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 6506 | private boolean isCallerSameApp(String pkg) { |
| 6507 | try { |
| 6508 | checkCallerIsSameApp(pkg); |
| 6509 | return true; |
| 6510 | } catch (SecurityException e) { |
| 6511 | return false; |
| 6512 | } |
| 6513 | } |
| 6514 | |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 6515 | private boolean isCallerSameApp(String pkg, int uid, int userId) { |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 6516 | try { |
Julia Reynolds | b663487 | 2018-09-25 13:19:53 -0400 | [diff] [blame] | 6517 | checkCallerIsSameApp(pkg, uid, userId); |
Julia Reynolds | a7ba45a | 2018-08-29 09:07:52 -0400 | [diff] [blame] | 6518 | return true; |
| 6519 | } catch (SecurityException e) { |
| 6520 | return false; |
| 6521 | } |
| 6522 | } |
| 6523 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 6524 | private static String callStateToString(int state) { |
| 6525 | switch (state) { |
| 6526 | case TelephonyManager.CALL_STATE_IDLE: return "CALL_STATE_IDLE"; |
| 6527 | case TelephonyManager.CALL_STATE_RINGING: return "CALL_STATE_RINGING"; |
| 6528 | case TelephonyManager.CALL_STATE_OFFHOOK: return "CALL_STATE_OFFHOOK"; |
| 6529 | default: return "CALL_STATE_UNKNOWN_" + state; |
| 6530 | } |
| 6531 | } |
| 6532 | |
| 6533 | private void listenForCallState() { |
| 6534 | TelephonyManager.from(getContext()).listen(new PhoneStateListener() { |
| 6535 | @Override |
| 6536 | public void onCallStateChanged(int state, String incomingNumber) { |
| 6537 | if (mCallState == state) return; |
| 6538 | if (DBG) Slog.d(TAG, "Call state changed: " + callStateToString(state)); |
| 6539 | mCallState = state; |
| 6540 | } |
| 6541 | }, PhoneStateListener.LISTEN_CALL_STATE); |
| 6542 | } |
| 6543 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6544 | /** |
| 6545 | * Generates a NotificationRankingUpdate from 'sbns', considering only |
| 6546 | * notifications visible to the given listener. |
| 6547 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6548 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6549 | private NotificationRankingUpdate makeRankingUpdateLocked(ManagedServiceInfo info) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6550 | final int N = mNotificationList.size(); |
| 6551 | ArrayList<String> keys = new ArrayList<String>(N); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 6552 | ArrayList<String> interceptedKeys = new ArrayList<String>(N); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6553 | ArrayList<Integer> importance = new ArrayList<>(N); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 6554 | Bundle overrideGroupKeys = new Bundle(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6555 | Bundle visibilityOverrides = new Bundle(); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 6556 | Bundle suppressedVisualEffects = new Bundle(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6557 | Bundle explanation = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 6558 | Bundle channels = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 6559 | Bundle overridePeople = new Bundle(); |
| 6560 | Bundle snoozeCriteria = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 6561 | Bundle showBadge = new Bundle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6562 | Bundle userSentiment = new Bundle(); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6563 | Bundle hidden = new Bundle(); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 6564 | Bundle smartActions = new Bundle(); |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 6565 | Bundle smartReplies = new Bundle(); |
Gus Prevas | a322649 | 2018-10-23 11:10:09 -0400 | [diff] [blame] | 6566 | Bundle audiblyAlerted = new Bundle(); |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame^] | 6567 | Bundle noisy = new Bundle(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6568 | for (int i = 0; i < N; i++) { |
| 6569 | NotificationRecord record = mNotificationList.get(i); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6570 | if (!isVisibleToListener(record.sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6571 | continue; |
| 6572 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6573 | final String key = record.sbn.getKey(); |
| 6574 | keys.add(key); |
| 6575 | importance.add(record.getImportance()); |
| 6576 | if (record.getImportanceExplanation() != null) { |
| 6577 | explanation.putCharSequence(key, record.getImportanceExplanation()); |
| 6578 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6579 | if (record.isIntercepted()) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6580 | interceptedKeys.add(key); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 6581 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6582 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6583 | suppressedVisualEffects.putInt(key, record.getSuppressedVisualEffects()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6584 | if (record.getPackageVisibilityOverride() |
| 6585 | != NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6586 | visibilityOverrides.putInt(key, record.getPackageVisibilityOverride()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6587 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 6588 | overrideGroupKeys.putString(key, record.sbn.getOverrideGroupKey()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 6589 | channels.putParcelable(key, record.getChannel()); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 6590 | overridePeople.putStringArrayList(key, record.getPeopleOverride()); |
| 6591 | snoozeCriteria.putParcelableArrayList(key, record.getSnoozeCriteria()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 6592 | showBadge.putBoolean(key, record.canShowBadge()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6593 | userSentiment.putInt(key, record.getUserSentiment()); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6594 | hidden.putBoolean(key, record.isHidden()); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 6595 | smartActions.putParcelableArrayList(key, record.getSmartActions()); |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 6596 | smartReplies.putCharSequenceArrayList(key, record.getSmartReplies()); |
Gus Prevas | a322649 | 2018-10-23 11:10:09 -0400 | [diff] [blame] | 6597 | audiblyAlerted.putBoolean(key, record.getAudiblyAlerted()); |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame^] | 6598 | noisy.putBoolean(key, record.getSound() != null || record.getVibration() != null); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6599 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6600 | final int M = keys.size(); |
| 6601 | String[] keysAr = keys.toArray(new String[M]); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 6602 | String[] interceptedKeysAr = interceptedKeys.toArray(new String[interceptedKeys.size()]); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6603 | int[] importanceAr = new int[M]; |
| 6604 | for (int i = 0; i < M; i++) { |
| 6605 | importanceAr[i] = importance.get(i); |
| 6606 | } |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6607 | return new NotificationRankingUpdate(keysAr, interceptedKeysAr, visibilityOverrides, |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 6608 | suppressedVisualEffects, importanceAr, explanation, overrideGroupKeys, |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 6609 | channels, overridePeople, snoozeCriteria, showBadge, userSentiment, hidden, |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame^] | 6610 | smartActions, smartReplies, audiblyAlerted, noisy); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6611 | } |
| 6612 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6613 | boolean hasCompanionDevice(ManagedServiceInfo info) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6614 | if (mCompanionManager == null) { |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 6615 | mCompanionManager = getCompanionManager(); |
| 6616 | } |
| 6617 | // Companion mgr doesn't exist on all device types |
| 6618 | if (mCompanionManager == null) { |
| 6619 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6620 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6621 | long identity = Binder.clearCallingIdentity(); |
| 6622 | try { |
| 6623 | List<String> associations = mCompanionManager.getAssociations( |
| 6624 | info.component.getPackageName(), info.userid); |
| 6625 | if (!ArrayUtils.isEmpty(associations)) { |
| 6626 | return true; |
| 6627 | } |
| 6628 | } catch (SecurityException se) { |
| 6629 | // Not a privileged listener |
| 6630 | } catch (RemoteException re) { |
| 6631 | Slog.e(TAG, "Cannot reach companion device service", re); |
| 6632 | } catch (Exception e) { |
| 6633 | Slog.e(TAG, "Cannot verify listener " + info, e); |
| 6634 | } finally { |
| 6635 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6636 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6637 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6638 | } |
| 6639 | |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 6640 | protected ICompanionDeviceManager getCompanionManager() { |
| 6641 | return ICompanionDeviceManager.Stub.asInterface( |
| 6642 | ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE)); |
| 6643 | } |
| 6644 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6645 | private boolean isVisibleToListener(StatusBarNotification sbn, ManagedServiceInfo listener) { |
| 6646 | if (!listener.enabledAndUserMatches(sbn.getUserId())) { |
| 6647 | return false; |
| 6648 | } |
Justin Koh | 8d11a5a | 2014-08-04 18:29:49 -0700 | [diff] [blame] | 6649 | // TODO: remove this for older listeners. |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6650 | return true; |
| 6651 | } |
| 6652 | |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 6653 | private boolean isPackageSuspendedForUser(String pkg, int uid) { |
Beverly | 2be7a05 | 2018-03-27 11:37:58 -0400 | [diff] [blame] | 6654 | final long identity = Binder.clearCallingIdentity(); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 6655 | int userId = UserHandle.getUserId(uid); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 6656 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6657 | return mPackageManager.isPackageSuspendedForUser(pkg, userId); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 6658 | } catch (RemoteException re) { |
| 6659 | throw new SecurityException("Could not talk to package manager service"); |
Andrei Stingaceanu | efc4a34 | 2016-03-22 14:43:01 +0000 | [diff] [blame] | 6660 | } catch (IllegalArgumentException ex) { |
| 6661 | // Package not found. |
| 6662 | return false; |
Beverly | 2be7a05 | 2018-03-27 11:37:58 -0400 | [diff] [blame] | 6663 | } finally { |
| 6664 | Binder.restoreCallingIdentity(identity); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 6665 | } |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 6666 | } |
| 6667 | |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 6668 | @VisibleForTesting |
| 6669 | boolean canUseManagedServices(String pkg) { |
| 6670 | boolean canUseManagedServices = !mActivityManager.isLowRamDevice() |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 6671 | || mPackageManagerClient.hasSystemFeature(PackageManager.FEATURE_WATCH); |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 6672 | |
| 6673 | for (String whitelisted : getContext().getResources().getStringArray( |
| 6674 | R.array.config_allowedManagedServicesOnLowRamDevices)) { |
| 6675 | if (whitelisted.equals(pkg)) { |
| 6676 | canUseManagedServices = true; |
| 6677 | } |
| 6678 | } |
| 6679 | |
| 6680 | return canUseManagedServices; |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 6681 | } |
| 6682 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6683 | private class TrimCache { |
| 6684 | StatusBarNotification heavy; |
| 6685 | StatusBarNotification sbnClone; |
| 6686 | StatusBarNotification sbnCloneLight; |
| 6687 | |
| 6688 | TrimCache(StatusBarNotification sbn) { |
| 6689 | heavy = sbn; |
| 6690 | } |
| 6691 | |
| 6692 | StatusBarNotification ForListener(ManagedServiceInfo info) { |
| 6693 | if (mListeners.getOnNotificationPostedTrim(info) == TRIM_LIGHT) { |
| 6694 | if (sbnCloneLight == null) { |
| 6695 | sbnCloneLight = heavy.cloneLight(); |
| 6696 | } |
| 6697 | return sbnCloneLight; |
| 6698 | } else { |
| 6699 | if (sbnClone == null) { |
| 6700 | sbnClone = heavy.clone(); |
| 6701 | } |
| 6702 | return sbnClone; |
| 6703 | } |
| 6704 | } |
| 6705 | } |
| 6706 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 6707 | public class NotificationAssistants extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6708 | static final String TAG_ENABLED_NOTIFICATION_ASSISTANTS = "enabled_assistants"; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6709 | |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 6710 | public NotificationAssistants(Context context, Object lock, UserProfiles up, |
| 6711 | IPackageManager pm) { |
| 6712 | super(context, lock, up, pm); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6713 | } |
| 6714 | |
| 6715 | @Override |
| 6716 | protected Config getConfig() { |
| 6717 | Config c = new Config(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6718 | c.caption = "notification assistant"; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 6719 | c.serviceInterface = NotificationAssistantService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6720 | c.xmlTag = TAG_ENABLED_NOTIFICATION_ASSISTANTS; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 6721 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_ASSISTANT; |
| 6722 | c.bindPermission = Manifest.permission.BIND_NOTIFICATION_ASSISTANT_SERVICE; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6723 | c.settingsAction = Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS; |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 6724 | c.clientLabel = R.string.notification_ranker_binding_label; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6725 | return c; |
| 6726 | } |
| 6727 | |
| 6728 | @Override |
| 6729 | protected IInterface asInterface(IBinder binder) { |
| 6730 | return INotificationListener.Stub.asInterface(binder); |
| 6731 | } |
| 6732 | |
| 6733 | @Override |
| 6734 | protected boolean checkType(IInterface service) { |
| 6735 | return service instanceof INotificationListener; |
| 6736 | } |
| 6737 | |
| 6738 | @Override |
| 6739 | protected void onServiceAdded(ManagedServiceInfo info) { |
| 6740 | mListeners.registerGuestService(info); |
| 6741 | } |
| 6742 | |
| 6743 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6744 | @GuardedBy("mNotificationLock") |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6745 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
| 6746 | mListeners.unregisterService(removed.service, removed.userid); |
| 6747 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6748 | |
Julia Reynolds | ef934fd | 2018-02-01 14:39:17 -0500 | [diff] [blame] | 6749 | @Override |
| 6750 | public void onUserUnlocked(int user) { |
| 6751 | if (DEBUG) Slog.d(TAG, "onUserUnlocked u=" + user); |
Julia Reynolds | ca8e535 | 2018-09-18 13:39:26 -0400 | [diff] [blame] | 6752 | // force rebind the assistant, as it might be keeping its own state in user locked |
| 6753 | // storage |
| 6754 | rebindServices(true, user); |
Julia Reynolds | ef934fd | 2018-02-01 14:39:17 -0500 | [diff] [blame] | 6755 | } |
| 6756 | |
Julia Reynolds | 6a63d1b | 2018-08-14 16:59:33 -0400 | [diff] [blame] | 6757 | protected void onNotificationsSeenLocked(ArrayList<NotificationRecord> records) { |
| 6758 | // There should be only one, but it's a list, so while we enforce |
| 6759 | // singularity elsewhere, we keep it general here, to avoid surprises. |
| 6760 | for (final ManagedServiceInfo info : NotificationAssistants.this.getServices()) { |
| 6761 | ArrayList<String> keys = new ArrayList<>(records.size()); |
| 6762 | for (NotificationRecord r : records) { |
| 6763 | boolean sbnVisible = isVisibleToListener(r.sbn, info) |
| 6764 | && info.isSameUser(r.getUserId()); |
| 6765 | if (sbnVisible) { |
| 6766 | keys.add(r.getKey()); |
| 6767 | } |
| 6768 | } |
| 6769 | |
| 6770 | if (!keys.isEmpty()) { |
| 6771 | mHandler.post(() -> notifySeen(info, keys)); |
| 6772 | } |
| 6773 | } |
| 6774 | } |
| 6775 | |
| 6776 | private void notifySeen(final ManagedServiceInfo info, |
| 6777 | final ArrayList<String> keys) { |
| 6778 | final INotificationListener assistant = (INotificationListener) info.service; |
| 6779 | try { |
| 6780 | assistant.onNotificationsSeen(keys); |
| 6781 | } catch (RemoteException ex) { |
| 6782 | Log.e(TAG, "unable to notify assistant (seen): " + assistant, ex); |
| 6783 | } |
| 6784 | } |
| 6785 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6786 | public void onNotificationEnqueued(final NotificationRecord r) { |
| 6787 | final StatusBarNotification sbn = r.sbn; |
| 6788 | TrimCache trimCache = new TrimCache(sbn); |
| 6789 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6790 | // There should be only one, but it's a list, so while we enforce |
| 6791 | // singularity elsewhere, we keep it general here, to avoid surprises. |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6792 | for (final ManagedServiceInfo info : NotificationAssistants.this.getServices()) { |
Julia Reynolds | 70aaea7 | 2018-07-13 13:38:34 -0400 | [diff] [blame] | 6793 | boolean sbnVisible = isVisibleToListener(sbn, info) |
| 6794 | && info.isSameUser(r.getUserId()); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6795 | if (!sbnVisible) { |
| 6796 | continue; |
| 6797 | } |
| 6798 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6799 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 6800 | mHandler.post(new Runnable() { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6801 | @Override |
| 6802 | public void run() { |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 6803 | notifyEnqueued(info, sbnToPost, r.getChannel()); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6804 | } |
| 6805 | }); |
| 6806 | } |
| 6807 | } |
| 6808 | |
| 6809 | private void notifyEnqueued(final ManagedServiceInfo info, |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 6810 | final StatusBarNotification sbn, final NotificationChannel channel) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 6811 | final INotificationListener assistant = (INotificationListener) info.service; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6812 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
| 6813 | try { |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 6814 | assistant.onNotificationEnqueuedWithChannel(sbnHolder, channel); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6815 | } catch (RemoteException ex) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 6816 | Log.e(TAG, "unable to notify assistant (enqueued): " + assistant, ex); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6817 | } |
| 6818 | } |
| 6819 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6820 | /** |
| 6821 | * asynchronously notify the assistant that a notification has been snoozed until a |
| 6822 | * context |
| 6823 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6824 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6825 | public void notifyAssistantSnoozedLocked(final StatusBarNotification sbn, |
| 6826 | final String snoozeCriterionId) { |
| 6827 | TrimCache trimCache = new TrimCache(sbn); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6828 | for (final ManagedServiceInfo info : getServices()) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6829 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 6830 | if (!sbnVisible) { |
| 6831 | continue; |
| 6832 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6833 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
| 6834 | mHandler.post(new Runnable() { |
| 6835 | @Override |
| 6836 | public void run() { |
| 6837 | final INotificationListener assistant = |
| 6838 | (INotificationListener) info.service; |
| 6839 | StatusBarNotificationHolder sbnHolder |
| 6840 | = new StatusBarNotificationHolder(sbnToPost); |
| 6841 | try { |
| 6842 | assistant.onNotificationSnoozedUntilContext( |
| 6843 | sbnHolder, snoozeCriterionId); |
| 6844 | } catch (RemoteException ex) { |
| 6845 | Log.e(TAG, "unable to notify assistant (snoozed): " + assistant, ex); |
| 6846 | } |
| 6847 | } |
| 6848 | }); |
| 6849 | } |
| 6850 | } |
| 6851 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6852 | public boolean isEnabled() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6853 | return !getServices().isEmpty(); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6854 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 6855 | |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 6856 | protected void ensureAssistant() { |
| 6857 | final List<UserInfo> activeUsers = mUm.getUsers(true); |
| 6858 | for (UserInfo userInfo : activeUsers) { |
| 6859 | int userId = userInfo.getUserHandle().getIdentifier(); |
| 6860 | if (getAllowedPackages(userId).isEmpty()) { |
| 6861 | Slog.d(TAG, "Approving default notification assistant for user " + userId); |
| 6862 | readDefaultAssistant(userId); |
| 6863 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 6864 | } |
| 6865 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6866 | } |
| 6867 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6868 | public class NotificationListeners extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6869 | static final String TAG_ENABLED_NOTIFICATION_LISTENERS = "enabled_listeners"; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6870 | |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6871 | private final ArraySet<ManagedServiceInfo> mLightTrimListeners = new ArraySet<>(); |
| 6872 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6873 | public NotificationListeners(IPackageManager pm) { |
| 6874 | super(getContext(), mNotificationLock, mUserProfiles, pm); |
| 6875 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6876 | } |
| 6877 | |
| 6878 | @Override |
Amith Yamasani | e5bfeee | 2018-09-05 18:52:35 -0700 | [diff] [blame] | 6879 | protected int getBindFlags() { |
| 6880 | // Most of the same flags as the base, but also add BIND_ADJUST_BELOW_PERCEPTIBLE |
| 6881 | // because too many 3P apps could be kept in memory as notification listeners and |
| 6882 | // cause extreme memory pressure. |
| 6883 | // TODO: Change the binding lifecycle of NotificationListeners to avoid this situation. |
| 6884 | return BIND_AUTO_CREATE | BIND_FOREGROUND_SERVICE |
| 6885 | | BIND_ADJUST_BELOW_PERCEPTIBLE | BIND_ALLOW_WHITELIST_MANAGEMENT; |
| 6886 | } |
| 6887 | |
| 6888 | @Override |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6889 | protected Config getConfig() { |
| 6890 | Config c = new Config(); |
| 6891 | c.caption = "notification listener"; |
| 6892 | c.serviceInterface = NotificationListenerService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6893 | c.xmlTag = TAG_ENABLED_NOTIFICATION_LISTENERS; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6894 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS; |
| 6895 | c.bindPermission = android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE; |
| 6896 | c.settingsAction = Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS; |
| 6897 | c.clientLabel = R.string.notification_listener_binding_label; |
| 6898 | return c; |
| 6899 | } |
| 6900 | |
| 6901 | @Override |
| 6902 | protected IInterface asInterface(IBinder binder) { |
| 6903 | return INotificationListener.Stub.asInterface(binder); |
| 6904 | } |
| 6905 | |
| 6906 | @Override |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6907 | protected boolean checkType(IInterface service) { |
| 6908 | return service instanceof INotificationListener; |
| 6909 | } |
| 6910 | |
| 6911 | @Override |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 6912 | public void onServiceAdded(ManagedServiceInfo info) { |
| 6913 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6914 | final NotificationRankingUpdate update; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6915 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6916 | update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6917 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6918 | try { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6919 | listener.onListenerConnected(update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6920 | } catch (RemoteException e) { |
| 6921 | // we tried |
| 6922 | } |
| 6923 | } |
| 6924 | |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6925 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6926 | @GuardedBy("mNotificationLock") |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6927 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 6928 | if (removeDisabledHints(removed)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6929 | updateListenerHintsLocked(); |
Christoph Studer | 0d6ef4b | 2014-12-02 15:00:48 +0100 | [diff] [blame] | 6930 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6931 | } |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6932 | mLightTrimListeners.remove(removed); |
| 6933 | } |
| 6934 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6935 | @GuardedBy("mNotificationLock") |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6936 | public void setOnNotificationPostedTrimLocked(ManagedServiceInfo info, int trim) { |
| 6937 | if (trim == TRIM_LIGHT) { |
| 6938 | mLightTrimListeners.add(info); |
| 6939 | } else { |
| 6940 | mLightTrimListeners.remove(info); |
| 6941 | } |
| 6942 | } |
| 6943 | |
| 6944 | public int getOnNotificationPostedTrim(ManagedServiceInfo info) { |
| 6945 | return mLightTrimListeners.contains(info) ? TRIM_LIGHT : TRIM_FULL; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6946 | } |
| 6947 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6948 | /** |
| 6949 | * asynchronously notify all listeners about a new notification |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6950 | * |
| 6951 | * <p> |
| 6952 | * Also takes care of removing a notification that has been visible to a listener before, |
| 6953 | * but isn't anymore. |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6954 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6955 | @GuardedBy("mNotificationLock") |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6956 | public void notifyPostedLocked(NotificationRecord r, NotificationRecord old) { |
| 6957 | notifyPostedLocked(r, old, true); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6958 | } |
| 6959 | |
| 6960 | /** |
| 6961 | * @param notifyAllListeners notifies all listeners if true, else only notifies listeners |
| 6962 | * targetting <= O_MR1 |
| 6963 | */ |
| 6964 | @GuardedBy("mNotificationLock") |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6965 | private void notifyPostedLocked(NotificationRecord r, NotificationRecord old, |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6966 | boolean notifyAllListeners) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6967 | // Lazily initialized snapshots of the notification. |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 6968 | StatusBarNotification sbn = r.sbn; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6969 | StatusBarNotification oldSbn = (old != null) ? old.sbn : null; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6970 | TrimCache trimCache = new TrimCache(sbn); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6971 | |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6972 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6973 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 6974 | boolean oldSbnVisible = oldSbn != null ? isVisibleToListener(oldSbn, info) : false; |
| 6975 | // This notification hasn't been and still isn't visible -> ignore. |
| 6976 | if (!oldSbnVisible && !sbnVisible) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6977 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6978 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6979 | // If the notification is hidden, don't notifyPosted listeners targeting < P. |
| 6980 | // Instead, those listeners will receive notifyPosted when the notification is |
| 6981 | // unhidden. |
| 6982 | if (r.isHidden() && info.targetSdkVersion < Build.VERSION_CODES.P) { |
| 6983 | continue; |
| 6984 | } |
| 6985 | |
| 6986 | // If we shouldn't notify all listeners, this means the hidden state of |
| 6987 | // a notification was changed. Don't notifyPosted listeners targeting >= P. |
| 6988 | // Instead, those listeners will receive notifyRankingUpdate. |
| 6989 | if (!notifyAllListeners && info.targetSdkVersion >= Build.VERSION_CODES.P) { |
| 6990 | continue; |
| 6991 | } |
| 6992 | |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6993 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6994 | |
| 6995 | // This notification became invisible -> remove the old one. |
| 6996 | if (oldSbnVisible && !sbnVisible) { |
| 6997 | final StatusBarNotification oldSbnLightClone = oldSbn.cloneLight(); |
| 6998 | mHandler.post(new Runnable() { |
| 6999 | @Override |
| 7000 | public void run() { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 7001 | notifyRemoved( |
| 7002 | info, oldSbnLightClone, update, null, REASON_USER_STOPPED); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 7003 | } |
| 7004 | }); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7005 | continue; |
| 7006 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 7007 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 7008 | // Grant access before listener is notified |
| 7009 | final int targetUserId = (info.userid == UserHandle.USER_ALL) |
| 7010 | ? UserHandle.USER_SYSTEM : info.userid; |
| 7011 | updateUriPermissions(r, old, info.component.getPackageName(), targetUserId); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 7012 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 7013 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7014 | mHandler.post(new Runnable() { |
| 7015 | @Override |
| 7016 | public void run() { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 7017 | notifyPosted(info, sbnToPost, update); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7018 | } |
| 7019 | }); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 7020 | } |
| 7021 | } |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 7022 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7023 | /** |
| 7024 | * asynchronously notify all listeners about a removed notification |
| 7025 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 7026 | @GuardedBy("mNotificationLock") |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7027 | public void notifyRemovedLocked(NotificationRecord r, int reason, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 7028 | NotificationStats notificationStats) { |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7029 | final StatusBarNotification sbn = r.sbn; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 7030 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7031 | // make a copy in case changes are made to the underlying Notification object |
| 7032 | // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the |
| 7033 | // notification |
| 7034 | final StatusBarNotification sbnLight = sbn.cloneLight(); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 7035 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 7036 | if (!isVisibleToListener(sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7037 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 7038 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7039 | |
| 7040 | // don't notifyRemoved for listeners targeting < P |
| 7041 | // if not for reason package suspended |
| 7042 | if (r.isHidden() && reason != REASON_PACKAGE_SUSPENDED |
| 7043 | && info.targetSdkVersion < Build.VERSION_CODES.P) { |
| 7044 | continue; |
| 7045 | } |
| 7046 | |
| 7047 | // don't notifyRemoved for listeners targeting >= P |
| 7048 | // if the reason is package suspended |
| 7049 | if (reason == REASON_PACKAGE_SUSPENDED |
| 7050 | && info.targetSdkVersion >= Build.VERSION_CODES.P) { |
| 7051 | continue; |
| 7052 | } |
| 7053 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 7054 | // Only assistants can get stats |
| 7055 | final NotificationStats stats = mAssistants.isServiceTokenValidLocked(info.service) |
| 7056 | ? notificationStats : null; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 7057 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7058 | mHandler.post(new Runnable() { |
| 7059 | @Override |
| 7060 | public void run() { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 7061 | notifyRemoved(info, sbnLight, update, stats, reason); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7062 | } |
| 7063 | }); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 7064 | } |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 7065 | |
| 7066 | // Revoke access after all listeners have been updated |
| 7067 | mHandler.post(() -> { |
| 7068 | updateUriPermissions(null, r, null, UserHandle.USER_SYSTEM); |
| 7069 | }); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 7070 | } |
| 7071 | |
| 7072 | /** |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7073 | * Asynchronously notify all listeners about a reordering of notifications |
| 7074 | * unless changedHiddenNotifications is populated. |
| 7075 | * If changedHiddenNotifications is populated, there was a change in the hidden state |
| 7076 | * of the notifications. In this case, we only send updates to listeners that |
| 7077 | * target >= P. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 7078 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 7079 | @GuardedBy("mNotificationLock") |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7080 | public void notifyRankingUpdateLocked(List<NotificationRecord> changedHiddenNotifications) { |
| 7081 | boolean isHiddenRankingUpdate = changedHiddenNotifications != null |
| 7082 | && changedHiddenNotifications.size() > 0; |
| 7083 | |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 7084 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7085 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 7086 | continue; |
| 7087 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7088 | |
| 7089 | boolean notifyThisListener = false; |
| 7090 | if (isHiddenRankingUpdate && serviceInfo.targetSdkVersion >= |
| 7091 | Build.VERSION_CODES.P) { |
| 7092 | for (NotificationRecord rec : changedHiddenNotifications) { |
| 7093 | if (isVisibleToListener(rec.sbn, serviceInfo)) { |
| 7094 | notifyThisListener = true; |
| 7095 | break; |
| 7096 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7097 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7098 | } |
| 7099 | |
| 7100 | if (notifyThisListener || !isHiddenRankingUpdate) { |
| 7101 | final NotificationRankingUpdate update = makeRankingUpdateLocked( |
| 7102 | serviceInfo); |
| 7103 | |
| 7104 | mHandler.post(new Runnable() { |
| 7105 | @Override |
| 7106 | public void run() { |
| 7107 | notifyRankingUpdate(serviceInfo, update); |
| 7108 | } |
| 7109 | }); |
| 7110 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 7111 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 7112 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 7113 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 7114 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 7115 | public void notifyListenerHintsChangedLocked(final int hints) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 7116 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 7117 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 7118 | continue; |
| 7119 | } |
| 7120 | mHandler.post(new Runnable() { |
| 7121 | @Override |
| 7122 | public void run() { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 7123 | notifyListenerHintsChanged(serviceInfo, hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 7124 | } |
| 7125 | }); |
| 7126 | } |
| 7127 | } |
| 7128 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7129 | /** |
| 7130 | * asynchronously notify relevant listeners their notification is hidden |
| 7131 | * NotificationListenerServices that target P+: |
| 7132 | * NotificationListenerService#notifyRankingUpdateLocked() |
| 7133 | * NotificationListenerServices that target <= P: |
| 7134 | * NotificationListenerService#notifyRemovedLocked() with REASON_PACKAGE_SUSPENDED. |
| 7135 | */ |
| 7136 | @GuardedBy("mNotificationLock") |
| 7137 | public void notifyHiddenLocked(List<NotificationRecord> changedNotifications) { |
| 7138 | if (changedNotifications == null || changedNotifications.size() == 0) { |
| 7139 | return; |
| 7140 | } |
| 7141 | |
| 7142 | notifyRankingUpdateLocked(changedNotifications); |
| 7143 | |
| 7144 | // for listeners that target < P, notifyRemoveLocked |
| 7145 | int numChangedNotifications = changedNotifications.size(); |
| 7146 | for (int i = 0; i < numChangedNotifications; i++) { |
| 7147 | NotificationRecord rec = changedNotifications.get(i); |
| 7148 | mListeners.notifyRemovedLocked(rec, REASON_PACKAGE_SUSPENDED, rec.getStats()); |
| 7149 | } |
| 7150 | } |
| 7151 | |
| 7152 | /** |
| 7153 | * asynchronously notify relevant listeners their notification is unhidden |
| 7154 | * NotificationListenerServices that target P+: |
| 7155 | * NotificationListenerService#notifyRankingUpdateLocked() |
| 7156 | * NotificationListenerServices that target <= P: |
| 7157 | * NotificationListeners#notifyPostedLocked() |
| 7158 | */ |
| 7159 | @GuardedBy("mNotificationLock") |
| 7160 | public void notifyUnhiddenLocked(List<NotificationRecord> changedNotifications) { |
| 7161 | if (changedNotifications == null || changedNotifications.size() == 0) { |
| 7162 | return; |
| 7163 | } |
| 7164 | |
| 7165 | notifyRankingUpdateLocked(changedNotifications); |
| 7166 | |
| 7167 | // for listeners that target < P, notifyPostedLocked |
| 7168 | int numChangedNotifications = changedNotifications.size(); |
| 7169 | for (int i = 0; i < numChangedNotifications; i++) { |
| 7170 | NotificationRecord rec = changedNotifications.get(i); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 7171 | mListeners.notifyPostedLocked(rec, rec, false); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7172 | } |
| 7173 | } |
| 7174 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 7175 | public void notifyInterruptionFilterChanged(final int interruptionFilter) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 7176 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 7177 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 7178 | continue; |
| 7179 | } |
| 7180 | mHandler.post(new Runnable() { |
| 7181 | @Override |
| 7182 | public void run() { |
| 7183 | notifyInterruptionFilterChanged(serviceInfo, interruptionFilter); |
| 7184 | } |
| 7185 | }); |
| 7186 | } |
| 7187 | } |
| 7188 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 7189 | protected void notifyNotificationChannelChanged(final String pkg, final UserHandle user, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7190 | final NotificationChannel channel, final int modificationType) { |
| 7191 | if (channel == null) { |
| 7192 | return; |
| 7193 | } |
| 7194 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 7195 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7196 | continue; |
| 7197 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 7198 | |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 7199 | BackgroundThread.getHandler().post(() -> { |
| 7200 | if (hasCompanionDevice(serviceInfo)) { |
| 7201 | notifyNotificationChannelChanged( |
| 7202 | serviceInfo, pkg, user, channel, modificationType); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 7203 | } |
| 7204 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7205 | } |
| 7206 | } |
| 7207 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 7208 | protected void notifyNotificationChannelGroupChanged( |
| 7209 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
| 7210 | final int modificationType) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7211 | if (group == null) { |
| 7212 | return; |
| 7213 | } |
| 7214 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 7215 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7216 | continue; |
| 7217 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 7218 | |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 7219 | BackgroundThread.getHandler().post(() -> { |
| 7220 | if (hasCompanionDevice(serviceInfo)) { |
| 7221 | notifyNotificationChannelGroupChanged( |
| 7222 | serviceInfo, pkg, user, group, modificationType); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 7223 | } |
| 7224 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7225 | } |
| 7226 | } |
| 7227 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 7228 | private void notifyPosted(final ManagedServiceInfo info, |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7229 | final StatusBarNotification sbn, NotificationRankingUpdate rankingUpdate) { |
Julia Reynolds | a75c752 | 2017-03-21 17:34:25 -0400 | [diff] [blame] | 7230 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 7231 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7232 | try { |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 7233 | listener.onNotificationPosted(sbnHolder, rankingUpdate); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7234 | } catch (RemoteException ex) { |
| 7235 | Log.e(TAG, "unable to notify listener (posted): " + listener, ex); |
| 7236 | } |
| 7237 | } |
| 7238 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 7239 | private void notifyRemoved(ManagedServiceInfo info, StatusBarNotification sbn, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 7240 | NotificationRankingUpdate rankingUpdate, NotificationStats stats, int reason) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7241 | if (!info.enabledAndUserMatches(sbn.getUserId())) { |
| 7242 | return; |
| 7243 | } |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7244 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 7245 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7246 | try { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 7247 | listener.onNotificationRemoved(sbnHolder, rankingUpdate, stats, reason); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 7248 | } catch (RemoteException ex) { |
| 7249 | Log.e(TAG, "unable to notify listener (removed): " + listener, ex); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 7250 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 7251 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 7252 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7253 | private void notifyRankingUpdate(ManagedServiceInfo info, |
| 7254 | NotificationRankingUpdate rankingUpdate) { |
| 7255 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 7256 | try { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 7257 | listener.onNotificationRankingUpdate(rankingUpdate); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 7258 | } catch (RemoteException ex) { |
| 7259 | Log.e(TAG, "unable to notify listener (ranking update): " + listener, ex); |
| 7260 | } |
| 7261 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 7262 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 7263 | private void notifyListenerHintsChanged(ManagedServiceInfo info, int hints) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 7264 | final INotificationListener listener = (INotificationListener) info.service; |
| 7265 | try { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 7266 | listener.onListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 7267 | } catch (RemoteException ex) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 7268 | Log.e(TAG, "unable to notify listener (listener hints): " + listener, ex); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 7269 | } |
| 7270 | } |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 7271 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 7272 | private void notifyInterruptionFilterChanged(ManagedServiceInfo info, |
| 7273 | int interruptionFilter) { |
| 7274 | final INotificationListener listener = (INotificationListener) info.service; |
| 7275 | try { |
| 7276 | listener.onInterruptionFilterChanged(interruptionFilter); |
| 7277 | } catch (RemoteException ex) { |
| 7278 | Log.e(TAG, "unable to notify listener (interruption filter): " + listener, ex); |
| 7279 | } |
| 7280 | } |
| 7281 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7282 | void notifyNotificationChannelChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 7283 | final String pkg, final UserHandle user, final NotificationChannel channel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7284 | final int modificationType) { |
| 7285 | final INotificationListener listener = (INotificationListener) info.service; |
| 7286 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 7287 | listener.onNotificationChannelModification(pkg, user, channel, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7288 | } catch (RemoteException ex) { |
| 7289 | Log.e(TAG, "unable to notify listener (channel changed): " + listener, ex); |
| 7290 | } |
| 7291 | } |
| 7292 | |
| 7293 | private void notifyNotificationChannelGroupChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 7294 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7295 | final int modificationType) { |
| 7296 | final INotificationListener listener = (INotificationListener) info.service; |
| 7297 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 7298 | listener.onNotificationChannelGroupModification(pkg, user, group, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 7299 | } catch (RemoteException ex) { |
| 7300 | Log.e(TAG, "unable to notify listener (channel group changed): " + listener, ex); |
| 7301 | } |
| 7302 | } |
| 7303 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 7304 | public boolean isListenerPackage(String packageName) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 7305 | if (packageName == null) { |
| 7306 | return false; |
| 7307 | } |
| 7308 | // TODO: clean up locking object later |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 7309 | synchronized (mNotificationLock) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 7310 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 7311 | if (packageName.equals(serviceInfo.component.getPackageName())) { |
| 7312 | return true; |
| 7313 | } |
| 7314 | } |
| 7315 | } |
| 7316 | return false; |
| 7317 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 7318 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 7319 | |
| 7320 | public static final class DumpFilter { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7321 | public boolean filtered = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 7322 | public String pkgFilter; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 7323 | public boolean zen; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 7324 | public long since; |
| 7325 | public boolean stats; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7326 | public boolean redact = true; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 7327 | public boolean proto = false; |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 7328 | public boolean criticalPriority = false; |
| 7329 | public boolean normalPriority = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 7330 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 7331 | @NonNull |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 7332 | public static DumpFilter parseFromArguments(String[] args) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7333 | final DumpFilter filter = new DumpFilter(); |
| 7334 | for (int ai = 0; ai < args.length; ai++) { |
| 7335 | final String a = args[ai]; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 7336 | if ("--proto".equals(a)) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 7337 | filter.proto = true; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 7338 | } else if ("--noredact".equals(a) || "--reveal".equals(a)) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7339 | filter.redact = false; |
| 7340 | } else if ("p".equals(a) || "pkg".equals(a) || "--package".equals(a)) { |
| 7341 | if (ai < args.length-1) { |
| 7342 | ai++; |
| 7343 | filter.pkgFilter = args[ai].trim().toLowerCase(); |
| 7344 | if (filter.pkgFilter.isEmpty()) { |
| 7345 | filter.pkgFilter = null; |
| 7346 | } else { |
| 7347 | filter.filtered = true; |
| 7348 | } |
| 7349 | } |
| 7350 | } else if ("--zen".equals(a) || "zen".equals(a)) { |
| 7351 | filter.filtered = true; |
| 7352 | filter.zen = true; |
| 7353 | } else if ("--stats".equals(a)) { |
| 7354 | filter.stats = true; |
| 7355 | if (ai < args.length-1) { |
| 7356 | ai++; |
Tobias Thierer | 28532d0 | 2016-04-21 14:52:10 +0100 | [diff] [blame] | 7357 | filter.since = Long.parseLong(args[ai]); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7358 | } else { |
| 7359 | filter.since = 0; |
| 7360 | } |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 7361 | } else if (PRIORITY_ARG.equals(a)) { |
| 7362 | // Bugreport will call the service twice with priority arguments, first to dump |
| 7363 | // critical sections and then non critical ones. Set approriate filters |
| 7364 | // to generate the desired data. |
| 7365 | if (ai < args.length - 1) { |
| 7366 | ai++; |
| 7367 | switch (args[ai]) { |
| 7368 | case PRIORITY_ARG_CRITICAL: |
| 7369 | filter.criticalPriority = true; |
| 7370 | break; |
| 7371 | case PRIORITY_ARG_NORMAL: |
| 7372 | filter.normalPriority = true; |
| 7373 | break; |
| 7374 | } |
| 7375 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7376 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 7377 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7378 | return filter; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 7379 | } |
| 7380 | |
| 7381 | public boolean matches(StatusBarNotification sbn) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7382 | if (!filtered) return true; |
| 7383 | return zen ? true : sbn != null |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 7384 | && (matches(sbn.getPackageName()) || matches(sbn.getOpPkg())); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 7385 | } |
| 7386 | |
| 7387 | public boolean matches(ComponentName component) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7388 | if (!filtered) return true; |
| 7389 | return zen ? true : component != null && matches(component.getPackageName()); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 7390 | } |
| 7391 | |
| 7392 | public boolean matches(String pkg) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7393 | if (!filtered) return true; |
| 7394 | return zen ? true : pkg != null && pkg.toLowerCase().contains(pkgFilter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 7395 | } |
| 7396 | |
| 7397 | @Override |
| 7398 | public String toString() { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 7399 | return stats ? "stats" : zen ? "zen" : ('\'' + pkgFilter + '\''); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 7400 | } |
| 7401 | } |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 7402 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7403 | @VisibleForTesting |
| 7404 | protected void simulatePackageSuspendBroadcast(boolean suspend, String pkg) { |
| 7405 | // only use for testing: mimic receive broadcast that package is (un)suspended |
| 7406 | // but does not actually (un)suspend the package |
| 7407 | final Bundle extras = new Bundle(); |
| 7408 | extras.putStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST, |
| 7409 | new String[]{pkg}); |
| 7410 | |
| 7411 | final String action = suspend ? Intent.ACTION_PACKAGES_SUSPENDED |
Beverly | 3c707b4 | 2018-09-14 09:49:07 -0400 | [diff] [blame] | 7412 | : Intent.ACTION_PACKAGES_UNSUSPENDED; |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7413 | final Intent intent = new Intent(action); |
| 7414 | intent.putExtras(extras); |
| 7415 | |
| 7416 | mPackageIntentReceiver.onReceive(getContext(), intent); |
| 7417 | } |
| 7418 | |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 7419 | /** |
| 7420 | * Wrapper for a StatusBarNotification object that allows transfer across a oneway |
| 7421 | * binder without sending large amounts of data over a oneway transaction. |
| 7422 | */ |
| 7423 | private static final class StatusBarNotificationHolder |
| 7424 | extends IStatusBarNotificationHolder.Stub { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 7425 | private StatusBarNotification mValue; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 7426 | |
| 7427 | public StatusBarNotificationHolder(StatusBarNotification value) { |
| 7428 | mValue = value; |
| 7429 | } |
| 7430 | |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 7431 | /** 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] | 7432 | @Override |
| 7433 | public StatusBarNotification get() { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 7434 | StatusBarNotification value = mValue; |
| 7435 | mValue = null; |
| 7436 | return value; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 7437 | } |
| 7438 | } |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7439 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7440 | private class ShellCmd extends ShellCommand { |
| 7441 | public static final String USAGE = "help\n" |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 7442 | + "allow_listener COMPONENT [user_id]\n" |
| 7443 | + "disallow_listener COMPONENT [user_id]\n" |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 7444 | + "allow_assistant COMPONENT\n" |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 7445 | + "remove_assistant COMPONENT\n" |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7446 | + "allow_dnd PACKAGE\n" |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7447 | + "disallow_dnd PACKAGE\n" |
| 7448 | + "suspend_package PACKAGE\n" |
| 7449 | + "unsuspend_package PACKAGE"; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7450 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7451 | @Override |
| 7452 | public int onCommand(String cmd) { |
Felipe Leme | 68d8041 | 2017-07-14 11:18:08 -0700 | [diff] [blame] | 7453 | if (cmd == null) { |
| 7454 | return handleDefaultCommands(cmd); |
| 7455 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 7456 | final PrintWriter pw = getOutPrintWriter(); |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 7457 | try { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7458 | switch (cmd) { |
| 7459 | case "allow_dnd": { |
| 7460 | getBinderService().setNotificationPolicyAccessGranted( |
| 7461 | getNextArgRequired(), true); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7462 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7463 | break; |
| 7464 | |
| 7465 | case "disallow_dnd": { |
| 7466 | getBinderService().setNotificationPolicyAccessGranted( |
| 7467 | getNextArgRequired(), false); |
| 7468 | } |
| 7469 | break; |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 7470 | case "allow_listener": { |
| 7471 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 7472 | if (cn == null) { |
| 7473 | pw.println("Invalid listener - must be a ComponentName"); |
| 7474 | return -1; |
| 7475 | } |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 7476 | String userId = getNextArg(); |
| 7477 | if (userId == null) { |
| 7478 | getBinderService().setNotificationListenerAccessGranted(cn, true); |
| 7479 | } else { |
| 7480 | getBinderService().setNotificationListenerAccessGrantedForUser( |
| 7481 | cn, Integer.parseInt(userId), true); |
| 7482 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 7483 | } |
| 7484 | break; |
| 7485 | case "disallow_listener": { |
| 7486 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 7487 | if (cn == null) { |
| 7488 | pw.println("Invalid listener - must be a ComponentName"); |
| 7489 | return -1; |
| 7490 | } |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 7491 | String userId = getNextArg(); |
| 7492 | if (userId == null) { |
| 7493 | getBinderService().setNotificationListenerAccessGranted(cn, false); |
| 7494 | } else { |
| 7495 | getBinderService().setNotificationListenerAccessGrantedForUser( |
| 7496 | cn, Integer.parseInt(userId), false); |
| 7497 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 7498 | } |
| 7499 | break; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 7500 | case "allow_assistant": { |
| 7501 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 7502 | if (cn == null) { |
| 7503 | pw.println("Invalid assistant - must be a ComponentName"); |
| 7504 | return -1; |
| 7505 | } |
| 7506 | getBinderService().setNotificationAssistantAccessGranted(cn, true); |
| 7507 | } |
| 7508 | break; |
| 7509 | case "disallow_assistant": { |
| 7510 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 7511 | if (cn == null) { |
| 7512 | pw.println("Invalid assistant - must be a ComponentName"); |
| 7513 | return -1; |
| 7514 | } |
| 7515 | getBinderService().setNotificationAssistantAccessGranted(cn, false); |
| 7516 | } |
| 7517 | break; |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7518 | case "suspend_package": { |
| 7519 | // only use for testing |
| 7520 | simulatePackageSuspendBroadcast(true, getNextArgRequired()); |
| 7521 | } |
| 7522 | break; |
| 7523 | case "unsuspend_package": { |
| 7524 | // only use for testing |
| 7525 | simulatePackageSuspendBroadcast(false, getNextArgRequired()); |
| 7526 | } |
| 7527 | break; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7528 | default: |
| 7529 | return handleDefaultCommands(cmd); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7530 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 7531 | } catch (Exception e) { |
| 7532 | pw.println("Error occurred. Check logcat for details. " + e.getMessage()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7533 | Slog.e(TAG, "Error running shell command", e); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7534 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7535 | return 0; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7536 | } |
| 7537 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7538 | @Override |
| 7539 | public void onHelp() { |
| 7540 | getOutPrintWriter().println(USAGE); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7541 | } |
| 7542 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7543 | } |