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 | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 19 | import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED; |
| 20 | import static android.app.NotificationManager.ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED; |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 21 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 22 | import static android.app.NotificationManager.IMPORTANCE_MIN; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 23 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 24 | import static android.content.pm.PackageManager.FEATURE_LEANBACK; |
| 25 | import static android.content.pm.PackageManager.FEATURE_TELEVISION; |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 26 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 27 | import static android.os.UserHandle.USER_NULL; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 28 | import static android.service.notification.NotificationListenerService |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 29 | .HINT_HOST_DISABLE_CALL_EFFECTS; |
| 30 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_EFFECTS; |
| 31 | import static android.service.notification.NotificationListenerService |
| 32 | .HINT_HOST_DISABLE_NOTIFICATION_EFFECTS; |
| 33 | import static android.service.notification.NotificationListenerService |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 34 | .NOTIFICATION_CHANNEL_OR_GROUP_ADDED; |
| 35 | import static android.service.notification.NotificationListenerService |
| 36 | .NOTIFICATION_CHANNEL_OR_GROUP_DELETED; |
| 37 | import static android.service.notification.NotificationListenerService |
| 38 | .NOTIFICATION_CHANNEL_OR_GROUP_UPDATED; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 39 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL; |
| 40 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL; |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 41 | import static android.service.notification.NotificationListenerService.REASON_CANCEL; |
| 42 | import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 43 | import static android.service.notification.NotificationListenerService.REASON_CHANNEL_BANNED; |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 44 | import static android.service.notification.NotificationListenerService.REASON_CLICK; |
| 45 | import static android.service.notification.NotificationListenerService.REASON_ERROR; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 46 | import static android.service.notification.NotificationListenerService |
| 47 | .REASON_GROUP_SUMMARY_CANCELED; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 48 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL; |
| 49 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL_ALL; |
| 50 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_BANNED; |
| 51 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_CHANGED; |
| 52 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_SUSPENDED; |
| 53 | import static android.service.notification.NotificationListenerService.REASON_PROFILE_TURNED_OFF; |
| 54 | import static android.service.notification.NotificationListenerService.REASON_SNOOZED; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 55 | import static android.service.notification.NotificationListenerService.REASON_TIMEOUT; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 56 | import static android.service.notification.NotificationListenerService.REASON_UNAUTOBUNDLED; |
| 57 | import static android.service.notification.NotificationListenerService.REASON_USER_STOPPED; |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 58 | import static android.service.notification.NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF; |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 59 | import static android.service.notification.NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_ON; |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 60 | import static android.service.notification.NotificationListenerService.TRIM_FULL; |
| 61 | import static android.service.notification.NotificationListenerService.TRIM_LIGHT; |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 62 | import static android.view.Display.DEFAULT_DISPLAY; |
| 63 | import static android.view.WindowManager.LayoutParams.TYPE_TOAST; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 64 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 65 | import android.Manifest; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 66 | import android.annotation.NonNull; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 67 | import android.annotation.Nullable; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 68 | import android.app.ActivityManager; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 69 | import android.app.ActivityManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 70 | import android.app.AlarmManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 71 | import android.app.AppGlobals; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 72 | import android.app.AppOpsManager; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 73 | import android.app.AutomaticZenRule; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | import android.app.IActivityManager; |
| 75 | import android.app.INotificationManager; |
| 76 | import android.app.ITransientNotification; |
| 77 | import android.app.Notification; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 78 | import android.app.NotificationChannel; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 79 | import android.app.NotificationChannelGroup; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 80 | import android.app.NotificationManager; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 81 | import android.app.NotificationManager.Policy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | import android.app.PendingIntent; |
| 83 | import android.app.StatusBarManager; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 84 | import android.app.backup.BackupManager; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 85 | import android.app.usage.UsageEvents; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 86 | import android.app.usage.UsageStatsManagerInternal; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 87 | import android.companion.ICompanionDeviceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | import android.content.BroadcastReceiver; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 89 | import android.content.ComponentName; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 90 | import android.content.ContentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | import android.content.Context; |
| 92 | import android.content.Intent; |
| 93 | import android.content.IntentFilter; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 94 | import android.content.pm.ApplicationInfo; |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 95 | import android.content.pm.IPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | import android.content.pm.PackageManager; |
| 97 | import android.content.pm.PackageManager.NameNotFoundException; |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 98 | import android.content.pm.ParceledListSlice; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | import android.content.res.Resources; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 100 | import android.database.ContentObserver; |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 101 | import android.media.AudioAttributes; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 102 | import android.media.AudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 103 | import android.media.AudioManagerInternal; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 104 | import android.media.IRingtonePlayer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | import android.os.Binder; |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 107 | import android.os.Build; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 108 | import android.os.Bundle; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 109 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import android.os.Handler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 111 | import android.os.HandlerThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | import android.os.IBinder; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 113 | import android.os.IInterface; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 114 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | import android.os.Message; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 116 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 117 | import android.os.RemoteException; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 118 | import android.os.ResultReceiver; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 119 | import android.os.ServiceManager; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 120 | import android.os.ShellCallback; |
| 121 | import android.os.ShellCommand; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 122 | import android.os.SystemClock; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 123 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 124 | import android.os.UserHandle; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 125 | import android.os.VibrationEffect; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 126 | import android.os.Vibrator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | import android.provider.Settings; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 128 | import android.service.notification.Adjustment; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 129 | import android.service.notification.Condition; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 130 | import android.service.notification.IConditionProvider; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 131 | import android.service.notification.INotificationListener; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 132 | import android.service.notification.IStatusBarNotificationHolder; |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 133 | import android.service.notification.ListenersDisablingEffectsProto; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 134 | import android.service.notification.NotificationAssistantService; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 135 | import android.service.notification.NotificationListenerService; |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 136 | import android.service.notification.NotificationRankingUpdate; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 137 | import android.service.notification.NotificationRecordProto; |
| 138 | import android.service.notification.NotificationServiceDumpProto; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 139 | import android.service.notification.NotificationStats; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 140 | import android.service.notification.SnoozeCriterion; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 141 | import android.service.notification.StatusBarNotification; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 142 | import android.service.notification.ZenModeConfig; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 143 | import android.service.notification.ZenModeProto; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 144 | import android.telephony.PhoneStateListener; |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 145 | import android.telephony.TelephonyManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 146 | import android.text.TextUtils; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 147 | import android.util.ArrayMap; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 148 | import android.util.ArraySet; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 149 | import android.util.AtomicFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | import android.util.Log; |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 151 | import android.util.Slog; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 152 | import android.util.SparseArray; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 153 | import android.util.Xml; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 154 | import android.util.proto.ProtoOutputStream; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 155 | import android.view.accessibility.AccessibilityEvent; |
| 156 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | import android.widget.Toast; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 158 | |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 159 | import com.android.internal.R; |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 160 | import com.android.internal.annotations.GuardedBy; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 161 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 162 | import com.android.internal.logging.MetricsLogger; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 163 | import com.android.internal.logging.nano.MetricsProto; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 164 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 165 | import com.android.internal.os.BackgroundThread; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 166 | import com.android.internal.statusbar.NotificationVisibility; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 167 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 168 | import com.android.internal.util.DumpUtils; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 169 | import com.android.internal.util.FastXmlSerializer; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 170 | import com.android.internal.util.Preconditions; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 171 | import com.android.internal.util.XmlUtils; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 172 | import com.android.server.DeviceIdleController; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 173 | import com.android.server.EventLogTags; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 174 | import com.android.server.LocalServices; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 175 | import com.android.server.SystemService; |
| 176 | import com.android.server.lights.Light; |
| 177 | import com.android.server.lights.LightsManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 178 | import com.android.server.notification.ManagedServices.ManagedServiceInfo; |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 179 | import com.android.server.notification.ManagedServices.UserProfiles; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 180 | import com.android.server.policy.PhoneWindowManager; |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 181 | import com.android.server.statusbar.StatusBarManagerInternal; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 182 | import com.android.server.wm.WindowManagerInternal; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 183 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 184 | import libcore.io.IoUtils; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 185 | |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 186 | import org.json.JSONException; |
| 187 | import org.json.JSONObject; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 188 | import org.xmlpull.v1.XmlPullParser; |
| 189 | import org.xmlpull.v1.XmlPullParserException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 190 | import org.xmlpull.v1.XmlSerializer; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 191 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 192 | import java.io.ByteArrayInputStream; |
| 193 | import java.io.ByteArrayOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 194 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | import java.io.FileDescriptor; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 196 | import java.io.FileNotFoundException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 197 | import java.io.FileOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 198 | import java.io.IOException; |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 199 | import java.io.InputStream; |
| 200 | import java.io.OutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 202 | import java.nio.charset.StandardCharsets; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 203 | import java.util.ArrayDeque; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | import java.util.ArrayList; |
Michael Wright | bc4d0d9 | 2017-03-23 18:57:57 +0000 | [diff] [blame] | 205 | import java.util.Arrays; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 206 | import java.util.Iterator; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 207 | import java.util.List; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 208 | import java.util.Map.Entry; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 209 | import java.util.Objects; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 210 | import java.util.Set; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 211 | import java.util.concurrent.TimeUnit; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 212 | |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 213 | /** {@hide} */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 214 | public class NotificationManagerService extends SystemService { |
| 215 | static final String TAG = "NotificationService"; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 216 | static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Selim Cinek | 4041249 | 2015-12-08 18:03:22 -0800 | [diff] [blame] | 217 | public static final boolean ENABLE_CHILD_NOTIFICATIONS |
| 218 | = SystemProperties.getBoolean("debug.child_notifs", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 220 | static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 221 | static final float DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE = 5f; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 222 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | // message codes |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 224 | static final int MESSAGE_TIMEOUT = 2; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 225 | static final int MESSAGE_SAVE_POLICY_FILE = 3; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 226 | static final int MESSAGE_SEND_RANKING_UPDATE = 4; |
| 227 | static final int MESSAGE_LISTENER_HINTS_CHANGED = 5; |
| 228 | static final int MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED = 6; |
| 229 | |
| 230 | // ranking thread messages |
| 231 | private static final int MESSAGE_RECONSIDER_RANKING = 1000; |
| 232 | private static final int MESSAGE_RANKING_SORT = 1001; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 234 | static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 235 | static final int SHORT_DELAY = 2000; // 2 seconds |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 236 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 237 | static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 238 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 239 | static final long SNOOZE_UNTIL_UNSPECIFIED = -1; |
| 240 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 241 | 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] | 242 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 243 | static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION; |
Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 244 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 245 | static final boolean ENABLE_BLOCKED_TOASTS = true; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 246 | |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 247 | // When #matchesCallFilter is called from the ringer, wait at most |
| 248 | // 3s to resolve the contacts. This timeout is required since |
| 249 | // ContactsProvider might take a long time to start up. |
| 250 | // |
| 251 | // Return STARRED_CONTACT when the timeout is hit in order to avoid |
| 252 | // missed calls in ZEN mode "Important". |
| 253 | static final int MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS = 3000; |
| 254 | static final float MATCHES_CALL_FILTER_TIMEOUT_AFFINITY = |
| 255 | ValidateNotificationPeople.STARRED_CONTACT; |
| 256 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 257 | /** notification_enqueue status value for a newly enqueued notification. */ |
| 258 | private static final int EVENTLOG_ENQUEUE_STATUS_NEW = 0; |
| 259 | |
| 260 | /** notification_enqueue status value for an existing notification. */ |
| 261 | private static final int EVENTLOG_ENQUEUE_STATUS_UPDATE = 1; |
| 262 | |
| 263 | /** notification_enqueue status value for an ignored notification. */ |
| 264 | private static final int EVENTLOG_ENQUEUE_STATUS_IGNORED = 2; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 265 | private static final long MIN_PACKAGE_OVERRATE_LOG_INTERVAL = 5000; // milliseconds |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 266 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 267 | private static final long DELAY_FOR_ASSISTANT_TIME = 100; |
| 268 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 269 | private static final String ACTION_NOTIFICATION_TIMEOUT = |
| 270 | NotificationManagerService.class.getSimpleName() + ".TIMEOUT"; |
| 271 | private static final int REQUEST_CODE_TIMEOUT = 1; |
| 272 | private static final String SCHEME_TIMEOUT = "timeout"; |
| 273 | private static final String EXTRA_KEY = "key"; |
| 274 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 275 | private IActivityManager mAm; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 276 | private ActivityManager mActivityManager; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 277 | private IPackageManager mPackageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 278 | private PackageManager mPackageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 279 | AudioManager mAudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 280 | AudioManagerInternal mAudioManagerInternal; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 281 | @Nullable StatusBarManagerInternal mStatusBar; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 282 | Vibrator mVibrator; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 283 | private WindowManagerInternal mWindowManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 284 | private AlarmManager mAlarmManager; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 285 | private ICompanionDeviceManager mCompanionManager; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 286 | private AccessibilityManager mAccessibilityManager; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 287 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | final IBinder mForegroundToken = new Binder(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 289 | private WorkerHandler mHandler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 290 | private final HandlerThread mRankingThread = new HandlerThread("ranker", |
| 291 | Process.THREAD_PRIORITY_BACKGROUND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 293 | private Light mNotificationLight; |
| 294 | Light mAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 295 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 296 | private long[] mFallbackVibrationPattern; |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 297 | private boolean mUseAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 298 | boolean mSystemReady; |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 299 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 300 | private boolean mDisableNotificationEffects; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 301 | private int mCallState; |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 302 | private String mSoundNotificationKey; |
| 303 | private String mVibrateNotificationKey; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 305 | private final SparseArray<ArraySet<ManagedServiceInfo>> mListenersDisablingEffects = |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 306 | new SparseArray<>(); |
| 307 | private List<ComponentName> mEffectsSuppressors = new ArrayList<>(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 308 | private int mListenerHints; // right now, all hints are global |
John Spurlock | 8310410 | 2015-02-12 23:25:12 -0500 | [diff] [blame] | 309 | private int mInterruptionFilter = NotificationListenerService.INTERRUPTION_FILTER_UNKNOWN; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 310 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 311 | // for enabling and disabling notification pulse behavior |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 312 | private boolean mScreenOn = true; |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 313 | protected boolean mInCall = false; |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 314 | private boolean mNotificationPulseEnabled; |
| 315 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 316 | private Uri mInCallNotificationUri; |
| 317 | private AudioAttributes mInCallNotificationAudioAttributes; |
| 318 | private float mInCallNotificationVolume; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 319 | |
Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 320 | // used as a mutex for access to all active notifications & listeners |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 321 | final Object mNotificationLock = new Object(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 322 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 323 | final ArrayList<NotificationRecord> mNotificationList = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 324 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 325 | final ArrayMap<String, NotificationRecord> mNotificationsByKey = new ArrayMap<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 326 | @GuardedBy("mNotificationLock") |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 327 | final ArrayList<NotificationRecord> mEnqueuedNotifications = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 328 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 329 | final ArrayMap<Integer, ArrayMap<String, String>> mAutobundledSummaries = new ArrayMap<>(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 330 | final ArrayList<ToastRecord> mToastQueue = new ArrayList<>(); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 331 | final ArrayMap<String, NotificationRecord> mSummaryByGroupKey = new ArrayMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 333 | // The last key in this list owns the hardware. |
| 334 | ArrayList<String> mLights = new ArrayList<>(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 335 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 336 | private AppOpsManager mAppOps; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 337 | private UsageStatsManagerInternal mAppUsageStats; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 338 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 339 | private Archive mArchive; |
| 340 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 341 | // Persistent storage for notification policy |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 342 | private AtomicFile mPolicyFile; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 343 | |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 344 | private static final int DB_VERSION = 1; |
| 345 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 346 | private static final String TAG_NOTIFICATION_POLICY = "notification-policy"; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 347 | private static final String ATTR_VERSION = "version"; |
| 348 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 349 | private RankingHelper mRankingHelper; |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 350 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 351 | private final UserProfiles mUserProfiles = new UserProfiles(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 352 | private NotificationListeners mListeners; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 353 | private NotificationAssistants mAssistants; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 354 | private ConditionProviders mConditionProviders; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 355 | private NotificationUsageStats mUsageStats; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 356 | |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 357 | private static final int MY_UID = Process.myUid(); |
| 358 | private static final int MY_PID = Process.myPid(); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 359 | private static final IBinder WHITELIST_TOKEN = new Binder(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 360 | private RankingHandler mRankingHandler; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 361 | private long mLastOverRateLogTime; |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 362 | private float mMaxPackageEnqueueRate = DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE; |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 363 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 364 | private SnoozeHelper mSnoozeHelper; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 365 | private GroupHelper mGroupHelper; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 366 | private boolean mIsTelevision; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 367 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 368 | private static class Archive { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 369 | final int mBufferSize; |
| 370 | final ArrayDeque<StatusBarNotification> mBuffer; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 371 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 372 | public Archive(int size) { |
| 373 | mBufferSize = size; |
| 374 | mBuffer = new ArrayDeque<StatusBarNotification>(mBufferSize); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 375 | } |
Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 376 | |
Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 377 | public String toString() { |
| 378 | final StringBuilder sb = new StringBuilder(); |
| 379 | final int N = mBuffer.size(); |
| 380 | sb.append("Archive ("); |
| 381 | sb.append(N); |
| 382 | sb.append(" notification"); |
| 383 | sb.append((N==1)?")":"s)"); |
| 384 | return sb.toString(); |
| 385 | } |
| 386 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 387 | public void record(StatusBarNotification nr) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 388 | if (mBuffer.size() == mBufferSize) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 389 | mBuffer.removeFirst(); |
| 390 | } |
Daniel Sandler | 26b81d5 | 2013-05-20 20:56:43 -0400 | [diff] [blame] | 391 | |
| 392 | // We don't want to store the heavy bits of the notification in the archive, |
| 393 | // but other clients in the system process might be using the object, so we |
| 394 | // store a (lightened) copy. |
| 395 | mBuffer.addLast(nr.cloneLight()); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 396 | } |
| 397 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 398 | public Iterator<StatusBarNotification> descendingIterator() { |
| 399 | return mBuffer.descendingIterator(); |
| 400 | } |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 401 | |
| 402 | public StatusBarNotification[] getArray(int count) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 403 | if (count == 0) count = mBufferSize; |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 404 | final StatusBarNotification[] a |
| 405 | = new StatusBarNotification[Math.min(count, mBuffer.size())]; |
| 406 | Iterator<StatusBarNotification> iter = descendingIterator(); |
| 407 | int i=0; |
| 408 | while (iter.hasNext() && i < count) { |
| 409 | a[i++] = iter.next(); |
| 410 | } |
| 411 | return a; |
| 412 | } |
| 413 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 414 | } |
| 415 | |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 416 | protected void readDefaultApprovedServices(int userId) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 417 | String defaultListenerAccess = getContext().getResources().getString( |
| 418 | com.android.internal.R.string.config_defaultListenerAccessPackages); |
| 419 | if (defaultListenerAccess != null) { |
| 420 | for (String whitelisted : |
| 421 | defaultListenerAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR)) { |
| 422 | // Gather all notification listener components for candidate pkgs. |
| 423 | Set<ComponentName> approvedListeners = |
| 424 | mListeners.queryPackageForServices(whitelisted, |
| 425 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 426 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId); |
| 427 | for (ComponentName cn : approvedListeners) { |
| 428 | try { |
| 429 | getBinderService().setNotificationListenerAccessGrantedForUser(cn, |
| 430 | userId, true); |
| 431 | } catch (RemoteException e) { |
| 432 | e.printStackTrace(); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | String defaultDndAccess = getContext().getResources().getString( |
| 438 | com.android.internal.R.string.config_defaultDndAccessPackages); |
| 439 | if (defaultListenerAccess != null) { |
| 440 | for (String whitelisted : |
| 441 | defaultDndAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR)) { |
| 442 | try { |
| 443 | getBinderService().setNotificationPolicyAccessGranted(whitelisted, true); |
| 444 | } catch (RemoteException e) { |
| 445 | e.printStackTrace(); |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 451 | void readPolicyXml(InputStream stream, boolean forRestore) |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 452 | throws XmlPullParserException, NumberFormatException, IOException { |
| 453 | final XmlPullParser parser = Xml.newPullParser(); |
| 454 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 455 | XmlUtils.beginDocument(parser, TAG_NOTIFICATION_POLICY); |
| 456 | boolean migratedManagedServices = false; |
| 457 | int outerDepth = parser.getDepth(); |
| 458 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 459 | if (ZenModeConfig.ZEN_TAG.equals(parser.getName())) { |
| 460 | mZenModeHelper.readXml(parser, forRestore); |
| 461 | } else if (RankingHelper.TAG_RANKING.equals(parser.getName())){ |
| 462 | mRankingHelper.readXml(parser, forRestore); |
| 463 | } |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 464 | // No non-system managed services are allowed on low ram devices |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 465 | if (canUseManagedServices()) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 466 | if (mListeners.getConfig().xmlTag.equals(parser.getName())) { |
| 467 | mListeners.readXml(parser); |
| 468 | migratedManagedServices = true; |
| 469 | } else if (mAssistants.getConfig().xmlTag.equals(parser.getName())) { |
| 470 | mAssistants.readXml(parser); |
| 471 | migratedManagedServices = true; |
| 472 | } else if (mConditionProviders.getConfig().xmlTag.equals(parser.getName())) { |
| 473 | mConditionProviders.readXml(parser); |
| 474 | migratedManagedServices = true; |
| 475 | } |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 476 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 477 | } |
| 478 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 479 | if (!migratedManagedServices) { |
| 480 | mListeners.migrateToXml(); |
| 481 | mAssistants.migrateToXml(); |
| 482 | mConditionProviders.migrateToXml(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 483 | savePolicyFile(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 487 | private void loadPolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 488 | if (DBG) Slog.d(TAG, "loadPolicyFile"); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 489 | synchronized (mPolicyFile) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 490 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 491 | InputStream infile = null; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 492 | try { |
| 493 | infile = mPolicyFile.openRead(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 494 | readPolicyXml(infile, false /*forRestore*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 495 | } catch (FileNotFoundException e) { |
| 496 | // No data yet |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 497 | // Load default managed services approvals |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 498 | readDefaultApprovedServices(UserHandle.USER_SYSTEM); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 499 | } catch (IOException e) { |
| 500 | Log.wtf(TAG, "Unable to read notification policy", e); |
| 501 | } catch (NumberFormatException e) { |
| 502 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 503 | } catch (XmlPullParserException e) { |
| 504 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 505 | } finally { |
| 506 | IoUtils.closeQuietly(infile); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | public void savePolicyFile() { |
| 512 | mHandler.removeMessages(MESSAGE_SAVE_POLICY_FILE); |
| 513 | mHandler.sendEmptyMessage(MESSAGE_SAVE_POLICY_FILE); |
| 514 | } |
| 515 | |
| 516 | private void handleSavePolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 517 | if (DBG) Slog.d(TAG, "handleSavePolicyFile"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 518 | synchronized (mPolicyFile) { |
| 519 | final FileOutputStream stream; |
| 520 | try { |
| 521 | stream = mPolicyFile.startWrite(); |
| 522 | } catch (IOException e) { |
| 523 | Slog.w(TAG, "Failed to save policy file", e); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | try { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 528 | writePolicyXml(stream, false /*forBackup*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 529 | mPolicyFile.finishWrite(stream); |
| 530 | } catch (IOException e) { |
| 531 | Slog.w(TAG, "Failed to save policy file, restoring backup", e); |
| 532 | mPolicyFile.failWrite(stream); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 533 | } |
| 534 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 535 | BackupManager.dataChanged(getContext().getPackageName()); |
| 536 | } |
| 537 | |
| 538 | private void writePolicyXml(OutputStream stream, boolean forBackup) throws IOException { |
| 539 | final XmlSerializer out = new FastXmlSerializer(); |
| 540 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
| 541 | out.startDocument(null, true); |
| 542 | out.startTag(null, TAG_NOTIFICATION_POLICY); |
| 543 | out.attribute(null, ATTR_VERSION, Integer.toString(DB_VERSION)); |
| 544 | mZenModeHelper.writeXml(out, forBackup); |
| 545 | mRankingHelper.writeXml(out, forBackup); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 546 | mListeners.writeXml(out, forBackup); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 547 | mAssistants.writeXml(out, forBackup); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 548 | mConditionProviders.writeXml(out, forBackup); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 549 | out.endTag(null, TAG_NOTIFICATION_POLICY); |
| 550 | out.endDocument(); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 551 | } |
| 552 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 553 | private static final class ToastRecord |
| 554 | { |
| 555 | final int pid; |
| 556 | final String pkg; |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 557 | ITransientNotification callback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | int duration; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 559 | Binder token; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 561 | ToastRecord(int pid, String pkg, ITransientNotification callback, int duration, |
| 562 | Binder token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 563 | this.pid = pid; |
| 564 | this.pkg = pkg; |
| 565 | this.callback = callback; |
| 566 | this.duration = duration; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 567 | this.token = token; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | void update(int duration) { |
| 571 | this.duration = duration; |
| 572 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 573 | |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 574 | void update(ITransientNotification callback) { |
| 575 | this.callback = callback; |
| 576 | } |
| 577 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 578 | void dump(PrintWriter pw, String prefix, DumpFilter filter) { |
| 579 | if (filter != null && !filter.matches(pkg)) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | pw.println(prefix + this); |
| 581 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 582 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | @Override |
| 584 | public final String toString() |
| 585 | { |
| 586 | return "ToastRecord{" |
| 587 | + Integer.toHexString(System.identityHashCode(this)) |
| 588 | + " pkg=" + pkg |
| 589 | + " callback=" + callback |
| 590 | + " duration=" + duration; |
| 591 | } |
| 592 | } |
| 593 | |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 594 | @VisibleForTesting |
| 595 | final NotificationDelegate mNotificationDelegate = new NotificationDelegate() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 597 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | public void onSetDisabled(int status) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 599 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 600 | mDisableNotificationEffects = |
| 601 | (status & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 602 | if (disableNotificationEffects(null) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | // cancel whatever's going on |
| 604 | long identity = Binder.clearCallingIdentity(); |
| 605 | try { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 606 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 607 | if (player != null) { |
| 608 | player.stopAsync(); |
| 609 | } |
| 610 | } catch (RemoteException e) { |
| 611 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | Binder.restoreCallingIdentity(identity); |
| 613 | } |
| 614 | |
| 615 | identity = Binder.clearCallingIdentity(); |
| 616 | try { |
| 617 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 618 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | Binder.restoreCallingIdentity(identity); |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 625 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 626 | public void onClearAll(int callingUid, int callingPid, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 627 | synchronized (mNotificationLock) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 628 | cancelAllLocked(callingUid, callingPid, userId, REASON_CANCEL_ALL, null, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 629 | /*includeCurrentProfiles*/ true); |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 630 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 633 | @Override |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 634 | public void onNotificationClick(int callingUid, int callingPid, String key) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 635 | synchronized (mNotificationLock) { |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 636 | NotificationRecord r = mNotificationsByKey.get(key); |
| 637 | if (r == null) { |
| 638 | Log.w(TAG, "No notification with key: " + key); |
| 639 | return; |
| 640 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 641 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 642 | MetricsLogger.action(r.getLogMaker(now) |
| 643 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
| 644 | .setType(MetricsEvent.TYPE_ACTION)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 645 | EventLogTags.writeNotificationClicked(key, |
| 646 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
| 647 | |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 648 | StatusBarNotification sbn = r.sbn; |
| 649 | cancelNotification(callingUid, callingPid, sbn.getPackageName(), sbn.getTag(), |
| 650 | sbn.getId(), Notification.FLAG_AUTO_CANCEL, |
| 651 | Notification.FLAG_FOREGROUND_SERVICE, false, r.getUserId(), |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 652 | REASON_CLICK, null); |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 653 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | } |
| 655 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 656 | @Override |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 657 | public void onNotificationActionClick(int callingUid, int callingPid, String key, |
| 658 | int actionIndex) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 659 | synchronized (mNotificationLock) { |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 660 | NotificationRecord r = mNotificationsByKey.get(key); |
| 661 | if (r == null) { |
| 662 | Log.w(TAG, "No notification with key: " + key); |
| 663 | return; |
| 664 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 665 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 666 | MetricsLogger.action(r.getLogMaker(now) |
| 667 | .setCategory(MetricsEvent.NOTIFICATION_ITEM_ACTION) |
| 668 | .setType(MetricsEvent.TYPE_ACTION) |
| 669 | .setSubtype(actionIndex)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 670 | EventLogTags.writeNotificationActionClicked(key, actionIndex, |
| 671 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 672 | // TODO: Log action click via UsageStats. |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 677 | public void onNotificationClear(int callingUid, int callingPid, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 678 | String pkg, String tag, int id, int userId, String key, |
| 679 | @NotificationStats.DismissalSurface int dismissalSurface) { |
| 680 | synchronized (mNotificationLock) { |
| 681 | NotificationRecord r = mNotificationsByKey.get(key); |
| 682 | if (r != null) { |
| 683 | r.recordDismissalSurface(dismissalSurface); |
| 684 | } |
| 685 | } |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 686 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 687 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 688 | true, userId, REASON_CANCEL, null); |
Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 689 | } |
| 690 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 691 | @Override |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 692 | public void onPanelRevealed(boolean clearEffects, int items) { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 693 | MetricsLogger.visible(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Chris Wren | 621933f | 2017-06-14 15:59:03 -0400 | [diff] [blame] | 694 | MetricsLogger.histogram(getContext(), "note_load", items); |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 695 | EventLogTags.writeNotificationPanelRevealed(items); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 696 | if (clearEffects) { |
| 697 | clearEffects(); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | @Override |
| 702 | public void onPanelHidden() { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 703 | MetricsLogger.hidden(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 704 | EventLogTags.writeNotificationPanelHidden(); |
| 705 | } |
| 706 | |
| 707 | @Override |
| 708 | public void clearEffects() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 709 | synchronized (mNotificationLock) { |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 710 | if (DBG) Slog.d(TAG, "clearEffects"); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 711 | clearSoundLocked(); |
| 712 | clearVibrateLocked(); |
| 713 | clearLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | } |
| 715 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 716 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 717 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 718 | public void onNotificationError(int callingUid, int callingPid, String pkg, String tag, int id, |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 719 | int uid, int initialPid, String message, int userId) { |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 720 | Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id |
| 721 | + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")"); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 722 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, 0, false, userId, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 723 | REASON_ERROR, null); |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 724 | long ident = Binder.clearCallingIdentity(); |
| 725 | try { |
Christopher Tate | 8aa8fe1 | 2017-01-20 17:50:32 -0800 | [diff] [blame] | 726 | ActivityManager.getService().crashApplication(uid, initialPid, pkg, -1, |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 727 | "Bad notification posted from package " + pkg |
| 728 | + ": " + message); |
| 729 | } catch (RemoteException e) { |
| 730 | } |
| 731 | Binder.restoreCallingIdentity(ident); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 732 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 733 | |
| 734 | @Override |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 735 | public void onNotificationVisibilityChanged(NotificationVisibility[] newlyVisibleKeys, |
| 736 | NotificationVisibility[] noLongerVisibleKeys) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 737 | synchronized (mNotificationLock) { |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 738 | for (NotificationVisibility nv : newlyVisibleKeys) { |
| 739 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 740 | if (r == null) continue; |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 741 | if (!r.isSeen()) { |
| 742 | // Report to usage stats that notification was made visible |
| 743 | if (DBG) Slog.d(TAG, "Marking notification as visible " + nv.key); |
| 744 | reportSeen(r); |
| 745 | } |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 746 | r.setVisibility(true, nv.rank); |
| 747 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 748 | } |
| 749 | // Note that we might receive this event after notifications |
| 750 | // have already left the system, e.g. after dismissing from the |
| 751 | // shade. Hence not finding notifications in |
| 752 | // mNotificationsByKey is not an exceptional condition. |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 753 | for (NotificationVisibility nv : noLongerVisibleKeys) { |
| 754 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 755 | if (r == null) continue; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 756 | r.setVisibility(false, nv.rank); |
| 757 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 758 | } |
| 759 | } |
Christoph Studer | 92b389d | 2014-04-01 18:44:40 +0200 | [diff] [blame] | 760 | } |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 761 | |
| 762 | @Override |
| 763 | public void onNotificationExpansionChanged(String key, |
| 764 | boolean userAction, boolean expanded) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 765 | synchronized (mNotificationLock) { |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 766 | NotificationRecord r = mNotificationsByKey.get(key); |
| 767 | if (r != null) { |
| 768 | r.stats.onExpansionChanged(userAction, expanded); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 769 | final long now = System.currentTimeMillis(); |
Chris Wren | f734271 | 2017-09-14 10:55:55 -0400 | [diff] [blame] | 770 | if (userAction) { |
Chris Wren | 377ac6d | 2017-09-12 14:15:23 -0400 | [diff] [blame] | 771 | MetricsLogger.action(r.getLogMaker(now) |
| 772 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
Chris Wren | f734271 | 2017-09-14 10:55:55 -0400 | [diff] [blame] | 773 | .setType(expanded ? MetricsEvent.TYPE_DETAIL |
| 774 | : MetricsEvent.TYPE_COLLAPSE)); |
Chris Wren | 377ac6d | 2017-09-12 14:15:23 -0400 | [diff] [blame] | 775 | } |
Julia Reynolds | 84dc96b | 2017-11-14 09:51:01 -0500 | [diff] [blame] | 776 | if (expanded && userAction) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 777 | r.recordExpanded(); |
| 778 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 779 | EventLogTags.writeNotificationExpansion(key, |
| 780 | userAction ? 1 : 0, expanded ? 1 : 0, |
| 781 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 785 | |
| 786 | @Override |
| 787 | public void onNotificationDirectReplied(String key) { |
| 788 | synchronized (mNotificationLock) { |
| 789 | NotificationRecord r = mNotificationsByKey.get(key); |
| 790 | if (r != null) { |
| 791 | r.recordDirectReplied(); |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | @Override |
| 797 | public void onNotificationSettingsViewed(String key) { |
| 798 | synchronized (mNotificationLock) { |
| 799 | NotificationRecord r = mNotificationsByKey.get(key); |
| 800 | if (r != null) { |
| 801 | r.recordViewedSettings(); |
| 802 | } |
| 803 | } |
| 804 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | }; |
| 806 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 807 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 808 | private void clearSoundLocked() { |
| 809 | mSoundNotificationKey = null; |
| 810 | long identity = Binder.clearCallingIdentity(); |
| 811 | try { |
| 812 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 813 | if (player != null) { |
| 814 | player.stopAsync(); |
| 815 | } |
| 816 | } catch (RemoteException e) { |
| 817 | } finally { |
| 818 | Binder.restoreCallingIdentity(identity); |
| 819 | } |
| 820 | } |
| 821 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 822 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 823 | private void clearVibrateLocked() { |
| 824 | mVibrateNotificationKey = null; |
| 825 | long identity = Binder.clearCallingIdentity(); |
| 826 | try { |
| 827 | mVibrator.cancel(); |
| 828 | } finally { |
| 829 | Binder.restoreCallingIdentity(identity); |
| 830 | } |
| 831 | } |
| 832 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 833 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 834 | private void clearLightsLocked() { |
| 835 | // light |
| 836 | mLights.clear(); |
| 837 | updateLightsLocked(); |
| 838 | } |
| 839 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 840 | protected final BroadcastReceiver mLocaleChangeReceiver = new BroadcastReceiver() { |
| 841 | @Override |
| 842 | public void onReceive(Context context, Intent intent) { |
| 843 | if (Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) { |
| 844 | mZenModeHelper.updateDefaultZenRules(); |
Julia Reynolds | 816797a | 2017-08-11 15:47:09 -0400 | [diff] [blame] | 845 | mRankingHelper.onLocaleChanged(context, ActivityManager.getCurrentUser()); |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | }; |
| 849 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 850 | private final BroadcastReceiver mRestoreReceiver = new BroadcastReceiver() { |
| 851 | @Override |
| 852 | public void onReceive(Context context, Intent intent) { |
| 853 | if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) { |
| 854 | try { |
| 855 | String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME); |
| 856 | String newValue = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE); |
Michal Karpinski | 6135a26 | 2017-08-11 10:45:58 +0100 | [diff] [blame] | 857 | int restoredFromSdkInt = intent.getIntExtra( |
| 858 | Intent.EXTRA_SETTING_RESTORED_FROM_SDK_INT, 0); |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 859 | mListeners.onSettingRestored( |
| 860 | element, newValue, restoredFromSdkInt, getSendingUserId()); |
| 861 | mConditionProviders.onSettingRestored( |
| 862 | element, newValue, restoredFromSdkInt, getSendingUserId()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 863 | } catch (Exception e) { |
| 864 | Slog.wtf(TAG, "Cannot restore managed services from settings", e); |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | }; |
| 869 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 870 | private final BroadcastReceiver mNotificationTimeoutReceiver = new BroadcastReceiver() { |
| 871 | @Override |
| 872 | public void onReceive(Context context, Intent intent) { |
| 873 | String action = intent.getAction(); |
| 874 | if (action == null) { |
| 875 | return; |
| 876 | } |
| 877 | if (ACTION_NOTIFICATION_TIMEOUT.equals(action)) { |
| 878 | final NotificationRecord record; |
| 879 | synchronized (mNotificationLock) { |
| 880 | record = findNotificationByKeyLocked(intent.getStringExtra(EXTRA_KEY)); |
| 881 | } |
| 882 | if (record != null) { |
| 883 | cancelNotification(record.sbn.getUid(), record.sbn.getInitialPid(), |
| 884 | record.sbn.getPackageName(), record.sbn.getTag(), |
| 885 | record.sbn.getId(), 0, |
| 886 | Notification.FLAG_FOREGROUND_SERVICE, true, record.getUserId(), |
| 887 | REASON_TIMEOUT, null); |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | }; |
| 892 | |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 893 | private final BroadcastReceiver mPackageIntentReceiver = new BroadcastReceiver() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 894 | @Override |
| 895 | public void onReceive(Context context, Intent intent) { |
| 896 | String action = intent.getAction(); |
Dianne Hackborn | 29cd7f1 | 2015-01-08 10:37:05 -0800 | [diff] [blame] | 897 | if (action == null) { |
| 898 | return; |
| 899 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 901 | boolean queryRestart = false; |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 902 | boolean queryRemove = false; |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 903 | boolean packageChanged = false; |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 904 | boolean cancelNotifications = true; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 905 | int reason = REASON_PACKAGE_CHANGED; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 906 | |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 907 | if (action.equals(Intent.ACTION_PACKAGE_ADDED) |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 908 | || (queryRemove=action.equals(Intent.ACTION_PACKAGE_REMOVED)) |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 909 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 910 | || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED)) |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 911 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 912 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE) |
| 913 | || action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 914 | int changeUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 915 | UserHandle.USER_ALL); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 916 | String pkgList[] = null; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 917 | int uidList[] = null; |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 918 | boolean removingPackage = queryRemove && |
| 919 | !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 920 | if (DBG) Slog.i(TAG, "action=" + action + " removing=" + removingPackage); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 921 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 922 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 923 | uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 924 | } else if (action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
| 925 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 926 | reason = REASON_PACKAGE_SUSPENDED; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 927 | } else if (queryRestart) { |
| 928 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 929 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 930 | } else { |
| 931 | Uri uri = intent.getData(); |
| 932 | if (uri == null) { |
| 933 | return; |
| 934 | } |
| 935 | String pkgName = uri.getSchemeSpecificPart(); |
| 936 | if (pkgName == null) { |
| 937 | return; |
| 938 | } |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 939 | if (packageChanged) { |
| 940 | // We cancel notifications for packages which have just been disabled |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 941 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 942 | final int enabled = mPackageManager.getApplicationEnabledSetting( |
| 943 | pkgName, |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 944 | changeUserId != UserHandle.USER_ALL ? changeUserId : |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 945 | UserHandle.USER_SYSTEM); |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 946 | if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 947 | || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 948 | cancelNotifications = false; |
| 949 | } |
| 950 | } catch (IllegalArgumentException e) { |
| 951 | // Package doesn't exist; probably racing with uninstall. |
| 952 | // cancelNotifications is already true, so nothing to do here. |
| 953 | if (DBG) { |
| 954 | Slog.i(TAG, "Exception trying to look up app enabled setting", e); |
| 955 | } |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 956 | } catch (RemoteException e) { |
| 957 | // Failed to talk to PackageManagerService Should never happen! |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 958 | } |
| 959 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 960 | pkgList = new String[]{pkgName}; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 961 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 962 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 963 | if (pkgList != null && (pkgList.length > 0)) { |
| 964 | for (String pkgName : pkgList) { |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 965 | if (cancelNotifications) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 966 | cancelAllNotificationsInt(MY_UID, MY_PID, pkgName, null, 0, 0, |
| 967 | !queryRestart, changeUserId, reason, null); |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 968 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 969 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 970 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 971 | mListeners.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 972 | mAssistants.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 973 | mConditionProviders.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 974 | mRankingHelper.onPackagesChanged(removingPackage, changeUserId, pkgList, uidList); |
| 975 | savePolicyFile(); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | }; |
| 979 | |
| 980 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 981 | @Override |
| 982 | public void onReceive(Context context, Intent intent) { |
| 983 | String action = intent.getAction(); |
| 984 | |
| 985 | if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 986 | // Keep track of screen on/off state, but do not turn off the notification light |
| 987 | // until user passes through the lock screen or views the notification. |
| 988 | mScreenOn = true; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 989 | updateNotificationPulse(); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 990 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 991 | mScreenOn = false; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 992 | updateNotificationPulse(); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 993 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
John Spurlock | 5d2eeb1 | 2014-01-16 10:46:36 -0500 | [diff] [blame] | 994 | mInCall = TelephonyManager.EXTRA_STATE_OFFHOOK |
| 995 | .equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE)); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 996 | updateNotificationPulse(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 997 | } else if (action.equals(Intent.ACTION_USER_STOPPED)) { |
| 998 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 999 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1000 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1001 | REASON_USER_STOPPED, null); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1002 | } |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1003 | } else if (action.equals(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) { |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 1004 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1005 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1006 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1007 | REASON_PROFILE_TURNED_OFF, null); |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 1008 | } |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1009 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { |
| 1010 | // turn off LED when user passes through lock screen |
| 1011 | mNotificationLight.turnOff(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1012 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1013 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1014 | // reload per-user settings |
| 1015 | mSettingsObserver.update(null); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1016 | mUserProfiles.updateCache(context); |
Christoph Studer | b53dfd4 | 2014-09-12 14:45:59 +0200 | [diff] [blame] | 1017 | // Refresh managed services |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 1018 | mConditionProviders.onUserSwitched(user); |
| 1019 | mListeners.onUserSwitched(user); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1020 | mAssistants.onUserSwitched(user); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1021 | mZenModeHelper.onUserSwitched(user); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1022 | } else if (action.equals(Intent.ACTION_USER_ADDED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1023 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
| 1024 | if (userId != USER_NULL) { |
| 1025 | mUserProfiles.updateCache(context); |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 1026 | if (!mUserProfiles.isManagedProfile(userId)) { |
| 1027 | readDefaultApprovedServices(userId); |
| 1028 | } |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1029 | } |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1030 | } else if (action.equals(Intent.ACTION_USER_REMOVED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1031 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1032 | mZenModeHelper.onUserRemoved(user); |
Julia Reynolds | 2e9bf5f | 2017-05-03 13:23:30 -0400 | [diff] [blame] | 1033 | mRankingHelper.onUserRemoved(user); |
Julia Reynolds | 5aa13a4 | 2017-08-24 09:10:23 -0400 | [diff] [blame] | 1034 | mListeners.onUserRemoved(user); |
| 1035 | mConditionProviders.onUserRemoved(user); |
| 1036 | mAssistants.onUserRemoved(user); |
Julia Reynolds | 2e9bf5f | 2017-05-03 13:23:30 -0400 | [diff] [blame] | 1037 | savePolicyFile(); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1038 | } else if (action.equals(Intent.ACTION_USER_UNLOCKED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 1039 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1040 | mConditionProviders.onUserUnlocked(user); |
| 1041 | mListeners.onUserUnlocked(user); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1042 | mAssistants.onUserUnlocked(user); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1043 | mZenModeHelper.onUserUnlocked(user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | }; |
| 1047 | |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 1048 | private final class SettingsObserver extends ContentObserver { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1049 | private final Uri NOTIFICATION_BADGING_URI |
| 1050 | = Settings.Secure.getUriFor(Settings.Secure.NOTIFICATION_BADGING); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1051 | private final Uri NOTIFICATION_LIGHT_PULSE_URI |
| 1052 | = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1053 | private final Uri NOTIFICATION_RATE_LIMIT_URI |
| 1054 | = Settings.Global.getUriFor(Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1055 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1056 | SettingsObserver(Handler handler) { |
| 1057 | super(handler); |
| 1058 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1059 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1060 | void observe() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1061 | ContentResolver resolver = getContext().getContentResolver(); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1062 | resolver.registerContentObserver(NOTIFICATION_BADGING_URI, |
| 1063 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1064 | resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1065 | false, this, UserHandle.USER_ALL); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1066 | resolver.registerContentObserver(NOTIFICATION_RATE_LIMIT_URI, |
| 1067 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1068 | update(null); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1071 | @Override public void onChange(boolean selfChange, Uri uri) { |
| 1072 | update(uri); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1075 | public void update(Uri uri) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1076 | ContentResolver resolver = getContext().getContentResolver(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1077 | if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { |
zhoulei | 7e37697 | 2017-05-17 18:41:25 +0800 | [diff] [blame] | 1078 | boolean pulseEnabled = Settings.System.getIntForUser(resolver, |
| 1079 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0, UserHandle.USER_CURRENT) != 0; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1080 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 1081 | mNotificationPulseEnabled = pulseEnabled; |
| 1082 | updateNotificationPulse(); |
| 1083 | } |
| 1084 | } |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1085 | if (uri == null || NOTIFICATION_RATE_LIMIT_URI.equals(uri)) { |
| 1086 | mMaxPackageEnqueueRate = Settings.Global.getFloat(resolver, |
| 1087 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, mMaxPackageEnqueueRate); |
| 1088 | } |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1089 | if (uri == null || NOTIFICATION_BADGING_URI.equals(uri)) { |
| 1090 | mRankingHelper.updateBadgingEnabled(); |
| 1091 | } |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1092 | } |
| 1093 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1094 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1095 | private SettingsObserver mSettingsObserver; |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1096 | protected ZenModeHelper mZenModeHelper; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1097 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1098 | static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) { |
| 1099 | int[] ar = r.getIntArray(resid); |
| 1100 | if (ar == null) { |
| 1101 | return def; |
| 1102 | } |
| 1103 | final int len = ar.length > maxlen ? maxlen : ar.length; |
| 1104 | long[] out = new long[len]; |
| 1105 | for (int i=0; i<len; i++) { |
| 1106 | out[i] = ar[i]; |
| 1107 | } |
| 1108 | return out; |
| 1109 | } |
| 1110 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 1111 | public NotificationManagerService(Context context) { |
| 1112 | super(context); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 1113 | Notification.processWhitelistToken = WHITELIST_TOKEN; |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 1114 | } |
| 1115 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1116 | // TODO - replace these methods with a single VisibleForTesting constructor |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1117 | @VisibleForTesting |
| 1118 | void setAudioManager(AudioManager audioMananger) { |
| 1119 | mAudioManager = audioMananger; |
| 1120 | } |
| 1121 | |
| 1122 | @VisibleForTesting |
| 1123 | void setVibrator(Vibrator vibrator) { |
| 1124 | mVibrator = vibrator; |
| 1125 | } |
| 1126 | |
| 1127 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1128 | void setLights(Light light) { |
| 1129 | mNotificationLight = light; |
| 1130 | mAttentionLight = light; |
Julia Reynolds | 033a412 | 2017-01-31 16:50:38 -0500 | [diff] [blame] | 1131 | mNotificationPulseEnabled = true; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | @VisibleForTesting |
| 1135 | void setScreenOn(boolean on) { |
| 1136 | mScreenOn = on; |
| 1137 | } |
| 1138 | |
| 1139 | @VisibleForTesting |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1140 | int getNotificationRecordCount() { |
| 1141 | synchronized (mNotificationLock) { |
| 1142 | int count = mNotificationList.size() + mNotificationsByKey.size() |
| 1143 | + mSummaryByGroupKey.size() + mEnqueuedNotifications.size(); |
| 1144 | // subtract duplicates |
| 1145 | for (NotificationRecord posted : mNotificationList) { |
| 1146 | if (mNotificationsByKey.containsKey(posted.getKey())) { |
| 1147 | count--; |
| 1148 | } |
| 1149 | if (posted.sbn.isGroup() && posted.getNotification().isGroupSummary()) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1150 | count--; |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | return count; |
| 1155 | } |
| 1156 | } |
| 1157 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1158 | void clearNotifications() { |
| 1159 | mEnqueuedNotifications.clear(); |
| 1160 | mNotificationList.clear(); |
| 1161 | mNotificationsByKey.clear(); |
| 1162 | mSummaryByGroupKey.clear(); |
| 1163 | } |
| 1164 | |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1165 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1166 | void addNotification(NotificationRecord r) { |
| 1167 | mNotificationList.add(r); |
| 1168 | mNotificationsByKey.put(r.sbn.getKey(), r); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1169 | if (r.sbn.isGroup()) { |
| 1170 | mSummaryByGroupKey.put(r.getGroupKey(), r); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | @VisibleForTesting |
| 1175 | void addEnqueuedNotification(NotificationRecord r) { |
| 1176 | mEnqueuedNotifications.add(r); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | @VisibleForTesting |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1180 | NotificationRecord getNotificationRecord(String key) { |
| 1181 | return mNotificationsByKey.get(key); |
| 1182 | } |
| 1183 | |
| 1184 | |
| 1185 | @VisibleForTesting |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1186 | void setSystemReady(boolean systemReady) { |
| 1187 | mSystemReady = systemReady; |
| 1188 | } |
| 1189 | |
| 1190 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1191 | void setHandler(WorkerHandler handler) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1192 | mHandler = handler; |
| 1193 | } |
| 1194 | |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1195 | @VisibleForTesting |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 1196 | void setFallbackVibrationPattern(long[] vibrationPattern) { |
| 1197 | mFallbackVibrationPattern = vibrationPattern; |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1198 | } |
| 1199 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1200 | @VisibleForTesting |
| 1201 | void setPackageManager(IPackageManager packageManager) { |
| 1202 | mPackageManager = packageManager; |
| 1203 | } |
| 1204 | |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1205 | @VisibleForTesting |
| 1206 | void setRankingHelper(RankingHelper rankingHelper) { |
| 1207 | mRankingHelper = rankingHelper; |
| 1208 | } |
| 1209 | |
| 1210 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1211 | void setRankingHandler(RankingHandler rankingHandler) { |
| 1212 | mRankingHandler = rankingHandler; |
| 1213 | } |
| 1214 | |
| 1215 | @VisibleForTesting |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1216 | void setIsTelevision(boolean isTelevision) { |
| 1217 | mIsTelevision = isTelevision; |
| 1218 | } |
| 1219 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 1220 | @VisibleForTesting |
| 1221 | void setUsageStats(NotificationUsageStats us) { |
| 1222 | mUsageStats = us; |
| 1223 | } |
| 1224 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 1225 | @VisibleForTesting |
| 1226 | void setAccessibilityManager(AccessibilityManager am) { |
| 1227 | mAccessibilityManager = am; |
| 1228 | } |
| 1229 | |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1230 | // 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] | 1231 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1232 | void init(Looper looper, IPackageManager packageManager, |
| 1233 | PackageManager packageManagerClient, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1234 | LightsManager lightsManager, NotificationListeners notificationListeners, |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1235 | NotificationAssistants notificationAssistants, ConditionProviders conditionProviders, |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1236 | ICompanionDeviceManager companionManager, SnoozeHelper snoozeHelper, |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1237 | NotificationUsageStats usageStats, AtomicFile policyFile, |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1238 | ActivityManager activityManager, GroupHelper groupHelper) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1239 | Resources resources = getContext().getResources(); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1240 | mMaxPackageEnqueueRate = Settings.Global.getFloat(getContext().getContentResolver(), |
| 1241 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, |
| 1242 | DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE); |
| 1243 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 1244 | mAccessibilityManager = |
| 1245 | (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1246 | mAm = ActivityManager.getService(); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1247 | mPackageManager = packageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1248 | mPackageManagerClient = packageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1249 | mAppOps = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE); |
| 1250 | mVibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 1251 | mAppUsageStats = LocalServices.getService(UsageStatsManagerInternal.class); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1252 | mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1253 | mCompanionManager = companionManager; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1254 | mActivityManager = activityManager; |
San Mehat | 3ee1317 | 2010-02-04 20:54:43 -0800 | [diff] [blame] | 1255 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1256 | mHandler = new WorkerHandler(looper); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1257 | mRankingThread.start(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1258 | String[] extractorNames; |
| 1259 | try { |
| 1260 | extractorNames = resources.getStringArray(R.array.config_notificationSignalExtractors); |
| 1261 | } catch (Resources.NotFoundException e) { |
| 1262 | extractorNames = new String[0]; |
| 1263 | } |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1264 | mUsageStats = usageStats; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1265 | mRankingHandler = new RankingHandlerWorker(mRankingThread.getLooper()); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1266 | mRankingHelper = new RankingHelper(getContext(), |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1267 | mPackageManagerClient, |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1268 | mRankingHandler, |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 1269 | mUsageStats, |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1270 | extractorNames); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1271 | mConditionProviders = conditionProviders; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1272 | mZenModeHelper = new ZenModeHelper(getContext(), mHandler.getLooper(), mConditionProviders); |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 1273 | mZenModeHelper.addCallback(new ZenModeHelper.Callback() { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1274 | @Override |
| 1275 | public void onConfigChanged() { |
| 1276 | savePolicyFile(); |
| 1277 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1278 | |
| 1279 | @Override |
| 1280 | void onZenModeChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1281 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED); |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1282 | getContext().sendBroadcastAsUser( |
Jason Monk | 6350674 | 2015-12-16 12:06:51 -0500 | [diff] [blame] | 1283 | new Intent(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL) |
| 1284 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT), |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1285 | UserHandle.ALL, android.Manifest.permission.MANAGE_NOTIFICATIONS); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1286 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1287 | updateInterruptionFilterLocked(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1288 | } |
| 1289 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1290 | |
| 1291 | @Override |
| 1292 | void onPolicyChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1293 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED); |
| 1294 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1295 | }); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1296 | mSnoozeHelper = snoozeHelper; |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1297 | mGroupHelper = groupHelper; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 1298 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1299 | // This is a ManagedServices object that keeps track of the listeners. |
| 1300 | mListeners = notificationListeners; |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1301 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 1302 | // This is a MangedServices object that keeps track of the assistant. |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1303 | mAssistants = notificationAssistants; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1304 | |
| 1305 | mPolicyFile = policyFile; |
| 1306 | loadPolicyFile(); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1307 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1308 | mStatusBar = getLocalService(StatusBarManagerInternal.class); |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 1309 | if (mStatusBar != null) { |
| 1310 | mStatusBar.setNotificationDelegate(mNotificationDelegate); |
| 1311 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1312 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1313 | mNotificationLight = lightsManager.getLight(LightsManager.LIGHT_ID_NOTIFICATIONS); |
| 1314 | mAttentionLight = lightsManager.getLight(LightsManager.LIGHT_ID_ATTENTION); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1315 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1316 | mFallbackVibrationPattern = getLongArray(resources, |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 1317 | R.array.config_notificationFallbackVibePattern, |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1318 | VIBRATE_PATTERN_MAXLEN, |
| 1319 | DEFAULT_VIBRATE_PATTERN); |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 1320 | mInCallNotificationUri = Uri.parse("file://" + |
| 1321 | resources.getString(R.string.config_inCallNotificationSound)); |
| 1322 | mInCallNotificationAudioAttributes = new AudioAttributes.Builder() |
| 1323 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 1324 | .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 1325 | .build(); |
| 1326 | mInCallNotificationVolume = resources.getFloat(R.dimen.config_inCallNotificationVolume); |
| 1327 | |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 1328 | mUseAttentionLight = resources.getBoolean(R.bool.config_useAttentionLight); |
| 1329 | |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1330 | // Don't start allowing notifications until the setup wizard has run once. |
| 1331 | // After that, including subsequent boots, init with notifications turned on. |
| 1332 | // This works on the first boot because the setup wizard will toggle this |
| 1333 | // 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] | 1334 | if (0 == Settings.Global.getInt(getContext().getContentResolver(), |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1335 | Settings.Global.DEVICE_PROVISIONED, 0)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1336 | mDisableNotificationEffects = true; |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1337 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1338 | mZenModeHelper.initZenMode(); |
John Spurlock | f370177 | 2015-02-12 13:29:37 -0500 | [diff] [blame] | 1339 | mInterruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1340 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1341 | mUserProfiles.updateCache(getContext()); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 1342 | listenForCallState(); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1343 | |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1344 | mSettingsObserver = new SettingsObserver(mHandler); |
| 1345 | |
| 1346 | mArchive = new Archive(resources.getInteger( |
| 1347 | R.integer.config_notificationServiceArchiveSize)); |
| 1348 | |
| 1349 | mIsTelevision = mPackageManagerClient.hasSystemFeature(FEATURE_LEANBACK) |
| 1350 | || mPackageManagerClient.hasSystemFeature(FEATURE_TELEVISION); |
| 1351 | } |
| 1352 | |
| 1353 | @Override |
| 1354 | public void onStart() { |
| 1355 | SnoozeHelper snoozeHelper = new SnoozeHelper(getContext(), new SnoozeHelper.Callback() { |
| 1356 | @Override |
| 1357 | public void repost(int userId, NotificationRecord r) { |
| 1358 | try { |
| 1359 | if (DBG) { |
| 1360 | Slog.d(TAG, "Reposting " + r.getKey()); |
| 1361 | } |
| 1362 | enqueueNotificationInternal(r.sbn.getPackageName(), r.sbn.getOpPkg(), |
| 1363 | r.sbn.getUid(), r.sbn.getInitialPid(), r.sbn.getTag(), r.sbn.getId(), |
| 1364 | r.sbn.getNotification(), userId); |
| 1365 | } catch (Exception e) { |
| 1366 | Slog.e(TAG, "Cannot un-snooze notification", e); |
| 1367 | } |
| 1368 | } |
| 1369 | }, mUserProfiles); |
| 1370 | |
| 1371 | final File systemDir = new File(Environment.getDataDirectory(), "system"); |
| 1372 | |
| 1373 | init(Looper.myLooper(), |
| 1374 | AppGlobals.getPackageManager(), getContext().getPackageManager(), |
| 1375 | getLocalService(LightsManager.class), |
| 1376 | new NotificationListeners(AppGlobals.getPackageManager()), |
| 1377 | new NotificationAssistants(AppGlobals.getPackageManager()), |
| 1378 | new ConditionProviders(getContext(), mUserProfiles, AppGlobals.getPackageManager()), |
| 1379 | null, snoozeHelper, new NotificationUsageStats(getContext()), |
| 1380 | new AtomicFile(new File(systemDir, "notification_policy.xml")), |
| 1381 | (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE), |
| 1382 | getGroupHelper()); |
| 1383 | |
Mike Lockwood | 35e16bf | 2010-11-30 19:53:36 -0500 | [diff] [blame] | 1384 | // register for various Intents |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1385 | IntentFilter filter = new IntentFilter(); |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1386 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 1387 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1388 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1389 | filter.addAction(Intent.ACTION_USER_PRESENT); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1390 | filter.addAction(Intent.ACTION_USER_STOPPED); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1391 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1392 | filter.addAction(Intent.ACTION_USER_ADDED); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1393 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1394 | filter.addAction(Intent.ACTION_USER_UNLOCKED); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1395 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1396 | getContext().registerReceiver(mIntentReceiver, filter); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1397 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1398 | IntentFilter pkgFilter = new IntentFilter(); |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1399 | pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1400 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
Daniel Sandler | aac0eb0 | 2011-08-06 22:51:56 -0400 | [diff] [blame] | 1401 | pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1402 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 1403 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 1404 | pkgFilter.addDataScheme("package"); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1405 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, pkgFilter, null, |
| 1406 | null); |
| 1407 | |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1408 | IntentFilter suspendedPkgFilter = new IntentFilter(); |
| 1409 | suspendedPkgFilter.addAction(Intent.ACTION_PACKAGES_SUSPENDED); |
| 1410 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, |
| 1411 | suspendedPkgFilter, null, null); |
| 1412 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1413 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1414 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, sdFilter, null, |
| 1415 | null); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1416 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1417 | IntentFilter timeoutFilter = new IntentFilter(ACTION_NOTIFICATION_TIMEOUT); |
| 1418 | timeoutFilter.addDataScheme(SCHEME_TIMEOUT); |
| 1419 | getContext().registerReceiver(mNotificationTimeoutReceiver, timeoutFilter); |
| 1420 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1421 | IntentFilter settingsRestoredFilter = new IntentFilter(Intent.ACTION_SETTING_RESTORED); |
| 1422 | getContext().registerReceiver(mRestoreReceiver, settingsRestoredFilter); |
| 1423 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1424 | IntentFilter localeChangedFilter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED); |
| 1425 | getContext().registerReceiver(mLocaleChangeReceiver, localeChangedFilter); |
| 1426 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1427 | publishBinderService(Context.NOTIFICATION_SERVICE, mService); |
| 1428 | publishLocalService(NotificationManagerInternal.class, mInternalService); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1429 | } |
| 1430 | |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1431 | private GroupHelper getGroupHelper() { |
| 1432 | return new GroupHelper(new GroupHelper.Callback() { |
| 1433 | @Override |
| 1434 | public void addAutoGroup(String key) { |
| 1435 | synchronized (mNotificationLock) { |
| 1436 | addAutogroupKeyLocked(key); |
| 1437 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | @Override |
| 1441 | public void removeAutoGroup(String key) { |
| 1442 | synchronized (mNotificationLock) { |
| 1443 | removeAutogroupKeyLocked(key); |
| 1444 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | @Override |
| 1448 | public void addAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 1449 | createAutoGroupSummary(userId, pkg, triggeringKey); |
| 1450 | } |
| 1451 | |
| 1452 | @Override |
| 1453 | public void removeAutoGroupSummary(int userId, String pkg) { |
| 1454 | synchronized (mNotificationLock) { |
| 1455 | clearAutogroupSummaryLocked(userId, pkg); |
| 1456 | } |
| 1457 | } |
| 1458 | }); |
| 1459 | } |
| 1460 | |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1461 | private void sendRegisteredOnlyBroadcast(String action) { |
| 1462 | getContext().sendBroadcastAsUser(new Intent(action) |
| 1463 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), UserHandle.ALL, null); |
| 1464 | } |
| 1465 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1466 | @Override |
| 1467 | public void onBootPhase(int phase) { |
| 1468 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 1469 | // no beeping until we're basically done booting |
| 1470 | mSystemReady = true; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1471 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1472 | // Grab our optional AudioService |
| 1473 | mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1474 | mAudioManagerInternal = getLocalService(AudioManagerInternal.class); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1475 | mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1476 | mZenModeHelper.onSystemReady(); |
Adam Lesinski | a6db4ab | 2014-03-24 12:31:45 -0700 | [diff] [blame] | 1477 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 1478 | // This observer will force an update when observe is called, causing us to |
| 1479 | // bind to listener services. |
| 1480 | mSettingsObserver.observe(); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1481 | mListeners.onBootPhaseAppsCanStart(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1482 | mAssistants.onBootPhaseAppsCanStart(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1483 | mConditionProviders.onBootPhaseAppsCanStart(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1484 | } |
| 1485 | } |
| 1486 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1487 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1488 | private void updateListenerHintsLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1489 | final int hints = calculateHints(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1490 | if (hints == mListenerHints) return; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1491 | ZenLog.traceListenerHintsChanged(mListenerHints, hints, mEffectsSuppressors.size()); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1492 | mListenerHints = hints; |
| 1493 | scheduleListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 1494 | } |
| 1495 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1496 | @GuardedBy("mNotificationLock") |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1497 | private void updateEffectsSuppressorLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1498 | final long updatedSuppressedEffects = calculateSuppressedEffects(); |
| 1499 | if (updatedSuppressedEffects == mZenModeHelper.getSuppressedEffects()) return; |
| 1500 | final List<ComponentName> suppressors = getSuppressors(); |
| 1501 | ZenLog.traceEffectsSuppressorChanged(mEffectsSuppressors, suppressors, updatedSuppressedEffects); |
| 1502 | mEffectsSuppressors = suppressors; |
| 1503 | mZenModeHelper.setSuppressedEffects(updatedSuppressedEffects); |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1504 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1505 | } |
| 1506 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1507 | private void updateNotificationChannelInt(String pkg, int uid, NotificationChannel channel, |
| 1508 | boolean fromListener) { |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1509 | if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) { |
| 1510 | // cancel |
| 1511 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channel.getId(), 0, 0, true, |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1512 | UserHandle.getUserId(uid), REASON_CHANNEL_BANNED, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1513 | null); |
Julia Reynolds | 33bef2c | 2017-09-05 11:07:18 -0400 | [diff] [blame] | 1514 | if (isUidSystemOrPhone(uid)) { |
| 1515 | int[] profileIds = mUserProfiles.getCurrentProfileIds(); |
| 1516 | int N = profileIds.length; |
| 1517 | for (int i = 0; i < N; i++) { |
| 1518 | int profileId = profileIds[i]; |
| 1519 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channel.getId(), 0, 0, true, |
| 1520 | profileId, REASON_CHANNEL_BANNED, |
| 1521 | null); |
| 1522 | } |
| 1523 | } |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1524 | } |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1525 | final NotificationChannel preUpdate = |
| 1526 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), true); |
| 1527 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 1528 | mRankingHelper.updateNotificationChannel(pkg, uid, channel, true); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1529 | maybeNotifyChannelOwner(pkg, uid, preUpdate, channel); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1530 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1531 | if (!fromListener) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1532 | final NotificationChannel modifiedChannel = |
| 1533 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), false); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1534 | mListeners.notifyNotificationChannelChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1535 | pkg, UserHandle.getUserHandleForUid(uid), |
| 1536 | modifiedChannel, NOTIFICATION_CHANNEL_OR_GROUP_UPDATED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1537 | } |
| 1538 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1539 | savePolicyFile(); |
| 1540 | } |
| 1541 | |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1542 | private void maybeNotifyChannelOwner(String pkg, int uid, NotificationChannel preUpdate, |
| 1543 | NotificationChannel update) { |
| 1544 | try { |
| 1545 | if ((preUpdate.getImportance() == IMPORTANCE_NONE |
| 1546 | && update.getImportance() != IMPORTANCE_NONE) |
| 1547 | || (preUpdate.getImportance() != IMPORTANCE_NONE |
| 1548 | && update.getImportance() == IMPORTANCE_NONE)) { |
| 1549 | getContext().sendBroadcastAsUser( |
| 1550 | new Intent(ACTION_NOTIFICATION_CHANNEL_BLOCK_STATE_CHANGED) |
| 1551 | .putExtra(NotificationManager.EXTRA_BLOCK_STATE_CHANGED_ID, |
| 1552 | update.getId()) |
| 1553 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, |
| 1554 | update.getImportance() == IMPORTANCE_NONE) |
| 1555 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 1556 | .setPackage(pkg), |
| 1557 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 1558 | } |
| 1559 | } catch (SecurityException e) { |
| 1560 | Slog.w(TAG, "Can't notify app about channel change", e); |
| 1561 | } |
| 1562 | } |
| 1563 | |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1564 | private void createNotificationChannelGroup(String pkg, int uid, NotificationChannelGroup group, |
| 1565 | boolean fromApp, boolean fromListener) { |
| 1566 | Preconditions.checkNotNull(group); |
| 1567 | Preconditions.checkNotNull(pkg); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1568 | |
| 1569 | final NotificationChannelGroup preUpdate = |
| 1570 | mRankingHelper.getNotificationChannelGroup(group.getId(), pkg, uid); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1571 | mRankingHelper.createNotificationChannelGroup(pkg, uid, group, |
| 1572 | fromApp); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1573 | if (!fromApp) { |
| 1574 | maybeNotifyChannelGroupOwner(pkg, uid, preUpdate, group); |
| 1575 | } |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1576 | if (!fromListener) { |
| 1577 | mListeners.notifyNotificationChannelGroupChanged(pkg, |
| 1578 | UserHandle.of(UserHandle.getCallingUserId()), group, |
| 1579 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
| 1580 | } |
| 1581 | } |
| 1582 | |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1583 | private void maybeNotifyChannelGroupOwner(String pkg, int uid, |
| 1584 | NotificationChannelGroup preUpdate, NotificationChannelGroup update) { |
| 1585 | try { |
| 1586 | if (preUpdate.isBlocked() != update.isBlocked()) { |
| 1587 | getContext().sendBroadcastAsUser( |
| 1588 | new Intent(ACTION_NOTIFICATION_CHANNEL_GROUP_BLOCK_STATE_CHANGED) |
| 1589 | .putExtra(NotificationManager.EXTRA_BLOCK_STATE_CHANGED_ID, |
| 1590 | update.getId()) |
| 1591 | .putExtra(NotificationManager.EXTRA_BLOCKED_STATE, |
| 1592 | update.isBlocked()) |
| 1593 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 1594 | .setPackage(pkg), |
| 1595 | UserHandle.of(UserHandle.getUserId(uid)), null); |
| 1596 | } |
| 1597 | } catch (SecurityException e) { |
| 1598 | Slog.w(TAG, "Can't notify app about group change", e); |
| 1599 | } |
| 1600 | } |
| 1601 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1602 | private ArrayList<ComponentName> getSuppressors() { |
| 1603 | ArrayList<ComponentName> names = new ArrayList<ComponentName>(); |
| 1604 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1605 | ArraySet<ManagedServiceInfo> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
| 1606 | |
| 1607 | for (ManagedServiceInfo info : serviceInfoList) { |
| 1608 | names.add(info.component); |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | return names; |
| 1613 | } |
| 1614 | |
| 1615 | private boolean removeDisabledHints(ManagedServiceInfo info) { |
| 1616 | return removeDisabledHints(info, 0); |
| 1617 | } |
| 1618 | |
| 1619 | private boolean removeDisabledHints(ManagedServiceInfo info, int hints) { |
| 1620 | boolean removed = false; |
| 1621 | |
| 1622 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1623 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 1624 | final ArraySet<ManagedServiceInfo> listeners = |
| 1625 | mListenersDisablingEffects.valueAt(i); |
| 1626 | |
| 1627 | if (hints == 0 || (hint & hints) == hint) { |
| 1628 | removed = removed || listeners.remove(info); |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | return removed; |
| 1633 | } |
| 1634 | |
| 1635 | private void addDisabledHints(ManagedServiceInfo info, int hints) { |
| 1636 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1637 | addDisabledHint(info, HINT_HOST_DISABLE_EFFECTS); |
| 1638 | } |
| 1639 | |
| 1640 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1641 | addDisabledHint(info, HINT_HOST_DISABLE_NOTIFICATION_EFFECTS); |
| 1642 | } |
| 1643 | |
| 1644 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1645 | addDisabledHint(info, HINT_HOST_DISABLE_CALL_EFFECTS); |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | private void addDisabledHint(ManagedServiceInfo info, int hint) { |
| 1650 | if (mListenersDisablingEffects.indexOfKey(hint) < 0) { |
| 1651 | mListenersDisablingEffects.put(hint, new ArraySet<ManagedServiceInfo>()); |
| 1652 | } |
| 1653 | |
| 1654 | ArraySet<ManagedServiceInfo> hintListeners = mListenersDisablingEffects.get(hint); |
| 1655 | hintListeners.add(info); |
| 1656 | } |
| 1657 | |
| 1658 | private int calculateHints() { |
| 1659 | int hints = 0; |
| 1660 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1661 | int hint = mListenersDisablingEffects.keyAt(i); |
| 1662 | ArraySet<ManagedServiceInfo> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
| 1663 | |
| 1664 | if (!serviceInfoList.isEmpty()) { |
| 1665 | hints |= hint; |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | return hints; |
| 1670 | } |
| 1671 | |
| 1672 | private long calculateSuppressedEffects() { |
| 1673 | int hints = calculateHints(); |
| 1674 | long suppressedEffects = 0; |
| 1675 | |
| 1676 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1677 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_ALL; |
| 1678 | } |
| 1679 | |
| 1680 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1681 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_NOTIFICATIONS; |
| 1682 | } |
| 1683 | |
| 1684 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1685 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_CALLS; |
| 1686 | } |
| 1687 | |
| 1688 | return suppressedEffects; |
| 1689 | } |
| 1690 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1691 | @GuardedBy("mNotificationLock") |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1692 | private void updateInterruptionFilterLocked() { |
| 1693 | int interruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
| 1694 | if (interruptionFilter == mInterruptionFilter) return; |
| 1695 | mInterruptionFilter = interruptionFilter; |
| 1696 | scheduleInterruptionFilterChanged(interruptionFilter); |
| 1697 | } |
| 1698 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1699 | @VisibleForTesting |
| 1700 | INotificationManager getBinderService() { |
| 1701 | return INotificationManager.Stub.asInterface(mService); |
| 1702 | } |
| 1703 | |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 1704 | protected void reportSeen(NotificationRecord r) { |
| 1705 | final int userId = r.sbn.getUserId(); |
| 1706 | mAppUsageStats.reportEvent(r.sbn.getPackageName(), |
| 1707 | userId == UserHandle.USER_ALL ? UserHandle.USER_SYSTEM |
| 1708 | : userId, |
| 1709 | UsageEvents.Event.NOTIFICATION_SEEN); |
| 1710 | } |
| 1711 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1712 | @VisibleForTesting |
| 1713 | NotificationManagerInternal getInternalService() { |
| 1714 | return mInternalService; |
| 1715 | } |
| 1716 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1717 | private final IBinder mService = new INotificationManager.Stub() { |
| 1718 | // Toasts |
| 1719 | // ============================================================================ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1720 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1721 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1722 | public void enqueueToast(String pkg, ITransientNotification callback, int duration) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1723 | { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1724 | if (DBG) { |
| 1725 | Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback |
| 1726 | + " duration=" + duration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1727 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1728 | |
| 1729 | if (pkg == null || callback == null) { |
| 1730 | Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback); |
| 1731 | return ; |
| 1732 | } |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1733 | final boolean isSystemToast = isCallerSystemOrPhone() || ("android".equals(pkg)); |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 1734 | final boolean isPackageSuspended = |
| 1735 | isPackageSuspendedForUser(pkg, Binder.getCallingUid()); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1736 | |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 1737 | if (ENABLE_BLOCKED_TOASTS && !isSystemToast && |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1738 | (!areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()) |
| 1739 | || isPackageSuspended)) { |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 1740 | Slog.e(TAG, "Suppressing toast from package " + pkg |
| 1741 | + (isPackageSuspended |
| 1742 | ? " due to package suspended by administrator." |
| 1743 | : " by user request.")); |
| 1744 | return; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | synchronized (mToastQueue) { |
| 1748 | int callingPid = Binder.getCallingPid(); |
| 1749 | long callingId = Binder.clearCallingIdentity(); |
| 1750 | try { |
| 1751 | ToastRecord record; |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 1752 | int index; |
| 1753 | // All packages aside from the android package can enqueue one toast at a time |
| 1754 | if (!isSystemToast) { |
| 1755 | index = indexOfToastPackageLocked(pkg); |
| 1756 | } else { |
| 1757 | index = indexOfToastLocked(pkg, callback); |
| 1758 | } |
| 1759 | |
| 1760 | // If the package already has a toast, we update its toast |
| 1761 | // 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] | 1762 | if (index >= 0) { |
| 1763 | record = mToastQueue.get(index); |
| 1764 | record.update(duration); |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 1765 | record.update(callback); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1766 | } else { |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1767 | Binder token = new Binder(); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 1768 | mWindowManagerInternal.addWindowToken(token, TYPE_TOAST, DEFAULT_DISPLAY); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1769 | record = new ToastRecord(callingPid, pkg, callback, duration, token); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1770 | mToastQueue.add(record); |
| 1771 | index = mToastQueue.size() - 1; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1772 | } |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 1773 | keepProcessAliveIfNeededLocked(callingPid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1774 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 1775 | // new or just been updated. Call back and tell it to show itself. |
| 1776 | // If the callback fails, this will remove it from the list, so don't |
| 1777 | // assume that it's valid after this. |
| 1778 | if (index == 0) { |
| 1779 | showNextToastLocked(); |
| 1780 | } |
| 1781 | } finally { |
| 1782 | Binder.restoreCallingIdentity(callingId); |
| 1783 | } |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | @Override |
| 1788 | public void cancelToast(String pkg, ITransientNotification callback) { |
| 1789 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| 1790 | |
| 1791 | if (pkg == null || callback == null) { |
| 1792 | Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback); |
| 1793 | return ; |
| 1794 | } |
| 1795 | |
| 1796 | synchronized (mToastQueue) { |
| 1797 | long callingId = Binder.clearCallingIdentity(); |
| 1798 | try { |
| 1799 | int index = indexOfToastLocked(pkg, callback); |
| 1800 | if (index >= 0) { |
| 1801 | cancelToastLocked(index); |
| 1802 | } else { |
| 1803 | Slog.w(TAG, "Toast already cancelled. pkg=" + pkg |
| 1804 | + " callback=" + callback); |
| 1805 | } |
| 1806 | } finally { |
| 1807 | Binder.restoreCallingIdentity(callingId); |
| 1808 | } |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 1813 | public void enqueueNotificationWithTag(String pkg, String opPkg, String tag, int id, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1814 | Notification notification, int userId) throws RemoteException { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 1815 | enqueueNotificationInternal(pkg, opPkg, Binder.getCallingUid(), |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1816 | Binder.getCallingPid(), tag, id, notification, userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | @Override |
| 1820 | public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1821 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1822 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1823 | Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 1824 | // Don't allow client applications to cancel foreground service notis or autobundled |
| 1825 | // summaries. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1826 | final int mustNotHaveFlags = isCallingUidSystem() ? 0 : |
| 1827 | (Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_AUTOGROUP_SUMMARY); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1828 | cancelNotification(Binder.getCallingUid(), Binder.getCallingPid(), pkg, tag, id, 0, |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1829 | mustNotHaveFlags, false, userId, REASON_APP_CANCEL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | @Override |
| 1833 | public void cancelAllNotifications(String pkg, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1834 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1835 | |
| 1836 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1837 | Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg); |
| 1838 | |
| 1839 | // Calling from user space, don't allow the canceling of actively |
| 1840 | // running foreground services. |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1841 | cancelAllNotificationsInt(Binder.getCallingUid(), Binder.getCallingPid(), |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1842 | pkg, null, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1843 | REASON_APP_CANCEL_ALL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | @Override |
| 1847 | public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1848 | checkCallerIsSystem(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1849 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1850 | mRankingHelper.setEnabled(pkg, uid, enabled); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1851 | // 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] | 1852 | if (!enabled) { |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1853 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, null, 0, 0, true, |
| 1854 | UserHandle.getUserId(uid), REASON_PACKAGE_BANNED, null); |
| 1855 | } |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1856 | savePolicyFile(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | /** |
| 1860 | * Use this when you just want to know if notifications are OK for this package. |
| 1861 | */ |
| 1862 | @Override |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1863 | public boolean areNotificationsEnabled(String pkg) { |
| 1864 | return areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()); |
| 1865 | } |
| 1866 | |
| 1867 | /** |
| 1868 | * Use this when you just want to know if notifications are OK for this package. |
| 1869 | */ |
| 1870 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1871 | public boolean areNotificationsEnabledForPackage(String pkg, int uid) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1872 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1873 | |
| 1874 | return mRankingHelper.getImportance(pkg, uid) != IMPORTANCE_NONE; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1875 | } |
| 1876 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1877 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1878 | public int getPackageImportance(String pkg) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1879 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1880 | return mRankingHelper.getImportance(pkg, Binder.getCallingUid()); |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | @Override |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1884 | public boolean canShowBadge(String pkg, int uid) { |
| 1885 | checkCallerIsSystem(); |
| 1886 | return mRankingHelper.canShowBadge(pkg, uid); |
| 1887 | } |
| 1888 | |
| 1889 | @Override |
| 1890 | public void setShowBadge(String pkg, int uid, boolean showBadge) { |
| 1891 | checkCallerIsSystem(); |
| 1892 | mRankingHelper.setShowBadge(pkg, uid, showBadge); |
| 1893 | savePolicyFile(); |
| 1894 | } |
| 1895 | |
| 1896 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1897 | public void updateNotificationChannelGroupForPackage(String pkg, int uid, |
| 1898 | NotificationChannelGroup group) throws RemoteException { |
| 1899 | enforceSystemOrSystemUI("Caller not system or systemui"); |
| 1900 | createNotificationChannelGroup(pkg, uid, group, false, false); |
| 1901 | savePolicyFile(); |
| 1902 | } |
| 1903 | |
| 1904 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1905 | public void createNotificationChannelGroups(String pkg, |
| 1906 | ParceledListSlice channelGroupList) throws RemoteException { |
| 1907 | checkCallerIsSystemOrSameApp(pkg); |
| 1908 | List<NotificationChannelGroup> groups = channelGroupList.getList(); |
| 1909 | final int groupSize = groups.size(); |
| 1910 | for (int i = 0; i < groupSize; i++) { |
| 1911 | final NotificationChannelGroup group = groups.get(i); |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 1912 | createNotificationChannelGroup(pkg, Binder.getCallingUid(), group, true, false); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1913 | } |
| 1914 | savePolicyFile(); |
| 1915 | } |
| 1916 | |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 1917 | private void createNotificationChannelsImpl(String pkg, int uid, |
| 1918 | ParceledListSlice channelsList) { |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1919 | List<NotificationChannel> channels = channelsList.getList(); |
| 1920 | final int channelsSize = channels.size(); |
| 1921 | for (int i = 0; i < channelsSize; i++) { |
| 1922 | final NotificationChannel channel = channels.get(i); |
| 1923 | Preconditions.checkNotNull(channel, "channel in list is null"); |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 1924 | mRankingHelper.createNotificationChannel(pkg, uid, channel, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1925 | true /* fromTargetApp */); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1926 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1927 | UserHandle.getUserHandleForUid(uid), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1928 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), false), |
| 1929 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1930 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1931 | savePolicyFile(); |
| 1932 | } |
| 1933 | |
| 1934 | @Override |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 1935 | public void createNotificationChannels(String pkg, |
| 1936 | ParceledListSlice channelsList) throws RemoteException { |
| 1937 | checkCallerIsSystemOrSameApp(pkg); |
| 1938 | createNotificationChannelsImpl(pkg, Binder.getCallingUid(), channelsList); |
| 1939 | } |
| 1940 | |
| 1941 | @Override |
| 1942 | public void createNotificationChannelsForPackage(String pkg, int uid, |
| 1943 | ParceledListSlice channelsList) throws RemoteException { |
| 1944 | checkCallerIsSystem(); |
| 1945 | createNotificationChannelsImpl(pkg, uid, channelsList); |
| 1946 | } |
| 1947 | |
| 1948 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1949 | public NotificationChannel getNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1950 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1951 | return mRankingHelper.getNotificationChannel( |
| 1952 | pkg, Binder.getCallingUid(), channelId, false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | @Override |
| 1956 | public NotificationChannel getNotificationChannelForPackage(String pkg, int uid, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1957 | String channelId, boolean includeDeleted) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1958 | checkCallerIsSystem(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1959 | return mRankingHelper.getNotificationChannel(pkg, uid, channelId, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | @Override |
| 1963 | public void deleteNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1964 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1965 | final int callingUid = Binder.getCallingUid(); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1966 | if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 1967 | throw new IllegalArgumentException("Cannot delete default channel"); |
| 1968 | } |
| 1969 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channelId, 0, 0, true, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1970 | UserHandle.getUserId(callingUid), REASON_CHANNEL_BANNED, null); |
| 1971 | mRankingHelper.deleteNotificationChannel(pkg, callingUid, channelId); |
| 1972 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1973 | UserHandle.getUserHandleForUid(callingUid), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1974 | mRankingHelper.getNotificationChannel(pkg, callingUid, channelId, true), |
| 1975 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1976 | savePolicyFile(); |
| 1977 | } |
| 1978 | |
| 1979 | @Override |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1980 | public NotificationChannelGroup getNotificationChannelGroup(String pkg, String groupId) { |
| 1981 | checkCallerIsSystemOrSameApp(pkg); |
| 1982 | return mRankingHelper.getNotificationChannelGroupWithChannels( |
| 1983 | pkg, Binder.getCallingUid(), groupId, false); |
| 1984 | } |
| 1985 | |
| 1986 | @Override |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1987 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroups( |
| 1988 | String pkg) { |
| 1989 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 1990 | return mRankingHelper.getNotificationChannelGroups( |
| 1991 | pkg, Binder.getCallingUid(), false, false); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1995 | public void deleteNotificationChannelGroup(String pkg, String groupId) { |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1996 | checkCallerIsSystemOrSameApp(pkg); |
| 1997 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1998 | final int callingUid = Binder.getCallingUid(); |
| 1999 | NotificationChannelGroup groupToDelete = |
| 2000 | mRankingHelper.getNotificationChannelGroup(groupId, pkg, callingUid); |
| 2001 | if (groupToDelete != null) { |
| 2002 | List<NotificationChannel> deletedChannels = |
| 2003 | mRankingHelper.deleteNotificationChannelGroup(pkg, callingUid, groupId); |
| 2004 | for (int i = 0; i < deletedChannels.size(); i++) { |
| 2005 | final NotificationChannel deletedChannel = deletedChannels.get(i); |
| 2006 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, deletedChannel.getId(), 0, 0, |
| 2007 | true, |
| 2008 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, |
| 2009 | null); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2010 | mListeners.notifyNotificationChannelChanged(pkg, |
| 2011 | UserHandle.getUserHandleForUid(callingUid), |
| 2012 | deletedChannel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2013 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
| 2014 | } |
| 2015 | mListeners.notifyNotificationChannelGroupChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2016 | pkg, UserHandle.getUserHandleForUid(callingUid), groupToDelete, |
| 2017 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2018 | savePolicyFile(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2019 | } |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2023 | public void updateNotificationChannelForPackage(String pkg, int uid, |
| 2024 | NotificationChannel channel) { |
Geoffrey Pitsch | 4dd5006 | 2016-12-06 16:41:22 -0500 | [diff] [blame] | 2025 | enforceSystemOrSystemUI("Caller not system or systemui"); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2026 | Preconditions.checkNotNull(channel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2027 | updateNotificationChannelInt(pkg, uid, channel, false); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | @Override |
| 2031 | public ParceledListSlice<NotificationChannel> getNotificationChannelsForPackage(String pkg, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2032 | int uid, boolean includeDeleted) { |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2033 | enforceSystemOrSystemUI("getNotificationChannelsForPackage"); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2034 | return mRankingHelper.getNotificationChannels(pkg, uid, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2038 | public int getNumNotificationChannelsForPackage(String pkg, int uid, |
| 2039 | boolean includeDeleted) { |
| 2040 | enforceSystemOrSystemUI("getNumNotificationChannelsForPackage"); |
| 2041 | return mRankingHelper.getNotificationChannels(pkg, uid, includeDeleted) |
| 2042 | .getList().size(); |
| 2043 | } |
| 2044 | |
| 2045 | @Override |
Julia Reynolds | 17717f5 | 2017-05-09 11:46:06 -0400 | [diff] [blame] | 2046 | public boolean onlyHasDefaultChannel(String pkg, int uid) { |
| 2047 | enforceSystemOrSystemUI("onlyHasDefaultChannel"); |
| 2048 | return mRankingHelper.onlyHasDefaultChannel(pkg, uid); |
| 2049 | } |
| 2050 | |
| 2051 | @Override |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 2052 | public int getDeletedChannelCount(String pkg, int uid) { |
| 2053 | enforceSystemOrSystemUI("getDeletedChannelCount"); |
| 2054 | return mRankingHelper.getDeletedChannelCount(pkg, uid); |
| 2055 | } |
| 2056 | |
| 2057 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2058 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroupsForPackage( |
| 2059 | String pkg, int uid, boolean includeDeleted) { |
| 2060 | checkCallerIsSystem(); |
Julia Reynolds | 3eb3ffd | 2017-11-16 10:11:32 -0500 | [diff] [blame] | 2061 | return mRankingHelper.getNotificationChannelGroups(pkg, uid, includeDeleted, true); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 2065 | public NotificationChannelGroup getPopulatedNotificationChannelGroupForPackage( |
| 2066 | String pkg, int uid, String groupId, boolean includeDeleted) { |
| 2067 | enforceSystemOrSystemUI("getPopulatedNotificationChannelGroupForPackage"); |
| 2068 | return mRankingHelper.getNotificationChannelGroupWithChannels( |
| 2069 | pkg, uid, groupId, includeDeleted); |
| 2070 | } |
| 2071 | |
| 2072 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 2073 | public NotificationChannelGroup getNotificationChannelGroupForPackage( |
| 2074 | String groupId, String pkg, int uid) { |
| 2075 | enforceSystemOrSystemUI("getNotificationChannelGroupForPackage"); |
| 2076 | return mRankingHelper.getNotificationChannelGroup(groupId, pkg, uid); |
| 2077 | } |
| 2078 | |
| 2079 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2080 | public ParceledListSlice<NotificationChannel> getNotificationChannels(String pkg) { |
| 2081 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2082 | return mRankingHelper.getNotificationChannels( |
| 2083 | pkg, Binder.getCallingUid(), false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 2084 | } |
| 2085 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2086 | @Override |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2087 | public void clearData(String packageName, int uid, boolean fromApp) throws RemoteException { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2088 | checkCallerIsSystem(); |
| 2089 | |
| 2090 | // Cancel posted notifications |
| 2091 | cancelAllNotificationsInt(MY_UID, MY_PID, packageName, null, 0, 0, true, |
| 2092 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, null); |
| 2093 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2094 | final String[] packages = new String[] {packageName}; |
| 2095 | final int[] uids = new int[] {uid}; |
| 2096 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2097 | // Listener & assistant |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2098 | mListeners.onPackagesChanged(true, packages, uids); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2099 | mAssistants.onPackagesChanged(true, packages, uids); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2100 | |
| 2101 | // Zen |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2102 | mConditionProviders.onPackagesChanged(true, packages, uids); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2103 | |
| 2104 | // Reset notification preferences |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2105 | if (!fromApp) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2106 | mRankingHelper.onPackagesChanged( |
| 2107 | true, UserHandle.getCallingUserId(), packages, uids); |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 2108 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 2109 | |
| 2110 | savePolicyFile(); |
| 2111 | } |
| 2112 | |
| 2113 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2114 | /** |
| 2115 | * System-only API for getting a list of current (i.e. not cleared) notifications. |
| 2116 | * |
| 2117 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2118 | * @returns A list of all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2119 | */ |
| 2120 | @Override |
| 2121 | public StatusBarNotification[] getActiveNotifications(String callingPkg) { |
| 2122 | // enforce() will ensure the calling uid has the correct permission |
| 2123 | getContext().enforceCallingOrSelfPermission( |
| 2124 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 2125 | "NotificationManagerService.getActiveNotifications"); |
| 2126 | |
| 2127 | StatusBarNotification[] tmp = null; |
| 2128 | int uid = Binder.getCallingUid(); |
| 2129 | |
| 2130 | // noteOp will check to make sure the callingPkg matches the uid |
| 2131 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 2132 | == AppOpsManager.MODE_ALLOWED) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2133 | synchronized (mNotificationLock) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2134 | tmp = new StatusBarNotification[mNotificationList.size()]; |
| 2135 | final int N = mNotificationList.size(); |
| 2136 | for (int i=0; i<N; i++) { |
| 2137 | tmp[i] = mNotificationList.get(i).sbn; |
| 2138 | } |
| 2139 | } |
| 2140 | } |
| 2141 | return tmp; |
| 2142 | } |
| 2143 | |
| 2144 | /** |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2145 | * Public API for getting a list of current notifications for the calling package/uid. |
| 2146 | * |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 2147 | * Note that since notification posting is done asynchronously, this will not return |
| 2148 | * notifications that are in the process of being posted. |
| 2149 | * |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2150 | * @returns A list of all the package's notifications, in natural order. |
| 2151 | */ |
| 2152 | @Override |
| 2153 | public ParceledListSlice<StatusBarNotification> getAppActiveNotifications(String pkg, |
| 2154 | int incomingUserId) { |
| 2155 | checkCallerIsSystemOrSameApp(pkg); |
| 2156 | int userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2157 | Binder.getCallingUid(), incomingUserId, true, false, |
| 2158 | "getAppActiveNotifications", pkg); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2159 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2160 | final ArrayMap<String, StatusBarNotification> map |
| 2161 | = new ArrayMap<>(mNotificationList.size() + mEnqueuedNotifications.size()); |
Erik Wolsheimer | 2242b4d | 2015-11-24 13:22:04 -0800 | [diff] [blame] | 2162 | final int N = mNotificationList.size(); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2163 | for (int i = 0; i < N; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2164 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 2165 | mNotificationList.get(i).sbn); |
| 2166 | if (sbn != null) { |
| 2167 | map.put(sbn.getKey(), sbn); |
| 2168 | } |
| 2169 | } |
| 2170 | for(NotificationRecord snoozed: mSnoozeHelper.getSnoozed(userId, pkg)) { |
| 2171 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, snoozed.sbn); |
| 2172 | if (sbn != null) { |
| 2173 | map.put(sbn.getKey(), sbn); |
| 2174 | } |
| 2175 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2176 | final int M = mEnqueuedNotifications.size(); |
| 2177 | for (int i = 0; i < M; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2178 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 2179 | mEnqueuedNotifications.get(i).sbn); |
| 2180 | if (sbn != null) { |
| 2181 | map.put(sbn.getKey(), sbn); // pending update overwrites existing post here |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2182 | } |
| 2183 | } |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2184 | final ArrayList<StatusBarNotification> list = new ArrayList<>(map.size()); |
| 2185 | list.addAll(map.values()); |
| 2186 | return new ParceledListSlice<StatusBarNotification>(list); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2187 | } |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2188 | } |
| 2189 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2190 | private StatusBarNotification sanitizeSbn(String pkg, int userId, |
| 2191 | StatusBarNotification sbn) { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 2192 | if (sbn.getPackageName().equals(pkg) && sbn.getUserId() == userId) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2193 | // We could pass back a cloneLight() but clients might get confused and |
| 2194 | // try to send this thing back to notify() again, which would not work |
| 2195 | // very well. |
| 2196 | return new StatusBarNotification( |
| 2197 | sbn.getPackageName(), |
| 2198 | sbn.getOpPkg(), |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2199 | sbn.getId(), sbn.getTag(), sbn.getUid(), sbn.getInitialPid(), |
| 2200 | sbn.getNotification().clone(), |
| 2201 | sbn.getUser(), sbn.getOverrideGroupKey(), sbn.getPostTime()); |
| 2202 | } |
| 2203 | return null; |
| 2204 | } |
| 2205 | |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2206 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2207 | * System-only API for getting a list of recent (cleared, no longer shown) notifications. |
| 2208 | * |
| 2209 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
| 2210 | */ |
| 2211 | @Override |
| 2212 | public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) { |
| 2213 | // enforce() will ensure the calling uid has the correct permission |
| 2214 | getContext().enforceCallingOrSelfPermission( |
| 2215 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 2216 | "NotificationManagerService.getHistoricalNotifications"); |
| 2217 | |
| 2218 | StatusBarNotification[] tmp = null; |
| 2219 | int uid = Binder.getCallingUid(); |
| 2220 | |
| 2221 | // noteOp will check to make sure the callingPkg matches the uid |
| 2222 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 2223 | == AppOpsManager.MODE_ALLOWED) { |
| 2224 | synchronized (mArchive) { |
| 2225 | tmp = mArchive.getArray(count); |
| 2226 | } |
| 2227 | } |
| 2228 | return tmp; |
| 2229 | } |
| 2230 | |
| 2231 | /** |
| 2232 | * Register a listener binder directly with the notification manager. |
| 2233 | * |
| 2234 | * Only works with system callers. Apps should extend |
| 2235 | * {@link android.service.notification.NotificationListenerService}. |
| 2236 | */ |
| 2237 | @Override |
| 2238 | public void registerListener(final INotificationListener listener, |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 2239 | final ComponentName component, final int userid) { |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 2240 | enforceSystemOrSystemUI("INotificationManager.registerListener"); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 2241 | mListeners.registerService(listener, component, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | /** |
| 2245 | * Remove a listener binder directly |
| 2246 | */ |
| 2247 | @Override |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 2248 | public void unregisterListener(INotificationListener token, int userid) { |
Chris Wren | b7c8109 | 2016-03-10 11:41:10 -0500 | [diff] [blame] | 2249 | mListeners.unregisterService(token, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | /** |
| 2253 | * Allow an INotificationListener to simulate a "clear all" operation. |
| 2254 | * |
| 2255 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications} |
| 2256 | * |
| 2257 | * @param token The binder for the listener, to check that the caller is allowed |
| 2258 | */ |
| 2259 | @Override |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2260 | public void cancelNotificationsFromListener(INotificationListener token, String[] keys) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2261 | final int callingUid = Binder.getCallingUid(); |
| 2262 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2263 | long identity = Binder.clearCallingIdentity(); |
| 2264 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2265 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2266 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2267 | if (keys != null) { |
| 2268 | final int N = keys.length; |
| 2269 | for (int i = 0; i < N; i++) { |
| 2270 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 2271 | if (r == null) continue; |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2272 | final int userId = r.sbn.getUserId(); |
| 2273 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 2274 | !mUserProfiles.isCurrentProfile(userId)) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2275 | throw new SecurityException("Disallowed call from listener: " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2276 | + info.service); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2277 | } |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 2278 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2279 | r.sbn.getPackageName(), r.sbn.getTag(), r.sbn.getId(), |
| 2280 | userId); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2281 | } |
| 2282 | } else { |
| 2283 | cancelAllLocked(callingUid, callingPid, info.userid, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2284 | REASON_LISTENER_CANCEL_ALL, info, info.supportsProfiles()); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2285 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2286 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2287 | } finally { |
| 2288 | Binder.restoreCallingIdentity(identity); |
| 2289 | } |
| 2290 | } |
| 2291 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2292 | /** |
| 2293 | * Handle request from an approved listener to re-enable itself. |
| 2294 | * |
| 2295 | * @param component The componenet to be re-enabled, caller must match package. |
| 2296 | */ |
| 2297 | @Override |
| 2298 | public void requestBindListener(ComponentName component) { |
| 2299 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 2300 | long identity = Binder.clearCallingIdentity(); |
| 2301 | try { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2302 | ManagedServices manager = |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2303 | mAssistants.isComponentEnabledForCurrentProfiles(component) |
| 2304 | ? mAssistants |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2305 | : mListeners; |
| 2306 | manager.setComponentState(component, true); |
| 2307 | } finally { |
| 2308 | Binder.restoreCallingIdentity(identity); |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | @Override |
| 2313 | public void requestUnbindListener(INotificationListener token) { |
| 2314 | long identity = Binder.clearCallingIdentity(); |
| 2315 | try { |
| 2316 | // allow bound services to disable themselves |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2317 | synchronized (mNotificationLock) { |
| 2318 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2319 | info.getOwner().setComponentState(info.component, false); |
| 2320 | } |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2321 | } finally { |
| 2322 | Binder.restoreCallingIdentity(identity); |
| 2323 | } |
| 2324 | } |
| 2325 | |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2326 | @Override |
| 2327 | public void setNotificationsShownFromListener(INotificationListener token, String[] keys) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2328 | long identity = Binder.clearCallingIdentity(); |
| 2329 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2330 | synchronized (mNotificationLock) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2331 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2332 | if (keys != null) { |
| 2333 | final int N = keys.length; |
| 2334 | for (int i = 0; i < N; i++) { |
| 2335 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
| 2336 | if (r == null) continue; |
| 2337 | final int userId = r.sbn.getUserId(); |
| 2338 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
| 2339 | !mUserProfiles.isCurrentProfile(userId)) { |
| 2340 | throw new SecurityException("Disallowed call from listener: " |
| 2341 | + info.service); |
| 2342 | } |
| 2343 | if (!r.isSeen()) { |
| 2344 | if (DBG) Slog.d(TAG, "Marking notification as seen " + keys[i]); |
Amith Yamasani | 803eab69 | 2017-11-09 17:47:04 -0800 | [diff] [blame] | 2345 | reportSeen(r); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2346 | r.setSeen(); |
| 2347 | } |
| 2348 | } |
| 2349 | } |
| 2350 | } |
| 2351 | } finally { |
| 2352 | Binder.restoreCallingIdentity(identity); |
| 2353 | } |
| 2354 | } |
| 2355 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2356 | /** |
| 2357 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2358 | * |
| 2359 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2360 | * |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2361 | * @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] | 2362 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2363 | @GuardedBy("mNotificationLock") |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2364 | private void cancelNotificationFromListenerLocked(ManagedServiceInfo info, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2365 | int callingUid, int callingPid, String pkg, String tag, int id, int userId) { |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2366 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
| 2367 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
| 2368 | true, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2369 | userId, REASON_LISTENER_CANCEL, info); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2370 | } |
| 2371 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2372 | /** |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2373 | * Allow an INotificationListener to snooze a single notification until a context. |
| 2374 | * |
| 2375 | * @param token The binder for the listener, to check that the caller is allowed |
| 2376 | */ |
| 2377 | @Override |
| 2378 | public void snoozeNotificationUntilContextFromListener(INotificationListener token, |
| 2379 | String key, String snoozeCriterionId) { |
| 2380 | long identity = Binder.clearCallingIdentity(); |
| 2381 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2382 | synchronized (mNotificationLock) { |
| 2383 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2384 | snoozeNotificationInt(key, SNOOZE_UNTIL_UNSPECIFIED, snoozeCriterionId, info); |
| 2385 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2386 | } finally { |
| 2387 | Binder.restoreCallingIdentity(identity); |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | /** |
| 2392 | * Allow an INotificationListener to snooze a single notification until a time. |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2393 | * |
| 2394 | * @param token The binder for the listener, to check that the caller is allowed |
| 2395 | */ |
| 2396 | @Override |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2397 | public void snoozeNotificationUntilFromListener(INotificationListener token, String key, |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 2398 | long duration) { |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2399 | long identity = Binder.clearCallingIdentity(); |
| 2400 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2401 | synchronized (mNotificationLock) { |
| 2402 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2403 | snoozeNotificationInt(key, duration, null, info); |
| 2404 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2405 | } finally { |
| 2406 | Binder.restoreCallingIdentity(identity); |
| 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | /** |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2411 | * Allows the notification assistant to un-snooze a single notification. |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2412 | * |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2413 | * @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] | 2414 | */ |
| 2415 | @Override |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2416 | public void unsnoozeNotificationFromAssistant(INotificationListener token, String key) { |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2417 | long identity = Binder.clearCallingIdentity(); |
| 2418 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2419 | synchronized (mNotificationLock) { |
| 2420 | final ManagedServiceInfo info = |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2421 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2422 | unsnoozeNotificationInt(key, info); |
| 2423 | } |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2424 | } finally { |
| 2425 | Binder.restoreCallingIdentity(identity); |
| 2426 | } |
| 2427 | } |
| 2428 | |
| 2429 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2430 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2431 | * |
| 2432 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2433 | * |
| 2434 | * @param token The binder for the listener, to check that the caller is allowed |
| 2435 | */ |
| 2436 | @Override |
| 2437 | public void cancelNotificationFromListener(INotificationListener token, String pkg, |
| 2438 | String tag, int id) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2439 | final int callingUid = Binder.getCallingUid(); |
| 2440 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2441 | long identity = Binder.clearCallingIdentity(); |
| 2442 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2443 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2444 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2445 | if (info.supportsProfiles()) { |
| 2446 | Log.e(TAG, "Ignoring deprecated cancelNotification(pkg, tag, id) " |
| 2447 | + "from " + info.component |
| 2448 | + " use cancelNotification(key) instead."); |
| 2449 | } else { |
| 2450 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2451 | pkg, tag, id, info.userid); |
| 2452 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2453 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2454 | } finally { |
| 2455 | Binder.restoreCallingIdentity(identity); |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | /** |
| 2460 | * Allow an INotificationListener to request the list of outstanding notifications seen by |
| 2461 | * the current user. Useful when starting up, after which point the listener callbacks |
| 2462 | * should be used. |
| 2463 | * |
| 2464 | * @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] | 2465 | * @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] | 2466 | * @returns The return value will contain the notifications specified in keys, in that |
| 2467 | * order, or if keys is null, all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2468 | */ |
| 2469 | @Override |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2470 | public ParceledListSlice<StatusBarNotification> getActiveNotificationsFromListener( |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2471 | INotificationListener token, String[] keys, int trim) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2472 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2473 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2474 | final boolean getKeys = keys != null; |
| 2475 | final int N = getKeys ? keys.length : mNotificationList.size(); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2476 | final ArrayList<StatusBarNotification> list |
| 2477 | = new ArrayList<StatusBarNotification>(N); |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2478 | for (int i=0; i<N; i++) { |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2479 | final NotificationRecord r = getKeys |
| 2480 | ? mNotificationsByKey.get(keys[i]) |
| 2481 | : mNotificationList.get(i); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2482 | if (r == null) continue; |
| 2483 | StatusBarNotification sbn = r.sbn; |
| 2484 | if (!isVisibleToListener(sbn, info)) continue; |
| 2485 | StatusBarNotification sbnToSend = |
| 2486 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2487 | list.add(sbnToSend); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2488 | } |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2489 | return new ParceledListSlice<StatusBarNotification>(list); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2490 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2491 | } |
| 2492 | |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2493 | /** |
| 2494 | * Allow an INotificationListener to request the list of outstanding snoozed notifications |
| 2495 | * seen by the current user. Useful when starting up, after which point the listener |
| 2496 | * callbacks should be used. |
| 2497 | * |
| 2498 | * @param token The binder for the listener, to check that the caller is allowed |
| 2499 | * @returns The return value will contain the notifications specified in keys, in that |
| 2500 | * order, or if keys is null, all the notifications, in natural order. |
| 2501 | */ |
| 2502 | @Override |
| 2503 | public ParceledListSlice<StatusBarNotification> getSnoozedNotificationsFromListener( |
| 2504 | INotificationListener token, int trim) { |
| 2505 | synchronized (mNotificationLock) { |
| 2506 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2507 | List<NotificationRecord> snoozedRecords = mSnoozeHelper.getSnoozed(); |
| 2508 | final int N = snoozedRecords.size(); |
| 2509 | final ArrayList<StatusBarNotification> list = new ArrayList<>(N); |
| 2510 | for (int i=0; i < N; i++) { |
| 2511 | final NotificationRecord r = snoozedRecords.get(i); |
| 2512 | if (r == null) continue; |
| 2513 | StatusBarNotification sbn = r.sbn; |
| 2514 | if (!isVisibleToListener(sbn, info)) continue; |
| 2515 | StatusBarNotification sbnToSend = |
| 2516 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2517 | list.add(sbnToSend); |
| 2518 | } |
| 2519 | return new ParceledListSlice<>(list); |
| 2520 | } |
| 2521 | } |
| 2522 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2523 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2524 | public void requestHintsFromListener(INotificationListener token, int hints) { |
| 2525 | final long identity = Binder.clearCallingIdentity(); |
| 2526 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2527 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2528 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2529 | final int disableEffectsMask = HINT_HOST_DISABLE_EFFECTS |
| 2530 | | HINT_HOST_DISABLE_NOTIFICATION_EFFECTS |
| 2531 | | HINT_HOST_DISABLE_CALL_EFFECTS; |
| 2532 | final boolean disableEffects = (hints & disableEffectsMask) != 0; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2533 | if (disableEffects) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2534 | addDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2535 | } else { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2536 | removeDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2537 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2538 | updateListenerHintsLocked(); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2539 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2540 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2541 | } finally { |
| 2542 | Binder.restoreCallingIdentity(identity); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2547 | public int getHintsFromListener(INotificationListener token) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2548 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2549 | return mListenerHints; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2550 | } |
| 2551 | } |
| 2552 | |
| 2553 | @Override |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2554 | public void requestInterruptionFilterFromListener(INotificationListener token, |
| 2555 | int interruptionFilter) throws RemoteException { |
| 2556 | final long identity = Binder.clearCallingIdentity(); |
| 2557 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2558 | synchronized (mNotificationLock) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2559 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2560 | mZenModeHelper.requestFromListener(info.component, interruptionFilter); |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2561 | updateInterruptionFilterLocked(); |
| 2562 | } |
| 2563 | } finally { |
| 2564 | Binder.restoreCallingIdentity(identity); |
| 2565 | } |
| 2566 | } |
| 2567 | |
| 2568 | @Override |
| 2569 | public int getInterruptionFilterFromListener(INotificationListener token) |
| 2570 | throws RemoteException { |
| 2571 | synchronized (mNotificationLight) { |
| 2572 | return mInterruptionFilter; |
| 2573 | } |
| 2574 | } |
| 2575 | |
| 2576 | @Override |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2577 | public void setOnNotificationPostedTrimFromListener(INotificationListener token, int trim) |
| 2578 | throws RemoteException { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2579 | synchronized (mNotificationLock) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2580 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2581 | if (info == null) return; |
| 2582 | mListeners.setOnNotificationPostedTrimLocked(info, trim); |
| 2583 | } |
| 2584 | } |
| 2585 | |
| 2586 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2587 | public int getZenMode() { |
| 2588 | return mZenModeHelper.getZenMode(); |
| 2589 | } |
| 2590 | |
| 2591 | @Override |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 2592 | public ZenModeConfig getZenModeConfig() { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2593 | enforceSystemOrSystemUI("INotificationManager.getZenModeConfig"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 2594 | return mZenModeHelper.getConfig(); |
| 2595 | } |
| 2596 | |
| 2597 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2598 | public void setZenMode(int mode, Uri conditionId, String reason) throws RemoteException { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2599 | enforceSystemOrSystemUI("INotificationManager.setZenMode"); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2600 | final long identity = Binder.clearCallingIdentity(); |
| 2601 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 2602 | mZenModeHelper.setManualZenMode(mode, conditionId, null, reason); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2603 | } finally { |
| 2604 | Binder.restoreCallingIdentity(identity); |
| 2605 | } |
| 2606 | } |
| 2607 | |
| 2608 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2609 | public List<ZenModeConfig.ZenRule> getZenRules() throws RemoteException { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2610 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRules"); |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2611 | return mZenModeHelper.getZenRules(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2615 | public AutomaticZenRule getAutomaticZenRule(String id) throws RemoteException { |
| 2616 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2617 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRule"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2618 | return mZenModeHelper.getAutomaticZenRule(id); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2622 | public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2623 | throws RemoteException { |
| 2624 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 2625 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 2626 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 2627 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2628 | enforcePolicyAccess(Binder.getCallingUid(), "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2629 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2630 | return mZenModeHelper.addAutomaticZenRule(automaticZenRule, |
| 2631 | "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2635 | public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2636 | throws RemoteException { |
| 2637 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 2638 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 2639 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 2640 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
| 2641 | enforcePolicyAccess(Binder.getCallingUid(), "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2642 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2643 | return mZenModeHelper.updateAutomaticZenRule(id, automaticZenRule, |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2644 | "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2648 | public boolean removeAutomaticZenRule(String id) throws RemoteException { |
| 2649 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2650 | // Verify that they can modify zen rules. |
| 2651 | enforcePolicyAccess(Binder.getCallingUid(), "removeAutomaticZenRule"); |
| 2652 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2653 | return mZenModeHelper.removeAutomaticZenRule(id, "removeAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | @Override |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 2657 | public boolean removeAutomaticZenRules(String packageName) throws RemoteException { |
| 2658 | Preconditions.checkNotNull(packageName, "Package name is null"); |
| 2659 | enforceSystemOrSystemUI("removeAutomaticZenRules"); |
| 2660 | |
| 2661 | return mZenModeHelper.removeAutomaticZenRules(packageName, "removeAutomaticZenRules"); |
| 2662 | } |
| 2663 | |
| 2664 | @Override |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 2665 | public int getRuleInstanceCount(ComponentName owner) throws RemoteException { |
| 2666 | Preconditions.checkNotNull(owner, "Owner is null"); |
| 2667 | enforceSystemOrSystemUI("getRuleInstanceCount"); |
| 2668 | |
| 2669 | return mZenModeHelper.getCurrentInstanceCount(owner); |
| 2670 | } |
| 2671 | |
| 2672 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2673 | public void setInterruptionFilter(String pkg, int filter) throws RemoteException { |
| 2674 | enforcePolicyAccess(pkg, "setInterruptionFilter"); |
| 2675 | final int zen = NotificationManager.zenModeFromInterruptionFilter(filter, -1); |
| 2676 | if (zen == -1) throw new IllegalArgumentException("Invalid filter: " + filter); |
| 2677 | final long identity = Binder.clearCallingIdentity(); |
| 2678 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 2679 | mZenModeHelper.setManualZenMode(zen, null, pkg, "setInterruptionFilter"); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2680 | } finally { |
| 2681 | Binder.restoreCallingIdentity(identity); |
| 2682 | } |
| 2683 | } |
| 2684 | |
| 2685 | @Override |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 2686 | public void notifyConditions(final String pkg, IConditionProvider provider, |
| 2687 | final Condition[] conditions) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2688 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 2689 | checkCallerIsSystemOrSameApp(pkg); |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 2690 | mHandler.post(new Runnable() { |
| 2691 | @Override |
| 2692 | public void run() { |
| 2693 | mConditionProviders.notifyConditions(pkg, info, conditions); |
| 2694 | } |
| 2695 | }); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2696 | } |
| 2697 | |
Julia Reynolds | 38e6ca4 | 2016-08-08 08:38:09 -0400 | [diff] [blame] | 2698 | @Override |
| 2699 | public void requestUnbindProvider(IConditionProvider provider) { |
| 2700 | long identity = Binder.clearCallingIdentity(); |
| 2701 | try { |
| 2702 | // allow bound services to disable themselves |
| 2703 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 2704 | info.getOwner().setComponentState(info.component, false); |
| 2705 | } finally { |
| 2706 | Binder.restoreCallingIdentity(identity); |
| 2707 | } |
| 2708 | } |
| 2709 | |
| 2710 | @Override |
| 2711 | public void requestBindProvider(ComponentName component) { |
| 2712 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 2713 | long identity = Binder.clearCallingIdentity(); |
| 2714 | try { |
| 2715 | mConditionProviders.setComponentState(component, true); |
| 2716 | } finally { |
| 2717 | Binder.restoreCallingIdentity(identity); |
| 2718 | } |
| 2719 | } |
| 2720 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2721 | private void enforceSystemOrSystemUI(String message) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 2722 | if (isCallerSystemOrPhone()) return; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2723 | getContext().enforceCallingPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 2724 | message); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2725 | } |
| 2726 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 2727 | private void enforceSystemOrSystemUIOrSamePackage(String pkg, String message) { |
| 2728 | try { |
| 2729 | checkCallerIsSystemOrSameApp(pkg); |
| 2730 | } catch (SecurityException e) { |
| 2731 | getContext().enforceCallingPermission( |
| 2732 | android.Manifest.permission.STATUS_BAR_SERVICE, |
| 2733 | message); |
| 2734 | } |
| 2735 | } |
| 2736 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2737 | private void enforcePolicyAccess(int uid, String method) { |
| 2738 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 2739 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 2740 | return; |
| 2741 | } |
| 2742 | boolean accessAllowed = false; |
| 2743 | String[] packages = getContext().getPackageManager().getPackagesForUid(uid); |
| 2744 | final int packageCount = packages.length; |
| 2745 | for (int i = 0; i < packageCount; i++) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2746 | if (mConditionProviders.isPackageOrComponentAllowed( |
| 2747 | packages[i], UserHandle.getUserId(uid))) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2748 | accessAllowed = true; |
| 2749 | } |
| 2750 | } |
| 2751 | if (!accessAllowed) { |
| 2752 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 2753 | throw new SecurityException("Notification policy access denied"); |
| 2754 | } |
| 2755 | } |
| 2756 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2757 | private void enforcePolicyAccess(String pkg, String method) { |
Julia Reynolds | 6ee2617 | 2015-09-28 11:34:48 -0400 | [diff] [blame] | 2758 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 2759 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 2760 | return; |
| 2761 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 2762 | checkCallerIsSameApp(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2763 | if (!checkPolicyAccess(pkg)) { |
| 2764 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 2765 | throw new SecurityException("Notification policy access denied"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2766 | } |
| 2767 | } |
| 2768 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2769 | private boolean checkPackagePolicyAccess(String pkg) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2770 | return mConditionProviders.isPackageOrComponentAllowed( |
| 2771 | pkg, getCallingUserHandle().getIdentifier()); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2772 | } |
| 2773 | |
| 2774 | private boolean checkPolicyAccess(String pkg) { |
Julia Reynolds | 0867b3a | 2016-03-30 17:29:54 -0400 | [diff] [blame] | 2775 | try { |
| 2776 | int uid = getContext().getPackageManager().getPackageUidAsUser( |
| 2777 | pkg, UserHandle.getCallingUserId()); |
| 2778 | if (PackageManager.PERMISSION_GRANTED == ActivityManager.checkComponentPermission( |
| 2779 | android.Manifest.permission.MANAGE_NOTIFICATIONS, uid, |
| 2780 | -1, true)) { |
| 2781 | return true; |
| 2782 | } |
| 2783 | } catch (NameNotFoundException e) { |
| 2784 | return false; |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 2785 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2786 | return checkPackagePolicyAccess(pkg) || mListeners.isComponentEnabledForPackage(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2787 | } |
| 2788 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2789 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2790 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2791 | if (!DumpUtils.checkDumpAndUsageStatsPermission(getContext(), TAG, pw)) return; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2792 | final DumpFilter filter = DumpFilter.parseFromArguments(args); |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 2793 | if (filter.stats) { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2794 | dumpJson(pw, filter); |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 2795 | } else if (filter.proto) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 2796 | dumpProto(fd, filter); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2797 | } else { |
| 2798 | dumpImpl(pw, filter); |
| 2799 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2800 | } |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2801 | |
| 2802 | @Override |
| 2803 | public ComponentName getEffectsSuppressor() { |
Bryce Lee | ba3d895 | 2016-04-12 12:39:15 -0700 | [diff] [blame] | 2804 | return !mEffectsSuppressors.isEmpty() ? mEffectsSuppressors.get(0) : null; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2805 | } |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 2806 | |
| 2807 | @Override |
| 2808 | public boolean matchesCallFilter(Bundle extras) { |
| 2809 | enforceSystemOrSystemUI("INotificationManager.matchesCallFilter"); |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 2810 | return mZenModeHelper.matchesCallFilter( |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 2811 | Binder.getCallingUserHandle(), |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 2812 | extras, |
| 2813 | mRankingHelper.findExtractor(ValidateNotificationPeople.class), |
| 2814 | MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS, |
| 2815 | MATCHES_CALL_FILTER_TIMEOUT_AFFINITY); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 2816 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 2817 | |
| 2818 | @Override |
| 2819 | public boolean isSystemConditionProviderEnabled(String path) { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2820 | enforceSystemOrSystemUI("INotificationManager.isSystemConditionProviderEnabled"); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2821 | return mConditionProviders.isSystemProviderEnabled(path); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 2822 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2823 | |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 2824 | // Backup/restore interface |
| 2825 | @Override |
| 2826 | public byte[] getBackupPayload(int user) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2827 | if (DBG) Slog.d(TAG, "getBackupPayload u=" + user); |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 2828 | //TODO: http://b/22388012 |
| 2829 | if (user != UserHandle.USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2830 | Slog.w(TAG, "getBackupPayload: cannot backup policy for user " + user); |
| 2831 | return null; |
| 2832 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 2833 | synchronized(mPolicyFile) { |
| 2834 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 2835 | try { |
| 2836 | writePolicyXml(baos, true /*forBackup*/); |
| 2837 | return baos.toByteArray(); |
| 2838 | } catch (IOException e) { |
| 2839 | Slog.w(TAG, "getBackupPayload: error writing payload for user " + user, e); |
| 2840 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2841 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 2842 | return null; |
| 2843 | } |
| 2844 | |
| 2845 | @Override |
| 2846 | public void applyRestore(byte[] payload, int user) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2847 | if (DBG) Slog.d(TAG, "applyRestore u=" + user + " payload=" |
| 2848 | + (payload != null ? new String(payload, StandardCharsets.UTF_8) : null)); |
| 2849 | if (payload == null) { |
| 2850 | Slog.w(TAG, "applyRestore: no payload to restore for user " + user); |
| 2851 | return; |
| 2852 | } |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 2853 | //TODO: http://b/22388012 |
| 2854 | if (user != UserHandle.USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2855 | Slog.w(TAG, "applyRestore: cannot restore policy for user " + user); |
| 2856 | return; |
| 2857 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 2858 | synchronized(mPolicyFile) { |
| 2859 | final ByteArrayInputStream bais = new ByteArrayInputStream(payload); |
| 2860 | try { |
| 2861 | readPolicyXml(bais, true /*forRestore*/); |
| 2862 | savePolicyFile(); |
| 2863 | } catch (NumberFormatException | XmlPullParserException | IOException e) { |
| 2864 | Slog.w(TAG, "applyRestore: error reading payload", e); |
| 2865 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2866 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 2867 | } |
| 2868 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2869 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2870 | public boolean isNotificationPolicyAccessGranted(String pkg) { |
| 2871 | return checkPolicyAccess(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | @Override |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 2875 | public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {; |
| 2876 | enforceSystemOrSystemUIOrSamePackage(pkg, |
| 2877 | "request policy access status for another package"); |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 2878 | return checkPolicyAccess(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2882 | public void setNotificationPolicyAccessGranted(String pkg, boolean granted) |
| 2883 | throws RemoteException { |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2884 | setNotificationPolicyAccessGrantedForUser( |
| 2885 | pkg, getCallingUserHandle().getIdentifier(), granted); |
| 2886 | } |
| 2887 | |
| 2888 | @Override |
| 2889 | public void setNotificationPolicyAccessGrantedForUser( |
| 2890 | String pkg, int userId, boolean granted) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2891 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 2892 | final long identity = Binder.clearCallingIdentity(); |
| 2893 | try { |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 2894 | if (canUseManagedServices()) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 2895 | mConditionProviders.setPackageOrComponentEnabled( |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2896 | pkg, userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2897 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 2898 | getContext().sendBroadcastAsUser(new Intent( |
| 2899 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 2900 | .setPackage(pkg) |
| 2901 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2902 | UserHandle.of(userId), null); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 2903 | savePolicyFile(); |
| 2904 | } |
| 2905 | } finally { |
| 2906 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2907 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2908 | } |
| 2909 | |
| 2910 | @Override |
| 2911 | public Policy getNotificationPolicy(String pkg) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2912 | final long identity = Binder.clearCallingIdentity(); |
| 2913 | try { |
| 2914 | return mZenModeHelper.getNotificationPolicy(); |
| 2915 | } finally { |
| 2916 | Binder.restoreCallingIdentity(identity); |
| 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2921 | public void setNotificationPolicy(String pkg, Policy policy) { |
| 2922 | enforcePolicyAccess(pkg, "setNotificationPolicy"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2923 | final long identity = Binder.clearCallingIdentity(); |
| 2924 | try { |
| 2925 | mZenModeHelper.setNotificationPolicy(policy); |
| 2926 | } finally { |
| 2927 | Binder.restoreCallingIdentity(identity); |
| 2928 | } |
| 2929 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2930 | |
| 2931 | @Override |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2932 | public List<String> getEnabledNotificationListenerPackages() { |
| 2933 | checkCallerIsSystem(); |
| 2934 | return mListeners.getAllowedPackages(getCallingUserHandle().getIdentifier()); |
| 2935 | } |
| 2936 | |
| 2937 | @Override |
| 2938 | public List<ComponentName> getEnabledNotificationListeners(int userId) { |
| 2939 | checkCallerIsSystem(); |
| 2940 | return mListeners.getAllowedComponents(userId); |
| 2941 | } |
| 2942 | |
| 2943 | @Override |
| 2944 | public boolean isNotificationListenerAccessGranted(ComponentName listener) { |
| 2945 | Preconditions.checkNotNull(listener); |
| 2946 | checkCallerIsSystemOrSameApp(listener.getPackageName()); |
| 2947 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 2948 | getCallingUserHandle().getIdentifier()); |
| 2949 | } |
| 2950 | |
| 2951 | @Override |
| 2952 | public boolean isNotificationListenerAccessGrantedForUser(ComponentName listener, |
| 2953 | int userId) { |
| 2954 | Preconditions.checkNotNull(listener); |
| 2955 | checkCallerIsSystem(); |
| 2956 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 2957 | userId); |
| 2958 | } |
| 2959 | |
| 2960 | @Override |
| 2961 | public boolean isNotificationAssistantAccessGranted(ComponentName assistant) { |
| 2962 | Preconditions.checkNotNull(assistant); |
| 2963 | checkCallerIsSystemOrSameApp(assistant.getPackageName()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2964 | return mAssistants.isPackageOrComponentAllowed(assistant.flattenToString(), |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2965 | getCallingUserHandle().getIdentifier()); |
| 2966 | } |
| 2967 | |
| 2968 | @Override |
| 2969 | public void setNotificationListenerAccessGranted(ComponentName listener, |
| 2970 | boolean granted) throws RemoteException { |
| 2971 | setNotificationListenerAccessGrantedForUser( |
| 2972 | listener, getCallingUserHandle().getIdentifier(), granted); |
| 2973 | } |
| 2974 | |
| 2975 | @Override |
| 2976 | public void setNotificationAssistantAccessGranted(ComponentName assistant, |
| 2977 | boolean granted) throws RemoteException { |
| 2978 | setNotificationAssistantAccessGrantedForUser( |
| 2979 | assistant, getCallingUserHandle().getIdentifier(), granted); |
| 2980 | } |
| 2981 | |
| 2982 | @Override |
| 2983 | public void setNotificationListenerAccessGrantedForUser(ComponentName listener, int userId, |
| 2984 | boolean granted) throws RemoteException { |
| 2985 | Preconditions.checkNotNull(listener); |
Julia Reynolds | 0d21764 | 2017-08-11 11:26:04 -0400 | [diff] [blame] | 2986 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 2987 | final long identity = Binder.clearCallingIdentity(); |
| 2988 | try { |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 2989 | if (canUseManagedServices()) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 2990 | mConditionProviders.setPackageOrComponentEnabled(listener.flattenToString(), |
| 2991 | userId, false, granted); |
| 2992 | mListeners.setPackageOrComponentEnabled(listener.flattenToString(), |
| 2993 | userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2994 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 2995 | getContext().sendBroadcastAsUser(new Intent( |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2996 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 2997 | .setPackage(listener.getPackageName()) |
| 2998 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 2999 | UserHandle.of(userId), null); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3000 | |
| 3001 | savePolicyFile(); |
| 3002 | } |
| 3003 | } finally { |
| 3004 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3005 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | @Override |
| 3009 | public void setNotificationAssistantAccessGrantedForUser(ComponentName assistant, |
| 3010 | int userId, boolean granted) throws RemoteException { |
| 3011 | Preconditions.checkNotNull(assistant); |
Julia Reynolds | 0d21764 | 2017-08-11 11:26:04 -0400 | [diff] [blame] | 3012 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3013 | final long identity = Binder.clearCallingIdentity(); |
| 3014 | try { |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3015 | if (canUseManagedServices()) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3016 | mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(), |
| 3017 | userId, false, granted); |
| 3018 | mAssistants.setPackageOrComponentEnabled(assistant.flattenToString(), |
| 3019 | userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3020 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3021 | getContext().sendBroadcastAsUser(new Intent( |
| 3022 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 3023 | .setPackage(assistant.getPackageName()) |
| 3024 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3025 | UserHandle.of(userId), null); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3026 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3027 | savePolicyFile(); |
| 3028 | } |
| 3029 | } finally { |
| 3030 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3031 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | @Override |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3035 | public void applyEnqueuedAdjustmentFromAssistant(INotificationListener token, |
| 3036 | Adjustment adjustment) throws RemoteException { |
| 3037 | final long identity = Binder.clearCallingIdentity(); |
| 3038 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3039 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3040 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3041 | int N = mEnqueuedNotifications.size(); |
| 3042 | for (int i = 0; i < N; i++) { |
| 3043 | final NotificationRecord n = mEnqueuedNotifications.get(i); |
| 3044 | if (Objects.equals(adjustment.getKey(), n.getKey()) |
| 3045 | && Objects.equals(adjustment.getUser(), n.getUserId())) { |
| 3046 | applyAdjustment(n, adjustment); |
| 3047 | break; |
| 3048 | } |
| 3049 | } |
| 3050 | } |
| 3051 | } finally { |
| 3052 | Binder.restoreCallingIdentity(identity); |
| 3053 | } |
| 3054 | } |
| 3055 | |
| 3056 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 3057 | public void applyAdjustmentFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3058 | Adjustment adjustment) throws RemoteException { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3059 | final long identity = Binder.clearCallingIdentity(); |
| 3060 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3061 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3062 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3063 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 3064 | applyAdjustment(n, adjustment); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3065 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3066 | mRankingHandler.requestSort(); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3067 | } finally { |
| 3068 | Binder.restoreCallingIdentity(identity); |
| 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 3073 | public void applyAdjustmentsFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3074 | List<Adjustment> adjustments) throws RemoteException { |
| 3075 | |
| 3076 | final long identity = Binder.clearCallingIdentity(); |
| 3077 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3078 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3079 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3080 | for (Adjustment adjustment : adjustments) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3081 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 3082 | applyAdjustment(n, adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3083 | } |
| 3084 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3085 | mRankingHandler.requestSort(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3086 | } finally { |
| 3087 | Binder.restoreCallingIdentity(identity); |
| 3088 | } |
| 3089 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3090 | |
| 3091 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 3092 | public void updateNotificationChannelGroupFromPrivilegedListener( |
| 3093 | INotificationListener token, String pkg, UserHandle user, |
| 3094 | NotificationChannelGroup group) throws RemoteException { |
| 3095 | Preconditions.checkNotNull(user); |
| 3096 | verifyPrivilegedListener(token, user); |
| 3097 | createNotificationChannelGroup( |
| 3098 | pkg, getUidForPackageAndUser(pkg, user), group, false, true); |
| 3099 | savePolicyFile(); |
| 3100 | } |
| 3101 | |
| 3102 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3103 | public void updateNotificationChannelFromPrivilegedListener(INotificationListener token, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3104 | String pkg, UserHandle user, NotificationChannel channel) throws RemoteException { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3105 | Preconditions.checkNotNull(channel); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3106 | Preconditions.checkNotNull(pkg); |
| 3107 | Preconditions.checkNotNull(user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3108 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3109 | verifyPrivilegedListener(token, user); |
| 3110 | updateNotificationChannelInt(pkg, getUidForPackageAndUser(pkg, user), channel, true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | @Override |
| 3114 | public ParceledListSlice<NotificationChannel> getNotificationChannelsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3115 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 3116 | Preconditions.checkNotNull(pkg); |
| 3117 | Preconditions.checkNotNull(user); |
| 3118 | verifyPrivilegedListener(token, user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3119 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3120 | return mRankingHelper.getNotificationChannels(pkg, getUidForPackageAndUser(pkg, user), |
| 3121 | false /* includeDeleted */); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | @Override |
| 3125 | public ParceledListSlice<NotificationChannelGroup> |
| 3126 | getNotificationChannelGroupsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3127 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 3128 | Preconditions.checkNotNull(pkg); |
| 3129 | Preconditions.checkNotNull(user); |
| 3130 | verifyPrivilegedListener(token, user); |
| 3131 | |
| 3132 | List<NotificationChannelGroup> groups = new ArrayList<>(); |
| 3133 | groups.addAll(mRankingHelper.getNotificationChannelGroups( |
| 3134 | pkg, getUidForPackageAndUser(pkg, user))); |
| 3135 | return new ParceledListSlice<>(groups); |
| 3136 | } |
| 3137 | |
| 3138 | private void verifyPrivilegedListener(INotificationListener token, UserHandle user) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3139 | ManagedServiceInfo info; |
| 3140 | synchronized (mNotificationLock) { |
| 3141 | info = mListeners.checkServiceTokenLocked(token); |
| 3142 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 3143 | if (!hasCompanionDevice(info)) { |
| 3144 | throw new SecurityException(info + " does not have access"); |
| 3145 | } |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3146 | if (!info.enabledAndUserMatches(user.getIdentifier())) { |
| 3147 | throw new SecurityException(info + " does not have access"); |
| 3148 | } |
| 3149 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3150 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3151 | private int getUidForPackageAndUser(String pkg, UserHandle user) throws RemoteException { |
| 3152 | int uid = 0; |
| 3153 | long identity = Binder.clearCallingIdentity(); |
| 3154 | try { |
| 3155 | uid = mPackageManager.getPackageUid(pkg, 0, user.getIdentifier()); |
| 3156 | } finally { |
| 3157 | Binder.restoreCallingIdentity(identity); |
| 3158 | } |
| 3159 | return uid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3160 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3161 | |
| 3162 | @Override |
| 3163 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 3164 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) |
| 3165 | throws RemoteException { |
| 3166 | new ShellCmd().exec(this, in, out, err, args, callback, resultReceiver); |
| 3167 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3168 | }; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 3169 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3170 | private void applyAdjustment(NotificationRecord r, Adjustment adjustment) { |
| 3171 | if (r == null) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3172 | return; |
| 3173 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3174 | if (adjustment.getSignals() != null) { |
| 3175 | Bundle.setDefusable(adjustment.getSignals(), true); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3176 | r.addAdjustment(adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3177 | } |
| 3178 | } |
| 3179 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3180 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3181 | void addAutogroupKeyLocked(String key) { |
| 3182 | NotificationRecord r = mNotificationsByKey.get(key); |
| 3183 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3184 | return; |
| 3185 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3186 | if (r.sbn.getOverrideGroupKey() == null) { |
| 3187 | addAutoGroupAdjustment(r, GroupHelper.AUTOGROUP_KEY); |
| 3188 | EventLogTags.writeNotificationAutogrouped(key); |
| 3189 | mRankingHandler.requestSort(); |
| 3190 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3191 | } |
| 3192 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3193 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3194 | void removeAutogroupKeyLocked(String key) { |
| 3195 | NotificationRecord r = mNotificationsByKey.get(key); |
| 3196 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3197 | return; |
| 3198 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3199 | if (r.sbn.getOverrideGroupKey() != null) { |
| 3200 | addAutoGroupAdjustment(r, null); |
| 3201 | EventLogTags.writeNotificationUnautogrouped(key); |
| 3202 | mRankingHandler.requestSort(); |
| 3203 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | private void addAutoGroupAdjustment(NotificationRecord r, String overrideGroupKey) { |
| 3207 | Bundle signals = new Bundle(); |
| 3208 | signals.putString(Adjustment.KEY_GROUP_KEY, overrideGroupKey); |
| 3209 | Adjustment adjustment = |
| 3210 | new Adjustment(r.sbn.getPackageName(), r.getKey(), signals, "", r.sbn.getUserId()); |
| 3211 | r.addAdjustment(adjustment); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3212 | } |
| 3213 | |
| 3214 | // Clears the 'fake' auto-group summary. |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3215 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3216 | private void clearAutogroupSummaryLocked(int userId, String pkg) { |
| 3217 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 3218 | if (summaries != null && summaries.containsKey(pkg)) { |
| 3219 | // Clear summary. |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3220 | final NotificationRecord removed = findNotificationByKeyLocked(summaries.remove(pkg)); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3221 | if (removed != null) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 3222 | boolean wasPosted = removeFromNotificationListsLocked(removed); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 3223 | cancelNotificationLocked(removed, false, REASON_UNAUTOBUNDLED, wasPosted, null); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3224 | } |
| 3225 | } |
| 3226 | } |
| 3227 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 3228 | @GuardedBy("mNotificationLock") |
| 3229 | private boolean hasAutoGroupSummaryLocked(StatusBarNotification sbn) { |
| 3230 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(sbn.getUserId()); |
| 3231 | return summaries != null && summaries.containsKey(sbn.getPackageName()); |
| 3232 | } |
| 3233 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3234 | // 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] | 3235 | private void createAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 3236 | NotificationRecord summaryRecord = null; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3237 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3238 | NotificationRecord notificationRecord = mNotificationsByKey.get(triggeringKey); |
| 3239 | if (notificationRecord == null) { |
| 3240 | // The notification could have been cancelled again already. A successive |
| 3241 | // adjustment will post a summary if needed. |
| 3242 | return; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3243 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3244 | final StatusBarNotification adjustedSbn = notificationRecord.sbn; |
| 3245 | userId = adjustedSbn.getUser().getIdentifier(); |
| 3246 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 3247 | if (summaries == null) { |
| 3248 | summaries = new ArrayMap<>(); |
| 3249 | } |
| 3250 | mAutobundledSummaries.put(userId, summaries); |
| 3251 | if (!summaries.containsKey(pkg)) { |
| 3252 | // Add summary |
| 3253 | final ApplicationInfo appInfo = |
| 3254 | adjustedSbn.getNotification().extras.getParcelable( |
| 3255 | Notification.EXTRA_BUILDER_APPLICATION_INFO); |
| 3256 | final Bundle extras = new Bundle(); |
| 3257 | extras.putParcelable(Notification.EXTRA_BUILDER_APPLICATION_INFO, appInfo); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3258 | final String channelId = notificationRecord.getChannel().getId(); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3259 | final Notification summaryNotification = |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3260 | new Notification.Builder(getContext(), channelId) |
| 3261 | .setSmallIcon(adjustedSbn.getNotification().getSmallIcon()) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3262 | .setGroupSummary(true) |
Julia Reynolds | 9d5786e | 2017-04-28 10:26:32 -0400 | [diff] [blame] | 3263 | .setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3264 | .setGroup(GroupHelper.AUTOGROUP_KEY) |
| 3265 | .setFlag(Notification.FLAG_AUTOGROUP_SUMMARY, true) |
| 3266 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 3267 | .setColor(adjustedSbn.getNotification().color) |
| 3268 | .setLocalOnly(true) |
| 3269 | .build(); |
| 3270 | summaryNotification.extras.putAll(extras); |
| 3271 | Intent appIntent = getContext().getPackageManager().getLaunchIntentForPackage(pkg); |
| 3272 | if (appIntent != null) { |
| 3273 | summaryNotification.contentIntent = PendingIntent.getActivityAsUser( |
| 3274 | getContext(), 0, appIntent, 0, null, UserHandle.of(userId)); |
| 3275 | } |
| 3276 | final StatusBarNotification summarySbn = |
| 3277 | new StatusBarNotification(adjustedSbn.getPackageName(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3278 | adjustedSbn.getOpPkg(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3279 | Integer.MAX_VALUE, |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3280 | GroupHelper.AUTOGROUP_KEY, adjustedSbn.getUid(), |
| 3281 | adjustedSbn.getInitialPid(), summaryNotification, |
| 3282 | adjustedSbn.getUser(), GroupHelper.AUTOGROUP_KEY, |
| 3283 | System.currentTimeMillis()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 3284 | summaryRecord = new NotificationRecord(getContext(), summarySbn, |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 3285 | notificationRecord.getChannel()); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3286 | summaries.put(pkg, summarySbn.getKey()); |
| 3287 | } |
| 3288 | } |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3289 | if (summaryRecord != null && checkDisqualifyingFeatures(userId, MY_UID, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3290 | summaryRecord.sbn.getId(), summaryRecord.sbn.getTag(), summaryRecord, true)) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3291 | mHandler.post(new EnqueueNotificationRunnable(userId, summaryRecord)); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3292 | } |
| 3293 | } |
| 3294 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3295 | private String disableNotificationEffects(NotificationRecord record) { |
| 3296 | if (mDisableNotificationEffects) { |
| 3297 | return "booleanState"; |
| 3298 | } |
| 3299 | if ((mListenerHints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 3300 | return "listenerHints"; |
| 3301 | } |
| 3302 | if (mCallState != TelephonyManager.CALL_STATE_IDLE && !mZenModeHelper.isCall(record)) { |
| 3303 | return "callState"; |
| 3304 | } |
| 3305 | return null; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3306 | }; |
| 3307 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3308 | private void dumpJson(PrintWriter pw, @NonNull DumpFilter filter) { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3309 | JSONObject dump = new JSONObject(); |
| 3310 | try { |
| 3311 | dump.put("service", "Notification Manager"); |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 3312 | dump.put("bans", mRankingHelper.dumpBansJson(filter)); |
| 3313 | dump.put("ranking", mRankingHelper.dumpJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3314 | dump.put("stats", mUsageStats.dumpJson(filter)); |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 3315 | dump.put("channels", mRankingHelper.dumpChannelsJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3316 | } catch (JSONException e) { |
| 3317 | e.printStackTrace(); |
| 3318 | } |
| 3319 | pw.println(dump); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3320 | } |
| 3321 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3322 | private void dumpProto(FileDescriptor fd, @NonNull DumpFilter filter) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3323 | final ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 3324 | synchronized (mNotificationLock) { |
| 3325 | long records = proto.start(NotificationServiceDumpProto.RECORDS); |
| 3326 | int N = mNotificationList.size(); |
| 3327 | if (N > 0) { |
| 3328 | for (int i = 0; i < N; i++) { |
| 3329 | final NotificationRecord nr = mNotificationList.get(i); |
| 3330 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3331 | nr.dump(proto, filter.redact); |
Yi Jin | c7f9307 | 2017-09-29 15:29:38 -0700 | [diff] [blame] | 3332 | proto.write(NotificationRecordProto.STATE, NotificationRecordProto.POSTED); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3333 | } |
| 3334 | } |
| 3335 | N = mEnqueuedNotifications.size(); |
| 3336 | if (N > 0) { |
| 3337 | for (int i = 0; i < N; i++) { |
| 3338 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 3339 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3340 | nr.dump(proto, filter.redact); |
Yi Jin | c7f9307 | 2017-09-29 15:29:38 -0700 | [diff] [blame] | 3341 | proto.write(NotificationRecordProto.STATE, NotificationRecordProto.ENQUEUED); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3342 | } |
| 3343 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3344 | List<NotificationRecord> snoozed = mSnoozeHelper.getSnoozed(); |
| 3345 | N = snoozed.size(); |
| 3346 | if (N > 0) { |
| 3347 | for (int i = 0; i < N; i++) { |
| 3348 | final NotificationRecord nr = snoozed.get(i); |
| 3349 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3350 | nr.dump(proto, filter.redact); |
Yi Jin | c7f9307 | 2017-09-29 15:29:38 -0700 | [diff] [blame] | 3351 | proto.write(NotificationRecordProto.STATE, NotificationRecordProto.SNOOZED); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3352 | } |
| 3353 | } |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3354 | proto.end(records); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3355 | |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3356 | long zenLog = proto.start(NotificationServiceDumpProto.ZEN); |
| 3357 | mZenModeHelper.dump(proto); |
| 3358 | for (ComponentName suppressor : mEffectsSuppressors) { |
| 3359 | proto.write(ZenModeProto.SUPPRESSORS, suppressor.toString()); |
| 3360 | } |
| 3361 | proto.end(zenLog); |
| 3362 | |
| 3363 | long listenersToken = proto.start(NotificationServiceDumpProto.NOTIFICATION_LISTENERS); |
| 3364 | mListeners.dump(proto, filter); |
| 3365 | proto.end(listenersToken); |
| 3366 | |
| 3367 | proto.write(NotificationServiceDumpProto.LISTENER_HINTS, mListenerHints); |
| 3368 | |
| 3369 | for (int i = 0; i < mListenersDisablingEffects.size(); ++i) { |
| 3370 | long effectsToken = proto.start( |
| 3371 | NotificationServiceDumpProto.LISTENERS_DISABLING_EFFECTS); |
| 3372 | |
| 3373 | proto.write( |
| 3374 | ListenersDisablingEffectsProto.HINT, mListenersDisablingEffects.keyAt(i)); |
| 3375 | final ArraySet<ManagedServiceInfo> listeners = |
| 3376 | mListenersDisablingEffects.valueAt(i); |
| 3377 | for (int j = 0; j < listeners.size(); j++) { |
| 3378 | final ManagedServiceInfo listener = listeners.valueAt(i); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 3379 | listener.writeToProto(proto, ListenersDisablingEffectsProto.LISTENERS, null); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3380 | } |
| 3381 | |
| 3382 | proto.end(effectsToken); |
| 3383 | } |
| 3384 | |
| 3385 | long assistantsToken = proto.start( |
| 3386 | NotificationServiceDumpProto.NOTIFICATION_ASSISTANTS); |
| 3387 | mAssistants.dump(proto, filter); |
| 3388 | proto.end(assistantsToken); |
| 3389 | |
| 3390 | long conditionsToken = proto.start(NotificationServiceDumpProto.CONDITION_PROVIDERS); |
| 3391 | mConditionProviders.dump(proto, filter); |
| 3392 | proto.end(conditionsToken); |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 3393 | |
| 3394 | long rankingToken = proto.start(NotificationServiceDumpProto.RANKING_CONFIG); |
| 3395 | mRankingHelper.dump(proto, filter); |
| 3396 | proto.end(rankingToken); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3397 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3398 | |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3399 | proto.flush(); |
| 3400 | } |
| 3401 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3402 | void dumpImpl(PrintWriter pw, @NonNull DumpFilter filter) { |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3403 | pw.print("Current Notification Manager state"); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3404 | if (filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3405 | pw.print(" (filtered to "); pw.print(filter); pw.print(")"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3406 | } |
| 3407 | pw.println(':'); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3408 | int N; |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3409 | final boolean zenOnly = filter.filtered && filter.zen; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3410 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3411 | if (!zenOnly) { |
| 3412 | synchronized (mToastQueue) { |
| 3413 | N = mToastQueue.size(); |
| 3414 | if (N > 0) { |
| 3415 | pw.println(" Toast Queue:"); |
| 3416 | for (int i=0; i<N; i++) { |
| 3417 | mToastQueue.get(i).dump(pw, " ", filter); |
| 3418 | } |
| 3419 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3420 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3421 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3422 | } |
| 3423 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3424 | synchronized (mNotificationLock) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3425 | if (!zenOnly) { |
| 3426 | N = mNotificationList.size(); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3427 | if (N > 0) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3428 | pw.println(" Notification List:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3429 | for (int i=0; i<N; i++) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3430 | final NotificationRecord nr = mNotificationList.get(i); |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3431 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3432 | nr.dump(pw, " ", getContext(), filter.redact); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3433 | } |
| 3434 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3435 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3436 | |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3437 | if (!filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3438 | N = mLights.size(); |
| 3439 | if (N > 0) { |
| 3440 | pw.println(" Lights List:"); |
| 3441 | for (int i=0; i<N; i++) { |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3442 | if (i == N - 1) { |
| 3443 | pw.print(" > "); |
| 3444 | } else { |
| 3445 | pw.print(" "); |
| 3446 | } |
| 3447 | pw.println(mLights.get(i)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3448 | } |
| 3449 | pw.println(" "); |
| 3450 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 3451 | pw.println(" mUseAttentionLight=" + mUseAttentionLight); |
| 3452 | pw.println(" mNotificationPulseEnabled=" + mNotificationPulseEnabled); |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3453 | pw.println(" mSoundNotificationKey=" + mSoundNotificationKey); |
| 3454 | pw.println(" mVibrateNotificationKey=" + mVibrateNotificationKey); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3455 | pw.println(" mDisableNotificationEffects=" + mDisableNotificationEffects); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3456 | pw.println(" mCallState=" + callStateToString(mCallState)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3457 | pw.println(" mSystemReady=" + mSystemReady); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 3458 | pw.println(" mMaxPackageEnqueueRate=" + mMaxPackageEnqueueRate); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3459 | } |
| 3460 | pw.println(" mArchive=" + mArchive.toString()); |
| 3461 | Iterator<StatusBarNotification> iter = mArchive.descendingIterator(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3462 | int j=0; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3463 | while (iter.hasNext()) { |
| 3464 | final StatusBarNotification sbn = iter.next(); |
| 3465 | if (filter != null && !filter.matches(sbn)) continue; |
| 3466 | pw.println(" " + sbn); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3467 | if (++j >= 5) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3468 | if (iter.hasNext()) pw.println(" ..."); |
| 3469 | break; |
| 3470 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3471 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3472 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3473 | if (!zenOnly) { |
| 3474 | N = mEnqueuedNotifications.size(); |
| 3475 | if (N > 0) { |
| 3476 | pw.println(" Enqueued Notification List:"); |
| 3477 | for (int i = 0; i < N; i++) { |
| 3478 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 3479 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3480 | nr.dump(pw, " ", getContext(), filter.redact); |
| 3481 | } |
| 3482 | pw.println(" "); |
| 3483 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3484 | |
| 3485 | mSnoozeHelper.dump(pw, filter); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3486 | } |
| 3487 | } |
| 3488 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3489 | if (!zenOnly) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3490 | pw.println("\n Ranking Config:"); |
| 3491 | mRankingHelper.dump(pw, " ", filter); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 3492 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3493 | pw.println("\n Notification listeners:"); |
| 3494 | mListeners.dump(pw, filter); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3495 | pw.print(" mListenerHints: "); pw.println(mListenerHints); |
| 3496 | pw.print(" mListenersDisablingEffects: ("); |
| 3497 | N = mListenersDisablingEffects.size(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3498 | for (int i = 0; i < N; i++) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 3499 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 3500 | if (i > 0) pw.print(';'); |
| 3501 | pw.print("hint[" + hint + "]:"); |
| 3502 | |
| 3503 | final ArraySet<ManagedServiceInfo> listeners = |
| 3504 | mListenersDisablingEffects.valueAt(i); |
| 3505 | final int listenerSize = listeners.size(); |
| 3506 | |
| 3507 | for (int j = 0; j < listenerSize; j++) { |
| 3508 | if (i > 0) pw.print(','); |
| 3509 | final ManagedServiceInfo listener = listeners.valueAt(i); |
| 3510 | pw.print(listener.component); |
| 3511 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3512 | } |
| 3513 | pw.println(')'); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 3514 | pw.println("\n Notification assistant services:"); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3515 | mAssistants.dump(pw, filter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3516 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3517 | |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3518 | if (!filter.filtered || zenOnly) { |
| 3519 | pw.println("\n Zen Mode:"); |
| 3520 | pw.print(" mInterruptionFilter="); pw.println(mInterruptionFilter); |
| 3521 | mZenModeHelper.dump(pw, " "); |
| 3522 | |
| 3523 | pw.println("\n Zen Log:"); |
| 3524 | ZenLog.dump(pw, " "); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3525 | } |
| 3526 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3527 | pw.println("\n Condition providers:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3528 | mConditionProviders.dump(pw, filter); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3529 | |
| 3530 | pw.println("\n Group summaries:"); |
| 3531 | for (Entry<String, NotificationRecord> entry : mSummaryByGroupKey.entrySet()) { |
| 3532 | NotificationRecord r = entry.getValue(); |
| 3533 | pw.println(" " + entry.getKey() + " -> " + r.getKey()); |
| 3534 | if (mNotificationsByKey.get(r.getKey()) != r) { |
| 3535 | pw.println("!!!!!!LEAK: Record not found in mNotificationsByKey."); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3536 | r.dump(pw, " ", getContext(), filter.redact); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3537 | } |
| 3538 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3539 | |
| 3540 | if (!zenOnly) { |
| 3541 | pw.println("\n Usage Stats:"); |
| 3542 | mUsageStats.dump(pw, " ", filter); |
| 3543 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3544 | } |
| 3545 | } |
| 3546 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3547 | /** |
| 3548 | * The private API only accessible to the system process. |
| 3549 | */ |
| 3550 | private final NotificationManagerInternal mInternalService = new NotificationManagerInternal() { |
| 3551 | @Override |
Julia Reynolds | f3de8aa | 2017-09-29 15:52:37 -0400 | [diff] [blame] | 3552 | public NotificationChannel getNotificationChannel(String pkg, int uid, String |
| 3553 | channelId) { |
| 3554 | return mRankingHelper.getNotificationChannel(pkg, uid, channelId, false); |
| 3555 | } |
| 3556 | |
| 3557 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3558 | public void enqueueNotification(String pkg, String opPkg, int callingUid, int callingPid, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3559 | String tag, int id, Notification notification, int userId) { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3560 | enqueueNotificationInternal(pkg, opPkg, callingUid, callingPid, tag, id, notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3561 | userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3562 | } |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3563 | |
| 3564 | @Override |
| 3565 | public void removeForegroundServiceFlagFromNotification(String pkg, int notificationId, |
| 3566 | int userId) { |
| 3567 | checkCallerIsSystem(); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3568 | mHandler.post(new Runnable() { |
| 3569 | @Override |
| 3570 | public void run() { |
| 3571 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3572 | removeForegroundServiceFlagByListLocked( |
| 3573 | mEnqueuedNotifications, pkg, notificationId, userId); |
| 3574 | removeForegroundServiceFlagByListLocked( |
| 3575 | mNotificationList, pkg, notificationId, userId); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3576 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3577 | } |
| 3578 | }); |
| 3579 | } |
| 3580 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3581 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3582 | private void removeForegroundServiceFlagByListLocked( |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3583 | ArrayList<NotificationRecord> notificationList, String pkg, int notificationId, |
| 3584 | int userId) { |
| 3585 | NotificationRecord r = findNotificationByListLocked( |
| 3586 | notificationList, pkg, null, notificationId, userId); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3587 | if (r == null) { |
| 3588 | return; |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3589 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3590 | StatusBarNotification sbn = r.sbn; |
| 3591 | // NoMan adds flags FLAG_NO_CLEAR and FLAG_ONGOING_EVENT when it sees |
| 3592 | // FLAG_FOREGROUND_SERVICE. Hence it's not enough to remove |
| 3593 | // FLAG_FOREGROUND_SERVICE, we have to revert to the flags we received |
| 3594 | // initially *and* force remove FLAG_FOREGROUND_SERVICE. |
| 3595 | sbn.getNotification().flags = |
| 3596 | (r.mOriginalFlags & ~Notification.FLAG_FOREGROUND_SERVICE); |
| 3597 | mRankingHelper.sort(mNotificationList); |
| 3598 | mListeners.notifyPostedLocked(sbn, sbn /* oldSbn */); |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3599 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3600 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3601 | |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3602 | void enqueueNotificationInternal(final String pkg, final String opPkg, final int callingUid, |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 3603 | final int callingPid, final String tag, final int id, final Notification notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3604 | int incomingUserId) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 3605 | if (DBG) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3606 | Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id |
| 3607 | + " notification=" + notification); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 3608 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3609 | checkCallerIsSystemOrSameApp(pkg); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3610 | |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 3611 | final int userId = ActivityManager.handleIncomingUser(callingPid, |
| 3612 | callingUid, incomingUserId, true, false, "enqueueNotification", pkg); |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 3613 | final UserHandle user = new UserHandle(userId); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3614 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3615 | if (pkg == null || notification == null) { |
| 3616 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 3617 | + " id=" + id + " notification=" + notification); |
| 3618 | } |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3619 | |
| 3620 | // The system can post notifications for any package, let us resolve that. |
| 3621 | final int notificationUid = resolveNotificationUid(opPkg, callingUid, userId); |
| 3622 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3623 | // Fix the notification as best we can. |
| 3624 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3625 | final ApplicationInfo ai = mPackageManagerClient.getApplicationInfoAsUser( |
Jeff Sharkey | 012bc7b | 2016-04-11 16:30:27 -0600 | [diff] [blame] | 3626 | pkg, PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 3627 | (userId == UserHandle.USER_ALL) ? UserHandle.USER_SYSTEM : userId); |
Julia Reynolds | e071abd | 2017-03-22 10:52:11 -0400 | [diff] [blame] | 3628 | Notification.addFieldsFromContext(ai, notification); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3629 | |
| 3630 | int canColorize = mPackageManagerClient.checkPermission( |
| 3631 | android.Manifest.permission.USE_COLORIZED_NOTIFICATIONS, pkg); |
| 3632 | if (canColorize == PERMISSION_GRANTED) { |
| 3633 | notification.flags |= Notification.FLAG_CAN_COLORIZE; |
| 3634 | } else { |
| 3635 | notification.flags &= ~Notification.FLAG_CAN_COLORIZE; |
| 3636 | } |
| 3637 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3638 | } catch (NameNotFoundException e) { |
| 3639 | Slog.e(TAG, "Cannot create a context for sending app", e); |
| 3640 | return; |
| 3641 | } |
| 3642 | |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 3643 | mUsageStats.registerEnqueuedByApp(pkg); |
| 3644 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3645 | // setup local book-keeping |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 3646 | String channelId = notification.getChannelId(); |
| 3647 | if (mIsTelevision && (new Notification.TvExtender(notification)).getChannelId() != null) { |
| 3648 | channelId = (new Notification.TvExtender(notification)).getChannelId(); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 3649 | } |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3650 | final NotificationChannel channel = mRankingHelper.getNotificationChannel(pkg, |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3651 | notificationUid, channelId, false /* includeDeleted */); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3652 | if (channel == null) { |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3653 | final String noChannelStr = "No Channel found for " |
| 3654 | + "pkg=" + pkg |
| 3655 | + ", channelId=" + channelId |
Julia Reynolds | f26eb91 | 2017-05-22 15:47:06 -0400 | [diff] [blame] | 3656 | + ", id=" + id |
| 3657 | + ", tag=" + tag |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3658 | + ", opPkg=" + opPkg |
| 3659 | + ", callingUid=" + callingUid |
| 3660 | + ", userId=" + userId |
| 3661 | + ", incomingUserId=" + incomingUserId |
| 3662 | + ", notificationUid=" + notificationUid |
| 3663 | + ", notification=" + notification; |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3664 | Log.e(TAG, noChannelStr); |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 3665 | doChannelWarningToast("Developer warning for package \"" + pkg + "\"\n" + |
Geoffrey Pitsch | cadb5dc | 2017-04-11 11:35:02 -0400 | [diff] [blame] | 3666 | "Failed to post notification on channel \"" + channelId + "\"\n" + |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3667 | "See log for more details"); |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3668 | return; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3669 | } |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3670 | |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 3671 | final StatusBarNotification n = new StatusBarNotification( |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3672 | pkg, opPkg, id, tag, notificationUid, callingPid, notification, |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3673 | user, null, System.currentTimeMillis()); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 3674 | final NotificationRecord r = new NotificationRecord(getContext(), n, channel); |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 3675 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3676 | if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0 |
| 3677 | && (channel.getUserLockedFields() & NotificationChannel.USER_LOCKED_IMPORTANCE) == 0 |
| 3678 | && (r.getImportance() == IMPORTANCE_MIN || r.getImportance() == IMPORTANCE_NONE)) { |
| 3679 | // Increase the importance of foreground service notifications unless the user had an |
| 3680 | // opinion otherwise |
| 3681 | if (TextUtils.isEmpty(channelId) |
| 3682 | || NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 3683 | r.setImportance(IMPORTANCE_LOW, "Bumped for foreground service"); |
| 3684 | } else { |
| 3685 | channel.setImportance(IMPORTANCE_LOW); |
| 3686 | mRankingHelper.updateNotificationChannel(pkg, notificationUid, channel, false); |
| 3687 | r.updateNotificationChannel(channel); |
| 3688 | } |
| 3689 | } |
| 3690 | |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3691 | if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r, |
| 3692 | r.sbn.getOverrideGroupKey() != null)) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3693 | return; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 3694 | } |
| 3695 | |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 3696 | // Whitelist pending intents. |
| 3697 | if (notification.allPendingIntents != null) { |
| 3698 | final int intentCount = notification.allPendingIntents.size(); |
| 3699 | if (intentCount > 0) { |
| 3700 | final ActivityManagerInternal am = LocalServices |
| 3701 | .getService(ActivityManagerInternal.class); |
| 3702 | final long duration = LocalServices.getService( |
| 3703 | DeviceIdleController.LocalService.class).getNotificationWhitelistDuration(); |
| 3704 | for (int i = 0; i < intentCount; i++) { |
| 3705 | PendingIntent pendingIntent = notification.allPendingIntents.valueAt(i); |
| 3706 | if (pendingIntent != null) { |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 3707 | am.setPendingIntentWhitelistDuration(pendingIntent.getTarget(), |
| 3708 | WHITELIST_TOKEN, duration); |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 3709 | } |
| 3710 | } |
| 3711 | } |
| 3712 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 3713 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3714 | mHandler.post(new EnqueueNotificationRunnable(userId, r)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3715 | } |
| 3716 | |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 3717 | private void doChannelWarningToast(CharSequence toastText) { |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 3718 | final int defaultWarningEnabled = Build.IS_DEBUGGABLE ? 1 : 0; |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 3719 | final boolean warningEnabled = Settings.Global.getInt(getContext().getContentResolver(), |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 3720 | Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, defaultWarningEnabled) != 0; |
| 3721 | if (warningEnabled) { |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 3722 | Toast toast = Toast.makeText(getContext(), mHandler.getLooper(), toastText, |
Geoffrey Pitsch | 5bdddbd | 2017-05-26 10:50:05 -0400 | [diff] [blame] | 3723 | Toast.LENGTH_SHORT); |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 3724 | toast.show(); |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3725 | } |
| 3726 | } |
| 3727 | |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3728 | private int resolveNotificationUid(String opPackageName, int callingUid, int userId) { |
| 3729 | // The system can post notifications on behalf of any package it wants |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3730 | if (isCallerSystemOrPhone() && opPackageName != null && !"android".equals(opPackageName)) { |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3731 | try { |
| 3732 | return getContext().getPackageManager() |
| 3733 | .getPackageUidAsUser(opPackageName, userId); |
| 3734 | } catch (NameNotFoundException e) { |
| 3735 | /* ignore */ |
| 3736 | } |
| 3737 | } |
| 3738 | return callingUid; |
| 3739 | } |
| 3740 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3741 | /** |
| 3742 | * Checks if a notification can be posted. checks rate limiter, snooze helper, and blocking. |
| 3743 | * |
| 3744 | * Has side effects. |
| 3745 | */ |
| 3746 | private boolean checkDisqualifyingFeatures(int userId, int callingUid, int id, String tag, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3747 | NotificationRecord r, boolean isAutogroup) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3748 | final String pkg = r.sbn.getPackageName(); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3749 | final boolean isSystemNotification = |
| 3750 | isUidSystemOrPhone(callingUid) || ("android".equals(pkg)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3751 | final boolean isNotificationFromListener = mListeners.isListenerPackage(pkg); |
| 3752 | |
| 3753 | // Limit the number of notifications that any given package except the android |
| 3754 | // package or a registered listener can enqueue. Prevents DOS attacks and deals with leaks. |
| 3755 | if (!isSystemNotification && !isNotificationFromListener) { |
| 3756 | synchronized (mNotificationLock) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3757 | if (mNotificationsByKey.get(r.sbn.getKey()) == null && isCallerInstantApp(pkg)) { |
| 3758 | // Ephemeral apps have some special constraints for notifications. |
| 3759 | // They are not allowed to create new notifications however they are allowed to |
| 3760 | // update notifications created by the system (e.g. a foreground service |
| 3761 | // notification). |
| 3762 | throw new SecurityException("Instant app " + pkg |
| 3763 | + " cannot create notifications"); |
| 3764 | } |
| 3765 | |
| 3766 | // rate limit updates that aren't completed progress notifications |
| 3767 | if (mNotificationsByKey.get(r.sbn.getKey()) != null |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3768 | && !r.getNotification().hasCompletedProgress() |
| 3769 | && !isAutogroup) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3770 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3771 | final float appEnqueueRate = mUsageStats.getAppEnqueueRate(pkg); |
| 3772 | if (appEnqueueRate > mMaxPackageEnqueueRate) { |
| 3773 | mUsageStats.registerOverRateQuota(pkg); |
| 3774 | final long now = SystemClock.elapsedRealtime(); |
| 3775 | if ((now - mLastOverRateLogTime) > MIN_PACKAGE_OVERRATE_LOG_INTERVAL) { |
| 3776 | Slog.e(TAG, "Package enqueue rate is " + appEnqueueRate |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3777 | + ". Shedding " + r.sbn.getKey() + ". package=" + pkg); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3778 | mLastOverRateLogTime = now; |
| 3779 | } |
| 3780 | return false; |
| 3781 | } |
| 3782 | } |
| 3783 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3784 | // limit the number of outstanding notificationrecords an app can have |
| 3785 | int count = getNotificationCountLocked(pkg, userId, id, tag); |
| 3786 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 3787 | mUsageStats.registerOverCountQuota(pkg); |
| 3788 | Slog.e(TAG, "Package has already posted or enqueued " + count |
| 3789 | + " notifications. Not showing more. package=" + pkg); |
| 3790 | return false; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3791 | } |
| 3792 | } |
| 3793 | } |
| 3794 | |
| 3795 | // snoozed apps |
| 3796 | if (mSnoozeHelper.isSnoozed(userId, pkg, r.getKey())) { |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3797 | MetricsLogger.action(r.getLogMaker() |
| 3798 | .setType(MetricsProto.MetricsEvent.TYPE_UPDATE) |
| 3799 | .setCategory(MetricsProto.MetricsEvent.NOTIFICATION_SNOOZED)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3800 | if (DBG) { |
| 3801 | Slog.d(TAG, "Ignored enqueue for snoozed notification " + r.getKey()); |
| 3802 | } |
| 3803 | mSnoozeHelper.update(userId, r); |
| 3804 | savePolicyFile(); |
| 3805 | return false; |
| 3806 | } |
| 3807 | |
| 3808 | |
| 3809 | // blocked apps |
| 3810 | if (isBlocked(r, mUsageStats)) { |
| 3811 | return false; |
| 3812 | } |
| 3813 | |
| 3814 | return true; |
| 3815 | } |
| 3816 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3817 | protected int getNotificationCountLocked(String pkg, int userId, int excludedId, |
| 3818 | String excludedTag) { |
| 3819 | int count = 0; |
| 3820 | final int N = mNotificationList.size(); |
| 3821 | for (int i = 0; i < N; i++) { |
| 3822 | final NotificationRecord existing = mNotificationList.get(i); |
| 3823 | if (existing.sbn.getPackageName().equals(pkg) |
| 3824 | && existing.sbn.getUserId() == userId) { |
| 3825 | if (existing.sbn.getId() == excludedId |
| 3826 | && TextUtils.equals(existing.sbn.getTag(), excludedTag)) { |
| 3827 | continue; |
| 3828 | } |
| 3829 | count++; |
| 3830 | } |
| 3831 | } |
| 3832 | final int M = mEnqueuedNotifications.size(); |
| 3833 | for (int i = 0; i < M; i++) { |
| 3834 | final NotificationRecord existing = mEnqueuedNotifications.get(i); |
| 3835 | if (existing.sbn.getPackageName().equals(pkg) |
| 3836 | && existing.sbn.getUserId() == userId) { |
| 3837 | count++; |
| 3838 | } |
| 3839 | } |
| 3840 | return count; |
| 3841 | } |
| 3842 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3843 | protected boolean isBlocked(NotificationRecord r, NotificationUsageStats usageStats) { |
| 3844 | final String pkg = r.sbn.getPackageName(); |
| 3845 | final int callingUid = r.sbn.getUid(); |
| 3846 | |
| 3847 | final boolean isPackageSuspended = isPackageSuspendedForUser(pkg, callingUid); |
| 3848 | if (isPackageSuspended) { |
| 3849 | Slog.e(TAG, "Suppressing notification from package due to package " |
| 3850 | + "suspended by administrator."); |
| 3851 | usageStats.registerSuspendedByAdmin(r); |
| 3852 | return isPackageSuspended; |
| 3853 | } |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 3854 | final boolean isBlocked = |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 3855 | mRankingHelper.isGroupBlocked(pkg, callingUid, r.getChannel().getGroup()) |
| 3856 | || mRankingHelper.getImportance(pkg, callingUid) |
| 3857 | == NotificationManager.IMPORTANCE_NONE |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 3858 | || r.getChannel().getImportance() == NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3859 | if (isBlocked) { |
| 3860 | Slog.e(TAG, "Suppressing notification from package by user request."); |
| 3861 | usageStats.registerBlocked(r); |
| 3862 | } |
| 3863 | return isBlocked; |
| 3864 | } |
| 3865 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3866 | protected class SnoozeNotificationRunnable implements Runnable { |
| 3867 | private final String mKey; |
| 3868 | private final long mDuration; |
| 3869 | private final String mSnoozeCriterionId; |
| 3870 | |
| 3871 | SnoozeNotificationRunnable(String key, long duration, String snoozeCriterionId) { |
| 3872 | mKey = key; |
| 3873 | mDuration = duration; |
| 3874 | mSnoozeCriterionId = snoozeCriterionId; |
| 3875 | } |
| 3876 | |
| 3877 | @Override |
| 3878 | public void run() { |
| 3879 | synchronized (mNotificationLock) { |
| 3880 | final NotificationRecord r = findNotificationByKeyLocked(mKey); |
| 3881 | if (r != null) { |
| 3882 | snoozeLocked(r); |
| 3883 | } |
| 3884 | } |
| 3885 | } |
| 3886 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3887 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3888 | void snoozeLocked(NotificationRecord r) { |
| 3889 | if (r.sbn.isGroup()) { |
| 3890 | final List<NotificationRecord> groupNotifications = findGroupNotificationsLocked( |
| 3891 | r.sbn.getPackageName(), r.sbn.getGroupKey(), r.sbn.getUserId()); |
| 3892 | if (r.getNotification().isGroupSummary()) { |
| 3893 | // snooze summary and all children |
| 3894 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 3895 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 3896 | } |
| 3897 | } else { |
| 3898 | // if there is a valid summary for this group, and we are snoozing the only |
| 3899 | // child, also snooze the summary |
| 3900 | if (mSummaryByGroupKey.containsKey(r.sbn.getGroupKey())) { |
| 3901 | if (groupNotifications.size() != 2) { |
| 3902 | snoozeNotificationLocked(r); |
| 3903 | } else { |
| 3904 | // snooze summary and the one child |
| 3905 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 3906 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 3907 | } |
| 3908 | } |
| 3909 | } else { |
| 3910 | snoozeNotificationLocked(r); |
| 3911 | } |
| 3912 | } |
| 3913 | } else { |
| 3914 | // just snooze the one notification |
| 3915 | snoozeNotificationLocked(r); |
| 3916 | } |
| 3917 | } |
| 3918 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3919 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3920 | void snoozeNotificationLocked(NotificationRecord r) { |
| 3921 | MetricsLogger.action(r.getLogMaker() |
| 3922 | .setCategory(MetricsEvent.NOTIFICATION_SNOOZED) |
| 3923 | .setType(MetricsEvent.TYPE_CLOSE) |
Chris Wren | 21a2e72 | 2017-10-02 17:44:53 -0400 | [diff] [blame] | 3924 | .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_SNOOZE_DURATION_MS, |
| 3925 | mDuration) |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3926 | .addTaggedData(MetricsEvent.NOTIFICATION_SNOOZED_CRITERIA, |
| 3927 | mSnoozeCriterionId == null ? 0 : 1)); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 3928 | boolean wasPosted = removeFromNotificationListsLocked(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 3929 | cancelNotificationLocked(r, false, REASON_SNOOZED, wasPosted, null); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3930 | updateLightsLocked(); |
| 3931 | if (mSnoozeCriterionId != null) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3932 | mAssistants.notifyAssistantSnoozedLocked(r.sbn, mSnoozeCriterionId); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3933 | mSnoozeHelper.snooze(r); |
| 3934 | } else { |
| 3935 | mSnoozeHelper.snooze(r, mDuration); |
| 3936 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3937 | r.recordSnoozed(); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3938 | savePolicyFile(); |
| 3939 | } |
| 3940 | } |
| 3941 | |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 3942 | protected class EnqueueNotificationRunnable implements Runnable { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3943 | private final NotificationRecord r; |
| 3944 | private final int userId; |
| 3945 | |
| 3946 | EnqueueNotificationRunnable(int userId, NotificationRecord r) { |
| 3947 | this.userId = userId; |
| 3948 | this.r = r; |
| 3949 | }; |
| 3950 | |
| 3951 | @Override |
| 3952 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3953 | synchronized (mNotificationLock) { |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 3954 | mEnqueuedNotifications.add(r); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 3955 | scheduleTimeoutLocked(r); |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 3956 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3957 | final StatusBarNotification n = r.sbn; |
| 3958 | if (DBG) Slog.d(TAG, "EnqueueNotificationRunnable.run for: " + n.getKey()); |
| 3959 | NotificationRecord old = mNotificationsByKey.get(n.getKey()); |
| 3960 | if (old != null) { |
| 3961 | // Retain ranking information from previous record |
| 3962 | r.copyRankingInformation(old); |
| 3963 | } |
| 3964 | |
| 3965 | final int callingUid = n.getUid(); |
| 3966 | final int callingPid = n.getInitialPid(); |
| 3967 | final Notification notification = n.getNotification(); |
| 3968 | final String pkg = n.getPackageName(); |
| 3969 | final int id = n.getId(); |
| 3970 | final String tag = n.getTag(); |
| 3971 | |
| 3972 | // Handle grouped notifications and bail out early if we |
| 3973 | // can to avoid extracting signals. |
| 3974 | handleGroupedNotificationLocked(r, old, callingUid, callingPid); |
| 3975 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3976 | // if this is a group child, unsnooze parent summary |
| 3977 | if (n.isGroup() && notification.isGroupChild()) { |
| 3978 | mSnoozeHelper.repostGroupSummary(pkg, r.getUserId(), n.getGroupKey()); |
| 3979 | } |
| 3980 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3981 | // This conditional is a dirty hack to limit the logging done on |
| 3982 | // behalf of the download manager without affecting other apps. |
| 3983 | if (!pkg.equals("com.android.providers.downloads") |
| 3984 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| 3985 | int enqueueStatus = EVENTLOG_ENQUEUE_STATUS_NEW; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3986 | if (old != null) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3987 | enqueueStatus = EVENTLOG_ENQUEUE_STATUS_UPDATE; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3988 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3989 | EventLogTags.writeNotificationEnqueue(callingUid, callingPid, |
| 3990 | pkg, id, tag, userId, notification.toString(), |
| 3991 | enqueueStatus); |
| 3992 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3993 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3994 | mRankingHelper.extractSignals(r); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3995 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3996 | // tell the assistant service about the notification |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3997 | if (mAssistants.isEnabled()) { |
| 3998 | mAssistants.onNotificationEnqueued(r); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3999 | mHandler.postDelayed(new PostNotificationRunnable(r.getKey()), |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4000 | DELAY_FOR_ASSISTANT_TIME); |
| 4001 | } else { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4002 | mHandler.post(new PostNotificationRunnable(r.getKey())); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4003 | } |
| 4004 | } |
| 4005 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4006 | } |
| 4007 | |
| 4008 | protected class PostNotificationRunnable implements Runnable { |
| 4009 | private final String key; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4010 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4011 | PostNotificationRunnable(String key) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4012 | this.key = key; |
| 4013 | } |
| 4014 | |
| 4015 | @Override |
| 4016 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4017 | synchronized (mNotificationLock) { |
| 4018 | try { |
| 4019 | NotificationRecord r = null; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4020 | int N = mEnqueuedNotifications.size(); |
| 4021 | for (int i = 0; i < N; i++) { |
| 4022 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 4023 | if (Objects.equals(key, enqueued.getKey())) { |
| 4024 | r = enqueued; |
| 4025 | break; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4026 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4027 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4028 | if (r == null) { |
| 4029 | Slog.i(TAG, "Cannot find enqueued record for key: " + key); |
| 4030 | return; |
| 4031 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4032 | NotificationRecord old = mNotificationsByKey.get(key); |
| 4033 | final StatusBarNotification n = r.sbn; |
| 4034 | final Notification notification = n.getNotification(); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4035 | int index = indexOfNotificationLocked(n.getKey()); |
| 4036 | if (index < 0) { |
| 4037 | mNotificationList.add(r); |
| 4038 | mUsageStats.registerPostedByApp(r); |
| 4039 | } else { |
| 4040 | old = mNotificationList.get(index); |
| 4041 | mNotificationList.set(index, r); |
| 4042 | mUsageStats.registerUpdatedByApp(r, old); |
| 4043 | // Make sure we don't lose the foreground service state. |
| 4044 | notification.flags |= |
| 4045 | old.getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE; |
| 4046 | r.isUpdate = true; |
| 4047 | } |
| 4048 | |
| 4049 | mNotificationsByKey.put(n.getKey(), r); |
| 4050 | |
| 4051 | // Ensure if this is a foreground service that the proper additional |
| 4052 | // flags are set. |
| 4053 | if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 4054 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 4055 | | Notification.FLAG_NO_CLEAR; |
| 4056 | } |
| 4057 | |
| 4058 | applyZenModeLocked(r); |
| 4059 | mRankingHelper.sort(mNotificationList); |
| 4060 | |
| 4061 | if (notification.getSmallIcon() != null) { |
| 4062 | StatusBarNotification oldSbn = (old != null) ? old.sbn : null; |
| 4063 | mListeners.notifyPostedLocked(n, oldSbn); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 4064 | if (oldSbn == null || !Objects.equals(oldSbn.getGroup(), n.getGroup())) { |
| 4065 | mHandler.post(new Runnable() { |
| 4066 | @Override |
| 4067 | public void run() { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 4068 | mGroupHelper.onNotificationPosted( |
| 4069 | n, hasAutoGroupSummaryLocked(n)); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 4070 | } |
| 4071 | }); |
| 4072 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4073 | } else { |
| 4074 | Slog.e(TAG, "Not posting notification without small icon: " + notification); |
| 4075 | if (old != null && !old.isCanceled) { |
| 4076 | mListeners.notifyRemovedLocked(n, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4077 | NotificationListenerService.REASON_ERROR, null); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4078 | mHandler.post(new Runnable() { |
| 4079 | @Override |
| 4080 | public void run() { |
| 4081 | mGroupHelper.onNotificationRemoved(n); |
| 4082 | } |
| 4083 | }); |
| 4084 | } |
| 4085 | // ATTENTION: in a future release we will bail out here |
| 4086 | // so that we do not play sounds, show lights, etc. for invalid |
| 4087 | // notifications |
| 4088 | Slog.e(TAG, "WARNING: In a future release this will crash the app: " |
| 4089 | + n.getPackageName()); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4090 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4091 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4092 | buzzBeepBlinkLocked(r); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4093 | } finally { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4094 | int N = mEnqueuedNotifications.size(); |
| 4095 | for (int i = 0; i < N; i++) { |
| 4096 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 4097 | if (Objects.equals(key, enqueued.getKey())) { |
| 4098 | mEnqueuedNotifications.remove(i); |
| 4099 | break; |
| 4100 | } |
| 4101 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4102 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4103 | } |
| 4104 | } |
| 4105 | } |
| 4106 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4107 | /** |
| 4108 | * Ensures that grouped notification receive their special treatment. |
| 4109 | * |
| 4110 | * <p>Cancels group children if the new notification causes a group to lose |
| 4111 | * its summary.</p> |
| 4112 | * |
| 4113 | * <p>Updates mSummaryByGroupKey.</p> |
| 4114 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4115 | @GuardedBy("mNotificationLock") |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4116 | private void handleGroupedNotificationLocked(NotificationRecord r, NotificationRecord old, |
| 4117 | int callingUid, int callingPid) { |
| 4118 | StatusBarNotification sbn = r.sbn; |
| 4119 | Notification n = sbn.getNotification(); |
Selim Cinek | 5b03ce9 | 2016-05-18 15:16:58 -0700 | [diff] [blame] | 4120 | if (n.isGroupSummary() && !sbn.isAppGroup()) { |
| 4121 | // notifications without a group shouldn't be a summary, otherwise autobundling can |
| 4122 | // lead to bugs |
| 4123 | n.flags &= ~Notification.FLAG_GROUP_SUMMARY; |
| 4124 | } |
| 4125 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4126 | String group = sbn.getGroupKey(); |
| 4127 | boolean isSummary = n.isGroupSummary(); |
| 4128 | |
| 4129 | Notification oldN = old != null ? old.sbn.getNotification() : null; |
| 4130 | String oldGroup = old != null ? old.sbn.getGroupKey() : null; |
| 4131 | boolean oldIsSummary = old != null && oldN.isGroupSummary(); |
| 4132 | |
| 4133 | if (oldIsSummary) { |
| 4134 | NotificationRecord removedSummary = mSummaryByGroupKey.remove(oldGroup); |
| 4135 | if (removedSummary != old) { |
| 4136 | String removedKey = |
| 4137 | removedSummary != null ? removedSummary.getKey() : "<null>"; |
| 4138 | Slog.w(TAG, "Removed summary didn't match old notification: old=" + old.getKey() + |
| 4139 | ", removed=" + removedKey); |
| 4140 | } |
| 4141 | } |
| 4142 | if (isSummary) { |
| 4143 | mSummaryByGroupKey.put(group, r); |
| 4144 | } |
| 4145 | |
| 4146 | // Clear out group children of the old notification if the update |
| 4147 | // causes the group summary to go away. This happens when the old |
| 4148 | // notification was a summary and the new one isn't, or when the old |
| 4149 | // notification was a summary and its group key changed. |
| 4150 | if (oldIsSummary && (!isSummary || !oldGroup.equals(group))) { |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 4151 | cancelGroupChildrenLocked(old, callingUid, callingPid, null, false /* sendDelete */, |
| 4152 | null); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4153 | } |
| 4154 | } |
| 4155 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4156 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4157 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4158 | void scheduleTimeoutLocked(NotificationRecord record) { |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 4159 | if (record.getNotification().getTimeoutAfter() > 0) { |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4160 | final PendingIntent pi = PendingIntent.getBroadcast(getContext(), |
| 4161 | REQUEST_CODE_TIMEOUT, |
| 4162 | new Intent(ACTION_NOTIFICATION_TIMEOUT) |
| 4163 | .setData(new Uri.Builder().scheme(SCHEME_TIMEOUT) |
| 4164 | .appendPath(record.getKey()).build()) |
| 4165 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 4166 | .putExtra(EXTRA_KEY, record.getKey()), |
| 4167 | PendingIntent.FLAG_UPDATE_CURRENT); |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 4168 | mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 4169 | SystemClock.elapsedRealtime() + record.getNotification().getTimeoutAfter(), pi); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4170 | } |
| 4171 | } |
| 4172 | |
| 4173 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4174 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4175 | void buzzBeepBlinkLocked(NotificationRecord record) { |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4176 | boolean buzz = false; |
| 4177 | boolean beep = false; |
| 4178 | boolean blink = false; |
| 4179 | |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4180 | final Notification notification = record.sbn.getNotification(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4181 | final String key = record.getKey(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4182 | |
| 4183 | // Should this notification make noise, vibe, or use the LED? |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 4184 | final boolean aboveThreshold = |
| 4185 | record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4186 | |
| 4187 | // Remember if this notification already owns the notification channels. |
| 4188 | boolean wasBeep = key != null && key.equals(mSoundNotificationKey); |
| 4189 | boolean wasBuzz = key != null && key.equals(mVibrateNotificationKey); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4190 | // These are set inside the conditional if the notification is allowed to make noise. |
| 4191 | boolean hasValidVibrate = false; |
| 4192 | boolean hasValidSound = false; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4193 | boolean sentAccessibilityEvent = false; |
| 4194 | // If the notification will appear in the status bar, it should send an accessibility |
| 4195 | // event |
| 4196 | if (!record.isUpdate && record.getImportance() > IMPORTANCE_MIN) { |
| 4197 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
| 4198 | sentAccessibilityEvent = true; |
| 4199 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4200 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4201 | if (aboveThreshold && isNotificationForCurrentUser(record)) { |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4202 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4203 | if (mSystemReady && mAudioManager != null) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4204 | Uri soundUri = record.getSound(); |
| 4205 | hasValidSound = soundUri != null && !Uri.EMPTY.equals(soundUri); |
| 4206 | long[] vibration = record.getVibration(); |
| 4207 | // Demote sound to vibration if vibration missing & phone in vibration mode. |
| 4208 | if (vibration == null |
| 4209 | && hasValidSound |
| 4210 | && (mAudioManager.getRingerModeInternal() |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 4211 | == AudioManager.RINGER_MODE_VIBRATE) |
| 4212 | && mAudioManager.getStreamVolume( |
| 4213 | AudioAttributes.toLegacyStreamType(record.getAudioAttributes())) == 0) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4214 | vibration = mFallbackVibrationPattern; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4215 | } |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4216 | hasValidVibrate = vibration != null; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4217 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4218 | boolean hasAudibleAlert = hasValidSound || hasValidVibrate; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4219 | if (hasAudibleAlert && !shouldMuteNotificationLocked(record)) { |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4220 | if (!sentAccessibilityEvent) { |
| 4221 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
| 4222 | sentAccessibilityEvent = true; |
| 4223 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4224 | if (DBG) Slog.v(TAG, "Interrupting!"); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4225 | if (hasValidSound) { |
| 4226 | mSoundNotificationKey = key; |
| 4227 | if (mInCall) { |
| 4228 | playInCallNotification(); |
| 4229 | beep = true; |
| 4230 | } else { |
| 4231 | beep = playSound(record, soundUri); |
| 4232 | } |
| 4233 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4234 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4235 | final boolean ringerModeSilent = |
| 4236 | mAudioManager.getRingerModeInternal() |
| 4237 | == AudioManager.RINGER_MODE_SILENT; |
| 4238 | if (!mInCall && hasValidVibrate && !ringerModeSilent) { |
| 4239 | mVibrateNotificationKey = key; |
| 4240 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4241 | buzz = playVibration(record, vibration, hasValidSound); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4242 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4243 | } |
| 4244 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4245 | } |
| 4246 | // If a notification is updated to remove the actively playing sound or vibrate, |
| 4247 | // cancel that feedback now |
| 4248 | if (wasBeep && !hasValidSound) { |
| 4249 | clearSoundLocked(); |
| 4250 | } |
| 4251 | if (wasBuzz && !hasValidVibrate) { |
| 4252 | clearVibrateLocked(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | // light |
| 4256 | // release the light |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4257 | boolean wasShowLights = mLights.remove(key); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 4258 | if (record.getLight() != null && aboveThreshold |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4259 | && ((record.getSuppressedVisualEffects() |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 4260 | & NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF) == 0)) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4261 | mLights.add(key); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4262 | updateLightsLocked(); |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 4263 | if (mUseAttentionLight) { |
| 4264 | mAttentionLight.pulse(); |
| 4265 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4266 | blink = true; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4267 | } else if (wasShowLights) { |
| 4268 | updateLightsLocked(); |
| 4269 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4270 | if (buzz || beep || blink) { |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 4271 | MetricsLogger.action(record.getLogMaker() |
| 4272 | .setCategory(MetricsEvent.NOTIFICATION_ALERT) |
| 4273 | .setType(MetricsEvent.TYPE_OPEN) |
| 4274 | .setSubtype((buzz ? 1 : 0) | (beep ? 2 : 0) | (blink ? 4 : 0))); |
| 4275 | EventLogTags.writeNotificationAlert(key, buzz ? 1 : 0, beep ? 1 : 0, blink ? 1 : 0); |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 4276 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4277 | } |
| 4278 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4279 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4280 | boolean shouldMuteNotificationLocked(final NotificationRecord record) { |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4281 | // Suppressed because it's a silent update |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4282 | final Notification notification = record.getNotification(); |
| 4283 | if(record.isUpdate |
| 4284 | && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) { |
| 4285 | return true; |
| 4286 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4287 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4288 | // muted by listener |
| 4289 | final String disableEffects = disableNotificationEffects(record); |
| 4290 | if (disableEffects != null) { |
| 4291 | ZenLog.traceDisableEffects(record, disableEffects); |
| 4292 | return true; |
| 4293 | } |
| 4294 | |
| 4295 | // suppressed due to DND |
| 4296 | if (record.isIntercepted()) { |
| 4297 | return true; |
| 4298 | } |
| 4299 | |
| 4300 | // Suppressed because another notification in its group handles alerting |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4301 | if (record.sbn.isGroup()) { |
Julia Reynolds | 3020315 | 2017-05-26 13:36:31 -0400 | [diff] [blame] | 4302 | return notification.suppressAlertingDueToGrouping(); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4303 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4304 | |
Julia Reynolds | 65b85cf | 2017-07-20 09:19:20 -0400 | [diff] [blame] | 4305 | // Suppressed for being too recently noisy |
| 4306 | final String pkg = record.sbn.getPackageName(); |
| 4307 | if (mUsageStats.isAlertRateLimited(pkg)) { |
| 4308 | Slog.e(TAG, "Muting recently noisy " + record.getKey()); |
| 4309 | return true; |
| 4310 | } |
| 4311 | |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4312 | return false; |
| 4313 | } |
| 4314 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 4315 | private boolean playSound(final NotificationRecord record, Uri soundUri) { |
| 4316 | boolean looping = (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 4317 | // 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] | 4318 | // or the device is in vibrate mode |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 4319 | if (!mAudioManager.isAudioFocusExclusive() && (mAudioManager.getRingerModeInternal() |
| 4320 | != AudioManager.RINGER_MODE_VIBRATE || mAudioManager.getStreamVolume( |
| 4321 | AudioAttributes.toLegacyStreamType(record.getAudioAttributes())) != 0)) { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 4322 | final long identity = Binder.clearCallingIdentity(); |
| 4323 | try { |
| 4324 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4325 | if (player != null) { |
| 4326 | if (DBG) Slog.v(TAG, "Playing sound " + soundUri |
| 4327 | + " with attributes " + record.getAudioAttributes()); |
| 4328 | player.playAsync(soundUri, record.sbn.getUser(), looping, |
| 4329 | record.getAudioAttributes()); |
| 4330 | return true; |
| 4331 | } |
| 4332 | } catch (RemoteException e) { |
| 4333 | } finally { |
| 4334 | Binder.restoreCallingIdentity(identity); |
| 4335 | } |
| 4336 | } |
| 4337 | return false; |
| 4338 | } |
| 4339 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4340 | private boolean playVibration(final NotificationRecord record, long[] vibration, |
| 4341 | boolean delayVibForSound) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 4342 | // Escalate privileges so we can use the vibrator even if the |
| 4343 | // notifying app does not have the VIBRATE permission. |
| 4344 | long identity = Binder.clearCallingIdentity(); |
| 4345 | try { |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4346 | final VibrationEffect effect; |
| 4347 | try { |
| 4348 | final boolean insistent = |
| 4349 | (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 4350 | effect = VibrationEffect.createWaveform( |
| 4351 | vibration, insistent ? 0 : -1 /*repeatIndex*/); |
| 4352 | } catch (IllegalArgumentException e) { |
| 4353 | Slog.e(TAG, "Error creating vibration waveform with pattern: " + |
| 4354 | Arrays.toString(vibration)); |
| 4355 | return false; |
| 4356 | } |
| 4357 | if (delayVibForSound) { |
| 4358 | new Thread(() -> { |
| 4359 | // delay the vibration by the same amount as the notification sound |
| 4360 | final int waitMs = mAudioManager.getFocusRampTimeMs( |
| 4361 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 4362 | record.getAudioAttributes()); |
| 4363 | if (DBG) Slog.v(TAG, "Delaying vibration by " + waitMs + "ms"); |
| 4364 | try { |
| 4365 | Thread.sleep(waitMs); |
| 4366 | } catch (InterruptedException e) { } |
| 4367 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 4368 | effect, record.getAudioAttributes()); |
| 4369 | }).start(); |
| 4370 | } else { |
| 4371 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 4372 | effect, record.getAudioAttributes()); |
| 4373 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 4374 | return true; |
| 4375 | } finally{ |
| 4376 | Binder.restoreCallingIdentity(identity); |
| 4377 | } |
| 4378 | } |
| 4379 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4380 | private boolean isNotificationForCurrentUser(NotificationRecord record) { |
| 4381 | final int currentUser; |
| 4382 | final long token = Binder.clearCallingIdentity(); |
| 4383 | try { |
| 4384 | currentUser = ActivityManager.getCurrentUser(); |
| 4385 | } finally { |
| 4386 | Binder.restoreCallingIdentity(token); |
| 4387 | } |
| 4388 | return (record.getUserId() == UserHandle.USER_ALL || |
| 4389 | record.getUserId() == currentUser || |
| 4390 | mUserProfiles.isCurrentProfile(record.getUserId())); |
| 4391 | } |
| 4392 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4393 | protected void playInCallNotification() { |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4394 | new Thread() { |
| 4395 | @Override |
| 4396 | public void run() { |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4397 | final long identity = Binder.clearCallingIdentity(); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4398 | try { |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4399 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4400 | if (player != null) { |
| 4401 | player.play(new Binder(), mInCallNotificationUri, |
| 4402 | mInCallNotificationAudioAttributes, |
| 4403 | mInCallNotificationVolume, false); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4404 | } |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4405 | } catch (RemoteException e) { |
| 4406 | } finally { |
| 4407 | Binder.restoreCallingIdentity(identity); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4408 | } |
| 4409 | } |
| 4410 | }.start(); |
| 4411 | } |
| 4412 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4413 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4414 | void showNextToastLocked() { |
| 4415 | ToastRecord record = mToastQueue.get(0); |
| 4416 | while (record != null) { |
| 4417 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| 4418 | try { |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4419 | record.callback.show(record.token); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4420 | scheduleTimeoutLocked(record); |
| 4421 | return; |
| 4422 | } catch (RemoteException e) { |
| 4423 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| 4424 | + " in package " + record.pkg); |
| 4425 | // remove it from the list and let the process die |
| 4426 | int index = mToastQueue.indexOf(record); |
| 4427 | if (index >= 0) { |
| 4428 | mToastQueue.remove(index); |
| 4429 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4430 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4431 | if (mToastQueue.size() > 0) { |
| 4432 | record = mToastQueue.get(0); |
| 4433 | } else { |
| 4434 | record = null; |
| 4435 | } |
| 4436 | } |
| 4437 | } |
| 4438 | } |
| 4439 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4440 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4441 | void cancelToastLocked(int index) { |
| 4442 | ToastRecord record = mToastQueue.get(index); |
| 4443 | try { |
| 4444 | record.callback.hide(); |
| 4445 | } catch (RemoteException e) { |
| 4446 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| 4447 | + " in package " + record.pkg); |
| 4448 | // don't worry about this, we're about to remove it from |
| 4449 | // the list anyway |
| 4450 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4451 | |
| 4452 | ToastRecord lastToast = mToastQueue.remove(index); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 4453 | mWindowManagerInternal.removeWindowToken(lastToast.token, true, DEFAULT_DISPLAY); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4454 | |
| 4455 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4456 | if (mToastQueue.size() > 0) { |
| 4457 | // Show the next one. If the callback fails, this will remove |
| 4458 | // it from the list, so don't assume that the list hasn't changed |
| 4459 | // after this point. |
| 4460 | showNextToastLocked(); |
| 4461 | } |
| 4462 | } |
| 4463 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4464 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4465 | private void scheduleTimeoutLocked(ToastRecord r) |
| 4466 | { |
| 4467 | mHandler.removeCallbacksAndMessages(r); |
| 4468 | Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r); |
| 4469 | long delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY; |
| 4470 | mHandler.sendMessageDelayed(m, delay); |
| 4471 | } |
| 4472 | |
| 4473 | private void handleTimeout(ToastRecord record) |
| 4474 | { |
| 4475 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| 4476 | synchronized (mToastQueue) { |
| 4477 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 4478 | if (index >= 0) { |
| 4479 | cancelToastLocked(index); |
| 4480 | } |
| 4481 | } |
| 4482 | } |
| 4483 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4484 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4485 | int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 4486 | { |
| 4487 | IBinder cbak = callback.asBinder(); |
| 4488 | ArrayList<ToastRecord> list = mToastQueue; |
| 4489 | int len = list.size(); |
| 4490 | for (int i=0; i<len; i++) { |
| 4491 | ToastRecord r = list.get(i); |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 4492 | if (r.pkg.equals(pkg) && r.callback.asBinder().equals(cbak)) { |
| 4493 | return i; |
| 4494 | } |
| 4495 | } |
| 4496 | return -1; |
| 4497 | } |
| 4498 | |
| 4499 | @GuardedBy("mToastQueue") |
| 4500 | int indexOfToastPackageLocked(String pkg) |
| 4501 | { |
| 4502 | ArrayList<ToastRecord> list = mToastQueue; |
| 4503 | int len = list.size(); |
| 4504 | for (int i=0; i<len; i++) { |
| 4505 | ToastRecord r = list.get(i); |
| 4506 | if (r.pkg.equals(pkg)) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4507 | return i; |
| 4508 | } |
| 4509 | } |
| 4510 | return -1; |
| 4511 | } |
| 4512 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4513 | @GuardedBy("mToastQueue") |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4514 | void keepProcessAliveIfNeededLocked(int pid) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4515 | { |
| 4516 | int toastCount = 0; // toasts from this pid |
| 4517 | ArrayList<ToastRecord> list = mToastQueue; |
| 4518 | int N = list.size(); |
| 4519 | for (int i=0; i<N; i++) { |
| 4520 | ToastRecord r = list.get(i); |
| 4521 | if (r.pid == pid) { |
| 4522 | toastCount++; |
| 4523 | } |
| 4524 | } |
| 4525 | try { |
Dianne Hackborn | f965f40 | 2017-05-04 23:27:23 -0700 | [diff] [blame] | 4526 | mAm.setProcessImportant(mForegroundToken, pid, toastCount > 0, "toast"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4527 | } catch (RemoteException e) { |
| 4528 | // Shouldn't happen. |
| 4529 | } |
| 4530 | } |
| 4531 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4532 | private void handleRankingReconsideration(Message message) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4533 | if (!(message.obj instanceof RankingReconsideration)) return; |
| 4534 | RankingReconsideration recon = (RankingReconsideration) message.obj; |
| 4535 | recon.run(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4536 | boolean changed; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4537 | synchronized (mNotificationLock) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4538 | final NotificationRecord record = mNotificationsByKey.get(recon.getKey()); |
| 4539 | if (record == null) { |
| 4540 | return; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4541 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4542 | int indexBefore = findNotificationRecordIndexLocked(record); |
| 4543 | boolean interceptBefore = record.isIntercepted(); |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4544 | float contactAffinityBefore = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4545 | int visibilityBefore = record.getPackageVisibilityOverride(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4546 | recon.applyChangesLocked(record); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4547 | applyZenModeLocked(record); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4548 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4549 | int indexAfter = findNotificationRecordIndexLocked(record); |
| 4550 | boolean interceptAfter = record.isIntercepted(); |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4551 | float contactAffinityAfter = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4552 | int visibilityAfter = record.getPackageVisibilityOverride(); |
| 4553 | changed = indexBefore != indexAfter || interceptBefore != interceptAfter |
| 4554 | || visibilityBefore != visibilityAfter; |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4555 | if (interceptBefore && !interceptAfter |
| 4556 | && Float.compare(contactAffinityBefore, contactAffinityAfter) != 0) { |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4557 | buzzBeepBlinkLocked(record); |
| 4558 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4559 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4560 | if (changed) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4561 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4562 | } |
| 4563 | } |
| 4564 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4565 | void handleRankingSort() { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 4566 | if (mRankingHelper == null) return; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4567 | synchronized (mNotificationLock) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4568 | final int N = mNotificationList.size(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4569 | // Any field that can change via one of the extractors needs to be added here. |
| 4570 | ArrayList<String> orderBefore = new ArrayList<>(N); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4571 | int[] visibilities = new int[N]; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4572 | boolean[] showBadges = new boolean[N]; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4573 | ArrayList<NotificationChannel> channelBefore = new ArrayList<>(N); |
| 4574 | ArrayList<String> groupKeyBefore = new ArrayList<>(N); |
| 4575 | ArrayList<ArrayList<String>> overridePeopleBefore = new ArrayList<>(N); |
| 4576 | ArrayList<ArrayList<SnoozeCriterion>> snoozeCriteriaBefore = new ArrayList<>(N); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4577 | ArrayList<Integer> userSentimentBefore = new ArrayList<>(N); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4578 | for (int i = 0; i < N; i++) { |
| 4579 | final NotificationRecord r = mNotificationList.get(i); |
| 4580 | orderBefore.add(r.getKey()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4581 | visibilities[i] = r.getPackageVisibilityOverride(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4582 | showBadges[i] = r.canShowBadge(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4583 | channelBefore.add(r.getChannel()); |
| 4584 | groupKeyBefore.add(r.getGroupKey()); |
| 4585 | overridePeopleBefore.add(r.getPeopleOverride()); |
| 4586 | snoozeCriteriaBefore.add(r.getSnoozeCriteria()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4587 | userSentimentBefore.add(r.getUserSentiment()); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4588 | mRankingHelper.extractSignals(r); |
| 4589 | } |
Chris Wren | 19a02b0 | 2015-12-22 10:34:22 -0500 | [diff] [blame] | 4590 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4591 | for (int i = 0; i < N; i++) { |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4592 | final NotificationRecord r = mNotificationList.get(i); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4593 | if (!orderBefore.get(i).equals(r.getKey()) |
Julia Reynolds | 6976669 | 2016-02-01 15:35:08 -0500 | [diff] [blame] | 4594 | || visibilities[i] != r.getPackageVisibilityOverride() |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4595 | || showBadges[i] != r.canShowBadge() |
| 4596 | || !Objects.equals(channelBefore.get(i), r.getChannel()) |
| 4597 | || !Objects.equals(groupKeyBefore.get(i), r.getGroupKey()) |
| 4598 | || !Objects.equals(overridePeopleBefore.get(i), r.getPeopleOverride()) |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4599 | || !Objects.equals(snoozeCriteriaBefore.get(i), r.getSnoozeCriteria()) |
| 4600 | || !Objects.equals(userSentimentBefore.get(i), r.getUserSentiment())) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4601 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4602 | return; |
| 4603 | } |
| 4604 | } |
| 4605 | } |
| 4606 | } |
| 4607 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4608 | @GuardedBy("mNotificationLock") |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 4609 | private void recordCallerLocked(NotificationRecord record) { |
| 4610 | if (mZenModeHelper.isCall(record)) { |
| 4611 | mZenModeHelper.recordCaller(record); |
| 4612 | } |
| 4613 | } |
| 4614 | |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 4615 | // let zen mode evaluate this record |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4616 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4617 | private void applyZenModeLocked(NotificationRecord record) { |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 4618 | record.setIntercepted(mZenModeHelper.shouldIntercept(record)); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4619 | if (record.isIntercepted()) { |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 4620 | int suppressed = (mZenModeHelper.shouldSuppressWhenScreenOff() |
| 4621 | ? SUPPRESSED_EFFECT_SCREEN_OFF : 0) |
| 4622 | | (mZenModeHelper.shouldSuppressWhenScreenOn() |
| 4623 | ? SUPPRESSED_EFFECT_SCREEN_ON : 0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4624 | record.setSuppressedVisualEffects(suppressed); |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 4625 | } else { |
| 4626 | record.setSuppressedVisualEffects(0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4627 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4628 | } |
| 4629 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4630 | @GuardedBy("mNotificationLock") |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4631 | private int findNotificationRecordIndexLocked(NotificationRecord target) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4632 | return mRankingHelper.indexOf(mNotificationList, target); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4633 | } |
| 4634 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4635 | private void handleSendRankingUpdate() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4636 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4637 | mListeners.notifyRankingUpdateLocked(); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4638 | } |
| 4639 | } |
| 4640 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4641 | private void scheduleListenerHintsChanged(int state) { |
| 4642 | mHandler.removeMessages(MESSAGE_LISTENER_HINTS_CHANGED); |
| 4643 | mHandler.obtainMessage(MESSAGE_LISTENER_HINTS_CHANGED, state, 0).sendToTarget(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4644 | } |
| 4645 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4646 | private void scheduleInterruptionFilterChanged(int listenerInterruptionFilter) { |
| 4647 | mHandler.removeMessages(MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED); |
| 4648 | mHandler.obtainMessage( |
| 4649 | MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED, |
| 4650 | listenerInterruptionFilter, |
| 4651 | 0).sendToTarget(); |
| 4652 | } |
| 4653 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4654 | private void handleListenerHintsChanged(int hints) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4655 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4656 | mListeners.notifyListenerHintsChangedLocked(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4657 | } |
| 4658 | } |
| 4659 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4660 | private void handleListenerInterruptionFilterChanged(int interruptionFilter) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4661 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4662 | mListeners.notifyInterruptionFilterChanged(interruptionFilter); |
| 4663 | } |
| 4664 | } |
| 4665 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4666 | protected class WorkerHandler extends Handler |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4667 | { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4668 | public WorkerHandler(Looper looper) { |
| 4669 | super(looper); |
| 4670 | } |
| 4671 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4672 | @Override |
| 4673 | public void handleMessage(Message msg) |
| 4674 | { |
| 4675 | switch (msg.what) |
| 4676 | { |
| 4677 | case MESSAGE_TIMEOUT: |
| 4678 | handleTimeout((ToastRecord)msg.obj); |
| 4679 | break; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 4680 | case MESSAGE_SAVE_POLICY_FILE: |
| 4681 | handleSavePolicyFile(); |
| 4682 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4683 | case MESSAGE_SEND_RANKING_UPDATE: |
| 4684 | handleSendRankingUpdate(); |
| 4685 | break; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4686 | case MESSAGE_LISTENER_HINTS_CHANGED: |
| 4687 | handleListenerHintsChanged(msg.arg1); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4688 | break; |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4689 | case MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED: |
| 4690 | handleListenerInterruptionFilterChanged(msg.arg1); |
| 4691 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4692 | } |
| 4693 | } |
| 4694 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4695 | protected void scheduleSendRankingUpdate() { |
| 4696 | if (!hasMessages(MESSAGE_SEND_RANKING_UPDATE)) { |
| 4697 | Message m = Message.obtain(this, MESSAGE_SEND_RANKING_UPDATE); |
| 4698 | sendMessage(m); |
| 4699 | } |
| 4700 | } |
| 4701 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4702 | } |
| 4703 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4704 | private final class RankingHandlerWorker extends Handler implements RankingHandler |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4705 | { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4706 | public RankingHandlerWorker(Looper looper) { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4707 | super(looper); |
| 4708 | } |
| 4709 | |
| 4710 | @Override |
| 4711 | public void handleMessage(Message msg) { |
| 4712 | switch (msg.what) { |
| 4713 | case MESSAGE_RECONSIDER_RANKING: |
| 4714 | handleRankingReconsideration(msg); |
| 4715 | break; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4716 | case MESSAGE_RANKING_SORT: |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4717 | handleRankingSort(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4718 | break; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4719 | } |
| 4720 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4721 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4722 | public void requestSort() { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4723 | removeMessages(MESSAGE_RANKING_SORT); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4724 | Message msg = Message.obtain(); |
| 4725 | msg.what = MESSAGE_RANKING_SORT; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4726 | sendMessage(msg); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4727 | } |
| 4728 | |
| 4729 | public void requestReconsideration(RankingReconsideration recon) { |
| 4730 | Message m = Message.obtain(this, |
| 4731 | NotificationManagerService.MESSAGE_RECONSIDER_RANKING, recon); |
| 4732 | long delay = recon.getDelay(TimeUnit.MILLISECONDS); |
| 4733 | sendMessageDelayed(m, delay); |
| 4734 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4735 | } |
| 4736 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4737 | // Notifications |
| 4738 | // ============================================================================ |
| 4739 | static int clamp(int x, int low, int high) { |
| 4740 | return (x < low) ? low : ((x > high) ? high : x); |
| 4741 | } |
| 4742 | |
| 4743 | void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
Eugene Susla | d4128ec | 2017-12-04 19:48:41 +0000 | [diff] [blame] | 4744 | if (!mAccessibilityManager.isEnabled()) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 4745 | return; |
| 4746 | } |
| 4747 | |
| 4748 | AccessibilityEvent event = |
| 4749 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 4750 | event.setPackageName(packageName); |
| 4751 | event.setClassName(Notification.class.getName()); |
| 4752 | event.setParcelableData(notification); |
| 4753 | CharSequence tickerText = notification.tickerText; |
| 4754 | if (!TextUtils.isEmpty(tickerText)) { |
| 4755 | event.getText().add(tickerText); |
| 4756 | } |
| 4757 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4758 | mAccessibilityManager.sendAccessibilityEvent(event); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 4759 | } |
| 4760 | |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4761 | /** |
| 4762 | * Removes all NotificationsRecords with the same key as the given notification record |
| 4763 | * from both lists. Do not call this method while iterating over either list. |
| 4764 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4765 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4766 | private boolean removeFromNotificationListsLocked(NotificationRecord r) { |
| 4767 | // Remove from both lists, either list could have a separate Record for what is |
| 4768 | // effectively the same notification. |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4769 | boolean wasPosted = false; |
| 4770 | NotificationRecord recordInList = null; |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4771 | if ((recordInList = findNotificationByListLocked(mNotificationList, r.getKey())) |
| 4772 | != null) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4773 | mNotificationList.remove(recordInList); |
| 4774 | mNotificationsByKey.remove(recordInList.sbn.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4775 | wasPosted = true; |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4776 | } |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4777 | while ((recordInList = findNotificationByListLocked(mEnqueuedNotifications, r.getKey())) |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4778 | != null) { |
| 4779 | mEnqueuedNotifications.remove(recordInList); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4780 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4781 | return wasPosted; |
| 4782 | } |
| 4783 | |
| 4784 | @GuardedBy("mNotificationLock") |
| 4785 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason, |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4786 | boolean wasPosted, String listenerName) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4787 | final String canceledKey = r.getKey(); |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 4788 | |
| 4789 | // Record caller. |
| 4790 | recordCallerLocked(r); |
| 4791 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4792 | if (r.getStats().getDismissalSurface() == NotificationStats.DISMISSAL_NOT_DISMISSED) { |
| 4793 | r.recordDismissalSurface(NotificationStats.DISMISSAL_OTHER); |
| 4794 | } |
| 4795 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4796 | // tell the app |
| 4797 | if (sendDelete) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4798 | if (r.getNotification().deleteIntent != null) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4799 | try { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4800 | r.getNotification().deleteIntent.send(); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4801 | } catch (PendingIntent.CanceledException ex) { |
| 4802 | // do nothing - there's no relevant way to recover, and |
| 4803 | // no reason to let this propagate |
Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 4804 | Slog.w(TAG, "canceled PendingIntent for " + r.sbn.getPackageName(), ex); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4805 | } |
| 4806 | } |
| 4807 | } |
| 4808 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4809 | // Only cancel these if this notification actually got to be posted. |
| 4810 | if (wasPosted) { |
| 4811 | // status bar |
| 4812 | if (r.getNotification().getSmallIcon() != null) { |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 4813 | if (reason != REASON_SNOOZED) { |
| 4814 | r.isCanceled = true; |
| 4815 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4816 | mListeners.notifyRemovedLocked(r.sbn, reason, r.getStats()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4817 | mHandler.post(new Runnable() { |
| 4818 | @Override |
| 4819 | public void run() { |
| 4820 | mGroupHelper.onNotificationRemoved(r.sbn); |
| 4821 | } |
| 4822 | }); |
| 4823 | } |
| 4824 | |
| 4825 | // sound |
| 4826 | if (canceledKey.equals(mSoundNotificationKey)) { |
| 4827 | mSoundNotificationKey = null; |
| 4828 | final long identity = Binder.clearCallingIdentity(); |
| 4829 | try { |
| 4830 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4831 | if (player != null) { |
| 4832 | player.stopAsync(); |
| 4833 | } |
| 4834 | } catch (RemoteException e) { |
| 4835 | } finally { |
| 4836 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4837 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4838 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4839 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4840 | // vibrate |
| 4841 | if (canceledKey.equals(mVibrateNotificationKey)) { |
| 4842 | mVibrateNotificationKey = null; |
| 4843 | long identity = Binder.clearCallingIdentity(); |
| 4844 | try { |
| 4845 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 4846 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4847 | finally { |
| 4848 | Binder.restoreCallingIdentity(identity); |
| 4849 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4850 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4851 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4852 | // light |
| 4853 | mLights.remove(canceledKey); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4854 | } |
| 4855 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4856 | // Record usage stats |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4857 | // TODO: add unbundling stats? |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4858 | switch (reason) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 4859 | case REASON_CANCEL: |
| 4860 | case REASON_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4861 | case REASON_LISTENER_CANCEL: |
| 4862 | case REASON_LISTENER_CANCEL_ALL: |
| 4863 | mUsageStats.registerDismissedByUser(r); |
| 4864 | break; |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 4865 | case REASON_APP_CANCEL: |
| 4866 | case REASON_APP_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4867 | mUsageStats.registerRemovedByApp(r); |
| 4868 | break; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4869 | } |
| 4870 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4871 | String groupKey = r.getGroupKey(); |
| 4872 | NotificationRecord groupSummary = mSummaryByGroupKey.get(groupKey); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4873 | if (groupSummary != null && groupSummary.getKey().equals(canceledKey)) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4874 | mSummaryByGroupKey.remove(groupKey); |
| 4875 | } |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 4876 | final ArrayMap<String, String> summaries = mAutobundledSummaries.get(r.sbn.getUserId()); |
| 4877 | if (summaries != null && r.sbn.getKey().equals(summaries.get(r.sbn.getPackageName()))) { |
| 4878 | summaries.remove(r.sbn.getPackageName()); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4879 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 4880 | |
Daniel Sandler | 23d7c70 | 2013-03-07 16:32:06 -0500 | [diff] [blame] | 4881 | // Save it for users of getHistoricalNotifications() |
| 4882 | mArchive.record(r.sbn); |
Christoph Studer | 81e5b5f | 2014-10-22 17:19:56 +0200 | [diff] [blame] | 4883 | |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 4884 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 4885 | MetricsLogger.action(r.getLogMaker(now) |
| 4886 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
| 4887 | .setType(MetricsEvent.TYPE_DISMISS) |
| 4888 | .setSubtype(reason)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 4889 | EventLogTags.writeNotificationCanceled(canceledKey, reason, |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4890 | 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] | 4891 | } |
| 4892 | |
| 4893 | /** |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 4894 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4895 | * and none of the {@code mustNotHaveFlags}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4896 | */ |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 4897 | void cancelNotification(final int callingUid, final int callingPid, |
| 4898 | final String pkg, final String tag, final int id, |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4899 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4900 | final int userId, final int reason, final ManagedServiceInfo listener) { |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4901 | // In enqueueNotificationInternal notifications are added by scheduling the |
| 4902 | // work on the worker handler. Hence, we also schedule the cancel on this |
| 4903 | // handler to avoid a scenario where an add notification call followed by a |
| 4904 | // remove notification call ends up in not removing the notification. |
| 4905 | mHandler.post(new Runnable() { |
| 4906 | @Override |
| 4907 | public void run() { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4908 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Chris Wren | bddb5bc | 2015-03-04 08:47:46 -0800 | [diff] [blame] | 4909 | if (DBG) EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, id, tag, |
| 4910 | userId, mustHaveFlags, mustNotHaveFlags, reason, listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4911 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4912 | synchronized (mNotificationLock) { |
| 4913 | // Look for the notification, searching both the posted and enqueued lists. |
| 4914 | NotificationRecord r = findNotificationLocked(pkg, tag, id, userId); |
| 4915 | if (r != null) { |
| 4916 | // The notification was found, check if it should be removed. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4917 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4918 | // Ideally we'd do this in the caller of this method. However, that would |
| 4919 | // require the caller to also find the notification. |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 4920 | if (reason == REASON_CLICK) { |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4921 | mUsageStats.registerClickedByUser(r); |
| 4922 | } |
| 4923 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4924 | if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) { |
| 4925 | return; |
| 4926 | } |
| 4927 | if ((r.getNotification().flags & mustNotHaveFlags) != 0) { |
| 4928 | return; |
| 4929 | } |
| 4930 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4931 | // Cancel the notification. |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4932 | boolean wasPosted = removeFromNotificationListsLocked(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4933 | cancelNotificationLocked(r, sendDelete, reason, wasPosted, listenerName); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4934 | cancelGroupChildrenLocked(r, callingUid, callingPid, listenerName, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 4935 | sendDelete, null); |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4936 | updateLightsLocked(); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 4937 | } else { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4938 | // No notification was found, assume that it is snoozed and cancel it. |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 4939 | if (reason != REASON_SNOOZED) { |
| 4940 | final boolean wasSnoozed = mSnoozeHelper.cancel(userId, pkg, tag, id); |
| 4941 | if (wasSnoozed) { |
| 4942 | savePolicyFile(); |
| 4943 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 4944 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4945 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4946 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4947 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4948 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4949 | } |
| 4950 | |
| 4951 | /** |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 4952 | * Determine whether the userId applies to the notification in question, either because |
| 4953 | * they match exactly, or one of them is USER_ALL (which is treated as a wildcard). |
| 4954 | */ |
| 4955 | private boolean notificationMatchesUserId(NotificationRecord r, int userId) { |
| 4956 | return |
| 4957 | // looking for USER_ALL notifications? match everything |
| 4958 | userId == UserHandle.USER_ALL |
| 4959 | // a notification sent to USER_ALL matches any query |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4960 | || r.getUserId() == UserHandle.USER_ALL |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 4961 | // an exact user match |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4962 | || r.getUserId() == userId; |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | /** |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4966 | * Determine whether the userId applies to the notification in question, either because |
| 4967 | * 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] | 4968 | * because it matches one of the users profiles. |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4969 | */ |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 4970 | private boolean notificationMatchesCurrentProfiles(NotificationRecord r, int userId) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4971 | return notificationMatchesUserId(r, userId) |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 4972 | || mUserProfiles.isCurrentProfile(r.getUserId()); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | /** |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 4976 | * 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] | 4977 | * {@code mustHaveFlags}. |
| 4978 | */ |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4979 | void cancelAllNotificationsInt(int callingUid, int callingPid, String pkg, String channelId, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 4980 | int mustHaveFlags, int mustNotHaveFlags, boolean doit, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4981 | ManagedServiceInfo listener) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4982 | mHandler.post(new Runnable() { |
| 4983 | @Override |
| 4984 | public void run() { |
| 4985 | String listenerName = listener == null ? null : listener.component.toShortString(); |
| 4986 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 4987 | pkg, userId, mustHaveFlags, mustNotHaveFlags, reason, |
| 4988 | listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4989 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4990 | // Why does this parameter exist? Do we actually want to execute the above if doit |
| 4991 | // is false? |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4992 | if (!doit) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4993 | return; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4994 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4995 | |
| 4996 | synchronized (mNotificationLock) { |
| 4997 | FlagChecker flagChecker = (int flags) -> { |
| 4998 | if ((flags & mustHaveFlags) != mustHaveFlags) { |
| 4999 | return false; |
| 5000 | } |
| 5001 | if ((flags & mustNotHaveFlags) != 0) { |
| 5002 | return false; |
| 5003 | } |
| 5004 | return true; |
| 5005 | }; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5006 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 5007 | pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, flagChecker, |
| 5008 | false /*includeCurrentProfiles*/, userId, false /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5009 | listenerName, true /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5010 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 5011 | callingPid, pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, |
| 5012 | flagChecker, false /*includeCurrentProfiles*/, userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5013 | false /*sendDelete*/, reason, listenerName, false /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5014 | mSnoozeHelper.cancel(userId, pkg); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5015 | } |
| 5016 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5017 | }); |
| 5018 | } |
| 5019 | |
| 5020 | private interface FlagChecker { |
| 5021 | // Returns false if these flags do not pass the defined flag test. |
| 5022 | public boolean apply(int flags); |
| 5023 | } |
| 5024 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5025 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5026 | private void cancelAllNotificationsByListLocked(ArrayList<NotificationRecord> notificationList, |
| 5027 | int callingUid, int callingPid, String pkg, boolean nullPkgIndicatesUserSwitch, |
| 5028 | String channelId, FlagChecker flagChecker, boolean includeCurrentProfiles, int userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5029 | boolean sendDelete, int reason, String listenerName, boolean wasPosted) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5030 | ArrayList<NotificationRecord> canceledNotifications = null; |
| 5031 | for (int i = notificationList.size() - 1; i >= 0; --i) { |
| 5032 | NotificationRecord r = notificationList.get(i); |
| 5033 | if (includeCurrentProfiles) { |
| 5034 | if (!notificationMatchesCurrentProfiles(r, userId)) { |
| 5035 | continue; |
| 5036 | } |
| 5037 | } else if (!notificationMatchesUserId(r, userId)) { |
| 5038 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5039 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5040 | // Don't remove notifications to all, if there's no package name specified |
| 5041 | if (nullPkgIndicatesUserSwitch && pkg == null && r.getUserId() == UserHandle.USER_ALL) { |
| 5042 | continue; |
| 5043 | } |
| 5044 | if (!flagChecker.apply(r.getFlags())) { |
| 5045 | continue; |
| 5046 | } |
| 5047 | if (pkg != null && !r.sbn.getPackageName().equals(pkg)) { |
| 5048 | continue; |
| 5049 | } |
| 5050 | if (channelId != null && !channelId.equals(r.getChannel().getId())) { |
| 5051 | continue; |
| 5052 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5053 | if (canceledNotifications == null) { |
| 5054 | canceledNotifications = new ArrayList<>(); |
| 5055 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5056 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 5057 | mNotificationsByKey.remove(r.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5058 | canceledNotifications.add(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5059 | cancelNotificationLocked(r, sendDelete, reason, wasPosted, listenerName); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5060 | } |
| 5061 | if (canceledNotifications != null) { |
| 5062 | final int M = canceledNotifications.size(); |
| 5063 | for (int i = 0; i < M; i++) { |
| 5064 | cancelGroupChildrenLocked(canceledNotifications.get(i), callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5065 | listenerName, false /* sendDelete */, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5066 | } |
| 5067 | updateLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5068 | } |
| 5069 | } |
| 5070 | |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 5071 | void snoozeNotificationInt(String key, long duration, String snoozeCriterionId, |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5072 | ManagedServiceInfo listener) { |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5073 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 5074 | if (duration <= 0 && snoozeCriterionId == null || key == null) { |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 5075 | return; |
| 5076 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 5077 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5078 | if (DBG) { |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 5079 | Slog.d(TAG, String.format("snooze event(%s, %d, %s, %s)", key, duration, |
| 5080 | snoozeCriterionId, listenerName)); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5081 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5082 | // Needs to post so that it can cancel notifications not yet enqueued. |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5083 | mHandler.post(new SnoozeNotificationRunnable(key, duration, snoozeCriterionId)); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 5084 | } |
| 5085 | |
| 5086 | void unsnoozeNotificationInt(String key, ManagedServiceInfo listener) { |
| 5087 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 5088 | if (DBG) { |
| 5089 | Slog.d(TAG, String.format("unsnooze event(%s, %s)", key, listenerName)); |
| 5090 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5091 | mSnoozeHelper.repost(key); |
| 5092 | savePolicyFile(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 5093 | } |
| 5094 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5095 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 350159c | 2014-03-27 11:15:11 -0700 | [diff] [blame] | 5096 | void cancelAllLocked(int callingUid, int callingPid, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5097 | ManagedServiceInfo listener, boolean includeCurrentProfiles) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5098 | mHandler.post(new Runnable() { |
| 5099 | @Override |
| 5100 | public void run() { |
| 5101 | synchronized (mNotificationLock) { |
| 5102 | String listenerName = |
| 5103 | listener == null ? null : listener.component.toShortString(); |
| 5104 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 5105 | null, userId, 0, 0, reason, listenerName); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5106 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5107 | FlagChecker flagChecker = (int flags) -> { |
| 5108 | if ((flags & (Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR)) |
| 5109 | != 0) { |
| 5110 | return false; |
| 5111 | } |
| 5112 | return true; |
| 5113 | }; |
| 5114 | |
| 5115 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 5116 | null, false /*nullPkgIndicatesUserSwitch*/, null, flagChecker, |
| 5117 | includeCurrentProfiles, userId, true /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5118 | listenerName, true); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5119 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 5120 | callingPid, null, false /*nullPkgIndicatesUserSwitch*/, null, |
| 5121 | flagChecker, includeCurrentProfiles, userId, true /*sendDelete*/, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5122 | reason, listenerName, false); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5123 | mSnoozeHelper.cancel(userId, includeCurrentProfiles); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5124 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5125 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5126 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5127 | } |
| 5128 | |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5129 | // Warning: The caller is responsible for invoking updateLightsLocked(). |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5130 | @GuardedBy("mNotificationLock") |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5131 | private void cancelGroupChildrenLocked(NotificationRecord r, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5132 | String listenerName, boolean sendDelete, FlagChecker flagChecker) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5133 | Notification n = r.getNotification(); |
Christoph Studer | 3f31f5d | 2014-07-31 16:55:32 +0200 | [diff] [blame] | 5134 | if (!n.isGroupSummary()) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5135 | return; |
| 5136 | } |
| 5137 | |
| 5138 | String pkg = r.sbn.getPackageName(); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5139 | |
| 5140 | if (pkg == null) { |
| 5141 | if (DBG) Log.e(TAG, "No package for group summary: " + r.getKey()); |
| 5142 | return; |
| 5143 | } |
| 5144 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5145 | cancelGroupChildrenByListLocked(mNotificationList, r, callingUid, callingPid, listenerName, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5146 | sendDelete, true, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5147 | cancelGroupChildrenByListLocked(mEnqueuedNotifications, r, callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5148 | listenerName, sendDelete, false, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5149 | } |
| 5150 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5151 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5152 | private void cancelGroupChildrenByListLocked(ArrayList<NotificationRecord> notificationList, |
| 5153 | NotificationRecord parentNotification, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5154 | String listenerName, boolean sendDelete, boolean wasPosted, FlagChecker flagChecker) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5155 | final String pkg = parentNotification.sbn.getPackageName(); |
| 5156 | final int userId = parentNotification.getUserId(); |
| 5157 | final int reason = REASON_GROUP_SUMMARY_CANCELED; |
| 5158 | for (int i = notificationList.size() - 1; i >= 0; i--) { |
| 5159 | final NotificationRecord childR = notificationList.get(i); |
| 5160 | final StatusBarNotification childSbn = childR.sbn; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5161 | if ((childSbn.isGroup() && !childSbn.getNotification().isGroupSummary()) && |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5162 | childR.getGroupKey().equals(parentNotification.getGroupKey()) |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5163 | && (childR.getFlags() & Notification.FLAG_FOREGROUND_SERVICE) == 0 |
| 5164 | && (flagChecker == null || flagChecker.apply(childR.getFlags()))) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5165 | EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, childSbn.getId(), |
| 5166 | childSbn.getTag(), userId, 0, 0, reason, listenerName); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5167 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 5168 | mNotificationsByKey.remove(childR.getKey()); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5169 | cancelNotificationLocked(childR, sendDelete, reason, wasPosted, listenerName); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5170 | } |
| 5171 | } |
| 5172 | } |
| 5173 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5174 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5175 | void updateLightsLocked() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5176 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5177 | // handle notification lights |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 5178 | NotificationRecord ledNotification = null; |
| 5179 | while (ledNotification == null && !mLights.isEmpty()) { |
| 5180 | final String owner = mLights.get(mLights.size() - 1); |
| 5181 | ledNotification = mNotificationsByKey.get(owner); |
| 5182 | if (ledNotification == null) { |
| 5183 | Slog.wtfStack(TAG, "LED Notification does not exist: " + owner); |
| 5184 | mLights.remove(owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5185 | } |
| 5186 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 5187 | |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 5188 | // 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] | 5189 | if (ledNotification == null || mInCall || mScreenOn) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 5190 | mNotificationLight.turnOff(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5191 | } else { |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 5192 | NotificationRecord.Light light = ledNotification.getLight(); |
| 5193 | if (light != null && mNotificationPulseEnabled) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 5194 | // pulse repeatedly |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 5195 | mNotificationLight.setFlashing(light.color, Light.LIGHT_FLASH_TIMED, |
| 5196 | light.onMs, light.offMs); |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 5197 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5198 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5199 | } |
| 5200 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5201 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5202 | @NonNull List<NotificationRecord> findGroupNotificationsLocked(String pkg, |
| 5203 | String groupKey, int userId) { |
| 5204 | List<NotificationRecord> records = new ArrayList<>(); |
| 5205 | records.addAll(findGroupNotificationByListLocked(mNotificationList, pkg, groupKey, userId)); |
| 5206 | records.addAll( |
| 5207 | findGroupNotificationByListLocked(mEnqueuedNotifications, pkg, groupKey, userId)); |
| 5208 | return records; |
| 5209 | } |
| 5210 | |
| 5211 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5212 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5213 | private @NonNull List<NotificationRecord> findGroupNotificationByListLocked( |
| 5214 | ArrayList<NotificationRecord> list, String pkg, String groupKey, int userId) { |
| 5215 | List<NotificationRecord> records = new ArrayList<>(); |
| 5216 | final int len = list.size(); |
| 5217 | for (int i = 0; i < len; i++) { |
| 5218 | NotificationRecord r = list.get(i); |
| 5219 | if (notificationMatchesUserId(r, userId) && r.getGroupKey().equals(groupKey) |
| 5220 | && r.sbn.getPackageName().equals(pkg)) { |
| 5221 | records.add(r); |
| 5222 | } |
| 5223 | } |
| 5224 | return records; |
| 5225 | } |
| 5226 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5227 | // Searches both enqueued and posted notifications by key. |
| 5228 | // TODO: need to combine a bunch of these getters with slightly different behavior. |
| 5229 | // TODO: Should enqueuing just add to mNotificationsByKey instead? |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5230 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5231 | private NotificationRecord findNotificationByKeyLocked(String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5232 | NotificationRecord r; |
| 5233 | if ((r = findNotificationByListLocked(mNotificationList, key)) != null) { |
| 5234 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5235 | } |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5236 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, key)) != null) { |
| 5237 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5238 | } |
| 5239 | return null; |
| 5240 | } |
| 5241 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5242 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5243 | NotificationRecord findNotificationLocked(String pkg, String tag, int id, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5244 | NotificationRecord r; |
| 5245 | if ((r = findNotificationByListLocked(mNotificationList, pkg, tag, id, userId)) != null) { |
| 5246 | return r; |
| 5247 | } |
| 5248 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, pkg, tag, id, userId)) |
| 5249 | != null) { |
| 5250 | return r; |
| 5251 | } |
| 5252 | return null; |
| 5253 | } |
| 5254 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5255 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5256 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5257 | String pkg, String tag, int id, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5258 | final int len = list.size(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5259 | for (int i = 0; i < len; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5260 | NotificationRecord r = list.get(i); |
Vladimir Marko | 2526f33 | 2013-09-11 11:13:55 +0100 | [diff] [blame] | 5261 | if (notificationMatchesUserId(r, userId) && r.sbn.getId() == id && |
| 5262 | TextUtils.equals(r.sbn.getTag(), tag) && r.sbn.getPackageName().equals(pkg)) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5263 | return r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5264 | } |
| 5265 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5266 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5267 | } |
| 5268 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5269 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5270 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5271 | String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5272 | final int N = list.size(); |
| 5273 | for (int i = 0; i < N; i++) { |
| 5274 | if (key.equals(list.get(i).getKey())) { |
| 5275 | return list.get(i); |
| 5276 | } |
| 5277 | } |
| 5278 | return null; |
| 5279 | } |
| 5280 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5281 | @GuardedBy("mNotificationLock") |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 5282 | int indexOfNotificationLocked(String key) { |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 5283 | final int N = mNotificationList.size(); |
| 5284 | for (int i = 0; i < N; i++) { |
| 5285 | if (key.equals(mNotificationList.get(i).getKey())) { |
| 5286 | return i; |
| 5287 | } |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 5288 | } |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 5289 | return -1; |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 5290 | } |
| 5291 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 5292 | private void updateNotificationPulse() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5293 | synchronized (mNotificationLock) { |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 5294 | updateLightsLocked(); |
| 5295 | } |
| 5296 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 5297 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5298 | protected boolean isCallingUidSystem() { |
| 5299 | final int uid = Binder.getCallingUid(); |
| 5300 | return uid == Process.SYSTEM_UID; |
| 5301 | } |
| 5302 | |
| 5303 | protected boolean isUidSystemOrPhone(int uid) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5304 | final int appid = UserHandle.getAppId(uid); |
| 5305 | return (appid == Process.SYSTEM_UID || appid == Process.PHONE_UID || uid == 0); |
| 5306 | } |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 5307 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5308 | // TODO: Most calls should probably move to isCallerSystem. |
| 5309 | protected boolean isCallerSystemOrPhone() { |
| 5310 | return isUidSystemOrPhone(Binder.getCallingUid()); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5311 | } |
| 5312 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5313 | private void checkCallerIsSystemOrShell() { |
| 5314 | if (Binder.getCallingUid() == Process.SHELL_UID) { |
| 5315 | return; |
| 5316 | } |
| 5317 | checkCallerIsSystem(); |
| 5318 | } |
| 5319 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5320 | private void checkCallerIsSystem() { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5321 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5322 | return; |
| 5323 | } |
| 5324 | throw new SecurityException("Disallowed call for uid " + Binder.getCallingUid()); |
| 5325 | } |
| 5326 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5327 | private void checkCallerIsSystemOrSameApp(String pkg) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5328 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5329 | return; |
| 5330 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 5331 | checkCallerIsSameApp(pkg); |
| 5332 | } |
| 5333 | |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 5334 | private boolean isCallerInstantApp(String pkg) { |
| 5335 | // System is always allowed to act for ephemeral apps. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5336 | if (isCallerSystemOrPhone()) { |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 5337 | return false; |
| 5338 | } |
| 5339 | |
| 5340 | mAppOps.checkPackage(Binder.getCallingUid(), pkg); |
| 5341 | |
| 5342 | try { |
| 5343 | ApplicationInfo ai = mPackageManager.getApplicationInfo(pkg, 0, |
| 5344 | UserHandle.getCallingUserId()); |
| 5345 | if (ai == null) { |
| 5346 | throw new SecurityException("Unknown package " + pkg); |
| 5347 | } |
| 5348 | return ai.isInstantApp(); |
| 5349 | } catch (RemoteException re) { |
| 5350 | throw new SecurityException("Unknown package " + pkg, re); |
| 5351 | } |
| 5352 | |
| 5353 | } |
| 5354 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5355 | private void checkCallerIsSameApp(String pkg) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5356 | final int uid = Binder.getCallingUid(); |
| 5357 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5358 | ApplicationInfo ai = mPackageManager.getApplicationInfo( |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5359 | pkg, 0, UserHandle.getCallingUserId()); |
Dan Sandler | 09afc2e | 2014-07-18 14:29:20 -0400 | [diff] [blame] | 5360 | if (ai == null) { |
| 5361 | throw new SecurityException("Unknown package " + pkg); |
| 5362 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5363 | if (!UserHandle.isSameApp(ai.uid, uid)) { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5364 | throw new SecurityException("Calling uid " + uid + " gave package " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5365 | + pkg + " which is owned by uid " + ai.uid); |
| 5366 | } |
| 5367 | } catch (RemoteException re) { |
| 5368 | throw new SecurityException("Unknown package " + pkg + "\n" + re); |
| 5369 | } |
| 5370 | } |
| 5371 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 5372 | private static String callStateToString(int state) { |
| 5373 | switch (state) { |
| 5374 | case TelephonyManager.CALL_STATE_IDLE: return "CALL_STATE_IDLE"; |
| 5375 | case TelephonyManager.CALL_STATE_RINGING: return "CALL_STATE_RINGING"; |
| 5376 | case TelephonyManager.CALL_STATE_OFFHOOK: return "CALL_STATE_OFFHOOK"; |
| 5377 | default: return "CALL_STATE_UNKNOWN_" + state; |
| 5378 | } |
| 5379 | } |
| 5380 | |
| 5381 | private void listenForCallState() { |
| 5382 | TelephonyManager.from(getContext()).listen(new PhoneStateListener() { |
| 5383 | @Override |
| 5384 | public void onCallStateChanged(int state, String incomingNumber) { |
| 5385 | if (mCallState == state) return; |
| 5386 | if (DBG) Slog.d(TAG, "Call state changed: " + callStateToString(state)); |
| 5387 | mCallState = state; |
| 5388 | } |
| 5389 | }, PhoneStateListener.LISTEN_CALL_STATE); |
| 5390 | } |
| 5391 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5392 | /** |
| 5393 | * Generates a NotificationRankingUpdate from 'sbns', considering only |
| 5394 | * notifications visible to the given listener. |
| 5395 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5396 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5397 | private NotificationRankingUpdate makeRankingUpdateLocked(ManagedServiceInfo info) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5398 | final int N = mNotificationList.size(); |
| 5399 | ArrayList<String> keys = new ArrayList<String>(N); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 5400 | ArrayList<String> interceptedKeys = new ArrayList<String>(N); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5401 | ArrayList<Integer> importance = new ArrayList<>(N); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5402 | Bundle overrideGroupKeys = new Bundle(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5403 | Bundle visibilityOverrides = new Bundle(); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 5404 | Bundle suppressedVisualEffects = new Bundle(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5405 | Bundle explanation = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5406 | Bundle channels = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5407 | Bundle overridePeople = new Bundle(); |
| 5408 | Bundle snoozeCriteria = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5409 | Bundle showBadge = new Bundle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5410 | Bundle userSentiment = new Bundle(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5411 | for (int i = 0; i < N; i++) { |
| 5412 | NotificationRecord record = mNotificationList.get(i); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5413 | if (!isVisibleToListener(record.sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5414 | continue; |
| 5415 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5416 | final String key = record.sbn.getKey(); |
| 5417 | keys.add(key); |
| 5418 | importance.add(record.getImportance()); |
| 5419 | if (record.getImportanceExplanation() != null) { |
| 5420 | explanation.putCharSequence(key, record.getImportanceExplanation()); |
| 5421 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5422 | if (record.isIntercepted()) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5423 | interceptedKeys.add(key); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 5424 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5425 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5426 | suppressedVisualEffects.putInt(key, record.getSuppressedVisualEffects()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5427 | if (record.getPackageVisibilityOverride() |
| 5428 | != NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5429 | visibilityOverrides.putInt(key, record.getPackageVisibilityOverride()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5430 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5431 | overrideGroupKeys.putString(key, record.sbn.getOverrideGroupKey()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5432 | channels.putParcelable(key, record.getChannel()); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5433 | overridePeople.putStringArrayList(key, record.getPeopleOverride()); |
| 5434 | snoozeCriteria.putParcelableArrayList(key, record.getSnoozeCriteria()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5435 | showBadge.putBoolean(key, record.canShowBadge()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5436 | userSentiment.putInt(key, record.getUserSentiment()); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5437 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5438 | final int M = keys.size(); |
| 5439 | String[] keysAr = keys.toArray(new String[M]); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 5440 | String[] interceptedKeysAr = interceptedKeys.toArray(new String[interceptedKeys.size()]); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5441 | int[] importanceAr = new int[M]; |
| 5442 | for (int i = 0; i < M; i++) { |
| 5443 | importanceAr[i] = importance.get(i); |
| 5444 | } |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5445 | return new NotificationRankingUpdate(keysAr, interceptedKeysAr, visibilityOverrides, |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5446 | suppressedVisualEffects, importanceAr, explanation, overrideGroupKeys, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5447 | channels, overridePeople, snoozeCriteria, showBadge, userSentiment); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5448 | } |
| 5449 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5450 | boolean hasCompanionDevice(ManagedServiceInfo info) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5451 | if (mCompanionManager == null) { |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 5452 | mCompanionManager = getCompanionManager(); |
| 5453 | } |
| 5454 | // Companion mgr doesn't exist on all device types |
| 5455 | if (mCompanionManager == null) { |
| 5456 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5457 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5458 | long identity = Binder.clearCallingIdentity(); |
| 5459 | try { |
| 5460 | List<String> associations = mCompanionManager.getAssociations( |
| 5461 | info.component.getPackageName(), info.userid); |
| 5462 | if (!ArrayUtils.isEmpty(associations)) { |
| 5463 | return true; |
| 5464 | } |
| 5465 | } catch (SecurityException se) { |
| 5466 | // Not a privileged listener |
| 5467 | } catch (RemoteException re) { |
| 5468 | Slog.e(TAG, "Cannot reach companion device service", re); |
| 5469 | } catch (Exception e) { |
| 5470 | Slog.e(TAG, "Cannot verify listener " + info, e); |
| 5471 | } finally { |
| 5472 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5473 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5474 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5475 | } |
| 5476 | |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 5477 | protected ICompanionDeviceManager getCompanionManager() { |
| 5478 | return ICompanionDeviceManager.Stub.asInterface( |
| 5479 | ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE)); |
| 5480 | } |
| 5481 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5482 | private boolean isVisibleToListener(StatusBarNotification sbn, ManagedServiceInfo listener) { |
| 5483 | if (!listener.enabledAndUserMatches(sbn.getUserId())) { |
| 5484 | return false; |
| 5485 | } |
Justin Koh | 8d11a5a | 2014-08-04 18:29:49 -0700 | [diff] [blame] | 5486 | // TODO: remove this for older listeners. |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5487 | return true; |
| 5488 | } |
| 5489 | |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 5490 | private boolean isPackageSuspendedForUser(String pkg, int uid) { |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5491 | int userId = UserHandle.getUserId(uid); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5492 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5493 | return mPackageManager.isPackageSuspendedForUser(pkg, userId); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5494 | } catch (RemoteException re) { |
| 5495 | throw new SecurityException("Could not talk to package manager service"); |
Andrei Stingaceanu | efc4a34 | 2016-03-22 14:43:01 +0000 | [diff] [blame] | 5496 | } catch (IllegalArgumentException ex) { |
| 5497 | // Package not found. |
| 5498 | return false; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5499 | } |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5500 | } |
| 5501 | |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 5502 | private boolean canUseManagedServices() { |
| 5503 | return !mActivityManager.isLowRamDevice() |
| 5504 | || mPackageManagerClient.hasSystemFeature(PackageManager.FEATURE_WATCH); |
| 5505 | } |
| 5506 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5507 | private class TrimCache { |
| 5508 | StatusBarNotification heavy; |
| 5509 | StatusBarNotification sbnClone; |
| 5510 | StatusBarNotification sbnCloneLight; |
| 5511 | |
| 5512 | TrimCache(StatusBarNotification sbn) { |
| 5513 | heavy = sbn; |
| 5514 | } |
| 5515 | |
| 5516 | StatusBarNotification ForListener(ManagedServiceInfo info) { |
| 5517 | if (mListeners.getOnNotificationPostedTrim(info) == TRIM_LIGHT) { |
| 5518 | if (sbnCloneLight == null) { |
| 5519 | sbnCloneLight = heavy.cloneLight(); |
| 5520 | } |
| 5521 | return sbnCloneLight; |
| 5522 | } else { |
| 5523 | if (sbnClone == null) { |
| 5524 | sbnClone = heavy.clone(); |
| 5525 | } |
| 5526 | return sbnClone; |
| 5527 | } |
| 5528 | } |
| 5529 | } |
| 5530 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5531 | public class NotificationAssistants extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5532 | static final String TAG_ENABLED_NOTIFICATION_ASSISTANTS = "enabled_assistants"; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5533 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5534 | public NotificationAssistants(IPackageManager pm) { |
| 5535 | super(getContext(), mNotificationLock, mUserProfiles, pm); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5536 | } |
| 5537 | |
| 5538 | @Override |
| 5539 | protected Config getConfig() { |
| 5540 | Config c = new Config(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5541 | c.caption = "notification assistant"; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5542 | c.serviceInterface = NotificationAssistantService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5543 | c.xmlTag = TAG_ENABLED_NOTIFICATION_ASSISTANTS; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5544 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_ASSISTANT; |
| 5545 | c.bindPermission = Manifest.permission.BIND_NOTIFICATION_ASSISTANT_SERVICE; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5546 | c.settingsAction = Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS; |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 5547 | c.clientLabel = R.string.notification_ranker_binding_label; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5548 | return c; |
| 5549 | } |
| 5550 | |
| 5551 | @Override |
| 5552 | protected IInterface asInterface(IBinder binder) { |
| 5553 | return INotificationListener.Stub.asInterface(binder); |
| 5554 | } |
| 5555 | |
| 5556 | @Override |
| 5557 | protected boolean checkType(IInterface service) { |
| 5558 | return service instanceof INotificationListener; |
| 5559 | } |
| 5560 | |
| 5561 | @Override |
| 5562 | protected void onServiceAdded(ManagedServiceInfo info) { |
| 5563 | mListeners.registerGuestService(info); |
| 5564 | } |
| 5565 | |
| 5566 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5567 | @GuardedBy("mNotificationLock") |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5568 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
| 5569 | mListeners.unregisterService(removed.service, removed.userid); |
| 5570 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5571 | |
| 5572 | public void onNotificationEnqueued(final NotificationRecord r) { |
| 5573 | final StatusBarNotification sbn = r.sbn; |
| 5574 | TrimCache trimCache = new TrimCache(sbn); |
| 5575 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5576 | // There should be only one, but it's a list, so while we enforce |
| 5577 | // singularity elsewhere, we keep it general here, to avoid surprises. |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5578 | for (final ManagedServiceInfo info : NotificationAssistants.this.getServices()) { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5579 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 5580 | if (!sbnVisible) { |
| 5581 | continue; |
| 5582 | } |
| 5583 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5584 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 5585 | mHandler.post(new Runnable() { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5586 | @Override |
| 5587 | public void run() { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5588 | notifyEnqueued(info, sbnToPost); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5589 | } |
| 5590 | }); |
| 5591 | } |
| 5592 | } |
| 5593 | |
| 5594 | private void notifyEnqueued(final ManagedServiceInfo info, |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5595 | final StatusBarNotification sbn) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5596 | final INotificationListener assistant = (INotificationListener) info.service; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5597 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
| 5598 | try { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5599 | assistant.onNotificationEnqueued(sbnHolder); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5600 | } catch (RemoteException ex) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5601 | Log.e(TAG, "unable to notify assistant (enqueued): " + assistant, ex); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5602 | } |
| 5603 | } |
| 5604 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5605 | /** |
| 5606 | * asynchronously notify the assistant that a notification has been snoozed until a |
| 5607 | * context |
| 5608 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5609 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5610 | public void notifyAssistantSnoozedLocked(final StatusBarNotification sbn, |
| 5611 | final String snoozeCriterionId) { |
| 5612 | TrimCache trimCache = new TrimCache(sbn); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5613 | for (final ManagedServiceInfo info : getServices()) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5614 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 5615 | if (!sbnVisible) { |
| 5616 | continue; |
| 5617 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5618 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
| 5619 | mHandler.post(new Runnable() { |
| 5620 | @Override |
| 5621 | public void run() { |
| 5622 | final INotificationListener assistant = |
| 5623 | (INotificationListener) info.service; |
| 5624 | StatusBarNotificationHolder sbnHolder |
| 5625 | = new StatusBarNotificationHolder(sbnToPost); |
| 5626 | try { |
| 5627 | assistant.onNotificationSnoozedUntilContext( |
| 5628 | sbnHolder, snoozeCriterionId); |
| 5629 | } catch (RemoteException ex) { |
| 5630 | Log.e(TAG, "unable to notify assistant (snoozed): " + assistant, ex); |
| 5631 | } |
| 5632 | } |
| 5633 | }); |
| 5634 | } |
| 5635 | } |
| 5636 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5637 | public boolean isEnabled() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5638 | return !getServices().isEmpty(); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5639 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5640 | } |
| 5641 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5642 | public class NotificationListeners extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5643 | static final String TAG_ENABLED_NOTIFICATION_LISTENERS = "enabled_listeners"; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5644 | |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5645 | private final ArraySet<ManagedServiceInfo> mLightTrimListeners = new ArraySet<>(); |
| 5646 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5647 | public NotificationListeners(IPackageManager pm) { |
| 5648 | super(getContext(), mNotificationLock, mUserProfiles, pm); |
| 5649 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5650 | } |
| 5651 | |
| 5652 | @Override |
| 5653 | protected Config getConfig() { |
| 5654 | Config c = new Config(); |
| 5655 | c.caption = "notification listener"; |
| 5656 | c.serviceInterface = NotificationListenerService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5657 | c.xmlTag = TAG_ENABLED_NOTIFICATION_LISTENERS; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5658 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS; |
| 5659 | c.bindPermission = android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE; |
| 5660 | c.settingsAction = Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS; |
| 5661 | c.clientLabel = R.string.notification_listener_binding_label; |
| 5662 | return c; |
| 5663 | } |
| 5664 | |
| 5665 | @Override |
| 5666 | protected IInterface asInterface(IBinder binder) { |
| 5667 | return INotificationListener.Stub.asInterface(binder); |
| 5668 | } |
| 5669 | |
| 5670 | @Override |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5671 | protected boolean checkType(IInterface service) { |
| 5672 | return service instanceof INotificationListener; |
| 5673 | } |
| 5674 | |
| 5675 | @Override |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 5676 | public void onServiceAdded(ManagedServiceInfo info) { |
| 5677 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5678 | final NotificationRankingUpdate update; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5679 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5680 | update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5681 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5682 | try { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5683 | listener.onListenerConnected(update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5684 | } catch (RemoteException e) { |
| 5685 | // we tried |
| 5686 | } |
| 5687 | } |
| 5688 | |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5689 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5690 | @GuardedBy("mNotificationLock") |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5691 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 5692 | if (removeDisabledHints(removed)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5693 | updateListenerHintsLocked(); |
Christoph Studer | 0d6ef4b | 2014-12-02 15:00:48 +0100 | [diff] [blame] | 5694 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5695 | } |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5696 | mLightTrimListeners.remove(removed); |
| 5697 | } |
| 5698 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5699 | @GuardedBy("mNotificationLock") |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5700 | public void setOnNotificationPostedTrimLocked(ManagedServiceInfo info, int trim) { |
| 5701 | if (trim == TRIM_LIGHT) { |
| 5702 | mLightTrimListeners.add(info); |
| 5703 | } else { |
| 5704 | mLightTrimListeners.remove(info); |
| 5705 | } |
| 5706 | } |
| 5707 | |
| 5708 | public int getOnNotificationPostedTrim(ManagedServiceInfo info) { |
| 5709 | return mLightTrimListeners.contains(info) ? TRIM_LIGHT : TRIM_FULL; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5710 | } |
| 5711 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5712 | /** |
| 5713 | * asynchronously notify all listeners about a new notification |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5714 | * |
| 5715 | * <p> |
| 5716 | * Also takes care of removing a notification that has been visible to a listener before, |
| 5717 | * but isn't anymore. |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5718 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5719 | @GuardedBy("mNotificationLock") |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5720 | public void notifyPostedLocked(StatusBarNotification sbn, StatusBarNotification oldSbn) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5721 | // Lazily initialized snapshots of the notification. |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5722 | TrimCache trimCache = new TrimCache(sbn); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5723 | |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5724 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5725 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 5726 | boolean oldSbnVisible = oldSbn != null ? isVisibleToListener(oldSbn, info) : false; |
| 5727 | // This notification hasn't been and still isn't visible -> ignore. |
| 5728 | if (!oldSbnVisible && !sbnVisible) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5729 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5730 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5731 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5732 | |
| 5733 | // This notification became invisible -> remove the old one. |
| 5734 | if (oldSbnVisible && !sbnVisible) { |
| 5735 | final StatusBarNotification oldSbnLightClone = oldSbn.cloneLight(); |
| 5736 | mHandler.post(new Runnable() { |
| 5737 | @Override |
| 5738 | public void run() { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5739 | notifyRemoved( |
| 5740 | info, oldSbnLightClone, update, null, REASON_USER_STOPPED); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5741 | } |
| 5742 | }); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5743 | continue; |
| 5744 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5745 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5746 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5747 | mHandler.post(new Runnable() { |
| 5748 | @Override |
| 5749 | public void run() { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5750 | notifyPosted(info, sbnToPost, update); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5751 | } |
| 5752 | }); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5753 | } |
| 5754 | } |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5755 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5756 | /** |
| 5757 | * asynchronously notify all listeners about a removed notification |
| 5758 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5759 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5760 | public void notifyRemovedLocked(StatusBarNotification sbn, int reason, |
| 5761 | NotificationStats notificationStats) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5762 | // make a copy in case changes are made to the underlying Notification object |
| 5763 | // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the |
| 5764 | // notification |
| 5765 | final StatusBarNotification sbnLight = sbn.cloneLight(); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5766 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5767 | if (!isVisibleToListener(sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5768 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5769 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5770 | // Only assistants can get stats |
| 5771 | final NotificationStats stats = mAssistants.isServiceTokenValidLocked(info.service) |
| 5772 | ? notificationStats : null; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5773 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5774 | mHandler.post(new Runnable() { |
| 5775 | @Override |
| 5776 | public void run() { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5777 | notifyRemoved(info, sbnLight, update, stats, reason); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5778 | } |
| 5779 | }); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5780 | } |
| 5781 | } |
| 5782 | |
| 5783 | /** |
| 5784 | * asynchronously notify all listeners about a reordering of notifications |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5785 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5786 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5787 | public void notifyRankingUpdateLocked() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5788 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5789 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 5790 | continue; |
| 5791 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5792 | final NotificationRankingUpdate update = makeRankingUpdateLocked(serviceInfo); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5793 | mHandler.post(new Runnable() { |
| 5794 | @Override |
| 5795 | public void run() { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5796 | notifyRankingUpdate(serviceInfo, update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5797 | } |
| 5798 | }); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5799 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5800 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5801 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5802 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5803 | public void notifyListenerHintsChangedLocked(final int hints) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5804 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5805 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 5806 | continue; |
| 5807 | } |
| 5808 | mHandler.post(new Runnable() { |
| 5809 | @Override |
| 5810 | public void run() { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5811 | notifyListenerHintsChanged(serviceInfo, hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5812 | } |
| 5813 | }); |
| 5814 | } |
| 5815 | } |
| 5816 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5817 | public void notifyInterruptionFilterChanged(final int interruptionFilter) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5818 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5819 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 5820 | continue; |
| 5821 | } |
| 5822 | mHandler.post(new Runnable() { |
| 5823 | @Override |
| 5824 | public void run() { |
| 5825 | notifyInterruptionFilterChanged(serviceInfo, interruptionFilter); |
| 5826 | } |
| 5827 | }); |
| 5828 | } |
| 5829 | } |
| 5830 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5831 | protected void notifyNotificationChannelChanged(final String pkg, final UserHandle user, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5832 | final NotificationChannel channel, final int modificationType) { |
| 5833 | if (channel == null) { |
| 5834 | return; |
| 5835 | } |
| 5836 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5837 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5838 | continue; |
| 5839 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5840 | |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 5841 | BackgroundThread.getHandler().post(() -> { |
| 5842 | if (hasCompanionDevice(serviceInfo)) { |
| 5843 | notifyNotificationChannelChanged( |
| 5844 | serviceInfo, pkg, user, channel, modificationType); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5845 | } |
| 5846 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5847 | } |
| 5848 | } |
| 5849 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5850 | protected void notifyNotificationChannelGroupChanged( |
| 5851 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
| 5852 | final int modificationType) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5853 | if (group == null) { |
| 5854 | return; |
| 5855 | } |
| 5856 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5857 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5858 | continue; |
| 5859 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5860 | |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 5861 | BackgroundThread.getHandler().post(() -> { |
| 5862 | if (hasCompanionDevice(serviceInfo)) { |
| 5863 | notifyNotificationChannelGroupChanged( |
| 5864 | serviceInfo, pkg, user, group, modificationType); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5865 | } |
| 5866 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5867 | } |
| 5868 | } |
| 5869 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5870 | private void notifyPosted(final ManagedServiceInfo info, |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5871 | final StatusBarNotification sbn, NotificationRankingUpdate rankingUpdate) { |
Julia Reynolds | a75c752 | 2017-03-21 17:34:25 -0400 | [diff] [blame] | 5872 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5873 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5874 | try { |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5875 | listener.onNotificationPosted(sbnHolder, rankingUpdate); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5876 | } catch (RemoteException ex) { |
| 5877 | Log.e(TAG, "unable to notify listener (posted): " + listener, ex); |
| 5878 | } |
| 5879 | } |
| 5880 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5881 | private void notifyRemoved(ManagedServiceInfo info, StatusBarNotification sbn, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5882 | NotificationRankingUpdate rankingUpdate, NotificationStats stats, int reason) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5883 | if (!info.enabledAndUserMatches(sbn.getUserId())) { |
| 5884 | return; |
| 5885 | } |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5886 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5887 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5888 | try { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5889 | listener.onNotificationRemoved(sbnHolder, rankingUpdate, stats, reason); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5890 | } catch (RemoteException ex) { |
| 5891 | Log.e(TAG, "unable to notify listener (removed): " + listener, ex); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 5892 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5893 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5894 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5895 | private void notifyRankingUpdate(ManagedServiceInfo info, |
| 5896 | NotificationRankingUpdate rankingUpdate) { |
| 5897 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5898 | try { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5899 | listener.onNotificationRankingUpdate(rankingUpdate); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5900 | } catch (RemoteException ex) { |
| 5901 | Log.e(TAG, "unable to notify listener (ranking update): " + listener, ex); |
| 5902 | } |
| 5903 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5904 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5905 | private void notifyListenerHintsChanged(ManagedServiceInfo info, int hints) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5906 | final INotificationListener listener = (INotificationListener) info.service; |
| 5907 | try { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5908 | listener.onListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5909 | } catch (RemoteException ex) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5910 | Log.e(TAG, "unable to notify listener (listener hints): " + listener, ex); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5911 | } |
| 5912 | } |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 5913 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5914 | private void notifyInterruptionFilterChanged(ManagedServiceInfo info, |
| 5915 | int interruptionFilter) { |
| 5916 | final INotificationListener listener = (INotificationListener) info.service; |
| 5917 | try { |
| 5918 | listener.onInterruptionFilterChanged(interruptionFilter); |
| 5919 | } catch (RemoteException ex) { |
| 5920 | Log.e(TAG, "unable to notify listener (interruption filter): " + listener, ex); |
| 5921 | } |
| 5922 | } |
| 5923 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5924 | void notifyNotificationChannelChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5925 | final String pkg, final UserHandle user, final NotificationChannel channel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5926 | final int modificationType) { |
| 5927 | final INotificationListener listener = (INotificationListener) info.service; |
| 5928 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5929 | listener.onNotificationChannelModification(pkg, user, channel, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5930 | } catch (RemoteException ex) { |
| 5931 | Log.e(TAG, "unable to notify listener (channel changed): " + listener, ex); |
| 5932 | } |
| 5933 | } |
| 5934 | |
| 5935 | private void notifyNotificationChannelGroupChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5936 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5937 | final int modificationType) { |
| 5938 | final INotificationListener listener = (INotificationListener) info.service; |
| 5939 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5940 | listener.onNotificationChannelGroupModification(pkg, user, group, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5941 | } catch (RemoteException ex) { |
| 5942 | Log.e(TAG, "unable to notify listener (channel group changed): " + listener, ex); |
| 5943 | } |
| 5944 | } |
| 5945 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5946 | public boolean isListenerPackage(String packageName) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 5947 | if (packageName == null) { |
| 5948 | return false; |
| 5949 | } |
| 5950 | // TODO: clean up locking object later |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5951 | synchronized (mNotificationLock) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5952 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 5953 | if (packageName.equals(serviceInfo.component.getPackageName())) { |
| 5954 | return true; |
| 5955 | } |
| 5956 | } |
| 5957 | } |
| 5958 | return false; |
| 5959 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5960 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5961 | |
| 5962 | public static final class DumpFilter { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5963 | public boolean filtered = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5964 | public String pkgFilter; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 5965 | public boolean zen; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 5966 | public long since; |
| 5967 | public boolean stats; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5968 | public boolean redact = true; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 5969 | public boolean proto = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5970 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 5971 | @NonNull |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5972 | public static DumpFilter parseFromArguments(String[] args) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5973 | final DumpFilter filter = new DumpFilter(); |
| 5974 | for (int ai = 0; ai < args.length; ai++) { |
| 5975 | final String a = args[ai]; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 5976 | if ("--proto".equals(a)) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 5977 | filter.proto = true; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 5978 | } else if ("--noredact".equals(a) || "--reveal".equals(a)) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5979 | filter.redact = false; |
| 5980 | } else if ("p".equals(a) || "pkg".equals(a) || "--package".equals(a)) { |
| 5981 | if (ai < args.length-1) { |
| 5982 | ai++; |
| 5983 | filter.pkgFilter = args[ai].trim().toLowerCase(); |
| 5984 | if (filter.pkgFilter.isEmpty()) { |
| 5985 | filter.pkgFilter = null; |
| 5986 | } else { |
| 5987 | filter.filtered = true; |
| 5988 | } |
| 5989 | } |
| 5990 | } else if ("--zen".equals(a) || "zen".equals(a)) { |
| 5991 | filter.filtered = true; |
| 5992 | filter.zen = true; |
| 5993 | } else if ("--stats".equals(a)) { |
| 5994 | filter.stats = true; |
| 5995 | if (ai < args.length-1) { |
| 5996 | ai++; |
Tobias Thierer | 28532d0 | 2016-04-21 14:52:10 +0100 | [diff] [blame] | 5997 | filter.since = Long.parseLong(args[ai]); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5998 | } else { |
| 5999 | filter.since = 0; |
| 6000 | } |
| 6001 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6002 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6003 | return filter; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6004 | } |
| 6005 | |
| 6006 | public boolean matches(StatusBarNotification sbn) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6007 | if (!filtered) return true; |
| 6008 | return zen ? true : sbn != null |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 6009 | && (matches(sbn.getPackageName()) || matches(sbn.getOpPkg())); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6010 | } |
| 6011 | |
| 6012 | public boolean matches(ComponentName component) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6013 | if (!filtered) return true; |
| 6014 | return zen ? true : component != null && matches(component.getPackageName()); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6015 | } |
| 6016 | |
| 6017 | public boolean matches(String pkg) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6018 | if (!filtered) return true; |
| 6019 | return zen ? true : pkg != null && pkg.toLowerCase().contains(pkgFilter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 6020 | } |
| 6021 | |
| 6022 | @Override |
| 6023 | public String toString() { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 6024 | return stats ? "stats" : zen ? "zen" : ('\'' + pkgFilter + '\''); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6025 | } |
| 6026 | } |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6027 | |
| 6028 | /** |
| 6029 | * Wrapper for a StatusBarNotification object that allows transfer across a oneway |
| 6030 | * binder without sending large amounts of data over a oneway transaction. |
| 6031 | */ |
| 6032 | private static final class StatusBarNotificationHolder |
| 6033 | extends IStatusBarNotificationHolder.Stub { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 6034 | private StatusBarNotification mValue; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6035 | |
| 6036 | public StatusBarNotificationHolder(StatusBarNotification value) { |
| 6037 | mValue = value; |
| 6038 | } |
| 6039 | |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 6040 | /** 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] | 6041 | @Override |
| 6042 | public StatusBarNotification get() { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 6043 | StatusBarNotification value = mValue; |
| 6044 | mValue = null; |
| 6045 | return value; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6046 | } |
| 6047 | } |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6048 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6049 | private class ShellCmd extends ShellCommand { |
| 6050 | public static final String USAGE = "help\n" |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 6051 | + "allow_listener COMPONENT [user_id]\n" |
| 6052 | + "disallow_listener COMPONENT [user_id]\n" |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6053 | + "set_assistant COMPONENT\n" |
| 6054 | + "remove_assistant COMPONENT\n" |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6055 | + "allow_dnd PACKAGE\n" |
| 6056 | + "disallow_dnd PACKAGE"; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6057 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6058 | @Override |
| 6059 | public int onCommand(String cmd) { |
Felipe Leme | 68d8041 | 2017-07-14 11:18:08 -0700 | [diff] [blame] | 6060 | if (cmd == null) { |
| 6061 | return handleDefaultCommands(cmd); |
| 6062 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 6063 | final PrintWriter pw = getOutPrintWriter(); |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 6064 | try { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6065 | switch (cmd) { |
| 6066 | case "allow_dnd": { |
| 6067 | getBinderService().setNotificationPolicyAccessGranted( |
| 6068 | getNextArgRequired(), true); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6069 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6070 | break; |
| 6071 | |
| 6072 | case "disallow_dnd": { |
| 6073 | getBinderService().setNotificationPolicyAccessGranted( |
| 6074 | getNextArgRequired(), false); |
| 6075 | } |
| 6076 | break; |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 6077 | case "allow_listener": { |
| 6078 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 6079 | if (cn == null) { |
| 6080 | pw.println("Invalid listener - must be a ComponentName"); |
| 6081 | return -1; |
| 6082 | } |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 6083 | String userId = getNextArg(); |
| 6084 | if (userId == null) { |
| 6085 | getBinderService().setNotificationListenerAccessGranted(cn, true); |
| 6086 | } else { |
| 6087 | getBinderService().setNotificationListenerAccessGrantedForUser( |
| 6088 | cn, Integer.parseInt(userId), true); |
| 6089 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 6090 | } |
| 6091 | break; |
| 6092 | case "disallow_listener": { |
| 6093 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 6094 | if (cn == null) { |
| 6095 | pw.println("Invalid listener - must be a ComponentName"); |
| 6096 | return -1; |
| 6097 | } |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 6098 | String userId = getNextArg(); |
| 6099 | if (userId == null) { |
| 6100 | getBinderService().setNotificationListenerAccessGranted(cn, false); |
| 6101 | } else { |
| 6102 | getBinderService().setNotificationListenerAccessGrantedForUser( |
| 6103 | cn, Integer.parseInt(userId), false); |
| 6104 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 6105 | } |
| 6106 | break; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6107 | case "allow_assistant": { |
| 6108 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 6109 | if (cn == null) { |
| 6110 | pw.println("Invalid assistant - must be a ComponentName"); |
| 6111 | return -1; |
| 6112 | } |
| 6113 | getBinderService().setNotificationAssistantAccessGranted(cn, true); |
| 6114 | } |
| 6115 | break; |
| 6116 | case "disallow_assistant": { |
| 6117 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 6118 | if (cn == null) { |
| 6119 | pw.println("Invalid assistant - must be a ComponentName"); |
| 6120 | return -1; |
| 6121 | } |
| 6122 | getBinderService().setNotificationAssistantAccessGranted(cn, false); |
| 6123 | } |
| 6124 | break; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6125 | |
| 6126 | default: |
| 6127 | return handleDefaultCommands(cmd); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6128 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 6129 | } catch (Exception e) { |
| 6130 | pw.println("Error occurred. Check logcat for details. " + e.getMessage()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6131 | Slog.e(TAG, "Error running shell command", e); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6132 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6133 | return 0; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6134 | } |
| 6135 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6136 | @Override |
| 6137 | public void onHelp() { |
| 6138 | getOutPrintWriter().println(USAGE); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6139 | } |
| 6140 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6141 | } |