The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 17 | package com.android.server.notification; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 19 | import static android.app.Notification.FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 20 | import static android.app.NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 21 | import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED; |
| 22 | import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 23 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 24 | import static android.app.NotificationManager.IMPORTANCE_MIN; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 25 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 26 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECTS_UNSET; |
| 27 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_AMBIENT; |
| 28 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_BADGE; |
| 29 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 30 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_LIGHTS; |
| 31 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST; |
| 32 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK; |
| 33 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_OFF; |
| 34 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_SCREEN_ON; |
| 35 | import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_STATUS_BAR; |
Amith Yamasani | e1fb58d | 2018-09-05 18:52:35 -0700 | [diff] [blame] | 36 | import static android.content.Context.BIND_ADJUST_BELOW_PERCEPTIBLE; |
| 37 | import static android.content.Context.BIND_ALLOW_WHITELIST_MANAGEMENT; |
| 38 | import static android.content.Context.BIND_AUTO_CREATE; |
| 39 | import static android.content.Context.BIND_FOREGROUND_SERVICE; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 40 | import static android.content.pm.PackageManager.FEATURE_LEANBACK; |
| 41 | import static android.content.pm.PackageManager.FEATURE_TELEVISION; |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 42 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 43 | import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_CRITICAL; |
| 44 | import static android.os.IServiceManager.DUMP_FLAG_PRIORITY_NORMAL; |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 45 | import static android.os.UserHandle.USER_NULL; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 46 | import static android.os.UserHandle.USER_SYSTEM; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 47 | import static android.service.notification.NotificationListenerService |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 48 | .HINT_HOST_DISABLE_CALL_EFFECTS; |
| 49 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_EFFECTS; |
| 50 | import static android.service.notification.NotificationListenerService |
| 51 | .HINT_HOST_DISABLE_NOTIFICATION_EFFECTS; |
| 52 | import static android.service.notification.NotificationListenerService |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 53 | .NOTIFICATION_CHANNEL_OR_GROUP_ADDED; |
| 54 | import static android.service.notification.NotificationListenerService |
| 55 | .NOTIFICATION_CHANNEL_OR_GROUP_DELETED; |
| 56 | import static android.service.notification.NotificationListenerService |
| 57 | .NOTIFICATION_CHANNEL_OR_GROUP_UPDATED; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 58 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL; |
| 59 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL; |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 60 | import static android.service.notification.NotificationListenerService.REASON_CANCEL; |
| 61 | import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 62 | import static android.service.notification.NotificationListenerService.REASON_CHANNEL_BANNED; |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 63 | import static android.service.notification.NotificationListenerService.REASON_CLICK; |
| 64 | import static android.service.notification.NotificationListenerService.REASON_ERROR; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 65 | import static android.service.notification.NotificationListenerService |
| 66 | .REASON_GROUP_SUMMARY_CANCELED; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 67 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL; |
| 68 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL_ALL; |
| 69 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_BANNED; |
| 70 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_CHANGED; |
| 71 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_SUSPENDED; |
| 72 | import static android.service.notification.NotificationListenerService.REASON_PROFILE_TURNED_OFF; |
| 73 | import static android.service.notification.NotificationListenerService.REASON_SNOOZED; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 74 | import static android.service.notification.NotificationListenerService.REASON_TIMEOUT; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 75 | import static android.service.notification.NotificationListenerService.REASON_UNAUTOBUNDLED; |
| 76 | import static android.service.notification.NotificationListenerService.REASON_USER_STOPPED; |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 77 | import static android.service.notification.NotificationListenerService.TRIM_FULL; |
| 78 | import static android.service.notification.NotificationListenerService.TRIM_LIGHT; |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 79 | import static android.view.Display.DEFAULT_DISPLAY; |
| 80 | import static android.view.WindowManager.LayoutParams.TYPE_TOAST; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 81 | |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 82 | import static com.android.server.utils.PriorityDump.PRIORITY_ARG; |
| 83 | import static com.android.server.utils.PriorityDump.PRIORITY_ARG_CRITICAL; |
| 84 | import static com.android.server.utils.PriorityDump.PRIORITY_ARG_NORMAL; |
| 85 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 86 | import android.Manifest; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 87 | import android.annotation.NonNull; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 88 | import android.annotation.Nullable; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 89 | import android.app.ActivityManager; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 90 | import android.app.ActivityManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 91 | import android.app.AlarmManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 92 | import android.app.AppGlobals; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 93 | import android.app.AppOpsManager; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 94 | import android.app.AutomaticZenRule; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.app.IActivityManager; |
| 96 | import android.app.INotificationManager; |
| 97 | import android.app.ITransientNotification; |
| 98 | import android.app.Notification; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 99 | import android.app.NotificationChannel; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 100 | import android.app.NotificationChannelGroup; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 101 | import android.app.NotificationManager; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 102 | import android.app.NotificationManager.Policy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | import android.app.PendingIntent; |
| 104 | import android.app.StatusBarManager; |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 105 | import android.app.admin.DeviceAdminInfo; |
| 106 | import android.app.admin.DevicePolicyManagerInternal; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 107 | import android.app.backup.BackupManager; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 108 | import android.app.usage.UsageEvents; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 109 | import android.app.usage.UsageStatsManagerInternal; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 110 | import android.companion.ICompanionDeviceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | import android.content.BroadcastReceiver; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 112 | import android.content.ComponentName; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 113 | import android.content.ContentProvider; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 114 | import android.content.ContentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | import android.content.Context; |
| 116 | import android.content.Intent; |
| 117 | import android.content.IntentFilter; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 118 | import android.content.pm.ApplicationInfo; |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 119 | import android.content.pm.IPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | import android.content.pm.PackageManager; |
| 121 | import android.content.pm.PackageManager.NameNotFoundException; |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 122 | import android.content.pm.ParceledListSlice; |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 123 | import android.content.pm.UserInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | import android.content.res.Resources; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 125 | import android.database.ContentObserver; |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 126 | import android.media.AudioAttributes; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 127 | import android.media.AudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 128 | import android.media.AudioManagerInternal; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 129 | import android.media.IRingtonePlayer; |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 130 | import android.metrics.LogMaker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | import android.os.Binder; |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 133 | import android.os.Build; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 134 | import android.os.Bundle; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 135 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | import android.os.Handler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 137 | import android.os.HandlerThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | import android.os.IBinder; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 139 | import android.os.IDeviceIdleController; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 140 | import android.os.IInterface; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 141 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | import android.os.Message; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 143 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 144 | import android.os.RemoteException; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 145 | import android.os.ResultReceiver; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 146 | import android.os.ServiceManager; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 147 | import android.os.ShellCallback; |
| 148 | import android.os.ShellCommand; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 149 | import android.os.SystemClock; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 150 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 151 | import android.os.UserHandle; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 152 | import android.os.VibrationEffect; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 153 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | import android.provider.Settings; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 155 | import android.service.notification.Adjustment; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 156 | import android.service.notification.Condition; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 157 | import android.service.notification.IConditionProvider; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 158 | import android.service.notification.INotificationListener; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 159 | import android.service.notification.IStatusBarNotificationHolder; |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 160 | import android.service.notification.ListenersDisablingEffectsProto; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 161 | import android.service.notification.NotificationAssistantService; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 162 | import android.service.notification.NotificationListenerService; |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 163 | import android.service.notification.NotificationRankingUpdate; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 164 | import android.service.notification.NotificationRecordProto; |
| 165 | import android.service.notification.NotificationServiceDumpProto; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 166 | import android.service.notification.NotificationStats; |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 167 | import android.service.notification.NotifyingApp; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 168 | import android.service.notification.SnoozeCriterion; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 169 | import android.service.notification.StatusBarNotification; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 170 | import android.service.notification.ZenModeConfig; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 171 | import android.service.notification.ZenModeProto; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 172 | import android.telephony.PhoneStateListener; |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 173 | import android.telephony.TelephonyManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 174 | import android.text.TextUtils; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 175 | import android.util.ArrayMap; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 176 | import android.util.ArraySet; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 177 | import android.util.AtomicFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | import android.util.Log; |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 179 | import android.util.Slog; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 180 | import android.util.SparseArray; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 181 | import android.util.Xml; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 182 | import android.util.proto.ProtoOutputStream; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 183 | import android.view.accessibility.AccessibilityEvent; |
| 184 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | import android.widget.Toast; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 186 | |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 187 | import com.android.internal.R; |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 188 | import com.android.internal.annotations.GuardedBy; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 189 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 190 | import com.android.internal.logging.MetricsLogger; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 191 | import com.android.internal.logging.nano.MetricsProto; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 192 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Beverly | 70dcd00 | 2018-03-29 17:09:16 -0400 | [diff] [blame] | 193 | import com.android.internal.notification.SystemNotificationChannels; |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 194 | import com.android.internal.os.BackgroundThread; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 195 | import com.android.internal.statusbar.NotificationVisibility; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 196 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 197 | import com.android.internal.util.DumpUtils; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 198 | import com.android.internal.util.FastXmlSerializer; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 199 | import com.android.internal.util.Preconditions; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 200 | import com.android.internal.util.XmlUtils; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 201 | import com.android.server.DeviceIdleController; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 202 | import com.android.server.EventLogTags; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 203 | import com.android.server.LocalServices; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 204 | import com.android.server.SystemService; |
| 205 | import com.android.server.lights.Light; |
| 206 | import com.android.server.lights.LightsManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 207 | import com.android.server.notification.ManagedServices.ManagedServiceInfo; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 208 | import com.android.server.notification.ManagedServices.UserProfiles; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 209 | import com.android.server.policy.PhoneWindowManager; |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 210 | import com.android.server.statusbar.StatusBarManagerInternal; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 211 | import com.android.server.wm.WindowManagerInternal; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 212 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 213 | import libcore.io.IoUtils; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 214 | |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 215 | import org.json.JSONException; |
| 216 | import org.json.JSONObject; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 217 | import org.xmlpull.v1.XmlPullParser; |
| 218 | import org.xmlpull.v1.XmlPullParserException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 219 | import org.xmlpull.v1.XmlSerializer; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 220 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 221 | import java.io.ByteArrayInputStream; |
| 222 | import java.io.ByteArrayOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 223 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | import java.io.FileDescriptor; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 225 | import java.io.FileNotFoundException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 226 | import java.io.FileOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 227 | import java.io.IOException; |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 228 | import java.io.InputStream; |
| 229 | import java.io.OutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 231 | import java.nio.charset.StandardCharsets; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 232 | import java.util.ArrayDeque; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | import java.util.ArrayList; |
Michael Wright | bc4d0d9 | 2017-03-23 18:57:57 +0000 | [diff] [blame] | 234 | import java.util.Arrays; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 235 | import java.util.Iterator; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 236 | import java.util.List; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 237 | import java.util.Map.Entry; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 238 | import java.util.Objects; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 239 | import java.util.Set; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 240 | import java.util.concurrent.TimeUnit; |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 241 | import java.util.function.Predicate; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 242 | |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 243 | /** {@hide} */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 244 | public class NotificationManagerService extends SystemService { |
| 245 | static final String TAG = "NotificationService"; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 246 | static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Selim Cinek | 4041249 | 2015-12-08 18:03:22 -0800 | [diff] [blame] | 247 | public static final boolean ENABLE_CHILD_NOTIFICATIONS |
| 248 | = SystemProperties.getBoolean("debug.child_notifs", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 250 | static final boolean DEBUG_INTERRUPTIVENESS = SystemProperties.getBoolean( |
| 251 | "debug.notification.interruptiveness", false); |
| 252 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 253 | static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 254 | static final float DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE = 5f; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | // message codes |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 257 | static final int MESSAGE_DURATION_REACHED = 2; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 258 | static final int MESSAGE_SAVE_POLICY_FILE = 3; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 259 | static final int MESSAGE_SEND_RANKING_UPDATE = 4; |
| 260 | static final int MESSAGE_LISTENER_HINTS_CHANGED = 5; |
| 261 | static final int MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED = 6; |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 262 | static final int MESSAGE_FINISH_TOKEN_TIMEOUT = 7; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 263 | |
| 264 | // ranking thread messages |
| 265 | private static final int MESSAGE_RECONSIDER_RANKING = 1000; |
| 266 | private static final int MESSAGE_RANKING_SORT = 1001; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 268 | static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 269 | static final int SHORT_DELAY = 2000; // 2 seconds |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 270 | |
Robert Carr | 3406d46 | 2018-03-15 16:19:07 -0700 | [diff] [blame] | 271 | // 1 second past the ANR timeout. |
| 272 | static final int FINISH_TOKEN_TIMEOUT = 11 * 1000; |
| 273 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 274 | static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 275 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 276 | static final long SNOOZE_UNTIL_UNSPECIFIED = -1; |
| 277 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 278 | 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] | 279 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 280 | static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION; |
Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 281 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 282 | static final boolean ENABLE_BLOCKED_TOASTS = true; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 283 | |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 284 | // When #matchesCallFilter is called from the ringer, wait at most |
| 285 | // 3s to resolve the contacts. This timeout is required since |
| 286 | // ContactsProvider might take a long time to start up. |
| 287 | // |
| 288 | // Return STARRED_CONTACT when the timeout is hit in order to avoid |
| 289 | // missed calls in ZEN mode "Important". |
| 290 | static final int MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS = 3000; |
| 291 | static final float MATCHES_CALL_FILTER_TIMEOUT_AFFINITY = |
| 292 | ValidateNotificationPeople.STARRED_CONTACT; |
| 293 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 294 | /** notification_enqueue status value for a newly enqueued notification. */ |
| 295 | private static final int EVENTLOG_ENQUEUE_STATUS_NEW = 0; |
| 296 | |
| 297 | /** notification_enqueue status value for an existing notification. */ |
| 298 | private static final int EVENTLOG_ENQUEUE_STATUS_UPDATE = 1; |
| 299 | |
| 300 | /** notification_enqueue status value for an ignored notification. */ |
| 301 | private static final int EVENTLOG_ENQUEUE_STATUS_IGNORED = 2; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 302 | private static final long MIN_PACKAGE_OVERRATE_LOG_INTERVAL = 5000; // milliseconds |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 303 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 304 | private static final long DELAY_FOR_ASSISTANT_TIME = 100; |
| 305 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 306 | private static final String ACTION_NOTIFICATION_TIMEOUT = |
| 307 | NotificationManagerService.class.getSimpleName() + ".TIMEOUT"; |
| 308 | private static final int REQUEST_CODE_TIMEOUT = 1; |
| 309 | private static final String SCHEME_TIMEOUT = "timeout"; |
| 310 | private static final String EXTRA_KEY = "key"; |
| 311 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 312 | private IActivityManager mAm; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 313 | private ActivityManager mActivityManager; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 314 | private IPackageManager mPackageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 315 | private PackageManager mPackageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 316 | AudioManager mAudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 317 | AudioManagerInternal mAudioManagerInternal; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 318 | @Nullable StatusBarManagerInternal mStatusBar; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 319 | Vibrator mVibrator; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 320 | private WindowManagerInternal mWindowManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 321 | private AlarmManager mAlarmManager; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 322 | private ICompanionDeviceManager mCompanionManager; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 323 | private AccessibilityManager mAccessibilityManager; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 324 | private IDeviceIdleController mDeviceIdleController; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 325 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | final IBinder mForegroundToken = new Binder(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 327 | private WorkerHandler mHandler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 328 | private final HandlerThread mRankingThread = new HandlerThread("ranker", |
| 329 | Process.THREAD_PRIORITY_BACKGROUND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 331 | private Light mNotificationLight; |
| 332 | Light mAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 333 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 334 | private long[] mFallbackVibrationPattern; |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 335 | private boolean mUseAttentionLight; |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 336 | boolean mHasLight = true; |
| 337 | boolean mLightEnabled; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 338 | boolean mSystemReady; |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 339 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 340 | private boolean mDisableNotificationEffects; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 341 | private int mCallState; |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 342 | private String mSoundNotificationKey; |
| 343 | private String mVibrateNotificationKey; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 345 | private final SparseArray<ArraySet<ManagedServiceInfo>> mListenersDisablingEffects = |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 346 | new SparseArray<>(); |
| 347 | private List<ComponentName> mEffectsSuppressors = new ArrayList<>(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 348 | private int mListenerHints; // right now, all hints are global |
John Spurlock | 8310410 | 2015-02-12 23:25:12 -0500 | [diff] [blame] | 349 | private int mInterruptionFilter = NotificationListenerService.INTERRUPTION_FILTER_UNKNOWN; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 350 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 351 | // for enabling and disabling notification pulse behavior |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 352 | boolean mScreenOn = true; |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 353 | protected boolean mInCall = false; |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 354 | boolean mNotificationPulseEnabled; |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 355 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 356 | private Uri mInCallNotificationUri; |
| 357 | private AudioAttributes mInCallNotificationAudioAttributes; |
| 358 | private float mInCallNotificationVolume; |
luochaojiang | 50e5273c | 2018-04-16 16:55:03 +0800 | [diff] [blame] | 359 | private Binder mCallNotificationToken = null; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 360 | |
Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 361 | // used as a mutex for access to all active notifications & listeners |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 362 | final Object mNotificationLock = new Object(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 363 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 364 | final ArrayList<NotificationRecord> mNotificationList = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 365 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 366 | final ArrayMap<String, NotificationRecord> mNotificationsByKey = new ArrayMap<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 367 | @GuardedBy("mNotificationLock") |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 368 | final ArrayList<NotificationRecord> mEnqueuedNotifications = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 369 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 370 | final ArrayMap<Integer, ArrayMap<String, String>> mAutobundledSummaries = new ArrayMap<>(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 371 | final ArrayList<ToastRecord> mToastQueue = new ArrayList<>(); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 372 | final ArrayMap<String, NotificationRecord> mSummaryByGroupKey = new ArrayMap<>(); |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 373 | final ArrayMap<Integer, ArrayList<NotifyingApp>> mRecentApps = new ArrayMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 375 | // The last key in this list owns the hardware. |
| 376 | ArrayList<String> mLights = new ArrayList<>(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 377 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 378 | private AppOpsManager mAppOps; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 379 | private UsageStatsManagerInternal mAppUsageStats; |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 380 | private DevicePolicyManagerInternal mDpm; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 381 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 382 | private Archive mArchive; |
| 383 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 384 | // Persistent storage for notification policy |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 385 | private AtomicFile mPolicyFile; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 386 | |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 387 | private static final int DB_VERSION = 1; |
| 388 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 389 | private static final String TAG_NOTIFICATION_POLICY = "notification-policy"; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 390 | private static final String ATTR_VERSION = "version"; |
| 391 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 392 | private RankingHelper mRankingHelper; |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 393 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 394 | private final UserProfiles mUserProfiles = new UserProfiles(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 395 | private NotificationListeners mListeners; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 396 | private NotificationAssistants mAssistants; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 397 | private ConditionProviders mConditionProviders; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 398 | private NotificationUsageStats mUsageStats; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 399 | |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 400 | private static final int MY_UID = Process.myUid(); |
| 401 | private static final int MY_PID = Process.myPid(); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 402 | private static final IBinder WHITELIST_TOKEN = new Binder(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 403 | private RankingHandler mRankingHandler; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 404 | private long mLastOverRateLogTime; |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 405 | private float mMaxPackageEnqueueRate = DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE; |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 406 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 407 | private SnoozeHelper mSnoozeHelper; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 408 | private GroupHelper mGroupHelper; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 409 | private boolean mIsTelevision; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 410 | |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 411 | private MetricsLogger mMetricsLogger; |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 412 | private Predicate<String> mAllowedManagedServicePackages; |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 413 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 414 | private static class Archive { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 415 | final int mBufferSize; |
| 416 | final ArrayDeque<StatusBarNotification> mBuffer; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 417 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 418 | public Archive(int size) { |
| 419 | mBufferSize = size; |
| 420 | mBuffer = new ArrayDeque<StatusBarNotification>(mBufferSize); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 421 | } |
Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 422 | |
Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 423 | public String toString() { |
| 424 | final StringBuilder sb = new StringBuilder(); |
| 425 | final int N = mBuffer.size(); |
| 426 | sb.append("Archive ("); |
| 427 | sb.append(N); |
| 428 | sb.append(" notification"); |
| 429 | sb.append((N==1)?")":"s)"); |
| 430 | return sb.toString(); |
| 431 | } |
| 432 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 433 | public void record(StatusBarNotification nr) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 434 | if (mBuffer.size() == mBufferSize) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 435 | mBuffer.removeFirst(); |
| 436 | } |
Daniel Sandler | 26b81d5 | 2013-05-20 20:56:43 -0400 | [diff] [blame] | 437 | |
| 438 | // We don't want to store the heavy bits of the notification in the archive, |
| 439 | // but other clients in the system process might be using the object, so we |
| 440 | // store a (lightened) copy. |
| 441 | mBuffer.addLast(nr.cloneLight()); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 442 | } |
| 443 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 444 | public Iterator<StatusBarNotification> descendingIterator() { |
| 445 | return mBuffer.descendingIterator(); |
| 446 | } |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 447 | |
| 448 | public StatusBarNotification[] getArray(int count) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 449 | if (count == 0) count = mBufferSize; |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 450 | final StatusBarNotification[] a |
| 451 | = new StatusBarNotification[Math.min(count, mBuffer.size())]; |
| 452 | Iterator<StatusBarNotification> iter = descendingIterator(); |
| 453 | int i=0; |
| 454 | while (iter.hasNext() && i < count) { |
| 455 | a[i++] = iter.next(); |
| 456 | } |
| 457 | return a; |
| 458 | } |
| 459 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 460 | } |
| 461 | |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 462 | protected void readDefaultApprovedServices(int userId) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 463 | String defaultListenerAccess = getContext().getResources().getString( |
| 464 | com.android.internal.R.string.config_defaultListenerAccessPackages); |
| 465 | if (defaultListenerAccess != null) { |
| 466 | for (String whitelisted : |
| 467 | defaultListenerAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR)) { |
| 468 | // Gather all notification listener components for candidate pkgs. |
| 469 | Set<ComponentName> approvedListeners = |
| 470 | mListeners.queryPackageForServices(whitelisted, |
| 471 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 472 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId); |
| 473 | for (ComponentName cn : approvedListeners) { |
| 474 | try { |
| 475 | getBinderService().setNotificationListenerAccessGrantedForUser(cn, |
| 476 | userId, true); |
| 477 | } catch (RemoteException e) { |
| 478 | e.printStackTrace(); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 483 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 484 | String defaultDndAccess = getContext().getResources().getString( |
| 485 | com.android.internal.R.string.config_defaultDndAccessPackages); |
Edward Savage-Jones | 36a8942 | 2018-07-19 12:23:58 +0200 | [diff] [blame] | 486 | if (defaultDndAccess != null) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 487 | for (String whitelisted : |
| 488 | defaultDndAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR)) { |
| 489 | try { |
| 490 | getBinderService().setNotificationPolicyAccessGranted(whitelisted, true); |
| 491 | } catch (RemoteException e) { |
| 492 | e.printStackTrace(); |
| 493 | } |
| 494 | } |
| 495 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 496 | |
| 497 | readDefaultAssistant(userId); |
| 498 | } |
| 499 | |
| 500 | protected void readDefaultAssistant(int userId) { |
| 501 | String defaultAssistantAccess = getContext().getResources().getString( |
| 502 | com.android.internal.R.string.config_defaultAssistantAccessPackage); |
| 503 | if (defaultAssistantAccess != null) { |
| 504 | // Gather all notification assistant components for candidate pkg. There should |
| 505 | // only be one |
| 506 | Set<ComponentName> approvedAssistants = |
| 507 | mAssistants.queryPackageForServices(defaultAssistantAccess, |
| 508 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 509 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId); |
| 510 | for (ComponentName cn : approvedAssistants) { |
| 511 | try { |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 512 | getBinderService().setNotificationAssistantAccessGrantedForUser( |
| 513 | cn, userId, true); |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 514 | } catch (RemoteException e) { |
| 515 | e.printStackTrace(); |
| 516 | } |
| 517 | } |
| 518 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 519 | } |
| 520 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 521 | void readPolicyXml(InputStream stream, boolean forRestore) |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 522 | throws XmlPullParserException, NumberFormatException, IOException { |
| 523 | final XmlPullParser parser = Xml.newPullParser(); |
| 524 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 525 | XmlUtils.beginDocument(parser, TAG_NOTIFICATION_POLICY); |
| 526 | boolean migratedManagedServices = false; |
| 527 | int outerDepth = parser.getDepth(); |
| 528 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 529 | if (ZenModeConfig.ZEN_TAG.equals(parser.getName())) { |
| 530 | mZenModeHelper.readXml(parser, forRestore); |
| 531 | } else if (RankingHelper.TAG_RANKING.equals(parser.getName())){ |
| 532 | mRankingHelper.readXml(parser, forRestore); |
| 533 | } |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 534 | if (mListeners.getConfig().xmlTag.equals(parser.getName())) { |
| 535 | mListeners.readXml(parser, mAllowedManagedServicePackages); |
| 536 | migratedManagedServices = true; |
| 537 | } else if (mAssistants.getConfig().xmlTag.equals(parser.getName())) { |
| 538 | mAssistants.readXml(parser, mAllowedManagedServicePackages); |
| 539 | migratedManagedServices = true; |
| 540 | } else if (mConditionProviders.getConfig().xmlTag.equals(parser.getName())) { |
| 541 | mConditionProviders.readXml(parser, mAllowedManagedServicePackages); |
| 542 | migratedManagedServices = true; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 543 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 544 | } |
| 545 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 546 | if (!migratedManagedServices) { |
| 547 | mListeners.migrateToXml(); |
| 548 | mAssistants.migrateToXml(); |
| 549 | mConditionProviders.migrateToXml(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 550 | savePolicyFile(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 551 | } |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 552 | |
| 553 | mAssistants.ensureAssistant(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 554 | } |
| 555 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 556 | private void loadPolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 557 | if (DBG) Slog.d(TAG, "loadPolicyFile"); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 558 | synchronized (mPolicyFile) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 559 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 560 | InputStream infile = null; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 561 | try { |
| 562 | infile = mPolicyFile.openRead(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 563 | readPolicyXml(infile, false /*forRestore*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 564 | } catch (FileNotFoundException e) { |
| 565 | // No data yet |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 566 | // Load default managed services approvals |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 567 | readDefaultApprovedServices(USER_SYSTEM); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 568 | } catch (IOException e) { |
| 569 | Log.wtf(TAG, "Unable to read notification policy", e); |
| 570 | } catch (NumberFormatException e) { |
| 571 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 572 | } catch (XmlPullParserException e) { |
| 573 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 574 | } finally { |
| 575 | IoUtils.closeQuietly(infile); |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | public void savePolicyFile() { |
| 581 | mHandler.removeMessages(MESSAGE_SAVE_POLICY_FILE); |
| 582 | mHandler.sendEmptyMessage(MESSAGE_SAVE_POLICY_FILE); |
| 583 | } |
| 584 | |
| 585 | private void handleSavePolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 586 | if (DBG) Slog.d(TAG, "handleSavePolicyFile"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 587 | synchronized (mPolicyFile) { |
| 588 | final FileOutputStream stream; |
| 589 | try { |
| 590 | stream = mPolicyFile.startWrite(); |
| 591 | } catch (IOException e) { |
| 592 | Slog.w(TAG, "Failed to save policy file", e); |
| 593 | return; |
| 594 | } |
| 595 | |
| 596 | try { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 597 | writePolicyXml(stream, false /*forBackup*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 598 | mPolicyFile.finishWrite(stream); |
| 599 | } catch (IOException e) { |
| 600 | Slog.w(TAG, "Failed to save policy file, restoring backup", e); |
| 601 | mPolicyFile.failWrite(stream); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 602 | } |
| 603 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 604 | BackupManager.dataChanged(getContext().getPackageName()); |
| 605 | } |
| 606 | |
| 607 | private void writePolicyXml(OutputStream stream, boolean forBackup) throws IOException { |
| 608 | final XmlSerializer out = new FastXmlSerializer(); |
| 609 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
| 610 | out.startDocument(null, true); |
| 611 | out.startTag(null, TAG_NOTIFICATION_POLICY); |
| 612 | out.attribute(null, ATTR_VERSION, Integer.toString(DB_VERSION)); |
Beverly | 4e2f76c | 2018-03-16 15:43:49 -0400 | [diff] [blame] | 613 | mZenModeHelper.writeXml(out, forBackup, null); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 614 | mRankingHelper.writeXml(out, forBackup); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 615 | mListeners.writeXml(out, forBackup); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 616 | mAssistants.writeXml(out, forBackup); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 617 | mConditionProviders.writeXml(out, forBackup); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 618 | out.endTag(null, TAG_NOTIFICATION_POLICY); |
| 619 | out.endDocument(); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 620 | } |
| 621 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | private static final class ToastRecord |
| 623 | { |
| 624 | final int pid; |
| 625 | final String pkg; |
Beverly Tai | 98efc79 | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 626 | final ITransientNotification callback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | int duration; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 628 | Binder token; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 630 | ToastRecord(int pid, String pkg, ITransientNotification callback, int duration, |
| 631 | Binder token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | this.pid = pid; |
| 633 | this.pkg = pkg; |
| 634 | this.callback = callback; |
| 635 | this.duration = duration; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 636 | this.token = token; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | void update(int duration) { |
| 640 | this.duration = duration; |
| 641 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 642 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 643 | void dump(PrintWriter pw, String prefix, DumpFilter filter) { |
| 644 | if (filter != null && !filter.matches(pkg)) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | pw.println(prefix + this); |
| 646 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 647 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | @Override |
| 649 | public final String toString() |
| 650 | { |
| 651 | return "ToastRecord{" |
| 652 | + Integer.toHexString(System.identityHashCode(this)) |
| 653 | + " pkg=" + pkg |
| 654 | + " callback=" + callback |
| 655 | + " duration=" + duration; |
| 656 | } |
| 657 | } |
| 658 | |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 659 | @VisibleForTesting |
| 660 | final NotificationDelegate mNotificationDelegate = new NotificationDelegate() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 662 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | public void onSetDisabled(int status) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 664 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 665 | mDisableNotificationEffects = |
| 666 | (status & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 667 | if (disableNotificationEffects(null) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | // cancel whatever's going on |
| 669 | long identity = Binder.clearCallingIdentity(); |
| 670 | try { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 671 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 672 | if (player != null) { |
| 673 | player.stopAsync(); |
| 674 | } |
| 675 | } catch (RemoteException e) { |
| 676 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | Binder.restoreCallingIdentity(identity); |
| 678 | } |
| 679 | |
| 680 | identity = Binder.clearCallingIdentity(); |
| 681 | try { |
| 682 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 683 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | Binder.restoreCallingIdentity(identity); |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 690 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 691 | public void onClearAll(int callingUid, int callingPid, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 692 | synchronized (mNotificationLock) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 693 | cancelAllLocked(callingUid, callingPid, userId, REASON_CANCEL_ALL, null, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 694 | /*includeCurrentProfiles*/ true); |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 695 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | } |
| 697 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 698 | @Override |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 699 | public void onNotificationClick(int callingUid, int callingPid, String key, NotificationVisibility nv) { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 700 | exitIdle(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 701 | synchronized (mNotificationLock) { |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 702 | NotificationRecord r = mNotificationsByKey.get(key); |
| 703 | if (r == null) { |
| 704 | Log.w(TAG, "No notification with key: " + key); |
| 705 | return; |
| 706 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 707 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 708 | MetricsLogger.action(r.getLogMaker(now) |
| 709 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 710 | .setType(MetricsEvent.TYPE_ACTION) |
| 711 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, nv.rank) |
| 712 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, nv.count)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 713 | EventLogTags.writeNotificationClicked(key, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 714 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now), |
| 715 | nv.rank, nv.count); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 716 | |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 717 | StatusBarNotification sbn = r.sbn; |
| 718 | cancelNotification(callingUid, callingPid, sbn.getPackageName(), sbn.getTag(), |
| 719 | sbn.getId(), Notification.FLAG_AUTO_CANCEL, |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 720 | FLAG_FOREGROUND_SERVICE, false, r.getUserId(), |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 721 | REASON_CLICK, nv.rank, nv.count, null); |
| 722 | nv.recycle(); |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 723 | reportUserInteraction(r); |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 724 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 725 | } |
| 726 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 727 | @Override |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 728 | public void onNotificationActionClick(int callingUid, int callingPid, String key, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 729 | int actionIndex, NotificationVisibility nv) { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 730 | exitIdle(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 731 | synchronized (mNotificationLock) { |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 732 | NotificationRecord r = mNotificationsByKey.get(key); |
| 733 | if (r == null) { |
| 734 | Log.w(TAG, "No notification with key: " + key); |
| 735 | return; |
| 736 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 737 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 738 | MetricsLogger.action(r.getLogMaker(now) |
| 739 | .setCategory(MetricsEvent.NOTIFICATION_ITEM_ACTION) |
| 740 | .setType(MetricsEvent.TYPE_ACTION) |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 741 | .setSubtype(actionIndex) |
| 742 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, nv.rank) |
| 743 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, nv.count)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 744 | EventLogTags.writeNotificationActionClicked(key, actionIndex, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 745 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now), |
| 746 | nv.rank, nv.count); |
| 747 | nv.recycle(); |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 748 | reportUserInteraction(r); |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
| 752 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 753 | public void onNotificationClear(int callingUid, int callingPid, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 754 | String pkg, String tag, int id, int userId, String key, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 755 | @NotificationStats.DismissalSurface int dismissalSurface, |
| 756 | NotificationVisibility nv) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 757 | synchronized (mNotificationLock) { |
| 758 | NotificationRecord r = mNotificationsByKey.get(key); |
| 759 | if (r != null) { |
| 760 | r.recordDismissalSurface(dismissalSurface); |
| 761 | } |
| 762 | } |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 763 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 764 | Notification.FLAG_ONGOING_EVENT | FLAG_FOREGROUND_SERVICE, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 765 | true, userId, REASON_CANCEL, nv.rank, nv.count,null); |
| 766 | nv.recycle(); |
Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 767 | } |
| 768 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 769 | @Override |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 770 | public void onPanelRevealed(boolean clearEffects, int items) { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 771 | MetricsLogger.visible(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Chris Wren | 621933f | 2017-06-14 15:59:03 -0400 | [diff] [blame] | 772 | MetricsLogger.histogram(getContext(), "note_load", items); |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 773 | EventLogTags.writeNotificationPanelRevealed(items); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 774 | if (clearEffects) { |
| 775 | clearEffects(); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | @Override |
| 780 | public void onPanelHidden() { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 781 | MetricsLogger.hidden(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 782 | EventLogTags.writeNotificationPanelHidden(); |
| 783 | } |
| 784 | |
| 785 | @Override |
| 786 | public void clearEffects() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 787 | synchronized (mNotificationLock) { |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 788 | if (DBG) Slog.d(TAG, "clearEffects"); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 789 | clearSoundLocked(); |
| 790 | clearVibrateLocked(); |
| 791 | clearLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | } |
| 793 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 794 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 795 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 796 | public void onNotificationError(int callingUid, int callingPid, String pkg, String tag, int id, |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 797 | int uid, int initialPid, String message, int userId) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 798 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, 0, false, userId, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 799 | REASON_ERROR, null); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 800 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 801 | |
| 802 | @Override |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 803 | public void onNotificationVisibilityChanged(NotificationVisibility[] newlyVisibleKeys, |
| 804 | NotificationVisibility[] noLongerVisibleKeys) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 805 | synchronized (mNotificationLock) { |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 806 | for (NotificationVisibility nv : newlyVisibleKeys) { |
| 807 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 808 | if (r == null) continue; |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 809 | if (!r.isSeen()) { |
| 810 | // Report to usage stats that notification was made visible |
| 811 | if (DBG) Slog.d(TAG, "Marking notification as visible " + nv.key); |
| 812 | reportSeen(r); |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 813 | |
| 814 | // If the newly visible notification has smart replies |
| 815 | // then log that the user has seen them. |
| 816 | if (r.getNumSmartRepliesAdded() > 0 |
| 817 | && !r.hasSeenSmartReplies()) { |
| 818 | r.setSeenSmartReplies(true); |
| 819 | LogMaker logMaker = r.getLogMaker() |
| 820 | .setCategory(MetricsEvent.SMART_REPLY_VISIBLE) |
| 821 | .addTaggedData(MetricsEvent.NOTIFICATION_SMART_REPLY_COUNT, |
| 822 | r.getNumSmartRepliesAdded()); |
| 823 | mMetricsLogger.write(logMaker); |
| 824 | } |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 825 | } |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 826 | r.setVisibility(true, nv.rank, nv.count); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 827 | maybeRecordInterruptionLocked(r); |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 828 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 829 | } |
| 830 | // Note that we might receive this event after notifications |
| 831 | // have already left the system, e.g. after dismissing from the |
| 832 | // shade. Hence not finding notifications in |
| 833 | // mNotificationsByKey is not an exceptional condition. |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 834 | for (NotificationVisibility nv : noLongerVisibleKeys) { |
| 835 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 836 | if (r == null) continue; |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 837 | r.setVisibility(false, nv.rank, nv.count); |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 838 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 839 | } |
| 840 | } |
Christoph Studer | 92b389d | 2014-04-01 18:44:40 +0200 | [diff] [blame] | 841 | } |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 842 | |
| 843 | @Override |
| 844 | public void onNotificationExpansionChanged(String key, |
| 845 | boolean userAction, boolean expanded) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 846 | synchronized (mNotificationLock) { |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 847 | NotificationRecord r = mNotificationsByKey.get(key); |
| 848 | if (r != null) { |
| 849 | r.stats.onExpansionChanged(userAction, expanded); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 850 | final long now = System.currentTimeMillis(); |
Chris Wren | f734271 | 2017-09-14 10:55:55 -0400 | [diff] [blame] | 851 | if (userAction) { |
Chris Wren | 377ac6d | 2017-09-12 14:15:23 -0400 | [diff] [blame] | 852 | MetricsLogger.action(r.getLogMaker(now) |
| 853 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
Chris Wren | f734271 | 2017-09-14 10:55:55 -0400 | [diff] [blame] | 854 | .setType(expanded ? MetricsEvent.TYPE_DETAIL |
| 855 | : MetricsEvent.TYPE_COLLAPSE)); |
Chris Wren | 377ac6d | 2017-09-12 14:15:23 -0400 | [diff] [blame] | 856 | } |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 857 | if (expanded && userAction) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 858 | r.recordExpanded(); |
| 859 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 860 | EventLogTags.writeNotificationExpansion(key, |
| 861 | userAction ? 1 : 0, expanded ? 1 : 0, |
| 862 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 866 | |
| 867 | @Override |
| 868 | public void onNotificationDirectReplied(String key) { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 869 | exitIdle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 870 | synchronized (mNotificationLock) { |
| 871 | NotificationRecord r = mNotificationsByKey.get(key); |
| 872 | if (r != null) { |
| 873 | r.recordDirectReplied(); |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 874 | reportUserInteraction(r); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | @Override |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 880 | public void onNotificationSmartRepliesAdded(String key, int replyCount) { |
| 881 | synchronized (mNotificationLock) { |
| 882 | NotificationRecord r = mNotificationsByKey.get(key); |
| 883 | if (r != null) { |
| 884 | r.setNumSmartRepliesAdded(replyCount); |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | @Override |
| 890 | public void onNotificationSmartReplySent(String key, int replyIndex) { |
| 891 | synchronized (mNotificationLock) { |
| 892 | NotificationRecord r = mNotificationsByKey.get(key); |
| 893 | if (r != null) { |
| 894 | LogMaker logMaker = r.getLogMaker() |
| 895 | .setCategory(MetricsEvent.SMART_REPLY_ACTION) |
| 896 | .setSubtype(replyIndex); |
| 897 | mMetricsLogger.write(logMaker); |
| 898 | // Treat clicking on a smart reply as a user interaction. |
| 899 | reportUserInteraction(r); |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | @Override |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 905 | public void onNotificationSettingsViewed(String key) { |
| 906 | synchronized (mNotificationLock) { |
| 907 | NotificationRecord r = mNotificationsByKey.get(key); |
| 908 | if (r != null) { |
| 909 | r.recordViewedSettings(); |
| 910 | } |
| 911 | } |
| 912 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | }; |
| 914 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 915 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 916 | private void clearSoundLocked() { |
| 917 | mSoundNotificationKey = null; |
| 918 | long identity = Binder.clearCallingIdentity(); |
| 919 | try { |
| 920 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 921 | if (player != null) { |
| 922 | player.stopAsync(); |
| 923 | } |
| 924 | } catch (RemoteException e) { |
| 925 | } finally { |
| 926 | Binder.restoreCallingIdentity(identity); |
| 927 | } |
| 928 | } |
| 929 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 930 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 931 | private void clearVibrateLocked() { |
| 932 | mVibrateNotificationKey = null; |
| 933 | long identity = Binder.clearCallingIdentity(); |
| 934 | try { |
| 935 | mVibrator.cancel(); |
| 936 | } finally { |
| 937 | Binder.restoreCallingIdentity(identity); |
| 938 | } |
| 939 | } |
| 940 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 941 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 942 | private void clearLightsLocked() { |
| 943 | // light |
| 944 | mLights.clear(); |
| 945 | updateLightsLocked(); |
| 946 | } |
| 947 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 948 | protected final BroadcastReceiver mLocaleChangeReceiver = new BroadcastReceiver() { |
| 949 | @Override |
| 950 | public void onReceive(Context context, Intent intent) { |
| 951 | if (Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) { |
Beverly | 70dcd00 | 2018-03-29 17:09:16 -0400 | [diff] [blame] | 952 | // update system notification channels |
| 953 | SystemNotificationChannels.createAll(context); |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 954 | mZenModeHelper.updateDefaultZenRules(); |
Julia Reynolds | 816797a | 2017-08-11 15:47:09 -0400 | [diff] [blame] | 955 | mRankingHelper.onLocaleChanged(context, ActivityManager.getCurrentUser()); |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | }; |
| 959 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 960 | private final BroadcastReceiver mRestoreReceiver = new BroadcastReceiver() { |
| 961 | @Override |
| 962 | public void onReceive(Context context, Intent intent) { |
| 963 | if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) { |
| 964 | try { |
| 965 | String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME); |
| 966 | String newValue = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE); |
Michal Karpinski | 6135a26 | 2017-08-11 10:45:58 +0100 | [diff] [blame] | 967 | int restoredFromSdkInt = intent.getIntExtra( |
| 968 | Intent.EXTRA_SETTING_RESTORED_FROM_SDK_INT, 0); |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 969 | mListeners.onSettingRestored( |
| 970 | element, newValue, restoredFromSdkInt, getSendingUserId()); |
| 971 | mConditionProviders.onSettingRestored( |
| 972 | element, newValue, restoredFromSdkInt, getSendingUserId()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 973 | } catch (Exception e) { |
| 974 | Slog.wtf(TAG, "Cannot restore managed services from settings", e); |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | }; |
| 979 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 980 | private final BroadcastReceiver mNotificationTimeoutReceiver = new BroadcastReceiver() { |
| 981 | @Override |
| 982 | public void onReceive(Context context, Intent intent) { |
| 983 | String action = intent.getAction(); |
| 984 | if (action == null) { |
| 985 | return; |
| 986 | } |
| 987 | if (ACTION_NOTIFICATION_TIMEOUT.equals(action)) { |
| 988 | final NotificationRecord record; |
| 989 | synchronized (mNotificationLock) { |
| 990 | record = findNotificationByKeyLocked(intent.getStringExtra(EXTRA_KEY)); |
| 991 | } |
| 992 | if (record != null) { |
| 993 | cancelNotification(record.sbn.getUid(), record.sbn.getInitialPid(), |
| 994 | record.sbn.getPackageName(), record.sbn.getTag(), |
| 995 | record.sbn.getId(), 0, |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 996 | FLAG_FOREGROUND_SERVICE, true, record.getUserId(), |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 997 | REASON_TIMEOUT, null); |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | }; |
| 1002 | |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1003 | private final BroadcastReceiver mPackageIntentReceiver = new BroadcastReceiver() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | @Override |
| 1005 | public void onReceive(Context context, Intent intent) { |
| 1006 | String action = intent.getAction(); |
Dianne Hackborn | 29cd7f1 | 2015-01-08 10:37:05 -0800 | [diff] [blame] | 1007 | if (action == null) { |
| 1008 | return; |
| 1009 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1011 | boolean queryRestart = false; |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 1012 | boolean queryRemove = false; |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1013 | boolean packageChanged = false; |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 1014 | boolean cancelNotifications = true; |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1015 | boolean hideNotifications = false; |
| 1016 | boolean unhideNotifications = false; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1017 | int reason = REASON_PACKAGE_CHANGED; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1018 | |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1019 | if (action.equals(Intent.ACTION_PACKAGE_ADDED) |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 1020 | || (queryRemove=action.equals(Intent.ACTION_PACKAGE_REMOVED)) |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1021 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1022 | || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED)) |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1023 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1024 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE) |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1025 | || action.equals(Intent.ACTION_PACKAGES_SUSPENDED) |
| 1026 | || action.equals(Intent.ACTION_PACKAGES_UNSUSPENDED)) { |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1027 | int changeUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 1028 | UserHandle.USER_ALL); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1029 | String pkgList[] = null; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1030 | int uidList[] = null; |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 1031 | boolean removingPackage = queryRemove && |
| 1032 | !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 1033 | if (DBG) Slog.i(TAG, "action=" + action + " removing=" + removingPackage); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1034 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1035 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1036 | uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1037 | } else if (action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
| 1038 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1039 | cancelNotifications = false; |
| 1040 | hideNotifications = true; |
| 1041 | } else if (action.equals(Intent.ACTION_PACKAGES_UNSUSPENDED)) { |
| 1042 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 1043 | cancelNotifications = false; |
| 1044 | unhideNotifications = true; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1045 | } else if (queryRestart) { |
| 1046 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1047 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1048 | } else { |
| 1049 | Uri uri = intent.getData(); |
| 1050 | if (uri == null) { |
| 1051 | return; |
| 1052 | } |
| 1053 | String pkgName = uri.getSchemeSpecificPart(); |
| 1054 | if (pkgName == null) { |
| 1055 | return; |
| 1056 | } |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1057 | if (packageChanged) { |
| 1058 | // We cancel notifications for packages which have just been disabled |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 1059 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1060 | final int enabled = mPackageManager.getApplicationEnabledSetting( |
| 1061 | pkgName, |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1062 | changeUserId != UserHandle.USER_ALL ? changeUserId : |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1063 | USER_SYSTEM); |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 1064 | if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 1065 | || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 1066 | cancelNotifications = false; |
| 1067 | } |
| 1068 | } catch (IllegalArgumentException e) { |
| 1069 | // Package doesn't exist; probably racing with uninstall. |
| 1070 | // cancelNotifications is already true, so nothing to do here. |
| 1071 | if (DBG) { |
| 1072 | Slog.i(TAG, "Exception trying to look up app enabled setting", e); |
| 1073 | } |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1074 | } catch (RemoteException e) { |
| 1075 | // Failed to talk to PackageManagerService Should never happen! |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1076 | } |
| 1077 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1078 | pkgList = new String[]{pkgName}; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1079 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1081 | if (pkgList != null && (pkgList.length > 0)) { |
| 1082 | for (String pkgName : pkgList) { |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 1083 | if (cancelNotifications) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1084 | cancelAllNotificationsInt(MY_UID, MY_PID, pkgName, null, 0, 0, |
| 1085 | !queryRestart, changeUserId, reason, null); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1086 | } else if (hideNotifications) { |
| 1087 | hideNotificationsForPackages(pkgList); |
| 1088 | } else if (unhideNotifications) { |
| 1089 | unhideNotificationsForPackages(pkgList); |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 1090 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1091 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1092 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1094 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1095 | mListeners.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1096 | mAssistants.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1097 | mConditionProviders.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1098 | mRankingHelper.onPackagesChanged(removingPackage, changeUserId, pkgList, uidList); |
| 1099 | savePolicyFile(); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | }; |
| 1103 | |
| 1104 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 1105 | @Override |
| 1106 | public void onReceive(Context context, Intent intent) { |
| 1107 | String action = intent.getAction(); |
| 1108 | |
| 1109 | if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1110 | // Keep track of screen on/off state, but do not turn off the notification light |
| 1111 | // until user passes through the lock screen or views the notification. |
| 1112 | mScreenOn = true; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 1113 | updateNotificationPulse(); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1114 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 1115 | mScreenOn = false; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 1116 | updateNotificationPulse(); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1117 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
John Spurlock | 5d2eeb1 | 2014-01-16 10:46:36 -0500 | [diff] [blame] | 1118 | mInCall = TelephonyManager.EXTRA_STATE_OFFHOOK |
| 1119 | .equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE)); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1120 | updateNotificationPulse(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1121 | } else if (action.equals(Intent.ACTION_USER_STOPPED)) { |
| 1122 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 1123 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1124 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1125 | REASON_USER_STOPPED, null); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1126 | } |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1127 | } else if (action.equals(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) { |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 1128 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1129 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1130 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1131 | REASON_PROFILE_TURNED_OFF, null); |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 1132 | } |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1133 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { |
| 1134 | // turn off LED when user passes through lock screen |
| 1135 | mNotificationLight.turnOff(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1136 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1137 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1138 | // reload per-user settings |
| 1139 | mSettingsObserver.update(null); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1140 | mUserProfiles.updateCache(context); |
Christoph Studer | b53dfd4 | 2014-09-12 14:45:59 +0200 | [diff] [blame] | 1141 | // Refresh managed services |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 1142 | mConditionProviders.onUserSwitched(user); |
| 1143 | mListeners.onUserSwitched(user); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1144 | mAssistants.onUserSwitched(user); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1145 | mZenModeHelper.onUserSwitched(user); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1146 | } else if (action.equals(Intent.ACTION_USER_ADDED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1147 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
| 1148 | if (userId != USER_NULL) { |
| 1149 | mUserProfiles.updateCache(context); |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 1150 | if (!mUserProfiles.isManagedProfile(userId)) { |
| 1151 | readDefaultApprovedServices(userId); |
| 1152 | } |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1153 | } |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1154 | } else if (action.equals(Intent.ACTION_USER_REMOVED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1155 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1156 | mUserProfiles.updateCache(context); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1157 | mZenModeHelper.onUserRemoved(user); |
Julia Reynolds | 2e9bf5f | 2017-05-03 13:23:30 -0400 | [diff] [blame] | 1158 | mRankingHelper.onUserRemoved(user); |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 1159 | mListeners.onUserRemoved(user); |
| 1160 | mConditionProviders.onUserRemoved(user); |
| 1161 | mAssistants.onUserRemoved(user); |
Julia Reynolds | 2e9bf5f | 2017-05-03 13:23:30 -0400 | [diff] [blame] | 1162 | savePolicyFile(); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1163 | } else if (action.equals(Intent.ACTION_USER_UNLOCKED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1164 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1165 | mConditionProviders.onUserUnlocked(user); |
| 1166 | mListeners.onUserUnlocked(user); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1167 | mAssistants.onUserUnlocked(user); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1168 | mZenModeHelper.onUserUnlocked(user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1169 | } |
| 1170 | } |
| 1171 | }; |
| 1172 | |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 1173 | private final class SettingsObserver extends ContentObserver { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1174 | private final Uri NOTIFICATION_BADGING_URI |
| 1175 | = Settings.Secure.getUriFor(Settings.Secure.NOTIFICATION_BADGING); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1176 | private final Uri NOTIFICATION_LIGHT_PULSE_URI |
| 1177 | = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1178 | private final Uri NOTIFICATION_RATE_LIMIT_URI |
| 1179 | = Settings.Global.getUriFor(Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1180 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1181 | SettingsObserver(Handler handler) { |
| 1182 | super(handler); |
| 1183 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1184 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1185 | void observe() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1186 | ContentResolver resolver = getContext().getContentResolver(); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1187 | resolver.registerContentObserver(NOTIFICATION_BADGING_URI, |
| 1188 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1189 | resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1190 | false, this, UserHandle.USER_ALL); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1191 | resolver.registerContentObserver(NOTIFICATION_RATE_LIMIT_URI, |
| 1192 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1193 | update(null); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1196 | @Override public void onChange(boolean selfChange, Uri uri) { |
| 1197 | update(uri); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1200 | public void update(Uri uri) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1201 | ContentResolver resolver = getContext().getContentResolver(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1202 | if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { |
zhoulei | 7e37697 | 2017-05-17 18:41:25 +0800 | [diff] [blame] | 1203 | boolean pulseEnabled = Settings.System.getIntForUser(resolver, |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 1204 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0, UserHandle.USER_CURRENT) |
| 1205 | != 0; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1206 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 1207 | mNotificationPulseEnabled = pulseEnabled; |
| 1208 | updateNotificationPulse(); |
| 1209 | } |
| 1210 | } |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1211 | if (uri == null || NOTIFICATION_RATE_LIMIT_URI.equals(uri)) { |
| 1212 | mMaxPackageEnqueueRate = Settings.Global.getFloat(resolver, |
| 1213 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, mMaxPackageEnqueueRate); |
| 1214 | } |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1215 | if (uri == null || NOTIFICATION_BADGING_URI.equals(uri)) { |
| 1216 | mRankingHelper.updateBadgingEnabled(); |
| 1217 | } |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1218 | } |
| 1219 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1220 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1221 | private SettingsObserver mSettingsObserver; |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1222 | protected ZenModeHelper mZenModeHelper; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1223 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1224 | static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) { |
| 1225 | int[] ar = r.getIntArray(resid); |
| 1226 | if (ar == null) { |
| 1227 | return def; |
| 1228 | } |
| 1229 | final int len = ar.length > maxlen ? maxlen : ar.length; |
| 1230 | long[] out = new long[len]; |
| 1231 | for (int i=0; i<len; i++) { |
| 1232 | out[i] = ar[i]; |
| 1233 | } |
| 1234 | return out; |
| 1235 | } |
| 1236 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 1237 | public NotificationManagerService(Context context) { |
| 1238 | super(context); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 1239 | Notification.processWhitelistToken = WHITELIST_TOKEN; |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 1240 | } |
| 1241 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1242 | // TODO - replace these methods with a single VisibleForTesting constructor |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1243 | @VisibleForTesting |
| 1244 | void setAudioManager(AudioManager audioMananger) { |
| 1245 | mAudioManager = audioMananger; |
| 1246 | } |
| 1247 | |
| 1248 | @VisibleForTesting |
| 1249 | void setVibrator(Vibrator vibrator) { |
| 1250 | mVibrator = vibrator; |
| 1251 | } |
| 1252 | |
| 1253 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1254 | void setLights(Light light) { |
| 1255 | mNotificationLight = light; |
| 1256 | mAttentionLight = light; |
Julia Reynolds | 033a412 | 2017-01-31 16:50:38 -0500 | [diff] [blame] | 1257 | mNotificationPulseEnabled = true; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | @VisibleForTesting |
| 1261 | void setScreenOn(boolean on) { |
| 1262 | mScreenOn = on; |
| 1263 | } |
| 1264 | |
| 1265 | @VisibleForTesting |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1266 | int getNotificationRecordCount() { |
| 1267 | synchronized (mNotificationLock) { |
| 1268 | int count = mNotificationList.size() + mNotificationsByKey.size() |
| 1269 | + mSummaryByGroupKey.size() + mEnqueuedNotifications.size(); |
| 1270 | // subtract duplicates |
| 1271 | for (NotificationRecord posted : mNotificationList) { |
| 1272 | if (mNotificationsByKey.containsKey(posted.getKey())) { |
| 1273 | count--; |
| 1274 | } |
| 1275 | if (posted.sbn.isGroup() && posted.getNotification().isGroupSummary()) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1276 | count--; |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | return count; |
| 1281 | } |
| 1282 | } |
| 1283 | |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 1284 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1285 | void clearNotifications() { |
| 1286 | mEnqueuedNotifications.clear(); |
| 1287 | mNotificationList.clear(); |
| 1288 | mNotificationsByKey.clear(); |
| 1289 | mSummaryByGroupKey.clear(); |
| 1290 | } |
| 1291 | |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1292 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1293 | void addNotification(NotificationRecord r) { |
| 1294 | mNotificationList.add(r); |
| 1295 | mNotificationsByKey.put(r.sbn.getKey(), r); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1296 | if (r.sbn.isGroup()) { |
| 1297 | mSummaryByGroupKey.put(r.getGroupKey(), r); |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | @VisibleForTesting |
| 1302 | void addEnqueuedNotification(NotificationRecord r) { |
| 1303 | mEnqueuedNotifications.add(r); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | @VisibleForTesting |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1307 | NotificationRecord getNotificationRecord(String key) { |
| 1308 | return mNotificationsByKey.get(key); |
| 1309 | } |
| 1310 | |
| 1311 | |
| 1312 | @VisibleForTesting |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1313 | void setSystemReady(boolean systemReady) { |
| 1314 | mSystemReady = systemReady; |
| 1315 | } |
| 1316 | |
| 1317 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1318 | void setHandler(WorkerHandler handler) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1319 | mHandler = handler; |
| 1320 | } |
| 1321 | |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1322 | @VisibleForTesting |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 1323 | void setFallbackVibrationPattern(long[] vibrationPattern) { |
| 1324 | mFallbackVibrationPattern = vibrationPattern; |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1325 | } |
| 1326 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1327 | @VisibleForTesting |
| 1328 | void setPackageManager(IPackageManager packageManager) { |
| 1329 | mPackageManager = packageManager; |
| 1330 | } |
| 1331 | |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1332 | @VisibleForTesting |
| 1333 | void setRankingHelper(RankingHelper rankingHelper) { |
| 1334 | mRankingHelper = rankingHelper; |
| 1335 | } |
| 1336 | |
| 1337 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1338 | void setRankingHandler(RankingHandler rankingHandler) { |
| 1339 | mRankingHandler = rankingHandler; |
| 1340 | } |
| 1341 | |
| 1342 | @VisibleForTesting |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1343 | void setIsTelevision(boolean isTelevision) { |
| 1344 | mIsTelevision = isTelevision; |
| 1345 | } |
| 1346 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 1347 | @VisibleForTesting |
| 1348 | void setUsageStats(NotificationUsageStats us) { |
| 1349 | mUsageStats = us; |
| 1350 | } |
| 1351 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 1352 | @VisibleForTesting |
| 1353 | void setAccessibilityManager(AccessibilityManager am) { |
| 1354 | mAccessibilityManager = am; |
| 1355 | } |
| 1356 | |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1357 | // 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] | 1358 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1359 | void init(Looper looper, IPackageManager packageManager, |
| 1360 | PackageManager packageManagerClient, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1361 | LightsManager lightsManager, NotificationListeners notificationListeners, |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1362 | NotificationAssistants notificationAssistants, ConditionProviders conditionProviders, |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1363 | ICompanionDeviceManager companionManager, SnoozeHelper snoozeHelper, |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1364 | NotificationUsageStats usageStats, AtomicFile policyFile, |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 1365 | ActivityManager activityManager, GroupHelper groupHelper, IActivityManager am, |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 1366 | UsageStatsManagerInternal appUsageStats, DevicePolicyManagerInternal dpm) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1367 | Resources resources = getContext().getResources(); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1368 | mMaxPackageEnqueueRate = Settings.Global.getFloat(getContext().getContentResolver(), |
| 1369 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, |
| 1370 | DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE); |
| 1371 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 1372 | mAccessibilityManager = |
| 1373 | (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1374 | mAm = am; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1375 | mPackageManager = packageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1376 | mPackageManagerClient = packageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1377 | mAppOps = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE); |
| 1378 | mVibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE); |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 1379 | mAppUsageStats = appUsageStats; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1380 | mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1381 | mCompanionManager = companionManager; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1382 | mActivityManager = activityManager; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 1383 | mDeviceIdleController = IDeviceIdleController.Stub.asInterface( |
| 1384 | ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER)); |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 1385 | mDpm = dpm; |
| 1386 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1387 | mHandler = new WorkerHandler(looper); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1388 | mRankingThread.start(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1389 | String[] extractorNames; |
| 1390 | try { |
| 1391 | extractorNames = resources.getStringArray(R.array.config_notificationSignalExtractors); |
| 1392 | } catch (Resources.NotFoundException e) { |
| 1393 | extractorNames = new String[0]; |
| 1394 | } |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1395 | mUsageStats = usageStats; |
Kenny Guy | 2399110 | 2018-04-05 21:18:38 +0100 | [diff] [blame] | 1396 | mMetricsLogger = new MetricsLogger(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1397 | mRankingHandler = new RankingHandlerWorker(mRankingThread.getLooper()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1398 | mConditionProviders = conditionProviders; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1399 | mZenModeHelper = new ZenModeHelper(getContext(), mHandler.getLooper(), mConditionProviders); |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 1400 | mZenModeHelper.addCallback(new ZenModeHelper.Callback() { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1401 | @Override |
| 1402 | public void onConfigChanged() { |
| 1403 | savePolicyFile(); |
| 1404 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1405 | |
| 1406 | @Override |
| 1407 | void onZenModeChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1408 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED); |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1409 | getContext().sendBroadcastAsUser( |
Jason Monk | 6350674 | 2015-12-16 12:06:51 -0500 | [diff] [blame] | 1410 | new Intent(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL) |
| 1411 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT), |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1412 | UserHandle.ALL, android.Manifest.permission.MANAGE_NOTIFICATIONS); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1413 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1414 | updateInterruptionFilterLocked(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1415 | } |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 1416 | mRankingHandler.requestSort(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1417 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1418 | |
| 1419 | @Override |
| 1420 | void onPolicyChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1421 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED); |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 1422 | mRankingHandler.requestSort(); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1423 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1424 | }); |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 1425 | mRankingHelper = new RankingHelper(getContext(), |
| 1426 | mPackageManagerClient, |
| 1427 | mRankingHandler, |
| 1428 | mZenModeHelper, |
| 1429 | mUsageStats, |
| 1430 | extractorNames); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1431 | mSnoozeHelper = snoozeHelper; |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1432 | mGroupHelper = groupHelper; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 1433 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1434 | // This is a ManagedServices object that keeps track of the listeners. |
| 1435 | mListeners = notificationListeners; |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1436 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 1437 | // This is a MangedServices object that keeps track of the assistant. |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1438 | mAssistants = notificationAssistants; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1439 | |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 1440 | // Needs to be set before loadPolicyFile |
| 1441 | mAllowedManagedServicePackages = this::canUseManagedServices; |
| 1442 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1443 | mPolicyFile = policyFile; |
| 1444 | loadPolicyFile(); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1445 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1446 | mStatusBar = getLocalService(StatusBarManagerInternal.class); |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 1447 | if (mStatusBar != null) { |
| 1448 | mStatusBar.setNotificationDelegate(mNotificationDelegate); |
| 1449 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1451 | mNotificationLight = lightsManager.getLight(LightsManager.LIGHT_ID_NOTIFICATIONS); |
| 1452 | mAttentionLight = lightsManager.getLight(LightsManager.LIGHT_ID_ATTENTION); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1453 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1454 | mFallbackVibrationPattern = getLongArray(resources, |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 1455 | R.array.config_notificationFallbackVibePattern, |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1456 | VIBRATE_PATTERN_MAXLEN, |
| 1457 | DEFAULT_VIBRATE_PATTERN); |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 1458 | mInCallNotificationUri = Uri.parse("file://" + |
| 1459 | resources.getString(R.string.config_inCallNotificationSound)); |
| 1460 | mInCallNotificationAudioAttributes = new AudioAttributes.Builder() |
| 1461 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 1462 | .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 1463 | .build(); |
| 1464 | mInCallNotificationVolume = resources.getFloat(R.dimen.config_inCallNotificationVolume); |
| 1465 | |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 1466 | mUseAttentionLight = resources.getBoolean(R.bool.config_useAttentionLight); |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 1467 | mHasLight = |
| 1468 | resources.getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed); |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 1469 | |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1470 | // Don't start allowing notifications until the setup wizard has run once. |
| 1471 | // After that, including subsequent boots, init with notifications turned on. |
| 1472 | // This works on the first boot because the setup wizard will toggle this |
| 1473 | // 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] | 1474 | if (0 == Settings.Global.getInt(getContext().getContentResolver(), |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1475 | Settings.Global.DEVICE_PROVISIONED, 0)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1476 | mDisableNotificationEffects = true; |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1477 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1478 | mZenModeHelper.initZenMode(); |
John Spurlock | f370177 | 2015-02-12 13:29:37 -0500 | [diff] [blame] | 1479 | mInterruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1480 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1481 | mUserProfiles.updateCache(getContext()); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 1482 | listenForCallState(); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1483 | |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1484 | mSettingsObserver = new SettingsObserver(mHandler); |
| 1485 | |
| 1486 | mArchive = new Archive(resources.getInteger( |
| 1487 | R.integer.config_notificationServiceArchiveSize)); |
| 1488 | |
| 1489 | mIsTelevision = mPackageManagerClient.hasSystemFeature(FEATURE_LEANBACK) |
| 1490 | || mPackageManagerClient.hasSystemFeature(FEATURE_TELEVISION); |
| 1491 | } |
| 1492 | |
| 1493 | @Override |
| 1494 | public void onStart() { |
| 1495 | SnoozeHelper snoozeHelper = new SnoozeHelper(getContext(), new SnoozeHelper.Callback() { |
| 1496 | @Override |
| 1497 | public void repost(int userId, NotificationRecord r) { |
| 1498 | try { |
| 1499 | if (DBG) { |
| 1500 | Slog.d(TAG, "Reposting " + r.getKey()); |
| 1501 | } |
| 1502 | enqueueNotificationInternal(r.sbn.getPackageName(), r.sbn.getOpPkg(), |
| 1503 | r.sbn.getUid(), r.sbn.getInitialPid(), r.sbn.getTag(), r.sbn.getId(), |
| 1504 | r.sbn.getNotification(), userId); |
| 1505 | } catch (Exception e) { |
| 1506 | Slog.e(TAG, "Cannot un-snooze notification", e); |
| 1507 | } |
| 1508 | } |
| 1509 | }, mUserProfiles); |
| 1510 | |
| 1511 | final File systemDir = new File(Environment.getDataDirectory(), "system"); |
| 1512 | |
| 1513 | init(Looper.myLooper(), |
| 1514 | AppGlobals.getPackageManager(), getContext().getPackageManager(), |
| 1515 | getLocalService(LightsManager.class), |
| 1516 | new NotificationListeners(AppGlobals.getPackageManager()), |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 1517 | new NotificationAssistants(getContext(), mNotificationLock, mUserProfiles, |
| 1518 | AppGlobals.getPackageManager()), |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1519 | new ConditionProviders(getContext(), mUserProfiles, AppGlobals.getPackageManager()), |
| 1520 | null, snoozeHelper, new NotificationUsageStats(getContext()), |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 1521 | new AtomicFile(new File(systemDir, "notification_policy.xml"), "notification-policy"), |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1522 | (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE), |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 1523 | getGroupHelper(), ActivityManager.getService(), |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 1524 | LocalServices.getService(UsageStatsManagerInternal.class), |
| 1525 | LocalServices.getService(DevicePolicyManagerInternal.class)); |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1526 | |
Mike Lockwood | 35e16bf | 2010-11-30 19:53:36 -0500 | [diff] [blame] | 1527 | // register for various Intents |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1528 | IntentFilter filter = new IntentFilter(); |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1529 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 1530 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1531 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1532 | filter.addAction(Intent.ACTION_USER_PRESENT); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1533 | filter.addAction(Intent.ACTION_USER_STOPPED); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1534 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1535 | filter.addAction(Intent.ACTION_USER_ADDED); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1536 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1537 | filter.addAction(Intent.ACTION_USER_UNLOCKED); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1538 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1539 | getContext().registerReceiver(mIntentReceiver, filter); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1540 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1541 | IntentFilter pkgFilter = new IntentFilter(); |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1542 | pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1543 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
Daniel Sandler | aac0eb0 | 2011-08-06 22:51:56 -0400 | [diff] [blame] | 1544 | pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1545 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 1546 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 1547 | pkgFilter.addDataScheme("package"); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1548 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, pkgFilter, null, |
| 1549 | null); |
| 1550 | |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1551 | IntentFilter suspendedPkgFilter = new IntentFilter(); |
| 1552 | suspendedPkgFilter.addAction(Intent.ACTION_PACKAGES_SUSPENDED); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 1553 | suspendedPkgFilter.addAction(Intent.ACTION_PACKAGES_UNSUSPENDED); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1554 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, |
| 1555 | suspendedPkgFilter, null, null); |
| 1556 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1557 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1558 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, sdFilter, null, |
| 1559 | null); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1560 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1561 | IntentFilter timeoutFilter = new IntentFilter(ACTION_NOTIFICATION_TIMEOUT); |
| 1562 | timeoutFilter.addDataScheme(SCHEME_TIMEOUT); |
| 1563 | getContext().registerReceiver(mNotificationTimeoutReceiver, timeoutFilter); |
| 1564 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1565 | IntentFilter settingsRestoredFilter = new IntentFilter(Intent.ACTION_SETTING_RESTORED); |
| 1566 | getContext().registerReceiver(mRestoreReceiver, settingsRestoredFilter); |
| 1567 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1568 | IntentFilter localeChangedFilter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED); |
| 1569 | getContext().registerReceiver(mLocaleChangeReceiver, localeChangedFilter); |
| 1570 | |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 1571 | publishBinderService(Context.NOTIFICATION_SERVICE, mService, /* allowIsolated= */ false, |
| 1572 | DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PRIORITY_NORMAL); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1573 | publishLocalService(NotificationManagerInternal.class, mInternalService); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1574 | } |
| 1575 | |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1576 | private GroupHelper getGroupHelper() { |
| 1577 | return new GroupHelper(new GroupHelper.Callback() { |
| 1578 | @Override |
| 1579 | public void addAutoGroup(String key) { |
| 1580 | synchronized (mNotificationLock) { |
| 1581 | addAutogroupKeyLocked(key); |
| 1582 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | @Override |
| 1586 | public void removeAutoGroup(String key) { |
| 1587 | synchronized (mNotificationLock) { |
| 1588 | removeAutogroupKeyLocked(key); |
| 1589 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | @Override |
| 1593 | public void addAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 1594 | createAutoGroupSummary(userId, pkg, triggeringKey); |
| 1595 | } |
| 1596 | |
| 1597 | @Override |
| 1598 | public void removeAutoGroupSummary(int userId, String pkg) { |
| 1599 | synchronized (mNotificationLock) { |
| 1600 | clearAutogroupSummaryLocked(userId, pkg); |
| 1601 | } |
| 1602 | } |
| 1603 | }); |
| 1604 | } |
| 1605 | |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1606 | private void sendRegisteredOnlyBroadcast(String action) { |
| 1607 | getContext().sendBroadcastAsUser(new Intent(action) |
| 1608 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), UserHandle.ALL, null); |
| 1609 | } |
| 1610 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1611 | @Override |
| 1612 | public void onBootPhase(int phase) { |
| 1613 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 1614 | // no beeping until we're basically done booting |
| 1615 | mSystemReady = true; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1616 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1617 | // Grab our optional AudioService |
| 1618 | mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1619 | mAudioManagerInternal = getLocalService(AudioManagerInternal.class); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1620 | mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1621 | mZenModeHelper.onSystemReady(); |
Adam Lesinski | a6db4ab | 2014-03-24 12:31:45 -0700 | [diff] [blame] | 1622 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 1623 | // This observer will force an update when observe is called, causing us to |
| 1624 | // bind to listener services. |
| 1625 | mSettingsObserver.observe(); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1626 | mListeners.onBootPhaseAppsCanStart(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1627 | mAssistants.onBootPhaseAppsCanStart(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1628 | mConditionProviders.onBootPhaseAppsCanStart(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1629 | } |
| 1630 | } |
| 1631 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1632 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1633 | private void updateListenerHintsLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1634 | final int hints = calculateHints(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1635 | if (hints == mListenerHints) return; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1636 | ZenLog.traceListenerHintsChanged(mListenerHints, hints, mEffectsSuppressors.size()); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1637 | mListenerHints = hints; |
| 1638 | scheduleListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 1639 | } |
| 1640 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1641 | @GuardedBy("mNotificationLock") |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1642 | private void updateEffectsSuppressorLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1643 | final long updatedSuppressedEffects = calculateSuppressedEffects(); |
| 1644 | if (updatedSuppressedEffects == mZenModeHelper.getSuppressedEffects()) return; |
| 1645 | final List<ComponentName> suppressors = getSuppressors(); |
| 1646 | ZenLog.traceEffectsSuppressorChanged(mEffectsSuppressors, suppressors, updatedSuppressedEffects); |
| 1647 | mEffectsSuppressors = suppressors; |
| 1648 | mZenModeHelper.setSuppressedEffects(updatedSuppressedEffects); |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1649 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1650 | } |
| 1651 | |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 1652 | private void exitIdle() { |
| 1653 | try { |
| 1654 | if (mDeviceIdleController != null) { |
| 1655 | mDeviceIdleController.exitIdle("notification interaction"); |
| 1656 | } |
| 1657 | } catch (RemoteException e) { |
| 1658 | } |
| 1659 | } |
| 1660 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1661 | private void updateNotificationChannelInt(String pkg, int uid, NotificationChannel channel, |
| 1662 | boolean fromListener) { |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1663 | if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) { |
| 1664 | // cancel |
| 1665 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channel.getId(), 0, 0, true, |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1666 | UserHandle.getUserId(uid), REASON_CHANNEL_BANNED, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1667 | null); |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1668 | if (isUidSystemOrPhone(uid)) { |
| 1669 | int[] profileIds = mUserProfiles.getCurrentProfileIds(); |
| 1670 | int N = profileIds.length; |
| 1671 | for (int i = 0; i < N; i++) { |
| 1672 | int profileId = profileIds[i]; |
| 1673 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channel.getId(), 0, 0, true, |
| 1674 | profileId, REASON_CHANNEL_BANNED, |
| 1675 | null); |
| 1676 | } |
| 1677 | } |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1678 | } |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1679 | final NotificationChannel preUpdate = |
| 1680 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), true); |
| 1681 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1682 | mRankingHelper.updateNotificationChannel(pkg, uid, channel, true); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1683 | maybeNotifyChannelOwner(pkg, uid, preUpdate, channel); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1684 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1685 | if (!fromListener) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1686 | final NotificationChannel modifiedChannel = |
| 1687 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), false); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1688 | mListeners.notifyNotificationChannelChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1689 | pkg, UserHandle.getUserHandleForUid(uid), |
| 1690 | modifiedChannel, NOTIFICATION_CHANNEL_OR_GROUP_UPDATED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1691 | } |
| 1692 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1693 | savePolicyFile(); |
| 1694 | } |
| 1695 | |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1696 | private void maybeNotifyChannelOwner(String pkg, int uid, NotificationChannel preUpdate, |
| 1697 | NotificationChannel update) { |
| 1698 | try { |
| 1699 | if ((preUpdate.getImportance() == IMPORTANCE_NONE |
| 1700 | && update.getImportance() != IMPORTANCE_NONE) |
| 1701 | || (preUpdate.getImportance() != IMPORTANCE_NONE |
| 1702 | && update.getImportance() == IMPORTANCE_NONE)) { |
| 1703 | getContext().sendBroadcastAsUser( |
| 1704 | new Intent(ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED) |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 1705 | .putExtra(NotificationManager.EXTRA_NOTIFICATION_CHANNEL_ID, |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1706 | update.getId()) |
| 1707 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, |
| 1708 | update.getImportance() == IMPORTANCE_NONE) |
| 1709 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 1710 | .setPackage(pkg), |
| 1711 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 1712 | } |
| 1713 | } catch (SecurityException e) { |
| 1714 | Slog.w(TAG, "Can't notify app about channel change", e); |
| 1715 | } |
| 1716 | } |
| 1717 | |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1718 | private void createNotificationChannelGroup(String pkg, int uid, NotificationChannelGroup group, |
| 1719 | boolean fromApp, boolean fromListener) { |
| 1720 | Preconditions.checkNotNull(group); |
| 1721 | Preconditions.checkNotNull(pkg); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1722 | |
| 1723 | final NotificationChannelGroup preUpdate = |
| 1724 | mRankingHelper.getNotificationChannelGroup(group.getId(), pkg, uid); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1725 | mRankingHelper.createNotificationChannelGroup(pkg, uid, group, |
| 1726 | fromApp); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1727 | if (!fromApp) { |
| 1728 | maybeNotifyChannelGroupOwner(pkg, uid, preUpdate, group); |
| 1729 | } |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1730 | if (!fromListener) { |
| 1731 | mListeners.notifyNotificationChannelGroupChanged(pkg, |
| 1732 | UserHandle.of(UserHandle.getCallingUserId()), group, |
| 1733 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
| 1734 | } |
| 1735 | } |
| 1736 | |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1737 | private void maybeNotifyChannelGroupOwner(String pkg, int uid, |
| 1738 | NotificationChannelGroup preUpdate, NotificationChannelGroup update) { |
| 1739 | try { |
| 1740 | if (preUpdate.isBlocked() != update.isBlocked()) { |
| 1741 | getContext().sendBroadcastAsUser( |
| 1742 | new Intent(ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED) |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 1743 | .putExtra(NotificationManager.EXTRA_NOTIFICATION_CHANNEL_GROUP_ID, |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1744 | update.getId()) |
| 1745 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, |
| 1746 | update.isBlocked()) |
| 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 group change", e); |
| 1753 | } |
| 1754 | } |
| 1755 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1756 | private ArrayList<ComponentName> getSuppressors() { |
| 1757 | ArrayList<ComponentName> names = new ArrayList<ComponentName>(); |
| 1758 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1759 | ArraySet<ManagedServiceInfo> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
| 1760 | |
| 1761 | for (ManagedServiceInfo info : serviceInfoList) { |
| 1762 | names.add(info.component); |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | return names; |
| 1767 | } |
| 1768 | |
| 1769 | private boolean removeDisabledHints(ManagedServiceInfo info) { |
| 1770 | return removeDisabledHints(info, 0); |
| 1771 | } |
| 1772 | |
| 1773 | private boolean removeDisabledHints(ManagedServiceInfo info, int hints) { |
| 1774 | boolean removed = false; |
| 1775 | |
| 1776 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1777 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 1778 | final ArraySet<ManagedServiceInfo> listeners = |
| 1779 | mListenersDisablingEffects.valueAt(i); |
| 1780 | |
| 1781 | if (hints == 0 || (hint & hints) == hint) { |
| 1782 | removed = removed || listeners.remove(info); |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | return removed; |
| 1787 | } |
| 1788 | |
| 1789 | private void addDisabledHints(ManagedServiceInfo info, int hints) { |
| 1790 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1791 | addDisabledHint(info, HINT_HOST_DISABLE_EFFECTS); |
| 1792 | } |
| 1793 | |
| 1794 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1795 | addDisabledHint(info, HINT_HOST_DISABLE_NOTIFICATION_EFFECTS); |
| 1796 | } |
| 1797 | |
| 1798 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1799 | addDisabledHint(info, HINT_HOST_DISABLE_CALL_EFFECTS); |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | private void addDisabledHint(ManagedServiceInfo info, int hint) { |
| 1804 | if (mListenersDisablingEffects.indexOfKey(hint) < 0) { |
| 1805 | mListenersDisablingEffects.put(hint, new ArraySet<ManagedServiceInfo>()); |
| 1806 | } |
| 1807 | |
| 1808 | ArraySet<ManagedServiceInfo> hintListeners = mListenersDisablingEffects.get(hint); |
| 1809 | hintListeners.add(info); |
| 1810 | } |
| 1811 | |
| 1812 | private int calculateHints() { |
| 1813 | int hints = 0; |
| 1814 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1815 | int hint = mListenersDisablingEffects.keyAt(i); |
| 1816 | ArraySet<ManagedServiceInfo> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
| 1817 | |
| 1818 | if (!serviceInfoList.isEmpty()) { |
| 1819 | hints |= hint; |
| 1820 | } |
| 1821 | } |
| 1822 | |
| 1823 | return hints; |
| 1824 | } |
| 1825 | |
| 1826 | private long calculateSuppressedEffects() { |
| 1827 | int hints = calculateHints(); |
| 1828 | long suppressedEffects = 0; |
| 1829 | |
| 1830 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1831 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_ALL; |
| 1832 | } |
| 1833 | |
| 1834 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1835 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_NOTIFICATIONS; |
| 1836 | } |
| 1837 | |
| 1838 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1839 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_CALLS; |
| 1840 | } |
| 1841 | |
| 1842 | return suppressedEffects; |
| 1843 | } |
| 1844 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1845 | @GuardedBy("mNotificationLock") |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1846 | private void updateInterruptionFilterLocked() { |
| 1847 | int interruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
| 1848 | if (interruptionFilter == mInterruptionFilter) return; |
| 1849 | mInterruptionFilter = interruptionFilter; |
| 1850 | scheduleInterruptionFilterChanged(interruptionFilter); |
| 1851 | } |
| 1852 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1853 | @VisibleForTesting |
| 1854 | INotificationManager getBinderService() { |
| 1855 | return INotificationManager.Stub.asInterface(mService); |
| 1856 | } |
| 1857 | |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1858 | /** |
| 1859 | * Report to usage stats that the notification was seen. |
| 1860 | * @param r notification record |
| 1861 | */ |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1862 | @GuardedBy("mNotificationLock") |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 1863 | protected void reportSeen(NotificationRecord r) { |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 1864 | mAppUsageStats.reportEvent(r.sbn.getPackageName(), |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1865 | getRealUserId(r.sbn.getUserId()), |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 1866 | UsageEvents.Event.NOTIFICATION_SEEN); |
| 1867 | } |
| 1868 | |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 1869 | protected int calculateSuppressedVisualEffects(Policy incomingPolicy, Policy currPolicy, |
| 1870 | int targetSdkVersion) { |
| 1871 | if (incomingPolicy.suppressedVisualEffects == SUPPRESSED_EFFECTS_UNSET) { |
| 1872 | return incomingPolicy.suppressedVisualEffects; |
| 1873 | } |
| 1874 | final int[] effectsIntroducedInP = { |
| 1875 | SUPPRESSED_EFFECT_FULL_SCREEN_INTENT, |
| 1876 | SUPPRESSED_EFFECT_LIGHTS, |
| 1877 | SUPPRESSED_EFFECT_PEEK, |
| 1878 | SUPPRESSED_EFFECT_STATUS_BAR, |
| 1879 | SUPPRESSED_EFFECT_BADGE, |
| 1880 | SUPPRESSED_EFFECT_AMBIENT, |
| 1881 | SUPPRESSED_EFFECT_NOTIFICATION_LIST |
| 1882 | }; |
| 1883 | |
| 1884 | int newSuppressedVisualEffects = incomingPolicy.suppressedVisualEffects; |
Jeff Sharkey | aa1a911 | 2018-04-10 15:18:12 -0600 | [diff] [blame] | 1885 | if (targetSdkVersion < Build.VERSION_CODES.P) { |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 1886 | // unset higher order bits introduced in P, maintain the user's higher order bits |
| 1887 | for (int i = 0; i < effectsIntroducedInP.length ; i++) { |
| 1888 | newSuppressedVisualEffects &= ~effectsIntroducedInP[i]; |
| 1889 | newSuppressedVisualEffects |= |
| 1890 | (currPolicy.suppressedVisualEffects & effectsIntroducedInP[i]); |
| 1891 | } |
| 1892 | // set higher order bits according to lower order bits |
| 1893 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_OFF) != 0) { |
| 1894 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_LIGHTS; |
| 1895 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 1896 | } |
| 1897 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_ON) != 0) { |
| 1898 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_PEEK; |
| 1899 | } |
| 1900 | } else { |
| 1901 | boolean hasNewEffects = (newSuppressedVisualEffects |
| 1902 | - SUPPRESSED_EFFECT_SCREEN_ON - SUPPRESSED_EFFECT_SCREEN_OFF) > 0; |
| 1903 | // if any of the new effects introduced in P are set |
| 1904 | if (hasNewEffects) { |
| 1905 | // clear out the deprecated effects |
| 1906 | newSuppressedVisualEffects &= ~ (SUPPRESSED_EFFECT_SCREEN_ON |
| 1907 | | SUPPRESSED_EFFECT_SCREEN_OFF); |
| 1908 | |
| 1909 | // set the deprecated effects according to the new more specific effects |
| 1910 | if ((newSuppressedVisualEffects & Policy.SUPPRESSED_EFFECT_PEEK) != 0) { |
| 1911 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_SCREEN_ON; |
| 1912 | } |
| 1913 | if ((newSuppressedVisualEffects & Policy.SUPPRESSED_EFFECT_LIGHTS) != 0 |
| 1914 | && (newSuppressedVisualEffects |
| 1915 | & Policy.SUPPRESSED_EFFECT_FULL_SCREEN_INTENT) != 0 |
| 1916 | && (newSuppressedVisualEffects |
| 1917 | & Policy.SUPPRESSED_EFFECT_AMBIENT) != 0) { |
| 1918 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_SCREEN_OFF; |
| 1919 | } |
| 1920 | } else { |
| 1921 | // set higher order bits according to lower order bits |
| 1922 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_OFF) != 0) { |
| 1923 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_LIGHTS; |
| 1924 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_FULL_SCREEN_INTENT; |
| 1925 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_AMBIENT; |
| 1926 | } |
| 1927 | if ((newSuppressedVisualEffects & SUPPRESSED_EFFECT_SCREEN_ON) != 0) { |
| 1928 | newSuppressedVisualEffects |= SUPPRESSED_EFFECT_PEEK; |
| 1929 | } |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | return newSuppressedVisualEffects; |
| 1934 | } |
| 1935 | |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1936 | @GuardedBy("mNotificationLock") |
| 1937 | protected void maybeRecordInterruptionLocked(NotificationRecord r) { |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 1938 | if (r.isInterruptive() && !r.hasRecordedInterruption()) { |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1939 | mAppUsageStats.reportInterruptiveNotification(r.sbn.getPackageName(), |
| 1940 | r.getChannel().getId(), |
| 1941 | getRealUserId(r.sbn.getUserId())); |
Julia Reynolds | ad7d713 | 2018-03-21 16:05:00 -0400 | [diff] [blame] | 1942 | logRecentLocked(r); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 1943 | r.setRecordedInterruption(true); |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1944 | } |
| 1945 | } |
| 1946 | |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1947 | /** |
| 1948 | * Report to usage stats that the notification was clicked. |
| 1949 | * @param r notification record |
| 1950 | */ |
| 1951 | protected void reportUserInteraction(NotificationRecord r) { |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1952 | mAppUsageStats.reportEvent(r.sbn.getPackageName(), |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1953 | getRealUserId(r.sbn.getUserId()), |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1954 | UsageEvents.Event.USER_INTERACTION); |
| 1955 | } |
| 1956 | |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 1957 | private int getRealUserId(int userId) { |
| 1958 | return userId == UserHandle.USER_ALL ? UserHandle.USER_SYSTEM : userId; |
| 1959 | } |
| 1960 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1961 | @VisibleForTesting |
| 1962 | NotificationManagerInternal getInternalService() { |
| 1963 | return mInternalService; |
| 1964 | } |
| 1965 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1966 | private final IBinder mService = new INotificationManager.Stub() { |
| 1967 | // Toasts |
| 1968 | // ============================================================================ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1969 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1970 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1971 | public void enqueueToast(String pkg, ITransientNotification callback, int duration) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1972 | { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1973 | if (DBG) { |
| 1974 | Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback |
| 1975 | + " duration=" + duration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1976 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1977 | |
| 1978 | if (pkg == null || callback == null) { |
| 1979 | Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback); |
| 1980 | return ; |
| 1981 | } |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1982 | final boolean isSystemToast = isCallerSystemOrPhone() || ("android".equals(pkg)); |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 1983 | final boolean isPackageSuspended = |
| 1984 | isPackageSuspendedForUser(pkg, Binder.getCallingUid()); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1985 | |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 1986 | if (ENABLE_BLOCKED_TOASTS && !isSystemToast && |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1987 | (!areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()) |
| 1988 | || isPackageSuspended)) { |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 1989 | Slog.e(TAG, "Suppressing toast from package " + pkg |
| 1990 | + (isPackageSuspended |
| 1991 | ? " due to package suspended by administrator." |
| 1992 | : " by user request.")); |
| 1993 | return; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | synchronized (mToastQueue) { |
| 1997 | int callingPid = Binder.getCallingPid(); |
| 1998 | long callingId = Binder.clearCallingIdentity(); |
| 1999 | try { |
| 2000 | ToastRecord record; |
Beverly Tai | 98efc79 | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 2001 | int index = indexOfToastLocked(pkg, callback); |
| 2002 | // If it's already in the queue, we update it in place, we don't |
| 2003 | // move it to the end of the queue. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2004 | if (index >= 0) { |
| 2005 | record = mToastQueue.get(index); |
| 2006 | record.update(duration); |
| 2007 | } else { |
Beverly Tai | 98efc79 | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 2008 | // Limit the number of toasts that any given package except the android |
| 2009 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| 2010 | if (!isSystemToast) { |
| 2011 | int count = 0; |
| 2012 | final int N = mToastQueue.size(); |
| 2013 | for (int i=0; i<N; i++) { |
| 2014 | final ToastRecord r = mToastQueue.get(i); |
| 2015 | if (r.pkg.equals(pkg)) { |
| 2016 | count++; |
| 2017 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 2018 | Slog.e(TAG, "Package has already posted " + count |
| 2019 | + " toasts. Not showing more. Package=" + pkg); |
| 2020 | return; |
| 2021 | } |
| 2022 | } |
| 2023 | } |
| 2024 | } |
| 2025 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 2026 | Binder token = new Binder(); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 2027 | mWindowManagerInternal.addWindowToken(token, TYPE_TOAST, DEFAULT_DISPLAY); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 2028 | record = new ToastRecord(callingPid, pkg, callback, duration, token); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2029 | mToastQueue.add(record); |
| 2030 | index = mToastQueue.size() - 1; |
Beverly Tai | 98efc79 | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 2031 | keepProcessAliveIfNeededLocked(callingPid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2032 | } |
| 2033 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 2034 | // new or just been updated. Call back and tell it to show itself. |
| 2035 | // If the callback fails, this will remove it from the list, so don't |
| 2036 | // assume that it's valid after this. |
| 2037 | if (index == 0) { |
| 2038 | showNextToastLocked(); |
| 2039 | } |
| 2040 | } finally { |
| 2041 | Binder.restoreCallingIdentity(callingId); |
| 2042 | } |
| 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | @Override |
| 2047 | public void cancelToast(String pkg, ITransientNotification callback) { |
| 2048 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| 2049 | |
| 2050 | if (pkg == null || callback == null) { |
| 2051 | Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback); |
| 2052 | return ; |
| 2053 | } |
| 2054 | |
| 2055 | synchronized (mToastQueue) { |
| 2056 | long callingId = Binder.clearCallingIdentity(); |
| 2057 | try { |
| 2058 | int index = indexOfToastLocked(pkg, callback); |
| 2059 | if (index >= 0) { |
| 2060 | cancelToastLocked(index); |
| 2061 | } else { |
| 2062 | Slog.w(TAG, "Toast already cancelled. pkg=" + pkg |
| 2063 | + " callback=" + callback); |
| 2064 | } |
| 2065 | } finally { |
| 2066 | Binder.restoreCallingIdentity(callingId); |
| 2067 | } |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | @Override |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 2072 | public void finishToken(String pkg, ITransientNotification callback) { |
| 2073 | synchronized (mToastQueue) { |
| 2074 | long callingId = Binder.clearCallingIdentity(); |
| 2075 | try { |
| 2076 | int index = indexOfToastLocked(pkg, callback); |
| 2077 | if (index >= 0) { |
| 2078 | ToastRecord record = mToastQueue.get(index); |
| 2079 | finishTokenLocked(record.token); |
| 2080 | } else { |
| 2081 | Slog.w(TAG, "Toast already killed. pkg=" + pkg |
| 2082 | + " callback=" + callback); |
| 2083 | } |
| 2084 | } finally { |
| 2085 | Binder.restoreCallingIdentity(callingId); |
| 2086 | } |
| 2087 | } |
| 2088 | } |
| 2089 | |
| 2090 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 2091 | public void enqueueNotificationWithTag(String pkg, String opPkg, String tag, int id, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 2092 | Notification notification, int userId) throws RemoteException { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 2093 | enqueueNotificationInternal(pkg, opPkg, Binder.getCallingUid(), |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 2094 | Binder.getCallingPid(), tag, id, notification, userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | @Override |
| 2098 | public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2099 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2100 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2101 | Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2102 | // Don't allow client applications to cancel foreground service notis or autobundled |
| 2103 | // summaries. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 2104 | final int mustNotHaveFlags = isCallingUidSystem() ? 0 : |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 2105 | (FLAG_FOREGROUND_SERVICE | Notification.FLAG_AUTOGROUP_SUMMARY); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2106 | cancelNotification(Binder.getCallingUid(), Binder.getCallingPid(), pkg, tag, id, 0, |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 2107 | mustNotHaveFlags, false, userId, REASON_APP_CANCEL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | @Override |
| 2111 | public void cancelAllNotifications(String pkg, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2112 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2113 | |
| 2114 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2115 | Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg); |
| 2116 | |
| 2117 | // Calling from user space, don't allow the canceling of actively |
| 2118 | // running foreground services. |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2119 | cancelAllNotificationsInt(Binder.getCallingUid(), Binder.getCallingPid(), |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 2120 | pkg, null, 0, FLAG_FOREGROUND_SERVICE, true, userId, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 2121 | REASON_APP_CANCEL_ALL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | @Override |
| 2125 | public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) { |
Rohan Shah | ca0447e | 2018-03-30 15:18:27 -0700 | [diff] [blame] | 2126 | enforceSystemOrSystemUI("setNotificationsEnabledForPackage"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2127 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 2128 | mRankingHelper.setEnabled(pkg, uid, enabled); |
Howard Ro | 4db243a | 2018-08-07 15:44:25 -0700 | [diff] [blame] | 2129 | mMetricsLogger.write(new LogMaker(MetricsEvent.ACTION_BAN_APP_NOTES) |
| 2130 | .setType(MetricsEvent.TYPE_ACTION) |
| 2131 | .setPackageName(pkg) |
| 2132 | .setSubtype(enabled ? 1 : 0)); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 2133 | // 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] | 2134 | if (!enabled) { |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 2135 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, null, 0, 0, true, |
| 2136 | UserHandle.getUserId(uid), REASON_PACKAGE_BANNED, null); |
| 2137 | } |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 2138 | |
| 2139 | try { |
| 2140 | getContext().sendBroadcastAsUser( |
| 2141 | new Intent(ACTION_APP_BLOCK_STATE_CHANGED) |
| 2142 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, !enabled) |
| 2143 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 2144 | .setPackage(pkg), |
| 2145 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 2146 | } catch (SecurityException e) { |
| 2147 | Slog.w(TAG, "Can't notify app about app block change", e); |
| 2148 | } |
| 2149 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 2150 | savePolicyFile(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2151 | } |
| 2152 | |
| 2153 | /** |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 2154 | * Updates the enabled state for notifications for the given package (and uid). |
| 2155 | * Additionally, this method marks the app importance as locked by the user, which means |
| 2156 | * that notifications from the app will <b>not</b> be considered for showing a |
| 2157 | * blocking helper. |
| 2158 | * |
| 2159 | * @param pkg package that owns the notifications to update |
| 2160 | * @param uid uid of the app providing notifications |
| 2161 | * @param enabled whether notifications should be enabled for the app |
| 2162 | * |
| 2163 | * @see #setNotificationsEnabledForPackage(String, int, boolean) |
| 2164 | */ |
| 2165 | @Override |
| 2166 | public void setNotificationsEnabledWithImportanceLockForPackage( |
| 2167 | String pkg, int uid, boolean enabled) { |
| 2168 | setNotificationsEnabledForPackage(pkg, uid, enabled); |
| 2169 | |
| 2170 | mRankingHelper.setAppImportanceLocked(pkg, uid); |
| 2171 | } |
| 2172 | |
| 2173 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2174 | * Use this when you just want to know if notifications are OK for this package. |
| 2175 | */ |
| 2176 | @Override |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 2177 | public boolean areNotificationsEnabled(String pkg) { |
| 2178 | return areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()); |
| 2179 | } |
| 2180 | |
| 2181 | /** |
| 2182 | * Use this when you just want to know if notifications are OK for this package. |
| 2183 | */ |
| 2184 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2185 | public boolean areNotificationsEnabledForPackage(String pkg, int uid) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 2186 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 2187 | |
| 2188 | return mRankingHelper.getImportance(pkg, uid) != IMPORTANCE_NONE; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2189 | } |
| 2190 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 2191 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 2192 | public int getPackageImportance(String pkg) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 2193 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 2194 | return mRankingHelper.getImportance(pkg, Binder.getCallingUid()); |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | @Override |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2198 | public boolean canShowBadge(String pkg, int uid) { |
| 2199 | checkCallerIsSystem(); |
| 2200 | return mRankingHelper.canShowBadge(pkg, uid); |
| 2201 | } |
| 2202 | |
| 2203 | @Override |
| 2204 | public void setShowBadge(String pkg, int uid, boolean showBadge) { |
| 2205 | checkCallerIsSystem(); |
| 2206 | mRankingHelper.setShowBadge(pkg, uid, showBadge); |
| 2207 | savePolicyFile(); |
| 2208 | } |
| 2209 | |
| 2210 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2211 | public void updateNotificationChannelGroupForPackage(String pkg, int uid, |
| 2212 | NotificationChannelGroup group) throws RemoteException { |
| 2213 | enforceSystemOrSystemUI("Caller not system or systemui"); |
| 2214 | createNotificationChannelGroup(pkg, uid, group, false, false); |
| 2215 | savePolicyFile(); |
| 2216 | } |
| 2217 | |
| 2218 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2219 | public void createNotificationChannelGroups(String pkg, |
| 2220 | ParceledListSlice channelGroupList) throws RemoteException { |
| 2221 | checkCallerIsSystemOrSameApp(pkg); |
| 2222 | List<NotificationChannelGroup> groups = channelGroupList.getList(); |
| 2223 | final int groupSize = groups.size(); |
| 2224 | for (int i = 0; i < groupSize; i++) { |
| 2225 | final NotificationChannelGroup group = groups.get(i); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2226 | createNotificationChannelGroup(pkg, Binder.getCallingUid(), group, true, false); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2227 | } |
| 2228 | savePolicyFile(); |
| 2229 | } |
| 2230 | |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 2231 | private void createNotificationChannelsImpl(String pkg, int uid, |
| 2232 | ParceledListSlice channelsList) { |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 2233 | List<NotificationChannel> channels = channelsList.getList(); |
| 2234 | final int channelsSize = channels.size(); |
| 2235 | for (int i = 0; i < channelsSize; i++) { |
| 2236 | final NotificationChannel channel = channels.get(i); |
| 2237 | Preconditions.checkNotNull(channel, "channel in list is null"); |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 2238 | mRankingHelper.createNotificationChannel(pkg, uid, channel, |
Julia Reynolds | 1fe1094 | 2018-03-28 12:46:51 -0400 | [diff] [blame] | 2239 | true /* fromTargetApp */, mConditionProviders.isPackageOrComponentAllowed( |
| 2240 | pkg, UserHandle.getUserId(uid))); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2241 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2242 | UserHandle.getUserHandleForUid(uid), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2243 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), false), |
| 2244 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 2245 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2246 | savePolicyFile(); |
| 2247 | } |
| 2248 | |
| 2249 | @Override |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 2250 | public void createNotificationChannels(String pkg, |
| 2251 | ParceledListSlice channelsList) throws RemoteException { |
| 2252 | checkCallerIsSystemOrSameApp(pkg); |
| 2253 | createNotificationChannelsImpl(pkg, Binder.getCallingUid(), channelsList); |
| 2254 | } |
| 2255 | |
| 2256 | @Override |
| 2257 | public void createNotificationChannelsForPackage(String pkg, int uid, |
| 2258 | ParceledListSlice channelsList) throws RemoteException { |
| 2259 | checkCallerIsSystem(); |
| 2260 | createNotificationChannelsImpl(pkg, uid, channelsList); |
| 2261 | } |
| 2262 | |
| 2263 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2264 | public NotificationChannel getNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2265 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2266 | return mRankingHelper.getNotificationChannel( |
| 2267 | pkg, Binder.getCallingUid(), channelId, false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | @Override |
| 2271 | public NotificationChannel getNotificationChannelForPackage(String pkg, int uid, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2272 | String channelId, boolean includeDeleted) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2273 | checkCallerIsSystem(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2274 | return mRankingHelper.getNotificationChannel(pkg, uid, channelId, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | @Override |
| 2278 | public void deleteNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2279 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2280 | final int callingUid = Binder.getCallingUid(); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2281 | if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 2282 | throw new IllegalArgumentException("Cannot delete default channel"); |
| 2283 | } |
| 2284 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channelId, 0, 0, true, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2285 | UserHandle.getUserId(callingUid), REASON_CHANNEL_BANNED, null); |
| 2286 | mRankingHelper.deleteNotificationChannel(pkg, callingUid, channelId); |
| 2287 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2288 | UserHandle.getUserHandleForUid(callingUid), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2289 | mRankingHelper.getNotificationChannel(pkg, callingUid, channelId, true), |
| 2290 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2291 | savePolicyFile(); |
| 2292 | } |
| 2293 | |
| 2294 | @Override |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2295 | public NotificationChannelGroup getNotificationChannelGroup(String pkg, String groupId) { |
| 2296 | checkCallerIsSystemOrSameApp(pkg); |
| 2297 | return mRankingHelper.getNotificationChannelGroupWithChannels( |
| 2298 | pkg, Binder.getCallingUid(), groupId, false); |
| 2299 | } |
| 2300 | |
| 2301 | @Override |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2302 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroups( |
| 2303 | String pkg) { |
| 2304 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2305 | return mRankingHelper.getNotificationChannelGroups( |
| 2306 | pkg, Binder.getCallingUid(), false, false); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2310 | public void deleteNotificationChannelGroup(String pkg, String groupId) { |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2311 | checkCallerIsSystemOrSameApp(pkg); |
| 2312 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2313 | final int callingUid = Binder.getCallingUid(); |
| 2314 | NotificationChannelGroup groupToDelete = |
| 2315 | mRankingHelper.getNotificationChannelGroup(groupId, pkg, callingUid); |
| 2316 | if (groupToDelete != null) { |
| 2317 | List<NotificationChannel> deletedChannels = |
| 2318 | mRankingHelper.deleteNotificationChannelGroup(pkg, callingUid, groupId); |
| 2319 | for (int i = 0; i < deletedChannels.size(); i++) { |
| 2320 | final NotificationChannel deletedChannel = deletedChannels.get(i); |
| 2321 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, deletedChannel.getId(), 0, 0, |
| 2322 | true, |
| 2323 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, |
| 2324 | null); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2325 | mListeners.notifyNotificationChannelChanged(pkg, |
| 2326 | UserHandle.getUserHandleForUid(callingUid), |
| 2327 | deletedChannel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2328 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
| 2329 | } |
| 2330 | mListeners.notifyNotificationChannelGroupChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2331 | pkg, UserHandle.getUserHandleForUid(callingUid), groupToDelete, |
| 2332 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2333 | savePolicyFile(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2334 | } |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2338 | public void updateNotificationChannelForPackage(String pkg, int uid, |
| 2339 | NotificationChannel channel) { |
Geoffrey Pitsch | 4dd5006 | 2016-12-06 16:41:22 -0500 | [diff] [blame] | 2340 | enforceSystemOrSystemUI("Caller not system or systemui"); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2341 | Preconditions.checkNotNull(channel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2342 | updateNotificationChannelInt(pkg, uid, channel, false); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | @Override |
| 2346 | public ParceledListSlice<NotificationChannel> getNotificationChannelsForPackage(String pkg, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2347 | int uid, boolean includeDeleted) { |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2348 | enforceSystemOrSystemUI("getNotificationChannelsForPackage"); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2349 | return mRankingHelper.getNotificationChannels(pkg, uid, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2353 | public int getNumNotificationChannelsForPackage(String pkg, int uid, |
| 2354 | boolean includeDeleted) { |
| 2355 | enforceSystemOrSystemUI("getNumNotificationChannelsForPackage"); |
| 2356 | return mRankingHelper.getNotificationChannels(pkg, uid, includeDeleted) |
| 2357 | .getList().size(); |
| 2358 | } |
| 2359 | |
| 2360 | @Override |
Julia Reynolds | 17717f5 | 2017-05-09 11:46:06 -0400 | [diff] [blame] | 2361 | public boolean onlyHasDefaultChannel(String pkg, int uid) { |
| 2362 | enforceSystemOrSystemUI("onlyHasDefaultChannel"); |
| 2363 | return mRankingHelper.onlyHasDefaultChannel(pkg, uid); |
| 2364 | } |
| 2365 | |
| 2366 | @Override |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 2367 | public int getDeletedChannelCount(String pkg, int uid) { |
| 2368 | enforceSystemOrSystemUI("getDeletedChannelCount"); |
| 2369 | return mRankingHelper.getDeletedChannelCount(pkg, uid); |
| 2370 | } |
| 2371 | |
| 2372 | @Override |
Julia Reynolds | f2e499d | 2018-03-30 10:36:42 -0400 | [diff] [blame] | 2373 | public int getBlockedChannelCount(String pkg, int uid) { |
| 2374 | enforceSystemOrSystemUI("getBlockedChannelCount"); |
| 2375 | return mRankingHelper.getBlockedChannelCount(pkg, uid); |
| 2376 | } |
| 2377 | |
| 2378 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2379 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroupsForPackage( |
| 2380 | String pkg, int uid, boolean includeDeleted) { |
| 2381 | checkCallerIsSystem(); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2382 | return mRankingHelper.getNotificationChannelGroups(pkg, uid, includeDeleted, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2386 | public NotificationChannelGroup getPopulatedNotificationChannelGroupForPackage( |
| 2387 | String pkg, int uid, String groupId, boolean includeDeleted) { |
| 2388 | enforceSystemOrSystemUI("getPopulatedNotificationChannelGroupForPackage"); |
| 2389 | return mRankingHelper.getNotificationChannelGroupWithChannels( |
| 2390 | pkg, uid, groupId, includeDeleted); |
| 2391 | } |
| 2392 | |
| 2393 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2394 | public NotificationChannelGroup getNotificationChannelGroupForPackage( |
| 2395 | String groupId, String pkg, int uid) { |
| 2396 | enforceSystemOrSystemUI("getNotificationChannelGroupForPackage"); |
| 2397 | return mRankingHelper.getNotificationChannelGroup(groupId, pkg, uid); |
| 2398 | } |
| 2399 | |
| 2400 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2401 | public ParceledListSlice<NotificationChannel> getNotificationChannels(String pkg) { |
| 2402 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2403 | return mRankingHelper.getNotificationChannels( |
| 2404 | pkg, Binder.getCallingUid(), false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2405 | } |
| 2406 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2407 | @Override |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 2408 | public ParceledListSlice<NotifyingApp> getRecentNotifyingAppsForUser(int userId) { |
| 2409 | checkCallerIsSystem(); |
| 2410 | synchronized (mNotificationLock) { |
| 2411 | List<NotifyingApp> apps = new ArrayList<>( |
| 2412 | mRecentApps.getOrDefault(userId, new ArrayList<>())); |
| 2413 | return new ParceledListSlice<>(apps); |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | @Override |
Julia Reynolds | e273f08 | 2018-04-12 13:48:49 -0400 | [diff] [blame] | 2418 | public int getBlockedAppCount(int userId) { |
| 2419 | checkCallerIsSystem(); |
| 2420 | return mRankingHelper.getBlockedAppCount(userId); |
| 2421 | } |
| 2422 | |
| 2423 | @Override |
Beverly | 86d076f | 2018-04-17 14:44:52 -0400 | [diff] [blame] | 2424 | public boolean areChannelsBypassingDnd() { |
| 2425 | return mRankingHelper.areChannelsBypassingDnd(); |
| 2426 | } |
| 2427 | |
| 2428 | @Override |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2429 | public void clearData(String packageName, int uid, boolean fromApp) throws RemoteException { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2430 | checkCallerIsSystem(); |
| 2431 | |
| 2432 | // Cancel posted notifications |
| 2433 | cancelAllNotificationsInt(MY_UID, MY_PID, packageName, null, 0, 0, true, |
| 2434 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, null); |
| 2435 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2436 | final String[] packages = new String[] {packageName}; |
| 2437 | final int[] uids = new int[] {uid}; |
| 2438 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2439 | // Listener & assistant |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2440 | mListeners.onPackagesChanged(true, packages, uids); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2441 | mAssistants.onPackagesChanged(true, packages, uids); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2442 | |
| 2443 | // Zen |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2444 | mConditionProviders.onPackagesChanged(true, packages, uids); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2445 | |
| 2446 | // Reset notification preferences |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2447 | if (!fromApp) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2448 | mRankingHelper.onPackagesChanged( |
| 2449 | true, UserHandle.getCallingUserId(), packages, uids); |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2450 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2451 | |
| 2452 | savePolicyFile(); |
| 2453 | } |
| 2454 | |
| 2455 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2456 | /** |
| 2457 | * System-only API for getting a list of current (i.e. not cleared) notifications. |
| 2458 | * |
| 2459 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2460 | * @returns A list of all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2461 | */ |
| 2462 | @Override |
| 2463 | public StatusBarNotification[] getActiveNotifications(String callingPkg) { |
| 2464 | // enforce() will ensure the calling uid has the correct permission |
| 2465 | getContext().enforceCallingOrSelfPermission( |
| 2466 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 2467 | "NotificationManagerService.getActiveNotifications"); |
| 2468 | |
| 2469 | StatusBarNotification[] tmp = null; |
| 2470 | int uid = Binder.getCallingUid(); |
| 2471 | |
| 2472 | // noteOp will check to make sure the callingPkg matches the uid |
| 2473 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 2474 | == AppOpsManager.MODE_ALLOWED) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2475 | synchronized (mNotificationLock) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2476 | tmp = new StatusBarNotification[mNotificationList.size()]; |
| 2477 | final int N = mNotificationList.size(); |
| 2478 | for (int i=0; i<N; i++) { |
| 2479 | tmp[i] = mNotificationList.get(i).sbn; |
| 2480 | } |
| 2481 | } |
| 2482 | } |
| 2483 | return tmp; |
| 2484 | } |
| 2485 | |
| 2486 | /** |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2487 | * Public API for getting a list of current notifications for the calling package/uid. |
| 2488 | * |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 2489 | * Note that since notification posting is done asynchronously, this will not return |
| 2490 | * notifications that are in the process of being posted. |
| 2491 | * |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2492 | * @returns A list of all the package's notifications, in natural order. |
| 2493 | */ |
| 2494 | @Override |
| 2495 | public ParceledListSlice<StatusBarNotification> getAppActiveNotifications(String pkg, |
| 2496 | int incomingUserId) { |
| 2497 | checkCallerIsSystemOrSameApp(pkg); |
| 2498 | int userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2499 | Binder.getCallingUid(), incomingUserId, true, false, |
| 2500 | "getAppActiveNotifications", pkg); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2501 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2502 | final ArrayMap<String, StatusBarNotification> map |
| 2503 | = new ArrayMap<>(mNotificationList.size() + mEnqueuedNotifications.size()); |
Erik Wolsheimer | 2242b4d | 2015-11-24 13:22:04 -0800 | [diff] [blame] | 2504 | final int N = mNotificationList.size(); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2505 | for (int i = 0; i < N; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2506 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 2507 | mNotificationList.get(i).sbn); |
| 2508 | if (sbn != null) { |
| 2509 | map.put(sbn.getKey(), sbn); |
| 2510 | } |
| 2511 | } |
| 2512 | for(NotificationRecord snoozed: mSnoozeHelper.getSnoozed(userId, pkg)) { |
| 2513 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, snoozed.sbn); |
| 2514 | if (sbn != null) { |
| 2515 | map.put(sbn.getKey(), sbn); |
| 2516 | } |
| 2517 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2518 | final int M = mEnqueuedNotifications.size(); |
| 2519 | for (int i = 0; i < M; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2520 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 2521 | mEnqueuedNotifications.get(i).sbn); |
| 2522 | if (sbn != null) { |
| 2523 | map.put(sbn.getKey(), sbn); // pending update overwrites existing post here |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2524 | } |
| 2525 | } |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2526 | final ArrayList<StatusBarNotification> list = new ArrayList<>(map.size()); |
| 2527 | list.addAll(map.values()); |
| 2528 | return new ParceledListSlice<StatusBarNotification>(list); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2529 | } |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2530 | } |
| 2531 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2532 | private StatusBarNotification sanitizeSbn(String pkg, int userId, |
| 2533 | StatusBarNotification sbn) { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 2534 | if (sbn.getPackageName().equals(pkg) && sbn.getUserId() == userId) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2535 | // We could pass back a cloneLight() but clients might get confused and |
| 2536 | // try to send this thing back to notify() again, which would not work |
| 2537 | // very well. |
| 2538 | return new StatusBarNotification( |
| 2539 | sbn.getPackageName(), |
| 2540 | sbn.getOpPkg(), |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2541 | sbn.getId(), sbn.getTag(), sbn.getUid(), sbn.getInitialPid(), |
| 2542 | sbn.getNotification().clone(), |
| 2543 | sbn.getUser(), sbn.getOverrideGroupKey(), sbn.getPostTime()); |
| 2544 | } |
| 2545 | return null; |
| 2546 | } |
| 2547 | |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2548 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2549 | * System-only API for getting a list of recent (cleared, no longer shown) notifications. |
| 2550 | * |
| 2551 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
| 2552 | */ |
| 2553 | @Override |
| 2554 | public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) { |
| 2555 | // enforce() will ensure the calling uid has the correct permission |
| 2556 | getContext().enforceCallingOrSelfPermission( |
| 2557 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 2558 | "NotificationManagerService.getHistoricalNotifications"); |
| 2559 | |
| 2560 | StatusBarNotification[] tmp = null; |
| 2561 | int uid = Binder.getCallingUid(); |
| 2562 | |
| 2563 | // noteOp will check to make sure the callingPkg matches the uid |
| 2564 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 2565 | == AppOpsManager.MODE_ALLOWED) { |
| 2566 | synchronized (mArchive) { |
| 2567 | tmp = mArchive.getArray(count); |
| 2568 | } |
| 2569 | } |
| 2570 | return tmp; |
| 2571 | } |
| 2572 | |
| 2573 | /** |
| 2574 | * Register a listener binder directly with the notification manager. |
| 2575 | * |
| 2576 | * Only works with system callers. Apps should extend |
| 2577 | * {@link android.service.notification.NotificationListenerService}. |
| 2578 | */ |
| 2579 | @Override |
| 2580 | public void registerListener(final INotificationListener listener, |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 2581 | final ComponentName component, final int userid) { |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 2582 | enforceSystemOrSystemUI("INotificationManager.registerListener"); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 2583 | mListeners.registerService(listener, component, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | /** |
| 2587 | * Remove a listener binder directly |
| 2588 | */ |
| 2589 | @Override |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 2590 | public void unregisterListener(INotificationListener token, int userid) { |
Chris Wren | b7c8109 | 2016-03-10 11:41:10 -0500 | [diff] [blame] | 2591 | mListeners.unregisterService(token, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2592 | } |
| 2593 | |
| 2594 | /** |
| 2595 | * Allow an INotificationListener to simulate a "clear all" operation. |
| 2596 | * |
| 2597 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications} |
| 2598 | * |
| 2599 | * @param token The binder for the listener, to check that the caller is allowed |
| 2600 | */ |
| 2601 | @Override |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2602 | public void cancelNotificationsFromListener(INotificationListener token, String[] keys) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2603 | final int callingUid = Binder.getCallingUid(); |
| 2604 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2605 | long identity = Binder.clearCallingIdentity(); |
| 2606 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2607 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2608 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 2609 | |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2610 | if (keys != null) { |
| 2611 | final int N = keys.length; |
| 2612 | for (int i = 0; i < N; i++) { |
| 2613 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 2614 | if (r == null) continue; |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2615 | final int userId = r.sbn.getUserId(); |
| 2616 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 2617 | !mUserProfiles.isCurrentProfile(userId)) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2618 | throw new SecurityException("Disallowed call from listener: " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2619 | + info.service); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2620 | } |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 2621 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2622 | r.sbn.getPackageName(), r.sbn.getTag(), r.sbn.getId(), |
| 2623 | userId); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2624 | } |
| 2625 | } else { |
| 2626 | cancelAllLocked(callingUid, callingPid, info.userid, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2627 | REASON_LISTENER_CANCEL_ALL, info, info.supportsProfiles()); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2628 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2629 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2630 | } finally { |
| 2631 | Binder.restoreCallingIdentity(identity); |
| 2632 | } |
| 2633 | } |
| 2634 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2635 | /** |
| 2636 | * Handle request from an approved listener to re-enable itself. |
| 2637 | * |
| 2638 | * @param component The componenet to be re-enabled, caller must match package. |
| 2639 | */ |
| 2640 | @Override |
| 2641 | public void requestBindListener(ComponentName component) { |
| 2642 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 2643 | long identity = Binder.clearCallingIdentity(); |
| 2644 | try { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2645 | ManagedServices manager = |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2646 | mAssistants.isComponentEnabledForCurrentProfiles(component) |
| 2647 | ? mAssistants |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2648 | : mListeners; |
| 2649 | manager.setComponentState(component, true); |
| 2650 | } finally { |
| 2651 | Binder.restoreCallingIdentity(identity); |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | @Override |
| 2656 | public void requestUnbindListener(INotificationListener token) { |
| 2657 | long identity = Binder.clearCallingIdentity(); |
| 2658 | try { |
| 2659 | // allow bound services to disable themselves |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2660 | synchronized (mNotificationLock) { |
| 2661 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2662 | info.getOwner().setComponentState(info.component, false); |
| 2663 | } |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2664 | } finally { |
| 2665 | Binder.restoreCallingIdentity(identity); |
| 2666 | } |
| 2667 | } |
| 2668 | |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2669 | @Override |
| 2670 | public void setNotificationsShownFromListener(INotificationListener token, String[] keys) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2671 | long identity = Binder.clearCallingIdentity(); |
| 2672 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2673 | synchronized (mNotificationLock) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2674 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2675 | if (keys != null) { |
| 2676 | final int N = keys.length; |
| 2677 | for (int i = 0; i < N; i++) { |
| 2678 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
| 2679 | if (r == null) continue; |
| 2680 | final int userId = r.sbn.getUserId(); |
| 2681 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
| 2682 | !mUserProfiles.isCurrentProfile(userId)) { |
| 2683 | throw new SecurityException("Disallowed call from listener: " |
| 2684 | + info.service); |
| 2685 | } |
| 2686 | if (!r.isSeen()) { |
| 2687 | if (DBG) Slog.d(TAG, "Marking notification as seen " + keys[i]); |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 2688 | reportSeen(r); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2689 | r.setSeen(); |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 2690 | maybeRecordInterruptionLocked(r); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2691 | } |
| 2692 | } |
| 2693 | } |
| 2694 | } |
| 2695 | } finally { |
| 2696 | Binder.restoreCallingIdentity(identity); |
| 2697 | } |
| 2698 | } |
| 2699 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2700 | /** |
| 2701 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2702 | * |
| 2703 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2704 | * |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2705 | * @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] | 2706 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2707 | @GuardedBy("mNotificationLock") |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2708 | private void cancelNotificationFromListenerLocked(ManagedServiceInfo info, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2709 | int callingUid, int callingPid, String pkg, String tag, int id, int userId) { |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2710 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 2711 | Notification.FLAG_ONGOING_EVENT | FLAG_FOREGROUND_SERVICE, |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2712 | true, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2713 | userId, REASON_LISTENER_CANCEL, info); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2714 | } |
| 2715 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2716 | /** |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2717 | * Allow an INotificationListener to snooze a single notification until a context. |
| 2718 | * |
| 2719 | * @param token The binder for the listener, to check that the caller is allowed |
| 2720 | */ |
| 2721 | @Override |
| 2722 | public void snoozeNotificationUntilContextFromListener(INotificationListener token, |
| 2723 | String key, String snoozeCriterionId) { |
| 2724 | long identity = Binder.clearCallingIdentity(); |
| 2725 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2726 | synchronized (mNotificationLock) { |
| 2727 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2728 | snoozeNotificationInt(key, SNOOZE_UNTIL_UNSPECIFIED, snoozeCriterionId, info); |
| 2729 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2730 | } finally { |
| 2731 | Binder.restoreCallingIdentity(identity); |
| 2732 | } |
| 2733 | } |
| 2734 | |
| 2735 | /** |
| 2736 | * Allow an INotificationListener to snooze a single notification until a time. |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2737 | * |
| 2738 | * @param token The binder for the listener, to check that the caller is allowed |
| 2739 | */ |
| 2740 | @Override |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2741 | public void snoozeNotificationUntilFromListener(INotificationListener token, String key, |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 2742 | long duration) { |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2743 | long identity = Binder.clearCallingIdentity(); |
| 2744 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2745 | synchronized (mNotificationLock) { |
| 2746 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2747 | snoozeNotificationInt(key, duration, null, info); |
| 2748 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2749 | } finally { |
| 2750 | Binder.restoreCallingIdentity(identity); |
| 2751 | } |
| 2752 | } |
| 2753 | |
| 2754 | /** |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2755 | * Allows the notification assistant to un-snooze a single notification. |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2756 | * |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2757 | * @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] | 2758 | */ |
| 2759 | @Override |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2760 | public void unsnoozeNotificationFromAssistant(INotificationListener token, String key) { |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2761 | long identity = Binder.clearCallingIdentity(); |
| 2762 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2763 | synchronized (mNotificationLock) { |
| 2764 | final ManagedServiceInfo info = |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2765 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2766 | unsnoozeNotificationInt(key, info); |
| 2767 | } |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2768 | } finally { |
| 2769 | Binder.restoreCallingIdentity(identity); |
| 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2774 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2775 | * |
| 2776 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2777 | * |
| 2778 | * @param token The binder for the listener, to check that the caller is allowed |
| 2779 | */ |
| 2780 | @Override |
| 2781 | public void cancelNotificationFromListener(INotificationListener token, String pkg, |
| 2782 | String tag, int id) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2783 | final int callingUid = Binder.getCallingUid(); |
| 2784 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2785 | long identity = Binder.clearCallingIdentity(); |
| 2786 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2787 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2788 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2789 | if (info.supportsProfiles()) { |
| 2790 | Log.e(TAG, "Ignoring deprecated cancelNotification(pkg, tag, id) " |
| 2791 | + "from " + info.component |
| 2792 | + " use cancelNotification(key) instead."); |
| 2793 | } else { |
| 2794 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2795 | pkg, tag, id, info.userid); |
| 2796 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2797 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2798 | } finally { |
| 2799 | Binder.restoreCallingIdentity(identity); |
| 2800 | } |
| 2801 | } |
| 2802 | |
| 2803 | /** |
| 2804 | * Allow an INotificationListener to request the list of outstanding notifications seen by |
| 2805 | * the current user. Useful when starting up, after which point the listener callbacks |
| 2806 | * should be used. |
| 2807 | * |
| 2808 | * @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] | 2809 | * @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] | 2810 | * @returns The return value will contain the notifications specified in keys, in that |
| 2811 | * order, or if keys is null, all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2812 | */ |
| 2813 | @Override |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2814 | public ParceledListSlice<StatusBarNotification> getActiveNotificationsFromListener( |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2815 | INotificationListener token, String[] keys, int trim) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2816 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2817 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2818 | final boolean getKeys = keys != null; |
| 2819 | final int N = getKeys ? keys.length : mNotificationList.size(); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2820 | final ArrayList<StatusBarNotification> list |
| 2821 | = new ArrayList<StatusBarNotification>(N); |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2822 | for (int i=0; i<N; i++) { |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2823 | final NotificationRecord r = getKeys |
| 2824 | ? mNotificationsByKey.get(keys[i]) |
| 2825 | : mNotificationList.get(i); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2826 | if (r == null) continue; |
| 2827 | StatusBarNotification sbn = r.sbn; |
| 2828 | if (!isVisibleToListener(sbn, info)) continue; |
| 2829 | StatusBarNotification sbnToSend = |
| 2830 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2831 | list.add(sbnToSend); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2832 | } |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2833 | return new ParceledListSlice<StatusBarNotification>(list); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2834 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2835 | } |
| 2836 | |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2837 | /** |
| 2838 | * Allow an INotificationListener to request the list of outstanding snoozed notifications |
| 2839 | * seen by the current user. Useful when starting up, after which point the listener |
| 2840 | * callbacks should be used. |
| 2841 | * |
| 2842 | * @param token The binder for the listener, to check that the caller is allowed |
| 2843 | * @returns The return value will contain the notifications specified in keys, in that |
| 2844 | * order, or if keys is null, all the notifications, in natural order. |
| 2845 | */ |
| 2846 | @Override |
| 2847 | public ParceledListSlice<StatusBarNotification> getSnoozedNotificationsFromListener( |
| 2848 | INotificationListener token, int trim) { |
| 2849 | synchronized (mNotificationLock) { |
| 2850 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2851 | List<NotificationRecord> snoozedRecords = mSnoozeHelper.getSnoozed(); |
| 2852 | final int N = snoozedRecords.size(); |
| 2853 | final ArrayList<StatusBarNotification> list = new ArrayList<>(N); |
| 2854 | for (int i=0; i < N; i++) { |
| 2855 | final NotificationRecord r = snoozedRecords.get(i); |
| 2856 | if (r == null) continue; |
| 2857 | StatusBarNotification sbn = r.sbn; |
| 2858 | if (!isVisibleToListener(sbn, info)) continue; |
| 2859 | StatusBarNotification sbnToSend = |
| 2860 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2861 | list.add(sbnToSend); |
| 2862 | } |
| 2863 | return new ParceledListSlice<>(list); |
| 2864 | } |
| 2865 | } |
| 2866 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2867 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2868 | public void requestHintsFromListener(INotificationListener token, int hints) { |
| 2869 | final long identity = Binder.clearCallingIdentity(); |
| 2870 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2871 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2872 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2873 | final int disableEffectsMask = HINT_HOST_DISABLE_EFFECTS |
| 2874 | | HINT_HOST_DISABLE_NOTIFICATION_EFFECTS |
| 2875 | | HINT_HOST_DISABLE_CALL_EFFECTS; |
| 2876 | final boolean disableEffects = (hints & disableEffectsMask) != 0; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2877 | if (disableEffects) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2878 | addDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2879 | } else { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2880 | removeDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2881 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2882 | updateListenerHintsLocked(); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2883 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2884 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2885 | } finally { |
| 2886 | Binder.restoreCallingIdentity(identity); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2891 | public int getHintsFromListener(INotificationListener token) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2892 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2893 | return mListenerHints; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | @Override |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2898 | public void requestInterruptionFilterFromListener(INotificationListener token, |
| 2899 | int interruptionFilter) throws RemoteException { |
| 2900 | final long identity = Binder.clearCallingIdentity(); |
| 2901 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2902 | synchronized (mNotificationLock) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2903 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2904 | mZenModeHelper.requestFromListener(info.component, interruptionFilter); |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2905 | updateInterruptionFilterLocked(); |
| 2906 | } |
| 2907 | } finally { |
| 2908 | Binder.restoreCallingIdentity(identity); |
| 2909 | } |
| 2910 | } |
| 2911 | |
| 2912 | @Override |
| 2913 | public int getInterruptionFilterFromListener(INotificationListener token) |
| 2914 | throws RemoteException { |
| 2915 | synchronized (mNotificationLight) { |
| 2916 | return mInterruptionFilter; |
| 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | @Override |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2921 | public void setOnNotificationPostedTrimFromListener(INotificationListener token, int trim) |
| 2922 | throws RemoteException { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2923 | synchronized (mNotificationLock) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2924 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2925 | if (info == null) return; |
| 2926 | mListeners.setOnNotificationPostedTrimLocked(info, trim); |
| 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2931 | public int getZenMode() { |
| 2932 | return mZenModeHelper.getZenMode(); |
| 2933 | } |
| 2934 | |
| 2935 | @Override |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 2936 | public ZenModeConfig getZenModeConfig() { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2937 | enforceSystemOrSystemUI("INotificationManager.getZenModeConfig"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 2938 | return mZenModeHelper.getConfig(); |
| 2939 | } |
| 2940 | |
| 2941 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2942 | public void setZenMode(int mode, Uri conditionId, String reason) throws RemoteException { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2943 | enforceSystemOrSystemUI("INotificationManager.setZenMode"); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2944 | final long identity = Binder.clearCallingIdentity(); |
| 2945 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 2946 | mZenModeHelper.setManualZenMode(mode, conditionId, null, reason); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2947 | } finally { |
| 2948 | Binder.restoreCallingIdentity(identity); |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2953 | public List<ZenModeConfig.ZenRule> getZenRules() throws RemoteException { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2954 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRules"); |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2955 | return mZenModeHelper.getZenRules(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2956 | } |
| 2957 | |
| 2958 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2959 | public AutomaticZenRule getAutomaticZenRule(String id) throws RemoteException { |
| 2960 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2961 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRule"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2962 | return mZenModeHelper.getAutomaticZenRule(id); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2966 | public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2967 | throws RemoteException { |
| 2968 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 2969 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 2970 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 2971 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2972 | enforcePolicyAccess(Binder.getCallingUid(), "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2973 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2974 | return mZenModeHelper.addAutomaticZenRule(automaticZenRule, |
| 2975 | "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2979 | public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2980 | throws RemoteException { |
| 2981 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 2982 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 2983 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 2984 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
| 2985 | enforcePolicyAccess(Binder.getCallingUid(), "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2986 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2987 | return mZenModeHelper.updateAutomaticZenRule(id, automaticZenRule, |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2988 | "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2992 | public boolean removeAutomaticZenRule(String id) throws RemoteException { |
| 2993 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2994 | // Verify that they can modify zen rules. |
| 2995 | enforcePolicyAccess(Binder.getCallingUid(), "removeAutomaticZenRule"); |
| 2996 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2997 | return mZenModeHelper.removeAutomaticZenRule(id, "removeAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | @Override |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 3001 | public boolean removeAutomaticZenRules(String packageName) throws RemoteException { |
| 3002 | Preconditions.checkNotNull(packageName, "Package name is null"); |
| 3003 | enforceSystemOrSystemUI("removeAutomaticZenRules"); |
| 3004 | |
| 3005 | return mZenModeHelper.removeAutomaticZenRules(packageName, "removeAutomaticZenRules"); |
| 3006 | } |
| 3007 | |
| 3008 | @Override |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 3009 | public int getRuleInstanceCount(ComponentName owner) throws RemoteException { |
| 3010 | Preconditions.checkNotNull(owner, "Owner is null"); |
| 3011 | enforceSystemOrSystemUI("getRuleInstanceCount"); |
| 3012 | |
| 3013 | return mZenModeHelper.getCurrentInstanceCount(owner); |
| 3014 | } |
| 3015 | |
| 3016 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3017 | public void setInterruptionFilter(String pkg, int filter) throws RemoteException { |
| 3018 | enforcePolicyAccess(pkg, "setInterruptionFilter"); |
| 3019 | final int zen = NotificationManager.zenModeFromInterruptionFilter(filter, -1); |
| 3020 | if (zen == -1) throw new IllegalArgumentException("Invalid filter: " + filter); |
| 3021 | final long identity = Binder.clearCallingIdentity(); |
| 3022 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 3023 | mZenModeHelper.setManualZenMode(zen, null, pkg, "setInterruptionFilter"); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3024 | } finally { |
| 3025 | Binder.restoreCallingIdentity(identity); |
| 3026 | } |
| 3027 | } |
| 3028 | |
| 3029 | @Override |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 3030 | public void notifyConditions(final String pkg, IConditionProvider provider, |
| 3031 | final Condition[] conditions) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3032 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 3033 | checkCallerIsSystemOrSameApp(pkg); |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 3034 | mHandler.post(new Runnable() { |
| 3035 | @Override |
| 3036 | public void run() { |
| 3037 | mConditionProviders.notifyConditions(pkg, info, conditions); |
| 3038 | } |
| 3039 | }); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3040 | } |
| 3041 | |
Julia Reynolds | 38e6ca4 | 2016-08-08 08:38:09 -0400 | [diff] [blame] | 3042 | @Override |
| 3043 | public void requestUnbindProvider(IConditionProvider provider) { |
| 3044 | long identity = Binder.clearCallingIdentity(); |
| 3045 | try { |
| 3046 | // allow bound services to disable themselves |
| 3047 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 3048 | info.getOwner().setComponentState(info.component, false); |
| 3049 | } finally { |
| 3050 | Binder.restoreCallingIdentity(identity); |
| 3051 | } |
| 3052 | } |
| 3053 | |
| 3054 | @Override |
| 3055 | public void requestBindProvider(ComponentName component) { |
| 3056 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 3057 | long identity = Binder.clearCallingIdentity(); |
| 3058 | try { |
| 3059 | mConditionProviders.setComponentState(component, true); |
| 3060 | } finally { |
| 3061 | Binder.restoreCallingIdentity(identity); |
| 3062 | } |
| 3063 | } |
| 3064 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3065 | private void enforceSystemOrSystemUI(String message) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3066 | if (isCallerSystemOrPhone()) return; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3067 | getContext().enforceCallingPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 3068 | message); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3069 | } |
| 3070 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 3071 | private void enforceSystemOrSystemUIOrSamePackage(String pkg, String message) { |
| 3072 | try { |
| 3073 | checkCallerIsSystemOrSameApp(pkg); |
| 3074 | } catch (SecurityException e) { |
| 3075 | getContext().enforceCallingPermission( |
| 3076 | android.Manifest.permission.STATUS_BAR_SERVICE, |
| 3077 | message); |
| 3078 | } |
| 3079 | } |
| 3080 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3081 | private void enforcePolicyAccess(int uid, String method) { |
| 3082 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 3083 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 3084 | return; |
| 3085 | } |
| 3086 | boolean accessAllowed = false; |
| 3087 | String[] packages = getContext().getPackageManager().getPackagesForUid(uid); |
| 3088 | final int packageCount = packages.length; |
| 3089 | for (int i = 0; i < packageCount; i++) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3090 | if (mConditionProviders.isPackageOrComponentAllowed( |
| 3091 | packages[i], UserHandle.getUserId(uid))) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 3092 | accessAllowed = true; |
| 3093 | } |
| 3094 | } |
| 3095 | if (!accessAllowed) { |
| 3096 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 3097 | throw new SecurityException("Notification policy access denied"); |
| 3098 | } |
| 3099 | } |
| 3100 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3101 | private void enforcePolicyAccess(String pkg, String method) { |
Julia Reynolds | 6ee2617 | 2015-09-28 11:34:48 -0400 | [diff] [blame] | 3102 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 3103 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 3104 | return; |
| 3105 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 3106 | checkCallerIsSameApp(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3107 | if (!checkPolicyAccess(pkg)) { |
| 3108 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 3109 | throw new SecurityException("Notification policy access denied"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3110 | } |
| 3111 | } |
| 3112 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3113 | private boolean checkPackagePolicyAccess(String pkg) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3114 | return mConditionProviders.isPackageOrComponentAllowed( |
| 3115 | pkg, getCallingUserHandle().getIdentifier()); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | private boolean checkPolicyAccess(String pkg) { |
Julia Reynolds | 0867b3a | 2016-03-30 17:29:54 -0400 | [diff] [blame] | 3119 | try { |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 3120 | int uid = getContext().getPackageManager().getPackageUidAsUser(pkg, |
| 3121 | UserHandle.getCallingUserId()); |
Julia Reynolds | 0867b3a | 2016-03-30 17:29:54 -0400 | [diff] [blame] | 3122 | if (PackageManager.PERMISSION_GRANTED == ActivityManager.checkComponentPermission( |
| 3123 | android.Manifest.permission.MANAGE_NOTIFICATIONS, uid, |
| 3124 | -1, true)) { |
| 3125 | return true; |
| 3126 | } |
| 3127 | } catch (NameNotFoundException e) { |
| 3128 | return false; |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 3129 | } |
Jason Parks | 50322ff | 2018-03-27 10:23:33 -0500 | [diff] [blame] | 3130 | return checkPackagePolicyAccess(pkg) |
| 3131 | || mListeners.isComponentEnabledForPackage(pkg) |
| 3132 | || (mDpm != null && |
| 3133 | mDpm.isActiveAdminWithPolicy(Binder.getCallingUid(), |
| 3134 | DeviceAdminInfo.USES_POLICY_PROFILE_OWNER)); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3135 | } |
| 3136 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3137 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3138 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 3139 | if (!DumpUtils.checkDumpAndUsageStatsPermission(getContext(), TAG, pw)) return; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3140 | final DumpFilter filter = DumpFilter.parseFromArguments(args); |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3141 | if (filter.stats) { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3142 | dumpJson(pw, filter); |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3143 | } else if (filter.proto) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3144 | dumpProto(fd, filter); |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 3145 | } else if (filter.criticalPriority) { |
| 3146 | dumpNotificationRecords(pw, filter); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3147 | } else { |
| 3148 | dumpImpl(pw, filter); |
| 3149 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3150 | } |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 3151 | |
| 3152 | @Override |
| 3153 | public ComponentName getEffectsSuppressor() { |
Bryce Lee | ba3d895 | 2016-04-12 12:39:15 -0700 | [diff] [blame] | 3154 | return !mEffectsSuppressors.isEmpty() ? mEffectsSuppressors.get(0) : null; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 3155 | } |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 3156 | |
| 3157 | @Override |
| 3158 | public boolean matchesCallFilter(Bundle extras) { |
| 3159 | enforceSystemOrSystemUI("INotificationManager.matchesCallFilter"); |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 3160 | return mZenModeHelper.matchesCallFilter( |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 3161 | Binder.getCallingUserHandle(), |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 3162 | extras, |
| 3163 | mRankingHelper.findExtractor(ValidateNotificationPeople.class), |
| 3164 | MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS, |
| 3165 | MATCHES_CALL_FILTER_TIMEOUT_AFFINITY); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 3166 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 3167 | |
| 3168 | @Override |
| 3169 | public boolean isSystemConditionProviderEnabled(String path) { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 3170 | enforceSystemOrSystemUI("INotificationManager.isSystemConditionProviderEnabled"); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 3171 | return mConditionProviders.isSystemProviderEnabled(path); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 3172 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3173 | |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 3174 | // Backup/restore interface |
| 3175 | @Override |
| 3176 | public byte[] getBackupPayload(int user) { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3177 | checkCallerIsSystem(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3178 | if (DBG) Slog.d(TAG, "getBackupPayload u=" + user); |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 3179 | //TODO: http://b/22388012 |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3180 | if (user != USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3181 | Slog.w(TAG, "getBackupPayload: cannot backup policy for user " + user); |
| 3182 | return null; |
| 3183 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 3184 | synchronized(mPolicyFile) { |
| 3185 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 3186 | try { |
| 3187 | writePolicyXml(baos, true /*forBackup*/); |
| 3188 | return baos.toByteArray(); |
| 3189 | } catch (IOException e) { |
| 3190 | Slog.w(TAG, "getBackupPayload: error writing payload for user " + user, e); |
| 3191 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3192 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 3193 | return null; |
| 3194 | } |
| 3195 | |
| 3196 | @Override |
| 3197 | public void applyRestore(byte[] payload, int user) { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 3198 | checkCallerIsSystem(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3199 | if (DBG) Slog.d(TAG, "applyRestore u=" + user + " payload=" |
| 3200 | + (payload != null ? new String(payload, StandardCharsets.UTF_8) : null)); |
| 3201 | if (payload == null) { |
| 3202 | Slog.w(TAG, "applyRestore: no payload to restore for user " + user); |
| 3203 | return; |
| 3204 | } |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 3205 | //TODO: http://b/22388012 |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3206 | if (user != USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3207 | Slog.w(TAG, "applyRestore: cannot restore policy for user " + user); |
| 3208 | return; |
| 3209 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 3210 | synchronized(mPolicyFile) { |
| 3211 | final ByteArrayInputStream bais = new ByteArrayInputStream(payload); |
| 3212 | try { |
| 3213 | readPolicyXml(bais, true /*forRestore*/); |
| 3214 | savePolicyFile(); |
| 3215 | } catch (NumberFormatException | XmlPullParserException | IOException e) { |
| 3216 | Slog.w(TAG, "applyRestore: error reading payload", e); |
| 3217 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 3218 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 3219 | } |
| 3220 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3221 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3222 | public boolean isNotificationPolicyAccessGranted(String pkg) { |
| 3223 | return checkPolicyAccess(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | @Override |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 3227 | public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {; |
| 3228 | enforceSystemOrSystemUIOrSamePackage(pkg, |
| 3229 | "request policy access status for another package"); |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 3230 | return checkPolicyAccess(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3234 | public void setNotificationPolicyAccessGranted(String pkg, boolean granted) |
| 3235 | throws RemoteException { |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3236 | setNotificationPolicyAccessGrantedForUser( |
| 3237 | pkg, getCallingUserHandle().getIdentifier(), granted); |
| 3238 | } |
| 3239 | |
| 3240 | @Override |
| 3241 | public void setNotificationPolicyAccessGrantedForUser( |
| 3242 | String pkg, int userId, boolean granted) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3243 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3244 | final long identity = Binder.clearCallingIdentity(); |
| 3245 | try { |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3246 | if (mAllowedManagedServicePackages.test(pkg)) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3247 | mConditionProviders.setPackageOrComponentEnabled( |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3248 | pkg, userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3249 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3250 | getContext().sendBroadcastAsUser(new Intent( |
| 3251 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 3252 | .setPackage(pkg) |
| 3253 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3254 | UserHandle.of(userId), null); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3255 | savePolicyFile(); |
| 3256 | } |
| 3257 | } finally { |
| 3258 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3259 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | @Override |
| 3263 | public Policy getNotificationPolicy(String pkg) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3264 | final long identity = Binder.clearCallingIdentity(); |
| 3265 | try { |
| 3266 | return mZenModeHelper.getNotificationPolicy(); |
| 3267 | } finally { |
| 3268 | Binder.restoreCallingIdentity(identity); |
| 3269 | } |
| 3270 | } |
| 3271 | |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3272 | /** |
| 3273 | * Sets the notification policy. Apps that target API levels below |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3274 | * {@link android.os.Build.VERSION_CODES#P} cannot change user-designated values to |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3275 | * allow or disallow {@link Policy#PRIORITY_CATEGORY_ALARMS}, |
| 3276 | * {@link Policy#PRIORITY_CATEGORY_SYSTEM} and |
| 3277 | * {@link Policy#PRIORITY_CATEGORY_MEDIA} from bypassing dnd |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3278 | */ |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3279 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3280 | public void setNotificationPolicy(String pkg, Policy policy) { |
| 3281 | enforcePolicyAccess(pkg, "setNotificationPolicy"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3282 | final long identity = Binder.clearCallingIdentity(); |
| 3283 | try { |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3284 | final ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(pkg, |
| 3285 | 0, UserHandle.getUserId(MY_UID)); |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3286 | Policy currPolicy = mZenModeHelper.getNotificationPolicy(); |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3287 | |
Jeff Sharkey | aa1a911 | 2018-04-10 15:18:12 -0600 | [diff] [blame] | 3288 | if (applicationInfo.targetSdkVersion < Build.VERSION_CODES.P) { |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3289 | int priorityCategories = policy.priorityCategories; |
| 3290 | // ignore alarm and media values from new policy |
| 3291 | priorityCategories &= ~Policy.PRIORITY_CATEGORY_ALARMS; |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3292 | priorityCategories &= ~Policy.PRIORITY_CATEGORY_MEDIA; |
| 3293 | priorityCategories &= ~Policy.PRIORITY_CATEGORY_SYSTEM; |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3294 | // use user-designated values |
Beverly | d696476 | 2018-02-16 14:07:03 -0500 | [diff] [blame] | 3295 | priorityCategories |= currPolicy.priorityCategories |
| 3296 | & Policy.PRIORITY_CATEGORY_ALARMS; |
| 3297 | priorityCategories |= currPolicy.priorityCategories |
| 3298 | & Policy.PRIORITY_CATEGORY_MEDIA; |
| 3299 | priorityCategories |= currPolicy.priorityCategories |
| 3300 | & Policy.PRIORITY_CATEGORY_SYSTEM; |
Beverly | 98ef61b | 2018-02-15 10:36:28 -0500 | [diff] [blame] | 3301 | |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3302 | policy = new Policy(priorityCategories, |
| 3303 | policy.priorityCallSenders, policy.priorityMessageSenders, |
| 3304 | policy.suppressedVisualEffects); |
| 3305 | } |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 3306 | int newVisualEffects = calculateSuppressedVisualEffects( |
| 3307 | policy, currPolicy, applicationInfo.targetSdkVersion); |
| 3308 | policy = new Policy(policy.priorityCategories, |
| 3309 | policy.priorityCallSenders, policy.priorityMessageSenders, |
| 3310 | newVisualEffects); |
Beverly | 5e07322 | 2018-03-08 10:36:25 -0500 | [diff] [blame] | 3311 | ZenLog.traceSetNotificationPolicy(pkg, applicationInfo.targetSdkVersion, policy); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3312 | mZenModeHelper.setNotificationPolicy(policy); |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3313 | } catch (RemoteException e) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3314 | } finally { |
| 3315 | Binder.restoreCallingIdentity(identity); |
| 3316 | } |
| 3317 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3318 | |
| 3319 | @Override |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3320 | public List<String> getEnabledNotificationListenerPackages() { |
| 3321 | checkCallerIsSystem(); |
| 3322 | return mListeners.getAllowedPackages(getCallingUserHandle().getIdentifier()); |
| 3323 | } |
| 3324 | |
| 3325 | @Override |
| 3326 | public List<ComponentName> getEnabledNotificationListeners(int userId) { |
| 3327 | checkCallerIsSystem(); |
| 3328 | return mListeners.getAllowedComponents(userId); |
| 3329 | } |
| 3330 | |
| 3331 | @Override |
| 3332 | public boolean isNotificationListenerAccessGranted(ComponentName listener) { |
| 3333 | Preconditions.checkNotNull(listener); |
| 3334 | checkCallerIsSystemOrSameApp(listener.getPackageName()); |
| 3335 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 3336 | getCallingUserHandle().getIdentifier()); |
| 3337 | } |
| 3338 | |
| 3339 | @Override |
| 3340 | public boolean isNotificationListenerAccessGrantedForUser(ComponentName listener, |
| 3341 | int userId) { |
| 3342 | Preconditions.checkNotNull(listener); |
| 3343 | checkCallerIsSystem(); |
| 3344 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 3345 | userId); |
| 3346 | } |
| 3347 | |
| 3348 | @Override |
| 3349 | public boolean isNotificationAssistantAccessGranted(ComponentName assistant) { |
| 3350 | Preconditions.checkNotNull(assistant); |
| 3351 | checkCallerIsSystemOrSameApp(assistant.getPackageName()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3352 | return mAssistants.isPackageOrComponentAllowed(assistant.flattenToString(), |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3353 | getCallingUserHandle().getIdentifier()); |
| 3354 | } |
| 3355 | |
| 3356 | @Override |
| 3357 | public void setNotificationListenerAccessGranted(ComponentName listener, |
| 3358 | boolean granted) throws RemoteException { |
| 3359 | setNotificationListenerAccessGrantedForUser( |
| 3360 | listener, getCallingUserHandle().getIdentifier(), granted); |
| 3361 | } |
| 3362 | |
| 3363 | @Override |
| 3364 | public void setNotificationAssistantAccessGranted(ComponentName assistant, |
| 3365 | boolean granted) throws RemoteException { |
| 3366 | setNotificationAssistantAccessGrantedForUser( |
| 3367 | assistant, getCallingUserHandle().getIdentifier(), granted); |
| 3368 | } |
| 3369 | |
| 3370 | @Override |
| 3371 | public void setNotificationListenerAccessGrantedForUser(ComponentName listener, int userId, |
| 3372 | boolean granted) throws RemoteException { |
| 3373 | Preconditions.checkNotNull(listener); |
Julia Reynolds | 0d21764 | 2017-08-11 11:26:04 -0400 | [diff] [blame] | 3374 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3375 | final long identity = Binder.clearCallingIdentity(); |
| 3376 | try { |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3377 | if (mAllowedManagedServicePackages.test(listener.getPackageName())) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3378 | mConditionProviders.setPackageOrComponentEnabled(listener.flattenToString(), |
| 3379 | userId, false, granted); |
| 3380 | mListeners.setPackageOrComponentEnabled(listener.flattenToString(), |
| 3381 | userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3382 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3383 | getContext().sendBroadcastAsUser(new Intent( |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3384 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3385 | .setPackage(listener.getPackageName()) |
| 3386 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3387 | UserHandle.of(userId), null); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3388 | |
| 3389 | savePolicyFile(); |
| 3390 | } |
| 3391 | } finally { |
| 3392 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3393 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3394 | } |
| 3395 | |
| 3396 | @Override |
| 3397 | public void setNotificationAssistantAccessGrantedForUser(ComponentName assistant, |
| 3398 | int userId, boolean granted) throws RemoteException { |
| 3399 | Preconditions.checkNotNull(assistant); |
Julia Reynolds | 0d21764 | 2017-08-11 11:26:04 -0400 | [diff] [blame] | 3400 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3401 | final long identity = Binder.clearCallingIdentity(); |
| 3402 | try { |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 3403 | if (mAllowedManagedServicePackages.test(assistant.getPackageName())) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3404 | mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(), |
| 3405 | userId, false, granted); |
| 3406 | mAssistants.setPackageOrComponentEnabled(assistant.flattenToString(), |
| 3407 | userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3408 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3409 | getContext().sendBroadcastAsUser(new Intent( |
| 3410 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 3411 | .setPackage(assistant.getPackageName()) |
| 3412 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3413 | UserHandle.of(userId), null); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3414 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3415 | savePolicyFile(); |
| 3416 | } |
| 3417 | } finally { |
| 3418 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3419 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3420 | } |
| 3421 | |
| 3422 | @Override |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3423 | public void applyEnqueuedAdjustmentFromAssistant(INotificationListener token, |
| 3424 | Adjustment adjustment) throws RemoteException { |
| 3425 | final long identity = Binder.clearCallingIdentity(); |
| 3426 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3427 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3428 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3429 | int N = mEnqueuedNotifications.size(); |
| 3430 | for (int i = 0; i < N; i++) { |
| 3431 | final NotificationRecord n = mEnqueuedNotifications.get(i); |
| 3432 | if (Objects.equals(adjustment.getKey(), n.getKey()) |
| 3433 | && Objects.equals(adjustment.getUser(), n.getUserId())) { |
| 3434 | applyAdjustment(n, adjustment); |
| 3435 | break; |
| 3436 | } |
| 3437 | } |
| 3438 | } |
| 3439 | } finally { |
| 3440 | Binder.restoreCallingIdentity(identity); |
| 3441 | } |
| 3442 | } |
| 3443 | |
| 3444 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 3445 | public void applyAdjustmentFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3446 | Adjustment adjustment) throws RemoteException { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3447 | final long identity = Binder.clearCallingIdentity(); |
| 3448 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3449 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3450 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3451 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 3452 | applyAdjustment(n, adjustment); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3453 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3454 | mRankingHandler.requestSort(); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3455 | } finally { |
| 3456 | Binder.restoreCallingIdentity(identity); |
| 3457 | } |
| 3458 | } |
| 3459 | |
| 3460 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 3461 | public void applyAdjustmentsFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3462 | List<Adjustment> adjustments) throws RemoteException { |
| 3463 | |
| 3464 | final long identity = Binder.clearCallingIdentity(); |
| 3465 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3466 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3467 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3468 | for (Adjustment adjustment : adjustments) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3469 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 3470 | applyAdjustment(n, adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3471 | } |
| 3472 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3473 | mRankingHandler.requestSort(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3474 | } finally { |
| 3475 | Binder.restoreCallingIdentity(identity); |
| 3476 | } |
| 3477 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3478 | |
| 3479 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 3480 | public void updateNotificationChannelGroupFromPrivilegedListener( |
| 3481 | INotificationListener token, String pkg, UserHandle user, |
| 3482 | NotificationChannelGroup group) throws RemoteException { |
| 3483 | Preconditions.checkNotNull(user); |
| 3484 | verifyPrivilegedListener(token, user); |
| 3485 | createNotificationChannelGroup( |
| 3486 | pkg, getUidForPackageAndUser(pkg, user), group, false, true); |
| 3487 | savePolicyFile(); |
| 3488 | } |
| 3489 | |
| 3490 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3491 | public void updateNotificationChannelFromPrivilegedListener(INotificationListener token, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3492 | String pkg, UserHandle user, NotificationChannel channel) throws RemoteException { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3493 | Preconditions.checkNotNull(channel); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3494 | Preconditions.checkNotNull(pkg); |
| 3495 | Preconditions.checkNotNull(user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3496 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3497 | verifyPrivilegedListener(token, user); |
| 3498 | updateNotificationChannelInt(pkg, getUidForPackageAndUser(pkg, user), channel, true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3499 | } |
| 3500 | |
| 3501 | @Override |
| 3502 | public ParceledListSlice<NotificationChannel> getNotificationChannelsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3503 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 3504 | Preconditions.checkNotNull(pkg); |
| 3505 | Preconditions.checkNotNull(user); |
| 3506 | verifyPrivilegedListener(token, user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3507 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3508 | return mRankingHelper.getNotificationChannels(pkg, getUidForPackageAndUser(pkg, user), |
| 3509 | false /* includeDeleted */); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | @Override |
| 3513 | public ParceledListSlice<NotificationChannelGroup> |
| 3514 | getNotificationChannelGroupsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3515 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 3516 | Preconditions.checkNotNull(pkg); |
| 3517 | Preconditions.checkNotNull(user); |
| 3518 | verifyPrivilegedListener(token, user); |
| 3519 | |
| 3520 | List<NotificationChannelGroup> groups = new ArrayList<>(); |
| 3521 | groups.addAll(mRankingHelper.getNotificationChannelGroups( |
| 3522 | pkg, getUidForPackageAndUser(pkg, user))); |
| 3523 | return new ParceledListSlice<>(groups); |
| 3524 | } |
| 3525 | |
| 3526 | private void verifyPrivilegedListener(INotificationListener token, UserHandle user) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3527 | ManagedServiceInfo info; |
| 3528 | synchronized (mNotificationLock) { |
| 3529 | info = mListeners.checkServiceTokenLocked(token); |
| 3530 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 3531 | if (!hasCompanionDevice(info)) { |
| 3532 | throw new SecurityException(info + " does not have access"); |
| 3533 | } |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3534 | if (!info.enabledAndUserMatches(user.getIdentifier())) { |
| 3535 | throw new SecurityException(info + " does not have access"); |
| 3536 | } |
| 3537 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3538 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3539 | private int getUidForPackageAndUser(String pkg, UserHandle user) throws RemoteException { |
| 3540 | int uid = 0; |
| 3541 | long identity = Binder.clearCallingIdentity(); |
| 3542 | try { |
| 3543 | uid = mPackageManager.getPackageUid(pkg, 0, user.getIdentifier()); |
| 3544 | } finally { |
| 3545 | Binder.restoreCallingIdentity(identity); |
| 3546 | } |
| 3547 | return uid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3548 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3549 | |
| 3550 | @Override |
| 3551 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 3552 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) |
| 3553 | throws RemoteException { |
| 3554 | new ShellCmd().exec(this, in, out, err, args, callback, resultReceiver); |
| 3555 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3556 | }; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 3557 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3558 | private void applyAdjustment(NotificationRecord r, Adjustment adjustment) { |
| 3559 | if (r == null) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3560 | return; |
| 3561 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3562 | if (adjustment.getSignals() != null) { |
| 3563 | Bundle.setDefusable(adjustment.getSignals(), true); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3564 | r.addAdjustment(adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3565 | } |
| 3566 | } |
| 3567 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3568 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3569 | void addAutogroupKeyLocked(String key) { |
| 3570 | NotificationRecord r = mNotificationsByKey.get(key); |
| 3571 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3572 | return; |
| 3573 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3574 | if (r.sbn.getOverrideGroupKey() == null) { |
| 3575 | addAutoGroupAdjustment(r, GroupHelper.AUTOGROUP_KEY); |
| 3576 | EventLogTags.writeNotificationAutogrouped(key); |
| 3577 | mRankingHandler.requestSort(); |
| 3578 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3579 | } |
| 3580 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3581 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3582 | void removeAutogroupKeyLocked(String key) { |
| 3583 | NotificationRecord r = mNotificationsByKey.get(key); |
| 3584 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3585 | return; |
| 3586 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3587 | if (r.sbn.getOverrideGroupKey() != null) { |
| 3588 | addAutoGroupAdjustment(r, null); |
| 3589 | EventLogTags.writeNotificationUnautogrouped(key); |
| 3590 | mRankingHandler.requestSort(); |
| 3591 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3592 | } |
| 3593 | |
| 3594 | private void addAutoGroupAdjustment(NotificationRecord r, String overrideGroupKey) { |
| 3595 | Bundle signals = new Bundle(); |
| 3596 | signals.putString(Adjustment.KEY_GROUP_KEY, overrideGroupKey); |
| 3597 | Adjustment adjustment = |
| 3598 | new Adjustment(r.sbn.getPackageName(), r.getKey(), signals, "", r.sbn.getUserId()); |
| 3599 | r.addAdjustment(adjustment); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | // Clears the 'fake' auto-group summary. |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3603 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3604 | private void clearAutogroupSummaryLocked(int userId, String pkg) { |
| 3605 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 3606 | if (summaries != null && summaries.containsKey(pkg)) { |
| 3607 | // Clear summary. |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3608 | final NotificationRecord removed = findNotificationByKeyLocked(summaries.remove(pkg)); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3609 | if (removed != null) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 3610 | boolean wasPosted = removeFromNotificationListsLocked(removed); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 3611 | cancelNotificationLocked(removed, false, REASON_UNAUTOBUNDLED, wasPosted, null); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3612 | } |
| 3613 | } |
| 3614 | } |
| 3615 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 3616 | @GuardedBy("mNotificationLock") |
| 3617 | private boolean hasAutoGroupSummaryLocked(StatusBarNotification sbn) { |
| 3618 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(sbn.getUserId()); |
| 3619 | return summaries != null && summaries.containsKey(sbn.getPackageName()); |
| 3620 | } |
| 3621 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3622 | // 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] | 3623 | private void createAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 3624 | NotificationRecord summaryRecord = null; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3625 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3626 | NotificationRecord notificationRecord = mNotificationsByKey.get(triggeringKey); |
| 3627 | if (notificationRecord == null) { |
| 3628 | // The notification could have been cancelled again already. A successive |
| 3629 | // adjustment will post a summary if needed. |
| 3630 | return; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3631 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3632 | final StatusBarNotification adjustedSbn = notificationRecord.sbn; |
| 3633 | userId = adjustedSbn.getUser().getIdentifier(); |
| 3634 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 3635 | if (summaries == null) { |
| 3636 | summaries = new ArrayMap<>(); |
| 3637 | } |
| 3638 | mAutobundledSummaries.put(userId, summaries); |
| 3639 | if (!summaries.containsKey(pkg)) { |
| 3640 | // Add summary |
| 3641 | final ApplicationInfo appInfo = |
| 3642 | adjustedSbn.getNotification().extras.getParcelable( |
| 3643 | Notification.EXTRA_BUILDER_APPLICATION_INFO); |
| 3644 | final Bundle extras = new Bundle(); |
| 3645 | extras.putParcelable(Notification.EXTRA_BUILDER_APPLICATION_INFO, appInfo); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3646 | final String channelId = notificationRecord.getChannel().getId(); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3647 | final Notification summaryNotification = |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3648 | new Notification.Builder(getContext(), channelId) |
| 3649 | .setSmallIcon(adjustedSbn.getNotification().getSmallIcon()) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3650 | .setGroupSummary(true) |
Julia Reynolds | 9d5786e | 2017-04-28 10:26:32 -0400 | [diff] [blame] | 3651 | .setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3652 | .setGroup(GroupHelper.AUTOGROUP_KEY) |
| 3653 | .setFlag(Notification.FLAG_AUTOGROUP_SUMMARY, true) |
| 3654 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 3655 | .setColor(adjustedSbn.getNotification().color) |
| 3656 | .setLocalOnly(true) |
| 3657 | .build(); |
| 3658 | summaryNotification.extras.putAll(extras); |
| 3659 | Intent appIntent = getContext().getPackageManager().getLaunchIntentForPackage(pkg); |
| 3660 | if (appIntent != null) { |
| 3661 | summaryNotification.contentIntent = PendingIntent.getActivityAsUser( |
| 3662 | getContext(), 0, appIntent, 0, null, UserHandle.of(userId)); |
| 3663 | } |
| 3664 | final StatusBarNotification summarySbn = |
| 3665 | new StatusBarNotification(adjustedSbn.getPackageName(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3666 | adjustedSbn.getOpPkg(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3667 | Integer.MAX_VALUE, |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3668 | GroupHelper.AUTOGROUP_KEY, adjustedSbn.getUid(), |
| 3669 | adjustedSbn.getInitialPid(), summaryNotification, |
| 3670 | adjustedSbn.getUser(), GroupHelper.AUTOGROUP_KEY, |
| 3671 | System.currentTimeMillis()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 3672 | summaryRecord = new NotificationRecord(getContext(), summarySbn, |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 3673 | notificationRecord.getChannel()); |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 3674 | summaryRecord.setIsAppImportanceLocked( |
| 3675 | notificationRecord.getIsAppImportanceLocked()); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3676 | summaries.put(pkg, summarySbn.getKey()); |
| 3677 | } |
| 3678 | } |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3679 | if (summaryRecord != null && checkDisqualifyingFeatures(userId, MY_UID, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3680 | summaryRecord.sbn.getId(), summaryRecord.sbn.getTag(), summaryRecord, true)) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3681 | mHandler.post(new EnqueueNotificationRunnable(userId, summaryRecord)); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3682 | } |
| 3683 | } |
| 3684 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3685 | private String disableNotificationEffects(NotificationRecord record) { |
| 3686 | if (mDisableNotificationEffects) { |
| 3687 | return "booleanState"; |
| 3688 | } |
| 3689 | if ((mListenerHints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 3690 | return "listenerHints"; |
| 3691 | } |
| 3692 | if (mCallState != TelephonyManager.CALL_STATE_IDLE && !mZenModeHelper.isCall(record)) { |
| 3693 | return "callState"; |
| 3694 | } |
| 3695 | return null; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3696 | }; |
| 3697 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3698 | private void dumpJson(PrintWriter pw, @NonNull DumpFilter filter) { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3699 | JSONObject dump = new JSONObject(); |
| 3700 | try { |
| 3701 | dump.put("service", "Notification Manager"); |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 3702 | dump.put("bans", mRankingHelper.dumpBansJson(filter)); |
| 3703 | dump.put("ranking", mRankingHelper.dumpJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3704 | dump.put("stats", mUsageStats.dumpJson(filter)); |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 3705 | dump.put("channels", mRankingHelper.dumpChannelsJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3706 | } catch (JSONException e) { |
| 3707 | e.printStackTrace(); |
| 3708 | } |
| 3709 | pw.println(dump); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3710 | } |
| 3711 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3712 | private void dumpProto(FileDescriptor fd, @NonNull DumpFilter filter) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3713 | final ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 3714 | synchronized (mNotificationLock) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3715 | int N = mNotificationList.size(); |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 3716 | for (int i = 0; i < N; i++) { |
| 3717 | final NotificationRecord nr = mNotificationList.get(i); |
| 3718 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3719 | nr.dump(proto, NotificationServiceDumpProto.RECORDS, filter.redact, |
| 3720 | NotificationRecordProto.POSTED); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3721 | } |
| 3722 | N = mEnqueuedNotifications.size(); |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 3723 | for (int i = 0; i < N; i++) { |
| 3724 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 3725 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3726 | nr.dump(proto, NotificationServiceDumpProto.RECORDS, filter.redact, |
| 3727 | NotificationRecordProto.ENQUEUED); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3728 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3729 | List<NotificationRecord> snoozed = mSnoozeHelper.getSnoozed(); |
| 3730 | N = snoozed.size(); |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 3731 | for (int i = 0; i < N; i++) { |
| 3732 | final NotificationRecord nr = snoozed.get(i); |
| 3733 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3734 | nr.dump(proto, NotificationServiceDumpProto.RECORDS, filter.redact, |
| 3735 | NotificationRecordProto.SNOOZED); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3736 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3737 | |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3738 | long zenLog = proto.start(NotificationServiceDumpProto.ZEN); |
| 3739 | mZenModeHelper.dump(proto); |
| 3740 | for (ComponentName suppressor : mEffectsSuppressors) { |
Kweku Adams | 9954633 | 2018-01-24 17:03:50 -0800 | [diff] [blame] | 3741 | suppressor.writeToProto(proto, ZenModeProto.SUPPRESSORS); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3742 | } |
| 3743 | proto.end(zenLog); |
| 3744 | |
| 3745 | long listenersToken = proto.start(NotificationServiceDumpProto.NOTIFICATION_LISTENERS); |
| 3746 | mListeners.dump(proto, filter); |
| 3747 | proto.end(listenersToken); |
| 3748 | |
| 3749 | proto.write(NotificationServiceDumpProto.LISTENER_HINTS, mListenerHints); |
| 3750 | |
| 3751 | for (int i = 0; i < mListenersDisablingEffects.size(); ++i) { |
| 3752 | long effectsToken = proto.start( |
| 3753 | NotificationServiceDumpProto.LISTENERS_DISABLING_EFFECTS); |
| 3754 | |
| 3755 | proto.write( |
| 3756 | ListenersDisablingEffectsProto.HINT, mListenersDisablingEffects.keyAt(i)); |
| 3757 | final ArraySet<ManagedServiceInfo> listeners = |
| 3758 | mListenersDisablingEffects.valueAt(i); |
| 3759 | for (int j = 0; j < listeners.size(); j++) { |
| 3760 | final ManagedServiceInfo listener = listeners.valueAt(i); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 3761 | listener.writeToProto(proto, ListenersDisablingEffectsProto.LISTENERS, null); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | proto.end(effectsToken); |
| 3765 | } |
| 3766 | |
| 3767 | long assistantsToken = proto.start( |
| 3768 | NotificationServiceDumpProto.NOTIFICATION_ASSISTANTS); |
| 3769 | mAssistants.dump(proto, filter); |
| 3770 | proto.end(assistantsToken); |
| 3771 | |
| 3772 | long conditionsToken = proto.start(NotificationServiceDumpProto.CONDITION_PROVIDERS); |
| 3773 | mConditionProviders.dump(proto, filter); |
| 3774 | proto.end(conditionsToken); |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 3775 | |
| 3776 | long rankingToken = proto.start(NotificationServiceDumpProto.RANKING_CONFIG); |
| 3777 | mRankingHelper.dump(proto, filter); |
| 3778 | proto.end(rankingToken); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3779 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3780 | |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3781 | proto.flush(); |
| 3782 | } |
| 3783 | |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 3784 | private void dumpNotificationRecords(PrintWriter pw, @NonNull DumpFilter filter) { |
| 3785 | synchronized (mNotificationLock) { |
| 3786 | int N; |
| 3787 | N = mNotificationList.size(); |
| 3788 | if (N > 0) { |
| 3789 | pw.println(" Notification List:"); |
| 3790 | for (int i = 0; i < N; i++) { |
| 3791 | final NotificationRecord nr = mNotificationList.get(i); |
| 3792 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3793 | nr.dump(pw, " ", getContext(), filter.redact); |
| 3794 | } |
| 3795 | pw.println(" "); |
| 3796 | } |
| 3797 | } |
| 3798 | } |
| 3799 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3800 | void dumpImpl(PrintWriter pw, @NonNull DumpFilter filter) { |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3801 | pw.print("Current Notification Manager state"); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3802 | if (filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3803 | pw.print(" (filtered to "); pw.print(filter); pw.print(")"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3804 | } |
| 3805 | pw.println(':'); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3806 | int N; |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3807 | final boolean zenOnly = filter.filtered && filter.zen; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3808 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3809 | if (!zenOnly) { |
| 3810 | synchronized (mToastQueue) { |
| 3811 | N = mToastQueue.size(); |
| 3812 | if (N > 0) { |
| 3813 | pw.println(" Toast Queue:"); |
| 3814 | for (int i=0; i<N; i++) { |
| 3815 | mToastQueue.get(i).dump(pw, " ", filter); |
| 3816 | } |
| 3817 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3818 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3819 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3820 | } |
| 3821 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3822 | synchronized (mNotificationLock) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3823 | if (!zenOnly) { |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 3824 | // Priority filters are only set when called via bugreport. If set |
| 3825 | // skip sections that are part of the critical section. |
| 3826 | if (!filter.normalPriority) { |
| 3827 | dumpNotificationRecords(pw, filter); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3828 | } |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3829 | if (!filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3830 | N = mLights.size(); |
| 3831 | if (N > 0) { |
| 3832 | pw.println(" Lights List:"); |
| 3833 | for (int i=0; i<N; i++) { |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3834 | if (i == N - 1) { |
| 3835 | pw.print(" > "); |
| 3836 | } else { |
| 3837 | pw.print(" "); |
| 3838 | } |
| 3839 | pw.println(mLights.get(i)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3840 | } |
| 3841 | pw.println(" "); |
| 3842 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 3843 | pw.println(" mUseAttentionLight=" + mUseAttentionLight); |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 3844 | pw.println(" mHasLight=" + mHasLight); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 3845 | pw.println(" mNotificationPulseEnabled=" + mNotificationPulseEnabled); |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3846 | pw.println(" mSoundNotificationKey=" + mSoundNotificationKey); |
| 3847 | pw.println(" mVibrateNotificationKey=" + mVibrateNotificationKey); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3848 | pw.println(" mDisableNotificationEffects=" + mDisableNotificationEffects); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3849 | pw.println(" mCallState=" + callStateToString(mCallState)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3850 | pw.println(" mSystemReady=" + mSystemReady); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 3851 | pw.println(" mMaxPackageEnqueueRate=" + mMaxPackageEnqueueRate); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3852 | } |
| 3853 | pw.println(" mArchive=" + mArchive.toString()); |
| 3854 | Iterator<StatusBarNotification> iter = mArchive.descendingIterator(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3855 | int j=0; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3856 | while (iter.hasNext()) { |
| 3857 | final StatusBarNotification sbn = iter.next(); |
| 3858 | if (filter != null && !filter.matches(sbn)) continue; |
| 3859 | pw.println(" " + sbn); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3860 | if (++j >= 5) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3861 | if (iter.hasNext()) pw.println(" ..."); |
| 3862 | break; |
| 3863 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3864 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3865 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3866 | if (!zenOnly) { |
| 3867 | N = mEnqueuedNotifications.size(); |
| 3868 | if (N > 0) { |
| 3869 | pw.println(" Enqueued Notification List:"); |
| 3870 | for (int i = 0; i < N; i++) { |
| 3871 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 3872 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3873 | nr.dump(pw, " ", getContext(), filter.redact); |
| 3874 | } |
| 3875 | pw.println(" "); |
| 3876 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3877 | |
| 3878 | mSnoozeHelper.dump(pw, filter); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3879 | } |
| 3880 | } |
| 3881 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3882 | if (!zenOnly) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3883 | pw.println("\n Ranking Config:"); |
| 3884 | mRankingHelper.dump(pw, " ", filter); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 3885 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3886 | pw.println("\n Notification listeners:"); |
| 3887 | mListeners.dump(pw, filter); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3888 | pw.print(" mListenerHints: "); pw.println(mListenerHints); |
| 3889 | pw.print(" mListenersDisablingEffects: ("); |
| 3890 | N = mListenersDisablingEffects.size(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3891 | for (int i = 0; i < N; i++) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 3892 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 3893 | if (i > 0) pw.print(';'); |
| 3894 | pw.print("hint[" + hint + "]:"); |
| 3895 | |
| 3896 | final ArraySet<ManagedServiceInfo> listeners = |
| 3897 | mListenersDisablingEffects.valueAt(i); |
| 3898 | final int listenerSize = listeners.size(); |
| 3899 | |
| 3900 | for (int j = 0; j < listenerSize; j++) { |
| 3901 | if (i > 0) pw.print(','); |
| 3902 | final ManagedServiceInfo listener = listeners.valueAt(i); |
Julia Reynolds | 1f58057 | 2018-04-27 14:48:36 -0400 | [diff] [blame] | 3903 | if (listener != null) { |
| 3904 | pw.print(listener.component); |
| 3905 | } |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 3906 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3907 | } |
| 3908 | pw.println(')'); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 3909 | pw.println("\n Notification assistant services:"); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3910 | mAssistants.dump(pw, filter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3911 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3912 | |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3913 | if (!filter.filtered || zenOnly) { |
| 3914 | pw.println("\n Zen Mode:"); |
| 3915 | pw.print(" mInterruptionFilter="); pw.println(mInterruptionFilter); |
| 3916 | mZenModeHelper.dump(pw, " "); |
| 3917 | |
| 3918 | pw.println("\n Zen Log:"); |
| 3919 | ZenLog.dump(pw, " "); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3920 | } |
| 3921 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3922 | pw.println("\n Condition providers:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3923 | mConditionProviders.dump(pw, filter); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3924 | |
| 3925 | pw.println("\n Group summaries:"); |
| 3926 | for (Entry<String, NotificationRecord> entry : mSummaryByGroupKey.entrySet()) { |
| 3927 | NotificationRecord r = entry.getValue(); |
| 3928 | pw.println(" " + entry.getKey() + " -> " + r.getKey()); |
| 3929 | if (mNotificationsByKey.get(r.getKey()) != r) { |
| 3930 | pw.println("!!!!!!LEAK: Record not found in mNotificationsByKey."); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3931 | r.dump(pw, " ", getContext(), filter.redact); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3932 | } |
| 3933 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3934 | |
| 3935 | if (!zenOnly) { |
| 3936 | pw.println("\n Usage Stats:"); |
| 3937 | mUsageStats.dump(pw, " ", filter); |
| 3938 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3939 | } |
| 3940 | } |
| 3941 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3942 | /** |
| 3943 | * The private API only accessible to the system process. |
| 3944 | */ |
| 3945 | private final NotificationManagerInternal mInternalService = new NotificationManagerInternal() { |
| 3946 | @Override |
Julia Reynolds | f3de8aa | 2017-09-29 15:52:37 -0400 | [diff] [blame] | 3947 | public NotificationChannel getNotificationChannel(String pkg, int uid, String |
| 3948 | channelId) { |
| 3949 | return mRankingHelper.getNotificationChannel(pkg, uid, channelId, false); |
| 3950 | } |
| 3951 | |
| 3952 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3953 | public void enqueueNotification(String pkg, String opPkg, int callingUid, int callingPid, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3954 | String tag, int id, Notification notification, int userId) { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3955 | enqueueNotificationInternal(pkg, opPkg, callingUid, callingPid, tag, id, notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3956 | userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3957 | } |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3958 | |
| 3959 | @Override |
| 3960 | public void removeForegroundServiceFlagFromNotification(String pkg, int notificationId, |
| 3961 | int userId) { |
| 3962 | checkCallerIsSystem(); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3963 | mHandler.post(new Runnable() { |
| 3964 | @Override |
| 3965 | public void run() { |
| 3966 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3967 | removeForegroundServiceFlagByListLocked( |
| 3968 | mEnqueuedNotifications, pkg, notificationId, userId); |
| 3969 | removeForegroundServiceFlagByListLocked( |
| 3970 | mNotificationList, pkg, notificationId, userId); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3971 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3972 | } |
| 3973 | }); |
| 3974 | } |
| 3975 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3976 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3977 | private void removeForegroundServiceFlagByListLocked( |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3978 | ArrayList<NotificationRecord> notificationList, String pkg, int notificationId, |
| 3979 | int userId) { |
| 3980 | NotificationRecord r = findNotificationByListLocked( |
| 3981 | notificationList, pkg, null, notificationId, userId); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3982 | if (r == null) { |
| 3983 | return; |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3984 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3985 | StatusBarNotification sbn = r.sbn; |
| 3986 | // NoMan adds flags FLAG_NO_CLEAR and FLAG_ONGOING_EVENT when it sees |
| 3987 | // FLAG_FOREGROUND_SERVICE. Hence it's not enough to remove |
| 3988 | // FLAG_FOREGROUND_SERVICE, we have to revert to the flags we received |
| 3989 | // initially *and* force remove FLAG_FOREGROUND_SERVICE. |
| 3990 | sbn.getNotification().flags = |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 3991 | (r.mOriginalFlags & ~FLAG_FOREGROUND_SERVICE); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3992 | mRankingHelper.sort(mNotificationList); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 3993 | mListeners.notifyPostedLocked(r, r); |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3994 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3995 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3996 | |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3997 | void enqueueNotificationInternal(final String pkg, final String opPkg, final int callingUid, |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 3998 | final int callingPid, final String tag, final int id, final Notification notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3999 | int incomingUserId) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 4000 | if (DBG) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4001 | Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id |
| 4002 | + " notification=" + notification); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 4003 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4004 | checkCallerIsSystemOrSameApp(pkg); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4005 | |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 4006 | final int userId = ActivityManager.handleIncomingUser(callingPid, |
| 4007 | callingUid, incomingUserId, true, false, "enqueueNotification", pkg); |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 4008 | final UserHandle user = new UserHandle(userId); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 4009 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4010 | if (pkg == null || notification == null) { |
| 4011 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 4012 | + " id=" + id + " notification=" + notification); |
| 4013 | } |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4014 | |
| 4015 | // The system can post notifications for any package, let us resolve that. |
| 4016 | final int notificationUid = resolveNotificationUid(opPkg, callingUid, userId); |
| 4017 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4018 | // Fix the notification as best we can. |
| 4019 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4020 | final ApplicationInfo ai = mPackageManagerClient.getApplicationInfoAsUser( |
Jeff Sharkey | 012bc7b | 2016-04-11 16:30:27 -0600 | [diff] [blame] | 4021 | pkg, PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 4022 | (userId == UserHandle.USER_ALL) ? USER_SYSTEM : userId); |
Julia Reynolds | e071abd | 2017-03-22 10:52:11 -0400 | [diff] [blame] | 4023 | Notification.addFieldsFromContext(ai, notification); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 4024 | |
| 4025 | int canColorize = mPackageManagerClient.checkPermission( |
| 4026 | android.Manifest.permission.USE_COLORIZED_NOTIFICATIONS, pkg); |
| 4027 | if (canColorize == PERMISSION_GRANTED) { |
| 4028 | notification.flags |= Notification.FLAG_CAN_COLORIZE; |
| 4029 | } else { |
| 4030 | notification.flags &= ~Notification.FLAG_CAN_COLORIZE; |
| 4031 | } |
| 4032 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4033 | } catch (NameNotFoundException e) { |
| 4034 | Slog.e(TAG, "Cannot create a context for sending app", e); |
| 4035 | return; |
| 4036 | } |
| 4037 | |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 4038 | mUsageStats.registerEnqueuedByApp(pkg); |
| 4039 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4040 | // setup local book-keeping |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 4041 | String channelId = notification.getChannelId(); |
| 4042 | if (mIsTelevision && (new Notification.TvExtender(notification)).getChannelId() != null) { |
| 4043 | channelId = (new Notification.TvExtender(notification)).getChannelId(); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 4044 | } |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 4045 | final NotificationChannel channel = mRankingHelper.getNotificationChannel(pkg, |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4046 | notificationUid, channelId, false /* includeDeleted */); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 4047 | if (channel == null) { |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 4048 | final String noChannelStr = "No Channel found for " |
| 4049 | + "pkg=" + pkg |
| 4050 | + ", channelId=" + channelId |
Julia Reynolds | f26eb91 | 2017-05-22 15:47:06 -0400 | [diff] [blame] | 4051 | + ", id=" + id |
| 4052 | + ", tag=" + tag |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 4053 | + ", opPkg=" + opPkg |
| 4054 | + ", callingUid=" + callingUid |
| 4055 | + ", userId=" + userId |
| 4056 | + ", incomingUserId=" + incomingUserId |
| 4057 | + ", notificationUid=" + notificationUid |
| 4058 | + ", notification=" + notification; |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 4059 | Log.e(TAG, noChannelStr); |
Beverly | 5d4564b | 2018-04-10 20:09:23 -0400 | [diff] [blame] | 4060 | boolean appNotificationsOff = mRankingHelper.getImportance(pkg, notificationUid) |
| 4061 | == NotificationManager.IMPORTANCE_NONE; |
| 4062 | |
| 4063 | if (!appNotificationsOff) { |
| 4064 | doChannelWarningToast("Developer warning for package \"" + pkg + "\"\n" + |
| 4065 | "Failed to post notification on channel \"" + channelId + "\"\n" + |
| 4066 | "See log for more details"); |
| 4067 | } |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 4068 | return; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 4069 | } |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 4070 | |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 4071 | final StatusBarNotification n = new StatusBarNotification( |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4072 | pkg, opPkg, id, tag, notificationUid, callingPid, notification, |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 4073 | user, null, System.currentTimeMillis()); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 4074 | final NotificationRecord r = new NotificationRecord(getContext(), n, channel); |
Rohan Shah | 590e1b2 | 2018-04-10 23:48:47 -0400 | [diff] [blame] | 4075 | r.setIsAppImportanceLocked(mRankingHelper.getIsAppImportanceLocked(pkg, callingUid)); |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 4076 | |
Dianne Hackborn | 025d4a5 | 2018-04-30 16:23:26 -0700 | [diff] [blame] | 4077 | if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 4078 | final boolean fgServiceShown = channel.isFgServiceShown(); |
| 4079 | if (((channel.getUserLockedFields() & NotificationChannel.USER_LOCKED_IMPORTANCE) == 0 |
| 4080 | || !fgServiceShown) |
| 4081 | && (r.getImportance() == IMPORTANCE_MIN |
| 4082 | || r.getImportance() == IMPORTANCE_NONE)) { |
| 4083 | // Increase the importance of foreground service notifications unless the user had |
| 4084 | // an opinion otherwise (and the channel hasn't yet shown a fg service). |
| 4085 | if (TextUtils.isEmpty(channelId) |
| 4086 | || NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 4087 | r.setImportance(IMPORTANCE_LOW, "Bumped for foreground service"); |
| 4088 | } else { |
| 4089 | channel.setImportance(IMPORTANCE_LOW); |
| 4090 | if (!fgServiceShown) { |
| 4091 | channel.unlockFields(NotificationChannel.USER_LOCKED_IMPORTANCE); |
| 4092 | channel.setFgServiceShown(true); |
| 4093 | } |
| 4094 | mRankingHelper.updateNotificationChannel(pkg, notificationUid, channel, false); |
| 4095 | r.updateNotificationChannel(channel); |
| 4096 | } |
| 4097 | } else if (!fgServiceShown && !TextUtils.isEmpty(channelId) |
| 4098 | && !NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 4099 | channel.setFgServiceShown(true); |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 4100 | r.updateNotificationChannel(channel); |
| 4101 | } |
| 4102 | } |
| 4103 | |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 4104 | if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r, |
| 4105 | r.sbn.getOverrideGroupKey() != null)) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4106 | return; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 4107 | } |
| 4108 | |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 4109 | // Whitelist pending intents. |
| 4110 | if (notification.allPendingIntents != null) { |
| 4111 | final int intentCount = notification.allPendingIntents.size(); |
| 4112 | if (intentCount > 0) { |
| 4113 | final ActivityManagerInternal am = LocalServices |
| 4114 | .getService(ActivityManagerInternal.class); |
| 4115 | final long duration = LocalServices.getService( |
| 4116 | DeviceIdleController.LocalService.class).getNotificationWhitelistDuration(); |
| 4117 | for (int i = 0; i < intentCount; i++) { |
| 4118 | PendingIntent pendingIntent = notification.allPendingIntents.valueAt(i); |
| 4119 | if (pendingIntent != null) { |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 4120 | am.setPendingIntentWhitelistDuration(pendingIntent.getTarget(), |
| 4121 | WHITELIST_TOKEN, duration); |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 4122 | } |
| 4123 | } |
| 4124 | } |
| 4125 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 4126 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4127 | mHandler.post(new EnqueueNotificationRunnable(userId, r)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4128 | } |
| 4129 | |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 4130 | private void doChannelWarningToast(CharSequence toastText) { |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 4131 | final int defaultWarningEnabled = Build.IS_DEBUGGABLE ? 1 : 0; |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 4132 | final boolean warningEnabled = Settings.Global.getInt(getContext().getContentResolver(), |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 4133 | Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, defaultWarningEnabled) != 0; |
| 4134 | if (warningEnabled) { |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 4135 | Toast toast = Toast.makeText(getContext(), mHandler.getLooper(), toastText, |
Geoffrey Pitsch | 5bdddbd | 2017-05-26 10:50:05 -0400 | [diff] [blame] | 4136 | Toast.LENGTH_SHORT); |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 4137 | toast.show(); |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 4138 | } |
| 4139 | } |
| 4140 | |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4141 | private int resolveNotificationUid(String opPackageName, int callingUid, int userId) { |
| 4142 | // The system can post notifications on behalf of any package it wants |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4143 | if (isCallerSystemOrPhone() && opPackageName != null && !"android".equals(opPackageName)) { |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 4144 | try { |
| 4145 | return getContext().getPackageManager() |
| 4146 | .getPackageUidAsUser(opPackageName, userId); |
| 4147 | } catch (NameNotFoundException e) { |
| 4148 | /* ignore */ |
| 4149 | } |
| 4150 | } |
| 4151 | return callingUid; |
| 4152 | } |
| 4153 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4154 | /** |
| 4155 | * Checks if a notification can be posted. checks rate limiter, snooze helper, and blocking. |
| 4156 | * |
| 4157 | * Has side effects. |
| 4158 | */ |
| 4159 | private boolean checkDisqualifyingFeatures(int userId, int callingUid, int id, String tag, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 4160 | NotificationRecord r, boolean isAutogroup) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4161 | final String pkg = r.sbn.getPackageName(); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4162 | final boolean isSystemNotification = |
| 4163 | isUidSystemOrPhone(callingUid) || ("android".equals(pkg)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4164 | final boolean isNotificationFromListener = mListeners.isListenerPackage(pkg); |
| 4165 | |
| 4166 | // Limit the number of notifications that any given package except the android |
| 4167 | // package or a registered listener can enqueue. Prevents DOS attacks and deals with leaks. |
| 4168 | if (!isSystemNotification && !isNotificationFromListener) { |
| 4169 | synchronized (mNotificationLock) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 4170 | if (mNotificationsByKey.get(r.sbn.getKey()) == null && isCallerInstantApp(pkg)) { |
| 4171 | // Ephemeral apps have some special constraints for notifications. |
| 4172 | // They are not allowed to create new notifications however they are allowed to |
| 4173 | // update notifications created by the system (e.g. a foreground service |
| 4174 | // notification). |
| 4175 | throw new SecurityException("Instant app " + pkg |
| 4176 | + " cannot create notifications"); |
| 4177 | } |
| 4178 | |
| 4179 | // rate limit updates that aren't completed progress notifications |
| 4180 | if (mNotificationsByKey.get(r.sbn.getKey()) != null |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 4181 | && !r.getNotification().hasCompletedProgress() |
| 4182 | && !isAutogroup) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 4183 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4184 | final float appEnqueueRate = mUsageStats.getAppEnqueueRate(pkg); |
| 4185 | if (appEnqueueRate > mMaxPackageEnqueueRate) { |
| 4186 | mUsageStats.registerOverRateQuota(pkg); |
| 4187 | final long now = SystemClock.elapsedRealtime(); |
| 4188 | if ((now - mLastOverRateLogTime) > MIN_PACKAGE_OVERRATE_LOG_INTERVAL) { |
| 4189 | Slog.e(TAG, "Package enqueue rate is " + appEnqueueRate |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 4190 | + ". Shedding " + r.sbn.getKey() + ". package=" + pkg); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4191 | mLastOverRateLogTime = now; |
| 4192 | } |
| 4193 | return false; |
| 4194 | } |
| 4195 | } |
| 4196 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 4197 | // limit the number of outstanding notificationrecords an app can have |
| 4198 | int count = getNotificationCountLocked(pkg, userId, id, tag); |
| 4199 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 4200 | mUsageStats.registerOverCountQuota(pkg); |
| 4201 | Slog.e(TAG, "Package has already posted or enqueued " + count |
| 4202 | + " notifications. Not showing more. package=" + pkg); |
| 4203 | return false; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4204 | } |
| 4205 | } |
| 4206 | } |
| 4207 | |
| 4208 | // snoozed apps |
| 4209 | if (mSnoozeHelper.isSnoozed(userId, pkg, r.getKey())) { |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4210 | MetricsLogger.action(r.getLogMaker() |
| 4211 | .setType(MetricsProto.MetricsEvent.TYPE_UPDATE) |
| 4212 | .setCategory(MetricsProto.MetricsEvent.NOTIFICATION_SNOOZED)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4213 | if (DBG) { |
| 4214 | Slog.d(TAG, "Ignored enqueue for snoozed notification " + r.getKey()); |
| 4215 | } |
| 4216 | mSnoozeHelper.update(userId, r); |
| 4217 | savePolicyFile(); |
| 4218 | return false; |
| 4219 | } |
| 4220 | |
| 4221 | |
| 4222 | // blocked apps |
| 4223 | if (isBlocked(r, mUsageStats)) { |
| 4224 | return false; |
| 4225 | } |
| 4226 | |
| 4227 | return true; |
| 4228 | } |
| 4229 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 4230 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 4231 | protected int getNotificationCountLocked(String pkg, int userId, int excludedId, |
| 4232 | String excludedTag) { |
| 4233 | int count = 0; |
| 4234 | final int N = mNotificationList.size(); |
| 4235 | for (int i = 0; i < N; i++) { |
| 4236 | final NotificationRecord existing = mNotificationList.get(i); |
| 4237 | if (existing.sbn.getPackageName().equals(pkg) |
| 4238 | && existing.sbn.getUserId() == userId) { |
| 4239 | if (existing.sbn.getId() == excludedId |
| 4240 | && TextUtils.equals(existing.sbn.getTag(), excludedTag)) { |
| 4241 | continue; |
| 4242 | } |
| 4243 | count++; |
| 4244 | } |
| 4245 | } |
| 4246 | final int M = mEnqueuedNotifications.size(); |
| 4247 | for (int i = 0; i < M; i++) { |
| 4248 | final NotificationRecord existing = mEnqueuedNotifications.get(i); |
| 4249 | if (existing.sbn.getPackageName().equals(pkg) |
| 4250 | && existing.sbn.getUserId() == userId) { |
| 4251 | count++; |
| 4252 | } |
| 4253 | } |
| 4254 | return count; |
| 4255 | } |
| 4256 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4257 | protected boolean isBlocked(NotificationRecord r, NotificationUsageStats usageStats) { |
| 4258 | final String pkg = r.sbn.getPackageName(); |
| 4259 | final int callingUid = r.sbn.getUid(); |
| 4260 | |
| 4261 | final boolean isPackageSuspended = isPackageSuspendedForUser(pkg, callingUid); |
| 4262 | if (isPackageSuspended) { |
| 4263 | Slog.e(TAG, "Suppressing notification from package due to package " |
| 4264 | + "suspended by administrator."); |
| 4265 | usageStats.registerSuspendedByAdmin(r); |
| 4266 | return isPackageSuspended; |
| 4267 | } |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 4268 | final boolean isBlocked = |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 4269 | mRankingHelper.isGroupBlocked(pkg, callingUid, r.getChannel().getGroup()) |
| 4270 | || mRankingHelper.getImportance(pkg, callingUid) |
| 4271 | == NotificationManager.IMPORTANCE_NONE |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 4272 | || r.getChannel().getImportance() == NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4273 | if (isBlocked) { |
| 4274 | Slog.e(TAG, "Suppressing notification from package by user request."); |
| 4275 | usageStats.registerBlocked(r); |
| 4276 | } |
| 4277 | return isBlocked; |
| 4278 | } |
| 4279 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4280 | protected class SnoozeNotificationRunnable implements Runnable { |
| 4281 | private final String mKey; |
| 4282 | private final long mDuration; |
| 4283 | private final String mSnoozeCriterionId; |
| 4284 | |
| 4285 | SnoozeNotificationRunnable(String key, long duration, String snoozeCriterionId) { |
| 4286 | mKey = key; |
| 4287 | mDuration = duration; |
| 4288 | mSnoozeCriterionId = snoozeCriterionId; |
| 4289 | } |
| 4290 | |
| 4291 | @Override |
| 4292 | public void run() { |
| 4293 | synchronized (mNotificationLock) { |
| 4294 | final NotificationRecord r = findNotificationByKeyLocked(mKey); |
| 4295 | if (r != null) { |
| 4296 | snoozeLocked(r); |
| 4297 | } |
| 4298 | } |
| 4299 | } |
| 4300 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4301 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4302 | void snoozeLocked(NotificationRecord r) { |
| 4303 | if (r.sbn.isGroup()) { |
| 4304 | final List<NotificationRecord> groupNotifications = findGroupNotificationsLocked( |
| 4305 | r.sbn.getPackageName(), r.sbn.getGroupKey(), r.sbn.getUserId()); |
| 4306 | if (r.getNotification().isGroupSummary()) { |
| 4307 | // snooze summary and all children |
| 4308 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 4309 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 4310 | } |
| 4311 | } else { |
| 4312 | // if there is a valid summary for this group, and we are snoozing the only |
| 4313 | // child, also snooze the summary |
| 4314 | if (mSummaryByGroupKey.containsKey(r.sbn.getGroupKey())) { |
| 4315 | if (groupNotifications.size() != 2) { |
| 4316 | snoozeNotificationLocked(r); |
| 4317 | } else { |
| 4318 | // snooze summary and the one child |
| 4319 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 4320 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 4321 | } |
| 4322 | } |
| 4323 | } else { |
| 4324 | snoozeNotificationLocked(r); |
| 4325 | } |
| 4326 | } |
| 4327 | } else { |
| 4328 | // just snooze the one notification |
| 4329 | snoozeNotificationLocked(r); |
| 4330 | } |
| 4331 | } |
| 4332 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4333 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4334 | void snoozeNotificationLocked(NotificationRecord r) { |
| 4335 | MetricsLogger.action(r.getLogMaker() |
| 4336 | .setCategory(MetricsEvent.NOTIFICATION_SNOOZED) |
| 4337 | .setType(MetricsEvent.TYPE_CLOSE) |
Chris Wren | 21a2e72 | 2017-10-02 17:44:53 -0400 | [diff] [blame] | 4338 | .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_SNOOZE_DURATION_MS, |
| 4339 | mDuration) |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4340 | .addTaggedData(MetricsEvent.NOTIFICATION_SNOOZED_CRITERIA, |
| 4341 | mSnoozeCriterionId == null ? 0 : 1)); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4342 | boolean wasPosted = removeFromNotificationListsLocked(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4343 | cancelNotificationLocked(r, false, REASON_SNOOZED, wasPosted, null); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4344 | updateLightsLocked(); |
| 4345 | if (mSnoozeCriterionId != null) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4346 | mAssistants.notifyAssistantSnoozedLocked(r.sbn, mSnoozeCriterionId); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4347 | mSnoozeHelper.snooze(r); |
| 4348 | } else { |
| 4349 | mSnoozeHelper.snooze(r, mDuration); |
| 4350 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4351 | r.recordSnoozed(); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4352 | savePolicyFile(); |
| 4353 | } |
| 4354 | } |
| 4355 | |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 4356 | protected class EnqueueNotificationRunnable implements Runnable { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4357 | private final NotificationRecord r; |
| 4358 | private final int userId; |
| 4359 | |
| 4360 | EnqueueNotificationRunnable(int userId, NotificationRecord r) { |
| 4361 | this.userId = userId; |
| 4362 | this.r = r; |
| 4363 | }; |
| 4364 | |
| 4365 | @Override |
| 4366 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4367 | synchronized (mNotificationLock) { |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 4368 | mEnqueuedNotifications.add(r); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4369 | scheduleTimeoutLocked(r); |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 4370 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4371 | final StatusBarNotification n = r.sbn; |
| 4372 | if (DBG) Slog.d(TAG, "EnqueueNotificationRunnable.run for: " + n.getKey()); |
| 4373 | NotificationRecord old = mNotificationsByKey.get(n.getKey()); |
| 4374 | if (old != null) { |
| 4375 | // Retain ranking information from previous record |
| 4376 | r.copyRankingInformation(old); |
| 4377 | } |
| 4378 | |
| 4379 | final int callingUid = n.getUid(); |
| 4380 | final int callingPid = n.getInitialPid(); |
| 4381 | final Notification notification = n.getNotification(); |
| 4382 | final String pkg = n.getPackageName(); |
| 4383 | final int id = n.getId(); |
| 4384 | final String tag = n.getTag(); |
| 4385 | |
| 4386 | // Handle grouped notifications and bail out early if we |
| 4387 | // can to avoid extracting signals. |
| 4388 | handleGroupedNotificationLocked(r, old, callingUid, callingPid); |
| 4389 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4390 | // if this is a group child, unsnooze parent summary |
| 4391 | if (n.isGroup() && notification.isGroupChild()) { |
| 4392 | mSnoozeHelper.repostGroupSummary(pkg, r.getUserId(), n.getGroupKey()); |
| 4393 | } |
| 4394 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4395 | // This conditional is a dirty hack to limit the logging done on |
| 4396 | // behalf of the download manager without affecting other apps. |
| 4397 | if (!pkg.equals("com.android.providers.downloads") |
| 4398 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| 4399 | int enqueueStatus = EVENTLOG_ENQUEUE_STATUS_NEW; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4400 | if (old != null) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4401 | enqueueStatus = EVENTLOG_ENQUEUE_STATUS_UPDATE; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4402 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4403 | EventLogTags.writeNotificationEnqueue(callingUid, callingPid, |
| 4404 | pkg, id, tag, userId, notification.toString(), |
| 4405 | enqueueStatus); |
| 4406 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4407 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4408 | mRankingHelper.extractSignals(r); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4409 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4410 | // tell the assistant service about the notification |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4411 | if (mAssistants.isEnabled()) { |
| 4412 | mAssistants.onNotificationEnqueued(r); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4413 | mHandler.postDelayed(new PostNotificationRunnable(r.getKey()), |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4414 | DELAY_FOR_ASSISTANT_TIME); |
| 4415 | } else { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4416 | mHandler.post(new PostNotificationRunnable(r.getKey())); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4417 | } |
| 4418 | } |
| 4419 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4420 | } |
| 4421 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 4422 | @GuardedBy("mNotificationLock") |
| 4423 | private boolean isPackageSuspendedLocked(NotificationRecord r) { |
| 4424 | final String pkg = r.sbn.getPackageName(); |
| 4425 | final int callingUid = r.sbn.getUid(); |
| 4426 | |
| 4427 | return isPackageSuspendedForUser(pkg, callingUid); |
| 4428 | } |
| 4429 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4430 | protected class PostNotificationRunnable implements Runnable { |
| 4431 | private final String key; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4432 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4433 | PostNotificationRunnable(String key) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4434 | this.key = key; |
| 4435 | } |
| 4436 | |
| 4437 | @Override |
| 4438 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4439 | synchronized (mNotificationLock) { |
| 4440 | try { |
| 4441 | NotificationRecord r = null; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4442 | int N = mEnqueuedNotifications.size(); |
| 4443 | for (int i = 0; i < N; i++) { |
| 4444 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 4445 | if (Objects.equals(key, enqueued.getKey())) { |
| 4446 | r = enqueued; |
| 4447 | break; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4448 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4449 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4450 | if (r == null) { |
| 4451 | Slog.i(TAG, "Cannot find enqueued record for key: " + key); |
| 4452 | return; |
| 4453 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 4454 | |
| 4455 | r.setHidden(isPackageSuspendedLocked(r)); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4456 | NotificationRecord old = mNotificationsByKey.get(key); |
| 4457 | final StatusBarNotification n = r.sbn; |
| 4458 | final Notification notification = n.getNotification(); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4459 | int index = indexOfNotificationLocked(n.getKey()); |
| 4460 | if (index < 0) { |
| 4461 | mNotificationList.add(r); |
| 4462 | mUsageStats.registerPostedByApp(r); |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 4463 | r.setInterruptive(isVisuallyInterruptive(null, r)); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4464 | } else { |
| 4465 | old = mNotificationList.get(index); |
| 4466 | mNotificationList.set(index, r); |
| 4467 | mUsageStats.registerUpdatedByApp(r, old); |
| 4468 | // Make sure we don't lose the foreground service state. |
| 4469 | notification.flags |= |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4470 | old.getNotification().flags & FLAG_FOREGROUND_SERVICE; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4471 | r.isUpdate = true; |
Julia Reynolds | b3c68ff | 2018-05-22 14:58:39 -0400 | [diff] [blame] | 4472 | r.setTextChanged(isVisuallyInterruptive(old, r)); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | mNotificationsByKey.put(n.getKey(), r); |
| 4476 | |
| 4477 | // Ensure if this is a foreground service that the proper additional |
| 4478 | // flags are set. |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4479 | if ((notification.flags & FLAG_FOREGROUND_SERVICE) != 0) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4480 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 4481 | | Notification.FLAG_NO_CLEAR; |
| 4482 | } |
| 4483 | |
| 4484 | applyZenModeLocked(r); |
| 4485 | mRankingHelper.sort(mNotificationList); |
| 4486 | |
| 4487 | if (notification.getSmallIcon() != null) { |
| 4488 | StatusBarNotification oldSbn = (old != null) ? old.sbn : null; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 4489 | mListeners.notifyPostedLocked(r, old); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 4490 | if (oldSbn == null || !Objects.equals(oldSbn.getGroup(), n.getGroup())) { |
| 4491 | mHandler.post(new Runnable() { |
| 4492 | @Override |
| 4493 | public void run() { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 4494 | mGroupHelper.onNotificationPosted( |
| 4495 | n, hasAutoGroupSummaryLocked(n)); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 4496 | } |
| 4497 | }); |
| 4498 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4499 | } else { |
| 4500 | Slog.e(TAG, "Not posting notification without small icon: " + notification); |
| 4501 | if (old != null && !old.isCanceled) { |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 4502 | mListeners.notifyRemovedLocked(r, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4503 | NotificationListenerService.REASON_ERROR, null); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4504 | mHandler.post(new Runnable() { |
| 4505 | @Override |
| 4506 | public void run() { |
| 4507 | mGroupHelper.onNotificationRemoved(n); |
| 4508 | } |
| 4509 | }); |
| 4510 | } |
| 4511 | // ATTENTION: in a future release we will bail out here |
| 4512 | // so that we do not play sounds, show lights, etc. for invalid |
| 4513 | // notifications |
| 4514 | Slog.e(TAG, "WARNING: In a future release this will crash the app: " |
| 4515 | + n.getPackageName()); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4516 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4517 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 4518 | if (!r.isHidden()) { |
| 4519 | buzzBeepBlinkLocked(r); |
| 4520 | } |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame] | 4521 | maybeRecordInterruptionLocked(r); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4522 | } finally { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4523 | int N = mEnqueuedNotifications.size(); |
| 4524 | for (int i = 0; i < N; i++) { |
| 4525 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 4526 | if (Objects.equals(key, enqueued.getKey())) { |
| 4527 | mEnqueuedNotifications.remove(i); |
| 4528 | break; |
| 4529 | } |
| 4530 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4531 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4532 | } |
| 4533 | } |
| 4534 | } |
| 4535 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4536 | /** |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4537 | * If the notification differs enough visually, consider it a new interruptive notification. |
| 4538 | */ |
| 4539 | @GuardedBy("mNotificationLock") |
| 4540 | @VisibleForTesting |
| 4541 | protected boolean isVisuallyInterruptive(NotificationRecord old, NotificationRecord r) { |
Julia Reynolds | 6b302d0 | 2018-06-19 15:39:23 -0400 | [diff] [blame] | 4542 | // Ignore summary updates because we don't display most of the information. |
| 4543 | if (r.sbn.isGroup() && r.sbn.getNotification().isGroupSummary()) { |
| 4544 | if (DEBUG_INTERRUPTIVENESS) { |
| 4545 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4546 | + r.getKey() + " is not interruptive: summary"); |
| 4547 | } |
| 4548 | return false; |
| 4549 | } |
| 4550 | |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4551 | if (old == null) { |
| 4552 | if (DEBUG_INTERRUPTIVENESS) { |
| 4553 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4554 | + r.getKey() + " is interruptive: new notification"); |
| 4555 | } |
| 4556 | return true; |
| 4557 | } |
| 4558 | |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 4559 | if (r == null) { |
| 4560 | if (DEBUG_INTERRUPTIVENESS) { |
| 4561 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4562 | + r.getKey() + " is not interruptive: null"); |
| 4563 | } |
| 4564 | return false; |
| 4565 | } |
| 4566 | |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4567 | Notification oldN = old.sbn.getNotification(); |
| 4568 | Notification newN = r.sbn.getNotification(); |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4569 | |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4570 | if (oldN.extras == null || newN.extras == null) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4571 | if (DEBUG_INTERRUPTIVENESS) { |
| 4572 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4573 | + r.getKey() + " is not interruptive: no extras"); |
| 4574 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4575 | return false; |
| 4576 | } |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4577 | |
| 4578 | // Ignore visual interruptions from foreground services because users |
| 4579 | // consider them one 'session'. Count them for everything else. |
Julia Reynolds | a4fb9da | 2018-06-04 12:27:58 -0400 | [diff] [blame] | 4580 | if ((r.sbn.getNotification().flags & FLAG_FOREGROUND_SERVICE) != 0) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4581 | if (DEBUG_INTERRUPTIVENESS) { |
| 4582 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4583 | + r.getKey() + " is not interruptive: foreground service"); |
| 4584 | } |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 4585 | return false; |
| 4586 | } |
| 4587 | |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4588 | final String oldTitle = String.valueOf(oldN.extras.get(Notification.EXTRA_TITLE)); |
| 4589 | final String newTitle = String.valueOf(newN.extras.get(Notification.EXTRA_TITLE)); |
| 4590 | if (!Objects.equals(oldTitle, newTitle)) { |
| 4591 | if (DEBUG_INTERRUPTIVENESS) { |
| 4592 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4593 | + r.getKey() + " is interruptive: changed title"); |
| 4594 | Log.v(TAG, "INTERRUPTIVENESS: " + String.format(" old title: %s (%s@0x%08x)", |
| 4595 | oldTitle, oldTitle.getClass(), oldTitle.hashCode())); |
| 4596 | Log.v(TAG, "INTERRUPTIVENESS: " + String.format(" new title: %s (%s@0x%08x)", |
| 4597 | newTitle, newTitle.getClass(), newTitle.hashCode())); |
| 4598 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4599 | return true; |
| 4600 | } |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4601 | // Do not compare Spannables (will always return false); compare unstyled Strings |
| 4602 | final String oldText = String.valueOf(oldN.extras.get(Notification.EXTRA_TEXT)); |
| 4603 | final String newText = String.valueOf(newN.extras.get(Notification.EXTRA_TEXT)); |
| 4604 | if (!Objects.equals(oldText, newText)) { |
| 4605 | if (DEBUG_INTERRUPTIVENESS) { |
| 4606 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4607 | + r.getKey() + " is interruptive: changed text"); |
| 4608 | Log.v(TAG, "INTERRUPTIVENESS: " + String.format(" old text: %s (%s@0x%08x)", |
| 4609 | oldText, oldText.getClass(), oldText.hashCode())); |
| 4610 | Log.v(TAG, "INTERRUPTIVENESS: " + String.format(" new text: %s (%s@0x%08x)", |
| 4611 | newText, newText.getClass(), newText.hashCode())); |
| 4612 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4613 | return true; |
| 4614 | } |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4615 | if (oldN.hasCompletedProgress() != newN.hasCompletedProgress()) { |
| 4616 | if (DEBUG_INTERRUPTIVENESS) { |
| 4617 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4618 | + r.getKey() + " is interruptive: completed progress"); |
| 4619 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4620 | return true; |
| 4621 | } |
| 4622 | // Actions |
| 4623 | if (Notification.areActionsVisiblyDifferent(oldN, newN)) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4624 | if (DEBUG_INTERRUPTIVENESS) { |
| 4625 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4626 | + r.getKey() + " is interruptive: changed actions"); |
| 4627 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4628 | return true; |
| 4629 | } |
| 4630 | |
| 4631 | try { |
| 4632 | Notification.Builder oldB = Notification.Builder.recoverBuilder(getContext(), oldN); |
| 4633 | Notification.Builder newB = Notification.Builder.recoverBuilder(getContext(), newN); |
| 4634 | |
| 4635 | // Style based comparisons |
| 4636 | if (Notification.areStyledNotificationsVisiblyDifferent(oldB, newB)) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4637 | if (DEBUG_INTERRUPTIVENESS) { |
| 4638 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4639 | + r.getKey() + " is interruptive: styles differ"); |
| 4640 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4641 | return true; |
| 4642 | } |
| 4643 | |
| 4644 | // Remote views |
| 4645 | if (Notification.areRemoteViewsChanged(oldB, newB)) { |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4646 | if (DEBUG_INTERRUPTIVENESS) { |
| 4647 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4648 | + r.getKey() + " is interruptive: remoteviews differ"); |
| 4649 | } |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4650 | return true; |
| 4651 | } |
| 4652 | } catch (Exception e) { |
| 4653 | Slog.w(TAG, "error recovering builder", e); |
| 4654 | } |
Dan Sandler | 7d67bd4 | 2018-05-15 14:06:38 -0400 | [diff] [blame] | 4655 | |
Julia Reynolds | 7217dc9 | 2018-03-07 12:12:09 -0500 | [diff] [blame] | 4656 | return false; |
| 4657 | } |
| 4658 | |
| 4659 | /** |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 4660 | * Keeps the last 5 packages that have notified, by user. |
| 4661 | */ |
| 4662 | @GuardedBy("mNotificationLock") |
| 4663 | @VisibleForTesting |
| 4664 | protected void logRecentLocked(NotificationRecord r) { |
| 4665 | if (r.isUpdate) { |
| 4666 | return; |
| 4667 | } |
| 4668 | ArrayList<NotifyingApp> recentAppsForUser = |
| 4669 | mRecentApps.getOrDefault(r.getUser().getIdentifier(), new ArrayList<>(6)); |
| 4670 | NotifyingApp na = new NotifyingApp() |
| 4671 | .setPackage(r.sbn.getPackageName()) |
| 4672 | .setUid(r.sbn.getUid()) |
| 4673 | .setLastNotified(r.sbn.getPostTime()); |
| 4674 | // A new notification gets an app moved to the front of the list |
| 4675 | for (int i = recentAppsForUser.size() - 1; i >= 0; i--) { |
| 4676 | NotifyingApp naExisting = recentAppsForUser.get(i); |
| 4677 | if (na.getPackage().equals(naExisting.getPackage()) |
| 4678 | && na.getUid() == naExisting.getUid()) { |
| 4679 | recentAppsForUser.remove(i); |
| 4680 | break; |
| 4681 | } |
| 4682 | } |
| 4683 | // time is always increasing, so always add to the front of the list |
| 4684 | recentAppsForUser.add(0, na); |
| 4685 | if (recentAppsForUser.size() > 5) { |
| 4686 | recentAppsForUser.remove(recentAppsForUser.size() -1); |
| 4687 | } |
| 4688 | mRecentApps.put(r.getUser().getIdentifier(), recentAppsForUser); |
| 4689 | } |
| 4690 | |
| 4691 | /** |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4692 | * Ensures that grouped notification receive their special treatment. |
| 4693 | * |
| 4694 | * <p>Cancels group children if the new notification causes a group to lose |
| 4695 | * its summary.</p> |
| 4696 | * |
| 4697 | * <p>Updates mSummaryByGroupKey.</p> |
| 4698 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4699 | @GuardedBy("mNotificationLock") |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4700 | private void handleGroupedNotificationLocked(NotificationRecord r, NotificationRecord old, |
| 4701 | int callingUid, int callingPid) { |
| 4702 | StatusBarNotification sbn = r.sbn; |
| 4703 | Notification n = sbn.getNotification(); |
Selim Cinek | 5b03ce9 | 2016-05-18 15:16:58 -0700 | [diff] [blame] | 4704 | if (n.isGroupSummary() && !sbn.isAppGroup()) { |
| 4705 | // notifications without a group shouldn't be a summary, otherwise autobundling can |
| 4706 | // lead to bugs |
| 4707 | n.flags &= ~Notification.FLAG_GROUP_SUMMARY; |
| 4708 | } |
| 4709 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4710 | String group = sbn.getGroupKey(); |
| 4711 | boolean isSummary = n.isGroupSummary(); |
| 4712 | |
| 4713 | Notification oldN = old != null ? old.sbn.getNotification() : null; |
| 4714 | String oldGroup = old != null ? old.sbn.getGroupKey() : null; |
| 4715 | boolean oldIsSummary = old != null && oldN.isGroupSummary(); |
| 4716 | |
| 4717 | if (oldIsSummary) { |
| 4718 | NotificationRecord removedSummary = mSummaryByGroupKey.remove(oldGroup); |
| 4719 | if (removedSummary != old) { |
| 4720 | String removedKey = |
| 4721 | removedSummary != null ? removedSummary.getKey() : "<null>"; |
| 4722 | Slog.w(TAG, "Removed summary didn't match old notification: old=" + old.getKey() + |
| 4723 | ", removed=" + removedKey); |
| 4724 | } |
| 4725 | } |
| 4726 | if (isSummary) { |
| 4727 | mSummaryByGroupKey.put(group, r); |
| 4728 | } |
| 4729 | |
| 4730 | // Clear out group children of the old notification if the update |
| 4731 | // causes the group summary to go away. This happens when the old |
| 4732 | // notification was a summary and the new one isn't, or when the old |
| 4733 | // notification was a summary and its group key changed. |
| 4734 | if (oldIsSummary && (!isSummary || !oldGroup.equals(group))) { |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 4735 | cancelGroupChildrenLocked(old, callingUid, callingPid, null, false /* sendDelete */, |
| 4736 | null); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4737 | } |
| 4738 | } |
| 4739 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4740 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4741 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4742 | void scheduleTimeoutLocked(NotificationRecord record) { |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 4743 | if (record.getNotification().getTimeoutAfter() > 0) { |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4744 | final PendingIntent pi = PendingIntent.getBroadcast(getContext(), |
| 4745 | REQUEST_CODE_TIMEOUT, |
| 4746 | new Intent(ACTION_NOTIFICATION_TIMEOUT) |
| 4747 | .setData(new Uri.Builder().scheme(SCHEME_TIMEOUT) |
| 4748 | .appendPath(record.getKey()).build()) |
| 4749 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 4750 | .putExtra(EXTRA_KEY, record.getKey()), |
| 4751 | PendingIntent.FLAG_UPDATE_CURRENT); |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 4752 | mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 4753 | SystemClock.elapsedRealtime() + record.getNotification().getTimeoutAfter(), pi); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4754 | } |
| 4755 | } |
| 4756 | |
| 4757 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4758 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4759 | void buzzBeepBlinkLocked(NotificationRecord record) { |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4760 | boolean buzz = false; |
| 4761 | boolean beep = false; |
| 4762 | boolean blink = false; |
| 4763 | |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4764 | final Notification notification = record.sbn.getNotification(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4765 | final String key = record.getKey(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4766 | |
| 4767 | // Should this notification make noise, vibe, or use the LED? |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 4768 | final boolean aboveThreshold = |
| 4769 | record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4770 | |
| 4771 | // Remember if this notification already owns the notification channels. |
| 4772 | boolean wasBeep = key != null && key.equals(mSoundNotificationKey); |
| 4773 | boolean wasBuzz = key != null && key.equals(mVibrateNotificationKey); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4774 | // These are set inside the conditional if the notification is allowed to make noise. |
| 4775 | boolean hasValidVibrate = false; |
| 4776 | boolean hasValidSound = false; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4777 | boolean sentAccessibilityEvent = false; |
| 4778 | // If the notification will appear in the status bar, it should send an accessibility |
| 4779 | // event |
| 4780 | if (!record.isUpdate && record.getImportance() > IMPORTANCE_MIN) { |
| 4781 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
| 4782 | sentAccessibilityEvent = true; |
| 4783 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4784 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4785 | if (aboveThreshold && isNotificationForCurrentUser(record)) { |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4786 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4787 | if (mSystemReady && mAudioManager != null) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4788 | Uri soundUri = record.getSound(); |
| 4789 | hasValidSound = soundUri != null && !Uri.EMPTY.equals(soundUri); |
| 4790 | long[] vibration = record.getVibration(); |
| 4791 | // Demote sound to vibration if vibration missing & phone in vibration mode. |
| 4792 | if (vibration == null |
| 4793 | && hasValidSound |
| 4794 | && (mAudioManager.getRingerModeInternal() |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 4795 | == AudioManager.RINGER_MODE_VIBRATE) |
| 4796 | && mAudioManager.getStreamVolume( |
| 4797 | AudioAttributes.toLegacyStreamType(record.getAudioAttributes())) == 0) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4798 | vibration = mFallbackVibrationPattern; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4799 | } |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4800 | hasValidVibrate = vibration != null; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4801 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4802 | boolean hasAudibleAlert = hasValidSound || hasValidVibrate; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4803 | if (hasAudibleAlert && !shouldMuteNotificationLocked(record)) { |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4804 | if (!sentAccessibilityEvent) { |
| 4805 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
| 4806 | sentAccessibilityEvent = true; |
| 4807 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4808 | if (DBG) Slog.v(TAG, "Interrupting!"); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4809 | if (hasValidSound) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4810 | if (mInCall) { |
| 4811 | playInCallNotification(); |
| 4812 | beep = true; |
| 4813 | } else { |
| 4814 | beep = playSound(record, soundUri); |
| 4815 | } |
Seungho Lee | 2ca1aec | 2018-12-09 19:27:36 +0900 | [diff] [blame] | 4816 | if(beep) { |
| 4817 | mSoundNotificationKey = key; |
| 4818 | } |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4819 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4820 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4821 | final boolean ringerModeSilent = |
| 4822 | mAudioManager.getRingerModeInternal() |
| 4823 | == AudioManager.RINGER_MODE_SILENT; |
| 4824 | if (!mInCall && hasValidVibrate && !ringerModeSilent) { |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4825 | buzz = playVibration(record, vibration, hasValidSound); |
Seungho Lee | 2ca1aec | 2018-12-09 19:27:36 +0900 | [diff] [blame] | 4826 | if(buzz) { |
| 4827 | mVibrateNotificationKey = key; |
| 4828 | } |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4829 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4830 | } |
| 4831 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4832 | } |
| 4833 | // If a notification is updated to remove the actively playing sound or vibrate, |
| 4834 | // cancel that feedback now |
| 4835 | if (wasBeep && !hasValidSound) { |
| 4836 | clearSoundLocked(); |
| 4837 | } |
| 4838 | if (wasBuzz && !hasValidVibrate) { |
| 4839 | clearVibrateLocked(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4840 | } |
| 4841 | |
| 4842 | // light |
| 4843 | // release the light |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4844 | boolean wasShowLights = mLights.remove(key); |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 4845 | if (canShowLightsLocked(record, aboveThreshold)) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4846 | mLights.add(key); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4847 | updateLightsLocked(); |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 4848 | if (mUseAttentionLight) { |
| 4849 | mAttentionLight.pulse(); |
| 4850 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4851 | blink = true; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4852 | } else if (wasShowLights) { |
| 4853 | updateLightsLocked(); |
| 4854 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4855 | if (buzz || beep || blink) { |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 4856 | // Ignore summary updates because we don't display most of the information. |
| 4857 | if (record.sbn.isGroup() && record.sbn.getNotification().isGroupSummary()) { |
| 4858 | if (DEBUG_INTERRUPTIVENESS) { |
| 4859 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4860 | + record.getKey() + " is not interruptive: summary"); |
| 4861 | } |
| 4862 | } else { |
| 4863 | if (DEBUG_INTERRUPTIVENESS) { |
| 4864 | Log.v(TAG, "INTERRUPTIVENESS: " |
| 4865 | + record.getKey() + " is interruptive: alerted"); |
| 4866 | } |
| 4867 | record.setInterruptive(true); |
| 4868 | } |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 4869 | MetricsLogger.action(record.getLogMaker() |
| 4870 | .setCategory(MetricsEvent.NOTIFICATION_ALERT) |
| 4871 | .setType(MetricsEvent.TYPE_OPEN) |
| 4872 | .setSubtype((buzz ? 1 : 0) | (beep ? 2 : 0) | (blink ? 4 : 0))); |
| 4873 | EventLogTags.writeNotificationAlert(key, buzz ? 1 : 0, beep ? 1 : 0, blink ? 1 : 0); |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 4874 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4875 | } |
| 4876 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4877 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 4878 | boolean canShowLightsLocked(final NotificationRecord record, boolean aboveThreshold) { |
| 4879 | // device lacks light |
| 4880 | if (!mHasLight) { |
| 4881 | return false; |
| 4882 | } |
| 4883 | // user turned lights off globally |
| 4884 | if (!mNotificationPulseEnabled) { |
| 4885 | return false; |
| 4886 | } |
| 4887 | // the notification/channel has no light |
| 4888 | if (record.getLight() == null) { |
| 4889 | return false; |
| 4890 | } |
| 4891 | // unimportant notification |
| 4892 | if (!aboveThreshold) { |
| 4893 | return false; |
| 4894 | } |
| 4895 | // suppressed due to DND |
| 4896 | if ((record.getSuppressedVisualEffects() & SUPPRESSED_EFFECT_LIGHTS) != 0) { |
| 4897 | return false; |
| 4898 | } |
| 4899 | // Suppressed because it's a silent update |
| 4900 | final Notification notification = record.getNotification(); |
| 4901 | if (record.isUpdate && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) { |
| 4902 | return false; |
| 4903 | } |
| 4904 | // Suppressed because another notification in its group handles alerting |
| 4905 | if (record.sbn.isGroup() && record.getNotification().suppressAlertingDueToGrouping()) { |
| 4906 | return false; |
| 4907 | } |
| 4908 | // not if in call or the screen's on |
| 4909 | if (mInCall || mScreenOn) { |
| 4910 | return false; |
| 4911 | } |
| 4912 | |
| 4913 | return true; |
| 4914 | } |
| 4915 | |
| 4916 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4917 | boolean shouldMuteNotificationLocked(final NotificationRecord record) { |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4918 | // Suppressed because it's a silent update |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4919 | final Notification notification = record.getNotification(); |
Julia Reynolds | 5436923 | 2018-07-03 10:43:35 -0400 | [diff] [blame] | 4920 | if (record.isUpdate && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) { |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4921 | return true; |
| 4922 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4923 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4924 | // muted by listener |
| 4925 | final String disableEffects = disableNotificationEffects(record); |
| 4926 | if (disableEffects != null) { |
| 4927 | ZenLog.traceDisableEffects(record, disableEffects); |
| 4928 | return true; |
| 4929 | } |
| 4930 | |
| 4931 | // suppressed due to DND |
| 4932 | if (record.isIntercepted()) { |
| 4933 | return true; |
| 4934 | } |
| 4935 | |
| 4936 | // Suppressed because another notification in its group handles alerting |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4937 | if (record.sbn.isGroup()) { |
Julia Reynolds | 79dfdd6 | 2018-04-17 15:36:33 -0400 | [diff] [blame] | 4938 | if (notification.suppressAlertingDueToGrouping()) { |
| 4939 | return true; |
| 4940 | } |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4941 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4942 | |
Julia Reynolds | 65b85cf | 2017-07-20 09:19:20 -0400 | [diff] [blame] | 4943 | // Suppressed for being too recently noisy |
| 4944 | final String pkg = record.sbn.getPackageName(); |
| 4945 | if (mUsageStats.isAlertRateLimited(pkg)) { |
| 4946 | Slog.e(TAG, "Muting recently noisy " + record.getKey()); |
| 4947 | return true; |
| 4948 | } |
| 4949 | |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4950 | return false; |
| 4951 | } |
| 4952 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 4953 | private boolean playSound(final NotificationRecord record, Uri soundUri) { |
| 4954 | boolean looping = (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
Jean-Michel Trivi | ec2bb18 | 2018-03-23 18:04:00 -0700 | [diff] [blame] | 4955 | // play notifications if there is no user of exclusive audio focus |
| 4956 | // and the stream volume is not 0 (non-zero volume implies not silenced by SILENT or |
| 4957 | // VIBRATE ringer mode) |
| 4958 | if (!mAudioManager.isAudioFocusExclusive() |
| 4959 | && (mAudioManager.getStreamVolume( |
| 4960 | AudioAttributes.toLegacyStreamType(record.getAudioAttributes())) != 0)) { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 4961 | final long identity = Binder.clearCallingIdentity(); |
| 4962 | try { |
| 4963 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4964 | if (player != null) { |
| 4965 | if (DBG) Slog.v(TAG, "Playing sound " + soundUri |
| 4966 | + " with attributes " + record.getAudioAttributes()); |
| 4967 | player.playAsync(soundUri, record.sbn.getUser(), looping, |
| 4968 | record.getAudioAttributes()); |
| 4969 | return true; |
| 4970 | } |
| 4971 | } catch (RemoteException e) { |
| 4972 | } finally { |
| 4973 | Binder.restoreCallingIdentity(identity); |
| 4974 | } |
| 4975 | } |
| 4976 | return false; |
| 4977 | } |
| 4978 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4979 | private boolean playVibration(final NotificationRecord record, long[] vibration, |
| 4980 | boolean delayVibForSound) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 4981 | // Escalate privileges so we can use the vibrator even if the |
| 4982 | // notifying app does not have the VIBRATE permission. |
| 4983 | long identity = Binder.clearCallingIdentity(); |
| 4984 | try { |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4985 | final VibrationEffect effect; |
| 4986 | try { |
| 4987 | final boolean insistent = |
| 4988 | (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 4989 | effect = VibrationEffect.createWaveform( |
| 4990 | vibration, insistent ? 0 : -1 /*repeatIndex*/); |
| 4991 | } catch (IllegalArgumentException e) { |
| 4992 | Slog.e(TAG, "Error creating vibration waveform with pattern: " + |
| 4993 | Arrays.toString(vibration)); |
| 4994 | return false; |
| 4995 | } |
| 4996 | if (delayVibForSound) { |
| 4997 | new Thread(() -> { |
| 4998 | // delay the vibration by the same amount as the notification sound |
| 4999 | final int waitMs = mAudioManager.getFocusRampTimeMs( |
| 5000 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 5001 | record.getAudioAttributes()); |
| 5002 | if (DBG) Slog.v(TAG, "Delaying vibration by " + waitMs + "ms"); |
| 5003 | try { |
| 5004 | Thread.sleep(waitMs); |
| 5005 | } catch (InterruptedException e) { } |
Seungho Lee | 0e97ae6 | 2018-10-31 21:49:09 +0900 | [diff] [blame] | 5006 | |
| 5007 | // Notifications might be canceled before it actually vibrates due to waitMs, |
| 5008 | // so need to check the notification still valide for vibrate. |
| 5009 | synchronized (mNotificationLock) { |
| 5010 | if (mNotificationsByKey.get(record.getKey()) != null) { |
| 5011 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 5012 | effect, record.getAudioAttributes()); |
| 5013 | } else { |
| 5014 | Slog.e(TAG, "No vibration for canceled notification : " + record.getKey()); |
| 5015 | } |
| 5016 | } |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 5017 | }).start(); |
| 5018 | } else { |
| 5019 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 5020 | effect, record.getAudioAttributes()); |
| 5021 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 5022 | return true; |
| 5023 | } finally{ |
| 5024 | Binder.restoreCallingIdentity(identity); |
| 5025 | } |
| 5026 | } |
| 5027 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 5028 | private boolean isNotificationForCurrentUser(NotificationRecord record) { |
| 5029 | final int currentUser; |
| 5030 | final long token = Binder.clearCallingIdentity(); |
| 5031 | try { |
| 5032 | currentUser = ActivityManager.getCurrentUser(); |
| 5033 | } finally { |
| 5034 | Binder.restoreCallingIdentity(token); |
| 5035 | } |
| 5036 | return (record.getUserId() == UserHandle.USER_ALL || |
| 5037 | record.getUserId() == currentUser || |
| 5038 | mUserProfiles.isCurrentProfile(record.getUserId())); |
| 5039 | } |
| 5040 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 5041 | protected void playInCallNotification() { |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 5042 | new Thread() { |
| 5043 | @Override |
| 5044 | public void run() { |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 5045 | final long identity = Binder.clearCallingIdentity(); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 5046 | try { |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 5047 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 5048 | if (player != null) { |
luochaojiang | 50e5273c | 2018-04-16 16:55:03 +0800 | [diff] [blame] | 5049 | if (mCallNotificationToken != null) { |
| 5050 | player.stop(mCallNotificationToken); |
| 5051 | } |
| 5052 | mCallNotificationToken = new Binder(); |
| 5053 | player.play(mCallNotificationToken, mInCallNotificationUri, |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 5054 | mInCallNotificationAudioAttributes, |
| 5055 | mInCallNotificationVolume, false); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 5056 | } |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 5057 | } catch (RemoteException e) { |
| 5058 | } finally { |
| 5059 | Binder.restoreCallingIdentity(identity); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 5060 | } |
| 5061 | } |
| 5062 | }.start(); |
| 5063 | } |
| 5064 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5065 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5066 | void showNextToastLocked() { |
| 5067 | ToastRecord record = mToastQueue.get(0); |
| 5068 | while (record != null) { |
| 5069 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| 5070 | try { |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5071 | record.callback.show(record.token); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5072 | scheduleDurationReachedLocked(record); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5073 | return; |
| 5074 | } catch (RemoteException e) { |
| 5075 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| 5076 | + " in package " + record.pkg); |
| 5077 | // remove it from the list and let the process die |
| 5078 | int index = mToastQueue.indexOf(record); |
| 5079 | if (index >= 0) { |
| 5080 | mToastQueue.remove(index); |
| 5081 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5082 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5083 | if (mToastQueue.size() > 0) { |
| 5084 | record = mToastQueue.get(0); |
| 5085 | } else { |
| 5086 | record = null; |
| 5087 | } |
| 5088 | } |
| 5089 | } |
| 5090 | } |
| 5091 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5092 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5093 | void cancelToastLocked(int index) { |
| 5094 | ToastRecord record = mToastQueue.get(index); |
| 5095 | try { |
| 5096 | record.callback.hide(); |
| 5097 | } catch (RemoteException e) { |
| 5098 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| 5099 | + " in package " + record.pkg); |
| 5100 | // don't worry about this, we're about to remove it from |
| 5101 | // the list anyway |
| 5102 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5103 | |
| 5104 | ToastRecord lastToast = mToastQueue.remove(index); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5105 | |
| 5106 | mWindowManagerInternal.removeWindowToken(lastToast.token, false /* removeWindows */, |
| 5107 | DEFAULT_DISPLAY); |
| 5108 | // We passed 'false' for 'removeWindows' so that the client has time to stop |
| 5109 | // rendering (as hide above is a one-way message), otherwise we could crash |
| 5110 | // a client which was actively using a surface made from the token. However |
| 5111 | // we need to schedule a timeout to make sure the token is eventually killed |
| 5112 | // one way or another. |
| 5113 | scheduleKillTokenTimeout(lastToast.token); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5114 | |
| 5115 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5116 | if (mToastQueue.size() > 0) { |
| 5117 | // Show the next one. If the callback fails, this will remove |
| 5118 | // it from the list, so don't assume that the list hasn't changed |
| 5119 | // after this point. |
| 5120 | showNextToastLocked(); |
| 5121 | } |
| 5122 | } |
| 5123 | |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5124 | void finishTokenLocked(IBinder t) { |
| 5125 | mHandler.removeCallbacksAndMessages(t); |
| 5126 | // We pass 'true' for 'removeWindows' to let the WindowManager destroy any |
| 5127 | // remaining surfaces as either the client has called finishToken indicating |
| 5128 | // it has successfully removed the views, or the client has timed out |
| 5129 | // at which point anything goes. |
| 5130 | mWindowManagerInternal.removeWindowToken(t, true /* removeWindows */, |
| 5131 | DEFAULT_DISPLAY); |
| 5132 | } |
| 5133 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5134 | @GuardedBy("mToastQueue") |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5135 | private void scheduleDurationReachedLocked(ToastRecord r) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5136 | { |
| 5137 | mHandler.removeCallbacksAndMessages(r); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5138 | Message m = Message.obtain(mHandler, MESSAGE_DURATION_REACHED, r); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5139 | long delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY; |
| 5140 | mHandler.sendMessageDelayed(m, delay); |
| 5141 | } |
| 5142 | |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5143 | private void handleDurationReached(ToastRecord record) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5144 | { |
| 5145 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| 5146 | synchronized (mToastQueue) { |
| 5147 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 5148 | if (index >= 0) { |
| 5149 | cancelToastLocked(index); |
| 5150 | } |
| 5151 | } |
| 5152 | } |
| 5153 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5154 | @GuardedBy("mToastQueue") |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5155 | private void scheduleKillTokenTimeout(IBinder token) |
| 5156 | { |
| 5157 | mHandler.removeCallbacksAndMessages(token); |
| 5158 | Message m = Message.obtain(mHandler, MESSAGE_FINISH_TOKEN_TIMEOUT, token); |
Robert Carr | 3406d46 | 2018-03-15 16:19:07 -0700 | [diff] [blame] | 5159 | mHandler.sendMessageDelayed(m, FINISH_TOKEN_TIMEOUT); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5160 | } |
| 5161 | |
| 5162 | private void handleKillTokenTimeout(IBinder token) |
| 5163 | { |
| 5164 | if (DBG) Slog.d(TAG, "Kill Token Timeout token=" + token); |
| 5165 | synchronized (mToastQueue) { |
| 5166 | finishTokenLocked(token); |
| 5167 | } |
| 5168 | } |
| 5169 | |
| 5170 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5171 | int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 5172 | { |
| 5173 | IBinder cbak = callback.asBinder(); |
| 5174 | ArrayList<ToastRecord> list = mToastQueue; |
| 5175 | int len = list.size(); |
| 5176 | for (int i=0; i<len; i++) { |
| 5177 | ToastRecord r = list.get(i); |
Beverly Tai | 98efc79 | 2018-06-11 14:50:36 +0000 | [diff] [blame] | 5178 | if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5179 | return i; |
| 5180 | } |
| 5181 | } |
| 5182 | return -1; |
| 5183 | } |
| 5184 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5185 | @GuardedBy("mToastQueue") |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 5186 | void keepProcessAliveIfNeededLocked(int pid) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5187 | { |
| 5188 | int toastCount = 0; // toasts from this pid |
| 5189 | ArrayList<ToastRecord> list = mToastQueue; |
| 5190 | int N = list.size(); |
| 5191 | for (int i=0; i<N; i++) { |
| 5192 | ToastRecord r = list.get(i); |
| 5193 | if (r.pid == pid) { |
| 5194 | toastCount++; |
| 5195 | } |
| 5196 | } |
| 5197 | try { |
Dianne Hackborn | f965f40 | 2017-05-04 23:27:23 -0700 | [diff] [blame] | 5198 | mAm.setProcessImportant(mForegroundToken, pid, toastCount > 0, "toast"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5199 | } catch (RemoteException e) { |
| 5200 | // Shouldn't happen. |
| 5201 | } |
| 5202 | } |
| 5203 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5204 | private void handleRankingReconsideration(Message message) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 5205 | if (!(message.obj instanceof RankingReconsideration)) return; |
| 5206 | RankingReconsideration recon = (RankingReconsideration) message.obj; |
| 5207 | recon.run(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5208 | boolean changed; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5209 | synchronized (mNotificationLock) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 5210 | final NotificationRecord record = mNotificationsByKey.get(recon.getKey()); |
| 5211 | if (record == null) { |
| 5212 | return; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5213 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5214 | int indexBefore = findNotificationRecordIndexLocked(record); |
| 5215 | boolean interceptBefore = record.isIntercepted(); |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 5216 | float contactAffinityBefore = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5217 | int visibilityBefore = record.getPackageVisibilityOverride(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 5218 | recon.applyChangesLocked(record); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5219 | applyZenModeLocked(record); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5220 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5221 | int indexAfter = findNotificationRecordIndexLocked(record); |
| 5222 | boolean interceptAfter = record.isIntercepted(); |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 5223 | float contactAffinityAfter = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5224 | int visibilityAfter = record.getPackageVisibilityOverride(); |
| 5225 | changed = indexBefore != indexAfter || interceptBefore != interceptAfter |
| 5226 | || visibilityBefore != visibilityAfter; |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 5227 | if (interceptBefore && !interceptAfter |
| 5228 | && Float.compare(contactAffinityBefore, contactAffinityAfter) != 0) { |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 5229 | buzzBeepBlinkLocked(record); |
| 5230 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5231 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5232 | if (changed) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5233 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 5234 | } |
| 5235 | } |
| 5236 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5237 | void handleRankingSort() { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 5238 | if (mRankingHelper == null) return; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5239 | synchronized (mNotificationLock) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5240 | final int N = mNotificationList.size(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5241 | // Any field that can change via one of the extractors needs to be added here. |
| 5242 | ArrayList<String> orderBefore = new ArrayList<>(N); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5243 | int[] visibilities = new int[N]; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5244 | boolean[] showBadges = new boolean[N]; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5245 | ArrayList<NotificationChannel> channelBefore = new ArrayList<>(N); |
| 5246 | ArrayList<String> groupKeyBefore = new ArrayList<>(N); |
| 5247 | ArrayList<ArrayList<String>> overridePeopleBefore = new ArrayList<>(N); |
| 5248 | ArrayList<ArrayList<SnoozeCriterion>> snoozeCriteriaBefore = new ArrayList<>(N); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5249 | ArrayList<Integer> userSentimentBefore = new ArrayList<>(N); |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 5250 | ArrayList<Integer> suppressVisuallyBefore = new ArrayList<>(N); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5251 | for (int i = 0; i < N; i++) { |
| 5252 | final NotificationRecord r = mNotificationList.get(i); |
| 5253 | orderBefore.add(r.getKey()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5254 | visibilities[i] = r.getPackageVisibilityOverride(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5255 | showBadges[i] = r.canShowBadge(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5256 | channelBefore.add(r.getChannel()); |
| 5257 | groupKeyBefore.add(r.getGroupKey()); |
| 5258 | overridePeopleBefore.add(r.getPeopleOverride()); |
| 5259 | snoozeCriteriaBefore.add(r.getSnoozeCriteria()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5260 | userSentimentBefore.add(r.getUserSentiment()); |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 5261 | suppressVisuallyBefore.add(r.getSuppressedVisualEffects()); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5262 | mRankingHelper.extractSignals(r); |
| 5263 | } |
Chris Wren | 19a02b0 | 2015-12-22 10:34:22 -0500 | [diff] [blame] | 5264 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5265 | for (int i = 0; i < N; i++) { |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5266 | final NotificationRecord r = mNotificationList.get(i); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5267 | if (!orderBefore.get(i).equals(r.getKey()) |
Julia Reynolds | 6976669 | 2016-02-01 15:35:08 -0500 | [diff] [blame] | 5268 | || visibilities[i] != r.getPackageVisibilityOverride() |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5269 | || showBadges[i] != r.canShowBadge() |
| 5270 | || !Objects.equals(channelBefore.get(i), r.getChannel()) |
| 5271 | || !Objects.equals(groupKeyBefore.get(i), r.getGroupKey()) |
| 5272 | || !Objects.equals(overridePeopleBefore.get(i), r.getPeopleOverride()) |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5273 | || !Objects.equals(snoozeCriteriaBefore.get(i), r.getSnoozeCriteria()) |
Julia Reynolds | c861a3d | 2018-02-15 10:34:49 -0500 | [diff] [blame] | 5274 | || !Objects.equals(userSentimentBefore.get(i), r.getUserSentiment()) |
| 5275 | || !Objects.equals(suppressVisuallyBefore.get(i), |
| 5276 | r.getSuppressedVisualEffects())) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5277 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5278 | return; |
| 5279 | } |
| 5280 | } |
| 5281 | } |
| 5282 | } |
| 5283 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5284 | @GuardedBy("mNotificationLock") |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 5285 | private void recordCallerLocked(NotificationRecord record) { |
| 5286 | if (mZenModeHelper.isCall(record)) { |
| 5287 | mZenModeHelper.recordCaller(record); |
| 5288 | } |
| 5289 | } |
| 5290 | |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 5291 | // let zen mode evaluate this record |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5292 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5293 | private void applyZenModeLocked(NotificationRecord record) { |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 5294 | record.setIntercepted(mZenModeHelper.shouldIntercept(record)); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 5295 | if (record.isIntercepted()) { |
Julia Reynolds | ccc6ae6 | 2018-03-01 16:24:49 -0500 | [diff] [blame] | 5296 | record.setSuppressedVisualEffects( |
| 5297 | mZenModeHelper.getNotificationPolicy().suppressedVisualEffects); |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 5298 | } else { |
| 5299 | record.setSuppressedVisualEffects(0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 5300 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5301 | } |
| 5302 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5303 | @GuardedBy("mNotificationLock") |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 5304 | private int findNotificationRecordIndexLocked(NotificationRecord target) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5305 | return mRankingHelper.indexOf(mNotificationList, target); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5306 | } |
| 5307 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5308 | private void handleSendRankingUpdate() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5309 | synchronized (mNotificationLock) { |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 5310 | mListeners.notifyRankingUpdateLocked(null); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5311 | } |
| 5312 | } |
| 5313 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5314 | private void scheduleListenerHintsChanged(int state) { |
| 5315 | mHandler.removeMessages(MESSAGE_LISTENER_HINTS_CHANGED); |
| 5316 | mHandler.obtainMessage(MESSAGE_LISTENER_HINTS_CHANGED, state, 0).sendToTarget(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5317 | } |
| 5318 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5319 | private void scheduleInterruptionFilterChanged(int listenerInterruptionFilter) { |
| 5320 | mHandler.removeMessages(MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED); |
| 5321 | mHandler.obtainMessage( |
| 5322 | MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED, |
| 5323 | listenerInterruptionFilter, |
| 5324 | 0).sendToTarget(); |
| 5325 | } |
| 5326 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5327 | private void handleListenerHintsChanged(int hints) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5328 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5329 | mListeners.notifyListenerHintsChangedLocked(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5330 | } |
| 5331 | } |
| 5332 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5333 | private void handleListenerInterruptionFilterChanged(int interruptionFilter) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5334 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5335 | mListeners.notifyInterruptionFilterChanged(interruptionFilter); |
| 5336 | } |
| 5337 | } |
| 5338 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5339 | protected class WorkerHandler extends Handler |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5340 | { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5341 | public WorkerHandler(Looper looper) { |
| 5342 | super(looper); |
| 5343 | } |
| 5344 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5345 | @Override |
| 5346 | public void handleMessage(Message msg) |
| 5347 | { |
| 5348 | switch (msg.what) |
| 5349 | { |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 5350 | case MESSAGE_DURATION_REACHED: |
| 5351 | handleDurationReached((ToastRecord)msg.obj); |
| 5352 | break; |
| 5353 | case MESSAGE_FINISH_TOKEN_TIMEOUT: |
| 5354 | handleKillTokenTimeout((IBinder)msg.obj); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5355 | break; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 5356 | case MESSAGE_SAVE_POLICY_FILE: |
| 5357 | handleSavePolicyFile(); |
| 5358 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5359 | case MESSAGE_SEND_RANKING_UPDATE: |
| 5360 | handleSendRankingUpdate(); |
| 5361 | break; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5362 | case MESSAGE_LISTENER_HINTS_CHANGED: |
| 5363 | handleListenerHintsChanged(msg.arg1); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5364 | break; |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5365 | case MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED: |
| 5366 | handleListenerInterruptionFilterChanged(msg.arg1); |
| 5367 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5368 | } |
| 5369 | } |
| 5370 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5371 | protected void scheduleSendRankingUpdate() { |
| 5372 | if (!hasMessages(MESSAGE_SEND_RANKING_UPDATE)) { |
| 5373 | Message m = Message.obtain(this, MESSAGE_SEND_RANKING_UPDATE); |
| 5374 | sendMessage(m); |
| 5375 | } |
| 5376 | } |
| 5377 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5378 | } |
| 5379 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5380 | private final class RankingHandlerWorker extends Handler implements RankingHandler |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5381 | { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5382 | public RankingHandlerWorker(Looper looper) { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5383 | super(looper); |
| 5384 | } |
| 5385 | |
| 5386 | @Override |
| 5387 | public void handleMessage(Message msg) { |
| 5388 | switch (msg.what) { |
| 5389 | case MESSAGE_RECONSIDER_RANKING: |
| 5390 | handleRankingReconsideration(msg); |
| 5391 | break; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5392 | case MESSAGE_RANKING_SORT: |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5393 | handleRankingSort(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 5394 | break; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5395 | } |
| 5396 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5397 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5398 | public void requestSort() { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5399 | removeMessages(MESSAGE_RANKING_SORT); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5400 | Message msg = Message.obtain(); |
| 5401 | msg.what = MESSAGE_RANKING_SORT; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5402 | sendMessage(msg); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5403 | } |
| 5404 | |
| 5405 | public void requestReconsideration(RankingReconsideration recon) { |
| 5406 | Message m = Message.obtain(this, |
| 5407 | NotificationManagerService.MESSAGE_RECONSIDER_RANKING, recon); |
| 5408 | long delay = recon.getDelay(TimeUnit.MILLISECONDS); |
| 5409 | sendMessageDelayed(m, delay); |
| 5410 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5411 | } |
| 5412 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5413 | // Notifications |
| 5414 | // ============================================================================ |
| 5415 | static int clamp(int x, int low, int high) { |
| 5416 | return (x < low) ? low : ((x > high) ? high : x); |
| 5417 | } |
| 5418 | |
| 5419 | void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
Eugene Susla | d4128ec | 2017-12-04 19:48:41 +0000 | [diff] [blame] | 5420 | if (!mAccessibilityManager.isEnabled()) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 5421 | return; |
| 5422 | } |
| 5423 | |
| 5424 | AccessibilityEvent event = |
| 5425 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 5426 | event.setPackageName(packageName); |
| 5427 | event.setClassName(Notification.class.getName()); |
| 5428 | event.setParcelableData(notification); |
| 5429 | CharSequence tickerText = notification.tickerText; |
| 5430 | if (!TextUtils.isEmpty(tickerText)) { |
| 5431 | event.getText().add(tickerText); |
| 5432 | } |
| 5433 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 5434 | mAccessibilityManager.sendAccessibilityEvent(event); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 5435 | } |
| 5436 | |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5437 | /** |
| 5438 | * Removes all NotificationsRecords with the same key as the given notification record |
| 5439 | * from both lists. Do not call this method while iterating over either list. |
| 5440 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5441 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5442 | private boolean removeFromNotificationListsLocked(NotificationRecord r) { |
| 5443 | // Remove from both lists, either list could have a separate Record for what is |
| 5444 | // effectively the same notification. |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5445 | boolean wasPosted = false; |
| 5446 | NotificationRecord recordInList = null; |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5447 | if ((recordInList = findNotificationByListLocked(mNotificationList, r.getKey())) |
| 5448 | != null) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5449 | mNotificationList.remove(recordInList); |
| 5450 | mNotificationsByKey.remove(recordInList.sbn.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5451 | wasPosted = true; |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5452 | } |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5453 | while ((recordInList = findNotificationByListLocked(mEnqueuedNotifications, r.getKey())) |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5454 | != null) { |
| 5455 | mEnqueuedNotifications.remove(recordInList); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5456 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5457 | return wasPosted; |
| 5458 | } |
| 5459 | |
| 5460 | @GuardedBy("mNotificationLock") |
| 5461 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason, |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5462 | boolean wasPosted, String listenerName) { |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5463 | cancelNotificationLocked(r, sendDelete, reason, -1, -1, wasPosted, listenerName); |
| 5464 | } |
| 5465 | |
| 5466 | @GuardedBy("mNotificationLock") |
| 5467 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason, |
| 5468 | int rank, int count, boolean wasPosted, String listenerName) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5469 | final String canceledKey = r.getKey(); |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 5470 | |
| 5471 | // Record caller. |
| 5472 | recordCallerLocked(r); |
| 5473 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5474 | if (r.getStats().getDismissalSurface() == NotificationStats.DISMISSAL_NOT_DISMISSED) { |
| 5475 | r.recordDismissalSurface(NotificationStats.DISMISSAL_OTHER); |
| 5476 | } |
| 5477 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 5478 | // tell the app |
| 5479 | if (sendDelete) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 5480 | if (r.getNotification().deleteIntent != null) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 5481 | try { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 5482 | r.getNotification().deleteIntent.send(); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 5483 | } catch (PendingIntent.CanceledException ex) { |
| 5484 | // do nothing - there's no relevant way to recover, and |
| 5485 | // no reason to let this propagate |
Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 5486 | Slog.w(TAG, "canceled PendingIntent for " + r.sbn.getPackageName(), ex); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 5487 | } |
| 5488 | } |
| 5489 | } |
| 5490 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5491 | // Only cancel these if this notification actually got to be posted. |
| 5492 | if (wasPosted) { |
| 5493 | // status bar |
| 5494 | if (r.getNotification().getSmallIcon() != null) { |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 5495 | if (reason != REASON_SNOOZED) { |
| 5496 | r.isCanceled = true; |
| 5497 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 5498 | mListeners.notifyRemovedLocked(r, reason, r.getStats()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5499 | mHandler.post(new Runnable() { |
| 5500 | @Override |
| 5501 | public void run() { |
| 5502 | mGroupHelper.onNotificationRemoved(r.sbn); |
| 5503 | } |
| 5504 | }); |
| 5505 | } |
| 5506 | |
| 5507 | // sound |
| 5508 | if (canceledKey.equals(mSoundNotificationKey)) { |
| 5509 | mSoundNotificationKey = null; |
| 5510 | final long identity = Binder.clearCallingIdentity(); |
| 5511 | try { |
| 5512 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 5513 | if (player != null) { |
| 5514 | player.stopAsync(); |
| 5515 | } |
| 5516 | } catch (RemoteException e) { |
| 5517 | } finally { |
| 5518 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 5519 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5520 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5521 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5522 | // vibrate |
| 5523 | if (canceledKey.equals(mVibrateNotificationKey)) { |
| 5524 | mVibrateNotificationKey = null; |
| 5525 | long identity = Binder.clearCallingIdentity(); |
| 5526 | try { |
| 5527 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 5528 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5529 | finally { |
| 5530 | Binder.restoreCallingIdentity(identity); |
| 5531 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5532 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5533 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5534 | // light |
| 5535 | mLights.remove(canceledKey); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5536 | } |
| 5537 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5538 | // Record usage stats |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5539 | // TODO: add unbundling stats? |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5540 | switch (reason) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 5541 | case REASON_CANCEL: |
| 5542 | case REASON_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5543 | case REASON_LISTENER_CANCEL: |
| 5544 | case REASON_LISTENER_CANCEL_ALL: |
| 5545 | mUsageStats.registerDismissedByUser(r); |
| 5546 | break; |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 5547 | case REASON_APP_CANCEL: |
| 5548 | case REASON_APP_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5549 | mUsageStats.registerRemovedByApp(r); |
| 5550 | break; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5551 | } |
| 5552 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5553 | String groupKey = r.getGroupKey(); |
| 5554 | NotificationRecord groupSummary = mSummaryByGroupKey.get(groupKey); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5555 | if (groupSummary != null && groupSummary.getKey().equals(canceledKey)) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5556 | mSummaryByGroupKey.remove(groupKey); |
| 5557 | } |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 5558 | final ArrayMap<String, String> summaries = mAutobundledSummaries.get(r.sbn.getUserId()); |
| 5559 | if (summaries != null && r.sbn.getKey().equals(summaries.get(r.sbn.getPackageName()))) { |
| 5560 | summaries.remove(r.sbn.getPackageName()); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5561 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5562 | |
Daniel Sandler | 23d7c70 | 2013-03-07 16:32:06 -0500 | [diff] [blame] | 5563 | // Save it for users of getHistoricalNotifications() |
| 5564 | mArchive.record(r.sbn); |
Christoph Studer | 81e5b5f | 2014-10-22 17:19:56 +0200 | [diff] [blame] | 5565 | |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 5566 | final long now = System.currentTimeMillis(); |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5567 | final LogMaker logMaker = r.getLogMaker(now) |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 5568 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
| 5569 | .setType(MetricsEvent.TYPE_DISMISS) |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5570 | .setSubtype(reason); |
| 5571 | if (rank != -1 && count != -1) { |
| 5572 | logMaker.addTaggedData(MetricsEvent.NOTIFICATION_SHADE_INDEX, rank) |
| 5573 | .addTaggedData(MetricsEvent.NOTIFICATION_SHADE_COUNT, count); |
| 5574 | } |
| 5575 | MetricsLogger.action(logMaker); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 5576 | EventLogTags.writeNotificationCanceled(canceledKey, reason, |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5577 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now), |
| 5578 | rank, count, listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5579 | } |
| 5580 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 5581 | @VisibleForTesting |
| 5582 | void updateUriPermissions(@Nullable NotificationRecord newRecord, |
| 5583 | @Nullable NotificationRecord oldRecord, String targetPkg, int targetUserId) { |
| 5584 | final String key = (newRecord != null) ? newRecord.getKey() : oldRecord.getKey(); |
| 5585 | if (DBG) Slog.d(TAG, key + ": updating permissions"); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 5586 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 5587 | final ArraySet<Uri> newUris = (newRecord != null) ? newRecord.getGrantableUris() : null; |
| 5588 | final ArraySet<Uri> oldUris = (oldRecord != null) ? oldRecord.getGrantableUris() : null; |
| 5589 | |
| 5590 | // Shortcut when no Uris involved |
| 5591 | if (newUris == null && oldUris == null) { |
| 5592 | return; |
| 5593 | } |
| 5594 | |
| 5595 | // Inherit any existing owner |
| 5596 | IBinder permissionOwner = null; |
| 5597 | if (newRecord != null && permissionOwner == null) { |
| 5598 | permissionOwner = newRecord.permissionOwner; |
| 5599 | } |
| 5600 | if (oldRecord != null && permissionOwner == null) { |
| 5601 | permissionOwner = oldRecord.permissionOwner; |
| 5602 | } |
| 5603 | |
| 5604 | // If we have Uris to grant, but no owner yet, go create one |
| 5605 | if (newUris != null && permissionOwner == null) { |
| 5606 | try { |
| 5607 | if (DBG) Slog.d(TAG, key + ": creating owner"); |
| 5608 | permissionOwner = mAm.newUriPermissionOwner("NOTIF:" + key); |
| 5609 | } catch (RemoteException ignored) { |
| 5610 | // Ignored because we're in same process |
| 5611 | } |
| 5612 | } |
| 5613 | |
| 5614 | // If we have no Uris to grant, but an existing owner, go destroy it |
| 5615 | if (newUris == null && permissionOwner != null) { |
| 5616 | final long ident = Binder.clearCallingIdentity(); |
| 5617 | try { |
| 5618 | if (DBG) Slog.d(TAG, key + ": destroying owner"); |
| 5619 | mAm.revokeUriPermissionFromOwner(permissionOwner, null, ~0, |
| 5620 | UserHandle.getUserId(oldRecord.getUid())); |
| 5621 | permissionOwner = null; |
| 5622 | } catch (RemoteException ignored) { |
| 5623 | // Ignored because we're in same process |
| 5624 | } finally { |
| 5625 | Binder.restoreCallingIdentity(ident); |
| 5626 | } |
| 5627 | } |
| 5628 | |
| 5629 | // Grant access to new Uris |
| 5630 | if (newUris != null && permissionOwner != null) { |
| 5631 | for (int i = 0; i < newUris.size(); i++) { |
| 5632 | final Uri uri = newUris.valueAt(i); |
| 5633 | if (oldUris == null || !oldUris.contains(uri)) { |
| 5634 | if (DBG) Slog.d(TAG, key + ": granting " + uri); |
| 5635 | grantUriPermission(permissionOwner, uri, newRecord.getUid(), targetPkg, |
| 5636 | targetUserId); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 5637 | } |
| 5638 | } |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 5639 | } |
| 5640 | |
| 5641 | // Revoke access to old Uris |
| 5642 | if (oldUris != null && permissionOwner != null) { |
| 5643 | for (int i = 0; i < oldUris.size(); i++) { |
| 5644 | final Uri uri = oldUris.valueAt(i); |
| 5645 | if (newUris == null || !newUris.contains(uri)) { |
| 5646 | if (DBG) Slog.d(TAG, key + ": revoking " + uri); |
| 5647 | revokeUriPermission(permissionOwner, uri, oldRecord.getUid()); |
| 5648 | } |
| 5649 | } |
| 5650 | } |
| 5651 | |
| 5652 | if (newRecord != null) { |
| 5653 | newRecord.permissionOwner = permissionOwner; |
| 5654 | } |
| 5655 | } |
| 5656 | |
| 5657 | private void grantUriPermission(IBinder owner, Uri uri, int sourceUid, String targetPkg, |
| 5658 | int targetUserId) { |
| 5659 | if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return; |
| 5660 | |
| 5661 | final long ident = Binder.clearCallingIdentity(); |
| 5662 | try { |
| 5663 | mAm.grantUriPermissionFromOwner(owner, sourceUid, targetPkg, |
| 5664 | ContentProvider.getUriWithoutUserId(uri), |
| 5665 | Intent.FLAG_GRANT_READ_URI_PERMISSION, |
| 5666 | ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid)), |
| 5667 | targetUserId); |
| 5668 | } catch (RemoteException ignored) { |
| 5669 | // Ignored because we're in same process |
| 5670 | } finally { |
| 5671 | Binder.restoreCallingIdentity(ident); |
| 5672 | } |
| 5673 | } |
| 5674 | |
| 5675 | private void revokeUriPermission(IBinder owner, Uri uri, int sourceUid) { |
| 5676 | if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return; |
| 5677 | |
| 5678 | final long ident = Binder.clearCallingIdentity(); |
| 5679 | try { |
| 5680 | mAm.revokeUriPermissionFromOwner(owner, |
| 5681 | ContentProvider.getUriWithoutUserId(uri), |
| 5682 | Intent.FLAG_GRANT_READ_URI_PERMISSION, |
| 5683 | ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid))); |
| 5684 | } catch (RemoteException ignored) { |
| 5685 | // Ignored because we're in same process |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 5686 | } finally { |
| 5687 | Binder.restoreCallingIdentity(ident); |
| 5688 | } |
| 5689 | } |
| 5690 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5691 | /** |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 5692 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5693 | * and none of the {@code mustNotHaveFlags}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5694 | */ |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 5695 | void cancelNotification(final int callingUid, final int callingPid, |
| 5696 | final String pkg, final String tag, final int id, |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5697 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5698 | final int userId, final int reason, final ManagedServiceInfo listener) { |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5699 | cancelNotification(callingUid, callingPid, pkg, tag, id, mustHaveFlags, mustNotHaveFlags, |
| 5700 | sendDelete, userId, reason, -1 /* rank */, -1 /* count */, listener); |
| 5701 | } |
| 5702 | |
| 5703 | /** |
| 5704 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
| 5705 | * and none of the {@code mustNotHaveFlags}. |
| 5706 | */ |
| 5707 | void cancelNotification(final int callingUid, final int callingPid, |
| 5708 | final String pkg, final String tag, final int id, |
| 5709 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
| 5710 | final int userId, final int reason, int rank, int count, final ManagedServiceInfo listener) { |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 5711 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5712 | // In enqueueNotificationInternal notifications are added by scheduling the |
| 5713 | // work on the worker handler. Hence, we also schedule the cancel on this |
| 5714 | // handler to avoid a scenario where an add notification call followed by a |
| 5715 | // remove notification call ends up in not removing the notification. |
| 5716 | mHandler.post(new Runnable() { |
| 5717 | @Override |
| 5718 | public void run() { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5719 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Chris Wren | bddb5bc | 2015-03-04 08:47:46 -0800 | [diff] [blame] | 5720 | if (DBG) EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, id, tag, |
| 5721 | userId, mustHaveFlags, mustNotHaveFlags, reason, listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5722 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5723 | synchronized (mNotificationLock) { |
| 5724 | // Look for the notification, searching both the posted and enqueued lists. |
| 5725 | NotificationRecord r = findNotificationLocked(pkg, tag, id, userId); |
| 5726 | if (r != null) { |
| 5727 | // The notification was found, check if it should be removed. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5728 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5729 | // Ideally we'd do this in the caller of this method. However, that would |
| 5730 | // require the caller to also find the notification. |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 5731 | if (reason == REASON_CLICK) { |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5732 | mUsageStats.registerClickedByUser(r); |
| 5733 | } |
| 5734 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5735 | if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) { |
| 5736 | return; |
| 5737 | } |
| 5738 | if ((r.getNotification().flags & mustNotHaveFlags) != 0) { |
| 5739 | return; |
| 5740 | } |
| 5741 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5742 | // Cancel the notification. |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5743 | boolean wasPosted = removeFromNotificationListsLocked(r); |
Dieter Hsu | d39f0d5 | 2018-04-14 02:08:30 +0800 | [diff] [blame] | 5744 | cancelNotificationLocked(r, sendDelete, reason, rank, count, wasPosted, listenerName); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5745 | cancelGroupChildrenLocked(r, callingUid, callingPid, listenerName, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5746 | sendDelete, null); |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5747 | updateLightsLocked(); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 5748 | } else { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5749 | // No notification was found, assume that it is snoozed and cancel it. |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 5750 | if (reason != REASON_SNOOZED) { |
| 5751 | final boolean wasSnoozed = mSnoozeHelper.cancel(userId, pkg, tag, id); |
| 5752 | if (wasSnoozed) { |
| 5753 | savePolicyFile(); |
| 5754 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 5755 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5756 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5757 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5758 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5759 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5760 | } |
| 5761 | |
| 5762 | /** |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 5763 | * Determine whether the userId applies to the notification in question, either because |
| 5764 | * they match exactly, or one of them is USER_ALL (which is treated as a wildcard). |
| 5765 | */ |
| 5766 | private boolean notificationMatchesUserId(NotificationRecord r, int userId) { |
| 5767 | return |
| 5768 | // looking for USER_ALL notifications? match everything |
| 5769 | userId == UserHandle.USER_ALL |
| 5770 | // a notification sent to USER_ALL matches any query |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 5771 | || r.getUserId() == UserHandle.USER_ALL |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 5772 | // an exact user match |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 5773 | || r.getUserId() == userId; |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 5774 | } |
| 5775 | |
| 5776 | /** |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5777 | * Determine whether the userId applies to the notification in question, either because |
| 5778 | * 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] | 5779 | * because it matches one of the users profiles. |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5780 | */ |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 5781 | private boolean notificationMatchesCurrentProfiles(NotificationRecord r, int userId) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5782 | return notificationMatchesUserId(r, userId) |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 5783 | || mUserProfiles.isCurrentProfile(r.getUserId()); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5784 | } |
| 5785 | |
| 5786 | /** |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 5787 | * 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] | 5788 | * {@code mustHaveFlags}. |
| 5789 | */ |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5790 | void cancelAllNotificationsInt(int callingUid, int callingPid, String pkg, String channelId, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 5791 | int mustHaveFlags, int mustNotHaveFlags, boolean doit, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5792 | ManagedServiceInfo listener) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5793 | mHandler.post(new Runnable() { |
| 5794 | @Override |
| 5795 | public void run() { |
| 5796 | String listenerName = listener == null ? null : listener.component.toShortString(); |
| 5797 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 5798 | pkg, userId, mustHaveFlags, mustNotHaveFlags, reason, |
| 5799 | listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5800 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5801 | // Why does this parameter exist? Do we actually want to execute the above if doit |
| 5802 | // is false? |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 5803 | if (!doit) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5804 | return; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 5805 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5806 | |
| 5807 | synchronized (mNotificationLock) { |
| 5808 | FlagChecker flagChecker = (int flags) -> { |
| 5809 | if ((flags & mustHaveFlags) != mustHaveFlags) { |
| 5810 | return false; |
| 5811 | } |
| 5812 | if ((flags & mustNotHaveFlags) != 0) { |
| 5813 | return false; |
| 5814 | } |
| 5815 | return true; |
| 5816 | }; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5817 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 5818 | pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, flagChecker, |
| 5819 | false /*includeCurrentProfiles*/, userId, false /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5820 | listenerName, true /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5821 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 5822 | callingPid, pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, |
| 5823 | flagChecker, false /*includeCurrentProfiles*/, userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5824 | false /*sendDelete*/, reason, listenerName, false /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5825 | mSnoozeHelper.cancel(userId, pkg); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5826 | } |
| 5827 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5828 | }); |
| 5829 | } |
| 5830 | |
| 5831 | private interface FlagChecker { |
| 5832 | // Returns false if these flags do not pass the defined flag test. |
| 5833 | public boolean apply(int flags); |
| 5834 | } |
| 5835 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5836 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5837 | private void cancelAllNotificationsByListLocked(ArrayList<NotificationRecord> notificationList, |
| 5838 | int callingUid, int callingPid, String pkg, boolean nullPkgIndicatesUserSwitch, |
| 5839 | String channelId, FlagChecker flagChecker, boolean includeCurrentProfiles, int userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5840 | boolean sendDelete, int reason, String listenerName, boolean wasPosted) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5841 | ArrayList<NotificationRecord> canceledNotifications = null; |
| 5842 | for (int i = notificationList.size() - 1; i >= 0; --i) { |
| 5843 | NotificationRecord r = notificationList.get(i); |
| 5844 | if (includeCurrentProfiles) { |
| 5845 | if (!notificationMatchesCurrentProfiles(r, userId)) { |
| 5846 | continue; |
| 5847 | } |
| 5848 | } else if (!notificationMatchesUserId(r, userId)) { |
| 5849 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5850 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5851 | // Don't remove notifications to all, if there's no package name specified |
| 5852 | if (nullPkgIndicatesUserSwitch && pkg == null && r.getUserId() == UserHandle.USER_ALL) { |
| 5853 | continue; |
| 5854 | } |
| 5855 | if (!flagChecker.apply(r.getFlags())) { |
| 5856 | continue; |
| 5857 | } |
| 5858 | if (pkg != null && !r.sbn.getPackageName().equals(pkg)) { |
| 5859 | continue; |
| 5860 | } |
| 5861 | if (channelId != null && !channelId.equals(r.getChannel().getId())) { |
| 5862 | continue; |
| 5863 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5864 | if (canceledNotifications == null) { |
| 5865 | canceledNotifications = new ArrayList<>(); |
| 5866 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5867 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 5868 | mNotificationsByKey.remove(r.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5869 | canceledNotifications.add(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5870 | cancelNotificationLocked(r, sendDelete, reason, wasPosted, listenerName); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5871 | } |
| 5872 | if (canceledNotifications != null) { |
| 5873 | final int M = canceledNotifications.size(); |
| 5874 | for (int i = 0; i < M; i++) { |
| 5875 | cancelGroupChildrenLocked(canceledNotifications.get(i), callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5876 | listenerName, false /* sendDelete */, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5877 | } |
| 5878 | updateLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5879 | } |
| 5880 | } |
| 5881 | |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 5882 | void snoozeNotificationInt(String key, long duration, String snoozeCriterionId, |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5883 | ManagedServiceInfo listener) { |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5884 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 5885 | if (duration <= 0 && snoozeCriterionId == null || key == null) { |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 5886 | return; |
| 5887 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 5888 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5889 | if (DBG) { |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 5890 | Slog.d(TAG, String.format("snooze event(%s, %d, %s, %s)", key, duration, |
| 5891 | snoozeCriterionId, listenerName)); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5892 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5893 | // Needs to post so that it can cancel notifications not yet enqueued. |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5894 | mHandler.post(new SnoozeNotificationRunnable(key, duration, snoozeCriterionId)); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 5895 | } |
| 5896 | |
| 5897 | void unsnoozeNotificationInt(String key, ManagedServiceInfo listener) { |
| 5898 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 5899 | if (DBG) { |
| 5900 | Slog.d(TAG, String.format("unsnooze event(%s, %s)", key, listenerName)); |
| 5901 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5902 | mSnoozeHelper.repost(key); |
| 5903 | savePolicyFile(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 5904 | } |
| 5905 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5906 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 350159c | 2014-03-27 11:15:11 -0700 | [diff] [blame] | 5907 | void cancelAllLocked(int callingUid, int callingPid, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5908 | ManagedServiceInfo listener, boolean includeCurrentProfiles) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5909 | mHandler.post(new Runnable() { |
| 5910 | @Override |
| 5911 | public void run() { |
| 5912 | synchronized (mNotificationLock) { |
| 5913 | String listenerName = |
| 5914 | listener == null ? null : listener.component.toShortString(); |
| 5915 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 5916 | null, userId, 0, 0, reason, listenerName); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5917 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5918 | FlagChecker flagChecker = (int flags) -> { |
| 5919 | if ((flags & (Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR)) |
| 5920 | != 0) { |
| 5921 | return false; |
| 5922 | } |
| 5923 | return true; |
| 5924 | }; |
| 5925 | |
| 5926 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 5927 | null, false /*nullPkgIndicatesUserSwitch*/, null, flagChecker, |
| 5928 | includeCurrentProfiles, userId, true /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5929 | listenerName, true); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5930 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 5931 | callingPid, null, false /*nullPkgIndicatesUserSwitch*/, null, |
| 5932 | flagChecker, includeCurrentProfiles, userId, true /*sendDelete*/, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5933 | reason, listenerName, false); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5934 | mSnoozeHelper.cancel(userId, includeCurrentProfiles); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5935 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5936 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5937 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5938 | } |
| 5939 | |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5940 | // Warning: The caller is responsible for invoking updateLightsLocked(). |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5941 | @GuardedBy("mNotificationLock") |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5942 | private void cancelGroupChildrenLocked(NotificationRecord r, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5943 | String listenerName, boolean sendDelete, FlagChecker flagChecker) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5944 | Notification n = r.getNotification(); |
Christoph Studer | 3f31f5d | 2014-07-31 16:55:32 +0200 | [diff] [blame] | 5945 | if (!n.isGroupSummary()) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5946 | return; |
| 5947 | } |
| 5948 | |
| 5949 | String pkg = r.sbn.getPackageName(); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5950 | |
| 5951 | if (pkg == null) { |
| 5952 | if (DBG) Log.e(TAG, "No package for group summary: " + r.getKey()); |
| 5953 | return; |
| 5954 | } |
| 5955 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5956 | cancelGroupChildrenByListLocked(mNotificationList, r, callingUid, callingPid, listenerName, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5957 | sendDelete, true, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5958 | cancelGroupChildrenByListLocked(mEnqueuedNotifications, r, callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5959 | listenerName, sendDelete, false, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5960 | } |
| 5961 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5962 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5963 | private void cancelGroupChildrenByListLocked(ArrayList<NotificationRecord> notificationList, |
| 5964 | NotificationRecord parentNotification, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5965 | String listenerName, boolean sendDelete, boolean wasPosted, FlagChecker flagChecker) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5966 | final String pkg = parentNotification.sbn.getPackageName(); |
| 5967 | final int userId = parentNotification.getUserId(); |
| 5968 | final int reason = REASON_GROUP_SUMMARY_CANCELED; |
| 5969 | for (int i = notificationList.size() - 1; i >= 0; i--) { |
| 5970 | final NotificationRecord childR = notificationList.get(i); |
| 5971 | final StatusBarNotification childSbn = childR.sbn; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5972 | if ((childSbn.isGroup() && !childSbn.getNotification().isGroupSummary()) && |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5973 | childR.getGroupKey().equals(parentNotification.getGroupKey()) |
Julia Reynolds | e5c6045 | 2018-04-30 14:41:36 -0400 | [diff] [blame] | 5974 | && (childR.getFlags() & FLAG_FOREGROUND_SERVICE) == 0 |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5975 | && (flagChecker == null || flagChecker.apply(childR.getFlags()))) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5976 | EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, childSbn.getId(), |
| 5977 | childSbn.getTag(), userId, 0, 0, reason, listenerName); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5978 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 5979 | mNotificationsByKey.remove(childR.getKey()); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5980 | cancelNotificationLocked(childR, sendDelete, reason, wasPosted, listenerName); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5981 | } |
| 5982 | } |
| 5983 | } |
| 5984 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5985 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5986 | void updateLightsLocked() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5987 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5988 | // handle notification lights |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 5989 | NotificationRecord ledNotification = null; |
| 5990 | while (ledNotification == null && !mLights.isEmpty()) { |
| 5991 | final String owner = mLights.get(mLights.size() - 1); |
| 5992 | ledNotification = mNotificationsByKey.get(owner); |
| 5993 | if (ledNotification == null) { |
| 5994 | Slog.wtfStack(TAG, "LED Notification does not exist: " + owner); |
| 5995 | mLights.remove(owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5996 | } |
| 5997 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 5998 | |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 5999 | // 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] | 6000 | if (ledNotification == null || mInCall || mScreenOn) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 6001 | mNotificationLight.turnOff(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 6002 | } else { |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 6003 | NotificationRecord.Light light = ledNotification.getLight(); |
| 6004 | if (light != null && mNotificationPulseEnabled) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 6005 | // pulse repeatedly |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 6006 | mNotificationLight.setFlashing(light.color, Light.LIGHT_FLASH_TIMED, |
| 6007 | light.onMs, light.offMs); |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 6008 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 6009 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6010 | } |
| 6011 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6012 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 6013 | @NonNull List<NotificationRecord> findGroupNotificationsLocked(String pkg, |
| 6014 | String groupKey, int userId) { |
| 6015 | List<NotificationRecord> records = new ArrayList<>(); |
| 6016 | records.addAll(findGroupNotificationByListLocked(mNotificationList, pkg, groupKey, userId)); |
| 6017 | records.addAll( |
| 6018 | findGroupNotificationByListLocked(mEnqueuedNotifications, pkg, groupKey, userId)); |
| 6019 | return records; |
| 6020 | } |
| 6021 | |
| 6022 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6023 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 6024 | private @NonNull List<NotificationRecord> findGroupNotificationByListLocked( |
| 6025 | ArrayList<NotificationRecord> list, String pkg, String groupKey, int userId) { |
| 6026 | List<NotificationRecord> records = new ArrayList<>(); |
| 6027 | final int len = list.size(); |
| 6028 | for (int i = 0; i < len; i++) { |
| 6029 | NotificationRecord r = list.get(i); |
| 6030 | if (notificationMatchesUserId(r, userId) && r.getGroupKey().equals(groupKey) |
| 6031 | && r.sbn.getPackageName().equals(pkg)) { |
| 6032 | records.add(r); |
| 6033 | } |
| 6034 | } |
| 6035 | return records; |
| 6036 | } |
| 6037 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6038 | // Searches both enqueued and posted notifications by key. |
| 6039 | // TODO: need to combine a bunch of these getters with slightly different behavior. |
| 6040 | // TODO: Should enqueuing just add to mNotificationsByKey instead? |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6041 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6042 | private NotificationRecord findNotificationByKeyLocked(String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6043 | NotificationRecord r; |
| 6044 | if ((r = findNotificationByListLocked(mNotificationList, key)) != null) { |
| 6045 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6046 | } |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6047 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, key)) != null) { |
| 6048 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6049 | } |
| 6050 | return null; |
| 6051 | } |
| 6052 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6053 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 6054 | NotificationRecord findNotificationLocked(String pkg, String tag, int id, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6055 | NotificationRecord r; |
| 6056 | if ((r = findNotificationByListLocked(mNotificationList, pkg, tag, id, userId)) != null) { |
| 6057 | return r; |
| 6058 | } |
| 6059 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, pkg, tag, id, userId)) |
| 6060 | != null) { |
| 6061 | return r; |
| 6062 | } |
| 6063 | return null; |
| 6064 | } |
| 6065 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6066 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6067 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6068 | String pkg, String tag, int id, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6069 | final int len = list.size(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6070 | for (int i = 0; i < len; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6071 | NotificationRecord r = list.get(i); |
Vladimir Marko | 2526f33 | 2013-09-11 11:13:55 +0100 | [diff] [blame] | 6072 | if (notificationMatchesUserId(r, userId) && r.sbn.getId() == id && |
| 6073 | TextUtils.equals(r.sbn.getTag(), tag) && r.sbn.getPackageName().equals(pkg)) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6074 | return r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6075 | } |
| 6076 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6077 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6078 | } |
| 6079 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6080 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6081 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6082 | String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 6083 | final int N = list.size(); |
| 6084 | for (int i = 0; i < N; i++) { |
| 6085 | if (key.equals(list.get(i).getKey())) { |
| 6086 | return list.get(i); |
| 6087 | } |
| 6088 | } |
| 6089 | return null; |
| 6090 | } |
| 6091 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6092 | @GuardedBy("mNotificationLock") |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 6093 | int indexOfNotificationLocked(String key) { |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 6094 | final int N = mNotificationList.size(); |
| 6095 | for (int i = 0; i < N; i++) { |
| 6096 | if (key.equals(mNotificationList.get(i).getKey())) { |
| 6097 | return i; |
| 6098 | } |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 6099 | } |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 6100 | return -1; |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 6101 | } |
| 6102 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6103 | @VisibleForTesting |
| 6104 | protected void hideNotificationsForPackages(String[] pkgs) { |
| 6105 | synchronized (mNotificationLock) { |
| 6106 | List<String> pkgList = Arrays.asList(pkgs); |
| 6107 | List<NotificationRecord> changedNotifications = new ArrayList<>(); |
| 6108 | int numNotifications = mNotificationList.size(); |
| 6109 | for (int i = 0; i < numNotifications; i++) { |
| 6110 | NotificationRecord rec = mNotificationList.get(i); |
| 6111 | if (pkgList.contains(rec.sbn.getPackageName())) { |
| 6112 | rec.setHidden(true); |
| 6113 | changedNotifications.add(rec); |
| 6114 | } |
| 6115 | } |
| 6116 | |
| 6117 | mListeners.notifyHiddenLocked(changedNotifications); |
| 6118 | } |
| 6119 | } |
| 6120 | |
| 6121 | @VisibleForTesting |
| 6122 | protected void unhideNotificationsForPackages(String[] pkgs) { |
| 6123 | synchronized (mNotificationLock) { |
| 6124 | List<String> pkgList = Arrays.asList(pkgs); |
| 6125 | List<NotificationRecord> changedNotifications = new ArrayList<>(); |
| 6126 | int numNotifications = mNotificationList.size(); |
| 6127 | for (int i = 0; i < numNotifications; i++) { |
| 6128 | NotificationRecord rec = mNotificationList.get(i); |
| 6129 | if (pkgList.contains(rec.sbn.getPackageName())) { |
| 6130 | rec.setHidden(false); |
| 6131 | changedNotifications.add(rec); |
| 6132 | } |
| 6133 | } |
| 6134 | |
| 6135 | mListeners.notifyUnhiddenLocked(changedNotifications); |
| 6136 | } |
| 6137 | } |
| 6138 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 6139 | private void updateNotificationPulse() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6140 | synchronized (mNotificationLock) { |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 6141 | updateLightsLocked(); |
| 6142 | } |
| 6143 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 6144 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 6145 | protected boolean isCallingUidSystem() { |
| 6146 | final int uid = Binder.getCallingUid(); |
| 6147 | return uid == Process.SYSTEM_UID; |
| 6148 | } |
| 6149 | |
| 6150 | protected boolean isUidSystemOrPhone(int uid) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6151 | final int appid = UserHandle.getAppId(uid); |
| 6152 | return (appid == Process.SYSTEM_UID || appid == Process.PHONE_UID || uid == 0); |
| 6153 | } |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 6154 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 6155 | // TODO: Most calls should probably move to isCallerSystem. |
| 6156 | protected boolean isCallerSystemOrPhone() { |
| 6157 | return isUidSystemOrPhone(Binder.getCallingUid()); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6158 | } |
| 6159 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6160 | private void checkCallerIsSystemOrShell() { |
| 6161 | if (Binder.getCallingUid() == Process.SHELL_UID) { |
| 6162 | return; |
| 6163 | } |
| 6164 | checkCallerIsSystem(); |
| 6165 | } |
| 6166 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6167 | private void checkCallerIsSystem() { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 6168 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6169 | return; |
| 6170 | } |
| 6171 | throw new SecurityException("Disallowed call for uid " + Binder.getCallingUid()); |
| 6172 | } |
| 6173 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6174 | private void checkCallerIsSystemOrSameApp(String pkg) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 6175 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6176 | return; |
| 6177 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 6178 | checkCallerIsSameApp(pkg); |
| 6179 | } |
| 6180 | |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 6181 | private boolean isCallerInstantApp(String pkg) { |
| 6182 | // System is always allowed to act for ephemeral apps. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 6183 | if (isCallerSystemOrPhone()) { |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 6184 | return false; |
| 6185 | } |
| 6186 | |
| 6187 | mAppOps.checkPackage(Binder.getCallingUid(), pkg); |
| 6188 | |
| 6189 | try { |
| 6190 | ApplicationInfo ai = mPackageManager.getApplicationInfo(pkg, 0, |
| 6191 | UserHandle.getCallingUserId()); |
| 6192 | if (ai == null) { |
| 6193 | throw new SecurityException("Unknown package " + pkg); |
| 6194 | } |
| 6195 | return ai.isInstantApp(); |
| 6196 | } catch (RemoteException re) { |
| 6197 | throw new SecurityException("Unknown package " + pkg, re); |
| 6198 | } |
| 6199 | |
| 6200 | } |
| 6201 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6202 | private void checkCallerIsSameApp(String pkg) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6203 | final int uid = Binder.getCallingUid(); |
| 6204 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6205 | ApplicationInfo ai = mPackageManager.getApplicationInfo( |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6206 | pkg, 0, UserHandle.getCallingUserId()); |
Dan Sandler | 09afc2e | 2014-07-18 14:29:20 -0400 | [diff] [blame] | 6207 | if (ai == null) { |
| 6208 | throw new SecurityException("Unknown package " + pkg); |
| 6209 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6210 | if (!UserHandle.isSameApp(ai.uid, uid)) { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6211 | throw new SecurityException("Calling uid " + uid + " gave package " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6212 | + pkg + " which is owned by uid " + ai.uid); |
| 6213 | } |
| 6214 | } catch (RemoteException re) { |
| 6215 | throw new SecurityException("Unknown package " + pkg + "\n" + re); |
| 6216 | } |
| 6217 | } |
| 6218 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 6219 | private static String callStateToString(int state) { |
| 6220 | switch (state) { |
| 6221 | case TelephonyManager.CALL_STATE_IDLE: return "CALL_STATE_IDLE"; |
| 6222 | case TelephonyManager.CALL_STATE_RINGING: return "CALL_STATE_RINGING"; |
| 6223 | case TelephonyManager.CALL_STATE_OFFHOOK: return "CALL_STATE_OFFHOOK"; |
| 6224 | default: return "CALL_STATE_UNKNOWN_" + state; |
| 6225 | } |
| 6226 | } |
| 6227 | |
| 6228 | private void listenForCallState() { |
| 6229 | TelephonyManager.from(getContext()).listen(new PhoneStateListener() { |
| 6230 | @Override |
| 6231 | public void onCallStateChanged(int state, String incomingNumber) { |
| 6232 | if (mCallState == state) return; |
| 6233 | if (DBG) Slog.d(TAG, "Call state changed: " + callStateToString(state)); |
| 6234 | mCallState = state; |
| 6235 | } |
| 6236 | }, PhoneStateListener.LISTEN_CALL_STATE); |
| 6237 | } |
| 6238 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6239 | /** |
| 6240 | * Generates a NotificationRankingUpdate from 'sbns', considering only |
| 6241 | * notifications visible to the given listener. |
| 6242 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6243 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6244 | private NotificationRankingUpdate makeRankingUpdateLocked(ManagedServiceInfo info) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6245 | final int N = mNotificationList.size(); |
| 6246 | ArrayList<String> keys = new ArrayList<String>(N); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 6247 | ArrayList<String> interceptedKeys = new ArrayList<String>(N); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6248 | ArrayList<Integer> importance = new ArrayList<>(N); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 6249 | Bundle overrideGroupKeys = new Bundle(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6250 | Bundle visibilityOverrides = new Bundle(); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 6251 | Bundle suppressedVisualEffects = new Bundle(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6252 | Bundle explanation = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 6253 | Bundle channels = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 6254 | Bundle overridePeople = new Bundle(); |
| 6255 | Bundle snoozeCriteria = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 6256 | Bundle showBadge = new Bundle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6257 | Bundle userSentiment = new Bundle(); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6258 | Bundle hidden = new Bundle(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6259 | for (int i = 0; i < N; i++) { |
| 6260 | NotificationRecord record = mNotificationList.get(i); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6261 | if (!isVisibleToListener(record.sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6262 | continue; |
| 6263 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6264 | final String key = record.sbn.getKey(); |
| 6265 | keys.add(key); |
| 6266 | importance.add(record.getImportance()); |
| 6267 | if (record.getImportanceExplanation() != null) { |
| 6268 | explanation.putCharSequence(key, record.getImportanceExplanation()); |
| 6269 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6270 | if (record.isIntercepted()) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6271 | interceptedKeys.add(key); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 6272 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6273 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6274 | suppressedVisualEffects.putInt(key, record.getSuppressedVisualEffects()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6275 | if (record.getPackageVisibilityOverride() |
| 6276 | != NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6277 | visibilityOverrides.putInt(key, record.getPackageVisibilityOverride()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6278 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 6279 | overrideGroupKeys.putString(key, record.sbn.getOverrideGroupKey()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 6280 | channels.putParcelable(key, record.getChannel()); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 6281 | overridePeople.putStringArrayList(key, record.getPeopleOverride()); |
| 6282 | snoozeCriteria.putParcelableArrayList(key, record.getSnoozeCriteria()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 6283 | showBadge.putBoolean(key, record.canShowBadge()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6284 | userSentiment.putInt(key, record.getUserSentiment()); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6285 | hidden.putBoolean(key, record.isHidden()); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6286 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6287 | final int M = keys.size(); |
| 6288 | String[] keysAr = keys.toArray(new String[M]); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 6289 | String[] interceptedKeysAr = interceptedKeys.toArray(new String[interceptedKeys.size()]); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 6290 | int[] importanceAr = new int[M]; |
| 6291 | for (int i = 0; i < M; i++) { |
| 6292 | importanceAr[i] = importance.get(i); |
| 6293 | } |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 6294 | return new NotificationRankingUpdate(keysAr, interceptedKeysAr, visibilityOverrides, |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 6295 | suppressedVisualEffects, importanceAr, explanation, overrideGroupKeys, |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6296 | channels, overridePeople, snoozeCriteria, showBadge, userSentiment, hidden); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6297 | } |
| 6298 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6299 | boolean hasCompanionDevice(ManagedServiceInfo info) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6300 | if (mCompanionManager == null) { |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 6301 | mCompanionManager = getCompanionManager(); |
| 6302 | } |
| 6303 | // Companion mgr doesn't exist on all device types |
| 6304 | if (mCompanionManager == null) { |
| 6305 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6306 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6307 | long identity = Binder.clearCallingIdentity(); |
| 6308 | try { |
| 6309 | List<String> associations = mCompanionManager.getAssociations( |
| 6310 | info.component.getPackageName(), info.userid); |
| 6311 | if (!ArrayUtils.isEmpty(associations)) { |
| 6312 | return true; |
| 6313 | } |
| 6314 | } catch (SecurityException se) { |
| 6315 | // Not a privileged listener |
| 6316 | } catch (RemoteException re) { |
| 6317 | Slog.e(TAG, "Cannot reach companion device service", re); |
| 6318 | } catch (Exception e) { |
| 6319 | Slog.e(TAG, "Cannot verify listener " + info, e); |
| 6320 | } finally { |
| 6321 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6322 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6323 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6324 | } |
| 6325 | |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 6326 | protected ICompanionDeviceManager getCompanionManager() { |
| 6327 | return ICompanionDeviceManager.Stub.asInterface( |
| 6328 | ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE)); |
| 6329 | } |
| 6330 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6331 | private boolean isVisibleToListener(StatusBarNotification sbn, ManagedServiceInfo listener) { |
| 6332 | if (!listener.enabledAndUserMatches(sbn.getUserId())) { |
| 6333 | return false; |
| 6334 | } |
Justin Koh | 8d11a5a | 2014-08-04 18:29:49 -0700 | [diff] [blame] | 6335 | // TODO: remove this for older listeners. |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6336 | return true; |
| 6337 | } |
| 6338 | |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 6339 | private boolean isPackageSuspendedForUser(String pkg, int uid) { |
Beverly | 2be7a05 | 2018-03-27 11:37:58 -0400 | [diff] [blame] | 6340 | final long identity = Binder.clearCallingIdentity(); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 6341 | int userId = UserHandle.getUserId(uid); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 6342 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 6343 | return mPackageManager.isPackageSuspendedForUser(pkg, userId); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 6344 | } catch (RemoteException re) { |
| 6345 | throw new SecurityException("Could not talk to package manager service"); |
Andrei Stingaceanu | efc4a34 | 2016-03-22 14:43:01 +0000 | [diff] [blame] | 6346 | } catch (IllegalArgumentException ex) { |
| 6347 | // Package not found. |
| 6348 | return false; |
Beverly | 2be7a05 | 2018-03-27 11:37:58 -0400 | [diff] [blame] | 6349 | } finally { |
| 6350 | Binder.restoreCallingIdentity(identity); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 6351 | } |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 6352 | } |
| 6353 | |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 6354 | @VisibleForTesting |
| 6355 | boolean canUseManagedServices(String pkg) { |
| 6356 | boolean canUseManagedServices = !mActivityManager.isLowRamDevice() |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 6357 | || mPackageManagerClient.hasSystemFeature(PackageManager.FEATURE_WATCH); |
Kristian Monsen | 30f59b2 | 2018-04-09 10:27:16 +0200 | [diff] [blame] | 6358 | |
| 6359 | for (String whitelisted : getContext().getResources().getStringArray( |
| 6360 | R.array.config_allowedManagedServicesOnLowRamDevices)) { |
| 6361 | if (whitelisted.equals(pkg)) { |
| 6362 | canUseManagedServices = true; |
| 6363 | } |
| 6364 | } |
| 6365 | |
| 6366 | return canUseManagedServices; |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 6367 | } |
| 6368 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6369 | private class TrimCache { |
| 6370 | StatusBarNotification heavy; |
| 6371 | StatusBarNotification sbnClone; |
| 6372 | StatusBarNotification sbnCloneLight; |
| 6373 | |
| 6374 | TrimCache(StatusBarNotification sbn) { |
| 6375 | heavy = sbn; |
| 6376 | } |
| 6377 | |
| 6378 | StatusBarNotification ForListener(ManagedServiceInfo info) { |
| 6379 | if (mListeners.getOnNotificationPostedTrim(info) == TRIM_LIGHT) { |
| 6380 | if (sbnCloneLight == null) { |
| 6381 | sbnCloneLight = heavy.cloneLight(); |
| 6382 | } |
| 6383 | return sbnCloneLight; |
| 6384 | } else { |
| 6385 | if (sbnClone == null) { |
| 6386 | sbnClone = heavy.clone(); |
| 6387 | } |
| 6388 | return sbnClone; |
| 6389 | } |
| 6390 | } |
| 6391 | } |
| 6392 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 6393 | public class NotificationAssistants extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6394 | static final String TAG_ENABLED_NOTIFICATION_ASSISTANTS = "enabled_assistants"; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6395 | |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 6396 | public NotificationAssistants(Context context, Object lock, UserProfiles up, |
| 6397 | IPackageManager pm) { |
| 6398 | super(context, lock, up, pm); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6399 | } |
| 6400 | |
| 6401 | @Override |
| 6402 | protected Config getConfig() { |
| 6403 | Config c = new Config(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6404 | c.caption = "notification assistant"; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 6405 | c.serviceInterface = NotificationAssistantService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6406 | c.xmlTag = TAG_ENABLED_NOTIFICATION_ASSISTANTS; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 6407 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_ASSISTANT; |
| 6408 | c.bindPermission = Manifest.permission.BIND_NOTIFICATION_ASSISTANT_SERVICE; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6409 | c.settingsAction = Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS; |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 6410 | c.clientLabel = R.string.notification_ranker_binding_label; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6411 | return c; |
| 6412 | } |
| 6413 | |
| 6414 | @Override |
| 6415 | protected IInterface asInterface(IBinder binder) { |
| 6416 | return INotificationListener.Stub.asInterface(binder); |
| 6417 | } |
| 6418 | |
| 6419 | @Override |
| 6420 | protected boolean checkType(IInterface service) { |
| 6421 | return service instanceof INotificationListener; |
| 6422 | } |
| 6423 | |
| 6424 | @Override |
| 6425 | protected void onServiceAdded(ManagedServiceInfo info) { |
| 6426 | mListeners.registerGuestService(info); |
| 6427 | } |
| 6428 | |
| 6429 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6430 | @GuardedBy("mNotificationLock") |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6431 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
| 6432 | mListeners.unregisterService(removed.service, removed.userid); |
| 6433 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6434 | |
Julia Reynolds | ef934fd | 2018-02-01 14:39:17 -0500 | [diff] [blame] | 6435 | @Override |
| 6436 | public void onUserUnlocked(int user) { |
| 6437 | if (DEBUG) Slog.d(TAG, "onUserUnlocked u=" + user); |
| 6438 | rebindServices(true); |
| 6439 | } |
| 6440 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6441 | public void onNotificationEnqueued(final NotificationRecord r) { |
| 6442 | final StatusBarNotification sbn = r.sbn; |
| 6443 | TrimCache trimCache = new TrimCache(sbn); |
| 6444 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6445 | // There should be only one, but it's a list, so while we enforce |
| 6446 | // singularity elsewhere, we keep it general here, to avoid surprises. |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6447 | for (final ManagedServiceInfo info : NotificationAssistants.this.getServices()) { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6448 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 6449 | if (!sbnVisible) { |
| 6450 | continue; |
| 6451 | } |
| 6452 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6453 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 6454 | mHandler.post(new Runnable() { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6455 | @Override |
| 6456 | public void run() { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 6457 | notifyEnqueued(info, sbnToPost); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6458 | } |
| 6459 | }); |
| 6460 | } |
| 6461 | } |
| 6462 | |
| 6463 | private void notifyEnqueued(final ManagedServiceInfo info, |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 6464 | final StatusBarNotification sbn) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 6465 | final INotificationListener assistant = (INotificationListener) info.service; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6466 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
| 6467 | try { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 6468 | assistant.onNotificationEnqueued(sbnHolder); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6469 | } catch (RemoteException ex) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 6470 | Log.e(TAG, "unable to notify assistant (enqueued): " + assistant, ex); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6471 | } |
| 6472 | } |
| 6473 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6474 | /** |
| 6475 | * asynchronously notify the assistant that a notification has been snoozed until a |
| 6476 | * context |
| 6477 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6478 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6479 | public void notifyAssistantSnoozedLocked(final StatusBarNotification sbn, |
| 6480 | final String snoozeCriterionId) { |
| 6481 | TrimCache trimCache = new TrimCache(sbn); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6482 | for (final ManagedServiceInfo info : getServices()) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6483 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 6484 | if (!sbnVisible) { |
| 6485 | continue; |
| 6486 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 6487 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
| 6488 | mHandler.post(new Runnable() { |
| 6489 | @Override |
| 6490 | public void run() { |
| 6491 | final INotificationListener assistant = |
| 6492 | (INotificationListener) info.service; |
| 6493 | StatusBarNotificationHolder sbnHolder |
| 6494 | = new StatusBarNotificationHolder(sbnToPost); |
| 6495 | try { |
| 6496 | assistant.onNotificationSnoozedUntilContext( |
| 6497 | sbnHolder, snoozeCriterionId); |
| 6498 | } catch (RemoteException ex) { |
| 6499 | Log.e(TAG, "unable to notify assistant (snoozed): " + assistant, ex); |
| 6500 | } |
| 6501 | } |
| 6502 | }); |
| 6503 | } |
| 6504 | } |
| 6505 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6506 | public boolean isEnabled() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6507 | return !getServices().isEmpty(); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6508 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 6509 | |
Julia Reynolds | d6d5a59 | 2018-04-02 11:03:32 -0400 | [diff] [blame] | 6510 | protected void ensureAssistant() { |
| 6511 | final List<UserInfo> activeUsers = mUm.getUsers(true); |
| 6512 | for (UserInfo userInfo : activeUsers) { |
| 6513 | int userId = userInfo.getUserHandle().getIdentifier(); |
| 6514 | if (getAllowedPackages(userId).isEmpty()) { |
| 6515 | Slog.d(TAG, "Approving default notification assistant for user " + userId); |
| 6516 | readDefaultAssistant(userId); |
| 6517 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 6518 | } |
| 6519 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6520 | } |
| 6521 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6522 | public class NotificationListeners extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6523 | static final String TAG_ENABLED_NOTIFICATION_LISTENERS = "enabled_listeners"; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6524 | |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6525 | private final ArraySet<ManagedServiceInfo> mLightTrimListeners = new ArraySet<>(); |
| 6526 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6527 | public NotificationListeners(IPackageManager pm) { |
| 6528 | super(getContext(), mNotificationLock, mUserProfiles, pm); |
| 6529 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6530 | } |
| 6531 | |
| 6532 | @Override |
Amith Yamasani | e1fb58d | 2018-09-05 18:52:35 -0700 | [diff] [blame] | 6533 | protected int getBindFlags() { |
| 6534 | // Most of the same flags as the base, but also add BIND_ADJUST_BELOW_PERCEPTIBLE |
| 6535 | // because too many 3P apps could be kept in memory as notification listeners and |
| 6536 | // cause extreme memory pressure. |
| 6537 | // TODO: Change the binding lifecycle of NotificationListeners to avoid this situation. |
| 6538 | return BIND_AUTO_CREATE | BIND_FOREGROUND_SERVICE |
| 6539 | | BIND_ADJUST_BELOW_PERCEPTIBLE | BIND_ALLOW_WHITELIST_MANAGEMENT; |
| 6540 | } |
| 6541 | |
| 6542 | @Override |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6543 | protected Config getConfig() { |
| 6544 | Config c = new Config(); |
| 6545 | c.caption = "notification listener"; |
| 6546 | c.serviceInterface = NotificationListenerService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6547 | c.xmlTag = TAG_ENABLED_NOTIFICATION_LISTENERS; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6548 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS; |
| 6549 | c.bindPermission = android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE; |
| 6550 | c.settingsAction = Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS; |
| 6551 | c.clientLabel = R.string.notification_listener_binding_label; |
| 6552 | return c; |
| 6553 | } |
| 6554 | |
| 6555 | @Override |
| 6556 | protected IInterface asInterface(IBinder binder) { |
| 6557 | return INotificationListener.Stub.asInterface(binder); |
| 6558 | } |
| 6559 | |
| 6560 | @Override |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 6561 | protected boolean checkType(IInterface service) { |
| 6562 | return service instanceof INotificationListener; |
| 6563 | } |
| 6564 | |
| 6565 | @Override |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 6566 | public void onServiceAdded(ManagedServiceInfo info) { |
| 6567 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6568 | final NotificationRankingUpdate update; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6569 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6570 | update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6571 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6572 | try { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6573 | listener.onListenerConnected(update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6574 | } catch (RemoteException e) { |
| 6575 | // we tried |
| 6576 | } |
| 6577 | } |
| 6578 | |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6579 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6580 | @GuardedBy("mNotificationLock") |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6581 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 6582 | if (removeDisabledHints(removed)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6583 | updateListenerHintsLocked(); |
Christoph Studer | 0d6ef4b | 2014-12-02 15:00:48 +0100 | [diff] [blame] | 6584 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6585 | } |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6586 | mLightTrimListeners.remove(removed); |
| 6587 | } |
| 6588 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6589 | @GuardedBy("mNotificationLock") |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6590 | public void setOnNotificationPostedTrimLocked(ManagedServiceInfo info, int trim) { |
| 6591 | if (trim == TRIM_LIGHT) { |
| 6592 | mLightTrimListeners.add(info); |
| 6593 | } else { |
| 6594 | mLightTrimListeners.remove(info); |
| 6595 | } |
| 6596 | } |
| 6597 | |
| 6598 | public int getOnNotificationPostedTrim(ManagedServiceInfo info) { |
| 6599 | return mLightTrimListeners.contains(info) ? TRIM_LIGHT : TRIM_FULL; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6600 | } |
| 6601 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6602 | /** |
| 6603 | * asynchronously notify all listeners about a new notification |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6604 | * |
| 6605 | * <p> |
| 6606 | * Also takes care of removing a notification that has been visible to a listener before, |
| 6607 | * but isn't anymore. |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6608 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6609 | @GuardedBy("mNotificationLock") |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6610 | public void notifyPostedLocked(NotificationRecord r, NotificationRecord old) { |
| 6611 | notifyPostedLocked(r, old, true); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6612 | } |
| 6613 | |
| 6614 | /** |
| 6615 | * @param notifyAllListeners notifies all listeners if true, else only notifies listeners |
| 6616 | * targetting <= O_MR1 |
| 6617 | */ |
| 6618 | @GuardedBy("mNotificationLock") |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6619 | private void notifyPostedLocked(NotificationRecord r, NotificationRecord old, |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6620 | boolean notifyAllListeners) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6621 | // Lazily initialized snapshots of the notification. |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 6622 | StatusBarNotification sbn = r.sbn; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6623 | StatusBarNotification oldSbn = (old != null) ? old.sbn : null; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 6624 | TrimCache trimCache = new TrimCache(sbn); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6625 | |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6626 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6627 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 6628 | boolean oldSbnVisible = oldSbn != null ? isVisibleToListener(oldSbn, info) : false; |
| 6629 | // This notification hasn't been and still isn't visible -> ignore. |
| 6630 | if (!oldSbnVisible && !sbnVisible) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6631 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6632 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6633 | |
| 6634 | // If the notification is hidden, don't notifyPosted listeners targeting < P. |
| 6635 | // Instead, those listeners will receive notifyPosted when the notification is |
| 6636 | // unhidden. |
| 6637 | if (r.isHidden() && info.targetSdkVersion < Build.VERSION_CODES.P) { |
| 6638 | continue; |
| 6639 | } |
| 6640 | |
| 6641 | // If we shouldn't notify all listeners, this means the hidden state of |
| 6642 | // a notification was changed. Don't notifyPosted listeners targeting >= P. |
| 6643 | // Instead, those listeners will receive notifyRankingUpdate. |
| 6644 | if (!notifyAllListeners && info.targetSdkVersion >= Build.VERSION_CODES.P) { |
| 6645 | continue; |
| 6646 | } |
| 6647 | |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6648 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6649 | |
| 6650 | // This notification became invisible -> remove the old one. |
| 6651 | if (oldSbnVisible && !sbnVisible) { |
| 6652 | final StatusBarNotification oldSbnLightClone = oldSbn.cloneLight(); |
| 6653 | mHandler.post(new Runnable() { |
| 6654 | @Override |
| 6655 | public void run() { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6656 | notifyRemoved( |
| 6657 | info, oldSbnLightClone, update, null, REASON_USER_STOPPED); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6658 | } |
| 6659 | }); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6660 | continue; |
| 6661 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6662 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6663 | // Grant access before listener is notified |
| 6664 | final int targetUserId = (info.userid == UserHandle.USER_ALL) |
| 6665 | ? UserHandle.USER_SYSTEM : info.userid; |
| 6666 | updateUriPermissions(r, old, info.component.getPackageName(), targetUserId); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 6667 | |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6668 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6669 | mHandler.post(new Runnable() { |
| 6670 | @Override |
| 6671 | public void run() { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 6672 | notifyPosted(info, sbnToPost, update); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6673 | } |
| 6674 | }); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6675 | } |
| 6676 | } |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6677 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6678 | /** |
| 6679 | * asynchronously notify all listeners about a removed notification |
| 6680 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6681 | @GuardedBy("mNotificationLock") |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6682 | public void notifyRemovedLocked(NotificationRecord r, int reason, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6683 | NotificationStats notificationStats) { |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6684 | final StatusBarNotification sbn = r.sbn; |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6685 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6686 | // make a copy in case changes are made to the underlying Notification object |
| 6687 | // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the |
| 6688 | // notification |
| 6689 | final StatusBarNotification sbnLight = sbn.cloneLight(); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6690 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6691 | if (!isVisibleToListener(sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6692 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6693 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6694 | |
| 6695 | // don't notifyRemoved for listeners targeting < P |
| 6696 | // if not for reason package suspended |
| 6697 | if (r.isHidden() && reason != REASON_PACKAGE_SUSPENDED |
| 6698 | && info.targetSdkVersion < Build.VERSION_CODES.P) { |
| 6699 | continue; |
| 6700 | } |
| 6701 | |
| 6702 | // don't notifyRemoved for listeners targeting >= P |
| 6703 | // if the reason is package suspended |
| 6704 | if (reason == REASON_PACKAGE_SUSPENDED |
| 6705 | && info.targetSdkVersion >= Build.VERSION_CODES.P) { |
| 6706 | continue; |
| 6707 | } |
| 6708 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6709 | // Only assistants can get stats |
| 6710 | final NotificationStats stats = mAssistants.isServiceTokenValidLocked(info.service) |
| 6711 | ? notificationStats : null; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6712 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6713 | mHandler.post(new Runnable() { |
| 6714 | @Override |
| 6715 | public void run() { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6716 | notifyRemoved(info, sbnLight, update, stats, reason); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6717 | } |
| 6718 | }); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6719 | } |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6720 | |
| 6721 | // Revoke access after all listeners have been updated |
| 6722 | mHandler.post(() -> { |
| 6723 | updateUriPermissions(null, r, null, UserHandle.USER_SYSTEM); |
| 6724 | }); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6725 | } |
| 6726 | |
| 6727 | /** |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6728 | * Asynchronously notify all listeners about a reordering of notifications |
| 6729 | * unless changedHiddenNotifications is populated. |
| 6730 | * If changedHiddenNotifications is populated, there was a change in the hidden state |
| 6731 | * of the notifications. In this case, we only send updates to listeners that |
| 6732 | * target >= P. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6733 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6734 | @GuardedBy("mNotificationLock") |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6735 | public void notifyRankingUpdateLocked(List<NotificationRecord> changedHiddenNotifications) { |
| 6736 | boolean isHiddenRankingUpdate = changedHiddenNotifications != null |
| 6737 | && changedHiddenNotifications.size() > 0; |
| 6738 | |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6739 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6740 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 6741 | continue; |
| 6742 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6743 | |
| 6744 | boolean notifyThisListener = false; |
| 6745 | if (isHiddenRankingUpdate && serviceInfo.targetSdkVersion >= |
| 6746 | Build.VERSION_CODES.P) { |
| 6747 | for (NotificationRecord rec : changedHiddenNotifications) { |
| 6748 | if (isVisibleToListener(rec.sbn, serviceInfo)) { |
| 6749 | notifyThisListener = true; |
| 6750 | break; |
| 6751 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6752 | } |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6753 | } |
| 6754 | |
| 6755 | if (notifyThisListener || !isHiddenRankingUpdate) { |
| 6756 | final NotificationRankingUpdate update = makeRankingUpdateLocked( |
| 6757 | serviceInfo); |
| 6758 | |
| 6759 | mHandler.post(new Runnable() { |
| 6760 | @Override |
| 6761 | public void run() { |
| 6762 | notifyRankingUpdate(serviceInfo, update); |
| 6763 | } |
| 6764 | }); |
| 6765 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6766 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6767 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6768 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6769 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6770 | public void notifyListenerHintsChangedLocked(final int hints) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6771 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6772 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 6773 | continue; |
| 6774 | } |
| 6775 | mHandler.post(new Runnable() { |
| 6776 | @Override |
| 6777 | public void run() { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6778 | notifyListenerHintsChanged(serviceInfo, hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6779 | } |
| 6780 | }); |
| 6781 | } |
| 6782 | } |
| 6783 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6784 | /** |
| 6785 | * asynchronously notify relevant listeners their notification is hidden |
| 6786 | * NotificationListenerServices that target P+: |
| 6787 | * NotificationListenerService#notifyRankingUpdateLocked() |
| 6788 | * NotificationListenerServices that target <= P: |
| 6789 | * NotificationListenerService#notifyRemovedLocked() with REASON_PACKAGE_SUSPENDED. |
| 6790 | */ |
| 6791 | @GuardedBy("mNotificationLock") |
| 6792 | public void notifyHiddenLocked(List<NotificationRecord> changedNotifications) { |
| 6793 | if (changedNotifications == null || changedNotifications.size() == 0) { |
| 6794 | return; |
| 6795 | } |
| 6796 | |
| 6797 | notifyRankingUpdateLocked(changedNotifications); |
| 6798 | |
| 6799 | // for listeners that target < P, notifyRemoveLocked |
| 6800 | int numChangedNotifications = changedNotifications.size(); |
| 6801 | for (int i = 0; i < numChangedNotifications; i++) { |
| 6802 | NotificationRecord rec = changedNotifications.get(i); |
| 6803 | mListeners.notifyRemovedLocked(rec, REASON_PACKAGE_SUSPENDED, rec.getStats()); |
| 6804 | } |
| 6805 | } |
| 6806 | |
| 6807 | /** |
| 6808 | * asynchronously notify relevant listeners their notification is unhidden |
| 6809 | * NotificationListenerServices that target P+: |
| 6810 | * NotificationListenerService#notifyRankingUpdateLocked() |
| 6811 | * NotificationListenerServices that target <= P: |
| 6812 | * NotificationListeners#notifyPostedLocked() |
| 6813 | */ |
| 6814 | @GuardedBy("mNotificationLock") |
| 6815 | public void notifyUnhiddenLocked(List<NotificationRecord> changedNotifications) { |
| 6816 | if (changedNotifications == null || changedNotifications.size() == 0) { |
| 6817 | return; |
| 6818 | } |
| 6819 | |
| 6820 | notifyRankingUpdateLocked(changedNotifications); |
| 6821 | |
| 6822 | // for listeners that target < P, notifyPostedLocked |
| 6823 | int numChangedNotifications = changedNotifications.size(); |
| 6824 | for (int i = 0; i < numChangedNotifications; i++) { |
| 6825 | NotificationRecord rec = changedNotifications.get(i); |
Jeff Sharkey | 6a97cc3 | 2018-04-17 12:16:20 -0600 | [diff] [blame] | 6826 | mListeners.notifyPostedLocked(rec, rec, false); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 6827 | } |
| 6828 | } |
| 6829 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 6830 | public void notifyInterruptionFilterChanged(final int interruptionFilter) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6831 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 6832 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 6833 | continue; |
| 6834 | } |
| 6835 | mHandler.post(new Runnable() { |
| 6836 | @Override |
| 6837 | public void run() { |
| 6838 | notifyInterruptionFilterChanged(serviceInfo, interruptionFilter); |
| 6839 | } |
| 6840 | }); |
| 6841 | } |
| 6842 | } |
| 6843 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6844 | protected void notifyNotificationChannelChanged(final String pkg, final UserHandle user, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6845 | final NotificationChannel channel, final int modificationType) { |
| 6846 | if (channel == null) { |
| 6847 | return; |
| 6848 | } |
| 6849 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6850 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6851 | continue; |
| 6852 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 6853 | |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 6854 | BackgroundThread.getHandler().post(() -> { |
| 6855 | if (hasCompanionDevice(serviceInfo)) { |
| 6856 | notifyNotificationChannelChanged( |
| 6857 | serviceInfo, pkg, user, channel, modificationType); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6858 | } |
| 6859 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6860 | } |
| 6861 | } |
| 6862 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6863 | protected void notifyNotificationChannelGroupChanged( |
| 6864 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
| 6865 | final int modificationType) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6866 | if (group == null) { |
| 6867 | return; |
| 6868 | } |
| 6869 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6870 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6871 | continue; |
| 6872 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 6873 | |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 6874 | BackgroundThread.getHandler().post(() -> { |
| 6875 | if (hasCompanionDevice(serviceInfo)) { |
| 6876 | notifyNotificationChannelGroupChanged( |
| 6877 | serviceInfo, pkg, user, group, modificationType); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6878 | } |
| 6879 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6880 | } |
| 6881 | } |
| 6882 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6883 | private void notifyPosted(final ManagedServiceInfo info, |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6884 | final StatusBarNotification sbn, NotificationRankingUpdate rankingUpdate) { |
Julia Reynolds | a75c752 | 2017-03-21 17:34:25 -0400 | [diff] [blame] | 6885 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6886 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6887 | try { |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6888 | listener.onNotificationPosted(sbnHolder, rankingUpdate); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6889 | } catch (RemoteException ex) { |
| 6890 | Log.e(TAG, "unable to notify listener (posted): " + listener, ex); |
| 6891 | } |
| 6892 | } |
| 6893 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6894 | private void notifyRemoved(ManagedServiceInfo info, StatusBarNotification sbn, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6895 | NotificationRankingUpdate rankingUpdate, NotificationStats stats, int reason) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6896 | if (!info.enabledAndUserMatches(sbn.getUserId())) { |
| 6897 | return; |
| 6898 | } |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6899 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6900 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6901 | try { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6902 | listener.onNotificationRemoved(sbnHolder, rankingUpdate, stats, reason); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6903 | } catch (RemoteException ex) { |
| 6904 | Log.e(TAG, "unable to notify listener (removed): " + listener, ex); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 6905 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6906 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6907 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6908 | private void notifyRankingUpdate(ManagedServiceInfo info, |
| 6909 | NotificationRankingUpdate rankingUpdate) { |
| 6910 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6911 | try { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6912 | listener.onNotificationRankingUpdate(rankingUpdate); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6913 | } catch (RemoteException ex) { |
| 6914 | Log.e(TAG, "unable to notify listener (ranking update): " + listener, ex); |
| 6915 | } |
| 6916 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6917 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6918 | private void notifyListenerHintsChanged(ManagedServiceInfo info, int hints) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6919 | final INotificationListener listener = (INotificationListener) info.service; |
| 6920 | try { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6921 | listener.onListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6922 | } catch (RemoteException ex) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6923 | Log.e(TAG, "unable to notify listener (listener hints): " + listener, ex); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6924 | } |
| 6925 | } |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 6926 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 6927 | private void notifyInterruptionFilterChanged(ManagedServiceInfo info, |
| 6928 | int interruptionFilter) { |
| 6929 | final INotificationListener listener = (INotificationListener) info.service; |
| 6930 | try { |
| 6931 | listener.onInterruptionFilterChanged(interruptionFilter); |
| 6932 | } catch (RemoteException ex) { |
| 6933 | Log.e(TAG, "unable to notify listener (interruption filter): " + listener, ex); |
| 6934 | } |
| 6935 | } |
| 6936 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6937 | void notifyNotificationChannelChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6938 | final String pkg, final UserHandle user, final NotificationChannel channel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6939 | final int modificationType) { |
| 6940 | final INotificationListener listener = (INotificationListener) info.service; |
| 6941 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6942 | listener.onNotificationChannelModification(pkg, user, channel, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6943 | } catch (RemoteException ex) { |
| 6944 | Log.e(TAG, "unable to notify listener (channel changed): " + listener, ex); |
| 6945 | } |
| 6946 | } |
| 6947 | |
| 6948 | private void notifyNotificationChannelGroupChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6949 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6950 | final int modificationType) { |
| 6951 | final INotificationListener listener = (INotificationListener) info.service; |
| 6952 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6953 | listener.onNotificationChannelGroupModification(pkg, user, group, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6954 | } catch (RemoteException ex) { |
| 6955 | Log.e(TAG, "unable to notify listener (channel group changed): " + listener, ex); |
| 6956 | } |
| 6957 | } |
| 6958 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6959 | public boolean isListenerPackage(String packageName) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 6960 | if (packageName == null) { |
| 6961 | return false; |
| 6962 | } |
| 6963 | // TODO: clean up locking object later |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6964 | synchronized (mNotificationLock) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6965 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 6966 | if (packageName.equals(serviceInfo.component.getPackageName())) { |
| 6967 | return true; |
| 6968 | } |
| 6969 | } |
| 6970 | } |
| 6971 | return false; |
| 6972 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6973 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6974 | |
| 6975 | public static final class DumpFilter { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6976 | public boolean filtered = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6977 | public String pkgFilter; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 6978 | public boolean zen; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 6979 | public long since; |
| 6980 | public boolean stats; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6981 | public boolean redact = true; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 6982 | public boolean proto = false; |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 6983 | public boolean criticalPriority = false; |
| 6984 | public boolean normalPriority = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6985 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 6986 | @NonNull |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6987 | public static DumpFilter parseFromArguments(String[] args) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6988 | final DumpFilter filter = new DumpFilter(); |
| 6989 | for (int ai = 0; ai < args.length; ai++) { |
| 6990 | final String a = args[ai]; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 6991 | if ("--proto".equals(a)) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 6992 | filter.proto = true; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 6993 | } else if ("--noredact".equals(a) || "--reveal".equals(a)) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6994 | filter.redact = false; |
| 6995 | } else if ("p".equals(a) || "pkg".equals(a) || "--package".equals(a)) { |
| 6996 | if (ai < args.length-1) { |
| 6997 | ai++; |
| 6998 | filter.pkgFilter = args[ai].trim().toLowerCase(); |
| 6999 | if (filter.pkgFilter.isEmpty()) { |
| 7000 | filter.pkgFilter = null; |
| 7001 | } else { |
| 7002 | filter.filtered = true; |
| 7003 | } |
| 7004 | } |
| 7005 | } else if ("--zen".equals(a) || "zen".equals(a)) { |
| 7006 | filter.filtered = true; |
| 7007 | filter.zen = true; |
| 7008 | } else if ("--stats".equals(a)) { |
| 7009 | filter.stats = true; |
| 7010 | if (ai < args.length-1) { |
| 7011 | ai++; |
Tobias Thierer | 28532d0 | 2016-04-21 14:52:10 +0100 | [diff] [blame] | 7012 | filter.since = Long.parseLong(args[ai]); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7013 | } else { |
| 7014 | filter.since = 0; |
| 7015 | } |
Vishnu Nair | e3e4d25 | 2018-03-01 11:26:57 -0800 | [diff] [blame] | 7016 | } else if (PRIORITY_ARG.equals(a)) { |
| 7017 | // Bugreport will call the service twice with priority arguments, first to dump |
| 7018 | // critical sections and then non critical ones. Set approriate filters |
| 7019 | // to generate the desired data. |
| 7020 | if (ai < args.length - 1) { |
| 7021 | ai++; |
| 7022 | switch (args[ai]) { |
| 7023 | case PRIORITY_ARG_CRITICAL: |
| 7024 | filter.criticalPriority = true; |
| 7025 | break; |
| 7026 | case PRIORITY_ARG_NORMAL: |
| 7027 | filter.normalPriority = true; |
| 7028 | break; |
| 7029 | } |
| 7030 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7031 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 7032 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7033 | return filter; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 7034 | } |
| 7035 | |
| 7036 | public boolean matches(StatusBarNotification sbn) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7037 | if (!filtered) return true; |
| 7038 | return zen ? true : sbn != null |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 7039 | && (matches(sbn.getPackageName()) || matches(sbn.getOpPkg())); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 7040 | } |
| 7041 | |
| 7042 | public boolean matches(ComponentName component) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7043 | if (!filtered) return true; |
| 7044 | return zen ? true : component != null && matches(component.getPackageName()); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 7045 | } |
| 7046 | |
| 7047 | public boolean matches(String pkg) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 7048 | if (!filtered) return true; |
| 7049 | return zen ? true : pkg != null && pkg.toLowerCase().contains(pkgFilter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 7050 | } |
| 7051 | |
| 7052 | @Override |
| 7053 | public String toString() { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 7054 | return stats ? "stats" : zen ? "zen" : ('\'' + pkgFilter + '\''); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 7055 | } |
| 7056 | } |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 7057 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7058 | @VisibleForTesting |
| 7059 | protected void simulatePackageSuspendBroadcast(boolean suspend, String pkg) { |
| 7060 | // only use for testing: mimic receive broadcast that package is (un)suspended |
| 7061 | // but does not actually (un)suspend the package |
| 7062 | final Bundle extras = new Bundle(); |
| 7063 | extras.putStringArray(Intent.EXTRA_CHANGED_PACKAGE_LIST, |
| 7064 | new String[]{pkg}); |
| 7065 | |
| 7066 | final String action = suspend ? Intent.ACTION_PACKAGES_SUSPENDED |
| 7067 | : Intent.ACTION_PACKAGES_UNSUSPENDED; |
| 7068 | final Intent intent = new Intent(action); |
| 7069 | intent.putExtras(extras); |
| 7070 | |
| 7071 | mPackageIntentReceiver.onReceive(getContext(), intent); |
| 7072 | } |
| 7073 | |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 7074 | /** |
| 7075 | * Wrapper for a StatusBarNotification object that allows transfer across a oneway |
| 7076 | * binder without sending large amounts of data over a oneway transaction. |
| 7077 | */ |
| 7078 | private static final class StatusBarNotificationHolder |
| 7079 | extends IStatusBarNotificationHolder.Stub { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 7080 | private StatusBarNotification mValue; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 7081 | |
| 7082 | public StatusBarNotificationHolder(StatusBarNotification value) { |
| 7083 | mValue = value; |
| 7084 | } |
| 7085 | |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 7086 | /** 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] | 7087 | @Override |
| 7088 | public StatusBarNotification get() { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 7089 | StatusBarNotification value = mValue; |
| 7090 | mValue = null; |
| 7091 | return value; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 7092 | } |
| 7093 | } |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7094 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7095 | private class ShellCmd extends ShellCommand { |
| 7096 | public static final String USAGE = "help\n" |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 7097 | + "allow_listener COMPONENT [user_id]\n" |
| 7098 | + "disallow_listener COMPONENT [user_id]\n" |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 7099 | + "allow_assistant COMPONENT\n" |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 7100 | + "remove_assistant COMPONENT\n" |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7101 | + "allow_dnd PACKAGE\n" |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7102 | + "disallow_dnd PACKAGE\n" |
| 7103 | + "suspend_package PACKAGE\n" |
| 7104 | + "unsuspend_package PACKAGE"; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7105 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7106 | @Override |
| 7107 | public int onCommand(String cmd) { |
Felipe Leme | 68d8041 | 2017-07-14 11:18:08 -0700 | [diff] [blame] | 7108 | if (cmd == null) { |
| 7109 | return handleDefaultCommands(cmd); |
| 7110 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 7111 | final PrintWriter pw = getOutPrintWriter(); |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 7112 | try { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7113 | switch (cmd) { |
| 7114 | case "allow_dnd": { |
| 7115 | getBinderService().setNotificationPolicyAccessGranted( |
| 7116 | getNextArgRequired(), true); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7117 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7118 | break; |
| 7119 | |
| 7120 | case "disallow_dnd": { |
| 7121 | getBinderService().setNotificationPolicyAccessGranted( |
| 7122 | getNextArgRequired(), false); |
| 7123 | } |
| 7124 | break; |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 7125 | case "allow_listener": { |
| 7126 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 7127 | if (cn == null) { |
| 7128 | pw.println("Invalid listener - must be a ComponentName"); |
| 7129 | return -1; |
| 7130 | } |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 7131 | String userId = getNextArg(); |
| 7132 | if (userId == null) { |
| 7133 | getBinderService().setNotificationListenerAccessGranted(cn, true); |
| 7134 | } else { |
| 7135 | getBinderService().setNotificationListenerAccessGrantedForUser( |
| 7136 | cn, Integer.parseInt(userId), true); |
| 7137 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 7138 | } |
| 7139 | break; |
| 7140 | case "disallow_listener": { |
| 7141 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 7142 | if (cn == null) { |
| 7143 | pw.println("Invalid listener - must be a ComponentName"); |
| 7144 | return -1; |
| 7145 | } |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 7146 | String userId = getNextArg(); |
| 7147 | if (userId == null) { |
| 7148 | getBinderService().setNotificationListenerAccessGranted(cn, false); |
| 7149 | } else { |
| 7150 | getBinderService().setNotificationListenerAccessGrantedForUser( |
| 7151 | cn, Integer.parseInt(userId), false); |
| 7152 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 7153 | } |
| 7154 | break; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 7155 | case "allow_assistant": { |
| 7156 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 7157 | if (cn == null) { |
| 7158 | pw.println("Invalid assistant - must be a ComponentName"); |
| 7159 | return -1; |
| 7160 | } |
| 7161 | getBinderService().setNotificationAssistantAccessGranted(cn, true); |
| 7162 | } |
| 7163 | break; |
| 7164 | case "disallow_assistant": { |
| 7165 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 7166 | if (cn == null) { |
| 7167 | pw.println("Invalid assistant - must be a ComponentName"); |
| 7168 | return -1; |
| 7169 | } |
| 7170 | getBinderService().setNotificationAssistantAccessGranted(cn, false); |
| 7171 | } |
| 7172 | break; |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 7173 | case "suspend_package": { |
| 7174 | // only use for testing |
| 7175 | simulatePackageSuspendBroadcast(true, getNextArgRequired()); |
| 7176 | } |
| 7177 | break; |
| 7178 | case "unsuspend_package": { |
| 7179 | // only use for testing |
| 7180 | simulatePackageSuspendBroadcast(false, getNextArgRequired()); |
| 7181 | } |
| 7182 | break; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7183 | default: |
| 7184 | return handleDefaultCommands(cmd); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7185 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 7186 | } catch (Exception e) { |
| 7187 | pw.println("Error occurred. Check logcat for details. " + e.getMessage()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7188 | Slog.e(TAG, "Error running shell command", e); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7189 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7190 | return 0; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7191 | } |
| 7192 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 7193 | @Override |
| 7194 | public void onHelp() { |
| 7195 | getOutPrintWriter().println(USAGE); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 7196 | } |
| 7197 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7198 | } |