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 | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 19 | import static android.app.NotificationManager.ACTION_APP_BLOCK_STATE_CHANGED; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 20 | import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED; |
| 21 | import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 22 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 23 | import static android.app.NotificationManager.IMPORTANCE_MIN; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 24 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 25 | import static android.content.pm.PackageManager.FEATURE_LEANBACK; |
| 26 | import static android.content.pm.PackageManager.FEATURE_TELEVISION; |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 27 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 28 | import static android.os.UserHandle.USER_ALL; |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 29 | import static android.os.UserHandle.USER_NULL; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 30 | import static android.os.UserHandle.USER_SYSTEM; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 31 | import static android.service.notification.NotificationListenerService |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 32 | .HINT_HOST_DISABLE_CALL_EFFECTS; |
| 33 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_EFFECTS; |
| 34 | import static android.service.notification.NotificationListenerService |
| 35 | .HINT_HOST_DISABLE_NOTIFICATION_EFFECTS; |
| 36 | import static android.service.notification.NotificationListenerService |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 37 | .NOTIFICATION_CHANNEL_OR_GROUP_ADDED; |
| 38 | import static android.service.notification.NotificationListenerService |
| 39 | .NOTIFICATION_CHANNEL_OR_GROUP_DELETED; |
| 40 | import static android.service.notification.NotificationListenerService |
| 41 | .NOTIFICATION_CHANNEL_OR_GROUP_UPDATED; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 42 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL; |
| 43 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL; |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 44 | import static android.service.notification.NotificationListenerService.REASON_CANCEL; |
| 45 | import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 46 | import static android.service.notification.NotificationListenerService.REASON_CHANNEL_BANNED; |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 47 | import static android.service.notification.NotificationListenerService.REASON_CLICK; |
| 48 | import static android.service.notification.NotificationListenerService.REASON_ERROR; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 49 | import static android.service.notification.NotificationListenerService |
| 50 | .REASON_GROUP_SUMMARY_CANCELED; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 51 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL; |
| 52 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL_ALL; |
| 53 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_BANNED; |
| 54 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_CHANGED; |
| 55 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_SUSPENDED; |
| 56 | import static android.service.notification.NotificationListenerService.REASON_PROFILE_TURNED_OFF; |
| 57 | import static android.service.notification.NotificationListenerService.REASON_SNOOZED; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 58 | import static android.service.notification.NotificationListenerService.REASON_TIMEOUT; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 59 | import static android.service.notification.NotificationListenerService.REASON_UNAUTOBUNDLED; |
| 60 | import static android.service.notification.NotificationListenerService.REASON_USER_STOPPED; |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 61 | import static android.service.notification.NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF; |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 62 | import static android.service.notification.NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_ON; |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 63 | import static android.service.notification.NotificationListenerService.TRIM_FULL; |
| 64 | import static android.service.notification.NotificationListenerService.TRIM_LIGHT; |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 65 | import static android.view.Display.DEFAULT_DISPLAY; |
| 66 | import static android.view.WindowManager.LayoutParams.TYPE_TOAST; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 67 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 68 | import android.Manifest; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 69 | import android.annotation.NonNull; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 70 | import android.annotation.Nullable; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 71 | import android.app.ActivityManager; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 72 | import android.app.ActivityManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 73 | import android.app.AlarmManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 74 | import android.app.AppGlobals; |
Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 75 | import android.app.AppOpsManager; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 76 | import android.app.AutomaticZenRule; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | import android.app.IActivityManager; |
| 78 | import android.app.INotificationManager; |
| 79 | import android.app.ITransientNotification; |
| 80 | import android.app.Notification; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 81 | import android.app.NotificationChannel; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 82 | import android.app.NotificationChannelGroup; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 83 | import android.app.NotificationManager; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 84 | import android.app.NotificationManager.Policy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | import android.app.PendingIntent; |
| 86 | import android.app.StatusBarManager; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 87 | import android.app.backup.BackupManager; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 88 | import android.app.usage.UsageEvents; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 89 | import android.app.usage.UsageStatsManagerInternal; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 90 | import android.companion.ICompanionDeviceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | import android.content.BroadcastReceiver; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 92 | import android.content.ComponentName; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 93 | import android.content.ContentProvider; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 94 | import android.content.ContentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.content.Context; |
| 96 | import android.content.Intent; |
| 97 | import android.content.IntentFilter; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 98 | import android.content.pm.ApplicationInfo; |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 99 | import android.content.pm.IPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import android.content.pm.PackageManager; |
| 101 | import android.content.pm.PackageManager.NameNotFoundException; |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 102 | import android.content.pm.ParceledListSlice; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | import android.content.res.Resources; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 104 | import android.database.ContentObserver; |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 105 | import android.media.AudioAttributes; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 106 | import android.media.AudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 107 | import android.media.AudioManagerInternal; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 108 | import android.media.IRingtonePlayer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import android.os.Binder; |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 111 | import android.os.Build; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 112 | import android.os.Bundle; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 113 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | import android.os.Handler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 115 | import android.os.HandlerThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | import android.os.IBinder; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 117 | import android.os.IDeviceIdleController; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 118 | import android.os.IInterface; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 119 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | import android.os.Message; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 121 | import android.os.Parcelable; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 122 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 123 | import android.os.RemoteException; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 124 | import android.os.ResultReceiver; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 125 | import android.os.ServiceManager; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 126 | import android.os.ShellCallback; |
| 127 | import android.os.ShellCommand; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 128 | import android.os.SystemClock; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 129 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 130 | import android.os.UserHandle; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 131 | import android.os.VibrationEffect; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 132 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | import android.provider.Settings; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 134 | import android.service.notification.Adjustment; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 135 | import android.service.notification.Condition; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 136 | import android.service.notification.IConditionProvider; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 137 | import android.service.notification.INotificationListener; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 138 | import android.service.notification.IStatusBarNotificationHolder; |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 139 | import android.service.notification.ListenersDisablingEffectsProto; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 140 | import android.service.notification.NotificationAssistantService; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 141 | import android.service.notification.NotificationListenerService; |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 142 | import android.service.notification.NotificationRankingUpdate; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 143 | import android.service.notification.NotificationRecordProto; |
| 144 | import android.service.notification.NotificationServiceDumpProto; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 145 | import android.service.notification.NotificationStats; |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 146 | import android.service.notification.NotifyingApp; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 147 | import android.service.notification.SnoozeCriterion; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 148 | import android.service.notification.StatusBarNotification; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 149 | import android.service.notification.ZenModeConfig; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 150 | import android.service.notification.ZenModeProto; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 151 | import android.telephony.PhoneStateListener; |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 152 | import android.telephony.TelephonyManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 153 | import android.text.TextUtils; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 154 | import android.util.ArrayMap; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 155 | import android.util.ArraySet; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 156 | import android.util.AtomicFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | import android.util.Log; |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 158 | import android.util.Slog; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 159 | import android.util.SparseArray; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 160 | import android.util.Xml; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 161 | import android.util.proto.ProtoOutputStream; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 162 | import android.view.accessibility.AccessibilityEvent; |
| 163 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | import android.widget.Toast; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 165 | |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 166 | import com.android.internal.R; |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 167 | import com.android.internal.annotations.GuardedBy; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 168 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 169 | import com.android.internal.logging.MetricsLogger; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 170 | import com.android.internal.logging.nano.MetricsProto; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 171 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 172 | import com.android.internal.os.BackgroundThread; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 173 | import com.android.internal.statusbar.NotificationVisibility; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 174 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 175 | import com.android.internal.util.DumpUtils; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 176 | import com.android.internal.util.FastXmlSerializer; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 177 | import com.android.internal.util.Preconditions; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 178 | import com.android.internal.util.XmlUtils; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 179 | import com.android.server.DeviceIdleController; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 180 | import com.android.server.EventLogTags; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 181 | import com.android.server.LocalServices; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 182 | import com.android.server.SystemService; |
| 183 | import com.android.server.lights.Light; |
| 184 | import com.android.server.lights.LightsManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 185 | import com.android.server.notification.ManagedServices.ManagedServiceInfo; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 186 | import com.android.server.notification.ManagedServices.UserProfiles; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 187 | import com.android.server.policy.PhoneWindowManager; |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 188 | import com.android.server.statusbar.StatusBarManagerInternal; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 189 | import com.android.server.wm.WindowManagerInternal; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 190 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 191 | import libcore.io.IoUtils; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 192 | |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 193 | import org.json.JSONException; |
| 194 | import org.json.JSONObject; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 195 | import org.xmlpull.v1.XmlPullParser; |
| 196 | import org.xmlpull.v1.XmlPullParserException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 197 | import org.xmlpull.v1.XmlSerializer; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 198 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 199 | import java.io.ByteArrayInputStream; |
| 200 | import java.io.ByteArrayOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 201 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | import java.io.FileDescriptor; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 203 | import java.io.FileNotFoundException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 204 | import java.io.FileOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 205 | import java.io.IOException; |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 206 | import java.io.InputStream; |
| 207 | import java.io.OutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 209 | import java.nio.charset.StandardCharsets; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 210 | import java.util.ArrayDeque; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | import java.util.ArrayList; |
Michael Wright | bc4d0d9 | 2017-03-23 18:57:57 +0000 | [diff] [blame] | 212 | import java.util.Arrays; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 213 | import java.util.HashSet; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 214 | import java.util.Iterator; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 215 | import java.util.List; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 216 | import java.util.Map.Entry; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 217 | import java.util.Objects; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 218 | import java.util.Set; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 219 | import java.util.concurrent.TimeUnit; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 220 | |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 221 | /** {@hide} */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 222 | public class NotificationManagerService extends SystemService { |
| 223 | static final String TAG = "NotificationService"; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 224 | static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Selim Cinek | 4041249 | 2015-12-08 18:03:22 -0800 | [diff] [blame] | 225 | public static final boolean ENABLE_CHILD_NOTIFICATIONS |
| 226 | = SystemProperties.getBoolean("debug.child_notifs", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 228 | static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 229 | static final float DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE = 5f; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 230 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | // message codes |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 232 | static final int MESSAGE_TIMEOUT = 2; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 233 | static final int MESSAGE_SAVE_POLICY_FILE = 3; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 234 | static final int MESSAGE_SEND_RANKING_UPDATE = 4; |
| 235 | static final int MESSAGE_LISTENER_HINTS_CHANGED = 5; |
| 236 | static final int MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED = 6; |
| 237 | |
| 238 | // ranking thread messages |
| 239 | private static final int MESSAGE_RECONSIDER_RANKING = 1000; |
| 240 | private static final int MESSAGE_RANKING_SORT = 1001; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 242 | static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 243 | static final int SHORT_DELAY = 2000; // 2 seconds |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 244 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 245 | static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 246 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 247 | static final long SNOOZE_UNTIL_UNSPECIFIED = -1; |
| 248 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 249 | 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] | 250 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 251 | static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION; |
Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 252 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 253 | static final boolean ENABLE_BLOCKED_TOASTS = true; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 254 | |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 255 | // When #matchesCallFilter is called from the ringer, wait at most |
| 256 | // 3s to resolve the contacts. This timeout is required since |
| 257 | // ContactsProvider might take a long time to start up. |
| 258 | // |
| 259 | // Return STARRED_CONTACT when the timeout is hit in order to avoid |
| 260 | // missed calls in ZEN mode "Important". |
| 261 | static final int MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS = 3000; |
| 262 | static final float MATCHES_CALL_FILTER_TIMEOUT_AFFINITY = |
| 263 | ValidateNotificationPeople.STARRED_CONTACT; |
| 264 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 265 | /** notification_enqueue status value for a newly enqueued notification. */ |
| 266 | private static final int EVENTLOG_ENQUEUE_STATUS_NEW = 0; |
| 267 | |
| 268 | /** notification_enqueue status value for an existing notification. */ |
| 269 | private static final int EVENTLOG_ENQUEUE_STATUS_UPDATE = 1; |
| 270 | |
| 271 | /** notification_enqueue status value for an ignored notification. */ |
| 272 | private static final int EVENTLOG_ENQUEUE_STATUS_IGNORED = 2; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 273 | private static final long MIN_PACKAGE_OVERRATE_LOG_INTERVAL = 5000; // milliseconds |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 274 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 275 | private static final long DELAY_FOR_ASSISTANT_TIME = 100; |
| 276 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 277 | private static final String ACTION_NOTIFICATION_TIMEOUT = |
| 278 | NotificationManagerService.class.getSimpleName() + ".TIMEOUT"; |
| 279 | private static final int REQUEST_CODE_TIMEOUT = 1; |
| 280 | private static final String SCHEME_TIMEOUT = "timeout"; |
| 281 | private static final String EXTRA_KEY = "key"; |
| 282 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 283 | private IActivityManager mAm; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 284 | private ActivityManager mActivityManager; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 285 | private IPackageManager mPackageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 286 | private PackageManager mPackageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 287 | AudioManager mAudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 288 | AudioManagerInternal mAudioManagerInternal; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 289 | @Nullable StatusBarManagerInternal mStatusBar; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 290 | Vibrator mVibrator; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 291 | private WindowManagerInternal mWindowManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 292 | private AlarmManager mAlarmManager; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 293 | private ICompanionDeviceManager mCompanionManager; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 294 | private AccessibilityManager mAccessibilityManager; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 295 | private IDeviceIdleController mDeviceIdleController; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 296 | private IBinder mPermissionOwner; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 297 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | final IBinder mForegroundToken = new Binder(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 299 | private WorkerHandler mHandler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 300 | private final HandlerThread mRankingThread = new HandlerThread("ranker", |
| 301 | Process.THREAD_PRIORITY_BACKGROUND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 303 | private Light mNotificationLight; |
| 304 | Light mAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 305 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 306 | private long[] mFallbackVibrationPattern; |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 307 | private boolean mUseAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 308 | boolean mSystemReady; |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 309 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 310 | private boolean mDisableNotificationEffects; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 311 | private int mCallState; |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 312 | private String mSoundNotificationKey; |
| 313 | private String mVibrateNotificationKey; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 315 | private final SparseArray<ArraySet<ManagedServiceInfo>> mListenersDisablingEffects = |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 316 | new SparseArray<>(); |
| 317 | private List<ComponentName> mEffectsSuppressors = new ArrayList<>(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 318 | private int mListenerHints; // right now, all hints are global |
John Spurlock | 8310410 | 2015-02-12 23:25:12 -0500 | [diff] [blame] | 319 | private int mInterruptionFilter = NotificationListenerService.INTERRUPTION_FILTER_UNKNOWN; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 320 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 321 | // for enabling and disabling notification pulse behavior |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 322 | private boolean mScreenOn = true; |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 323 | protected boolean mInCall = false; |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 324 | private boolean mNotificationPulseEnabled; |
| 325 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 326 | private Uri mInCallNotificationUri; |
| 327 | private AudioAttributes mInCallNotificationAudioAttributes; |
| 328 | private float mInCallNotificationVolume; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 329 | |
Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 330 | // used as a mutex for access to all active notifications & listeners |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 331 | final Object mNotificationLock = new Object(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 332 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 333 | final ArrayList<NotificationRecord> mNotificationList = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 334 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 335 | final ArrayMap<String, NotificationRecord> mNotificationsByKey = new ArrayMap<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 336 | @GuardedBy("mNotificationLock") |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 337 | final ArrayList<NotificationRecord> mEnqueuedNotifications = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 338 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 339 | final ArrayMap<Integer, ArrayMap<String, String>> mAutobundledSummaries = new ArrayMap<>(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 340 | final ArrayList<ToastRecord> mToastQueue = new ArrayList<>(); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 341 | final ArrayMap<String, NotificationRecord> mSummaryByGroupKey = new ArrayMap<>(); |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 342 | final ArrayMap<Integer, ArrayList<NotifyingApp>> mRecentApps = new ArrayMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 344 | // The last key in this list owns the hardware. |
| 345 | ArrayList<String> mLights = new ArrayList<>(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 346 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 347 | private AppOpsManager mAppOps; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 348 | private UsageStatsManagerInternal mAppUsageStats; |
Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 349 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 350 | private Archive mArchive; |
| 351 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 352 | // Persistent storage for notification policy |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 353 | private AtomicFile mPolicyFile; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 354 | |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 355 | private static final int DB_VERSION = 1; |
| 356 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 357 | private static final String TAG_NOTIFICATION_POLICY = "notification-policy"; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 358 | private static final String ATTR_VERSION = "version"; |
| 359 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 360 | private RankingHelper mRankingHelper; |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 361 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 362 | private final UserProfiles mUserProfiles = new UserProfiles(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 363 | private NotificationListeners mListeners; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 364 | private NotificationAssistants mAssistants; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 365 | private ConditionProviders mConditionProviders; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 366 | private NotificationUsageStats mUsageStats; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 367 | |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 368 | private static final int MY_UID = Process.myUid(); |
| 369 | private static final int MY_PID = Process.myPid(); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 370 | private static final IBinder WHITELIST_TOKEN = new Binder(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 371 | private RankingHandler mRankingHandler; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 372 | private long mLastOverRateLogTime; |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 373 | private float mMaxPackageEnqueueRate = DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE; |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 374 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 375 | private SnoozeHelper mSnoozeHelper; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 376 | private GroupHelper mGroupHelper; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 377 | private boolean mIsTelevision; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 378 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 379 | private static class Archive { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 380 | final int mBufferSize; |
| 381 | final ArrayDeque<StatusBarNotification> mBuffer; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 382 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 383 | public Archive(int size) { |
| 384 | mBufferSize = size; |
| 385 | mBuffer = new ArrayDeque<StatusBarNotification>(mBufferSize); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 386 | } |
Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 387 | |
Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 388 | public String toString() { |
| 389 | final StringBuilder sb = new StringBuilder(); |
| 390 | final int N = mBuffer.size(); |
| 391 | sb.append("Archive ("); |
| 392 | sb.append(N); |
| 393 | sb.append(" notification"); |
| 394 | sb.append((N==1)?")":"s)"); |
| 395 | return sb.toString(); |
| 396 | } |
| 397 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 398 | public void record(StatusBarNotification nr) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 399 | if (mBuffer.size() == mBufferSize) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 400 | mBuffer.removeFirst(); |
| 401 | } |
Daniel Sandler | 26b81d5 | 2013-05-20 20:56:43 -0400 | [diff] [blame] | 402 | |
| 403 | // We don't want to store the heavy bits of the notification in the archive, |
| 404 | // but other clients in the system process might be using the object, so we |
| 405 | // store a (lightened) copy. |
| 406 | mBuffer.addLast(nr.cloneLight()); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 407 | } |
| 408 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 409 | public Iterator<StatusBarNotification> descendingIterator() { |
| 410 | return mBuffer.descendingIterator(); |
| 411 | } |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 412 | |
| 413 | public StatusBarNotification[] getArray(int count) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 414 | if (count == 0) count = mBufferSize; |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 415 | final StatusBarNotification[] a |
| 416 | = new StatusBarNotification[Math.min(count, mBuffer.size())]; |
| 417 | Iterator<StatusBarNotification> iter = descendingIterator(); |
| 418 | int i=0; |
| 419 | while (iter.hasNext() && i < count) { |
| 420 | a[i++] = iter.next(); |
| 421 | } |
| 422 | return a; |
| 423 | } |
| 424 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 425 | } |
| 426 | |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 427 | protected void readDefaultApprovedServices(int userId) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 428 | String defaultListenerAccess = getContext().getResources().getString( |
| 429 | com.android.internal.R.string.config_defaultListenerAccessPackages); |
| 430 | if (defaultListenerAccess != null) { |
| 431 | for (String whitelisted : |
| 432 | defaultListenerAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR)) { |
| 433 | // Gather all notification listener components for candidate pkgs. |
| 434 | Set<ComponentName> approvedListeners = |
| 435 | mListeners.queryPackageForServices(whitelisted, |
| 436 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 437 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId); |
| 438 | for (ComponentName cn : approvedListeners) { |
| 439 | try { |
| 440 | getBinderService().setNotificationListenerAccessGrantedForUser(cn, |
| 441 | userId, true); |
| 442 | } catch (RemoteException e) { |
| 443 | e.printStackTrace(); |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 448 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 449 | String defaultDndAccess = getContext().getResources().getString( |
| 450 | com.android.internal.R.string.config_defaultDndAccessPackages); |
| 451 | if (defaultListenerAccess != null) { |
| 452 | for (String whitelisted : |
| 453 | defaultDndAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR)) { |
| 454 | try { |
| 455 | getBinderService().setNotificationPolicyAccessGranted(whitelisted, true); |
| 456 | } catch (RemoteException e) { |
| 457 | e.printStackTrace(); |
| 458 | } |
| 459 | } |
| 460 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 461 | |
| 462 | readDefaultAssistant(userId); |
| 463 | } |
| 464 | |
| 465 | protected void readDefaultAssistant(int userId) { |
| 466 | String defaultAssistantAccess = getContext().getResources().getString( |
| 467 | com.android.internal.R.string.config_defaultAssistantAccessPackage); |
| 468 | if (defaultAssistantAccess != null) { |
| 469 | // Gather all notification assistant components for candidate pkg. There should |
| 470 | // only be one |
| 471 | Set<ComponentName> approvedAssistants = |
| 472 | mAssistants.queryPackageForServices(defaultAssistantAccess, |
| 473 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 474 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId); |
| 475 | for (ComponentName cn : approvedAssistants) { |
| 476 | try { |
| 477 | getBinderService().setNotificationAssistantAccessGrantedForUser(cn, |
| 478 | userId, true); |
| 479 | } catch (RemoteException e) { |
| 480 | e.printStackTrace(); |
| 481 | } |
| 482 | } |
| 483 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 484 | } |
| 485 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 486 | void readPolicyXml(InputStream stream, boolean forRestore) |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 487 | throws XmlPullParserException, NumberFormatException, IOException { |
| 488 | final XmlPullParser parser = Xml.newPullParser(); |
| 489 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 490 | XmlUtils.beginDocument(parser, TAG_NOTIFICATION_POLICY); |
| 491 | boolean migratedManagedServices = false; |
| 492 | int outerDepth = parser.getDepth(); |
| 493 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 494 | if (ZenModeConfig.ZEN_TAG.equals(parser.getName())) { |
| 495 | mZenModeHelper.readXml(parser, forRestore); |
| 496 | } else if (RankingHelper.TAG_RANKING.equals(parser.getName())){ |
| 497 | mRankingHelper.readXml(parser, forRestore); |
| 498 | } |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 499 | // No non-system managed services are allowed on low ram devices |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 500 | if (canUseManagedServices()) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 501 | if (mListeners.getConfig().xmlTag.equals(parser.getName())) { |
| 502 | mListeners.readXml(parser); |
| 503 | migratedManagedServices = true; |
| 504 | } else if (mAssistants.getConfig().xmlTag.equals(parser.getName())) { |
| 505 | mAssistants.readXml(parser); |
| 506 | migratedManagedServices = true; |
| 507 | } else if (mConditionProviders.getConfig().xmlTag.equals(parser.getName())) { |
| 508 | mConditionProviders.readXml(parser); |
| 509 | migratedManagedServices = true; |
| 510 | } |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 511 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 512 | } |
| 513 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 514 | if (!migratedManagedServices) { |
| 515 | mListeners.migrateToXml(); |
| 516 | mAssistants.migrateToXml(); |
| 517 | mConditionProviders.migrateToXml(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 518 | savePolicyFile(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 522 | private void loadPolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 523 | if (DBG) Slog.d(TAG, "loadPolicyFile"); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 524 | synchronized (mPolicyFile) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 525 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 526 | InputStream infile = null; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 527 | try { |
| 528 | infile = mPolicyFile.openRead(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 529 | readPolicyXml(infile, false /*forRestore*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 530 | } catch (FileNotFoundException e) { |
| 531 | // No data yet |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 532 | // Load default managed services approvals |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 533 | readDefaultApprovedServices(USER_SYSTEM); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 534 | } catch (IOException e) { |
| 535 | Log.wtf(TAG, "Unable to read notification policy", e); |
| 536 | } catch (NumberFormatException e) { |
| 537 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 538 | } catch (XmlPullParserException e) { |
| 539 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 540 | } finally { |
| 541 | IoUtils.closeQuietly(infile); |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | public void savePolicyFile() { |
| 547 | mHandler.removeMessages(MESSAGE_SAVE_POLICY_FILE); |
| 548 | mHandler.sendEmptyMessage(MESSAGE_SAVE_POLICY_FILE); |
| 549 | } |
| 550 | |
| 551 | private void handleSavePolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 552 | if (DBG) Slog.d(TAG, "handleSavePolicyFile"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 553 | synchronized (mPolicyFile) { |
| 554 | final FileOutputStream stream; |
| 555 | try { |
| 556 | stream = mPolicyFile.startWrite(); |
| 557 | } catch (IOException e) { |
| 558 | Slog.w(TAG, "Failed to save policy file", e); |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | try { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 563 | writePolicyXml(stream, false /*forBackup*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 564 | mPolicyFile.finishWrite(stream); |
| 565 | } catch (IOException e) { |
| 566 | Slog.w(TAG, "Failed to save policy file, restoring backup", e); |
| 567 | mPolicyFile.failWrite(stream); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 568 | } |
| 569 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 570 | BackupManager.dataChanged(getContext().getPackageName()); |
| 571 | } |
| 572 | |
| 573 | private void writePolicyXml(OutputStream stream, boolean forBackup) throws IOException { |
| 574 | final XmlSerializer out = new FastXmlSerializer(); |
| 575 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
| 576 | out.startDocument(null, true); |
| 577 | out.startTag(null, TAG_NOTIFICATION_POLICY); |
| 578 | out.attribute(null, ATTR_VERSION, Integer.toString(DB_VERSION)); |
| 579 | mZenModeHelper.writeXml(out, forBackup); |
| 580 | mRankingHelper.writeXml(out, forBackup); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 581 | mListeners.writeXml(out, forBackup); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 582 | mAssistants.writeXml(out, forBackup); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 583 | mConditionProviders.writeXml(out, forBackup); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 584 | out.endTag(null, TAG_NOTIFICATION_POLICY); |
| 585 | out.endDocument(); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 586 | } |
| 587 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 588 | private static final class ToastRecord |
| 589 | { |
| 590 | final int pid; |
| 591 | final String pkg; |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 592 | ITransientNotification callback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | int duration; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 594 | Binder token; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 596 | ToastRecord(int pid, String pkg, ITransientNotification callback, int duration, |
| 597 | Binder token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | this.pid = pid; |
| 599 | this.pkg = pkg; |
| 600 | this.callback = callback; |
| 601 | this.duration = duration; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 602 | this.token = token; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | void update(int duration) { |
| 606 | this.duration = duration; |
| 607 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 608 | |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 609 | void update(ITransientNotification callback) { |
| 610 | this.callback = callback; |
| 611 | } |
| 612 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 613 | void dump(PrintWriter pw, String prefix, DumpFilter filter) { |
| 614 | if (filter != null && !filter.matches(pkg)) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | pw.println(prefix + this); |
| 616 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 617 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | @Override |
| 619 | public final String toString() |
| 620 | { |
| 621 | return "ToastRecord{" |
| 622 | + Integer.toHexString(System.identityHashCode(this)) |
| 623 | + " pkg=" + pkg |
| 624 | + " callback=" + callback |
| 625 | + " duration=" + duration; |
| 626 | } |
| 627 | } |
| 628 | |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 629 | @VisibleForTesting |
| 630 | final NotificationDelegate mNotificationDelegate = new NotificationDelegate() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 632 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | public void onSetDisabled(int status) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 634 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 635 | mDisableNotificationEffects = |
| 636 | (status & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 637 | if (disableNotificationEffects(null) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | // cancel whatever's going on |
| 639 | long identity = Binder.clearCallingIdentity(); |
| 640 | try { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 641 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 642 | if (player != null) { |
| 643 | player.stopAsync(); |
| 644 | } |
| 645 | } catch (RemoteException e) { |
| 646 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 647 | Binder.restoreCallingIdentity(identity); |
| 648 | } |
| 649 | |
| 650 | identity = Binder.clearCallingIdentity(); |
| 651 | try { |
| 652 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 653 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | Binder.restoreCallingIdentity(identity); |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 660 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 661 | public void onClearAll(int callingUid, int callingPid, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 662 | synchronized (mNotificationLock) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 663 | cancelAllLocked(callingUid, callingPid, userId, REASON_CANCEL_ALL, null, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 664 | /*includeCurrentProfiles*/ true); |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 665 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 666 | } |
| 667 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 668 | @Override |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 669 | public void onNotificationClick(int callingUid, int callingPid, String key) { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 670 | exitIdle(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 671 | synchronized (mNotificationLock) { |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 672 | NotificationRecord r = mNotificationsByKey.get(key); |
| 673 | if (r == null) { |
| 674 | Log.w(TAG, "No notification with key: " + key); |
| 675 | return; |
| 676 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 677 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 678 | MetricsLogger.action(r.getLogMaker(now) |
| 679 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
| 680 | .setType(MetricsEvent.TYPE_ACTION)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 681 | EventLogTags.writeNotificationClicked(key, |
| 682 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
| 683 | |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 684 | StatusBarNotification sbn = r.sbn; |
| 685 | cancelNotification(callingUid, callingPid, sbn.getPackageName(), sbn.getTag(), |
| 686 | sbn.getId(), Notification.FLAG_AUTO_CANCEL, |
| 687 | Notification.FLAG_FOREGROUND_SERVICE, false, r.getUserId(), |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 688 | REASON_CLICK, null); |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 689 | reportUserInteraction(r); |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 690 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | } |
| 692 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 693 | @Override |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 694 | public void onNotificationActionClick(int callingUid, int callingPid, String key, |
| 695 | int actionIndex) { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 696 | exitIdle(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 697 | synchronized (mNotificationLock) { |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 698 | NotificationRecord r = mNotificationsByKey.get(key); |
| 699 | if (r == null) { |
| 700 | Log.w(TAG, "No notification with key: " + key); |
| 701 | return; |
| 702 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 703 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 704 | MetricsLogger.action(r.getLogMaker(now) |
| 705 | .setCategory(MetricsEvent.NOTIFICATION_ITEM_ACTION) |
| 706 | .setType(MetricsEvent.TYPE_ACTION) |
| 707 | .setSubtype(actionIndex)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 708 | EventLogTags.writeNotificationActionClicked(key, actionIndex, |
| 709 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 710 | reportUserInteraction(r); |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
| 714 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 715 | public void onNotificationClear(int callingUid, int callingPid, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 716 | String pkg, String tag, int id, int userId, String key, |
| 717 | @NotificationStats.DismissalSurface int dismissalSurface) { |
| 718 | synchronized (mNotificationLock) { |
| 719 | NotificationRecord r = mNotificationsByKey.get(key); |
| 720 | if (r != null) { |
| 721 | r.recordDismissalSurface(dismissalSurface); |
| 722 | } |
| 723 | } |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 724 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
Kenny Guy | 3a7c4a56 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 725 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 726 | true, userId, REASON_CANCEL, null); |
Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 727 | } |
| 728 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 729 | @Override |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 730 | public void onPanelRevealed(boolean clearEffects, int items) { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 731 | MetricsLogger.visible(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Chris Wren | 621933f | 2017-06-14 15:59:03 -0400 | [diff] [blame] | 732 | MetricsLogger.histogram(getContext(), "note_load", items); |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 733 | EventLogTags.writeNotificationPanelRevealed(items); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 734 | if (clearEffects) { |
| 735 | clearEffects(); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | @Override |
| 740 | public void onPanelHidden() { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 741 | MetricsLogger.hidden(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 742 | EventLogTags.writeNotificationPanelHidden(); |
| 743 | } |
| 744 | |
| 745 | @Override |
| 746 | public void clearEffects() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 747 | synchronized (mNotificationLock) { |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 748 | if (DBG) Slog.d(TAG, "clearEffects"); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 749 | clearSoundLocked(); |
| 750 | clearVibrateLocked(); |
| 751 | clearLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 752 | } |
| 753 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 754 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 755 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 756 | public void onNotificationError(int callingUid, int callingPid, String pkg, String tag, int id, |
Kenny Guy | 3a7c4a56 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 757 | int uid, int initialPid, String message, int userId) { |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 758 | Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id |
| 759 | + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")"); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 760 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, 0, false, userId, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 761 | REASON_ERROR, null); |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 762 | long ident = Binder.clearCallingIdentity(); |
| 763 | try { |
Christopher Tate | 8aa8fe1 | 2017-01-20 17:50:32 -0800 | [diff] [blame] | 764 | ActivityManager.getService().crashApplication(uid, initialPid, pkg, -1, |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 765 | "Bad notification posted from package " + pkg |
| 766 | + ": " + message); |
| 767 | } catch (RemoteException e) { |
| 768 | } |
| 769 | Binder.restoreCallingIdentity(ident); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 770 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 771 | |
| 772 | @Override |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 773 | public void onNotificationVisibilityChanged(NotificationVisibility[] newlyVisibleKeys, |
| 774 | NotificationVisibility[] noLongerVisibleKeys) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 775 | synchronized (mNotificationLock) { |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 776 | for (NotificationVisibility nv : newlyVisibleKeys) { |
| 777 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 778 | if (r == null) continue; |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 779 | if (!r.isSeen()) { |
| 780 | // Report to usage stats that notification was made visible |
| 781 | if (DBG) Slog.d(TAG, "Marking notification as visible " + nv.key); |
| 782 | reportSeen(r); |
| 783 | } |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 784 | r.setVisibility(true, nv.rank); |
| 785 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 786 | } |
| 787 | // Note that we might receive this event after notifications |
| 788 | // have already left the system, e.g. after dismissing from the |
| 789 | // shade. Hence not finding notifications in |
| 790 | // mNotificationsByKey is not an exceptional condition. |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 791 | for (NotificationVisibility nv : noLongerVisibleKeys) { |
| 792 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 793 | if (r == null) continue; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 794 | r.setVisibility(false, nv.rank); |
| 795 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 796 | } |
| 797 | } |
Christoph Studer | 92b389d | 2014-04-01 18:44:40 +0200 | [diff] [blame] | 798 | } |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 799 | |
| 800 | @Override |
| 801 | public void onNotificationExpansionChanged(String key, |
| 802 | boolean userAction, boolean expanded) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 803 | synchronized (mNotificationLock) { |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 804 | NotificationRecord r = mNotificationsByKey.get(key); |
| 805 | if (r != null) { |
| 806 | r.stats.onExpansionChanged(userAction, expanded); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 807 | final long now = System.currentTimeMillis(); |
Chris Wren | f734271 | 2017-09-14 10:55:55 -0400 | [diff] [blame] | 808 | if (userAction) { |
Chris Wren | 377ac6d | 2017-09-12 14:15:23 -0400 | [diff] [blame] | 809 | MetricsLogger.action(r.getLogMaker(now) |
| 810 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
Chris Wren | f734271 | 2017-09-14 10:55:55 -0400 | [diff] [blame] | 811 | .setType(expanded ? MetricsEvent.TYPE_DETAIL |
| 812 | : MetricsEvent.TYPE_COLLAPSE)); |
Chris Wren | 377ac6d | 2017-09-12 14:15:23 -0400 | [diff] [blame] | 813 | } |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 814 | if (expanded && userAction) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 815 | r.recordExpanded(); |
| 816 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 817 | EventLogTags.writeNotificationExpansion(key, |
| 818 | userAction ? 1 : 0, expanded ? 1 : 0, |
| 819 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 823 | |
| 824 | @Override |
| 825 | public void onNotificationDirectReplied(String key) { |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 826 | exitIdle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 827 | synchronized (mNotificationLock) { |
| 828 | NotificationRecord r = mNotificationsByKey.get(key); |
| 829 | if (r != null) { |
| 830 | r.recordDirectReplied(); |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 831 | reportUserInteraction(r); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | @Override |
| 837 | public void onNotificationSettingsViewed(String key) { |
| 838 | synchronized (mNotificationLock) { |
| 839 | NotificationRecord r = mNotificationsByKey.get(key); |
| 840 | if (r != null) { |
| 841 | r.recordViewedSettings(); |
| 842 | } |
| 843 | } |
| 844 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 845 | }; |
| 846 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 847 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 848 | private void clearSoundLocked() { |
| 849 | mSoundNotificationKey = null; |
| 850 | long identity = Binder.clearCallingIdentity(); |
| 851 | try { |
| 852 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 853 | if (player != null) { |
| 854 | player.stopAsync(); |
| 855 | } |
| 856 | } catch (RemoteException e) { |
| 857 | } finally { |
| 858 | Binder.restoreCallingIdentity(identity); |
| 859 | } |
| 860 | } |
| 861 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 862 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 863 | private void clearVibrateLocked() { |
| 864 | mVibrateNotificationKey = null; |
| 865 | long identity = Binder.clearCallingIdentity(); |
| 866 | try { |
| 867 | mVibrator.cancel(); |
| 868 | } finally { |
| 869 | Binder.restoreCallingIdentity(identity); |
| 870 | } |
| 871 | } |
| 872 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 873 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 874 | private void clearLightsLocked() { |
| 875 | // light |
| 876 | mLights.clear(); |
| 877 | updateLightsLocked(); |
| 878 | } |
| 879 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 880 | protected final BroadcastReceiver mLocaleChangeReceiver = new BroadcastReceiver() { |
| 881 | @Override |
| 882 | public void onReceive(Context context, Intent intent) { |
| 883 | if (Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) { |
| 884 | mZenModeHelper.updateDefaultZenRules(); |
Julia Reynolds | 816797a | 2017-08-11 15:47:09 -0400 | [diff] [blame] | 885 | mRankingHelper.onLocaleChanged(context, ActivityManager.getCurrentUser()); |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | }; |
| 889 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 890 | private final BroadcastReceiver mRestoreReceiver = new BroadcastReceiver() { |
| 891 | @Override |
| 892 | public void onReceive(Context context, Intent intent) { |
| 893 | if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) { |
| 894 | try { |
| 895 | String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME); |
| 896 | String newValue = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE); |
Michal Karpinski | 6135a26 | 2017-08-11 10:45:58 +0100 | [diff] [blame] | 897 | int restoredFromSdkInt = intent.getIntExtra( |
| 898 | Intent.EXTRA_SETTING_RESTORED_FROM_SDK_INT, 0); |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 899 | mListeners.onSettingRestored( |
| 900 | element, newValue, restoredFromSdkInt, getSendingUserId()); |
| 901 | mConditionProviders.onSettingRestored( |
| 902 | element, newValue, restoredFromSdkInt, getSendingUserId()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 903 | } catch (Exception e) { |
| 904 | Slog.wtf(TAG, "Cannot restore managed services from settings", e); |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | }; |
| 909 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 910 | private final BroadcastReceiver mNotificationTimeoutReceiver = new BroadcastReceiver() { |
| 911 | @Override |
| 912 | public void onReceive(Context context, Intent intent) { |
| 913 | String action = intent.getAction(); |
| 914 | if (action == null) { |
| 915 | return; |
| 916 | } |
| 917 | if (ACTION_NOTIFICATION_TIMEOUT.equals(action)) { |
| 918 | final NotificationRecord record; |
| 919 | synchronized (mNotificationLock) { |
| 920 | record = findNotificationByKeyLocked(intent.getStringExtra(EXTRA_KEY)); |
| 921 | } |
| 922 | if (record != null) { |
| 923 | cancelNotification(record.sbn.getUid(), record.sbn.getInitialPid(), |
| 924 | record.sbn.getPackageName(), record.sbn.getTag(), |
| 925 | record.sbn.getId(), 0, |
| 926 | Notification.FLAG_FOREGROUND_SERVICE, true, record.getUserId(), |
| 927 | REASON_TIMEOUT, null); |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | }; |
| 932 | |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 933 | private final BroadcastReceiver mPackageIntentReceiver = new BroadcastReceiver() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | @Override |
| 935 | public void onReceive(Context context, Intent intent) { |
| 936 | String action = intent.getAction(); |
Dianne Hackborn | 29cd7f1 | 2015-01-08 10:37:05 -0800 | [diff] [blame] | 937 | if (action == null) { |
| 938 | return; |
| 939 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 940 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 941 | boolean queryRestart = false; |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 942 | boolean queryRemove = false; |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 943 | boolean packageChanged = false; |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 944 | boolean cancelNotifications = true; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 945 | int reason = REASON_PACKAGE_CHANGED; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 946 | |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 947 | if (action.equals(Intent.ACTION_PACKAGE_ADDED) |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 948 | || (queryRemove=action.equals(Intent.ACTION_PACKAGE_REMOVED)) |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 949 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 950 | || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED)) |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 951 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 952 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE) |
| 953 | || action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 954 | int changeUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 955 | UserHandle.USER_ALL); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 956 | String pkgList[] = null; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 957 | int uidList[] = null; |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 958 | boolean removingPackage = queryRemove && |
| 959 | !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 960 | if (DBG) Slog.i(TAG, "action=" + action + " removing=" + removingPackage); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 961 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 962 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 963 | uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 964 | } else if (action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
| 965 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 966 | reason = REASON_PACKAGE_SUSPENDED; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 967 | } else if (queryRestart) { |
| 968 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 969 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 970 | } else { |
| 971 | Uri uri = intent.getData(); |
| 972 | if (uri == null) { |
| 973 | return; |
| 974 | } |
| 975 | String pkgName = uri.getSchemeSpecificPart(); |
| 976 | if (pkgName == null) { |
| 977 | return; |
| 978 | } |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 979 | if (packageChanged) { |
| 980 | // We cancel notifications for packages which have just been disabled |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 981 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 982 | final int enabled = mPackageManager.getApplicationEnabledSetting( |
| 983 | pkgName, |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 984 | changeUserId != UserHandle.USER_ALL ? changeUserId : |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 985 | USER_SYSTEM); |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 986 | if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 987 | || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 988 | cancelNotifications = false; |
| 989 | } |
| 990 | } catch (IllegalArgumentException e) { |
| 991 | // Package doesn't exist; probably racing with uninstall. |
| 992 | // cancelNotifications is already true, so nothing to do here. |
| 993 | if (DBG) { |
| 994 | Slog.i(TAG, "Exception trying to look up app enabled setting", e); |
| 995 | } |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 996 | } catch (RemoteException e) { |
| 997 | // Failed to talk to PackageManagerService Should never happen! |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 998 | } |
| 999 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1000 | pkgList = new String[]{pkgName}; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1001 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1002 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1003 | if (pkgList != null && (pkgList.length > 0)) { |
| 1004 | for (String pkgName : pkgList) { |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 1005 | if (cancelNotifications) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1006 | cancelAllNotificationsInt(MY_UID, MY_PID, pkgName, null, 0, 0, |
| 1007 | !queryRestart, changeUserId, reason, null); |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 1008 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1009 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1011 | mListeners.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1012 | mAssistants.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1013 | mConditionProviders.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1014 | mRankingHelper.onPackagesChanged(removingPackage, changeUserId, pkgList, uidList); |
| 1015 | savePolicyFile(); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | }; |
| 1019 | |
| 1020 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 1021 | @Override |
| 1022 | public void onReceive(Context context, Intent intent) { |
| 1023 | String action = intent.getAction(); |
| 1024 | |
| 1025 | if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1026 | // Keep track of screen on/off state, but do not turn off the notification light |
| 1027 | // until user passes through the lock screen or views the notification. |
| 1028 | mScreenOn = true; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 1029 | updateNotificationPulse(); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1030 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 1031 | mScreenOn = false; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 1032 | updateNotificationPulse(); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1033 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
John Spurlock | 5d2eeb1 | 2014-01-16 10:46:36 -0500 | [diff] [blame] | 1034 | mInCall = TelephonyManager.EXTRA_STATE_OFFHOOK |
| 1035 | .equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE)); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1036 | updateNotificationPulse(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1037 | } else if (action.equals(Intent.ACTION_USER_STOPPED)) { |
| 1038 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 1039 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1040 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1041 | REASON_USER_STOPPED, null); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1042 | } |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1043 | } else if (action.equals(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) { |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 1044 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1045 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1046 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1047 | REASON_PROFILE_TURNED_OFF, null); |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 1048 | } |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1049 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { |
| 1050 | // turn off LED when user passes through lock screen |
| 1051 | mNotificationLight.turnOff(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1052 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1053 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1054 | // reload per-user settings |
| 1055 | mSettingsObserver.update(null); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1056 | mUserProfiles.updateCache(context); |
Christoph Studer | b53dfd4 | 2014-09-12 14:45:59 +0200 | [diff] [blame] | 1057 | // Refresh managed services |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 1058 | mConditionProviders.onUserSwitched(user); |
| 1059 | mListeners.onUserSwitched(user); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1060 | mAssistants.onUserSwitched(user); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1061 | mZenModeHelper.onUserSwitched(user); |
Kenny Guy | 3a7c4a56 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1062 | } else if (action.equals(Intent.ACTION_USER_ADDED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1063 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
| 1064 | if (userId != USER_NULL) { |
| 1065 | mUserProfiles.updateCache(context); |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 1066 | if (!mUserProfiles.isManagedProfile(userId)) { |
| 1067 | readDefaultApprovedServices(userId); |
| 1068 | } |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1069 | } |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1070 | } else if (action.equals(Intent.ACTION_USER_REMOVED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1071 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1072 | mUserProfiles.updateCache(context); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1073 | mZenModeHelper.onUserRemoved(user); |
Julia Reynolds | 2e9bf5f | 2017-05-03 13:23:30 -0400 | [diff] [blame] | 1074 | mRankingHelper.onUserRemoved(user); |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 1075 | mListeners.onUserRemoved(user); |
| 1076 | mConditionProviders.onUserRemoved(user); |
| 1077 | mAssistants.onUserRemoved(user); |
Julia Reynolds | 2e9bf5f | 2017-05-03 13:23:30 -0400 | [diff] [blame] | 1078 | savePolicyFile(); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1079 | } else if (action.equals(Intent.ACTION_USER_UNLOCKED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1080 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1081 | mConditionProviders.onUserUnlocked(user); |
| 1082 | mListeners.onUserUnlocked(user); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1083 | mAssistants.onUserUnlocked(user); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1084 | mZenModeHelper.onUserUnlocked(user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | }; |
| 1088 | |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 1089 | private final class SettingsObserver extends ContentObserver { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1090 | private final Uri NOTIFICATION_BADGING_URI |
| 1091 | = Settings.Secure.getUriFor(Settings.Secure.NOTIFICATION_BADGING); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1092 | private final Uri NOTIFICATION_LIGHT_PULSE_URI |
| 1093 | = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1094 | private final Uri NOTIFICATION_RATE_LIMIT_URI |
| 1095 | = Settings.Global.getUriFor(Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1096 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1097 | SettingsObserver(Handler handler) { |
| 1098 | super(handler); |
| 1099 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1100 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1101 | void observe() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1102 | ContentResolver resolver = getContext().getContentResolver(); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1103 | resolver.registerContentObserver(NOTIFICATION_BADGING_URI, |
| 1104 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1105 | resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1106 | false, this, UserHandle.USER_ALL); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1107 | resolver.registerContentObserver(NOTIFICATION_RATE_LIMIT_URI, |
| 1108 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1109 | update(null); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1112 | @Override public void onChange(boolean selfChange, Uri uri) { |
| 1113 | update(uri); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1116 | public void update(Uri uri) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1117 | ContentResolver resolver = getContext().getContentResolver(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1118 | if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { |
zhoulei | 7e37697 | 2017-05-17 18:41:25 +0800 | [diff] [blame] | 1119 | boolean pulseEnabled = Settings.System.getIntForUser(resolver, |
| 1120 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0, UserHandle.USER_CURRENT) != 0; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1121 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 1122 | mNotificationPulseEnabled = pulseEnabled; |
| 1123 | updateNotificationPulse(); |
| 1124 | } |
| 1125 | } |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1126 | if (uri == null || NOTIFICATION_RATE_LIMIT_URI.equals(uri)) { |
| 1127 | mMaxPackageEnqueueRate = Settings.Global.getFloat(resolver, |
| 1128 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, mMaxPackageEnqueueRate); |
| 1129 | } |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1130 | if (uri == null || NOTIFICATION_BADGING_URI.equals(uri)) { |
| 1131 | mRankingHelper.updateBadgingEnabled(); |
| 1132 | } |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1133 | } |
| 1134 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1135 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1136 | private SettingsObserver mSettingsObserver; |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1137 | protected ZenModeHelper mZenModeHelper; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1138 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1139 | static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) { |
| 1140 | int[] ar = r.getIntArray(resid); |
| 1141 | if (ar == null) { |
| 1142 | return def; |
| 1143 | } |
| 1144 | final int len = ar.length > maxlen ? maxlen : ar.length; |
| 1145 | long[] out = new long[len]; |
| 1146 | for (int i=0; i<len; i++) { |
| 1147 | out[i] = ar[i]; |
| 1148 | } |
| 1149 | return out; |
| 1150 | } |
| 1151 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 1152 | public NotificationManagerService(Context context) { |
| 1153 | super(context); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 1154 | Notification.processWhitelistToken = WHITELIST_TOKEN; |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 1155 | } |
| 1156 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1157 | // TODO - replace these methods with a single VisibleForTesting constructor |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1158 | @VisibleForTesting |
| 1159 | void setAudioManager(AudioManager audioMananger) { |
| 1160 | mAudioManager = audioMananger; |
| 1161 | } |
| 1162 | |
| 1163 | @VisibleForTesting |
| 1164 | void setVibrator(Vibrator vibrator) { |
| 1165 | mVibrator = vibrator; |
| 1166 | } |
| 1167 | |
| 1168 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1169 | void setLights(Light light) { |
| 1170 | mNotificationLight = light; |
| 1171 | mAttentionLight = light; |
Julia Reynolds | 033a412 | 2017-01-31 16:50:38 -0500 | [diff] [blame] | 1172 | mNotificationPulseEnabled = true; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | @VisibleForTesting |
| 1176 | void setScreenOn(boolean on) { |
| 1177 | mScreenOn = on; |
| 1178 | } |
| 1179 | |
| 1180 | @VisibleForTesting |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1181 | int getNotificationRecordCount() { |
| 1182 | synchronized (mNotificationLock) { |
| 1183 | int count = mNotificationList.size() + mNotificationsByKey.size() |
| 1184 | + mSummaryByGroupKey.size() + mEnqueuedNotifications.size(); |
| 1185 | // subtract duplicates |
| 1186 | for (NotificationRecord posted : mNotificationList) { |
| 1187 | if (mNotificationsByKey.containsKey(posted.getKey())) { |
| 1188 | count--; |
| 1189 | } |
| 1190 | if (posted.sbn.isGroup() && posted.getNotification().isGroupSummary()) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1191 | count--; |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | return count; |
| 1196 | } |
| 1197 | } |
| 1198 | |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 1199 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1200 | void clearNotifications() { |
| 1201 | mEnqueuedNotifications.clear(); |
| 1202 | mNotificationList.clear(); |
| 1203 | mNotificationsByKey.clear(); |
| 1204 | mSummaryByGroupKey.clear(); |
| 1205 | } |
| 1206 | |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1207 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1208 | void addNotification(NotificationRecord r) { |
| 1209 | mNotificationList.add(r); |
| 1210 | mNotificationsByKey.put(r.sbn.getKey(), r); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1211 | if (r.sbn.isGroup()) { |
| 1212 | mSummaryByGroupKey.put(r.getGroupKey(), r); |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | @VisibleForTesting |
| 1217 | void addEnqueuedNotification(NotificationRecord r) { |
| 1218 | mEnqueuedNotifications.add(r); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | @VisibleForTesting |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1222 | NotificationRecord getNotificationRecord(String key) { |
| 1223 | return mNotificationsByKey.get(key); |
| 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | @VisibleForTesting |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1228 | void setSystemReady(boolean systemReady) { |
| 1229 | mSystemReady = systemReady; |
| 1230 | } |
| 1231 | |
| 1232 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1233 | void setHandler(WorkerHandler handler) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1234 | mHandler = handler; |
| 1235 | } |
| 1236 | |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1237 | @VisibleForTesting |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 1238 | void setFallbackVibrationPattern(long[] vibrationPattern) { |
| 1239 | mFallbackVibrationPattern = vibrationPattern; |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1240 | } |
| 1241 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1242 | @VisibleForTesting |
| 1243 | void setPackageManager(IPackageManager packageManager) { |
| 1244 | mPackageManager = packageManager; |
| 1245 | } |
| 1246 | |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1247 | @VisibleForTesting |
| 1248 | void setRankingHelper(RankingHelper rankingHelper) { |
| 1249 | mRankingHelper = rankingHelper; |
| 1250 | } |
| 1251 | |
| 1252 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1253 | void setRankingHandler(RankingHandler rankingHandler) { |
| 1254 | mRankingHandler = rankingHandler; |
| 1255 | } |
| 1256 | |
| 1257 | @VisibleForTesting |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1258 | void setIsTelevision(boolean isTelevision) { |
| 1259 | mIsTelevision = isTelevision; |
| 1260 | } |
| 1261 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 1262 | @VisibleForTesting |
| 1263 | void setUsageStats(NotificationUsageStats us) { |
| 1264 | mUsageStats = us; |
| 1265 | } |
| 1266 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 1267 | @VisibleForTesting |
| 1268 | void setAccessibilityManager(AccessibilityManager am) { |
| 1269 | mAccessibilityManager = am; |
| 1270 | } |
| 1271 | |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1272 | // 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] | 1273 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1274 | void init(Looper looper, IPackageManager packageManager, |
| 1275 | PackageManager packageManagerClient, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1276 | LightsManager lightsManager, NotificationListeners notificationListeners, |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1277 | NotificationAssistants notificationAssistants, ConditionProviders conditionProviders, |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1278 | ICompanionDeviceManager companionManager, SnoozeHelper snoozeHelper, |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1279 | NotificationUsageStats usageStats, AtomicFile policyFile, |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1280 | ActivityManager activityManager, GroupHelper groupHelper, IActivityManager am) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1281 | Resources resources = getContext().getResources(); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1282 | mMaxPackageEnqueueRate = Settings.Global.getFloat(getContext().getContentResolver(), |
| 1283 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, |
| 1284 | DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE); |
| 1285 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 1286 | mAccessibilityManager = |
| 1287 | (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1288 | mAm = am; |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1289 | mPackageManager = packageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1290 | mPackageManagerClient = packageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1291 | mAppOps = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE); |
| 1292 | mVibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 1293 | mAppUsageStats = LocalServices.getService(UsageStatsManagerInternal.class); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1294 | mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1295 | mCompanionManager = companionManager; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1296 | mActivityManager = activityManager; |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 1297 | mDeviceIdleController = IDeviceIdleController.Stub.asInterface( |
| 1298 | ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER)); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1299 | try { |
| 1300 | mPermissionOwner = mAm.newUriPermissionOwner("notification"); |
| 1301 | } catch (RemoteException e) { |
| 1302 | Slog.w(TAG, "AM dead", e); |
| 1303 | } |
San Mehat | 3ee1317 | 2010-02-04 20:54:43 -0800 | [diff] [blame] | 1304 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1305 | mHandler = new WorkerHandler(looper); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1306 | mRankingThread.start(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1307 | String[] extractorNames; |
| 1308 | try { |
| 1309 | extractorNames = resources.getStringArray(R.array.config_notificationSignalExtractors); |
| 1310 | } catch (Resources.NotFoundException e) { |
| 1311 | extractorNames = new String[0]; |
| 1312 | } |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1313 | mUsageStats = usageStats; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1314 | mRankingHandler = new RankingHandlerWorker(mRankingThread.getLooper()); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1315 | mRankingHelper = new RankingHelper(getContext(), |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1316 | mPackageManagerClient, |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1317 | mRankingHandler, |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 1318 | mUsageStats, |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1319 | extractorNames); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1320 | mConditionProviders = conditionProviders; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1321 | mZenModeHelper = new ZenModeHelper(getContext(), mHandler.getLooper(), mConditionProviders); |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 1322 | mZenModeHelper.addCallback(new ZenModeHelper.Callback() { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1323 | @Override |
| 1324 | public void onConfigChanged() { |
| 1325 | savePolicyFile(); |
| 1326 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1327 | |
| 1328 | @Override |
| 1329 | void onZenModeChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1330 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED); |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1331 | getContext().sendBroadcastAsUser( |
Jason Monk | 6350674 | 2015-12-16 12:06:51 -0500 | [diff] [blame] | 1332 | new Intent(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL) |
| 1333 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT), |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1334 | UserHandle.ALL, android.Manifest.permission.MANAGE_NOTIFICATIONS); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1335 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1336 | updateInterruptionFilterLocked(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1337 | } |
| 1338 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1339 | |
| 1340 | @Override |
| 1341 | void onPolicyChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1342 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED); |
| 1343 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1344 | }); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1345 | mSnoozeHelper = snoozeHelper; |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1346 | mGroupHelper = groupHelper; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 1347 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1348 | // This is a ManagedServices object that keeps track of the listeners. |
| 1349 | mListeners = notificationListeners; |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1350 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 1351 | // This is a MangedServices object that keeps track of the assistant. |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1352 | mAssistants = notificationAssistants; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1353 | |
| 1354 | mPolicyFile = policyFile; |
| 1355 | loadPolicyFile(); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1356 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1357 | mStatusBar = getLocalService(StatusBarManagerInternal.class); |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 1358 | if (mStatusBar != null) { |
| 1359 | mStatusBar.setNotificationDelegate(mNotificationDelegate); |
| 1360 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1361 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1362 | mNotificationLight = lightsManager.getLight(LightsManager.LIGHT_ID_NOTIFICATIONS); |
| 1363 | mAttentionLight = lightsManager.getLight(LightsManager.LIGHT_ID_ATTENTION); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1364 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1365 | mFallbackVibrationPattern = getLongArray(resources, |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 1366 | R.array.config_notificationFallbackVibePattern, |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1367 | VIBRATE_PATTERN_MAXLEN, |
| 1368 | DEFAULT_VIBRATE_PATTERN); |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 1369 | mInCallNotificationUri = Uri.parse("file://" + |
| 1370 | resources.getString(R.string.config_inCallNotificationSound)); |
| 1371 | mInCallNotificationAudioAttributes = new AudioAttributes.Builder() |
| 1372 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 1373 | .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 1374 | .build(); |
| 1375 | mInCallNotificationVolume = resources.getFloat(R.dimen.config_inCallNotificationVolume); |
| 1376 | |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 1377 | mUseAttentionLight = resources.getBoolean(R.bool.config_useAttentionLight); |
| 1378 | |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1379 | // Don't start allowing notifications until the setup wizard has run once. |
| 1380 | // After that, including subsequent boots, init with notifications turned on. |
| 1381 | // This works on the first boot because the setup wizard will toggle this |
| 1382 | // 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] | 1383 | if (0 == Settings.Global.getInt(getContext().getContentResolver(), |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1384 | Settings.Global.DEVICE_PROVISIONED, 0)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1385 | mDisableNotificationEffects = true; |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1386 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1387 | mZenModeHelper.initZenMode(); |
John Spurlock | f370177 | 2015-02-12 13:29:37 -0500 | [diff] [blame] | 1388 | mInterruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1389 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1390 | mUserProfiles.updateCache(getContext()); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 1391 | listenForCallState(); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1392 | |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1393 | mSettingsObserver = new SettingsObserver(mHandler); |
| 1394 | |
| 1395 | mArchive = new Archive(resources.getInteger( |
| 1396 | R.integer.config_notificationServiceArchiveSize)); |
| 1397 | |
| 1398 | mIsTelevision = mPackageManagerClient.hasSystemFeature(FEATURE_LEANBACK) |
| 1399 | || mPackageManagerClient.hasSystemFeature(FEATURE_TELEVISION); |
| 1400 | } |
| 1401 | |
| 1402 | @Override |
| 1403 | public void onStart() { |
| 1404 | SnoozeHelper snoozeHelper = new SnoozeHelper(getContext(), new SnoozeHelper.Callback() { |
| 1405 | @Override |
| 1406 | public void repost(int userId, NotificationRecord r) { |
| 1407 | try { |
| 1408 | if (DBG) { |
| 1409 | Slog.d(TAG, "Reposting " + r.getKey()); |
| 1410 | } |
| 1411 | enqueueNotificationInternal(r.sbn.getPackageName(), r.sbn.getOpPkg(), |
| 1412 | r.sbn.getUid(), r.sbn.getInitialPid(), r.sbn.getTag(), r.sbn.getId(), |
| 1413 | r.sbn.getNotification(), userId); |
| 1414 | } catch (Exception e) { |
| 1415 | Slog.e(TAG, "Cannot un-snooze notification", e); |
| 1416 | } |
| 1417 | } |
| 1418 | }, mUserProfiles); |
| 1419 | |
| 1420 | final File systemDir = new File(Environment.getDataDirectory(), "system"); |
| 1421 | |
| 1422 | init(Looper.myLooper(), |
| 1423 | AppGlobals.getPackageManager(), getContext().getPackageManager(), |
| 1424 | getLocalService(LightsManager.class), |
| 1425 | new NotificationListeners(AppGlobals.getPackageManager()), |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 1426 | new NotificationAssistants(getContext(), mNotificationLock, mUserProfiles, |
| 1427 | AppGlobals.getPackageManager()), |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1428 | new ConditionProviders(getContext(), mUserProfiles, AppGlobals.getPackageManager()), |
| 1429 | null, snoozeHelper, new NotificationUsageStats(getContext()), |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 1430 | new AtomicFile(new File(systemDir, "notification_policy.xml"), "notification-policy"), |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1431 | (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE), |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1432 | getGroupHelper(), ActivityManager.getService()); |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1433 | |
Mike Lockwood | 35e16bf | 2010-11-30 19:53:36 -0500 | [diff] [blame] | 1434 | // register for various Intents |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1435 | IntentFilter filter = new IntentFilter(); |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1436 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 1437 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1438 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1439 | filter.addAction(Intent.ACTION_USER_PRESENT); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1440 | filter.addAction(Intent.ACTION_USER_STOPPED); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1441 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Kenny Guy | 3a7c4a56 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1442 | filter.addAction(Intent.ACTION_USER_ADDED); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1443 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1444 | filter.addAction(Intent.ACTION_USER_UNLOCKED); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1445 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1446 | getContext().registerReceiver(mIntentReceiver, filter); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1447 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1448 | IntentFilter pkgFilter = new IntentFilter(); |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1449 | pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1450 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
Daniel Sandler | aac0eb0 | 2011-08-06 22:51:56 -0400 | [diff] [blame] | 1451 | pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1452 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 1453 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 1454 | pkgFilter.addDataScheme("package"); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1455 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, pkgFilter, null, |
| 1456 | null); |
| 1457 | |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1458 | IntentFilter suspendedPkgFilter = new IntentFilter(); |
| 1459 | suspendedPkgFilter.addAction(Intent.ACTION_PACKAGES_SUSPENDED); |
| 1460 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, |
| 1461 | suspendedPkgFilter, null, null); |
| 1462 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1463 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1464 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, sdFilter, null, |
| 1465 | null); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1466 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1467 | IntentFilter timeoutFilter = new IntentFilter(ACTION_NOTIFICATION_TIMEOUT); |
| 1468 | timeoutFilter.addDataScheme(SCHEME_TIMEOUT); |
| 1469 | getContext().registerReceiver(mNotificationTimeoutReceiver, timeoutFilter); |
| 1470 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1471 | IntentFilter settingsRestoredFilter = new IntentFilter(Intent.ACTION_SETTING_RESTORED); |
| 1472 | getContext().registerReceiver(mRestoreReceiver, settingsRestoredFilter); |
| 1473 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1474 | IntentFilter localeChangedFilter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED); |
| 1475 | getContext().registerReceiver(mLocaleChangeReceiver, localeChangedFilter); |
| 1476 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1477 | publishBinderService(Context.NOTIFICATION_SERVICE, mService); |
| 1478 | publishLocalService(NotificationManagerInternal.class, mInternalService); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1479 | } |
| 1480 | |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1481 | private GroupHelper getGroupHelper() { |
| 1482 | return new GroupHelper(new GroupHelper.Callback() { |
| 1483 | @Override |
| 1484 | public void addAutoGroup(String key) { |
| 1485 | synchronized (mNotificationLock) { |
| 1486 | addAutogroupKeyLocked(key); |
| 1487 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | @Override |
| 1491 | public void removeAutoGroup(String key) { |
| 1492 | synchronized (mNotificationLock) { |
| 1493 | removeAutogroupKeyLocked(key); |
| 1494 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | @Override |
| 1498 | public void addAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 1499 | createAutoGroupSummary(userId, pkg, triggeringKey); |
| 1500 | } |
| 1501 | |
| 1502 | @Override |
| 1503 | public void removeAutoGroupSummary(int userId, String pkg) { |
| 1504 | synchronized (mNotificationLock) { |
| 1505 | clearAutogroupSummaryLocked(userId, pkg); |
| 1506 | } |
| 1507 | } |
| 1508 | }); |
| 1509 | } |
| 1510 | |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1511 | private void sendRegisteredOnlyBroadcast(String action) { |
| 1512 | getContext().sendBroadcastAsUser(new Intent(action) |
| 1513 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), UserHandle.ALL, null); |
| 1514 | } |
| 1515 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1516 | @Override |
| 1517 | public void onBootPhase(int phase) { |
| 1518 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 1519 | // no beeping until we're basically done booting |
| 1520 | mSystemReady = true; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1521 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1522 | // Grab our optional AudioService |
| 1523 | mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1524 | mAudioManagerInternal = getLocalService(AudioManagerInternal.class); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1525 | mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1526 | mZenModeHelper.onSystemReady(); |
Adam Lesinski | a6db4ab | 2014-03-24 12:31:45 -0700 | [diff] [blame] | 1527 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 1528 | // This observer will force an update when observe is called, causing us to |
| 1529 | // bind to listener services. |
| 1530 | mSettingsObserver.observe(); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1531 | mListeners.onBootPhaseAppsCanStart(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1532 | mAssistants.onBootPhaseAppsCanStart(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1533 | mConditionProviders.onBootPhaseAppsCanStart(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1534 | } |
| 1535 | } |
| 1536 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1537 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1538 | private void updateListenerHintsLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1539 | final int hints = calculateHints(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1540 | if (hints == mListenerHints) return; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1541 | ZenLog.traceListenerHintsChanged(mListenerHints, hints, mEffectsSuppressors.size()); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1542 | mListenerHints = hints; |
| 1543 | scheduleListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 1544 | } |
| 1545 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1546 | @GuardedBy("mNotificationLock") |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1547 | private void updateEffectsSuppressorLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1548 | final long updatedSuppressedEffects = calculateSuppressedEffects(); |
| 1549 | if (updatedSuppressedEffects == mZenModeHelper.getSuppressedEffects()) return; |
| 1550 | final List<ComponentName> suppressors = getSuppressors(); |
| 1551 | ZenLog.traceEffectsSuppressorChanged(mEffectsSuppressors, suppressors, updatedSuppressedEffects); |
| 1552 | mEffectsSuppressors = suppressors; |
| 1553 | mZenModeHelper.setSuppressedEffects(updatedSuppressedEffects); |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1554 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1555 | } |
| 1556 | |
Amith Yamasani | 396a10c | 2018-01-19 10:58:07 -0800 | [diff] [blame] | 1557 | private void exitIdle() { |
| 1558 | try { |
| 1559 | if (mDeviceIdleController != null) { |
| 1560 | mDeviceIdleController.exitIdle("notification interaction"); |
| 1561 | } |
| 1562 | } catch (RemoteException e) { |
| 1563 | } |
| 1564 | } |
| 1565 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1566 | private void updateNotificationChannelInt(String pkg, int uid, NotificationChannel channel, |
| 1567 | boolean fromListener) { |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1568 | if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) { |
| 1569 | // cancel |
| 1570 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channel.getId(), 0, 0, true, |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1571 | UserHandle.getUserId(uid), REASON_CHANNEL_BANNED, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1572 | null); |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1573 | if (isUidSystemOrPhone(uid)) { |
| 1574 | int[] profileIds = mUserProfiles.getCurrentProfileIds(); |
| 1575 | int N = profileIds.length; |
| 1576 | for (int i = 0; i < N; i++) { |
| 1577 | int profileId = profileIds[i]; |
| 1578 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channel.getId(), 0, 0, true, |
| 1579 | profileId, REASON_CHANNEL_BANNED, |
| 1580 | null); |
| 1581 | } |
| 1582 | } |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1583 | } |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1584 | final NotificationChannel preUpdate = |
| 1585 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), true); |
| 1586 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1587 | mRankingHelper.updateNotificationChannel(pkg, uid, channel, true); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1588 | maybeNotifyChannelOwner(pkg, uid, preUpdate, channel); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1589 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1590 | if (!fromListener) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1591 | final NotificationChannel modifiedChannel = |
| 1592 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), false); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1593 | mListeners.notifyNotificationChannelChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1594 | pkg, UserHandle.getUserHandleForUid(uid), |
| 1595 | modifiedChannel, NOTIFICATION_CHANNEL_OR_GROUP_UPDATED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1596 | } |
| 1597 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1598 | savePolicyFile(); |
| 1599 | } |
| 1600 | |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1601 | private void maybeNotifyChannelOwner(String pkg, int uid, NotificationChannel preUpdate, |
| 1602 | NotificationChannel update) { |
| 1603 | try { |
| 1604 | if ((preUpdate.getImportance() == IMPORTANCE_NONE |
| 1605 | && update.getImportance() != IMPORTANCE_NONE) |
| 1606 | || (preUpdate.getImportance() != IMPORTANCE_NONE |
| 1607 | && update.getImportance() == IMPORTANCE_NONE)) { |
| 1608 | getContext().sendBroadcastAsUser( |
| 1609 | new Intent(ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED) |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 1610 | .putExtra(NotificationManager.EXTRA_NOTIFICATION_CHANNEL_ID, |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1611 | update.getId()) |
| 1612 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, |
| 1613 | update.getImportance() == IMPORTANCE_NONE) |
| 1614 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 1615 | .setPackage(pkg), |
| 1616 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 1617 | } |
| 1618 | } catch (SecurityException e) { |
| 1619 | Slog.w(TAG, "Can't notify app about channel change", e); |
| 1620 | } |
| 1621 | } |
| 1622 | |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1623 | private void createNotificationChannelGroup(String pkg, int uid, NotificationChannelGroup group, |
| 1624 | boolean fromApp, boolean fromListener) { |
| 1625 | Preconditions.checkNotNull(group); |
| 1626 | Preconditions.checkNotNull(pkg); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1627 | |
| 1628 | final NotificationChannelGroup preUpdate = |
| 1629 | mRankingHelper.getNotificationChannelGroup(group.getId(), pkg, uid); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1630 | mRankingHelper.createNotificationChannelGroup(pkg, uid, group, |
| 1631 | fromApp); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1632 | if (!fromApp) { |
| 1633 | maybeNotifyChannelGroupOwner(pkg, uid, preUpdate, group); |
| 1634 | } |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1635 | if (!fromListener) { |
| 1636 | mListeners.notifyNotificationChannelGroupChanged(pkg, |
| 1637 | UserHandle.of(UserHandle.getCallingUserId()), group, |
| 1638 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
| 1639 | } |
| 1640 | } |
| 1641 | |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1642 | private void maybeNotifyChannelGroupOwner(String pkg, int uid, |
| 1643 | NotificationChannelGroup preUpdate, NotificationChannelGroup update) { |
| 1644 | try { |
| 1645 | if (preUpdate.isBlocked() != update.isBlocked()) { |
| 1646 | getContext().sendBroadcastAsUser( |
| 1647 | new Intent(ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED) |
Julia Reynolds | 44ff7c9 | 2018-02-05 10:02:30 -0500 | [diff] [blame] | 1648 | .putExtra(NotificationManager.EXTRA_NOTIFICATION_CHANNEL_GROUP_ID, |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1649 | update.getId()) |
| 1650 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, |
| 1651 | update.isBlocked()) |
| 1652 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 1653 | .setPackage(pkg), |
| 1654 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 1655 | } |
| 1656 | } catch (SecurityException e) { |
| 1657 | Slog.w(TAG, "Can't notify app about group change", e); |
| 1658 | } |
| 1659 | } |
| 1660 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1661 | private ArrayList<ComponentName> getSuppressors() { |
| 1662 | ArrayList<ComponentName> names = new ArrayList<ComponentName>(); |
| 1663 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1664 | ArraySet<ManagedServiceInfo> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
| 1665 | |
| 1666 | for (ManagedServiceInfo info : serviceInfoList) { |
| 1667 | names.add(info.component); |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | return names; |
| 1672 | } |
| 1673 | |
| 1674 | private boolean removeDisabledHints(ManagedServiceInfo info) { |
| 1675 | return removeDisabledHints(info, 0); |
| 1676 | } |
| 1677 | |
| 1678 | private boolean removeDisabledHints(ManagedServiceInfo info, int hints) { |
| 1679 | boolean removed = false; |
| 1680 | |
| 1681 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1682 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 1683 | final ArraySet<ManagedServiceInfo> listeners = |
| 1684 | mListenersDisablingEffects.valueAt(i); |
| 1685 | |
| 1686 | if (hints == 0 || (hint & hints) == hint) { |
| 1687 | removed = removed || listeners.remove(info); |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | return removed; |
| 1692 | } |
| 1693 | |
| 1694 | private void addDisabledHints(ManagedServiceInfo info, int hints) { |
| 1695 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1696 | addDisabledHint(info, HINT_HOST_DISABLE_EFFECTS); |
| 1697 | } |
| 1698 | |
| 1699 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1700 | addDisabledHint(info, HINT_HOST_DISABLE_NOTIFICATION_EFFECTS); |
| 1701 | } |
| 1702 | |
| 1703 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1704 | addDisabledHint(info, HINT_HOST_DISABLE_CALL_EFFECTS); |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | private void addDisabledHint(ManagedServiceInfo info, int hint) { |
| 1709 | if (mListenersDisablingEffects.indexOfKey(hint) < 0) { |
| 1710 | mListenersDisablingEffects.put(hint, new ArraySet<ManagedServiceInfo>()); |
| 1711 | } |
| 1712 | |
| 1713 | ArraySet<ManagedServiceInfo> hintListeners = mListenersDisablingEffects.get(hint); |
| 1714 | hintListeners.add(info); |
| 1715 | } |
| 1716 | |
| 1717 | private int calculateHints() { |
| 1718 | int hints = 0; |
| 1719 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1720 | int hint = mListenersDisablingEffects.keyAt(i); |
| 1721 | ArraySet<ManagedServiceInfo> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
| 1722 | |
| 1723 | if (!serviceInfoList.isEmpty()) { |
| 1724 | hints |= hint; |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | return hints; |
| 1729 | } |
| 1730 | |
| 1731 | private long calculateSuppressedEffects() { |
| 1732 | int hints = calculateHints(); |
| 1733 | long suppressedEffects = 0; |
| 1734 | |
| 1735 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1736 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_ALL; |
| 1737 | } |
| 1738 | |
| 1739 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1740 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_NOTIFICATIONS; |
| 1741 | } |
| 1742 | |
| 1743 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1744 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_CALLS; |
| 1745 | } |
| 1746 | |
| 1747 | return suppressedEffects; |
| 1748 | } |
| 1749 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1750 | @GuardedBy("mNotificationLock") |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1751 | private void updateInterruptionFilterLocked() { |
| 1752 | int interruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
| 1753 | if (interruptionFilter == mInterruptionFilter) return; |
| 1754 | mInterruptionFilter = interruptionFilter; |
| 1755 | scheduleInterruptionFilterChanged(interruptionFilter); |
| 1756 | } |
| 1757 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1758 | @VisibleForTesting |
| 1759 | INotificationManager getBinderService() { |
| 1760 | return INotificationManager.Stub.asInterface(mService); |
| 1761 | } |
| 1762 | |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1763 | /** |
| 1764 | * Report to usage stats that the notification was seen. |
| 1765 | * @param r notification record |
| 1766 | */ |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 1767 | protected void reportSeen(NotificationRecord r) { |
| 1768 | final int userId = r.sbn.getUserId(); |
| 1769 | mAppUsageStats.reportEvent(r.sbn.getPackageName(), |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 1770 | userId == UserHandle.USER_ALL ? USER_SYSTEM |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 1771 | : userId, |
| 1772 | UsageEvents.Event.NOTIFICATION_SEEN); |
| 1773 | } |
| 1774 | |
Amith Yamasani | 7ec8941 | 2018-02-07 08:48:49 -0800 | [diff] [blame] | 1775 | /** |
| 1776 | * Report to usage stats that the notification was clicked. |
| 1777 | * @param r notification record |
| 1778 | */ |
| 1779 | protected void reportUserInteraction(NotificationRecord r) { |
| 1780 | final int userId = r.sbn.getUserId(); |
| 1781 | mAppUsageStats.reportEvent(r.sbn.getPackageName(), |
| 1782 | userId == UserHandle.USER_ALL ? UserHandle.USER_SYSTEM : userId, |
| 1783 | UsageEvents.Event.USER_INTERACTION); |
| 1784 | } |
| 1785 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1786 | @VisibleForTesting |
| 1787 | NotificationManagerInternal getInternalService() { |
| 1788 | return mInternalService; |
| 1789 | } |
| 1790 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1791 | private final IBinder mService = new INotificationManager.Stub() { |
| 1792 | // Toasts |
| 1793 | // ============================================================================ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1794 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1795 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1796 | public void enqueueToast(String pkg, ITransientNotification callback, int duration) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1797 | { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1798 | if (DBG) { |
| 1799 | Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback |
| 1800 | + " duration=" + duration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1801 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1802 | |
| 1803 | if (pkg == null || callback == null) { |
| 1804 | Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback); |
| 1805 | return ; |
| 1806 | } |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1807 | final boolean isSystemToast = isCallerSystemOrPhone() || ("android".equals(pkg)); |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 1808 | final boolean isPackageSuspended = |
| 1809 | isPackageSuspendedForUser(pkg, Binder.getCallingUid()); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1810 | |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 1811 | if (ENABLE_BLOCKED_TOASTS && !isSystemToast && |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1812 | (!areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()) |
| 1813 | || isPackageSuspended)) { |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 1814 | Slog.e(TAG, "Suppressing toast from package " + pkg |
| 1815 | + (isPackageSuspended |
| 1816 | ? " due to package suspended by administrator." |
| 1817 | : " by user request.")); |
| 1818 | return; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | synchronized (mToastQueue) { |
| 1822 | int callingPid = Binder.getCallingPid(); |
| 1823 | long callingId = Binder.clearCallingIdentity(); |
| 1824 | try { |
| 1825 | ToastRecord record; |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 1826 | int index; |
| 1827 | // All packages aside from the android package can enqueue one toast at a time |
| 1828 | if (!isSystemToast) { |
| 1829 | index = indexOfToastPackageLocked(pkg); |
| 1830 | } else { |
| 1831 | index = indexOfToastLocked(pkg, callback); |
| 1832 | } |
| 1833 | |
| 1834 | // If the package already has a toast, we update its toast |
| 1835 | // in the queue, we don't move it to the end of the queue. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1836 | if (index >= 0) { |
| 1837 | record = mToastQueue.get(index); |
| 1838 | record.update(duration); |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 1839 | record.update(callback); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1840 | } else { |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1841 | Binder token = new Binder(); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 1842 | mWindowManagerInternal.addWindowToken(token, TYPE_TOAST, DEFAULT_DISPLAY); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1843 | record = new ToastRecord(callingPid, pkg, callback, duration, token); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1844 | mToastQueue.add(record); |
| 1845 | index = mToastQueue.size() - 1; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1846 | } |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 1847 | keepProcessAliveIfNeededLocked(callingPid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1848 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 1849 | // new or just been updated. Call back and tell it to show itself. |
| 1850 | // If the callback fails, this will remove it from the list, so don't |
| 1851 | // assume that it's valid after this. |
| 1852 | if (index == 0) { |
| 1853 | showNextToastLocked(); |
| 1854 | } |
| 1855 | } finally { |
| 1856 | Binder.restoreCallingIdentity(callingId); |
| 1857 | } |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | @Override |
| 1862 | public void cancelToast(String pkg, ITransientNotification callback) { |
| 1863 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| 1864 | |
| 1865 | if (pkg == null || callback == null) { |
| 1866 | Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback); |
| 1867 | return ; |
| 1868 | } |
| 1869 | |
| 1870 | synchronized (mToastQueue) { |
| 1871 | long callingId = Binder.clearCallingIdentity(); |
| 1872 | try { |
| 1873 | int index = indexOfToastLocked(pkg, callback); |
| 1874 | if (index >= 0) { |
| 1875 | cancelToastLocked(index); |
| 1876 | } else { |
| 1877 | Slog.w(TAG, "Toast already cancelled. pkg=" + pkg |
| 1878 | + " callback=" + callback); |
| 1879 | } |
| 1880 | } finally { |
| 1881 | Binder.restoreCallingIdentity(callingId); |
| 1882 | } |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 1887 | public void enqueueNotificationWithTag(String pkg, String opPkg, String tag, int id, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1888 | Notification notification, int userId) throws RemoteException { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 1889 | enqueueNotificationInternal(pkg, opPkg, Binder.getCallingUid(), |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1890 | Binder.getCallingPid(), tag, id, notification, userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | @Override |
| 1894 | public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1895 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1896 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1897 | Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 1898 | // Don't allow client applications to cancel foreground service notis or autobundled |
| 1899 | // summaries. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1900 | final int mustNotHaveFlags = isCallingUidSystem() ? 0 : |
| 1901 | (Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_AUTOGROUP_SUMMARY); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1902 | cancelNotification(Binder.getCallingUid(), Binder.getCallingPid(), pkg, tag, id, 0, |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1903 | mustNotHaveFlags, false, userId, REASON_APP_CANCEL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | @Override |
| 1907 | public void cancelAllNotifications(String pkg, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1908 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1909 | |
| 1910 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1911 | Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg); |
| 1912 | |
| 1913 | // Calling from user space, don't allow the canceling of actively |
| 1914 | // running foreground services. |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1915 | cancelAllNotificationsInt(Binder.getCallingUid(), Binder.getCallingPid(), |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1916 | pkg, null, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1917 | REASON_APP_CANCEL_ALL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | @Override |
| 1921 | public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1922 | checkCallerIsSystem(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1923 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1924 | mRankingHelper.setEnabled(pkg, uid, enabled); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1925 | // 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] | 1926 | if (!enabled) { |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1927 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, null, 0, 0, true, |
| 1928 | UserHandle.getUserId(uid), REASON_PACKAGE_BANNED, null); |
| 1929 | } |
Julia Reynolds | fc9767b | 2018-01-22 17:45:16 -0500 | [diff] [blame] | 1930 | |
| 1931 | try { |
| 1932 | getContext().sendBroadcastAsUser( |
| 1933 | new Intent(ACTION_APP_BLOCK_STATE_CHANGED) |
| 1934 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, !enabled) |
| 1935 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 1936 | .setPackage(pkg), |
| 1937 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 1938 | } catch (SecurityException e) { |
| 1939 | Slog.w(TAG, "Can't notify app about app block change", e); |
| 1940 | } |
| 1941 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1942 | savePolicyFile(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | /** |
| 1946 | * Use this when you just want to know if notifications are OK for this package. |
| 1947 | */ |
| 1948 | @Override |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1949 | public boolean areNotificationsEnabled(String pkg) { |
| 1950 | return areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()); |
| 1951 | } |
| 1952 | |
| 1953 | /** |
| 1954 | * Use this when you just want to know if notifications are OK for this package. |
| 1955 | */ |
| 1956 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1957 | public boolean areNotificationsEnabledForPackage(String pkg, int uid) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1958 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1959 | |
| 1960 | return mRankingHelper.getImportance(pkg, uid) != IMPORTANCE_NONE; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1961 | } |
| 1962 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1963 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1964 | public int getPackageImportance(String pkg) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1965 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1966 | return mRankingHelper.getImportance(pkg, Binder.getCallingUid()); |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1967 | } |
| 1968 | |
| 1969 | @Override |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1970 | public boolean canShowBadge(String pkg, int uid) { |
| 1971 | checkCallerIsSystem(); |
| 1972 | return mRankingHelper.canShowBadge(pkg, uid); |
| 1973 | } |
| 1974 | |
| 1975 | @Override |
| 1976 | public void setShowBadge(String pkg, int uid, boolean showBadge) { |
| 1977 | checkCallerIsSystem(); |
| 1978 | mRankingHelper.setShowBadge(pkg, uid, showBadge); |
| 1979 | savePolicyFile(); |
| 1980 | } |
| 1981 | |
| 1982 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1983 | public void updateNotificationChannelGroupForPackage(String pkg, int uid, |
| 1984 | NotificationChannelGroup group) throws RemoteException { |
| 1985 | enforceSystemOrSystemUI("Caller not system or systemui"); |
| 1986 | createNotificationChannelGroup(pkg, uid, group, false, false); |
| 1987 | savePolicyFile(); |
| 1988 | } |
| 1989 | |
| 1990 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1991 | public void createNotificationChannelGroups(String pkg, |
| 1992 | ParceledListSlice channelGroupList) throws RemoteException { |
| 1993 | checkCallerIsSystemOrSameApp(pkg); |
| 1994 | List<NotificationChannelGroup> groups = channelGroupList.getList(); |
| 1995 | final int groupSize = groups.size(); |
| 1996 | for (int i = 0; i < groupSize; i++) { |
| 1997 | final NotificationChannelGroup group = groups.get(i); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1998 | createNotificationChannelGroup(pkg, Binder.getCallingUid(), group, true, false); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1999 | } |
| 2000 | savePolicyFile(); |
| 2001 | } |
| 2002 | |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 2003 | private void createNotificationChannelsImpl(String pkg, int uid, |
| 2004 | ParceledListSlice channelsList) { |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 2005 | List<NotificationChannel> channels = channelsList.getList(); |
| 2006 | final int channelsSize = channels.size(); |
| 2007 | for (int i = 0; i < channelsSize; i++) { |
| 2008 | final NotificationChannel channel = channels.get(i); |
| 2009 | Preconditions.checkNotNull(channel, "channel in list is null"); |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 2010 | mRankingHelper.createNotificationChannel(pkg, uid, channel, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 2011 | true /* fromTargetApp */); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2012 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2013 | UserHandle.getUserHandleForUid(uid), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2014 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), false), |
| 2015 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 2016 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2017 | savePolicyFile(); |
| 2018 | } |
| 2019 | |
| 2020 | @Override |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 2021 | public void createNotificationChannels(String pkg, |
| 2022 | ParceledListSlice channelsList) throws RemoteException { |
| 2023 | checkCallerIsSystemOrSameApp(pkg); |
| 2024 | createNotificationChannelsImpl(pkg, Binder.getCallingUid(), channelsList); |
| 2025 | } |
| 2026 | |
| 2027 | @Override |
| 2028 | public void createNotificationChannelsForPackage(String pkg, int uid, |
| 2029 | ParceledListSlice channelsList) throws RemoteException { |
| 2030 | checkCallerIsSystem(); |
| 2031 | createNotificationChannelsImpl(pkg, uid, channelsList); |
| 2032 | } |
| 2033 | |
| 2034 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2035 | public NotificationChannel getNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2036 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2037 | return mRankingHelper.getNotificationChannel( |
| 2038 | pkg, Binder.getCallingUid(), channelId, false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | @Override |
| 2042 | public NotificationChannel getNotificationChannelForPackage(String pkg, int uid, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2043 | String channelId, boolean includeDeleted) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2044 | checkCallerIsSystem(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2045 | return mRankingHelper.getNotificationChannel(pkg, uid, channelId, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | @Override |
| 2049 | public void deleteNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2050 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2051 | final int callingUid = Binder.getCallingUid(); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2052 | if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 2053 | throw new IllegalArgumentException("Cannot delete default channel"); |
| 2054 | } |
| 2055 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channelId, 0, 0, true, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2056 | UserHandle.getUserId(callingUid), REASON_CHANNEL_BANNED, null); |
| 2057 | mRankingHelper.deleteNotificationChannel(pkg, callingUid, channelId); |
| 2058 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2059 | UserHandle.getUserHandleForUid(callingUid), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2060 | mRankingHelper.getNotificationChannel(pkg, callingUid, channelId, true), |
| 2061 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2062 | savePolicyFile(); |
| 2063 | } |
| 2064 | |
| 2065 | @Override |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2066 | public NotificationChannelGroup getNotificationChannelGroup(String pkg, String groupId) { |
| 2067 | checkCallerIsSystemOrSameApp(pkg); |
| 2068 | return mRankingHelper.getNotificationChannelGroupWithChannels( |
| 2069 | pkg, Binder.getCallingUid(), groupId, false); |
| 2070 | } |
| 2071 | |
| 2072 | @Override |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2073 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroups( |
| 2074 | String pkg) { |
| 2075 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2076 | return mRankingHelper.getNotificationChannelGroups( |
| 2077 | pkg, Binder.getCallingUid(), false, false); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2081 | public void deleteNotificationChannelGroup(String pkg, String groupId) { |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2082 | checkCallerIsSystemOrSameApp(pkg); |
| 2083 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2084 | final int callingUid = Binder.getCallingUid(); |
| 2085 | NotificationChannelGroup groupToDelete = |
| 2086 | mRankingHelper.getNotificationChannelGroup(groupId, pkg, callingUid); |
| 2087 | if (groupToDelete != null) { |
| 2088 | List<NotificationChannel> deletedChannels = |
| 2089 | mRankingHelper.deleteNotificationChannelGroup(pkg, callingUid, groupId); |
| 2090 | for (int i = 0; i < deletedChannels.size(); i++) { |
| 2091 | final NotificationChannel deletedChannel = deletedChannels.get(i); |
| 2092 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, deletedChannel.getId(), 0, 0, |
| 2093 | true, |
| 2094 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, |
| 2095 | null); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2096 | mListeners.notifyNotificationChannelChanged(pkg, |
| 2097 | UserHandle.getUserHandleForUid(callingUid), |
| 2098 | deletedChannel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2099 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
| 2100 | } |
| 2101 | mListeners.notifyNotificationChannelGroupChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2102 | pkg, UserHandle.getUserHandleForUid(callingUid), groupToDelete, |
| 2103 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2104 | savePolicyFile(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2105 | } |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2109 | public void updateNotificationChannelForPackage(String pkg, int uid, |
| 2110 | NotificationChannel channel) { |
Geoffrey Pitsch | 4dd5006 | 2016-12-06 16:41:22 -0500 | [diff] [blame] | 2111 | enforceSystemOrSystemUI("Caller not system or systemui"); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2112 | Preconditions.checkNotNull(channel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2113 | updateNotificationChannelInt(pkg, uid, channel, false); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | @Override |
| 2117 | public ParceledListSlice<NotificationChannel> getNotificationChannelsForPackage(String pkg, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2118 | int uid, boolean includeDeleted) { |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2119 | enforceSystemOrSystemUI("getNotificationChannelsForPackage"); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2120 | return mRankingHelper.getNotificationChannels(pkg, uid, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2124 | public int getNumNotificationChannelsForPackage(String pkg, int uid, |
| 2125 | boolean includeDeleted) { |
| 2126 | enforceSystemOrSystemUI("getNumNotificationChannelsForPackage"); |
| 2127 | return mRankingHelper.getNotificationChannels(pkg, uid, includeDeleted) |
| 2128 | .getList().size(); |
| 2129 | } |
| 2130 | |
| 2131 | @Override |
Julia Reynolds | 17717f5 | 2017-05-09 11:46:06 -0400 | [diff] [blame] | 2132 | public boolean onlyHasDefaultChannel(String pkg, int uid) { |
| 2133 | enforceSystemOrSystemUI("onlyHasDefaultChannel"); |
| 2134 | return mRankingHelper.onlyHasDefaultChannel(pkg, uid); |
| 2135 | } |
| 2136 | |
| 2137 | @Override |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 2138 | public int getDeletedChannelCount(String pkg, int uid) { |
| 2139 | enforceSystemOrSystemUI("getDeletedChannelCount"); |
| 2140 | return mRankingHelper.getDeletedChannelCount(pkg, uid); |
| 2141 | } |
| 2142 | |
| 2143 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2144 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroupsForPackage( |
| 2145 | String pkg, int uid, boolean includeDeleted) { |
| 2146 | checkCallerIsSystem(); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2147 | return mRankingHelper.getNotificationChannelGroups(pkg, uid, includeDeleted, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2151 | public NotificationChannelGroup getPopulatedNotificationChannelGroupForPackage( |
| 2152 | String pkg, int uid, String groupId, boolean includeDeleted) { |
| 2153 | enforceSystemOrSystemUI("getPopulatedNotificationChannelGroupForPackage"); |
| 2154 | return mRankingHelper.getNotificationChannelGroupWithChannels( |
| 2155 | pkg, uid, groupId, includeDeleted); |
| 2156 | } |
| 2157 | |
| 2158 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2159 | public NotificationChannelGroup getNotificationChannelGroupForPackage( |
| 2160 | String groupId, String pkg, int uid) { |
| 2161 | enforceSystemOrSystemUI("getNotificationChannelGroupForPackage"); |
| 2162 | return mRankingHelper.getNotificationChannelGroup(groupId, pkg, uid); |
| 2163 | } |
| 2164 | |
| 2165 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2166 | public ParceledListSlice<NotificationChannel> getNotificationChannels(String pkg) { |
| 2167 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2168 | return mRankingHelper.getNotificationChannels( |
| 2169 | pkg, Binder.getCallingUid(), false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2170 | } |
| 2171 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2172 | @Override |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 2173 | public ParceledListSlice<NotifyingApp> getRecentNotifyingAppsForUser(int userId) { |
| 2174 | checkCallerIsSystem(); |
| 2175 | synchronized (mNotificationLock) { |
| 2176 | List<NotifyingApp> apps = new ArrayList<>( |
| 2177 | mRecentApps.getOrDefault(userId, new ArrayList<>())); |
| 2178 | return new ParceledListSlice<>(apps); |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | @Override |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2183 | public void clearData(String packageName, int uid, boolean fromApp) throws RemoteException { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2184 | checkCallerIsSystem(); |
| 2185 | |
| 2186 | // Cancel posted notifications |
| 2187 | cancelAllNotificationsInt(MY_UID, MY_PID, packageName, null, 0, 0, true, |
| 2188 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, null); |
| 2189 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2190 | final String[] packages = new String[] {packageName}; |
| 2191 | final int[] uids = new int[] {uid}; |
| 2192 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2193 | // Listener & assistant |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2194 | mListeners.onPackagesChanged(true, packages, uids); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2195 | mAssistants.onPackagesChanged(true, packages, uids); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2196 | |
| 2197 | // Zen |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2198 | mConditionProviders.onPackagesChanged(true, packages, uids); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2199 | |
| 2200 | // Reset notification preferences |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2201 | if (!fromApp) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2202 | mRankingHelper.onPackagesChanged( |
| 2203 | true, UserHandle.getCallingUserId(), packages, uids); |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2204 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2205 | |
| 2206 | savePolicyFile(); |
| 2207 | } |
| 2208 | |
| 2209 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2210 | /** |
| 2211 | * System-only API for getting a list of current (i.e. not cleared) notifications. |
| 2212 | * |
| 2213 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2214 | * @returns A list of all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2215 | */ |
| 2216 | @Override |
| 2217 | public StatusBarNotification[] getActiveNotifications(String callingPkg) { |
| 2218 | // enforce() will ensure the calling uid has the correct permission |
| 2219 | getContext().enforceCallingOrSelfPermission( |
| 2220 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 2221 | "NotificationManagerService.getActiveNotifications"); |
| 2222 | |
| 2223 | StatusBarNotification[] tmp = null; |
| 2224 | int uid = Binder.getCallingUid(); |
| 2225 | |
| 2226 | // noteOp will check to make sure the callingPkg matches the uid |
| 2227 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 2228 | == AppOpsManager.MODE_ALLOWED) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2229 | synchronized (mNotificationLock) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2230 | tmp = new StatusBarNotification[mNotificationList.size()]; |
| 2231 | final int N = mNotificationList.size(); |
| 2232 | for (int i=0; i<N; i++) { |
| 2233 | tmp[i] = mNotificationList.get(i).sbn; |
| 2234 | } |
| 2235 | } |
| 2236 | } |
| 2237 | return tmp; |
| 2238 | } |
| 2239 | |
| 2240 | /** |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2241 | * Public API for getting a list of current notifications for the calling package/uid. |
| 2242 | * |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 2243 | * Note that since notification posting is done asynchronously, this will not return |
| 2244 | * notifications that are in the process of being posted. |
| 2245 | * |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2246 | * @returns A list of all the package's notifications, in natural order. |
| 2247 | */ |
| 2248 | @Override |
| 2249 | public ParceledListSlice<StatusBarNotification> getAppActiveNotifications(String pkg, |
| 2250 | int incomingUserId) { |
| 2251 | checkCallerIsSystemOrSameApp(pkg); |
| 2252 | int userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2253 | Binder.getCallingUid(), incomingUserId, true, false, |
| 2254 | "getAppActiveNotifications", pkg); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2255 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2256 | final ArrayMap<String, StatusBarNotification> map |
| 2257 | = new ArrayMap<>(mNotificationList.size() + mEnqueuedNotifications.size()); |
Erik Wolsheimer | 2242b4d | 2015-11-24 13:22:04 -0800 | [diff] [blame] | 2258 | final int N = mNotificationList.size(); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2259 | for (int i = 0; i < N; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2260 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 2261 | mNotificationList.get(i).sbn); |
| 2262 | if (sbn != null) { |
| 2263 | map.put(sbn.getKey(), sbn); |
| 2264 | } |
| 2265 | } |
| 2266 | for(NotificationRecord snoozed: mSnoozeHelper.getSnoozed(userId, pkg)) { |
| 2267 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, snoozed.sbn); |
| 2268 | if (sbn != null) { |
| 2269 | map.put(sbn.getKey(), sbn); |
| 2270 | } |
| 2271 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2272 | final int M = mEnqueuedNotifications.size(); |
| 2273 | for (int i = 0; i < M; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2274 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 2275 | mEnqueuedNotifications.get(i).sbn); |
| 2276 | if (sbn != null) { |
| 2277 | map.put(sbn.getKey(), sbn); // pending update overwrites existing post here |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2278 | } |
| 2279 | } |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2280 | final ArrayList<StatusBarNotification> list = new ArrayList<>(map.size()); |
| 2281 | list.addAll(map.values()); |
| 2282 | return new ParceledListSlice<StatusBarNotification>(list); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2283 | } |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2284 | } |
| 2285 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2286 | private StatusBarNotification sanitizeSbn(String pkg, int userId, |
| 2287 | StatusBarNotification sbn) { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 2288 | if (sbn.getPackageName().equals(pkg) && sbn.getUserId() == userId) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2289 | // We could pass back a cloneLight() but clients might get confused and |
| 2290 | // try to send this thing back to notify() again, which would not work |
| 2291 | // very well. |
| 2292 | return new StatusBarNotification( |
| 2293 | sbn.getPackageName(), |
| 2294 | sbn.getOpPkg(), |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2295 | sbn.getId(), sbn.getTag(), sbn.getUid(), sbn.getInitialPid(), |
| 2296 | sbn.getNotification().clone(), |
| 2297 | sbn.getUser(), sbn.getOverrideGroupKey(), sbn.getPostTime()); |
| 2298 | } |
| 2299 | return null; |
| 2300 | } |
| 2301 | |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2302 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2303 | * System-only API for getting a list of recent (cleared, no longer shown) notifications. |
| 2304 | * |
| 2305 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
| 2306 | */ |
| 2307 | @Override |
| 2308 | public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) { |
| 2309 | // enforce() will ensure the calling uid has the correct permission |
| 2310 | getContext().enforceCallingOrSelfPermission( |
| 2311 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 2312 | "NotificationManagerService.getHistoricalNotifications"); |
| 2313 | |
| 2314 | StatusBarNotification[] tmp = null; |
| 2315 | int uid = Binder.getCallingUid(); |
| 2316 | |
| 2317 | // noteOp will check to make sure the callingPkg matches the uid |
| 2318 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 2319 | == AppOpsManager.MODE_ALLOWED) { |
| 2320 | synchronized (mArchive) { |
| 2321 | tmp = mArchive.getArray(count); |
| 2322 | } |
| 2323 | } |
| 2324 | return tmp; |
| 2325 | } |
| 2326 | |
| 2327 | /** |
| 2328 | * Register a listener binder directly with the notification manager. |
| 2329 | * |
| 2330 | * Only works with system callers. Apps should extend |
| 2331 | * {@link android.service.notification.NotificationListenerService}. |
| 2332 | */ |
| 2333 | @Override |
| 2334 | public void registerListener(final INotificationListener listener, |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 2335 | final ComponentName component, final int userid) { |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 2336 | enforceSystemOrSystemUI("INotificationManager.registerListener"); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 2337 | mListeners.registerService(listener, component, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2338 | } |
| 2339 | |
| 2340 | /** |
| 2341 | * Remove a listener binder directly |
| 2342 | */ |
| 2343 | @Override |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 2344 | public void unregisterListener(INotificationListener token, int userid) { |
Chris Wren | b7c8109 | 2016-03-10 11:41:10 -0500 | [diff] [blame] | 2345 | mListeners.unregisterService(token, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2346 | } |
| 2347 | |
| 2348 | /** |
| 2349 | * Allow an INotificationListener to simulate a "clear all" operation. |
| 2350 | * |
| 2351 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications} |
| 2352 | * |
| 2353 | * @param token The binder for the listener, to check that the caller is allowed |
| 2354 | */ |
| 2355 | @Override |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2356 | public void cancelNotificationsFromListener(INotificationListener token, String[] keys) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2357 | final int callingUid = Binder.getCallingUid(); |
| 2358 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2359 | long identity = Binder.clearCallingIdentity(); |
| 2360 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2361 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2362 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2363 | if (keys != null) { |
| 2364 | final int N = keys.length; |
| 2365 | for (int i = 0; i < N; i++) { |
| 2366 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 2367 | if (r == null) continue; |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2368 | final int userId = r.sbn.getUserId(); |
| 2369 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 2370 | !mUserProfiles.isCurrentProfile(userId)) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2371 | throw new SecurityException("Disallowed call from listener: " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2372 | + info.service); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2373 | } |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 2374 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2375 | r.sbn.getPackageName(), r.sbn.getTag(), r.sbn.getId(), |
| 2376 | userId); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2377 | } |
| 2378 | } else { |
| 2379 | cancelAllLocked(callingUid, callingPid, info.userid, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2380 | REASON_LISTENER_CANCEL_ALL, info, info.supportsProfiles()); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2381 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2382 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2383 | } finally { |
| 2384 | Binder.restoreCallingIdentity(identity); |
| 2385 | } |
| 2386 | } |
| 2387 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2388 | /** |
| 2389 | * Handle request from an approved listener to re-enable itself. |
| 2390 | * |
| 2391 | * @param component The componenet to be re-enabled, caller must match package. |
| 2392 | */ |
| 2393 | @Override |
| 2394 | public void requestBindListener(ComponentName component) { |
| 2395 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 2396 | long identity = Binder.clearCallingIdentity(); |
| 2397 | try { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2398 | ManagedServices manager = |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2399 | mAssistants.isComponentEnabledForCurrentProfiles(component) |
| 2400 | ? mAssistants |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2401 | : mListeners; |
| 2402 | manager.setComponentState(component, true); |
| 2403 | } finally { |
| 2404 | Binder.restoreCallingIdentity(identity); |
| 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | @Override |
| 2409 | public void requestUnbindListener(INotificationListener token) { |
| 2410 | long identity = Binder.clearCallingIdentity(); |
| 2411 | try { |
| 2412 | // allow bound services to disable themselves |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2413 | synchronized (mNotificationLock) { |
| 2414 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2415 | info.getOwner().setComponentState(info.component, false); |
| 2416 | } |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2417 | } finally { |
| 2418 | Binder.restoreCallingIdentity(identity); |
| 2419 | } |
| 2420 | } |
| 2421 | |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2422 | @Override |
| 2423 | public void setNotificationsShownFromListener(INotificationListener token, String[] keys) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2424 | long identity = Binder.clearCallingIdentity(); |
| 2425 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2426 | synchronized (mNotificationLock) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2427 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2428 | if (keys != null) { |
| 2429 | final int N = keys.length; |
| 2430 | for (int i = 0; i < N; i++) { |
| 2431 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
| 2432 | if (r == null) continue; |
| 2433 | final int userId = r.sbn.getUserId(); |
| 2434 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
| 2435 | !mUserProfiles.isCurrentProfile(userId)) { |
| 2436 | throw new SecurityException("Disallowed call from listener: " |
| 2437 | + info.service); |
| 2438 | } |
| 2439 | if (!r.isSeen()) { |
| 2440 | if (DBG) Slog.d(TAG, "Marking notification as seen " + keys[i]); |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 2441 | reportSeen(r); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2442 | r.setSeen(); |
| 2443 | } |
| 2444 | } |
| 2445 | } |
| 2446 | } |
| 2447 | } finally { |
| 2448 | Binder.restoreCallingIdentity(identity); |
| 2449 | } |
| 2450 | } |
| 2451 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2452 | /** |
| 2453 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2454 | * |
| 2455 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2456 | * |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2457 | * @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] | 2458 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2459 | @GuardedBy("mNotificationLock") |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2460 | private void cancelNotificationFromListenerLocked(ManagedServiceInfo info, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2461 | int callingUid, int callingPid, String pkg, String tag, int id, int userId) { |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2462 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
| 2463 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
| 2464 | true, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2465 | userId, REASON_LISTENER_CANCEL, info); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2466 | } |
| 2467 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2468 | /** |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2469 | * Allow an INotificationListener to snooze a single notification until a context. |
| 2470 | * |
| 2471 | * @param token The binder for the listener, to check that the caller is allowed |
| 2472 | */ |
| 2473 | @Override |
| 2474 | public void snoozeNotificationUntilContextFromListener(INotificationListener token, |
| 2475 | String key, String snoozeCriterionId) { |
| 2476 | long identity = Binder.clearCallingIdentity(); |
| 2477 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2478 | synchronized (mNotificationLock) { |
| 2479 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2480 | snoozeNotificationInt(key, SNOOZE_UNTIL_UNSPECIFIED, snoozeCriterionId, info); |
| 2481 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2482 | } finally { |
| 2483 | Binder.restoreCallingIdentity(identity); |
| 2484 | } |
| 2485 | } |
| 2486 | |
| 2487 | /** |
| 2488 | * Allow an INotificationListener to snooze a single notification until a time. |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2489 | * |
| 2490 | * @param token The binder for the listener, to check that the caller is allowed |
| 2491 | */ |
| 2492 | @Override |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2493 | public void snoozeNotificationUntilFromListener(INotificationListener token, String key, |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 2494 | long duration) { |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2495 | long identity = Binder.clearCallingIdentity(); |
| 2496 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2497 | synchronized (mNotificationLock) { |
| 2498 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2499 | snoozeNotificationInt(key, duration, null, info); |
| 2500 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2501 | } finally { |
| 2502 | Binder.restoreCallingIdentity(identity); |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | /** |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2507 | * Allows the notification assistant to un-snooze a single notification. |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2508 | * |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2509 | * @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] | 2510 | */ |
| 2511 | @Override |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2512 | public void unsnoozeNotificationFromAssistant(INotificationListener token, String key) { |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2513 | long identity = Binder.clearCallingIdentity(); |
| 2514 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2515 | synchronized (mNotificationLock) { |
| 2516 | final ManagedServiceInfo info = |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2517 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2518 | unsnoozeNotificationInt(key, info); |
| 2519 | } |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2520 | } finally { |
| 2521 | Binder.restoreCallingIdentity(identity); |
| 2522 | } |
| 2523 | } |
| 2524 | |
| 2525 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2526 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2527 | * |
| 2528 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2529 | * |
| 2530 | * @param token The binder for the listener, to check that the caller is allowed |
| 2531 | */ |
| 2532 | @Override |
| 2533 | public void cancelNotificationFromListener(INotificationListener token, String pkg, |
| 2534 | String tag, int id) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2535 | final int callingUid = Binder.getCallingUid(); |
| 2536 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2537 | long identity = Binder.clearCallingIdentity(); |
| 2538 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2539 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2540 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2541 | if (info.supportsProfiles()) { |
| 2542 | Log.e(TAG, "Ignoring deprecated cancelNotification(pkg, tag, id) " |
| 2543 | + "from " + info.component |
| 2544 | + " use cancelNotification(key) instead."); |
| 2545 | } else { |
| 2546 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2547 | pkg, tag, id, info.userid); |
| 2548 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2549 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2550 | } finally { |
| 2551 | Binder.restoreCallingIdentity(identity); |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | /** |
| 2556 | * Allow an INotificationListener to request the list of outstanding notifications seen by |
| 2557 | * the current user. Useful when starting up, after which point the listener callbacks |
| 2558 | * should be used. |
| 2559 | * |
| 2560 | * @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] | 2561 | * @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] | 2562 | * @returns The return value will contain the notifications specified in keys, in that |
| 2563 | * order, or if keys is null, all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2564 | */ |
| 2565 | @Override |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2566 | public ParceledListSlice<StatusBarNotification> getActiveNotificationsFromListener( |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2567 | INotificationListener token, String[] keys, int trim) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2568 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2569 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2570 | final boolean getKeys = keys != null; |
| 2571 | final int N = getKeys ? keys.length : mNotificationList.size(); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2572 | final ArrayList<StatusBarNotification> list |
| 2573 | = new ArrayList<StatusBarNotification>(N); |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2574 | for (int i=0; i<N; i++) { |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2575 | final NotificationRecord r = getKeys |
| 2576 | ? mNotificationsByKey.get(keys[i]) |
| 2577 | : mNotificationList.get(i); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2578 | if (r == null) continue; |
| 2579 | StatusBarNotification sbn = r.sbn; |
| 2580 | if (!isVisibleToListener(sbn, info)) continue; |
| 2581 | StatusBarNotification sbnToSend = |
| 2582 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2583 | list.add(sbnToSend); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2584 | } |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2585 | return new ParceledListSlice<StatusBarNotification>(list); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2586 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2587 | } |
| 2588 | |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2589 | /** |
| 2590 | * Allow an INotificationListener to request the list of outstanding snoozed notifications |
| 2591 | * seen by the current user. Useful when starting up, after which point the listener |
| 2592 | * callbacks should be used. |
| 2593 | * |
| 2594 | * @param token The binder for the listener, to check that the caller is allowed |
| 2595 | * @returns The return value will contain the notifications specified in keys, in that |
| 2596 | * order, or if keys is null, all the notifications, in natural order. |
| 2597 | */ |
| 2598 | @Override |
| 2599 | public ParceledListSlice<StatusBarNotification> getSnoozedNotificationsFromListener( |
| 2600 | INotificationListener token, int trim) { |
| 2601 | synchronized (mNotificationLock) { |
| 2602 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2603 | List<NotificationRecord> snoozedRecords = mSnoozeHelper.getSnoozed(); |
| 2604 | final int N = snoozedRecords.size(); |
| 2605 | final ArrayList<StatusBarNotification> list = new ArrayList<>(N); |
| 2606 | for (int i=0; i < N; i++) { |
| 2607 | final NotificationRecord r = snoozedRecords.get(i); |
| 2608 | if (r == null) continue; |
| 2609 | StatusBarNotification sbn = r.sbn; |
| 2610 | if (!isVisibleToListener(sbn, info)) continue; |
| 2611 | StatusBarNotification sbnToSend = |
| 2612 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2613 | list.add(sbnToSend); |
| 2614 | } |
| 2615 | return new ParceledListSlice<>(list); |
| 2616 | } |
| 2617 | } |
| 2618 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2619 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2620 | public void requestHintsFromListener(INotificationListener token, int hints) { |
| 2621 | final long identity = Binder.clearCallingIdentity(); |
| 2622 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2623 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2624 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2625 | final int disableEffectsMask = HINT_HOST_DISABLE_EFFECTS |
| 2626 | | HINT_HOST_DISABLE_NOTIFICATION_EFFECTS |
| 2627 | | HINT_HOST_DISABLE_CALL_EFFECTS; |
| 2628 | final boolean disableEffects = (hints & disableEffectsMask) != 0; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2629 | if (disableEffects) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2630 | addDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2631 | } else { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2632 | removeDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2633 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2634 | updateListenerHintsLocked(); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2635 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2636 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2637 | } finally { |
| 2638 | Binder.restoreCallingIdentity(identity); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2643 | public int getHintsFromListener(INotificationListener token) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2644 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2645 | return mListenerHints; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | @Override |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2650 | public void requestInterruptionFilterFromListener(INotificationListener token, |
| 2651 | int interruptionFilter) throws RemoteException { |
| 2652 | final long identity = Binder.clearCallingIdentity(); |
| 2653 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2654 | synchronized (mNotificationLock) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2655 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2656 | mZenModeHelper.requestFromListener(info.component, interruptionFilter); |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2657 | updateInterruptionFilterLocked(); |
| 2658 | } |
| 2659 | } finally { |
| 2660 | Binder.restoreCallingIdentity(identity); |
| 2661 | } |
| 2662 | } |
| 2663 | |
| 2664 | @Override |
| 2665 | public int getInterruptionFilterFromListener(INotificationListener token) |
| 2666 | throws RemoteException { |
| 2667 | synchronized (mNotificationLight) { |
| 2668 | return mInterruptionFilter; |
| 2669 | } |
| 2670 | } |
| 2671 | |
| 2672 | @Override |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2673 | public void setOnNotificationPostedTrimFromListener(INotificationListener token, int trim) |
| 2674 | throws RemoteException { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2675 | synchronized (mNotificationLock) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2676 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2677 | if (info == null) return; |
| 2678 | mListeners.setOnNotificationPostedTrimLocked(info, trim); |
| 2679 | } |
| 2680 | } |
| 2681 | |
| 2682 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2683 | public int getZenMode() { |
| 2684 | return mZenModeHelper.getZenMode(); |
| 2685 | } |
| 2686 | |
| 2687 | @Override |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 2688 | public ZenModeConfig getZenModeConfig() { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2689 | enforceSystemOrSystemUI("INotificationManager.getZenModeConfig"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 2690 | return mZenModeHelper.getConfig(); |
| 2691 | } |
| 2692 | |
| 2693 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2694 | public void setZenMode(int mode, Uri conditionId, String reason) throws RemoteException { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2695 | enforceSystemOrSystemUI("INotificationManager.setZenMode"); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2696 | final long identity = Binder.clearCallingIdentity(); |
| 2697 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 2698 | mZenModeHelper.setManualZenMode(mode, conditionId, null, reason); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2699 | } finally { |
| 2700 | Binder.restoreCallingIdentity(identity); |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2705 | public List<ZenModeConfig.ZenRule> getZenRules() throws RemoteException { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2706 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRules"); |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2707 | return mZenModeHelper.getZenRules(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2711 | public AutomaticZenRule getAutomaticZenRule(String id) throws RemoteException { |
| 2712 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2713 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRule"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2714 | return mZenModeHelper.getAutomaticZenRule(id); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2718 | public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2719 | throws RemoteException { |
| 2720 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 2721 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 2722 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 2723 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2724 | enforcePolicyAccess(Binder.getCallingUid(), "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2725 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2726 | return mZenModeHelper.addAutomaticZenRule(automaticZenRule, |
| 2727 | "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2731 | public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2732 | throws RemoteException { |
| 2733 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 2734 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 2735 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 2736 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
| 2737 | enforcePolicyAccess(Binder.getCallingUid(), "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2738 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2739 | return mZenModeHelper.updateAutomaticZenRule(id, automaticZenRule, |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2740 | "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2744 | public boolean removeAutomaticZenRule(String id) throws RemoteException { |
| 2745 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2746 | // Verify that they can modify zen rules. |
| 2747 | enforcePolicyAccess(Binder.getCallingUid(), "removeAutomaticZenRule"); |
| 2748 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2749 | return mZenModeHelper.removeAutomaticZenRule(id, "removeAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2750 | } |
| 2751 | |
| 2752 | @Override |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 2753 | public boolean removeAutomaticZenRules(String packageName) throws RemoteException { |
| 2754 | Preconditions.checkNotNull(packageName, "Package name is null"); |
| 2755 | enforceSystemOrSystemUI("removeAutomaticZenRules"); |
| 2756 | |
| 2757 | return mZenModeHelper.removeAutomaticZenRules(packageName, "removeAutomaticZenRules"); |
| 2758 | } |
| 2759 | |
| 2760 | @Override |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 2761 | public int getRuleInstanceCount(ComponentName owner) throws RemoteException { |
| 2762 | Preconditions.checkNotNull(owner, "Owner is null"); |
| 2763 | enforceSystemOrSystemUI("getRuleInstanceCount"); |
| 2764 | |
| 2765 | return mZenModeHelper.getCurrentInstanceCount(owner); |
| 2766 | } |
| 2767 | |
| 2768 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2769 | public void setInterruptionFilter(String pkg, int filter) throws RemoteException { |
| 2770 | enforcePolicyAccess(pkg, "setInterruptionFilter"); |
| 2771 | final int zen = NotificationManager.zenModeFromInterruptionFilter(filter, -1); |
| 2772 | if (zen == -1) throw new IllegalArgumentException("Invalid filter: " + filter); |
| 2773 | final long identity = Binder.clearCallingIdentity(); |
| 2774 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 2775 | mZenModeHelper.setManualZenMode(zen, null, pkg, "setInterruptionFilter"); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2776 | } finally { |
| 2777 | Binder.restoreCallingIdentity(identity); |
| 2778 | } |
| 2779 | } |
| 2780 | |
| 2781 | @Override |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 2782 | public void notifyConditions(final String pkg, IConditionProvider provider, |
| 2783 | final Condition[] conditions) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2784 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 2785 | checkCallerIsSystemOrSameApp(pkg); |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 2786 | mHandler.post(new Runnable() { |
| 2787 | @Override |
| 2788 | public void run() { |
| 2789 | mConditionProviders.notifyConditions(pkg, info, conditions); |
| 2790 | } |
| 2791 | }); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2792 | } |
| 2793 | |
Julia Reynolds | 38e6ca4 | 2016-08-08 08:38:09 -0400 | [diff] [blame] | 2794 | @Override |
| 2795 | public void requestUnbindProvider(IConditionProvider provider) { |
| 2796 | long identity = Binder.clearCallingIdentity(); |
| 2797 | try { |
| 2798 | // allow bound services to disable themselves |
| 2799 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 2800 | info.getOwner().setComponentState(info.component, false); |
| 2801 | } finally { |
| 2802 | Binder.restoreCallingIdentity(identity); |
| 2803 | } |
| 2804 | } |
| 2805 | |
| 2806 | @Override |
| 2807 | public void requestBindProvider(ComponentName component) { |
| 2808 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 2809 | long identity = Binder.clearCallingIdentity(); |
| 2810 | try { |
| 2811 | mConditionProviders.setComponentState(component, true); |
| 2812 | } finally { |
| 2813 | Binder.restoreCallingIdentity(identity); |
| 2814 | } |
| 2815 | } |
| 2816 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2817 | private void enforceSystemOrSystemUI(String message) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 2818 | if (isCallerSystemOrPhone()) return; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2819 | getContext().enforceCallingPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 2820 | message); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2821 | } |
| 2822 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 2823 | private void enforceSystemOrSystemUIOrSamePackage(String pkg, String message) { |
| 2824 | try { |
| 2825 | checkCallerIsSystemOrSameApp(pkg); |
| 2826 | } catch (SecurityException e) { |
| 2827 | getContext().enforceCallingPermission( |
| 2828 | android.Manifest.permission.STATUS_BAR_SERVICE, |
| 2829 | message); |
| 2830 | } |
| 2831 | } |
| 2832 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2833 | private void enforcePolicyAccess(int uid, String method) { |
| 2834 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 2835 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 2836 | return; |
| 2837 | } |
| 2838 | boolean accessAllowed = false; |
| 2839 | String[] packages = getContext().getPackageManager().getPackagesForUid(uid); |
| 2840 | final int packageCount = packages.length; |
| 2841 | for (int i = 0; i < packageCount; i++) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2842 | if (mConditionProviders.isPackageOrComponentAllowed( |
| 2843 | packages[i], UserHandle.getUserId(uid))) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2844 | accessAllowed = true; |
| 2845 | } |
| 2846 | } |
| 2847 | if (!accessAllowed) { |
| 2848 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 2849 | throw new SecurityException("Notification policy access denied"); |
| 2850 | } |
| 2851 | } |
| 2852 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2853 | private void enforcePolicyAccess(String pkg, String method) { |
Julia Reynolds | 6ee2617 | 2015-09-28 11:34:48 -0400 | [diff] [blame] | 2854 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 2855 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 2856 | return; |
| 2857 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 2858 | checkCallerIsSameApp(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2859 | if (!checkPolicyAccess(pkg)) { |
| 2860 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 2861 | throw new SecurityException("Notification policy access denied"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2862 | } |
| 2863 | } |
| 2864 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2865 | private boolean checkPackagePolicyAccess(String pkg) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2866 | return mConditionProviders.isPackageOrComponentAllowed( |
| 2867 | pkg, getCallingUserHandle().getIdentifier()); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | private boolean checkPolicyAccess(String pkg) { |
Julia Reynolds | 0867b3a | 2016-03-30 17:29:54 -0400 | [diff] [blame] | 2871 | try { |
| 2872 | int uid = getContext().getPackageManager().getPackageUidAsUser( |
| 2873 | pkg, UserHandle.getCallingUserId()); |
| 2874 | if (PackageManager.PERMISSION_GRANTED == ActivityManager.checkComponentPermission( |
| 2875 | android.Manifest.permission.MANAGE_NOTIFICATIONS, uid, |
| 2876 | -1, true)) { |
| 2877 | return true; |
| 2878 | } |
| 2879 | } catch (NameNotFoundException e) { |
| 2880 | return false; |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 2881 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2882 | return checkPackagePolicyAccess(pkg) || mListeners.isComponentEnabledForPackage(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2883 | } |
| 2884 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2885 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2886 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2887 | if (!DumpUtils.checkDumpAndUsageStatsPermission(getContext(), TAG, pw)) return; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2888 | final DumpFilter filter = DumpFilter.parseFromArguments(args); |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 2889 | if (filter.stats) { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2890 | dumpJson(pw, filter); |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 2891 | } else if (filter.proto) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 2892 | dumpProto(fd, filter); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2893 | } else { |
| 2894 | dumpImpl(pw, filter); |
| 2895 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2896 | } |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2897 | |
| 2898 | @Override |
| 2899 | public ComponentName getEffectsSuppressor() { |
Bryce Lee | ba3d895 | 2016-04-12 12:39:15 -0700 | [diff] [blame] | 2900 | return !mEffectsSuppressors.isEmpty() ? mEffectsSuppressors.get(0) : null; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2901 | } |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 2902 | |
| 2903 | @Override |
| 2904 | public boolean matchesCallFilter(Bundle extras) { |
| 2905 | enforceSystemOrSystemUI("INotificationManager.matchesCallFilter"); |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 2906 | return mZenModeHelper.matchesCallFilter( |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 2907 | Binder.getCallingUserHandle(), |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 2908 | extras, |
| 2909 | mRankingHelper.findExtractor(ValidateNotificationPeople.class), |
| 2910 | MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS, |
| 2911 | MATCHES_CALL_FILTER_TIMEOUT_AFFINITY); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 2912 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 2913 | |
| 2914 | @Override |
| 2915 | public boolean isSystemConditionProviderEnabled(String path) { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2916 | enforceSystemOrSystemUI("INotificationManager.isSystemConditionProviderEnabled"); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2917 | return mConditionProviders.isSystemProviderEnabled(path); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 2918 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2919 | |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 2920 | // Backup/restore interface |
| 2921 | @Override |
| 2922 | public byte[] getBackupPayload(int user) { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 2923 | checkCallerIsSystem(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2924 | if (DBG) Slog.d(TAG, "getBackupPayload u=" + user); |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 2925 | //TODO: http://b/22388012 |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 2926 | if (user != USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2927 | Slog.w(TAG, "getBackupPayload: cannot backup policy for user " + user); |
| 2928 | return null; |
| 2929 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 2930 | synchronized(mPolicyFile) { |
| 2931 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 2932 | try { |
| 2933 | writePolicyXml(baos, true /*forBackup*/); |
| 2934 | return baos.toByteArray(); |
| 2935 | } catch (IOException e) { |
| 2936 | Slog.w(TAG, "getBackupPayload: error writing payload for user " + user, e); |
| 2937 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2938 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 2939 | return null; |
| 2940 | } |
| 2941 | |
| 2942 | @Override |
| 2943 | public void applyRestore(byte[] payload, int user) { |
Julia Reynolds | d78263d | 2018-01-30 10:40:41 -0500 | [diff] [blame] | 2944 | checkCallerIsSystem(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2945 | if (DBG) Slog.d(TAG, "applyRestore u=" + user + " payload=" |
| 2946 | + (payload != null ? new String(payload, StandardCharsets.UTF_8) : null)); |
| 2947 | if (payload == null) { |
| 2948 | Slog.w(TAG, "applyRestore: no payload to restore for user " + user); |
| 2949 | return; |
| 2950 | } |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 2951 | //TODO: http://b/22388012 |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 2952 | if (user != USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2953 | Slog.w(TAG, "applyRestore: cannot restore policy for user " + user); |
| 2954 | return; |
| 2955 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 2956 | synchronized(mPolicyFile) { |
| 2957 | final ByteArrayInputStream bais = new ByteArrayInputStream(payload); |
| 2958 | try { |
| 2959 | readPolicyXml(bais, true /*forRestore*/); |
| 2960 | savePolicyFile(); |
| 2961 | } catch (NumberFormatException | XmlPullParserException | IOException e) { |
| 2962 | Slog.w(TAG, "applyRestore: error reading payload", e); |
| 2963 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2964 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 2965 | } |
| 2966 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2967 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2968 | public boolean isNotificationPolicyAccessGranted(String pkg) { |
| 2969 | return checkPolicyAccess(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | @Override |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 2973 | public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {; |
| 2974 | enforceSystemOrSystemUIOrSamePackage(pkg, |
| 2975 | "request policy access status for another package"); |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 2976 | return checkPolicyAccess(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2980 | public void setNotificationPolicyAccessGranted(String pkg, boolean granted) |
| 2981 | throws RemoteException { |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2982 | setNotificationPolicyAccessGrantedForUser( |
| 2983 | pkg, getCallingUserHandle().getIdentifier(), granted); |
| 2984 | } |
| 2985 | |
| 2986 | @Override |
| 2987 | public void setNotificationPolicyAccessGrantedForUser( |
| 2988 | String pkg, int userId, boolean granted) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2989 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 2990 | final long identity = Binder.clearCallingIdentity(); |
| 2991 | try { |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 2992 | if (canUseManagedServices()) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 2993 | mConditionProviders.setPackageOrComponentEnabled( |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2994 | pkg, userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2995 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 2996 | getContext().sendBroadcastAsUser(new Intent( |
| 2997 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 2998 | .setPackage(pkg) |
| 2999 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3000 | UserHandle.of(userId), null); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3001 | savePolicyFile(); |
| 3002 | } |
| 3003 | } finally { |
| 3004 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3005 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | @Override |
| 3009 | public Policy getNotificationPolicy(String pkg) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3010 | final long identity = Binder.clearCallingIdentity(); |
| 3011 | try { |
| 3012 | return mZenModeHelper.getNotificationPolicy(); |
| 3013 | } finally { |
| 3014 | Binder.restoreCallingIdentity(identity); |
| 3015 | } |
| 3016 | } |
| 3017 | |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3018 | /** |
| 3019 | * Sets the notification policy. Apps that target API levels below |
| 3020 | * {@link android.os.Build.VERSION_CODES#P} cannot make DND silence |
| 3021 | * {@link Policy#PRIORITY_CATEGORY_ALARMS} or |
| 3022 | * {@link Policy#PRIORITY_CATEGORY_MEDIA_SYSTEM_OTHER} |
| 3023 | */ |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3024 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3025 | public void setNotificationPolicy(String pkg, Policy policy) { |
| 3026 | enforcePolicyAccess(pkg, "setNotificationPolicy"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3027 | final long identity = Binder.clearCallingIdentity(); |
| 3028 | try { |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3029 | final ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(pkg, |
| 3030 | 0, UserHandle.getUserId(MY_UID)); |
| 3031 | |
| 3032 | if (applicationInfo.targetSdkVersion <= Build.VERSION_CODES.O_MR1) { |
| 3033 | Policy currPolicy = mZenModeHelper.getNotificationPolicy(); |
| 3034 | |
| 3035 | int priorityCategories = policy.priorityCategories |
| 3036 | | (currPolicy.priorityCategories & Policy.PRIORITY_CATEGORY_ALARMS) |
| 3037 | | (currPolicy.priorityCategories & |
| 3038 | Policy.PRIORITY_CATEGORY_MEDIA_SYSTEM_OTHER); |
| 3039 | policy = new Policy(priorityCategories, |
| 3040 | policy.priorityCallSenders, policy.priorityMessageSenders, |
| 3041 | policy.suppressedVisualEffects); |
| 3042 | } |
| 3043 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3044 | mZenModeHelper.setNotificationPolicy(policy); |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 3045 | } catch (RemoteException e) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3046 | } finally { |
| 3047 | Binder.restoreCallingIdentity(identity); |
| 3048 | } |
| 3049 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3050 | |
| 3051 | @Override |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3052 | public List<String> getEnabledNotificationListenerPackages() { |
| 3053 | checkCallerIsSystem(); |
| 3054 | return mListeners.getAllowedPackages(getCallingUserHandle().getIdentifier()); |
| 3055 | } |
| 3056 | |
| 3057 | @Override |
| 3058 | public List<ComponentName> getEnabledNotificationListeners(int userId) { |
| 3059 | checkCallerIsSystem(); |
| 3060 | return mListeners.getAllowedComponents(userId); |
| 3061 | } |
| 3062 | |
| 3063 | @Override |
| 3064 | public boolean isNotificationListenerAccessGranted(ComponentName listener) { |
| 3065 | Preconditions.checkNotNull(listener); |
| 3066 | checkCallerIsSystemOrSameApp(listener.getPackageName()); |
| 3067 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 3068 | getCallingUserHandle().getIdentifier()); |
| 3069 | } |
| 3070 | |
| 3071 | @Override |
| 3072 | public boolean isNotificationListenerAccessGrantedForUser(ComponentName listener, |
| 3073 | int userId) { |
| 3074 | Preconditions.checkNotNull(listener); |
| 3075 | checkCallerIsSystem(); |
| 3076 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 3077 | userId); |
| 3078 | } |
| 3079 | |
| 3080 | @Override |
| 3081 | public boolean isNotificationAssistantAccessGranted(ComponentName assistant) { |
| 3082 | Preconditions.checkNotNull(assistant); |
| 3083 | checkCallerIsSystemOrSameApp(assistant.getPackageName()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3084 | return mAssistants.isPackageOrComponentAllowed(assistant.flattenToString(), |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3085 | getCallingUserHandle().getIdentifier()); |
| 3086 | } |
| 3087 | |
| 3088 | @Override |
| 3089 | public void setNotificationListenerAccessGranted(ComponentName listener, |
| 3090 | boolean granted) throws RemoteException { |
| 3091 | setNotificationListenerAccessGrantedForUser( |
| 3092 | listener, getCallingUserHandle().getIdentifier(), granted); |
| 3093 | } |
| 3094 | |
| 3095 | @Override |
| 3096 | public void setNotificationAssistantAccessGranted(ComponentName assistant, |
| 3097 | boolean granted) throws RemoteException { |
| 3098 | setNotificationAssistantAccessGrantedForUser( |
| 3099 | assistant, getCallingUserHandle().getIdentifier(), granted); |
| 3100 | } |
| 3101 | |
| 3102 | @Override |
| 3103 | public void setNotificationListenerAccessGrantedForUser(ComponentName listener, int userId, |
| 3104 | boolean granted) throws RemoteException { |
| 3105 | Preconditions.checkNotNull(listener); |
Julia Reynolds | 0d21764 | 2017-08-11 11:26:04 -0400 | [diff] [blame] | 3106 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3107 | final long identity = Binder.clearCallingIdentity(); |
| 3108 | try { |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3109 | if (canUseManagedServices()) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3110 | mConditionProviders.setPackageOrComponentEnabled(listener.flattenToString(), |
| 3111 | userId, false, granted); |
| 3112 | mListeners.setPackageOrComponentEnabled(listener.flattenToString(), |
| 3113 | userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3114 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3115 | getContext().sendBroadcastAsUser(new Intent( |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3116 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3117 | .setPackage(listener.getPackageName()) |
| 3118 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3119 | UserHandle.of(userId), null); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3120 | |
| 3121 | savePolicyFile(); |
| 3122 | } |
| 3123 | } finally { |
| 3124 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3125 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | @Override |
| 3129 | public void setNotificationAssistantAccessGrantedForUser(ComponentName assistant, |
| 3130 | int userId, boolean granted) throws RemoteException { |
| 3131 | Preconditions.checkNotNull(assistant); |
Julia Reynolds | 0d21764 | 2017-08-11 11:26:04 -0400 | [diff] [blame] | 3132 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3133 | final long identity = Binder.clearCallingIdentity(); |
| 3134 | try { |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3135 | if (canUseManagedServices()) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3136 | mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(), |
| 3137 | userId, false, granted); |
| 3138 | mAssistants.setPackageOrComponentEnabled(assistant.flattenToString(), |
| 3139 | userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3140 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3141 | getContext().sendBroadcastAsUser(new Intent( |
| 3142 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 3143 | .setPackage(assistant.getPackageName()) |
| 3144 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3145 | UserHandle.of(userId), null); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3146 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3147 | savePolicyFile(); |
| 3148 | } |
| 3149 | } finally { |
| 3150 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3151 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | @Override |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3155 | public void applyEnqueuedAdjustmentFromAssistant(INotificationListener token, |
| 3156 | Adjustment adjustment) throws RemoteException { |
| 3157 | final long identity = Binder.clearCallingIdentity(); |
| 3158 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3159 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3160 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3161 | int N = mEnqueuedNotifications.size(); |
| 3162 | for (int i = 0; i < N; i++) { |
| 3163 | final NotificationRecord n = mEnqueuedNotifications.get(i); |
| 3164 | if (Objects.equals(adjustment.getKey(), n.getKey()) |
| 3165 | && Objects.equals(adjustment.getUser(), n.getUserId())) { |
| 3166 | applyAdjustment(n, adjustment); |
| 3167 | break; |
| 3168 | } |
| 3169 | } |
| 3170 | } |
| 3171 | } finally { |
| 3172 | Binder.restoreCallingIdentity(identity); |
| 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 3177 | public void applyAdjustmentFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3178 | Adjustment adjustment) throws RemoteException { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3179 | final long identity = Binder.clearCallingIdentity(); |
| 3180 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3181 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3182 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3183 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 3184 | applyAdjustment(n, adjustment); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3185 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3186 | mRankingHandler.requestSort(); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3187 | } finally { |
| 3188 | Binder.restoreCallingIdentity(identity); |
| 3189 | } |
| 3190 | } |
| 3191 | |
| 3192 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 3193 | public void applyAdjustmentsFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3194 | List<Adjustment> adjustments) throws RemoteException { |
| 3195 | |
| 3196 | final long identity = Binder.clearCallingIdentity(); |
| 3197 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3198 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3199 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3200 | for (Adjustment adjustment : adjustments) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3201 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 3202 | applyAdjustment(n, adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3203 | } |
| 3204 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3205 | mRankingHandler.requestSort(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3206 | } finally { |
| 3207 | Binder.restoreCallingIdentity(identity); |
| 3208 | } |
| 3209 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3210 | |
| 3211 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 3212 | public void updateNotificationChannelGroupFromPrivilegedListener( |
| 3213 | INotificationListener token, String pkg, UserHandle user, |
| 3214 | NotificationChannelGroup group) throws RemoteException { |
| 3215 | Preconditions.checkNotNull(user); |
| 3216 | verifyPrivilegedListener(token, user); |
| 3217 | createNotificationChannelGroup( |
| 3218 | pkg, getUidForPackageAndUser(pkg, user), group, false, true); |
| 3219 | savePolicyFile(); |
| 3220 | } |
| 3221 | |
| 3222 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3223 | public void updateNotificationChannelFromPrivilegedListener(INotificationListener token, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3224 | String pkg, UserHandle user, NotificationChannel channel) throws RemoteException { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3225 | Preconditions.checkNotNull(channel); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3226 | Preconditions.checkNotNull(pkg); |
| 3227 | Preconditions.checkNotNull(user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3228 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3229 | verifyPrivilegedListener(token, user); |
| 3230 | updateNotificationChannelInt(pkg, getUidForPackageAndUser(pkg, user), channel, true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | @Override |
| 3234 | public ParceledListSlice<NotificationChannel> getNotificationChannelsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3235 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 3236 | Preconditions.checkNotNull(pkg); |
| 3237 | Preconditions.checkNotNull(user); |
| 3238 | verifyPrivilegedListener(token, user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3239 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3240 | return mRankingHelper.getNotificationChannels(pkg, getUidForPackageAndUser(pkg, user), |
| 3241 | false /* includeDeleted */); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | @Override |
| 3245 | public ParceledListSlice<NotificationChannelGroup> |
| 3246 | getNotificationChannelGroupsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3247 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 3248 | Preconditions.checkNotNull(pkg); |
| 3249 | Preconditions.checkNotNull(user); |
| 3250 | verifyPrivilegedListener(token, user); |
| 3251 | |
| 3252 | List<NotificationChannelGroup> groups = new ArrayList<>(); |
| 3253 | groups.addAll(mRankingHelper.getNotificationChannelGroups( |
| 3254 | pkg, getUidForPackageAndUser(pkg, user))); |
| 3255 | return new ParceledListSlice<>(groups); |
| 3256 | } |
| 3257 | |
| 3258 | private void verifyPrivilegedListener(INotificationListener token, UserHandle user) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3259 | ManagedServiceInfo info; |
| 3260 | synchronized (mNotificationLock) { |
| 3261 | info = mListeners.checkServiceTokenLocked(token); |
| 3262 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 3263 | if (!hasCompanionDevice(info)) { |
| 3264 | throw new SecurityException(info + " does not have access"); |
| 3265 | } |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3266 | if (!info.enabledAndUserMatches(user.getIdentifier())) { |
| 3267 | throw new SecurityException(info + " does not have access"); |
| 3268 | } |
| 3269 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3270 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3271 | private int getUidForPackageAndUser(String pkg, UserHandle user) throws RemoteException { |
| 3272 | int uid = 0; |
| 3273 | long identity = Binder.clearCallingIdentity(); |
| 3274 | try { |
| 3275 | uid = mPackageManager.getPackageUid(pkg, 0, user.getIdentifier()); |
| 3276 | } finally { |
| 3277 | Binder.restoreCallingIdentity(identity); |
| 3278 | } |
| 3279 | return uid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3280 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3281 | |
| 3282 | @Override |
| 3283 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 3284 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) |
| 3285 | throws RemoteException { |
| 3286 | new ShellCmd().exec(this, in, out, err, args, callback, resultReceiver); |
| 3287 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3288 | }; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 3289 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3290 | private void applyAdjustment(NotificationRecord r, Adjustment adjustment) { |
| 3291 | if (r == null) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3292 | return; |
| 3293 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3294 | if (adjustment.getSignals() != null) { |
| 3295 | Bundle.setDefusable(adjustment.getSignals(), true); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3296 | r.addAdjustment(adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3297 | } |
| 3298 | } |
| 3299 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3300 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3301 | void addAutogroupKeyLocked(String key) { |
| 3302 | NotificationRecord r = mNotificationsByKey.get(key); |
| 3303 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3304 | return; |
| 3305 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3306 | if (r.sbn.getOverrideGroupKey() == null) { |
| 3307 | addAutoGroupAdjustment(r, GroupHelper.AUTOGROUP_KEY); |
| 3308 | EventLogTags.writeNotificationAutogrouped(key); |
| 3309 | mRankingHandler.requestSort(); |
| 3310 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3311 | } |
| 3312 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3313 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3314 | void removeAutogroupKeyLocked(String key) { |
| 3315 | NotificationRecord r = mNotificationsByKey.get(key); |
| 3316 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3317 | return; |
| 3318 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3319 | if (r.sbn.getOverrideGroupKey() != null) { |
| 3320 | addAutoGroupAdjustment(r, null); |
| 3321 | EventLogTags.writeNotificationUnautogrouped(key); |
| 3322 | mRankingHandler.requestSort(); |
| 3323 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | private void addAutoGroupAdjustment(NotificationRecord r, String overrideGroupKey) { |
| 3327 | Bundle signals = new Bundle(); |
| 3328 | signals.putString(Adjustment.KEY_GROUP_KEY, overrideGroupKey); |
| 3329 | Adjustment adjustment = |
| 3330 | new Adjustment(r.sbn.getPackageName(), r.getKey(), signals, "", r.sbn.getUserId()); |
| 3331 | r.addAdjustment(adjustment); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | // Clears the 'fake' auto-group summary. |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3335 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3336 | private void clearAutogroupSummaryLocked(int userId, String pkg) { |
| 3337 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 3338 | if (summaries != null && summaries.containsKey(pkg)) { |
| 3339 | // Clear summary. |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3340 | final NotificationRecord removed = findNotificationByKeyLocked(summaries.remove(pkg)); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3341 | if (removed != null) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 3342 | boolean wasPosted = removeFromNotificationListsLocked(removed); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 3343 | cancelNotificationLocked(removed, false, REASON_UNAUTOBUNDLED, wasPosted, null); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3344 | } |
| 3345 | } |
| 3346 | } |
| 3347 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 3348 | @GuardedBy("mNotificationLock") |
| 3349 | private boolean hasAutoGroupSummaryLocked(StatusBarNotification sbn) { |
| 3350 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(sbn.getUserId()); |
| 3351 | return summaries != null && summaries.containsKey(sbn.getPackageName()); |
| 3352 | } |
| 3353 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3354 | // 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] | 3355 | private void createAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 3356 | NotificationRecord summaryRecord = null; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3357 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3358 | NotificationRecord notificationRecord = mNotificationsByKey.get(triggeringKey); |
| 3359 | if (notificationRecord == null) { |
| 3360 | // The notification could have been cancelled again already. A successive |
| 3361 | // adjustment will post a summary if needed. |
| 3362 | return; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3363 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3364 | final StatusBarNotification adjustedSbn = notificationRecord.sbn; |
| 3365 | userId = adjustedSbn.getUser().getIdentifier(); |
| 3366 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 3367 | if (summaries == null) { |
| 3368 | summaries = new ArrayMap<>(); |
| 3369 | } |
| 3370 | mAutobundledSummaries.put(userId, summaries); |
| 3371 | if (!summaries.containsKey(pkg)) { |
| 3372 | // Add summary |
| 3373 | final ApplicationInfo appInfo = |
| 3374 | adjustedSbn.getNotification().extras.getParcelable( |
| 3375 | Notification.EXTRA_BUILDER_APPLICATION_INFO); |
| 3376 | final Bundle extras = new Bundle(); |
| 3377 | extras.putParcelable(Notification.EXTRA_BUILDER_APPLICATION_INFO, appInfo); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3378 | final String channelId = notificationRecord.getChannel().getId(); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3379 | final Notification summaryNotification = |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3380 | new Notification.Builder(getContext(), channelId) |
| 3381 | .setSmallIcon(adjustedSbn.getNotification().getSmallIcon()) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3382 | .setGroupSummary(true) |
Julia Reynolds | 9d5786e | 2017-04-28 10:26:32 -0400 | [diff] [blame] | 3383 | .setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3384 | .setGroup(GroupHelper.AUTOGROUP_KEY) |
| 3385 | .setFlag(Notification.FLAG_AUTOGROUP_SUMMARY, true) |
| 3386 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 3387 | .setColor(adjustedSbn.getNotification().color) |
| 3388 | .setLocalOnly(true) |
| 3389 | .build(); |
| 3390 | summaryNotification.extras.putAll(extras); |
| 3391 | Intent appIntent = getContext().getPackageManager().getLaunchIntentForPackage(pkg); |
| 3392 | if (appIntent != null) { |
| 3393 | summaryNotification.contentIntent = PendingIntent.getActivityAsUser( |
| 3394 | getContext(), 0, appIntent, 0, null, UserHandle.of(userId)); |
| 3395 | } |
| 3396 | final StatusBarNotification summarySbn = |
| 3397 | new StatusBarNotification(adjustedSbn.getPackageName(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3398 | adjustedSbn.getOpPkg(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3399 | Integer.MAX_VALUE, |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3400 | GroupHelper.AUTOGROUP_KEY, adjustedSbn.getUid(), |
| 3401 | adjustedSbn.getInitialPid(), summaryNotification, |
| 3402 | adjustedSbn.getUser(), GroupHelper.AUTOGROUP_KEY, |
| 3403 | System.currentTimeMillis()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 3404 | summaryRecord = new NotificationRecord(getContext(), summarySbn, |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 3405 | notificationRecord.getChannel()); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3406 | summaries.put(pkg, summarySbn.getKey()); |
| 3407 | } |
| 3408 | } |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3409 | if (summaryRecord != null && checkDisqualifyingFeatures(userId, MY_UID, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3410 | summaryRecord.sbn.getId(), summaryRecord.sbn.getTag(), summaryRecord, true)) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3411 | mHandler.post(new EnqueueNotificationRunnable(userId, summaryRecord)); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3412 | } |
| 3413 | } |
| 3414 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3415 | private String disableNotificationEffects(NotificationRecord record) { |
| 3416 | if (mDisableNotificationEffects) { |
| 3417 | return "booleanState"; |
| 3418 | } |
| 3419 | if ((mListenerHints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 3420 | return "listenerHints"; |
| 3421 | } |
| 3422 | if (mCallState != TelephonyManager.CALL_STATE_IDLE && !mZenModeHelper.isCall(record)) { |
| 3423 | return "callState"; |
| 3424 | } |
| 3425 | return null; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3426 | }; |
| 3427 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3428 | private void dumpJson(PrintWriter pw, @NonNull DumpFilter filter) { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3429 | JSONObject dump = new JSONObject(); |
| 3430 | try { |
| 3431 | dump.put("service", "Notification Manager"); |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 3432 | dump.put("bans", mRankingHelper.dumpBansJson(filter)); |
| 3433 | dump.put("ranking", mRankingHelper.dumpJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3434 | dump.put("stats", mUsageStats.dumpJson(filter)); |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 3435 | dump.put("channels", mRankingHelper.dumpChannelsJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3436 | } catch (JSONException e) { |
| 3437 | e.printStackTrace(); |
| 3438 | } |
| 3439 | pw.println(dump); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3440 | } |
| 3441 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3442 | private void dumpProto(FileDescriptor fd, @NonNull DumpFilter filter) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3443 | final ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 3444 | synchronized (mNotificationLock) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3445 | int N = mNotificationList.size(); |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 3446 | for (int i = 0; i < N; i++) { |
| 3447 | final NotificationRecord nr = mNotificationList.get(i); |
| 3448 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3449 | nr.dump(proto, NotificationServiceDumpProto.RECORDS, filter.redact, |
| 3450 | NotificationRecordProto.POSTED); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3451 | } |
| 3452 | N = mEnqueuedNotifications.size(); |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 3453 | for (int i = 0; i < N; i++) { |
| 3454 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 3455 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3456 | nr.dump(proto, NotificationServiceDumpProto.RECORDS, filter.redact, |
| 3457 | NotificationRecordProto.ENQUEUED); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3458 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3459 | List<NotificationRecord> snoozed = mSnoozeHelper.getSnoozed(); |
| 3460 | N = snoozed.size(); |
Kweku Adams | bc84aec | 2018-01-23 13:33:12 -0800 | [diff] [blame] | 3461 | for (int i = 0; i < N; i++) { |
| 3462 | final NotificationRecord nr = snoozed.get(i); |
| 3463 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3464 | nr.dump(proto, NotificationServiceDumpProto.RECORDS, filter.redact, |
| 3465 | NotificationRecordProto.SNOOZED); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3466 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3467 | |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3468 | long zenLog = proto.start(NotificationServiceDumpProto.ZEN); |
| 3469 | mZenModeHelper.dump(proto); |
| 3470 | for (ComponentName suppressor : mEffectsSuppressors) { |
Kweku Adams | 9954633 | 2018-01-24 17:03:50 -0800 | [diff] [blame] | 3471 | suppressor.writeToProto(proto, ZenModeProto.SUPPRESSORS); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3472 | } |
| 3473 | proto.end(zenLog); |
| 3474 | |
| 3475 | long listenersToken = proto.start(NotificationServiceDumpProto.NOTIFICATION_LISTENERS); |
| 3476 | mListeners.dump(proto, filter); |
| 3477 | proto.end(listenersToken); |
| 3478 | |
| 3479 | proto.write(NotificationServiceDumpProto.LISTENER_HINTS, mListenerHints); |
| 3480 | |
| 3481 | for (int i = 0; i < mListenersDisablingEffects.size(); ++i) { |
| 3482 | long effectsToken = proto.start( |
| 3483 | NotificationServiceDumpProto.LISTENERS_DISABLING_EFFECTS); |
| 3484 | |
| 3485 | proto.write( |
| 3486 | ListenersDisablingEffectsProto.HINT, mListenersDisablingEffects.keyAt(i)); |
| 3487 | final ArraySet<ManagedServiceInfo> listeners = |
| 3488 | mListenersDisablingEffects.valueAt(i); |
| 3489 | for (int j = 0; j < listeners.size(); j++) { |
| 3490 | final ManagedServiceInfo listener = listeners.valueAt(i); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 3491 | listener.writeToProto(proto, ListenersDisablingEffectsProto.LISTENERS, null); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | proto.end(effectsToken); |
| 3495 | } |
| 3496 | |
| 3497 | long assistantsToken = proto.start( |
| 3498 | NotificationServiceDumpProto.NOTIFICATION_ASSISTANTS); |
| 3499 | mAssistants.dump(proto, filter); |
| 3500 | proto.end(assistantsToken); |
| 3501 | |
| 3502 | long conditionsToken = proto.start(NotificationServiceDumpProto.CONDITION_PROVIDERS); |
| 3503 | mConditionProviders.dump(proto, filter); |
| 3504 | proto.end(conditionsToken); |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 3505 | |
| 3506 | long rankingToken = proto.start(NotificationServiceDumpProto.RANKING_CONFIG); |
| 3507 | mRankingHelper.dump(proto, filter); |
| 3508 | proto.end(rankingToken); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3509 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3510 | |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3511 | proto.flush(); |
| 3512 | } |
| 3513 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3514 | void dumpImpl(PrintWriter pw, @NonNull DumpFilter filter) { |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3515 | pw.print("Current Notification Manager state"); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3516 | if (filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3517 | pw.print(" (filtered to "); pw.print(filter); pw.print(")"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3518 | } |
| 3519 | pw.println(':'); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3520 | int N; |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3521 | final boolean zenOnly = filter.filtered && filter.zen; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3522 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3523 | if (!zenOnly) { |
| 3524 | synchronized (mToastQueue) { |
| 3525 | N = mToastQueue.size(); |
| 3526 | if (N > 0) { |
| 3527 | pw.println(" Toast Queue:"); |
| 3528 | for (int i=0; i<N; i++) { |
| 3529 | mToastQueue.get(i).dump(pw, " ", filter); |
| 3530 | } |
| 3531 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3532 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3533 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3534 | } |
| 3535 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3536 | synchronized (mNotificationLock) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3537 | if (!zenOnly) { |
| 3538 | N = mNotificationList.size(); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3539 | if (N > 0) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3540 | pw.println(" Notification List:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3541 | for (int i=0; i<N; i++) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3542 | final NotificationRecord nr = mNotificationList.get(i); |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3543 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3544 | nr.dump(pw, " ", getContext(), filter.redact); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3545 | } |
| 3546 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3547 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3548 | |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3549 | if (!filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3550 | N = mLights.size(); |
| 3551 | if (N > 0) { |
| 3552 | pw.println(" Lights List:"); |
| 3553 | for (int i=0; i<N; i++) { |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3554 | if (i == N - 1) { |
| 3555 | pw.print(" > "); |
| 3556 | } else { |
| 3557 | pw.print(" "); |
| 3558 | } |
| 3559 | pw.println(mLights.get(i)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3560 | } |
| 3561 | pw.println(" "); |
| 3562 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 3563 | pw.println(" mUseAttentionLight=" + mUseAttentionLight); |
| 3564 | pw.println(" mNotificationPulseEnabled=" + mNotificationPulseEnabled); |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3565 | pw.println(" mSoundNotificationKey=" + mSoundNotificationKey); |
| 3566 | pw.println(" mVibrateNotificationKey=" + mVibrateNotificationKey); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3567 | pw.println(" mDisableNotificationEffects=" + mDisableNotificationEffects); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3568 | pw.println(" mCallState=" + callStateToString(mCallState)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3569 | pw.println(" mSystemReady=" + mSystemReady); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 3570 | pw.println(" mMaxPackageEnqueueRate=" + mMaxPackageEnqueueRate); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3571 | } |
| 3572 | pw.println(" mArchive=" + mArchive.toString()); |
| 3573 | Iterator<StatusBarNotification> iter = mArchive.descendingIterator(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3574 | int j=0; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3575 | while (iter.hasNext()) { |
| 3576 | final StatusBarNotification sbn = iter.next(); |
| 3577 | if (filter != null && !filter.matches(sbn)) continue; |
| 3578 | pw.println(" " + sbn); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3579 | if (++j >= 5) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3580 | if (iter.hasNext()) pw.println(" ..."); |
| 3581 | break; |
| 3582 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3583 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3584 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3585 | if (!zenOnly) { |
| 3586 | N = mEnqueuedNotifications.size(); |
| 3587 | if (N > 0) { |
| 3588 | pw.println(" Enqueued Notification List:"); |
| 3589 | for (int i = 0; i < N; i++) { |
| 3590 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 3591 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3592 | nr.dump(pw, " ", getContext(), filter.redact); |
| 3593 | } |
| 3594 | pw.println(" "); |
| 3595 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3596 | |
| 3597 | mSnoozeHelper.dump(pw, filter); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3598 | } |
| 3599 | } |
| 3600 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3601 | if (!zenOnly) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3602 | pw.println("\n Ranking Config:"); |
| 3603 | mRankingHelper.dump(pw, " ", filter); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 3604 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3605 | pw.println("\n Notification listeners:"); |
| 3606 | mListeners.dump(pw, filter); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3607 | pw.print(" mListenerHints: "); pw.println(mListenerHints); |
| 3608 | pw.print(" mListenersDisablingEffects: ("); |
| 3609 | N = mListenersDisablingEffects.size(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3610 | for (int i = 0; i < N; i++) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 3611 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 3612 | if (i > 0) pw.print(';'); |
| 3613 | pw.print("hint[" + hint + "]:"); |
| 3614 | |
| 3615 | final ArraySet<ManagedServiceInfo> listeners = |
| 3616 | mListenersDisablingEffects.valueAt(i); |
| 3617 | final int listenerSize = listeners.size(); |
| 3618 | |
| 3619 | for (int j = 0; j < listenerSize; j++) { |
| 3620 | if (i > 0) pw.print(','); |
| 3621 | final ManagedServiceInfo listener = listeners.valueAt(i); |
| 3622 | pw.print(listener.component); |
| 3623 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3624 | } |
| 3625 | pw.println(')'); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 3626 | pw.println("\n Notification assistant services:"); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3627 | mAssistants.dump(pw, filter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3628 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3629 | |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3630 | if (!filter.filtered || zenOnly) { |
| 3631 | pw.println("\n Zen Mode:"); |
| 3632 | pw.print(" mInterruptionFilter="); pw.println(mInterruptionFilter); |
| 3633 | mZenModeHelper.dump(pw, " "); |
| 3634 | |
| 3635 | pw.println("\n Zen Log:"); |
| 3636 | ZenLog.dump(pw, " "); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3637 | } |
| 3638 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3639 | pw.println("\n Condition providers:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3640 | mConditionProviders.dump(pw, filter); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3641 | |
| 3642 | pw.println("\n Group summaries:"); |
| 3643 | for (Entry<String, NotificationRecord> entry : mSummaryByGroupKey.entrySet()) { |
| 3644 | NotificationRecord r = entry.getValue(); |
| 3645 | pw.println(" " + entry.getKey() + " -> " + r.getKey()); |
| 3646 | if (mNotificationsByKey.get(r.getKey()) != r) { |
| 3647 | pw.println("!!!!!!LEAK: Record not found in mNotificationsByKey."); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3648 | r.dump(pw, " ", getContext(), filter.redact); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3649 | } |
| 3650 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3651 | |
| 3652 | if (!zenOnly) { |
| 3653 | pw.println("\n Usage Stats:"); |
| 3654 | mUsageStats.dump(pw, " ", filter); |
| 3655 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3656 | } |
| 3657 | } |
| 3658 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3659 | /** |
| 3660 | * The private API only accessible to the system process. |
| 3661 | */ |
| 3662 | private final NotificationManagerInternal mInternalService = new NotificationManagerInternal() { |
| 3663 | @Override |
Julia Reynolds | f3de8aa | 2017-09-29 15:52:37 -0400 | [diff] [blame] | 3664 | public NotificationChannel getNotificationChannel(String pkg, int uid, String |
| 3665 | channelId) { |
| 3666 | return mRankingHelper.getNotificationChannel(pkg, uid, channelId, false); |
| 3667 | } |
| 3668 | |
| 3669 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3670 | public void enqueueNotification(String pkg, String opPkg, int callingUid, int callingPid, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3671 | String tag, int id, Notification notification, int userId) { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3672 | enqueueNotificationInternal(pkg, opPkg, callingUid, callingPid, tag, id, notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3673 | userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3674 | } |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3675 | |
| 3676 | @Override |
| 3677 | public void removeForegroundServiceFlagFromNotification(String pkg, int notificationId, |
| 3678 | int userId) { |
| 3679 | checkCallerIsSystem(); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3680 | mHandler.post(new Runnable() { |
| 3681 | @Override |
| 3682 | public void run() { |
| 3683 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3684 | removeForegroundServiceFlagByListLocked( |
| 3685 | mEnqueuedNotifications, pkg, notificationId, userId); |
| 3686 | removeForegroundServiceFlagByListLocked( |
| 3687 | mNotificationList, pkg, notificationId, userId); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3688 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3689 | } |
| 3690 | }); |
| 3691 | } |
| 3692 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3693 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3694 | private void removeForegroundServiceFlagByListLocked( |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3695 | ArrayList<NotificationRecord> notificationList, String pkg, int notificationId, |
| 3696 | int userId) { |
| 3697 | NotificationRecord r = findNotificationByListLocked( |
| 3698 | notificationList, pkg, null, notificationId, userId); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3699 | if (r == null) { |
| 3700 | return; |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3701 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3702 | StatusBarNotification sbn = r.sbn; |
| 3703 | // NoMan adds flags FLAG_NO_CLEAR and FLAG_ONGOING_EVENT when it sees |
| 3704 | // FLAG_FOREGROUND_SERVICE. Hence it's not enough to remove |
| 3705 | // FLAG_FOREGROUND_SERVICE, we have to revert to the flags we received |
| 3706 | // initially *and* force remove FLAG_FOREGROUND_SERVICE. |
| 3707 | sbn.getNotification().flags = |
| 3708 | (r.mOriginalFlags & ~Notification.FLAG_FOREGROUND_SERVICE); |
| 3709 | mRankingHelper.sort(mNotificationList); |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3710 | mListeners.notifyPostedLocked(r, sbn /* oldSbn */); |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3711 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3712 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3713 | |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3714 | void enqueueNotificationInternal(final String pkg, final String opPkg, final int callingUid, |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 3715 | final int callingPid, final String tag, final int id, final Notification notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3716 | int incomingUserId) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 3717 | if (DBG) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3718 | Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id |
| 3719 | + " notification=" + notification); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 3720 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3721 | checkCallerIsSystemOrSameApp(pkg); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3722 | |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 3723 | final int userId = ActivityManager.handleIncomingUser(callingPid, |
| 3724 | callingUid, incomingUserId, true, false, "enqueueNotification", pkg); |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 3725 | final UserHandle user = new UserHandle(userId); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3726 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3727 | if (pkg == null || notification == null) { |
| 3728 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 3729 | + " id=" + id + " notification=" + notification); |
| 3730 | } |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3731 | |
| 3732 | // The system can post notifications for any package, let us resolve that. |
| 3733 | final int notificationUid = resolveNotificationUid(opPkg, callingUid, userId); |
| 3734 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3735 | // Fix the notification as best we can. |
| 3736 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3737 | final ApplicationInfo ai = mPackageManagerClient.getApplicationInfoAsUser( |
Jeff Sharkey | 012bc7b | 2016-04-11 16:30:27 -0600 | [diff] [blame] | 3738 | pkg, PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 3739 | (userId == UserHandle.USER_ALL) ? USER_SYSTEM : userId); |
Julia Reynolds | e071abd | 2017-03-22 10:52:11 -0400 | [diff] [blame] | 3740 | Notification.addFieldsFromContext(ai, notification); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3741 | |
| 3742 | int canColorize = mPackageManagerClient.checkPermission( |
| 3743 | android.Manifest.permission.USE_COLORIZED_NOTIFICATIONS, pkg); |
| 3744 | if (canColorize == PERMISSION_GRANTED) { |
| 3745 | notification.flags |= Notification.FLAG_CAN_COLORIZE; |
| 3746 | } else { |
| 3747 | notification.flags &= ~Notification.FLAG_CAN_COLORIZE; |
| 3748 | } |
| 3749 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3750 | } catch (NameNotFoundException e) { |
| 3751 | Slog.e(TAG, "Cannot create a context for sending app", e); |
| 3752 | return; |
| 3753 | } |
| 3754 | |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 3755 | mUsageStats.registerEnqueuedByApp(pkg); |
| 3756 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3757 | // setup local book-keeping |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 3758 | String channelId = notification.getChannelId(); |
| 3759 | if (mIsTelevision && (new Notification.TvExtender(notification)).getChannelId() != null) { |
| 3760 | channelId = (new Notification.TvExtender(notification)).getChannelId(); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 3761 | } |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3762 | final NotificationChannel channel = mRankingHelper.getNotificationChannel(pkg, |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3763 | notificationUid, channelId, false /* includeDeleted */); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3764 | if (channel == null) { |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3765 | final String noChannelStr = "No Channel found for " |
| 3766 | + "pkg=" + pkg |
| 3767 | + ", channelId=" + channelId |
Julia Reynolds | f26eb91 | 2017-05-22 15:47:06 -0400 | [diff] [blame] | 3768 | + ", id=" + id |
| 3769 | + ", tag=" + tag |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3770 | + ", opPkg=" + opPkg |
| 3771 | + ", callingUid=" + callingUid |
| 3772 | + ", userId=" + userId |
| 3773 | + ", incomingUserId=" + incomingUserId |
| 3774 | + ", notificationUid=" + notificationUid |
| 3775 | + ", notification=" + notification; |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3776 | Log.e(TAG, noChannelStr); |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 3777 | doChannelWarningToast("Developer warning for package \"" + pkg + "\"\n" + |
Geoffrey Pitsch | cadb5dc | 2017-04-11 11:35:02 -0400 | [diff] [blame] | 3778 | "Failed to post notification on channel \"" + channelId + "\"\n" + |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3779 | "See log for more details"); |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3780 | return; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3781 | } |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3782 | |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 3783 | final StatusBarNotification n = new StatusBarNotification( |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3784 | pkg, opPkg, id, tag, notificationUid, callingPid, notification, |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3785 | user, null, System.currentTimeMillis()); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 3786 | final NotificationRecord r = new NotificationRecord(getContext(), n, channel); |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 3787 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3788 | if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0 |
| 3789 | && (channel.getUserLockedFields() & NotificationChannel.USER_LOCKED_IMPORTANCE) == 0 |
| 3790 | && (r.getImportance() == IMPORTANCE_MIN || r.getImportance() == IMPORTANCE_NONE)) { |
| 3791 | // Increase the importance of foreground service notifications unless the user had an |
| 3792 | // opinion otherwise |
| 3793 | if (TextUtils.isEmpty(channelId) |
| 3794 | || NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 3795 | r.setImportance(IMPORTANCE_LOW, "Bumped for foreground service"); |
| 3796 | } else { |
| 3797 | channel.setImportance(IMPORTANCE_LOW); |
| 3798 | mRankingHelper.updateNotificationChannel(pkg, notificationUid, channel, false); |
| 3799 | r.updateNotificationChannel(channel); |
| 3800 | } |
| 3801 | } |
| 3802 | |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3803 | if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r, |
| 3804 | r.sbn.getOverrideGroupKey() != null)) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3805 | return; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 3806 | } |
| 3807 | |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 3808 | // Whitelist pending intents. |
| 3809 | if (notification.allPendingIntents != null) { |
| 3810 | final int intentCount = notification.allPendingIntents.size(); |
| 3811 | if (intentCount > 0) { |
| 3812 | final ActivityManagerInternal am = LocalServices |
| 3813 | .getService(ActivityManagerInternal.class); |
| 3814 | final long duration = LocalServices.getService( |
| 3815 | DeviceIdleController.LocalService.class).getNotificationWhitelistDuration(); |
| 3816 | for (int i = 0; i < intentCount; i++) { |
| 3817 | PendingIntent pendingIntent = notification.allPendingIntents.valueAt(i); |
| 3818 | if (pendingIntent != null) { |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 3819 | am.setPendingIntentWhitelistDuration(pendingIntent.getTarget(), |
| 3820 | WHITELIST_TOKEN, duration); |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 3821 | } |
| 3822 | } |
| 3823 | } |
| 3824 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 3825 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3826 | mHandler.post(new EnqueueNotificationRunnable(userId, r)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3827 | } |
| 3828 | |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 3829 | private void doChannelWarningToast(CharSequence toastText) { |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 3830 | final int defaultWarningEnabled = Build.IS_DEBUGGABLE ? 1 : 0; |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 3831 | final boolean warningEnabled = Settings.Global.getInt(getContext().getContentResolver(), |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 3832 | Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, defaultWarningEnabled) != 0; |
| 3833 | if (warningEnabled) { |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 3834 | Toast toast = Toast.makeText(getContext(), mHandler.getLooper(), toastText, |
Geoffrey Pitsch | 5bdddbd | 2017-05-26 10:50:05 -0400 | [diff] [blame] | 3835 | Toast.LENGTH_SHORT); |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 3836 | toast.show(); |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3837 | } |
| 3838 | } |
| 3839 | |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3840 | private int resolveNotificationUid(String opPackageName, int callingUid, int userId) { |
| 3841 | // The system can post notifications on behalf of any package it wants |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3842 | if (isCallerSystemOrPhone() && opPackageName != null && !"android".equals(opPackageName)) { |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3843 | try { |
| 3844 | return getContext().getPackageManager() |
| 3845 | .getPackageUidAsUser(opPackageName, userId); |
| 3846 | } catch (NameNotFoundException e) { |
| 3847 | /* ignore */ |
| 3848 | } |
| 3849 | } |
| 3850 | return callingUid; |
| 3851 | } |
| 3852 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3853 | /** |
| 3854 | * Checks if a notification can be posted. checks rate limiter, snooze helper, and blocking. |
| 3855 | * |
| 3856 | * Has side effects. |
| 3857 | */ |
| 3858 | private boolean checkDisqualifyingFeatures(int userId, int callingUid, int id, String tag, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3859 | NotificationRecord r, boolean isAutogroup) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3860 | final String pkg = r.sbn.getPackageName(); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3861 | final boolean isSystemNotification = |
| 3862 | isUidSystemOrPhone(callingUid) || ("android".equals(pkg)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3863 | final boolean isNotificationFromListener = mListeners.isListenerPackage(pkg); |
| 3864 | |
| 3865 | // Limit the number of notifications that any given package except the android |
| 3866 | // package or a registered listener can enqueue. Prevents DOS attacks and deals with leaks. |
| 3867 | if (!isSystemNotification && !isNotificationFromListener) { |
| 3868 | synchronized (mNotificationLock) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3869 | if (mNotificationsByKey.get(r.sbn.getKey()) == null && isCallerInstantApp(pkg)) { |
| 3870 | // Ephemeral apps have some special constraints for notifications. |
| 3871 | // They are not allowed to create new notifications however they are allowed to |
| 3872 | // update notifications created by the system (e.g. a foreground service |
| 3873 | // notification). |
| 3874 | throw new SecurityException("Instant app " + pkg |
| 3875 | + " cannot create notifications"); |
| 3876 | } |
| 3877 | |
| 3878 | // rate limit updates that aren't completed progress notifications |
| 3879 | if (mNotificationsByKey.get(r.sbn.getKey()) != null |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3880 | && !r.getNotification().hasCompletedProgress() |
| 3881 | && !isAutogroup) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3882 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3883 | final float appEnqueueRate = mUsageStats.getAppEnqueueRate(pkg); |
| 3884 | if (appEnqueueRate > mMaxPackageEnqueueRate) { |
| 3885 | mUsageStats.registerOverRateQuota(pkg); |
| 3886 | final long now = SystemClock.elapsedRealtime(); |
| 3887 | if ((now - mLastOverRateLogTime) > MIN_PACKAGE_OVERRATE_LOG_INTERVAL) { |
| 3888 | Slog.e(TAG, "Package enqueue rate is " + appEnqueueRate |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3889 | + ". Shedding " + r.sbn.getKey() + ". package=" + pkg); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3890 | mLastOverRateLogTime = now; |
| 3891 | } |
| 3892 | return false; |
| 3893 | } |
| 3894 | } |
| 3895 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3896 | // limit the number of outstanding notificationrecords an app can have |
| 3897 | int count = getNotificationCountLocked(pkg, userId, id, tag); |
| 3898 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 3899 | mUsageStats.registerOverCountQuota(pkg); |
| 3900 | Slog.e(TAG, "Package has already posted or enqueued " + count |
| 3901 | + " notifications. Not showing more. package=" + pkg); |
| 3902 | return false; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3903 | } |
| 3904 | } |
| 3905 | } |
| 3906 | |
| 3907 | // snoozed apps |
| 3908 | if (mSnoozeHelper.isSnoozed(userId, pkg, r.getKey())) { |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3909 | MetricsLogger.action(r.getLogMaker() |
| 3910 | .setType(MetricsProto.MetricsEvent.TYPE_UPDATE) |
| 3911 | .setCategory(MetricsProto.MetricsEvent.NOTIFICATION_SNOOZED)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3912 | if (DBG) { |
| 3913 | Slog.d(TAG, "Ignored enqueue for snoozed notification " + r.getKey()); |
| 3914 | } |
| 3915 | mSnoozeHelper.update(userId, r); |
| 3916 | savePolicyFile(); |
| 3917 | return false; |
| 3918 | } |
| 3919 | |
| 3920 | |
| 3921 | // blocked apps |
| 3922 | if (isBlocked(r, mUsageStats)) { |
| 3923 | return false; |
| 3924 | } |
| 3925 | |
| 3926 | return true; |
| 3927 | } |
| 3928 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3929 | protected int getNotificationCountLocked(String pkg, int userId, int excludedId, |
| 3930 | String excludedTag) { |
| 3931 | int count = 0; |
| 3932 | final int N = mNotificationList.size(); |
| 3933 | for (int i = 0; i < N; i++) { |
| 3934 | final NotificationRecord existing = mNotificationList.get(i); |
| 3935 | if (existing.sbn.getPackageName().equals(pkg) |
| 3936 | && existing.sbn.getUserId() == userId) { |
| 3937 | if (existing.sbn.getId() == excludedId |
| 3938 | && TextUtils.equals(existing.sbn.getTag(), excludedTag)) { |
| 3939 | continue; |
| 3940 | } |
| 3941 | count++; |
| 3942 | } |
| 3943 | } |
| 3944 | final int M = mEnqueuedNotifications.size(); |
| 3945 | for (int i = 0; i < M; i++) { |
| 3946 | final NotificationRecord existing = mEnqueuedNotifications.get(i); |
| 3947 | if (existing.sbn.getPackageName().equals(pkg) |
| 3948 | && existing.sbn.getUserId() == userId) { |
| 3949 | count++; |
| 3950 | } |
| 3951 | } |
| 3952 | return count; |
| 3953 | } |
| 3954 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3955 | protected boolean isBlocked(NotificationRecord r, NotificationUsageStats usageStats) { |
| 3956 | final String pkg = r.sbn.getPackageName(); |
| 3957 | final int callingUid = r.sbn.getUid(); |
| 3958 | |
| 3959 | final boolean isPackageSuspended = isPackageSuspendedForUser(pkg, callingUid); |
| 3960 | if (isPackageSuspended) { |
| 3961 | Slog.e(TAG, "Suppressing notification from package due to package " |
| 3962 | + "suspended by administrator."); |
| 3963 | usageStats.registerSuspendedByAdmin(r); |
| 3964 | return isPackageSuspended; |
| 3965 | } |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 3966 | final boolean isBlocked = |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 3967 | mRankingHelper.isGroupBlocked(pkg, callingUid, r.getChannel().getGroup()) |
| 3968 | || mRankingHelper.getImportance(pkg, callingUid) |
| 3969 | == NotificationManager.IMPORTANCE_NONE |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 3970 | || r.getChannel().getImportance() == NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3971 | if (isBlocked) { |
| 3972 | Slog.e(TAG, "Suppressing notification from package by user request."); |
| 3973 | usageStats.registerBlocked(r); |
| 3974 | } |
| 3975 | return isBlocked; |
| 3976 | } |
| 3977 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3978 | protected class SnoozeNotificationRunnable implements Runnable { |
| 3979 | private final String mKey; |
| 3980 | private final long mDuration; |
| 3981 | private final String mSnoozeCriterionId; |
| 3982 | |
| 3983 | SnoozeNotificationRunnable(String key, long duration, String snoozeCriterionId) { |
| 3984 | mKey = key; |
| 3985 | mDuration = duration; |
| 3986 | mSnoozeCriterionId = snoozeCriterionId; |
| 3987 | } |
| 3988 | |
| 3989 | @Override |
| 3990 | public void run() { |
| 3991 | synchronized (mNotificationLock) { |
| 3992 | final NotificationRecord r = findNotificationByKeyLocked(mKey); |
| 3993 | if (r != null) { |
| 3994 | snoozeLocked(r); |
| 3995 | } |
| 3996 | } |
| 3997 | } |
| 3998 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3999 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4000 | void snoozeLocked(NotificationRecord r) { |
| 4001 | if (r.sbn.isGroup()) { |
| 4002 | final List<NotificationRecord> groupNotifications = findGroupNotificationsLocked( |
| 4003 | r.sbn.getPackageName(), r.sbn.getGroupKey(), r.sbn.getUserId()); |
| 4004 | if (r.getNotification().isGroupSummary()) { |
| 4005 | // snooze summary and all children |
| 4006 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 4007 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 4008 | } |
| 4009 | } else { |
| 4010 | // if there is a valid summary for this group, and we are snoozing the only |
| 4011 | // child, also snooze the summary |
| 4012 | if (mSummaryByGroupKey.containsKey(r.sbn.getGroupKey())) { |
| 4013 | if (groupNotifications.size() != 2) { |
| 4014 | snoozeNotificationLocked(r); |
| 4015 | } else { |
| 4016 | // snooze summary and the one child |
| 4017 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 4018 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 4019 | } |
| 4020 | } |
| 4021 | } else { |
| 4022 | snoozeNotificationLocked(r); |
| 4023 | } |
| 4024 | } |
| 4025 | } else { |
| 4026 | // just snooze the one notification |
| 4027 | snoozeNotificationLocked(r); |
| 4028 | } |
| 4029 | } |
| 4030 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4031 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4032 | void snoozeNotificationLocked(NotificationRecord r) { |
| 4033 | MetricsLogger.action(r.getLogMaker() |
| 4034 | .setCategory(MetricsEvent.NOTIFICATION_SNOOZED) |
| 4035 | .setType(MetricsEvent.TYPE_CLOSE) |
Chris Wren | 21a2e72 | 2017-10-02 17:44:53 -0400 | [diff] [blame] | 4036 | .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_SNOOZE_DURATION_MS, |
| 4037 | mDuration) |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4038 | .addTaggedData(MetricsEvent.NOTIFICATION_SNOOZED_CRITERIA, |
| 4039 | mSnoozeCriterionId == null ? 0 : 1)); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4040 | boolean wasPosted = removeFromNotificationListsLocked(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4041 | cancelNotificationLocked(r, false, REASON_SNOOZED, wasPosted, null); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4042 | updateLightsLocked(); |
| 4043 | if (mSnoozeCriterionId != null) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4044 | mAssistants.notifyAssistantSnoozedLocked(r.sbn, mSnoozeCriterionId); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4045 | mSnoozeHelper.snooze(r); |
| 4046 | } else { |
| 4047 | mSnoozeHelper.snooze(r, mDuration); |
| 4048 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4049 | r.recordSnoozed(); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4050 | savePolicyFile(); |
| 4051 | } |
| 4052 | } |
| 4053 | |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 4054 | protected class EnqueueNotificationRunnable implements Runnable { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4055 | private final NotificationRecord r; |
| 4056 | private final int userId; |
| 4057 | |
| 4058 | EnqueueNotificationRunnable(int userId, NotificationRecord r) { |
| 4059 | this.userId = userId; |
| 4060 | this.r = r; |
| 4061 | }; |
| 4062 | |
| 4063 | @Override |
| 4064 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4065 | synchronized (mNotificationLock) { |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 4066 | mEnqueuedNotifications.add(r); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4067 | scheduleTimeoutLocked(r); |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 4068 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4069 | final StatusBarNotification n = r.sbn; |
| 4070 | if (DBG) Slog.d(TAG, "EnqueueNotificationRunnable.run for: " + n.getKey()); |
| 4071 | NotificationRecord old = mNotificationsByKey.get(n.getKey()); |
| 4072 | if (old != null) { |
| 4073 | // Retain ranking information from previous record |
| 4074 | r.copyRankingInformation(old); |
| 4075 | } |
| 4076 | |
| 4077 | final int callingUid = n.getUid(); |
| 4078 | final int callingPid = n.getInitialPid(); |
| 4079 | final Notification notification = n.getNotification(); |
| 4080 | final String pkg = n.getPackageName(); |
| 4081 | final int id = n.getId(); |
| 4082 | final String tag = n.getTag(); |
| 4083 | |
| 4084 | // Handle grouped notifications and bail out early if we |
| 4085 | // can to avoid extracting signals. |
| 4086 | handleGroupedNotificationLocked(r, old, callingUid, callingPid); |
| 4087 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4088 | // if this is a group child, unsnooze parent summary |
| 4089 | if (n.isGroup() && notification.isGroupChild()) { |
| 4090 | mSnoozeHelper.repostGroupSummary(pkg, r.getUserId(), n.getGroupKey()); |
| 4091 | } |
| 4092 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4093 | // This conditional is a dirty hack to limit the logging done on |
| 4094 | // behalf of the download manager without affecting other apps. |
| 4095 | if (!pkg.equals("com.android.providers.downloads") |
| 4096 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| 4097 | int enqueueStatus = EVENTLOG_ENQUEUE_STATUS_NEW; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4098 | if (old != null) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4099 | enqueueStatus = EVENTLOG_ENQUEUE_STATUS_UPDATE; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4100 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4101 | EventLogTags.writeNotificationEnqueue(callingUid, callingPid, |
| 4102 | pkg, id, tag, userId, notification.toString(), |
| 4103 | enqueueStatus); |
| 4104 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4105 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4106 | mRankingHelper.extractSignals(r); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4107 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4108 | // tell the assistant service about the notification |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4109 | if (mAssistants.isEnabled()) { |
| 4110 | mAssistants.onNotificationEnqueued(r); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4111 | mHandler.postDelayed(new PostNotificationRunnable(r.getKey()), |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4112 | DELAY_FOR_ASSISTANT_TIME); |
| 4113 | } else { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4114 | mHandler.post(new PostNotificationRunnable(r.getKey())); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4115 | } |
| 4116 | } |
| 4117 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | protected class PostNotificationRunnable implements Runnable { |
| 4121 | private final String key; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4122 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4123 | PostNotificationRunnable(String key) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4124 | this.key = key; |
| 4125 | } |
| 4126 | |
| 4127 | @Override |
| 4128 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4129 | synchronized (mNotificationLock) { |
| 4130 | try { |
| 4131 | NotificationRecord r = null; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4132 | int N = mEnqueuedNotifications.size(); |
| 4133 | for (int i = 0; i < N; i++) { |
| 4134 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 4135 | if (Objects.equals(key, enqueued.getKey())) { |
| 4136 | r = enqueued; |
| 4137 | break; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4138 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4139 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4140 | if (r == null) { |
| 4141 | Slog.i(TAG, "Cannot find enqueued record for key: " + key); |
| 4142 | return; |
| 4143 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4144 | NotificationRecord old = mNotificationsByKey.get(key); |
| 4145 | final StatusBarNotification n = r.sbn; |
| 4146 | final Notification notification = n.getNotification(); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4147 | int index = indexOfNotificationLocked(n.getKey()); |
| 4148 | if (index < 0) { |
| 4149 | mNotificationList.add(r); |
| 4150 | mUsageStats.registerPostedByApp(r); |
| 4151 | } else { |
| 4152 | old = mNotificationList.get(index); |
| 4153 | mNotificationList.set(index, r); |
| 4154 | mUsageStats.registerUpdatedByApp(r, old); |
| 4155 | // Make sure we don't lose the foreground service state. |
| 4156 | notification.flags |= |
| 4157 | old.getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 4158 | // revoke uri permissions for changed uris |
| 4159 | revokeUriPermissions(r, old); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4160 | r.isUpdate = true; |
| 4161 | } |
| 4162 | |
| 4163 | mNotificationsByKey.put(n.getKey(), r); |
| 4164 | |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 4165 | if (!r.isUpdate) { |
| 4166 | logRecentLocked(r); |
| 4167 | } |
| 4168 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4169 | // Ensure if this is a foreground service that the proper additional |
| 4170 | // flags are set. |
| 4171 | if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 4172 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 4173 | | Notification.FLAG_NO_CLEAR; |
| 4174 | } |
| 4175 | |
| 4176 | applyZenModeLocked(r); |
| 4177 | mRankingHelper.sort(mNotificationList); |
| 4178 | |
| 4179 | if (notification.getSmallIcon() != null) { |
| 4180 | StatusBarNotification oldSbn = (old != null) ? old.sbn : null; |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 4181 | mListeners.notifyPostedLocked(r, oldSbn); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 4182 | if (oldSbn == null || !Objects.equals(oldSbn.getGroup(), n.getGroup())) { |
| 4183 | mHandler.post(new Runnable() { |
| 4184 | @Override |
| 4185 | public void run() { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 4186 | mGroupHelper.onNotificationPosted( |
| 4187 | n, hasAutoGroupSummaryLocked(n)); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 4188 | } |
| 4189 | }); |
| 4190 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4191 | } else { |
| 4192 | Slog.e(TAG, "Not posting notification without small icon: " + notification); |
| 4193 | if (old != null && !old.isCanceled) { |
| 4194 | mListeners.notifyRemovedLocked(n, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4195 | NotificationListenerService.REASON_ERROR, null); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4196 | mHandler.post(new Runnable() { |
| 4197 | @Override |
| 4198 | public void run() { |
| 4199 | mGroupHelper.onNotificationRemoved(n); |
| 4200 | } |
| 4201 | }); |
| 4202 | } |
| 4203 | // ATTENTION: in a future release we will bail out here |
| 4204 | // so that we do not play sounds, show lights, etc. for invalid |
| 4205 | // notifications |
| 4206 | Slog.e(TAG, "WARNING: In a future release this will crash the app: " |
| 4207 | + n.getPackageName()); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4208 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4209 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4210 | buzzBeepBlinkLocked(r); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4211 | } finally { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4212 | int N = mEnqueuedNotifications.size(); |
| 4213 | for (int i = 0; i < N; i++) { |
| 4214 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 4215 | if (Objects.equals(key, enqueued.getKey())) { |
| 4216 | mEnqueuedNotifications.remove(i); |
| 4217 | break; |
| 4218 | } |
| 4219 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4220 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4221 | } |
| 4222 | } |
| 4223 | } |
| 4224 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4225 | /** |
Julia Reynolds | 7bcb57b | 2018-01-22 10:37:58 -0500 | [diff] [blame] | 4226 | * Keeps the last 5 packages that have notified, by user. |
| 4227 | */ |
| 4228 | @GuardedBy("mNotificationLock") |
| 4229 | @VisibleForTesting |
| 4230 | protected void logRecentLocked(NotificationRecord r) { |
| 4231 | if (r.isUpdate) { |
| 4232 | return; |
| 4233 | } |
| 4234 | ArrayList<NotifyingApp> recentAppsForUser = |
| 4235 | mRecentApps.getOrDefault(r.getUser().getIdentifier(), new ArrayList<>(6)); |
| 4236 | NotifyingApp na = new NotifyingApp() |
| 4237 | .setPackage(r.sbn.getPackageName()) |
| 4238 | .setUid(r.sbn.getUid()) |
| 4239 | .setLastNotified(r.sbn.getPostTime()); |
| 4240 | // A new notification gets an app moved to the front of the list |
| 4241 | for (int i = recentAppsForUser.size() - 1; i >= 0; i--) { |
| 4242 | NotifyingApp naExisting = recentAppsForUser.get(i); |
| 4243 | if (na.getPackage().equals(naExisting.getPackage()) |
| 4244 | && na.getUid() == naExisting.getUid()) { |
| 4245 | recentAppsForUser.remove(i); |
| 4246 | break; |
| 4247 | } |
| 4248 | } |
| 4249 | // time is always increasing, so always add to the front of the list |
| 4250 | recentAppsForUser.add(0, na); |
| 4251 | if (recentAppsForUser.size() > 5) { |
| 4252 | recentAppsForUser.remove(recentAppsForUser.size() -1); |
| 4253 | } |
| 4254 | mRecentApps.put(r.getUser().getIdentifier(), recentAppsForUser); |
| 4255 | } |
| 4256 | |
| 4257 | /** |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4258 | * Ensures that grouped notification receive their special treatment. |
| 4259 | * |
| 4260 | * <p>Cancels group children if the new notification causes a group to lose |
| 4261 | * its summary.</p> |
| 4262 | * |
| 4263 | * <p>Updates mSummaryByGroupKey.</p> |
| 4264 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4265 | @GuardedBy("mNotificationLock") |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4266 | private void handleGroupedNotificationLocked(NotificationRecord r, NotificationRecord old, |
| 4267 | int callingUid, int callingPid) { |
| 4268 | StatusBarNotification sbn = r.sbn; |
| 4269 | Notification n = sbn.getNotification(); |
Selim Cinek | 5b03ce9 | 2016-05-18 15:16:58 -0700 | [diff] [blame] | 4270 | if (n.isGroupSummary() && !sbn.isAppGroup()) { |
| 4271 | // notifications without a group shouldn't be a summary, otherwise autobundling can |
| 4272 | // lead to bugs |
| 4273 | n.flags &= ~Notification.FLAG_GROUP_SUMMARY; |
| 4274 | } |
| 4275 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4276 | String group = sbn.getGroupKey(); |
| 4277 | boolean isSummary = n.isGroupSummary(); |
| 4278 | |
| 4279 | Notification oldN = old != null ? old.sbn.getNotification() : null; |
| 4280 | String oldGroup = old != null ? old.sbn.getGroupKey() : null; |
| 4281 | boolean oldIsSummary = old != null && oldN.isGroupSummary(); |
| 4282 | |
| 4283 | if (oldIsSummary) { |
| 4284 | NotificationRecord removedSummary = mSummaryByGroupKey.remove(oldGroup); |
| 4285 | if (removedSummary != old) { |
| 4286 | String removedKey = |
| 4287 | removedSummary != null ? removedSummary.getKey() : "<null>"; |
| 4288 | Slog.w(TAG, "Removed summary didn't match old notification: old=" + old.getKey() + |
| 4289 | ", removed=" + removedKey); |
| 4290 | } |
| 4291 | } |
| 4292 | if (isSummary) { |
| 4293 | mSummaryByGroupKey.put(group, r); |
| 4294 | } |
| 4295 | |
| 4296 | // Clear out group children of the old notification if the update |
| 4297 | // causes the group summary to go away. This happens when the old |
| 4298 | // notification was a summary and the new one isn't, or when the old |
| 4299 | // notification was a summary and its group key changed. |
| 4300 | if (oldIsSummary && (!isSummary || !oldGroup.equals(group))) { |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 4301 | cancelGroupChildrenLocked(old, callingUid, callingPid, null, false /* sendDelete */, |
| 4302 | null); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4303 | } |
| 4304 | } |
| 4305 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4306 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4307 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4308 | void scheduleTimeoutLocked(NotificationRecord record) { |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 4309 | if (record.getNotification().getTimeoutAfter() > 0) { |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4310 | final PendingIntent pi = PendingIntent.getBroadcast(getContext(), |
| 4311 | REQUEST_CODE_TIMEOUT, |
| 4312 | new Intent(ACTION_NOTIFICATION_TIMEOUT) |
| 4313 | .setData(new Uri.Builder().scheme(SCHEME_TIMEOUT) |
| 4314 | .appendPath(record.getKey()).build()) |
| 4315 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 4316 | .putExtra(EXTRA_KEY, record.getKey()), |
| 4317 | PendingIntent.FLAG_UPDATE_CURRENT); |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 4318 | mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 4319 | SystemClock.elapsedRealtime() + record.getNotification().getTimeoutAfter(), pi); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4320 | } |
| 4321 | } |
| 4322 | |
| 4323 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4324 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4325 | void buzzBeepBlinkLocked(NotificationRecord record) { |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4326 | boolean buzz = false; |
| 4327 | boolean beep = false; |
| 4328 | boolean blink = false; |
| 4329 | |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4330 | final Notification notification = record.sbn.getNotification(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4331 | final String key = record.getKey(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4332 | |
| 4333 | // Should this notification make noise, vibe, or use the LED? |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 4334 | final boolean aboveThreshold = |
| 4335 | record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4336 | |
| 4337 | // Remember if this notification already owns the notification channels. |
| 4338 | boolean wasBeep = key != null && key.equals(mSoundNotificationKey); |
| 4339 | boolean wasBuzz = key != null && key.equals(mVibrateNotificationKey); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4340 | // These are set inside the conditional if the notification is allowed to make noise. |
| 4341 | boolean hasValidVibrate = false; |
| 4342 | boolean hasValidSound = false; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4343 | boolean sentAccessibilityEvent = false; |
| 4344 | // If the notification will appear in the status bar, it should send an accessibility |
| 4345 | // event |
| 4346 | if (!record.isUpdate && record.getImportance() > IMPORTANCE_MIN) { |
| 4347 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
| 4348 | sentAccessibilityEvent = true; |
| 4349 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4350 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4351 | if (aboveThreshold && isNotificationForCurrentUser(record)) { |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4352 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4353 | if (mSystemReady && mAudioManager != null) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4354 | Uri soundUri = record.getSound(); |
| 4355 | hasValidSound = soundUri != null && !Uri.EMPTY.equals(soundUri); |
| 4356 | long[] vibration = record.getVibration(); |
| 4357 | // Demote sound to vibration if vibration missing & phone in vibration mode. |
| 4358 | if (vibration == null |
| 4359 | && hasValidSound |
| 4360 | && (mAudioManager.getRingerModeInternal() |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 4361 | == AudioManager.RINGER_MODE_VIBRATE) |
| 4362 | && mAudioManager.getStreamVolume( |
| 4363 | AudioAttributes.toLegacyStreamType(record.getAudioAttributes())) == 0) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4364 | vibration = mFallbackVibrationPattern; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4365 | } |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4366 | hasValidVibrate = vibration != null; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4367 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4368 | boolean hasAudibleAlert = hasValidSound || hasValidVibrate; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4369 | if (hasAudibleAlert && !shouldMuteNotificationLocked(record)) { |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4370 | if (!sentAccessibilityEvent) { |
| 4371 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
| 4372 | sentAccessibilityEvent = true; |
| 4373 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4374 | if (DBG) Slog.v(TAG, "Interrupting!"); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4375 | if (hasValidSound) { |
| 4376 | mSoundNotificationKey = key; |
| 4377 | if (mInCall) { |
| 4378 | playInCallNotification(); |
| 4379 | beep = true; |
| 4380 | } else { |
| 4381 | beep = playSound(record, soundUri); |
| 4382 | } |
| 4383 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4384 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4385 | final boolean ringerModeSilent = |
| 4386 | mAudioManager.getRingerModeInternal() |
| 4387 | == AudioManager.RINGER_MODE_SILENT; |
| 4388 | if (!mInCall && hasValidVibrate && !ringerModeSilent) { |
| 4389 | mVibrateNotificationKey = key; |
| 4390 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4391 | buzz = playVibration(record, vibration, hasValidSound); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4392 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4393 | } |
| 4394 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4395 | } |
| 4396 | // If a notification is updated to remove the actively playing sound or vibrate, |
| 4397 | // cancel that feedback now |
| 4398 | if (wasBeep && !hasValidSound) { |
| 4399 | clearSoundLocked(); |
| 4400 | } |
| 4401 | if (wasBuzz && !hasValidVibrate) { |
| 4402 | clearVibrateLocked(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4403 | } |
| 4404 | |
| 4405 | // light |
| 4406 | // release the light |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4407 | boolean wasShowLights = mLights.remove(key); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 4408 | if (record.getLight() != null && aboveThreshold |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4409 | && ((record.getSuppressedVisualEffects() |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 4410 | & NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF) == 0)) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4411 | mLights.add(key); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4412 | updateLightsLocked(); |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 4413 | if (mUseAttentionLight) { |
| 4414 | mAttentionLight.pulse(); |
| 4415 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4416 | blink = true; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4417 | } else if (wasShowLights) { |
| 4418 | updateLightsLocked(); |
| 4419 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4420 | if (buzz || beep || blink) { |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 4421 | MetricsLogger.action(record.getLogMaker() |
| 4422 | .setCategory(MetricsEvent.NOTIFICATION_ALERT) |
| 4423 | .setType(MetricsEvent.TYPE_OPEN) |
| 4424 | .setSubtype((buzz ? 1 : 0) | (beep ? 2 : 0) | (blink ? 4 : 0))); |
| 4425 | EventLogTags.writeNotificationAlert(key, buzz ? 1 : 0, beep ? 1 : 0, blink ? 1 : 0); |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 4426 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4427 | } |
| 4428 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4429 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4430 | boolean shouldMuteNotificationLocked(final NotificationRecord record) { |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4431 | // Suppressed because it's a silent update |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4432 | final Notification notification = record.getNotification(); |
| 4433 | if(record.isUpdate |
| 4434 | && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) { |
| 4435 | return true; |
| 4436 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4437 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4438 | // muted by listener |
| 4439 | final String disableEffects = disableNotificationEffects(record); |
| 4440 | if (disableEffects != null) { |
| 4441 | ZenLog.traceDisableEffects(record, disableEffects); |
| 4442 | return true; |
| 4443 | } |
| 4444 | |
| 4445 | // suppressed due to DND |
| 4446 | if (record.isIntercepted()) { |
| 4447 | return true; |
| 4448 | } |
| 4449 | |
| 4450 | // Suppressed because another notification in its group handles alerting |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4451 | if (record.sbn.isGroup()) { |
Julia Reynolds | 3020315 | 2017-05-26 13:36:31 -0400 | [diff] [blame] | 4452 | return notification.suppressAlertingDueToGrouping(); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4453 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4454 | |
Julia Reynolds | 65b85cf | 2017-07-20 09:19:20 -0400 | [diff] [blame] | 4455 | // Suppressed for being too recently noisy |
| 4456 | final String pkg = record.sbn.getPackageName(); |
| 4457 | if (mUsageStats.isAlertRateLimited(pkg)) { |
| 4458 | Slog.e(TAG, "Muting recently noisy " + record.getKey()); |
| 4459 | return true; |
| 4460 | } |
| 4461 | |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4462 | return false; |
| 4463 | } |
| 4464 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 4465 | private boolean playSound(final NotificationRecord record, Uri soundUri) { |
| 4466 | boolean looping = (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 4467 | // do not play notifications if there is a user of exclusive audio focus |
Julia Reynolds | 2143e5d | 2017-01-17 16:28:48 -0500 | [diff] [blame] | 4468 | // or the device is in vibrate mode |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 4469 | if (!mAudioManager.isAudioFocusExclusive() && (mAudioManager.getRingerModeInternal() |
| 4470 | != AudioManager.RINGER_MODE_VIBRATE || mAudioManager.getStreamVolume( |
| 4471 | AudioAttributes.toLegacyStreamType(record.getAudioAttributes())) != 0)) { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 4472 | final long identity = Binder.clearCallingIdentity(); |
| 4473 | try { |
| 4474 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4475 | if (player != null) { |
| 4476 | if (DBG) Slog.v(TAG, "Playing sound " + soundUri |
| 4477 | + " with attributes " + record.getAudioAttributes()); |
| 4478 | player.playAsync(soundUri, record.sbn.getUser(), looping, |
| 4479 | record.getAudioAttributes()); |
| 4480 | return true; |
| 4481 | } |
| 4482 | } catch (RemoteException e) { |
| 4483 | } finally { |
| 4484 | Binder.restoreCallingIdentity(identity); |
| 4485 | } |
| 4486 | } |
| 4487 | return false; |
| 4488 | } |
| 4489 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4490 | private boolean playVibration(final NotificationRecord record, long[] vibration, |
| 4491 | boolean delayVibForSound) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 4492 | // Escalate privileges so we can use the vibrator even if the |
| 4493 | // notifying app does not have the VIBRATE permission. |
| 4494 | long identity = Binder.clearCallingIdentity(); |
| 4495 | try { |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4496 | final VibrationEffect effect; |
| 4497 | try { |
| 4498 | final boolean insistent = |
| 4499 | (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 4500 | effect = VibrationEffect.createWaveform( |
| 4501 | vibration, insistent ? 0 : -1 /*repeatIndex*/); |
| 4502 | } catch (IllegalArgumentException e) { |
| 4503 | Slog.e(TAG, "Error creating vibration waveform with pattern: " + |
| 4504 | Arrays.toString(vibration)); |
| 4505 | return false; |
| 4506 | } |
| 4507 | if (delayVibForSound) { |
| 4508 | new Thread(() -> { |
| 4509 | // delay the vibration by the same amount as the notification sound |
| 4510 | final int waitMs = mAudioManager.getFocusRampTimeMs( |
| 4511 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 4512 | record.getAudioAttributes()); |
| 4513 | if (DBG) Slog.v(TAG, "Delaying vibration by " + waitMs + "ms"); |
| 4514 | try { |
| 4515 | Thread.sleep(waitMs); |
| 4516 | } catch (InterruptedException e) { } |
| 4517 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 4518 | effect, record.getAudioAttributes()); |
| 4519 | }).start(); |
| 4520 | } else { |
| 4521 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 4522 | effect, record.getAudioAttributes()); |
| 4523 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 4524 | return true; |
| 4525 | } finally{ |
| 4526 | Binder.restoreCallingIdentity(identity); |
| 4527 | } |
| 4528 | } |
| 4529 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4530 | private boolean isNotificationForCurrentUser(NotificationRecord record) { |
| 4531 | final int currentUser; |
| 4532 | final long token = Binder.clearCallingIdentity(); |
| 4533 | try { |
| 4534 | currentUser = ActivityManager.getCurrentUser(); |
| 4535 | } finally { |
| 4536 | Binder.restoreCallingIdentity(token); |
| 4537 | } |
| 4538 | return (record.getUserId() == UserHandle.USER_ALL || |
| 4539 | record.getUserId() == currentUser || |
| 4540 | mUserProfiles.isCurrentProfile(record.getUserId())); |
| 4541 | } |
| 4542 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4543 | protected void playInCallNotification() { |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4544 | new Thread() { |
| 4545 | @Override |
| 4546 | public void run() { |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4547 | final long identity = Binder.clearCallingIdentity(); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4548 | try { |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4549 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4550 | if (player != null) { |
| 4551 | player.play(new Binder(), mInCallNotificationUri, |
| 4552 | mInCallNotificationAudioAttributes, |
| 4553 | mInCallNotificationVolume, false); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4554 | } |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4555 | } catch (RemoteException e) { |
| 4556 | } finally { |
| 4557 | Binder.restoreCallingIdentity(identity); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4558 | } |
| 4559 | } |
| 4560 | }.start(); |
| 4561 | } |
| 4562 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4563 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4564 | void showNextToastLocked() { |
| 4565 | ToastRecord record = mToastQueue.get(0); |
| 4566 | while (record != null) { |
| 4567 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| 4568 | try { |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4569 | record.callback.show(record.token); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4570 | scheduleTimeoutLocked(record); |
| 4571 | return; |
| 4572 | } catch (RemoteException e) { |
| 4573 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| 4574 | + " in package " + record.pkg); |
| 4575 | // remove it from the list and let the process die |
| 4576 | int index = mToastQueue.indexOf(record); |
| 4577 | if (index >= 0) { |
| 4578 | mToastQueue.remove(index); |
| 4579 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4580 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4581 | if (mToastQueue.size() > 0) { |
| 4582 | record = mToastQueue.get(0); |
| 4583 | } else { |
| 4584 | record = null; |
| 4585 | } |
| 4586 | } |
| 4587 | } |
| 4588 | } |
| 4589 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4590 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4591 | void cancelToastLocked(int index) { |
| 4592 | ToastRecord record = mToastQueue.get(index); |
| 4593 | try { |
| 4594 | record.callback.hide(); |
| 4595 | } catch (RemoteException e) { |
| 4596 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| 4597 | + " in package " + record.pkg); |
| 4598 | // don't worry about this, we're about to remove it from |
| 4599 | // the list anyway |
| 4600 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4601 | |
| 4602 | ToastRecord lastToast = mToastQueue.remove(index); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 4603 | mWindowManagerInternal.removeWindowToken(lastToast.token, true, DEFAULT_DISPLAY); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4604 | |
| 4605 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4606 | if (mToastQueue.size() > 0) { |
| 4607 | // Show the next one. If the callback fails, this will remove |
| 4608 | // it from the list, so don't assume that the list hasn't changed |
| 4609 | // after this point. |
| 4610 | showNextToastLocked(); |
| 4611 | } |
| 4612 | } |
| 4613 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4614 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4615 | private void scheduleTimeoutLocked(ToastRecord r) |
| 4616 | { |
| 4617 | mHandler.removeCallbacksAndMessages(r); |
| 4618 | Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r); |
| 4619 | long delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY; |
| 4620 | mHandler.sendMessageDelayed(m, delay); |
| 4621 | } |
| 4622 | |
| 4623 | private void handleTimeout(ToastRecord record) |
| 4624 | { |
| 4625 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| 4626 | synchronized (mToastQueue) { |
| 4627 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 4628 | if (index >= 0) { |
| 4629 | cancelToastLocked(index); |
| 4630 | } |
| 4631 | } |
| 4632 | } |
| 4633 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4634 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4635 | int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 4636 | { |
| 4637 | IBinder cbak = callback.asBinder(); |
| 4638 | ArrayList<ToastRecord> list = mToastQueue; |
| 4639 | int len = list.size(); |
| 4640 | for (int i=0; i<len; i++) { |
| 4641 | ToastRecord r = list.get(i); |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 4642 | if (r.pkg.equals(pkg) && r.callback.asBinder().equals(cbak)) { |
| 4643 | return i; |
| 4644 | } |
| 4645 | } |
| 4646 | return -1; |
| 4647 | } |
| 4648 | |
| 4649 | @GuardedBy("mToastQueue") |
| 4650 | int indexOfToastPackageLocked(String pkg) |
| 4651 | { |
| 4652 | ArrayList<ToastRecord> list = mToastQueue; |
| 4653 | int len = list.size(); |
| 4654 | for (int i=0; i<len; i++) { |
| 4655 | ToastRecord r = list.get(i); |
| 4656 | if (r.pkg.equals(pkg)) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4657 | return i; |
| 4658 | } |
| 4659 | } |
| 4660 | return -1; |
| 4661 | } |
| 4662 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4663 | @GuardedBy("mToastQueue") |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4664 | void keepProcessAliveIfNeededLocked(int pid) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4665 | { |
| 4666 | int toastCount = 0; // toasts from this pid |
| 4667 | ArrayList<ToastRecord> list = mToastQueue; |
| 4668 | int N = list.size(); |
| 4669 | for (int i=0; i<N; i++) { |
| 4670 | ToastRecord r = list.get(i); |
| 4671 | if (r.pid == pid) { |
| 4672 | toastCount++; |
| 4673 | } |
| 4674 | } |
| 4675 | try { |
Dianne Hackborn | f965f40 | 2017-05-04 23:27:23 -0700 | [diff] [blame] | 4676 | mAm.setProcessImportant(mForegroundToken, pid, toastCount > 0, "toast"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4677 | } catch (RemoteException e) { |
| 4678 | // Shouldn't happen. |
| 4679 | } |
| 4680 | } |
| 4681 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4682 | private void handleRankingReconsideration(Message message) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4683 | if (!(message.obj instanceof RankingReconsideration)) return; |
| 4684 | RankingReconsideration recon = (RankingReconsideration) message.obj; |
| 4685 | recon.run(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4686 | boolean changed; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4687 | synchronized (mNotificationLock) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4688 | final NotificationRecord record = mNotificationsByKey.get(recon.getKey()); |
| 4689 | if (record == null) { |
| 4690 | return; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4691 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4692 | int indexBefore = findNotificationRecordIndexLocked(record); |
| 4693 | boolean interceptBefore = record.isIntercepted(); |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4694 | float contactAffinityBefore = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4695 | int visibilityBefore = record.getPackageVisibilityOverride(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4696 | recon.applyChangesLocked(record); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4697 | applyZenModeLocked(record); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4698 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4699 | int indexAfter = findNotificationRecordIndexLocked(record); |
| 4700 | boolean interceptAfter = record.isIntercepted(); |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4701 | float contactAffinityAfter = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4702 | int visibilityAfter = record.getPackageVisibilityOverride(); |
| 4703 | changed = indexBefore != indexAfter || interceptBefore != interceptAfter |
| 4704 | || visibilityBefore != visibilityAfter; |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4705 | if (interceptBefore && !interceptAfter |
| 4706 | && Float.compare(contactAffinityBefore, contactAffinityAfter) != 0) { |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4707 | buzzBeepBlinkLocked(record); |
| 4708 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4709 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4710 | if (changed) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4711 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4712 | } |
| 4713 | } |
| 4714 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4715 | void handleRankingSort() { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 4716 | if (mRankingHelper == null) return; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4717 | synchronized (mNotificationLock) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4718 | final int N = mNotificationList.size(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4719 | // Any field that can change via one of the extractors needs to be added here. |
| 4720 | ArrayList<String> orderBefore = new ArrayList<>(N); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4721 | int[] visibilities = new int[N]; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4722 | boolean[] showBadges = new boolean[N]; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4723 | ArrayList<NotificationChannel> channelBefore = new ArrayList<>(N); |
| 4724 | ArrayList<String> groupKeyBefore = new ArrayList<>(N); |
| 4725 | ArrayList<ArrayList<String>> overridePeopleBefore = new ArrayList<>(N); |
| 4726 | ArrayList<ArrayList<SnoozeCriterion>> snoozeCriteriaBefore = new ArrayList<>(N); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4727 | ArrayList<Integer> userSentimentBefore = new ArrayList<>(N); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4728 | for (int i = 0; i < N; i++) { |
| 4729 | final NotificationRecord r = mNotificationList.get(i); |
| 4730 | orderBefore.add(r.getKey()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4731 | visibilities[i] = r.getPackageVisibilityOverride(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4732 | showBadges[i] = r.canShowBadge(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4733 | channelBefore.add(r.getChannel()); |
| 4734 | groupKeyBefore.add(r.getGroupKey()); |
| 4735 | overridePeopleBefore.add(r.getPeopleOverride()); |
| 4736 | snoozeCriteriaBefore.add(r.getSnoozeCriteria()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4737 | userSentimentBefore.add(r.getUserSentiment()); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4738 | mRankingHelper.extractSignals(r); |
| 4739 | } |
Chris Wren | 19a02b0 | 2015-12-22 10:34:22 -0500 | [diff] [blame] | 4740 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4741 | for (int i = 0; i < N; i++) { |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4742 | final NotificationRecord r = mNotificationList.get(i); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4743 | if (!orderBefore.get(i).equals(r.getKey()) |
Julia Reynolds | 6976669 | 2016-02-01 15:35:08 -0500 | [diff] [blame] | 4744 | || visibilities[i] != r.getPackageVisibilityOverride() |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4745 | || showBadges[i] != r.canShowBadge() |
| 4746 | || !Objects.equals(channelBefore.get(i), r.getChannel()) |
| 4747 | || !Objects.equals(groupKeyBefore.get(i), r.getGroupKey()) |
| 4748 | || !Objects.equals(overridePeopleBefore.get(i), r.getPeopleOverride()) |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4749 | || !Objects.equals(snoozeCriteriaBefore.get(i), r.getSnoozeCriteria()) |
| 4750 | || !Objects.equals(userSentimentBefore.get(i), r.getUserSentiment())) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4751 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4752 | return; |
| 4753 | } |
| 4754 | } |
| 4755 | } |
| 4756 | } |
| 4757 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4758 | @GuardedBy("mNotificationLock") |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 4759 | private void recordCallerLocked(NotificationRecord record) { |
| 4760 | if (mZenModeHelper.isCall(record)) { |
| 4761 | mZenModeHelper.recordCaller(record); |
| 4762 | } |
| 4763 | } |
| 4764 | |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 4765 | // let zen mode evaluate this record |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4766 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4767 | private void applyZenModeLocked(NotificationRecord record) { |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 4768 | record.setIntercepted(mZenModeHelper.shouldIntercept(record)); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4769 | if (record.isIntercepted()) { |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 4770 | int suppressed = (mZenModeHelper.shouldSuppressWhenScreenOff() |
| 4771 | ? SUPPRESSED_EFFECT_SCREEN_OFF : 0) |
| 4772 | | (mZenModeHelper.shouldSuppressWhenScreenOn() |
| 4773 | ? SUPPRESSED_EFFECT_SCREEN_ON : 0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4774 | record.setSuppressedVisualEffects(suppressed); |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 4775 | } else { |
| 4776 | record.setSuppressedVisualEffects(0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4777 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4778 | } |
| 4779 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4780 | @GuardedBy("mNotificationLock") |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4781 | private int findNotificationRecordIndexLocked(NotificationRecord target) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4782 | return mRankingHelper.indexOf(mNotificationList, target); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4783 | } |
| 4784 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4785 | private void handleSendRankingUpdate() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4786 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4787 | mListeners.notifyRankingUpdateLocked(); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4788 | } |
| 4789 | } |
| 4790 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4791 | private void scheduleListenerHintsChanged(int state) { |
| 4792 | mHandler.removeMessages(MESSAGE_LISTENER_HINTS_CHANGED); |
| 4793 | mHandler.obtainMessage(MESSAGE_LISTENER_HINTS_CHANGED, state, 0).sendToTarget(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4794 | } |
| 4795 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4796 | private void scheduleInterruptionFilterChanged(int listenerInterruptionFilter) { |
| 4797 | mHandler.removeMessages(MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED); |
| 4798 | mHandler.obtainMessage( |
| 4799 | MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED, |
| 4800 | listenerInterruptionFilter, |
| 4801 | 0).sendToTarget(); |
| 4802 | } |
| 4803 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4804 | private void handleListenerHintsChanged(int hints) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4805 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4806 | mListeners.notifyListenerHintsChangedLocked(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4807 | } |
| 4808 | } |
| 4809 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4810 | private void handleListenerInterruptionFilterChanged(int interruptionFilter) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4811 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4812 | mListeners.notifyInterruptionFilterChanged(interruptionFilter); |
| 4813 | } |
| 4814 | } |
| 4815 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4816 | protected class WorkerHandler extends Handler |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4817 | { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4818 | public WorkerHandler(Looper looper) { |
| 4819 | super(looper); |
| 4820 | } |
| 4821 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4822 | @Override |
| 4823 | public void handleMessage(Message msg) |
| 4824 | { |
| 4825 | switch (msg.what) |
| 4826 | { |
| 4827 | case MESSAGE_TIMEOUT: |
| 4828 | handleTimeout((ToastRecord)msg.obj); |
| 4829 | break; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 4830 | case MESSAGE_SAVE_POLICY_FILE: |
| 4831 | handleSavePolicyFile(); |
| 4832 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4833 | case MESSAGE_SEND_RANKING_UPDATE: |
| 4834 | handleSendRankingUpdate(); |
| 4835 | break; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4836 | case MESSAGE_LISTENER_HINTS_CHANGED: |
| 4837 | handleListenerHintsChanged(msg.arg1); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4838 | break; |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4839 | case MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED: |
| 4840 | handleListenerInterruptionFilterChanged(msg.arg1); |
| 4841 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4842 | } |
| 4843 | } |
| 4844 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4845 | protected void scheduleSendRankingUpdate() { |
| 4846 | if (!hasMessages(MESSAGE_SEND_RANKING_UPDATE)) { |
| 4847 | Message m = Message.obtain(this, MESSAGE_SEND_RANKING_UPDATE); |
| 4848 | sendMessage(m); |
| 4849 | } |
| 4850 | } |
| 4851 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4852 | } |
| 4853 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4854 | private final class RankingHandlerWorker extends Handler implements RankingHandler |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4855 | { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4856 | public RankingHandlerWorker(Looper looper) { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4857 | super(looper); |
| 4858 | } |
| 4859 | |
| 4860 | @Override |
| 4861 | public void handleMessage(Message msg) { |
| 4862 | switch (msg.what) { |
| 4863 | case MESSAGE_RECONSIDER_RANKING: |
| 4864 | handleRankingReconsideration(msg); |
| 4865 | break; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4866 | case MESSAGE_RANKING_SORT: |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4867 | handleRankingSort(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4868 | break; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4869 | } |
| 4870 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4871 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4872 | public void requestSort() { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4873 | removeMessages(MESSAGE_RANKING_SORT); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4874 | Message msg = Message.obtain(); |
| 4875 | msg.what = MESSAGE_RANKING_SORT; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4876 | sendMessage(msg); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | public void requestReconsideration(RankingReconsideration recon) { |
| 4880 | Message m = Message.obtain(this, |
| 4881 | NotificationManagerService.MESSAGE_RECONSIDER_RANKING, recon); |
| 4882 | long delay = recon.getDelay(TimeUnit.MILLISECONDS); |
| 4883 | sendMessageDelayed(m, delay); |
| 4884 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4885 | } |
| 4886 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4887 | // Notifications |
| 4888 | // ============================================================================ |
| 4889 | static int clamp(int x, int low, int high) { |
| 4890 | return (x < low) ? low : ((x > high) ? high : x); |
| 4891 | } |
| 4892 | |
| 4893 | void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
Eugene Susla | d4128ec | 2017-12-04 19:48:41 +0000 | [diff] [blame] | 4894 | if (!mAccessibilityManager.isEnabled()) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 4895 | return; |
| 4896 | } |
| 4897 | |
| 4898 | AccessibilityEvent event = |
| 4899 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 4900 | event.setPackageName(packageName); |
| 4901 | event.setClassName(Notification.class.getName()); |
| 4902 | event.setParcelableData(notification); |
| 4903 | CharSequence tickerText = notification.tickerText; |
| 4904 | if (!TextUtils.isEmpty(tickerText)) { |
| 4905 | event.getText().add(tickerText); |
| 4906 | } |
| 4907 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4908 | mAccessibilityManager.sendAccessibilityEvent(event); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 4909 | } |
| 4910 | |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4911 | /** |
| 4912 | * Removes all NotificationsRecords with the same key as the given notification record |
| 4913 | * from both lists. Do not call this method while iterating over either list. |
| 4914 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4915 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4916 | private boolean removeFromNotificationListsLocked(NotificationRecord r) { |
| 4917 | // Remove from both lists, either list could have a separate Record for what is |
| 4918 | // effectively the same notification. |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4919 | boolean wasPosted = false; |
| 4920 | NotificationRecord recordInList = null; |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4921 | if ((recordInList = findNotificationByListLocked(mNotificationList, r.getKey())) |
| 4922 | != null) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4923 | mNotificationList.remove(recordInList); |
| 4924 | mNotificationsByKey.remove(recordInList.sbn.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4925 | wasPosted = true; |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4926 | } |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4927 | while ((recordInList = findNotificationByListLocked(mEnqueuedNotifications, r.getKey())) |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4928 | != null) { |
| 4929 | mEnqueuedNotifications.remove(recordInList); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4930 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4931 | return wasPosted; |
| 4932 | } |
| 4933 | |
| 4934 | @GuardedBy("mNotificationLock") |
| 4935 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason, |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4936 | boolean wasPosted, String listenerName) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4937 | final String canceledKey = r.getKey(); |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 4938 | |
| 4939 | // Record caller. |
| 4940 | recordCallerLocked(r); |
| 4941 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4942 | if (r.getStats().getDismissalSurface() == NotificationStats.DISMISSAL_NOT_DISMISSED) { |
| 4943 | r.recordDismissalSurface(NotificationStats.DISMISSAL_OTHER); |
| 4944 | } |
| 4945 | |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 4946 | // Revoke permissions |
| 4947 | revokeUriPermissions(null, r); |
| 4948 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4949 | // tell the app |
| 4950 | if (sendDelete) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4951 | if (r.getNotification().deleteIntent != null) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4952 | try { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4953 | r.getNotification().deleteIntent.send(); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4954 | } catch (PendingIntent.CanceledException ex) { |
| 4955 | // do nothing - there's no relevant way to recover, and |
| 4956 | // no reason to let this propagate |
Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 4957 | Slog.w(TAG, "canceled PendingIntent for " + r.sbn.getPackageName(), ex); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4958 | } |
| 4959 | } |
| 4960 | } |
| 4961 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4962 | // Only cancel these if this notification actually got to be posted. |
| 4963 | if (wasPosted) { |
| 4964 | // status bar |
| 4965 | if (r.getNotification().getSmallIcon() != null) { |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 4966 | if (reason != REASON_SNOOZED) { |
| 4967 | r.isCanceled = true; |
| 4968 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4969 | mListeners.notifyRemovedLocked(r.sbn, reason, r.getStats()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4970 | mHandler.post(new Runnable() { |
| 4971 | @Override |
| 4972 | public void run() { |
| 4973 | mGroupHelper.onNotificationRemoved(r.sbn); |
| 4974 | } |
| 4975 | }); |
| 4976 | } |
| 4977 | |
| 4978 | // sound |
| 4979 | if (canceledKey.equals(mSoundNotificationKey)) { |
| 4980 | mSoundNotificationKey = null; |
| 4981 | final long identity = Binder.clearCallingIdentity(); |
| 4982 | try { |
| 4983 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4984 | if (player != null) { |
| 4985 | player.stopAsync(); |
| 4986 | } |
| 4987 | } catch (RemoteException e) { |
| 4988 | } finally { |
| 4989 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4990 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4991 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4992 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4993 | // vibrate |
| 4994 | if (canceledKey.equals(mVibrateNotificationKey)) { |
| 4995 | mVibrateNotificationKey = null; |
| 4996 | long identity = Binder.clearCallingIdentity(); |
| 4997 | try { |
| 4998 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 4999 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5000 | finally { |
| 5001 | Binder.restoreCallingIdentity(identity); |
| 5002 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5003 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5004 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5005 | // light |
| 5006 | mLights.remove(canceledKey); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5007 | } |
| 5008 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5009 | // Record usage stats |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5010 | // TODO: add unbundling stats? |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5011 | switch (reason) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 5012 | case REASON_CANCEL: |
| 5013 | case REASON_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5014 | case REASON_LISTENER_CANCEL: |
| 5015 | case REASON_LISTENER_CANCEL_ALL: |
| 5016 | mUsageStats.registerDismissedByUser(r); |
| 5017 | break; |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 5018 | case REASON_APP_CANCEL: |
| 5019 | case REASON_APP_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5020 | mUsageStats.registerRemovedByApp(r); |
| 5021 | break; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5022 | } |
| 5023 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5024 | String groupKey = r.getGroupKey(); |
| 5025 | NotificationRecord groupSummary = mSummaryByGroupKey.get(groupKey); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5026 | if (groupSummary != null && groupSummary.getKey().equals(canceledKey)) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5027 | mSummaryByGroupKey.remove(groupKey); |
| 5028 | } |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 5029 | final ArrayMap<String, String> summaries = mAutobundledSummaries.get(r.sbn.getUserId()); |
| 5030 | if (summaries != null && r.sbn.getKey().equals(summaries.get(r.sbn.getPackageName()))) { |
| 5031 | summaries.remove(r.sbn.getPackageName()); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5032 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5033 | |
Daniel Sandler | 23d7c70 | 2013-03-07 16:32:06 -0500 | [diff] [blame] | 5034 | // Save it for users of getHistoricalNotifications() |
| 5035 | mArchive.record(r.sbn); |
Christoph Studer | 81e5b5f | 2014-10-22 17:19:56 +0200 | [diff] [blame] | 5036 | |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 5037 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 5038 | MetricsLogger.action(r.getLogMaker(now) |
| 5039 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
| 5040 | .setType(MetricsEvent.TYPE_DISMISS) |
| 5041 | .setSubtype(reason)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 5042 | EventLogTags.writeNotificationCanceled(canceledKey, reason, |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5043 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now), listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5044 | } |
| 5045 | |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 5046 | void revokeUriPermissions(NotificationRecord newRecord, NotificationRecord oldRecord) { |
| 5047 | Set<Uri> oldUris = oldRecord.getNotificationUris(); |
| 5048 | Set<Uri> newUris = newRecord == null ? new HashSet<>() : newRecord.getNotificationUris(); |
| 5049 | oldUris.removeAll(newUris); |
| 5050 | |
| 5051 | long ident = Binder.clearCallingIdentity(); |
| 5052 | try { |
| 5053 | for (Uri uri : oldUris) { |
| 5054 | if (uri != null) { |
| 5055 | int notiUserId = oldRecord.getUserId(); |
| 5056 | int sourceUserId = notiUserId == USER_ALL ? USER_SYSTEM |
| 5057 | : ContentProvider.getUserIdFromUri(uri, notiUserId); |
| 5058 | uri = ContentProvider.getUriWithoutUserId(uri); |
| 5059 | mAm.revokeUriPermissionFromOwner(mPermissionOwner, |
| 5060 | uri, Intent.FLAG_GRANT_READ_URI_PERMISSION, sourceUserId); |
| 5061 | } |
| 5062 | } |
| 5063 | } catch (RemoteException e) { |
| 5064 | Log.e(TAG, "Count not revoke uri permissions", e); |
| 5065 | } finally { |
| 5066 | Binder.restoreCallingIdentity(ident); |
| 5067 | } |
| 5068 | } |
| 5069 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5070 | /** |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 5071 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5072 | * and none of the {@code mustNotHaveFlags}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5073 | */ |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 5074 | void cancelNotification(final int callingUid, final int callingPid, |
| 5075 | final String pkg, final String tag, final int id, |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5076 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5077 | final int userId, final int reason, final ManagedServiceInfo listener) { |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5078 | // In enqueueNotificationInternal notifications are added by scheduling the |
| 5079 | // work on the worker handler. Hence, we also schedule the cancel on this |
| 5080 | // handler to avoid a scenario where an add notification call followed by a |
| 5081 | // remove notification call ends up in not removing the notification. |
| 5082 | mHandler.post(new Runnable() { |
| 5083 | @Override |
| 5084 | public void run() { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5085 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Chris Wren | bddb5bc | 2015-03-04 08:47:46 -0800 | [diff] [blame] | 5086 | if (DBG) EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, id, tag, |
| 5087 | userId, mustHaveFlags, mustNotHaveFlags, reason, listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5088 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5089 | synchronized (mNotificationLock) { |
| 5090 | // Look for the notification, searching both the posted and enqueued lists. |
| 5091 | NotificationRecord r = findNotificationLocked(pkg, tag, id, userId); |
| 5092 | if (r != null) { |
| 5093 | // The notification was found, check if it should be removed. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 5094 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5095 | // Ideally we'd do this in the caller of this method. However, that would |
| 5096 | // require the caller to also find the notification. |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 5097 | if (reason == REASON_CLICK) { |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5098 | mUsageStats.registerClickedByUser(r); |
| 5099 | } |
| 5100 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5101 | if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) { |
| 5102 | return; |
| 5103 | } |
| 5104 | if ((r.getNotification().flags & mustNotHaveFlags) != 0) { |
| 5105 | return; |
| 5106 | } |
| 5107 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5108 | // Cancel the notification. |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5109 | boolean wasPosted = removeFromNotificationListsLocked(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5110 | cancelNotificationLocked(r, sendDelete, reason, wasPosted, listenerName); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5111 | cancelGroupChildrenLocked(r, callingUid, callingPid, listenerName, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5112 | sendDelete, null); |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5113 | updateLightsLocked(); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 5114 | } else { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5115 | // No notification was found, assume that it is snoozed and cancel it. |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 5116 | if (reason != REASON_SNOOZED) { |
| 5117 | final boolean wasSnoozed = mSnoozeHelper.cancel(userId, pkg, tag, id); |
| 5118 | if (wasSnoozed) { |
| 5119 | savePolicyFile(); |
| 5120 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 5121 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5122 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5123 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5124 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 5125 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5126 | } |
| 5127 | |
| 5128 | /** |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 5129 | * Determine whether the userId applies to the notification in question, either because |
| 5130 | * they match exactly, or one of them is USER_ALL (which is treated as a wildcard). |
| 5131 | */ |
| 5132 | private boolean notificationMatchesUserId(NotificationRecord r, int userId) { |
| 5133 | return |
| 5134 | // looking for USER_ALL notifications? match everything |
| 5135 | userId == UserHandle.USER_ALL |
| 5136 | // a notification sent to USER_ALL matches any query |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 5137 | || r.getUserId() == UserHandle.USER_ALL |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 5138 | // an exact user match |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 5139 | || r.getUserId() == userId; |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | /** |
Kenny Guy | 3a7c4a56 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5143 | * Determine whether the userId applies to the notification in question, either because |
| 5144 | * 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] | 5145 | * because it matches one of the users profiles. |
Kenny Guy | 3a7c4a56 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5146 | */ |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 5147 | private boolean notificationMatchesCurrentProfiles(NotificationRecord r, int userId) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5148 | return notificationMatchesUserId(r, userId) |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 5149 | || mUserProfiles.isCurrentProfile(r.getUserId()); |
Kenny Guy | 3a7c4a56 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5150 | } |
| 5151 | |
| 5152 | /** |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 5153 | * 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] | 5154 | * {@code mustHaveFlags}. |
| 5155 | */ |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5156 | void cancelAllNotificationsInt(int callingUid, int callingPid, String pkg, String channelId, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 5157 | int mustHaveFlags, int mustNotHaveFlags, boolean doit, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5158 | ManagedServiceInfo listener) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5159 | mHandler.post(new Runnable() { |
| 5160 | @Override |
| 5161 | public void run() { |
| 5162 | String listenerName = listener == null ? null : listener.component.toShortString(); |
| 5163 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 5164 | pkg, userId, mustHaveFlags, mustNotHaveFlags, reason, |
| 5165 | listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5166 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5167 | // Why does this parameter exist? Do we actually want to execute the above if doit |
| 5168 | // is false? |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 5169 | if (!doit) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5170 | return; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 5171 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5172 | |
| 5173 | synchronized (mNotificationLock) { |
| 5174 | FlagChecker flagChecker = (int flags) -> { |
| 5175 | if ((flags & mustHaveFlags) != mustHaveFlags) { |
| 5176 | return false; |
| 5177 | } |
| 5178 | if ((flags & mustNotHaveFlags) != 0) { |
| 5179 | return false; |
| 5180 | } |
| 5181 | return true; |
| 5182 | }; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5183 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 5184 | pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, flagChecker, |
| 5185 | false /*includeCurrentProfiles*/, userId, false /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5186 | listenerName, true /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5187 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 5188 | callingPid, pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, |
| 5189 | flagChecker, false /*includeCurrentProfiles*/, userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5190 | false /*sendDelete*/, reason, listenerName, false /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5191 | mSnoozeHelper.cancel(userId, pkg); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5192 | } |
| 5193 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5194 | }); |
| 5195 | } |
| 5196 | |
| 5197 | private interface FlagChecker { |
| 5198 | // Returns false if these flags do not pass the defined flag test. |
| 5199 | public boolean apply(int flags); |
| 5200 | } |
| 5201 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5202 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5203 | private void cancelAllNotificationsByListLocked(ArrayList<NotificationRecord> notificationList, |
| 5204 | int callingUid, int callingPid, String pkg, boolean nullPkgIndicatesUserSwitch, |
| 5205 | String channelId, FlagChecker flagChecker, boolean includeCurrentProfiles, int userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5206 | boolean sendDelete, int reason, String listenerName, boolean wasPosted) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5207 | ArrayList<NotificationRecord> canceledNotifications = null; |
| 5208 | for (int i = notificationList.size() - 1; i >= 0; --i) { |
| 5209 | NotificationRecord r = notificationList.get(i); |
| 5210 | if (includeCurrentProfiles) { |
| 5211 | if (!notificationMatchesCurrentProfiles(r, userId)) { |
| 5212 | continue; |
| 5213 | } |
| 5214 | } else if (!notificationMatchesUserId(r, userId)) { |
| 5215 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5216 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5217 | // Don't remove notifications to all, if there's no package name specified |
| 5218 | if (nullPkgIndicatesUserSwitch && pkg == null && r.getUserId() == UserHandle.USER_ALL) { |
| 5219 | continue; |
| 5220 | } |
| 5221 | if (!flagChecker.apply(r.getFlags())) { |
| 5222 | continue; |
| 5223 | } |
| 5224 | if (pkg != null && !r.sbn.getPackageName().equals(pkg)) { |
| 5225 | continue; |
| 5226 | } |
| 5227 | if (channelId != null && !channelId.equals(r.getChannel().getId())) { |
| 5228 | continue; |
| 5229 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5230 | if (canceledNotifications == null) { |
| 5231 | canceledNotifications = new ArrayList<>(); |
| 5232 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5233 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 5234 | mNotificationsByKey.remove(r.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5235 | canceledNotifications.add(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5236 | cancelNotificationLocked(r, sendDelete, reason, wasPosted, listenerName); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5237 | } |
| 5238 | if (canceledNotifications != null) { |
| 5239 | final int M = canceledNotifications.size(); |
| 5240 | for (int i = 0; i < M; i++) { |
| 5241 | cancelGroupChildrenLocked(canceledNotifications.get(i), callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5242 | listenerName, false /* sendDelete */, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5243 | } |
| 5244 | updateLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5245 | } |
| 5246 | } |
| 5247 | |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 5248 | void snoozeNotificationInt(String key, long duration, String snoozeCriterionId, |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5249 | ManagedServiceInfo listener) { |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5250 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 5251 | if (duration <= 0 && snoozeCriterionId == null || key == null) { |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 5252 | return; |
| 5253 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 5254 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5255 | if (DBG) { |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 5256 | Slog.d(TAG, String.format("snooze event(%s, %d, %s, %s)", key, duration, |
| 5257 | snoozeCriterionId, listenerName)); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5258 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5259 | // Needs to post so that it can cancel notifications not yet enqueued. |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5260 | mHandler.post(new SnoozeNotificationRunnable(key, duration, snoozeCriterionId)); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 5261 | } |
| 5262 | |
| 5263 | void unsnoozeNotificationInt(String key, ManagedServiceInfo listener) { |
| 5264 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 5265 | if (DBG) { |
| 5266 | Slog.d(TAG, String.format("unsnooze event(%s, %s)", key, listenerName)); |
| 5267 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5268 | mSnoozeHelper.repost(key); |
| 5269 | savePolicyFile(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 5270 | } |
| 5271 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5272 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 350159c | 2014-03-27 11:15:11 -0700 | [diff] [blame] | 5273 | void cancelAllLocked(int callingUid, int callingPid, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5274 | ManagedServiceInfo listener, boolean includeCurrentProfiles) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5275 | mHandler.post(new Runnable() { |
| 5276 | @Override |
| 5277 | public void run() { |
| 5278 | synchronized (mNotificationLock) { |
| 5279 | String listenerName = |
| 5280 | listener == null ? null : listener.component.toShortString(); |
| 5281 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 5282 | null, userId, 0, 0, reason, listenerName); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5283 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5284 | FlagChecker flagChecker = (int flags) -> { |
| 5285 | if ((flags & (Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR)) |
| 5286 | != 0) { |
| 5287 | return false; |
| 5288 | } |
| 5289 | return true; |
| 5290 | }; |
| 5291 | |
| 5292 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 5293 | null, false /*nullPkgIndicatesUserSwitch*/, null, flagChecker, |
| 5294 | includeCurrentProfiles, userId, true /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5295 | listenerName, true); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5296 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 5297 | callingPid, null, false /*nullPkgIndicatesUserSwitch*/, null, |
| 5298 | flagChecker, includeCurrentProfiles, userId, true /*sendDelete*/, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5299 | reason, listenerName, false); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5300 | mSnoozeHelper.cancel(userId, includeCurrentProfiles); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5301 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5302 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5303 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5304 | } |
| 5305 | |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5306 | // Warning: The caller is responsible for invoking updateLightsLocked(). |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5307 | @GuardedBy("mNotificationLock") |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5308 | private void cancelGroupChildrenLocked(NotificationRecord r, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5309 | String listenerName, boolean sendDelete, FlagChecker flagChecker) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5310 | Notification n = r.getNotification(); |
Christoph Studer | 3f31f5d | 2014-07-31 16:55:32 +0200 | [diff] [blame] | 5311 | if (!n.isGroupSummary()) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5312 | return; |
| 5313 | } |
| 5314 | |
| 5315 | String pkg = r.sbn.getPackageName(); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5316 | |
| 5317 | if (pkg == null) { |
| 5318 | if (DBG) Log.e(TAG, "No package for group summary: " + r.getKey()); |
| 5319 | return; |
| 5320 | } |
| 5321 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5322 | cancelGroupChildrenByListLocked(mNotificationList, r, callingUid, callingPid, listenerName, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5323 | sendDelete, true, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5324 | cancelGroupChildrenByListLocked(mEnqueuedNotifications, r, callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5325 | listenerName, sendDelete, false, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5326 | } |
| 5327 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5328 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5329 | private void cancelGroupChildrenByListLocked(ArrayList<NotificationRecord> notificationList, |
| 5330 | NotificationRecord parentNotification, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5331 | String listenerName, boolean sendDelete, boolean wasPosted, FlagChecker flagChecker) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5332 | final String pkg = parentNotification.sbn.getPackageName(); |
| 5333 | final int userId = parentNotification.getUserId(); |
| 5334 | final int reason = REASON_GROUP_SUMMARY_CANCELED; |
| 5335 | for (int i = notificationList.size() - 1; i >= 0; i--) { |
| 5336 | final NotificationRecord childR = notificationList.get(i); |
| 5337 | final StatusBarNotification childSbn = childR.sbn; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5338 | if ((childSbn.isGroup() && !childSbn.getNotification().isGroupSummary()) && |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5339 | childR.getGroupKey().equals(parentNotification.getGroupKey()) |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5340 | && (childR.getFlags() & Notification.FLAG_FOREGROUND_SERVICE) == 0 |
| 5341 | && (flagChecker == null || flagChecker.apply(childR.getFlags()))) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5342 | EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, childSbn.getId(), |
| 5343 | childSbn.getTag(), userId, 0, 0, reason, listenerName); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5344 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 5345 | mNotificationsByKey.remove(childR.getKey()); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5346 | cancelNotificationLocked(childR, sendDelete, reason, wasPosted, listenerName); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5347 | } |
| 5348 | } |
| 5349 | } |
| 5350 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5351 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5352 | void updateLightsLocked() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5353 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5354 | // handle notification lights |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 5355 | NotificationRecord ledNotification = null; |
| 5356 | while (ledNotification == null && !mLights.isEmpty()) { |
| 5357 | final String owner = mLights.get(mLights.size() - 1); |
| 5358 | ledNotification = mNotificationsByKey.get(owner); |
| 5359 | if (ledNotification == null) { |
| 5360 | Slog.wtfStack(TAG, "LED Notification does not exist: " + owner); |
| 5361 | mLights.remove(owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5362 | } |
| 5363 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 5364 | |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 5365 | // 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] | 5366 | if (ledNotification == null || mInCall || mScreenOn) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 5367 | mNotificationLight.turnOff(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5368 | } else { |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 5369 | NotificationRecord.Light light = ledNotification.getLight(); |
| 5370 | if (light != null && mNotificationPulseEnabled) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 5371 | // pulse repeatedly |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 5372 | mNotificationLight.setFlashing(light.color, Light.LIGHT_FLASH_TIMED, |
| 5373 | light.onMs, light.offMs); |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 5374 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5375 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5376 | } |
| 5377 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5378 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5379 | @NonNull List<NotificationRecord> findGroupNotificationsLocked(String pkg, |
| 5380 | String groupKey, int userId) { |
| 5381 | List<NotificationRecord> records = new ArrayList<>(); |
| 5382 | records.addAll(findGroupNotificationByListLocked(mNotificationList, pkg, groupKey, userId)); |
| 5383 | records.addAll( |
| 5384 | findGroupNotificationByListLocked(mEnqueuedNotifications, pkg, groupKey, userId)); |
| 5385 | return records; |
| 5386 | } |
| 5387 | |
| 5388 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5389 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5390 | private @NonNull List<NotificationRecord> findGroupNotificationByListLocked( |
| 5391 | ArrayList<NotificationRecord> list, String pkg, String groupKey, int userId) { |
| 5392 | List<NotificationRecord> records = new ArrayList<>(); |
| 5393 | final int len = list.size(); |
| 5394 | for (int i = 0; i < len; i++) { |
| 5395 | NotificationRecord r = list.get(i); |
| 5396 | if (notificationMatchesUserId(r, userId) && r.getGroupKey().equals(groupKey) |
| 5397 | && r.sbn.getPackageName().equals(pkg)) { |
| 5398 | records.add(r); |
| 5399 | } |
| 5400 | } |
| 5401 | return records; |
| 5402 | } |
| 5403 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5404 | // Searches both enqueued and posted notifications by key. |
| 5405 | // TODO: need to combine a bunch of these getters with slightly different behavior. |
| 5406 | // TODO: Should enqueuing just add to mNotificationsByKey instead? |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5407 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5408 | private NotificationRecord findNotificationByKeyLocked(String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5409 | NotificationRecord r; |
| 5410 | if ((r = findNotificationByListLocked(mNotificationList, key)) != null) { |
| 5411 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5412 | } |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5413 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, key)) != null) { |
| 5414 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5415 | } |
| 5416 | return null; |
| 5417 | } |
| 5418 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5419 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5420 | NotificationRecord findNotificationLocked(String pkg, String tag, int id, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5421 | NotificationRecord r; |
| 5422 | if ((r = findNotificationByListLocked(mNotificationList, pkg, tag, id, userId)) != null) { |
| 5423 | return r; |
| 5424 | } |
| 5425 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, pkg, tag, id, userId)) |
| 5426 | != null) { |
| 5427 | return r; |
| 5428 | } |
| 5429 | return null; |
| 5430 | } |
| 5431 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5432 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5433 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5434 | String pkg, String tag, int id, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5435 | final int len = list.size(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5436 | for (int i = 0; i < len; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5437 | NotificationRecord r = list.get(i); |
Vladimir Marko | 2526f33 | 2013-09-11 11:13:55 +0100 | [diff] [blame] | 5438 | if (notificationMatchesUserId(r, userId) && r.sbn.getId() == id && |
| 5439 | TextUtils.equals(r.sbn.getTag(), tag) && r.sbn.getPackageName().equals(pkg)) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5440 | return r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5441 | } |
| 5442 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5443 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5444 | } |
| 5445 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5446 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5447 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5448 | String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5449 | final int N = list.size(); |
| 5450 | for (int i = 0; i < N; i++) { |
| 5451 | if (key.equals(list.get(i).getKey())) { |
| 5452 | return list.get(i); |
| 5453 | } |
| 5454 | } |
| 5455 | return null; |
| 5456 | } |
| 5457 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5458 | @GuardedBy("mNotificationLock") |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 5459 | int indexOfNotificationLocked(String key) { |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 5460 | final int N = mNotificationList.size(); |
| 5461 | for (int i = 0; i < N; i++) { |
| 5462 | if (key.equals(mNotificationList.get(i).getKey())) { |
| 5463 | return i; |
| 5464 | } |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 5465 | } |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 5466 | return -1; |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 5467 | } |
| 5468 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 5469 | private void updateNotificationPulse() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5470 | synchronized (mNotificationLock) { |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 5471 | updateLightsLocked(); |
| 5472 | } |
| 5473 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 5474 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5475 | protected boolean isCallingUidSystem() { |
| 5476 | final int uid = Binder.getCallingUid(); |
| 5477 | return uid == Process.SYSTEM_UID; |
| 5478 | } |
| 5479 | |
| 5480 | protected boolean isUidSystemOrPhone(int uid) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5481 | final int appid = UserHandle.getAppId(uid); |
| 5482 | return (appid == Process.SYSTEM_UID || appid == Process.PHONE_UID || uid == 0); |
| 5483 | } |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 5484 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5485 | // TODO: Most calls should probably move to isCallerSystem. |
| 5486 | protected boolean isCallerSystemOrPhone() { |
| 5487 | return isUidSystemOrPhone(Binder.getCallingUid()); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5488 | } |
| 5489 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5490 | private void checkCallerIsSystemOrShell() { |
| 5491 | if (Binder.getCallingUid() == Process.SHELL_UID) { |
| 5492 | return; |
| 5493 | } |
| 5494 | checkCallerIsSystem(); |
| 5495 | } |
| 5496 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5497 | private void checkCallerIsSystem() { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5498 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5499 | return; |
| 5500 | } |
| 5501 | throw new SecurityException("Disallowed call for uid " + Binder.getCallingUid()); |
| 5502 | } |
| 5503 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5504 | private void checkCallerIsSystemOrSameApp(String pkg) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5505 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5506 | return; |
| 5507 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 5508 | checkCallerIsSameApp(pkg); |
| 5509 | } |
| 5510 | |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 5511 | private boolean isCallerInstantApp(String pkg) { |
| 5512 | // System is always allowed to act for ephemeral apps. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5513 | if (isCallerSystemOrPhone()) { |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 5514 | return false; |
| 5515 | } |
| 5516 | |
| 5517 | mAppOps.checkPackage(Binder.getCallingUid(), pkg); |
| 5518 | |
| 5519 | try { |
| 5520 | ApplicationInfo ai = mPackageManager.getApplicationInfo(pkg, 0, |
| 5521 | UserHandle.getCallingUserId()); |
| 5522 | if (ai == null) { |
| 5523 | throw new SecurityException("Unknown package " + pkg); |
| 5524 | } |
| 5525 | return ai.isInstantApp(); |
| 5526 | } catch (RemoteException re) { |
| 5527 | throw new SecurityException("Unknown package " + pkg, re); |
| 5528 | } |
| 5529 | |
| 5530 | } |
| 5531 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5532 | private void checkCallerIsSameApp(String pkg) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5533 | final int uid = Binder.getCallingUid(); |
| 5534 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5535 | ApplicationInfo ai = mPackageManager.getApplicationInfo( |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5536 | pkg, 0, UserHandle.getCallingUserId()); |
Dan Sandler | 09afc2e | 2014-07-18 14:29:20 -0400 | [diff] [blame] | 5537 | if (ai == null) { |
| 5538 | throw new SecurityException("Unknown package " + pkg); |
| 5539 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5540 | if (!UserHandle.isSameApp(ai.uid, uid)) { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5541 | throw new SecurityException("Calling uid " + uid + " gave package " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5542 | + pkg + " which is owned by uid " + ai.uid); |
| 5543 | } |
| 5544 | } catch (RemoteException re) { |
| 5545 | throw new SecurityException("Unknown package " + pkg + "\n" + re); |
| 5546 | } |
| 5547 | } |
| 5548 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 5549 | private static String callStateToString(int state) { |
| 5550 | switch (state) { |
| 5551 | case TelephonyManager.CALL_STATE_IDLE: return "CALL_STATE_IDLE"; |
| 5552 | case TelephonyManager.CALL_STATE_RINGING: return "CALL_STATE_RINGING"; |
| 5553 | case TelephonyManager.CALL_STATE_OFFHOOK: return "CALL_STATE_OFFHOOK"; |
| 5554 | default: return "CALL_STATE_UNKNOWN_" + state; |
| 5555 | } |
| 5556 | } |
| 5557 | |
| 5558 | private void listenForCallState() { |
| 5559 | TelephonyManager.from(getContext()).listen(new PhoneStateListener() { |
| 5560 | @Override |
| 5561 | public void onCallStateChanged(int state, String incomingNumber) { |
| 5562 | if (mCallState == state) return; |
| 5563 | if (DBG) Slog.d(TAG, "Call state changed: " + callStateToString(state)); |
| 5564 | mCallState = state; |
| 5565 | } |
| 5566 | }, PhoneStateListener.LISTEN_CALL_STATE); |
| 5567 | } |
| 5568 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5569 | /** |
| 5570 | * Generates a NotificationRankingUpdate from 'sbns', considering only |
| 5571 | * notifications visible to the given listener. |
| 5572 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5573 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5574 | private NotificationRankingUpdate makeRankingUpdateLocked(ManagedServiceInfo info) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5575 | final int N = mNotificationList.size(); |
| 5576 | ArrayList<String> keys = new ArrayList<String>(N); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 5577 | ArrayList<String> interceptedKeys = new ArrayList<String>(N); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5578 | ArrayList<Integer> importance = new ArrayList<>(N); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5579 | Bundle overrideGroupKeys = new Bundle(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5580 | Bundle visibilityOverrides = new Bundle(); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 5581 | Bundle suppressedVisualEffects = new Bundle(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5582 | Bundle explanation = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5583 | Bundle channels = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5584 | Bundle overridePeople = new Bundle(); |
| 5585 | Bundle snoozeCriteria = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5586 | Bundle showBadge = new Bundle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5587 | Bundle userSentiment = new Bundle(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5588 | for (int i = 0; i < N; i++) { |
| 5589 | NotificationRecord record = mNotificationList.get(i); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5590 | if (!isVisibleToListener(record.sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5591 | continue; |
| 5592 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5593 | final String key = record.sbn.getKey(); |
| 5594 | keys.add(key); |
| 5595 | importance.add(record.getImportance()); |
| 5596 | if (record.getImportanceExplanation() != null) { |
| 5597 | explanation.putCharSequence(key, record.getImportanceExplanation()); |
| 5598 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5599 | if (record.isIntercepted()) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5600 | interceptedKeys.add(key); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 5601 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5602 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5603 | suppressedVisualEffects.putInt(key, record.getSuppressedVisualEffects()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5604 | if (record.getPackageVisibilityOverride() |
| 5605 | != NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5606 | visibilityOverrides.putInt(key, record.getPackageVisibilityOverride()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5607 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5608 | overrideGroupKeys.putString(key, record.sbn.getOverrideGroupKey()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5609 | channels.putParcelable(key, record.getChannel()); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5610 | overridePeople.putStringArrayList(key, record.getPeopleOverride()); |
| 5611 | snoozeCriteria.putParcelableArrayList(key, record.getSnoozeCriteria()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5612 | showBadge.putBoolean(key, record.canShowBadge()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5613 | userSentiment.putInt(key, record.getUserSentiment()); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5614 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5615 | final int M = keys.size(); |
| 5616 | String[] keysAr = keys.toArray(new String[M]); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 5617 | String[] interceptedKeysAr = interceptedKeys.toArray(new String[interceptedKeys.size()]); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5618 | int[] importanceAr = new int[M]; |
| 5619 | for (int i = 0; i < M; i++) { |
| 5620 | importanceAr[i] = importance.get(i); |
| 5621 | } |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5622 | return new NotificationRankingUpdate(keysAr, interceptedKeysAr, visibilityOverrides, |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5623 | suppressedVisualEffects, importanceAr, explanation, overrideGroupKeys, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5624 | channels, overridePeople, snoozeCriteria, showBadge, userSentiment); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5625 | } |
| 5626 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5627 | boolean hasCompanionDevice(ManagedServiceInfo info) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5628 | if (mCompanionManager == null) { |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 5629 | mCompanionManager = getCompanionManager(); |
| 5630 | } |
| 5631 | // Companion mgr doesn't exist on all device types |
| 5632 | if (mCompanionManager == null) { |
| 5633 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5634 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5635 | long identity = Binder.clearCallingIdentity(); |
| 5636 | try { |
| 5637 | List<String> associations = mCompanionManager.getAssociations( |
| 5638 | info.component.getPackageName(), info.userid); |
| 5639 | if (!ArrayUtils.isEmpty(associations)) { |
| 5640 | return true; |
| 5641 | } |
| 5642 | } catch (SecurityException se) { |
| 5643 | // Not a privileged listener |
| 5644 | } catch (RemoteException re) { |
| 5645 | Slog.e(TAG, "Cannot reach companion device service", re); |
| 5646 | } catch (Exception e) { |
| 5647 | Slog.e(TAG, "Cannot verify listener " + info, e); |
| 5648 | } finally { |
| 5649 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5650 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5651 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5652 | } |
| 5653 | |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 5654 | protected ICompanionDeviceManager getCompanionManager() { |
| 5655 | return ICompanionDeviceManager.Stub.asInterface( |
| 5656 | ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE)); |
| 5657 | } |
| 5658 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5659 | private boolean isVisibleToListener(StatusBarNotification sbn, ManagedServiceInfo listener) { |
| 5660 | if (!listener.enabledAndUserMatches(sbn.getUserId())) { |
| 5661 | return false; |
| 5662 | } |
Justin Koh | 8d11a5a | 2014-08-04 18:29:49 -0700 | [diff] [blame] | 5663 | // TODO: remove this for older listeners. |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5664 | return true; |
| 5665 | } |
| 5666 | |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 5667 | private boolean isPackageSuspendedForUser(String pkg, int uid) { |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5668 | int userId = UserHandle.getUserId(uid); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5669 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5670 | return mPackageManager.isPackageSuspendedForUser(pkg, userId); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5671 | } catch (RemoteException re) { |
| 5672 | throw new SecurityException("Could not talk to package manager service"); |
Andrei Stingaceanu | efc4a34 | 2016-03-22 14:43:01 +0000 | [diff] [blame] | 5673 | } catch (IllegalArgumentException ex) { |
| 5674 | // Package not found. |
| 5675 | return false; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5676 | } |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5677 | } |
| 5678 | |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 5679 | private boolean canUseManagedServices() { |
| 5680 | return !mActivityManager.isLowRamDevice() |
| 5681 | || mPackageManagerClient.hasSystemFeature(PackageManager.FEATURE_WATCH); |
| 5682 | } |
| 5683 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5684 | private class TrimCache { |
| 5685 | StatusBarNotification heavy; |
| 5686 | StatusBarNotification sbnClone; |
| 5687 | StatusBarNotification sbnCloneLight; |
| 5688 | |
| 5689 | TrimCache(StatusBarNotification sbn) { |
| 5690 | heavy = sbn; |
| 5691 | } |
| 5692 | |
| 5693 | StatusBarNotification ForListener(ManagedServiceInfo info) { |
| 5694 | if (mListeners.getOnNotificationPostedTrim(info) == TRIM_LIGHT) { |
| 5695 | if (sbnCloneLight == null) { |
| 5696 | sbnCloneLight = heavy.cloneLight(); |
| 5697 | } |
| 5698 | return sbnCloneLight; |
| 5699 | } else { |
| 5700 | if (sbnClone == null) { |
| 5701 | sbnClone = heavy.clone(); |
| 5702 | } |
| 5703 | return sbnClone; |
| 5704 | } |
| 5705 | } |
| 5706 | } |
| 5707 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5708 | public class NotificationAssistants extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5709 | static final String TAG_ENABLED_NOTIFICATION_ASSISTANTS = "enabled_assistants"; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5710 | |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 5711 | public NotificationAssistants(Context context, Object lock, UserProfiles up, |
| 5712 | IPackageManager pm) { |
| 5713 | super(context, lock, up, pm); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5714 | } |
| 5715 | |
| 5716 | @Override |
| 5717 | protected Config getConfig() { |
| 5718 | Config c = new Config(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5719 | c.caption = "notification assistant"; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5720 | c.serviceInterface = NotificationAssistantService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5721 | c.xmlTag = TAG_ENABLED_NOTIFICATION_ASSISTANTS; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5722 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_ASSISTANT; |
| 5723 | c.bindPermission = Manifest.permission.BIND_NOTIFICATION_ASSISTANT_SERVICE; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5724 | c.settingsAction = Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS; |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 5725 | c.clientLabel = R.string.notification_ranker_binding_label; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5726 | return c; |
| 5727 | } |
| 5728 | |
| 5729 | @Override |
| 5730 | protected IInterface asInterface(IBinder binder) { |
| 5731 | return INotificationListener.Stub.asInterface(binder); |
| 5732 | } |
| 5733 | |
| 5734 | @Override |
| 5735 | protected boolean checkType(IInterface service) { |
| 5736 | return service instanceof INotificationListener; |
| 5737 | } |
| 5738 | |
| 5739 | @Override |
| 5740 | protected void onServiceAdded(ManagedServiceInfo info) { |
| 5741 | mListeners.registerGuestService(info); |
| 5742 | } |
| 5743 | |
| 5744 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5745 | @GuardedBy("mNotificationLock") |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5746 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
| 5747 | mListeners.unregisterService(removed.service, removed.userid); |
| 5748 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5749 | |
Julia Reynolds | ef934fd | 2018-02-01 14:39:17 -0500 | [diff] [blame] | 5750 | @Override |
| 5751 | public void onUserUnlocked(int user) { |
| 5752 | if (DEBUG) Slog.d(TAG, "onUserUnlocked u=" + user); |
| 5753 | rebindServices(true); |
| 5754 | } |
| 5755 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5756 | public void onNotificationEnqueued(final NotificationRecord r) { |
| 5757 | final StatusBarNotification sbn = r.sbn; |
| 5758 | TrimCache trimCache = new TrimCache(sbn); |
| 5759 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5760 | // There should be only one, but it's a list, so while we enforce |
| 5761 | // singularity elsewhere, we keep it general here, to avoid surprises. |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5762 | for (final ManagedServiceInfo info : NotificationAssistants.this.getServices()) { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5763 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 5764 | if (!sbnVisible) { |
| 5765 | continue; |
| 5766 | } |
| 5767 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5768 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 5769 | mHandler.post(new Runnable() { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5770 | @Override |
| 5771 | public void run() { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5772 | notifyEnqueued(info, sbnToPost); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5773 | } |
| 5774 | }); |
| 5775 | } |
| 5776 | } |
| 5777 | |
| 5778 | private void notifyEnqueued(final ManagedServiceInfo info, |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5779 | final StatusBarNotification sbn) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5780 | final INotificationListener assistant = (INotificationListener) info.service; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5781 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
| 5782 | try { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5783 | assistant.onNotificationEnqueued(sbnHolder); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5784 | } catch (RemoteException ex) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5785 | Log.e(TAG, "unable to notify assistant (enqueued): " + assistant, ex); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5786 | } |
| 5787 | } |
| 5788 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5789 | /** |
| 5790 | * asynchronously notify the assistant that a notification has been snoozed until a |
| 5791 | * context |
| 5792 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5793 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5794 | public void notifyAssistantSnoozedLocked(final StatusBarNotification sbn, |
| 5795 | final String snoozeCriterionId) { |
| 5796 | TrimCache trimCache = new TrimCache(sbn); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5797 | for (final ManagedServiceInfo info : getServices()) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5798 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 5799 | if (!sbnVisible) { |
| 5800 | continue; |
| 5801 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5802 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
| 5803 | mHandler.post(new Runnable() { |
| 5804 | @Override |
| 5805 | public void run() { |
| 5806 | final INotificationListener assistant = |
| 5807 | (INotificationListener) info.service; |
| 5808 | StatusBarNotificationHolder sbnHolder |
| 5809 | = new StatusBarNotificationHolder(sbnToPost); |
| 5810 | try { |
| 5811 | assistant.onNotificationSnoozedUntilContext( |
| 5812 | sbnHolder, snoozeCriterionId); |
| 5813 | } catch (RemoteException ex) { |
| 5814 | Log.e(TAG, "unable to notify assistant (snoozed): " + assistant, ex); |
| 5815 | } |
| 5816 | } |
| 5817 | }); |
| 5818 | } |
| 5819 | } |
| 5820 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5821 | public boolean isEnabled() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5822 | return !getServices().isEmpty(); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5823 | } |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 5824 | |
| 5825 | protected void upgradeXml(final int xmlVersion, final int userId) { |
| 5826 | if (xmlVersion == 0) { |
| 5827 | // one time approval of the OOB assistant |
| 5828 | Slog.d(TAG, "Approving default notification assistant for user " + userId); |
| 5829 | readDefaultAssistant(userId); |
| 5830 | } |
| 5831 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5832 | } |
| 5833 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5834 | public class NotificationListeners extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5835 | static final String TAG_ENABLED_NOTIFICATION_LISTENERS = "enabled_listeners"; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5836 | |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5837 | private final ArraySet<ManagedServiceInfo> mLightTrimListeners = new ArraySet<>(); |
| 5838 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5839 | public NotificationListeners(IPackageManager pm) { |
| 5840 | super(getContext(), mNotificationLock, mUserProfiles, pm); |
| 5841 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5842 | } |
| 5843 | |
| 5844 | @Override |
| 5845 | protected Config getConfig() { |
| 5846 | Config c = new Config(); |
| 5847 | c.caption = "notification listener"; |
| 5848 | c.serviceInterface = NotificationListenerService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5849 | c.xmlTag = TAG_ENABLED_NOTIFICATION_LISTENERS; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5850 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS; |
| 5851 | c.bindPermission = android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE; |
| 5852 | c.settingsAction = Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS; |
| 5853 | c.clientLabel = R.string.notification_listener_binding_label; |
| 5854 | return c; |
| 5855 | } |
| 5856 | |
| 5857 | @Override |
| 5858 | protected IInterface asInterface(IBinder binder) { |
| 5859 | return INotificationListener.Stub.asInterface(binder); |
| 5860 | } |
| 5861 | |
| 5862 | @Override |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5863 | protected boolean checkType(IInterface service) { |
| 5864 | return service instanceof INotificationListener; |
| 5865 | } |
| 5866 | |
| 5867 | @Override |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 5868 | public void onServiceAdded(ManagedServiceInfo info) { |
| 5869 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5870 | final NotificationRankingUpdate update; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5871 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5872 | update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5873 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5874 | try { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5875 | listener.onListenerConnected(update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5876 | } catch (RemoteException e) { |
| 5877 | // we tried |
| 5878 | } |
| 5879 | } |
| 5880 | |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5881 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5882 | @GuardedBy("mNotificationLock") |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5883 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 5884 | if (removeDisabledHints(removed)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5885 | updateListenerHintsLocked(); |
Christoph Studer | 0d6ef4b | 2014-12-02 15:00:48 +0100 | [diff] [blame] | 5886 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5887 | } |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5888 | mLightTrimListeners.remove(removed); |
| 5889 | } |
| 5890 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5891 | @GuardedBy("mNotificationLock") |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5892 | public void setOnNotificationPostedTrimLocked(ManagedServiceInfo info, int trim) { |
| 5893 | if (trim == TRIM_LIGHT) { |
| 5894 | mLightTrimListeners.add(info); |
| 5895 | } else { |
| 5896 | mLightTrimListeners.remove(info); |
| 5897 | } |
| 5898 | } |
| 5899 | |
| 5900 | public int getOnNotificationPostedTrim(ManagedServiceInfo info) { |
| 5901 | return mLightTrimListeners.contains(info) ? TRIM_LIGHT : TRIM_FULL; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5902 | } |
| 5903 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5904 | /** |
| 5905 | * asynchronously notify all listeners about a new notification |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5906 | * |
| 5907 | * <p> |
| 5908 | * Also takes care of removing a notification that has been visible to a listener before, |
| 5909 | * but isn't anymore. |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5910 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5911 | @GuardedBy("mNotificationLock") |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 5912 | public void notifyPostedLocked(NotificationRecord r, StatusBarNotification oldSbn) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5913 | // Lazily initialized snapshots of the notification. |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 5914 | StatusBarNotification sbn = r.sbn; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5915 | TrimCache trimCache = new TrimCache(sbn); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5916 | |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 5917 | Set<Uri> uris = r.getNotificationUris(); |
| 5918 | |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5919 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5920 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 5921 | boolean oldSbnVisible = oldSbn != null ? isVisibleToListener(oldSbn, info) : false; |
| 5922 | // This notification hasn't been and still isn't visible -> ignore. |
| 5923 | if (!oldSbnVisible && !sbnVisible) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5924 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5925 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5926 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5927 | |
| 5928 | // This notification became invisible -> remove the old one. |
| 5929 | if (oldSbnVisible && !sbnVisible) { |
| 5930 | final StatusBarNotification oldSbnLightClone = oldSbn.cloneLight(); |
| 5931 | mHandler.post(new Runnable() { |
| 5932 | @Override |
| 5933 | public void run() { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5934 | notifyRemoved( |
| 5935 | info, oldSbnLightClone, update, null, REASON_USER_STOPPED); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5936 | } |
| 5937 | }); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5938 | continue; |
| 5939 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5940 | |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 5941 | grantUriPermissions(uris, sbn.getUserId(), info.component.getPackageName(), |
| 5942 | info.userid); |
| 5943 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5944 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5945 | mHandler.post(new Runnable() { |
| 5946 | @Override |
| 5947 | public void run() { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5948 | notifyPosted(info, sbnToPost, update); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5949 | } |
| 5950 | }); |
Kenny Guy | 3a7c4a56 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5951 | } |
| 5952 | } |
Kenny Guy | 3a7c4a56 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5953 | |
Julia Reynolds | e0d711f | 2017-09-01 08:50:47 -0400 | [diff] [blame] | 5954 | private void grantUriPermissions(Set<Uri> uris, int notiUserId, String listenerPkg, |
| 5955 | int listenerUserId) { |
| 5956 | long ident = Binder.clearCallingIdentity(); |
| 5957 | try { |
| 5958 | for (Uri uri : uris) { |
| 5959 | if (uri != null) { |
| 5960 | int sourceUserId = notiUserId == USER_ALL ? USER_SYSTEM |
| 5961 | : ContentProvider.getUserIdFromUri(uri, notiUserId); |
| 5962 | uri = ContentProvider.getUriWithoutUserId(uri); |
| 5963 | mAm.grantUriPermissionFromOwner(mPermissionOwner, Process.myUid(), |
| 5964 | listenerPkg, |
| 5965 | uri, Intent.FLAG_GRANT_READ_URI_PERMISSION, sourceUserId, |
| 5966 | listenerUserId == USER_ALL ? USER_SYSTEM : listenerUserId); |
| 5967 | } |
| 5968 | } |
| 5969 | } catch (RemoteException e) { |
| 5970 | Log.e(TAG, "Count not grant uri permission to " + listenerPkg, e); |
| 5971 | } finally { |
| 5972 | Binder.restoreCallingIdentity(ident); |
| 5973 | } |
| 5974 | } |
| 5975 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5976 | /** |
| 5977 | * asynchronously notify all listeners about a removed notification |
| 5978 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5979 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5980 | public void notifyRemovedLocked(StatusBarNotification sbn, int reason, |
| 5981 | NotificationStats notificationStats) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5982 | // make a copy in case changes are made to the underlying Notification object |
| 5983 | // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the |
| 5984 | // notification |
| 5985 | final StatusBarNotification sbnLight = sbn.cloneLight(); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5986 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5987 | if (!isVisibleToListener(sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5988 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5989 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5990 | // Only assistants can get stats |
| 5991 | final NotificationStats stats = mAssistants.isServiceTokenValidLocked(info.service) |
| 5992 | ? notificationStats : null; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5993 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5994 | mHandler.post(new Runnable() { |
| 5995 | @Override |
| 5996 | public void run() { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5997 | notifyRemoved(info, sbnLight, update, stats, reason); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5998 | } |
| 5999 | }); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6000 | } |
| 6001 | } |
| 6002 | |
| 6003 | /** |
| 6004 | * asynchronously notify all listeners about a reordering of notifications |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6005 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6006 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6007 | public void notifyRankingUpdateLocked() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6008 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6009 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 6010 | continue; |
| 6011 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6012 | final NotificationRankingUpdate update = makeRankingUpdateLocked(serviceInfo); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6013 | mHandler.post(new Runnable() { |
| 6014 | @Override |
| 6015 | public void run() { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 6016 | notifyRankingUpdate(serviceInfo, update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6017 | } |
| 6018 | }); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6019 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6020 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6021 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 6022 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6023 | public void notifyListenerHintsChangedLocked(final int hints) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6024 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6025 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 6026 | continue; |
| 6027 | } |
| 6028 | mHandler.post(new Runnable() { |
| 6029 | @Override |
| 6030 | public void run() { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6031 | notifyListenerHintsChanged(serviceInfo, hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6032 | } |
| 6033 | }); |
| 6034 | } |
| 6035 | } |
| 6036 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 6037 | public void notifyInterruptionFilterChanged(final int interruptionFilter) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6038 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 6039 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 6040 | continue; |
| 6041 | } |
| 6042 | mHandler.post(new Runnable() { |
| 6043 | @Override |
| 6044 | public void run() { |
| 6045 | notifyInterruptionFilterChanged(serviceInfo, interruptionFilter); |
| 6046 | } |
| 6047 | }); |
| 6048 | } |
| 6049 | } |
| 6050 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6051 | protected void notifyNotificationChannelChanged(final String pkg, final UserHandle user, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6052 | final NotificationChannel channel, final int modificationType) { |
| 6053 | if (channel == null) { |
| 6054 | return; |
| 6055 | } |
| 6056 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6057 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6058 | continue; |
| 6059 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 6060 | |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 6061 | BackgroundThread.getHandler().post(() -> { |
| 6062 | if (hasCompanionDevice(serviceInfo)) { |
| 6063 | notifyNotificationChannelChanged( |
| 6064 | serviceInfo, pkg, user, channel, modificationType); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6065 | } |
| 6066 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6067 | } |
| 6068 | } |
| 6069 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6070 | protected void notifyNotificationChannelGroupChanged( |
| 6071 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
| 6072 | final int modificationType) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6073 | if (group == null) { |
| 6074 | return; |
| 6075 | } |
| 6076 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6077 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6078 | continue; |
| 6079 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 6080 | |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 6081 | BackgroundThread.getHandler().post(() -> { |
| 6082 | if (hasCompanionDevice(serviceInfo)) { |
| 6083 | notifyNotificationChannelGroupChanged( |
| 6084 | serviceInfo, pkg, user, group, modificationType); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 6085 | } |
| 6086 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6087 | } |
| 6088 | } |
| 6089 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6090 | private void notifyPosted(final ManagedServiceInfo info, |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6091 | final StatusBarNotification sbn, NotificationRankingUpdate rankingUpdate) { |
Julia Reynolds | a75c752 | 2017-03-21 17:34:25 -0400 | [diff] [blame] | 6092 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6093 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6094 | try { |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6095 | listener.onNotificationPosted(sbnHolder, rankingUpdate); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6096 | } catch (RemoteException ex) { |
| 6097 | Log.e(TAG, "unable to notify listener (posted): " + listener, ex); |
| 6098 | } |
| 6099 | } |
| 6100 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 6101 | private void notifyRemoved(ManagedServiceInfo info, StatusBarNotification sbn, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6102 | NotificationRankingUpdate rankingUpdate, NotificationStats stats, int reason) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6103 | if (!info.enabledAndUserMatches(sbn.getUserId())) { |
| 6104 | return; |
| 6105 | } |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6106 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6107 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6108 | try { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 6109 | listener.onNotificationRemoved(sbnHolder, rankingUpdate, stats, reason); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 6110 | } catch (RemoteException ex) { |
| 6111 | Log.e(TAG, "unable to notify listener (removed): " + listener, ex); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 6112 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6113 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6114 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6115 | private void notifyRankingUpdate(ManagedServiceInfo info, |
| 6116 | NotificationRankingUpdate rankingUpdate) { |
| 6117 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6118 | try { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 6119 | listener.onNotificationRankingUpdate(rankingUpdate); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 6120 | } catch (RemoteException ex) { |
| 6121 | Log.e(TAG, "unable to notify listener (ranking update): " + listener, ex); |
| 6122 | } |
| 6123 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6124 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6125 | private void notifyListenerHintsChanged(ManagedServiceInfo info, int hints) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6126 | final INotificationListener listener = (INotificationListener) info.service; |
| 6127 | try { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6128 | listener.onListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6129 | } catch (RemoteException ex) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 6130 | Log.e(TAG, "unable to notify listener (listener hints): " + listener, ex); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 6131 | } |
| 6132 | } |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 6133 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 6134 | private void notifyInterruptionFilterChanged(ManagedServiceInfo info, |
| 6135 | int interruptionFilter) { |
| 6136 | final INotificationListener listener = (INotificationListener) info.service; |
| 6137 | try { |
| 6138 | listener.onInterruptionFilterChanged(interruptionFilter); |
| 6139 | } catch (RemoteException ex) { |
| 6140 | Log.e(TAG, "unable to notify listener (interruption filter): " + listener, ex); |
| 6141 | } |
| 6142 | } |
| 6143 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6144 | void notifyNotificationChannelChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6145 | final String pkg, final UserHandle user, final NotificationChannel channel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6146 | final int modificationType) { |
| 6147 | final INotificationListener listener = (INotificationListener) info.service; |
| 6148 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6149 | listener.onNotificationChannelModification(pkg, user, channel, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6150 | } catch (RemoteException ex) { |
| 6151 | Log.e(TAG, "unable to notify listener (channel changed): " + listener, ex); |
| 6152 | } |
| 6153 | } |
| 6154 | |
| 6155 | private void notifyNotificationChannelGroupChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6156 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6157 | final int modificationType) { |
| 6158 | final INotificationListener listener = (INotificationListener) info.service; |
| 6159 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 6160 | listener.onNotificationChannelGroupModification(pkg, user, group, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 6161 | } catch (RemoteException ex) { |
| 6162 | Log.e(TAG, "unable to notify listener (channel group changed): " + listener, ex); |
| 6163 | } |
| 6164 | } |
| 6165 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6166 | public boolean isListenerPackage(String packageName) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 6167 | if (packageName == null) { |
| 6168 | return false; |
| 6169 | } |
| 6170 | // TODO: clean up locking object later |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 6171 | synchronized (mNotificationLock) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 6172 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 6173 | if (packageName.equals(serviceInfo.component.getPackageName())) { |
| 6174 | return true; |
| 6175 | } |
| 6176 | } |
| 6177 | } |
| 6178 | return false; |
| 6179 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 6180 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6181 | |
| 6182 | public static final class DumpFilter { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6183 | public boolean filtered = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6184 | public String pkgFilter; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 6185 | public boolean zen; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 6186 | public long since; |
| 6187 | public boolean stats; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6188 | public boolean redact = true; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 6189 | public boolean proto = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6190 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 6191 | @NonNull |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6192 | public static DumpFilter parseFromArguments(String[] args) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6193 | final DumpFilter filter = new DumpFilter(); |
| 6194 | for (int ai = 0; ai < args.length; ai++) { |
| 6195 | final String a = args[ai]; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 6196 | if ("--proto".equals(a)) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 6197 | filter.proto = true; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 6198 | } else if ("--noredact".equals(a) || "--reveal".equals(a)) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6199 | filter.redact = false; |
| 6200 | } else if ("p".equals(a) || "pkg".equals(a) || "--package".equals(a)) { |
| 6201 | if (ai < args.length-1) { |
| 6202 | ai++; |
| 6203 | filter.pkgFilter = args[ai].trim().toLowerCase(); |
| 6204 | if (filter.pkgFilter.isEmpty()) { |
| 6205 | filter.pkgFilter = null; |
| 6206 | } else { |
| 6207 | filter.filtered = true; |
| 6208 | } |
| 6209 | } |
| 6210 | } else if ("--zen".equals(a) || "zen".equals(a)) { |
| 6211 | filter.filtered = true; |
| 6212 | filter.zen = true; |
| 6213 | } else if ("--stats".equals(a)) { |
| 6214 | filter.stats = true; |
| 6215 | if (ai < args.length-1) { |
| 6216 | ai++; |
Tobias Thierer | 28532d0 | 2016-04-21 14:52:10 +0100 | [diff] [blame] | 6217 | filter.since = Long.parseLong(args[ai]); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6218 | } else { |
| 6219 | filter.since = 0; |
| 6220 | } |
| 6221 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6222 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6223 | return filter; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6224 | } |
| 6225 | |
| 6226 | public boolean matches(StatusBarNotification sbn) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6227 | if (!filtered) return true; |
| 6228 | return zen ? true : sbn != null |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 6229 | && (matches(sbn.getPackageName()) || matches(sbn.getOpPkg())); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6230 | } |
| 6231 | |
| 6232 | public boolean matches(ComponentName component) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6233 | if (!filtered) return true; |
| 6234 | return zen ? true : component != null && matches(component.getPackageName()); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6235 | } |
| 6236 | |
| 6237 | public boolean matches(String pkg) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6238 | if (!filtered) return true; |
| 6239 | return zen ? true : pkg != null && pkg.toLowerCase().contains(pkgFilter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 6240 | } |
| 6241 | |
| 6242 | @Override |
| 6243 | public String toString() { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 6244 | return stats ? "stats" : zen ? "zen" : ('\'' + pkgFilter + '\''); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6245 | } |
| 6246 | } |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6247 | |
| 6248 | /** |
| 6249 | * Wrapper for a StatusBarNotification object that allows transfer across a oneway |
| 6250 | * binder without sending large amounts of data over a oneway transaction. |
| 6251 | */ |
| 6252 | private static final class StatusBarNotificationHolder |
| 6253 | extends IStatusBarNotificationHolder.Stub { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 6254 | private StatusBarNotification mValue; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6255 | |
| 6256 | public StatusBarNotificationHolder(StatusBarNotification value) { |
| 6257 | mValue = value; |
| 6258 | } |
| 6259 | |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 6260 | /** 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] | 6261 | @Override |
| 6262 | public StatusBarNotification get() { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 6263 | StatusBarNotification value = mValue; |
| 6264 | mValue = null; |
| 6265 | return value; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6266 | } |
| 6267 | } |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6268 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6269 | private class ShellCmd extends ShellCommand { |
| 6270 | public static final String USAGE = "help\n" |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 6271 | + "allow_listener COMPONENT [user_id]\n" |
| 6272 | + "disallow_listener COMPONENT [user_id]\n" |
Julia Reynolds | 7380d87 | 2018-01-12 10:28:26 -0500 | [diff] [blame] | 6273 | + "allow_assistant COMPONENT\n" |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6274 | + "remove_assistant COMPONENT\n" |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6275 | + "allow_dnd PACKAGE\n" |
| 6276 | + "disallow_dnd PACKAGE"; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6277 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6278 | @Override |
| 6279 | public int onCommand(String cmd) { |
Felipe Leme | 68d8041 | 2017-07-14 11:18:08 -0700 | [diff] [blame] | 6280 | if (cmd == null) { |
| 6281 | return handleDefaultCommands(cmd); |
| 6282 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 6283 | final PrintWriter pw = getOutPrintWriter(); |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 6284 | try { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6285 | switch (cmd) { |
| 6286 | case "allow_dnd": { |
| 6287 | getBinderService().setNotificationPolicyAccessGranted( |
| 6288 | getNextArgRequired(), true); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6289 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6290 | break; |
| 6291 | |
| 6292 | case "disallow_dnd": { |
| 6293 | getBinderService().setNotificationPolicyAccessGranted( |
| 6294 | getNextArgRequired(), false); |
| 6295 | } |
| 6296 | break; |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 6297 | case "allow_listener": { |
| 6298 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 6299 | if (cn == null) { |
| 6300 | pw.println("Invalid listener - must be a ComponentName"); |
| 6301 | return -1; |
| 6302 | } |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 6303 | String userId = getNextArg(); |
| 6304 | if (userId == null) { |
| 6305 | getBinderService().setNotificationListenerAccessGranted(cn, true); |
| 6306 | } else { |
| 6307 | getBinderService().setNotificationListenerAccessGrantedForUser( |
| 6308 | cn, Integer.parseInt(userId), true); |
| 6309 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 6310 | } |
| 6311 | break; |
| 6312 | case "disallow_listener": { |
| 6313 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 6314 | if (cn == null) { |
| 6315 | pw.println("Invalid listener - must be a ComponentName"); |
| 6316 | return -1; |
| 6317 | } |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 6318 | String userId = getNextArg(); |
| 6319 | if (userId == null) { |
| 6320 | getBinderService().setNotificationListenerAccessGranted(cn, false); |
| 6321 | } else { |
| 6322 | getBinderService().setNotificationListenerAccessGrantedForUser( |
| 6323 | cn, Integer.parseInt(userId), false); |
| 6324 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 6325 | } |
| 6326 | break; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6327 | case "allow_assistant": { |
| 6328 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 6329 | if (cn == null) { |
| 6330 | pw.println("Invalid assistant - must be a ComponentName"); |
| 6331 | return -1; |
| 6332 | } |
| 6333 | getBinderService().setNotificationAssistantAccessGranted(cn, true); |
| 6334 | } |
| 6335 | break; |
| 6336 | case "disallow_assistant": { |
| 6337 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 6338 | if (cn == null) { |
| 6339 | pw.println("Invalid assistant - must be a ComponentName"); |
| 6340 | return -1; |
| 6341 | } |
| 6342 | getBinderService().setNotificationAssistantAccessGranted(cn, false); |
| 6343 | } |
| 6344 | break; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6345 | |
| 6346 | default: |
| 6347 | return handleDefaultCommands(cmd); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6348 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 6349 | } catch (Exception e) { |
| 6350 | pw.println("Error occurred. Check logcat for details. " + e.getMessage()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6351 | Slog.e(TAG, "Error running shell command", e); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6352 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6353 | return 0; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6354 | } |
| 6355 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6356 | @Override |
| 6357 | public void onHelp() { |
| 6358 | getOutPrintWriter().println(USAGE); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6359 | } |
| 6360 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6361 | } |