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 | |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 2920 | /** |
| 2921 | * Sets the notification policy. Apps that target API levels below |
| 2922 | * {@link android.os.Build.VERSION_CODES#P} cannot make DND silence |
| 2923 | * {@link Policy#PRIORITY_CATEGORY_ALARMS} or |
| 2924 | * {@link Policy#PRIORITY_CATEGORY_MEDIA_SYSTEM_OTHER} |
| 2925 | */ |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2926 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2927 | public void setNotificationPolicy(String pkg, Policy policy) { |
| 2928 | enforcePolicyAccess(pkg, "setNotificationPolicy"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2929 | final long identity = Binder.clearCallingIdentity(); |
| 2930 | try { |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 2931 | final ApplicationInfo applicationInfo = mPackageManager.getApplicationInfo(pkg, |
| 2932 | 0, UserHandle.getUserId(MY_UID)); |
| 2933 | |
| 2934 | if (applicationInfo.targetSdkVersion <= Build.VERSION_CODES.O_MR1) { |
| 2935 | Policy currPolicy = mZenModeHelper.getNotificationPolicy(); |
| 2936 | |
| 2937 | int priorityCategories = policy.priorityCategories |
| 2938 | | (currPolicy.priorityCategories & Policy.PRIORITY_CATEGORY_ALARMS) |
| 2939 | | (currPolicy.priorityCategories & |
| 2940 | Policy.PRIORITY_CATEGORY_MEDIA_SYSTEM_OTHER); |
| 2941 | policy = new Policy(priorityCategories, |
| 2942 | policy.priorityCallSenders, policy.priorityMessageSenders, |
| 2943 | policy.suppressedVisualEffects); |
| 2944 | } |
| 2945 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2946 | mZenModeHelper.setNotificationPolicy(policy); |
Beverly | 6697eff | 2017-12-14 15:00:27 -0500 | [diff] [blame] | 2947 | } catch (RemoteException e) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2948 | } finally { |
| 2949 | Binder.restoreCallingIdentity(identity); |
| 2950 | } |
| 2951 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2952 | |
| 2953 | @Override |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2954 | public List<String> getEnabledNotificationListenerPackages() { |
| 2955 | checkCallerIsSystem(); |
| 2956 | return mListeners.getAllowedPackages(getCallingUserHandle().getIdentifier()); |
| 2957 | } |
| 2958 | |
| 2959 | @Override |
| 2960 | public List<ComponentName> getEnabledNotificationListeners(int userId) { |
| 2961 | checkCallerIsSystem(); |
| 2962 | return mListeners.getAllowedComponents(userId); |
| 2963 | } |
| 2964 | |
| 2965 | @Override |
| 2966 | public boolean isNotificationListenerAccessGranted(ComponentName listener) { |
| 2967 | Preconditions.checkNotNull(listener); |
| 2968 | checkCallerIsSystemOrSameApp(listener.getPackageName()); |
| 2969 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 2970 | getCallingUserHandle().getIdentifier()); |
| 2971 | } |
| 2972 | |
| 2973 | @Override |
| 2974 | public boolean isNotificationListenerAccessGrantedForUser(ComponentName listener, |
| 2975 | int userId) { |
| 2976 | Preconditions.checkNotNull(listener); |
| 2977 | checkCallerIsSystem(); |
| 2978 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 2979 | userId); |
| 2980 | } |
| 2981 | |
| 2982 | @Override |
| 2983 | public boolean isNotificationAssistantAccessGranted(ComponentName assistant) { |
| 2984 | Preconditions.checkNotNull(assistant); |
| 2985 | checkCallerIsSystemOrSameApp(assistant.getPackageName()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2986 | return mAssistants.isPackageOrComponentAllowed(assistant.flattenToString(), |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2987 | getCallingUserHandle().getIdentifier()); |
| 2988 | } |
| 2989 | |
| 2990 | @Override |
| 2991 | public void setNotificationListenerAccessGranted(ComponentName listener, |
| 2992 | boolean granted) throws RemoteException { |
| 2993 | setNotificationListenerAccessGrantedForUser( |
| 2994 | listener, getCallingUserHandle().getIdentifier(), granted); |
| 2995 | } |
| 2996 | |
| 2997 | @Override |
| 2998 | public void setNotificationAssistantAccessGranted(ComponentName assistant, |
| 2999 | boolean granted) throws RemoteException { |
| 3000 | setNotificationAssistantAccessGrantedForUser( |
| 3001 | assistant, getCallingUserHandle().getIdentifier(), granted); |
| 3002 | } |
| 3003 | |
| 3004 | @Override |
| 3005 | public void setNotificationListenerAccessGrantedForUser(ComponentName listener, int userId, |
| 3006 | boolean granted) throws RemoteException { |
| 3007 | Preconditions.checkNotNull(listener); |
Julia Reynolds | 0d21764 | 2017-08-11 11:26:04 -0400 | [diff] [blame] | 3008 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3009 | final long identity = Binder.clearCallingIdentity(); |
| 3010 | try { |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3011 | if (canUseManagedServices()) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3012 | mConditionProviders.setPackageOrComponentEnabled(listener.flattenToString(), |
| 3013 | userId, false, granted); |
| 3014 | mListeners.setPackageOrComponentEnabled(listener.flattenToString(), |
| 3015 | userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3016 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3017 | getContext().sendBroadcastAsUser(new Intent( |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3018 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3019 | .setPackage(listener.getPackageName()) |
| 3020 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3021 | UserHandle.of(userId), null); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3022 | |
| 3023 | savePolicyFile(); |
| 3024 | } |
| 3025 | } finally { |
| 3026 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3027 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | @Override |
| 3031 | public void setNotificationAssistantAccessGrantedForUser(ComponentName assistant, |
| 3032 | int userId, boolean granted) throws RemoteException { |
| 3033 | Preconditions.checkNotNull(assistant); |
Julia Reynolds | 0d21764 | 2017-08-11 11:26:04 -0400 | [diff] [blame] | 3034 | checkCallerIsSystemOrShell(); |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3035 | final long identity = Binder.clearCallingIdentity(); |
| 3036 | try { |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 3037 | if (canUseManagedServices()) { |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3038 | mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(), |
| 3039 | userId, false, granted); |
| 3040 | mAssistants.setPackageOrComponentEnabled(assistant.flattenToString(), |
| 3041 | userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3042 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3043 | getContext().sendBroadcastAsUser(new Intent( |
| 3044 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 3045 | .setPackage(assistant.getPackageName()) |
| 3046 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
Julia Reynolds | 92febc3 | 2017-10-26 11:30:31 -0400 | [diff] [blame] | 3047 | UserHandle.of(userId), null); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3048 | |
Julia Reynolds | e5c680f | 2017-09-13 09:25:10 -0400 | [diff] [blame] | 3049 | savePolicyFile(); |
| 3050 | } |
| 3051 | } finally { |
| 3052 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 3053 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | @Override |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3057 | public void applyEnqueuedAdjustmentFromAssistant(INotificationListener token, |
| 3058 | Adjustment adjustment) throws RemoteException { |
| 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 | int N = mEnqueuedNotifications.size(); |
| 3064 | for (int i = 0; i < N; i++) { |
| 3065 | final NotificationRecord n = mEnqueuedNotifications.get(i); |
| 3066 | if (Objects.equals(adjustment.getKey(), n.getKey()) |
| 3067 | && Objects.equals(adjustment.getUser(), n.getUserId())) { |
| 3068 | applyAdjustment(n, adjustment); |
| 3069 | break; |
| 3070 | } |
| 3071 | } |
| 3072 | } |
| 3073 | } finally { |
| 3074 | Binder.restoreCallingIdentity(identity); |
| 3075 | } |
| 3076 | } |
| 3077 | |
| 3078 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 3079 | public void applyAdjustmentFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3080 | Adjustment adjustment) throws RemoteException { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3081 | final long identity = Binder.clearCallingIdentity(); |
| 3082 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3083 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3084 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3085 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 3086 | applyAdjustment(n, adjustment); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3087 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3088 | mRankingHandler.requestSort(); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3089 | } finally { |
| 3090 | Binder.restoreCallingIdentity(identity); |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 3095 | public void applyAdjustmentsFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3096 | List<Adjustment> adjustments) throws RemoteException { |
| 3097 | |
| 3098 | final long identity = Binder.clearCallingIdentity(); |
| 3099 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3100 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3101 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3102 | for (Adjustment adjustment : adjustments) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3103 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 3104 | applyAdjustment(n, adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3105 | } |
| 3106 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3107 | mRankingHandler.requestSort(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3108 | } finally { |
| 3109 | Binder.restoreCallingIdentity(identity); |
| 3110 | } |
| 3111 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3112 | |
| 3113 | @Override |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 3114 | public void updateNotificationChannelGroupFromPrivilegedListener( |
| 3115 | INotificationListener token, String pkg, UserHandle user, |
| 3116 | NotificationChannelGroup group) throws RemoteException { |
| 3117 | Preconditions.checkNotNull(user); |
| 3118 | verifyPrivilegedListener(token, user); |
| 3119 | createNotificationChannelGroup( |
| 3120 | pkg, getUidForPackageAndUser(pkg, user), group, false, true); |
| 3121 | savePolicyFile(); |
| 3122 | } |
| 3123 | |
| 3124 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3125 | public void updateNotificationChannelFromPrivilegedListener(INotificationListener token, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3126 | String pkg, UserHandle user, NotificationChannel channel) throws RemoteException { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3127 | Preconditions.checkNotNull(channel); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3128 | Preconditions.checkNotNull(pkg); |
| 3129 | Preconditions.checkNotNull(user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3130 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3131 | verifyPrivilegedListener(token, user); |
| 3132 | updateNotificationChannelInt(pkg, getUidForPackageAndUser(pkg, user), channel, true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | @Override |
| 3136 | public ParceledListSlice<NotificationChannel> getNotificationChannelsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3137 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 3138 | Preconditions.checkNotNull(pkg); |
| 3139 | Preconditions.checkNotNull(user); |
| 3140 | verifyPrivilegedListener(token, user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3141 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3142 | return mRankingHelper.getNotificationChannels(pkg, getUidForPackageAndUser(pkg, user), |
| 3143 | false /* includeDeleted */); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3144 | } |
| 3145 | |
| 3146 | @Override |
| 3147 | public ParceledListSlice<NotificationChannelGroup> |
| 3148 | getNotificationChannelGroupsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3149 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 3150 | Preconditions.checkNotNull(pkg); |
| 3151 | Preconditions.checkNotNull(user); |
| 3152 | verifyPrivilegedListener(token, user); |
| 3153 | |
| 3154 | List<NotificationChannelGroup> groups = new ArrayList<>(); |
| 3155 | groups.addAll(mRankingHelper.getNotificationChannelGroups( |
| 3156 | pkg, getUidForPackageAndUser(pkg, user))); |
| 3157 | return new ParceledListSlice<>(groups); |
| 3158 | } |
| 3159 | |
| 3160 | private void verifyPrivilegedListener(INotificationListener token, UserHandle user) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3161 | ManagedServiceInfo info; |
| 3162 | synchronized (mNotificationLock) { |
| 3163 | info = mListeners.checkServiceTokenLocked(token); |
| 3164 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 3165 | if (!hasCompanionDevice(info)) { |
| 3166 | throw new SecurityException(info + " does not have access"); |
| 3167 | } |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3168 | if (!info.enabledAndUserMatches(user.getIdentifier())) { |
| 3169 | throw new SecurityException(info + " does not have access"); |
| 3170 | } |
| 3171 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3172 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 3173 | private int getUidForPackageAndUser(String pkg, UserHandle user) throws RemoteException { |
| 3174 | int uid = 0; |
| 3175 | long identity = Binder.clearCallingIdentity(); |
| 3176 | try { |
| 3177 | uid = mPackageManager.getPackageUid(pkg, 0, user.getIdentifier()); |
| 3178 | } finally { |
| 3179 | Binder.restoreCallingIdentity(identity); |
| 3180 | } |
| 3181 | return uid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 3182 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 3183 | |
| 3184 | @Override |
| 3185 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 3186 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) |
| 3187 | throws RemoteException { |
| 3188 | new ShellCmd().exec(this, in, out, err, args, callback, resultReceiver); |
| 3189 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 3190 | }; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 3191 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3192 | private void applyAdjustment(NotificationRecord r, Adjustment adjustment) { |
| 3193 | if (r == null) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3194 | return; |
| 3195 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3196 | if (adjustment.getSignals() != null) { |
| 3197 | Bundle.setDefusable(adjustment.getSignals(), true); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3198 | r.addAdjustment(adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3199 | } |
| 3200 | } |
| 3201 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3202 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3203 | void addAutogroupKeyLocked(String key) { |
| 3204 | NotificationRecord r = mNotificationsByKey.get(key); |
| 3205 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3206 | return; |
| 3207 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3208 | if (r.sbn.getOverrideGroupKey() == null) { |
| 3209 | addAutoGroupAdjustment(r, GroupHelper.AUTOGROUP_KEY); |
| 3210 | EventLogTags.writeNotificationAutogrouped(key); |
| 3211 | mRankingHandler.requestSort(); |
| 3212 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3213 | } |
| 3214 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3215 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3216 | void removeAutogroupKeyLocked(String key) { |
| 3217 | NotificationRecord r = mNotificationsByKey.get(key); |
| 3218 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3219 | return; |
| 3220 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3221 | if (r.sbn.getOverrideGroupKey() != null) { |
| 3222 | addAutoGroupAdjustment(r, null); |
| 3223 | EventLogTags.writeNotificationUnautogrouped(key); |
| 3224 | mRankingHandler.requestSort(); |
| 3225 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | private void addAutoGroupAdjustment(NotificationRecord r, String overrideGroupKey) { |
| 3229 | Bundle signals = new Bundle(); |
| 3230 | signals.putString(Adjustment.KEY_GROUP_KEY, overrideGroupKey); |
| 3231 | Adjustment adjustment = |
| 3232 | new Adjustment(r.sbn.getPackageName(), r.getKey(), signals, "", r.sbn.getUserId()); |
| 3233 | r.addAdjustment(adjustment); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | // Clears the 'fake' auto-group summary. |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3237 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3238 | private void clearAutogroupSummaryLocked(int userId, String pkg) { |
| 3239 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 3240 | if (summaries != null && summaries.containsKey(pkg)) { |
| 3241 | // Clear summary. |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3242 | final NotificationRecord removed = findNotificationByKeyLocked(summaries.remove(pkg)); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3243 | if (removed != null) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 3244 | boolean wasPosted = removeFromNotificationListsLocked(removed); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 3245 | cancelNotificationLocked(removed, false, REASON_UNAUTOBUNDLED, wasPosted, null); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3246 | } |
| 3247 | } |
| 3248 | } |
| 3249 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 3250 | @GuardedBy("mNotificationLock") |
| 3251 | private boolean hasAutoGroupSummaryLocked(StatusBarNotification sbn) { |
| 3252 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(sbn.getUserId()); |
| 3253 | return summaries != null && summaries.containsKey(sbn.getPackageName()); |
| 3254 | } |
| 3255 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3256 | // 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] | 3257 | private void createAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 3258 | NotificationRecord summaryRecord = null; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3259 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3260 | NotificationRecord notificationRecord = mNotificationsByKey.get(triggeringKey); |
| 3261 | if (notificationRecord == null) { |
| 3262 | // The notification could have been cancelled again already. A successive |
| 3263 | // adjustment will post a summary if needed. |
| 3264 | return; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3265 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3266 | final StatusBarNotification adjustedSbn = notificationRecord.sbn; |
| 3267 | userId = adjustedSbn.getUser().getIdentifier(); |
| 3268 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 3269 | if (summaries == null) { |
| 3270 | summaries = new ArrayMap<>(); |
| 3271 | } |
| 3272 | mAutobundledSummaries.put(userId, summaries); |
| 3273 | if (!summaries.containsKey(pkg)) { |
| 3274 | // Add summary |
| 3275 | final ApplicationInfo appInfo = |
| 3276 | adjustedSbn.getNotification().extras.getParcelable( |
| 3277 | Notification.EXTRA_BUILDER_APPLICATION_INFO); |
| 3278 | final Bundle extras = new Bundle(); |
| 3279 | extras.putParcelable(Notification.EXTRA_BUILDER_APPLICATION_INFO, appInfo); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3280 | final String channelId = notificationRecord.getChannel().getId(); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3281 | final Notification summaryNotification = |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3282 | new Notification.Builder(getContext(), channelId) |
| 3283 | .setSmallIcon(adjustedSbn.getNotification().getSmallIcon()) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3284 | .setGroupSummary(true) |
Julia Reynolds | 9d5786e | 2017-04-28 10:26:32 -0400 | [diff] [blame] | 3285 | .setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3286 | .setGroup(GroupHelper.AUTOGROUP_KEY) |
| 3287 | .setFlag(Notification.FLAG_AUTOGROUP_SUMMARY, true) |
| 3288 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 3289 | .setColor(adjustedSbn.getNotification().color) |
| 3290 | .setLocalOnly(true) |
| 3291 | .build(); |
| 3292 | summaryNotification.extras.putAll(extras); |
| 3293 | Intent appIntent = getContext().getPackageManager().getLaunchIntentForPackage(pkg); |
| 3294 | if (appIntent != null) { |
| 3295 | summaryNotification.contentIntent = PendingIntent.getActivityAsUser( |
| 3296 | getContext(), 0, appIntent, 0, null, UserHandle.of(userId)); |
| 3297 | } |
| 3298 | final StatusBarNotification summarySbn = |
| 3299 | new StatusBarNotification(adjustedSbn.getPackageName(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3300 | adjustedSbn.getOpPkg(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3301 | Integer.MAX_VALUE, |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3302 | GroupHelper.AUTOGROUP_KEY, adjustedSbn.getUid(), |
| 3303 | adjustedSbn.getInitialPid(), summaryNotification, |
| 3304 | adjustedSbn.getUser(), GroupHelper.AUTOGROUP_KEY, |
| 3305 | System.currentTimeMillis()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 3306 | summaryRecord = new NotificationRecord(getContext(), summarySbn, |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 3307 | notificationRecord.getChannel()); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3308 | summaries.put(pkg, summarySbn.getKey()); |
| 3309 | } |
| 3310 | } |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3311 | if (summaryRecord != null && checkDisqualifyingFeatures(userId, MY_UID, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3312 | summaryRecord.sbn.getId(), summaryRecord.sbn.getTag(), summaryRecord, true)) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3313 | mHandler.post(new EnqueueNotificationRunnable(userId, summaryRecord)); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3314 | } |
| 3315 | } |
| 3316 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3317 | private String disableNotificationEffects(NotificationRecord record) { |
| 3318 | if (mDisableNotificationEffects) { |
| 3319 | return "booleanState"; |
| 3320 | } |
| 3321 | if ((mListenerHints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 3322 | return "listenerHints"; |
| 3323 | } |
| 3324 | if (mCallState != TelephonyManager.CALL_STATE_IDLE && !mZenModeHelper.isCall(record)) { |
| 3325 | return "callState"; |
| 3326 | } |
| 3327 | return null; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3328 | }; |
| 3329 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3330 | private void dumpJson(PrintWriter pw, @NonNull DumpFilter filter) { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3331 | JSONObject dump = new JSONObject(); |
| 3332 | try { |
| 3333 | dump.put("service", "Notification Manager"); |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 3334 | dump.put("bans", mRankingHelper.dumpBansJson(filter)); |
| 3335 | dump.put("ranking", mRankingHelper.dumpJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3336 | dump.put("stats", mUsageStats.dumpJson(filter)); |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 3337 | dump.put("channels", mRankingHelper.dumpChannelsJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3338 | } catch (JSONException e) { |
| 3339 | e.printStackTrace(); |
| 3340 | } |
| 3341 | pw.println(dump); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3342 | } |
| 3343 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3344 | private void dumpProto(FileDescriptor fd, @NonNull DumpFilter filter) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3345 | final ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 3346 | synchronized (mNotificationLock) { |
| 3347 | long records = proto.start(NotificationServiceDumpProto.RECORDS); |
| 3348 | int N = mNotificationList.size(); |
| 3349 | if (N > 0) { |
| 3350 | for (int i = 0; i < N; i++) { |
| 3351 | final NotificationRecord nr = mNotificationList.get(i); |
| 3352 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3353 | nr.dump(proto, filter.redact); |
Yi Jin | c7f9307 | 2017-09-29 15:29:38 -0700 | [diff] [blame] | 3354 | proto.write(NotificationRecordProto.STATE, NotificationRecordProto.POSTED); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3355 | } |
| 3356 | } |
| 3357 | N = mEnqueuedNotifications.size(); |
| 3358 | if (N > 0) { |
| 3359 | for (int i = 0; i < N; i++) { |
| 3360 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 3361 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3362 | nr.dump(proto, filter.redact); |
Yi Jin | c7f9307 | 2017-09-29 15:29:38 -0700 | [diff] [blame] | 3363 | proto.write(NotificationRecordProto.STATE, NotificationRecordProto.ENQUEUED); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3364 | } |
| 3365 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3366 | List<NotificationRecord> snoozed = mSnoozeHelper.getSnoozed(); |
| 3367 | N = snoozed.size(); |
| 3368 | if (N > 0) { |
| 3369 | for (int i = 0; i < N; i++) { |
| 3370 | final NotificationRecord nr = snoozed.get(i); |
| 3371 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3372 | nr.dump(proto, filter.redact); |
Yi Jin | c7f9307 | 2017-09-29 15:29:38 -0700 | [diff] [blame] | 3373 | proto.write(NotificationRecordProto.STATE, NotificationRecordProto.SNOOZED); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3374 | } |
| 3375 | } |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3376 | proto.end(records); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3377 | |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3378 | long zenLog = proto.start(NotificationServiceDumpProto.ZEN); |
| 3379 | mZenModeHelper.dump(proto); |
| 3380 | for (ComponentName suppressor : mEffectsSuppressors) { |
| 3381 | proto.write(ZenModeProto.SUPPRESSORS, suppressor.toString()); |
| 3382 | } |
| 3383 | proto.end(zenLog); |
| 3384 | |
| 3385 | long listenersToken = proto.start(NotificationServiceDumpProto.NOTIFICATION_LISTENERS); |
| 3386 | mListeners.dump(proto, filter); |
| 3387 | proto.end(listenersToken); |
| 3388 | |
| 3389 | proto.write(NotificationServiceDumpProto.LISTENER_HINTS, mListenerHints); |
| 3390 | |
| 3391 | for (int i = 0; i < mListenersDisablingEffects.size(); ++i) { |
| 3392 | long effectsToken = proto.start( |
| 3393 | NotificationServiceDumpProto.LISTENERS_DISABLING_EFFECTS); |
| 3394 | |
| 3395 | proto.write( |
| 3396 | ListenersDisablingEffectsProto.HINT, mListenersDisablingEffects.keyAt(i)); |
| 3397 | final ArraySet<ManagedServiceInfo> listeners = |
| 3398 | mListenersDisablingEffects.valueAt(i); |
| 3399 | for (int j = 0; j < listeners.size(); j++) { |
| 3400 | final ManagedServiceInfo listener = listeners.valueAt(i); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 3401 | listener.writeToProto(proto, ListenersDisablingEffectsProto.LISTENERS, null); |
Kweku Adams | 93304b6 | 2017-09-20 17:03:00 -0700 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | proto.end(effectsToken); |
| 3405 | } |
| 3406 | |
| 3407 | long assistantsToken = proto.start( |
| 3408 | NotificationServiceDumpProto.NOTIFICATION_ASSISTANTS); |
| 3409 | mAssistants.dump(proto, filter); |
| 3410 | proto.end(assistantsToken); |
| 3411 | |
| 3412 | long conditionsToken = proto.start(NotificationServiceDumpProto.CONDITION_PROVIDERS); |
| 3413 | mConditionProviders.dump(proto, filter); |
| 3414 | proto.end(conditionsToken); |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 3415 | |
| 3416 | long rankingToken = proto.start(NotificationServiceDumpProto.RANKING_CONFIG); |
| 3417 | mRankingHelper.dump(proto, filter); |
| 3418 | proto.end(rankingToken); |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3419 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3420 | |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3421 | proto.flush(); |
| 3422 | } |
| 3423 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 3424 | void dumpImpl(PrintWriter pw, @NonNull DumpFilter filter) { |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3425 | pw.print("Current Notification Manager state"); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3426 | if (filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3427 | pw.print(" (filtered to "); pw.print(filter); pw.print(")"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3428 | } |
| 3429 | pw.println(':'); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3430 | int N; |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3431 | final boolean zenOnly = filter.filtered && filter.zen; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3432 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3433 | if (!zenOnly) { |
| 3434 | synchronized (mToastQueue) { |
| 3435 | N = mToastQueue.size(); |
| 3436 | if (N > 0) { |
| 3437 | pw.println(" Toast Queue:"); |
| 3438 | for (int i=0; i<N; i++) { |
| 3439 | mToastQueue.get(i).dump(pw, " ", filter); |
| 3440 | } |
| 3441 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3442 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3443 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3444 | } |
| 3445 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3446 | synchronized (mNotificationLock) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3447 | if (!zenOnly) { |
| 3448 | N = mNotificationList.size(); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3449 | if (N > 0) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3450 | pw.println(" Notification List:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3451 | for (int i=0; i<N; i++) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3452 | final NotificationRecord nr = mNotificationList.get(i); |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3453 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3454 | nr.dump(pw, " ", getContext(), filter.redact); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3455 | } |
| 3456 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3457 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3458 | |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3459 | if (!filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3460 | N = mLights.size(); |
| 3461 | if (N > 0) { |
| 3462 | pw.println(" Lights List:"); |
| 3463 | for (int i=0; i<N; i++) { |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3464 | if (i == N - 1) { |
| 3465 | pw.print(" > "); |
| 3466 | } else { |
| 3467 | pw.print(" "); |
| 3468 | } |
| 3469 | pw.println(mLights.get(i)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3470 | } |
| 3471 | pw.println(" "); |
| 3472 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 3473 | pw.println(" mUseAttentionLight=" + mUseAttentionLight); |
| 3474 | pw.println(" mNotificationPulseEnabled=" + mNotificationPulseEnabled); |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3475 | pw.println(" mSoundNotificationKey=" + mSoundNotificationKey); |
| 3476 | pw.println(" mVibrateNotificationKey=" + mVibrateNotificationKey); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3477 | pw.println(" mDisableNotificationEffects=" + mDisableNotificationEffects); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3478 | pw.println(" mCallState=" + callStateToString(mCallState)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3479 | pw.println(" mSystemReady=" + mSystemReady); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 3480 | pw.println(" mMaxPackageEnqueueRate=" + mMaxPackageEnqueueRate); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3481 | } |
| 3482 | pw.println(" mArchive=" + mArchive.toString()); |
| 3483 | Iterator<StatusBarNotification> iter = mArchive.descendingIterator(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3484 | int j=0; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3485 | while (iter.hasNext()) { |
| 3486 | final StatusBarNotification sbn = iter.next(); |
| 3487 | if (filter != null && !filter.matches(sbn)) continue; |
| 3488 | pw.println(" " + sbn); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3489 | if (++j >= 5) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3490 | if (iter.hasNext()) pw.println(" ..."); |
| 3491 | break; |
| 3492 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3493 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3494 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3495 | if (!zenOnly) { |
| 3496 | N = mEnqueuedNotifications.size(); |
| 3497 | if (N > 0) { |
| 3498 | pw.println(" Enqueued Notification List:"); |
| 3499 | for (int i = 0; i < N; i++) { |
| 3500 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 3501 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3502 | nr.dump(pw, " ", getContext(), filter.redact); |
| 3503 | } |
| 3504 | pw.println(" "); |
| 3505 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3506 | |
| 3507 | mSnoozeHelper.dump(pw, filter); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3508 | } |
| 3509 | } |
| 3510 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3511 | if (!zenOnly) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3512 | pw.println("\n Ranking Config:"); |
| 3513 | mRankingHelper.dump(pw, " ", filter); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 3514 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3515 | pw.println("\n Notification listeners:"); |
| 3516 | mListeners.dump(pw, filter); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3517 | pw.print(" mListenerHints: "); pw.println(mListenerHints); |
| 3518 | pw.print(" mListenersDisablingEffects: ("); |
| 3519 | N = mListenersDisablingEffects.size(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3520 | for (int i = 0; i < N; i++) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 3521 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 3522 | if (i > 0) pw.print(';'); |
| 3523 | pw.print("hint[" + hint + "]:"); |
| 3524 | |
| 3525 | final ArraySet<ManagedServiceInfo> listeners = |
| 3526 | mListenersDisablingEffects.valueAt(i); |
| 3527 | final int listenerSize = listeners.size(); |
| 3528 | |
| 3529 | for (int j = 0; j < listenerSize; j++) { |
| 3530 | if (i > 0) pw.print(','); |
| 3531 | final ManagedServiceInfo listener = listeners.valueAt(i); |
| 3532 | pw.print(listener.component); |
| 3533 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3534 | } |
| 3535 | pw.println(')'); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 3536 | pw.println("\n Notification assistant services:"); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3537 | mAssistants.dump(pw, filter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3538 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3539 | |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3540 | if (!filter.filtered || zenOnly) { |
| 3541 | pw.println("\n Zen Mode:"); |
| 3542 | pw.print(" mInterruptionFilter="); pw.println(mInterruptionFilter); |
| 3543 | mZenModeHelper.dump(pw, " "); |
| 3544 | |
| 3545 | pw.println("\n Zen Log:"); |
| 3546 | ZenLog.dump(pw, " "); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3547 | } |
| 3548 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3549 | pw.println("\n Condition providers:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3550 | mConditionProviders.dump(pw, filter); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3551 | |
| 3552 | pw.println("\n Group summaries:"); |
| 3553 | for (Entry<String, NotificationRecord> entry : mSummaryByGroupKey.entrySet()) { |
| 3554 | NotificationRecord r = entry.getValue(); |
| 3555 | pw.println(" " + entry.getKey() + " -> " + r.getKey()); |
| 3556 | if (mNotificationsByKey.get(r.getKey()) != r) { |
| 3557 | pw.println("!!!!!!LEAK: Record not found in mNotificationsByKey."); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3558 | r.dump(pw, " ", getContext(), filter.redact); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3559 | } |
| 3560 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3561 | |
| 3562 | if (!zenOnly) { |
| 3563 | pw.println("\n Usage Stats:"); |
| 3564 | mUsageStats.dump(pw, " ", filter); |
| 3565 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3566 | } |
| 3567 | } |
| 3568 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3569 | /** |
| 3570 | * The private API only accessible to the system process. |
| 3571 | */ |
| 3572 | private final NotificationManagerInternal mInternalService = new NotificationManagerInternal() { |
| 3573 | @Override |
Julia Reynolds | f3de8aa | 2017-09-29 15:52:37 -0400 | [diff] [blame] | 3574 | public NotificationChannel getNotificationChannel(String pkg, int uid, String |
| 3575 | channelId) { |
| 3576 | return mRankingHelper.getNotificationChannel(pkg, uid, channelId, false); |
| 3577 | } |
| 3578 | |
| 3579 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3580 | public void enqueueNotification(String pkg, String opPkg, int callingUid, int callingPid, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3581 | String tag, int id, Notification notification, int userId) { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3582 | enqueueNotificationInternal(pkg, opPkg, callingUid, callingPid, tag, id, notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3583 | userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3584 | } |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3585 | |
| 3586 | @Override |
| 3587 | public void removeForegroundServiceFlagFromNotification(String pkg, int notificationId, |
| 3588 | int userId) { |
| 3589 | checkCallerIsSystem(); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3590 | mHandler.post(new Runnable() { |
| 3591 | @Override |
| 3592 | public void run() { |
| 3593 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3594 | removeForegroundServiceFlagByListLocked( |
| 3595 | mEnqueuedNotifications, pkg, notificationId, userId); |
| 3596 | removeForegroundServiceFlagByListLocked( |
| 3597 | mNotificationList, pkg, notificationId, userId); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3598 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3599 | } |
| 3600 | }); |
| 3601 | } |
| 3602 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3603 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3604 | private void removeForegroundServiceFlagByListLocked( |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3605 | ArrayList<NotificationRecord> notificationList, String pkg, int notificationId, |
| 3606 | int userId) { |
| 3607 | NotificationRecord r = findNotificationByListLocked( |
| 3608 | notificationList, pkg, null, notificationId, userId); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3609 | if (r == null) { |
| 3610 | return; |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3611 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3612 | StatusBarNotification sbn = r.sbn; |
| 3613 | // NoMan adds flags FLAG_NO_CLEAR and FLAG_ONGOING_EVENT when it sees |
| 3614 | // FLAG_FOREGROUND_SERVICE. Hence it's not enough to remove |
| 3615 | // FLAG_FOREGROUND_SERVICE, we have to revert to the flags we received |
| 3616 | // initially *and* force remove FLAG_FOREGROUND_SERVICE. |
| 3617 | sbn.getNotification().flags = |
| 3618 | (r.mOriginalFlags & ~Notification.FLAG_FOREGROUND_SERVICE); |
| 3619 | mRankingHelper.sort(mNotificationList); |
| 3620 | mListeners.notifyPostedLocked(sbn, sbn /* oldSbn */); |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3621 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3622 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3623 | |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3624 | void enqueueNotificationInternal(final String pkg, final String opPkg, final int callingUid, |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 3625 | final int callingPid, final String tag, final int id, final Notification notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3626 | int incomingUserId) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 3627 | if (DBG) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3628 | Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id |
| 3629 | + " notification=" + notification); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 3630 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3631 | checkCallerIsSystemOrSameApp(pkg); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3632 | |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 3633 | final int userId = ActivityManager.handleIncomingUser(callingPid, |
| 3634 | callingUid, incomingUserId, true, false, "enqueueNotification", pkg); |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 3635 | final UserHandle user = new UserHandle(userId); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3636 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3637 | if (pkg == null || notification == null) { |
| 3638 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 3639 | + " id=" + id + " notification=" + notification); |
| 3640 | } |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3641 | |
| 3642 | // The system can post notifications for any package, let us resolve that. |
| 3643 | final int notificationUid = resolveNotificationUid(opPkg, callingUid, userId); |
| 3644 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3645 | // Fix the notification as best we can. |
| 3646 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3647 | final ApplicationInfo ai = mPackageManagerClient.getApplicationInfoAsUser( |
Jeff Sharkey | 012bc7b | 2016-04-11 16:30:27 -0600 | [diff] [blame] | 3648 | pkg, PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 3649 | (userId == UserHandle.USER_ALL) ? UserHandle.USER_SYSTEM : userId); |
Julia Reynolds | e071abd | 2017-03-22 10:52:11 -0400 | [diff] [blame] | 3650 | Notification.addFieldsFromContext(ai, notification); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3651 | |
| 3652 | int canColorize = mPackageManagerClient.checkPermission( |
| 3653 | android.Manifest.permission.USE_COLORIZED_NOTIFICATIONS, pkg); |
| 3654 | if (canColorize == PERMISSION_GRANTED) { |
| 3655 | notification.flags |= Notification.FLAG_CAN_COLORIZE; |
| 3656 | } else { |
| 3657 | notification.flags &= ~Notification.FLAG_CAN_COLORIZE; |
| 3658 | } |
| 3659 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3660 | } catch (NameNotFoundException e) { |
| 3661 | Slog.e(TAG, "Cannot create a context for sending app", e); |
| 3662 | return; |
| 3663 | } |
| 3664 | |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 3665 | mUsageStats.registerEnqueuedByApp(pkg); |
| 3666 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3667 | // setup local book-keeping |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 3668 | String channelId = notification.getChannelId(); |
| 3669 | if (mIsTelevision && (new Notification.TvExtender(notification)).getChannelId() != null) { |
| 3670 | channelId = (new Notification.TvExtender(notification)).getChannelId(); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 3671 | } |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3672 | final NotificationChannel channel = mRankingHelper.getNotificationChannel(pkg, |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3673 | notificationUid, channelId, false /* includeDeleted */); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3674 | if (channel == null) { |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3675 | final String noChannelStr = "No Channel found for " |
| 3676 | + "pkg=" + pkg |
| 3677 | + ", channelId=" + channelId |
Julia Reynolds | f26eb91 | 2017-05-22 15:47:06 -0400 | [diff] [blame] | 3678 | + ", id=" + id |
| 3679 | + ", tag=" + tag |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3680 | + ", opPkg=" + opPkg |
| 3681 | + ", callingUid=" + callingUid |
| 3682 | + ", userId=" + userId |
| 3683 | + ", incomingUserId=" + incomingUserId |
| 3684 | + ", notificationUid=" + notificationUid |
| 3685 | + ", notification=" + notification; |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3686 | Log.e(TAG, noChannelStr); |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 3687 | doChannelWarningToast("Developer warning for package \"" + pkg + "\"\n" + |
Geoffrey Pitsch | cadb5dc | 2017-04-11 11:35:02 -0400 | [diff] [blame] | 3688 | "Failed to post notification on channel \"" + channelId + "\"\n" + |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3689 | "See log for more details"); |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3690 | return; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3691 | } |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3692 | |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 3693 | final StatusBarNotification n = new StatusBarNotification( |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3694 | pkg, opPkg, id, tag, notificationUid, callingPid, notification, |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3695 | user, null, System.currentTimeMillis()); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 3696 | final NotificationRecord r = new NotificationRecord(getContext(), n, channel); |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 3697 | |
Julia Reynolds | 8617e4e | 2017-09-18 16:52:37 -0400 | [diff] [blame] | 3698 | if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0 |
| 3699 | && (channel.getUserLockedFields() & NotificationChannel.USER_LOCKED_IMPORTANCE) == 0 |
| 3700 | && (r.getImportance() == IMPORTANCE_MIN || r.getImportance() == IMPORTANCE_NONE)) { |
| 3701 | // Increase the importance of foreground service notifications unless the user had an |
| 3702 | // opinion otherwise |
| 3703 | if (TextUtils.isEmpty(channelId) |
| 3704 | || NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 3705 | r.setImportance(IMPORTANCE_LOW, "Bumped for foreground service"); |
| 3706 | } else { |
| 3707 | channel.setImportance(IMPORTANCE_LOW); |
| 3708 | mRankingHelper.updateNotificationChannel(pkg, notificationUid, channel, false); |
| 3709 | r.updateNotificationChannel(channel); |
| 3710 | } |
| 3711 | } |
| 3712 | |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3713 | if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r, |
| 3714 | r.sbn.getOverrideGroupKey() != null)) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3715 | return; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 3716 | } |
| 3717 | |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 3718 | // Whitelist pending intents. |
| 3719 | if (notification.allPendingIntents != null) { |
| 3720 | final int intentCount = notification.allPendingIntents.size(); |
| 3721 | if (intentCount > 0) { |
| 3722 | final ActivityManagerInternal am = LocalServices |
| 3723 | .getService(ActivityManagerInternal.class); |
| 3724 | final long duration = LocalServices.getService( |
| 3725 | DeviceIdleController.LocalService.class).getNotificationWhitelistDuration(); |
| 3726 | for (int i = 0; i < intentCount; i++) { |
| 3727 | PendingIntent pendingIntent = notification.allPendingIntents.valueAt(i); |
| 3728 | if (pendingIntent != null) { |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 3729 | am.setPendingIntentWhitelistDuration(pendingIntent.getTarget(), |
| 3730 | WHITELIST_TOKEN, duration); |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 3731 | } |
| 3732 | } |
| 3733 | } |
| 3734 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 3735 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3736 | mHandler.post(new EnqueueNotificationRunnable(userId, r)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3737 | } |
| 3738 | |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 3739 | private void doChannelWarningToast(CharSequence toastText) { |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 3740 | final int defaultWarningEnabled = Build.IS_DEBUGGABLE ? 1 : 0; |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 3741 | final boolean warningEnabled = Settings.Global.getInt(getContext().getContentResolver(), |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 3742 | Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, defaultWarningEnabled) != 0; |
| 3743 | if (warningEnabled) { |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 3744 | Toast toast = Toast.makeText(getContext(), mHandler.getLooper(), toastText, |
Geoffrey Pitsch | 5bdddbd | 2017-05-26 10:50:05 -0400 | [diff] [blame] | 3745 | Toast.LENGTH_SHORT); |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 3746 | toast.show(); |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3747 | } |
| 3748 | } |
| 3749 | |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3750 | private int resolveNotificationUid(String opPackageName, int callingUid, int userId) { |
| 3751 | // The system can post notifications on behalf of any package it wants |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3752 | if (isCallerSystemOrPhone() && opPackageName != null && !"android".equals(opPackageName)) { |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3753 | try { |
| 3754 | return getContext().getPackageManager() |
| 3755 | .getPackageUidAsUser(opPackageName, userId); |
| 3756 | } catch (NameNotFoundException e) { |
| 3757 | /* ignore */ |
| 3758 | } |
| 3759 | } |
| 3760 | return callingUid; |
| 3761 | } |
| 3762 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3763 | /** |
| 3764 | * Checks if a notification can be posted. checks rate limiter, snooze helper, and blocking. |
| 3765 | * |
| 3766 | * Has side effects. |
| 3767 | */ |
| 3768 | private boolean checkDisqualifyingFeatures(int userId, int callingUid, int id, String tag, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3769 | NotificationRecord r, boolean isAutogroup) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3770 | final String pkg = r.sbn.getPackageName(); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3771 | final boolean isSystemNotification = |
| 3772 | isUidSystemOrPhone(callingUid) || ("android".equals(pkg)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3773 | final boolean isNotificationFromListener = mListeners.isListenerPackage(pkg); |
| 3774 | |
| 3775 | // Limit the number of notifications that any given package except the android |
| 3776 | // package or a registered listener can enqueue. Prevents DOS attacks and deals with leaks. |
| 3777 | if (!isSystemNotification && !isNotificationFromListener) { |
| 3778 | synchronized (mNotificationLock) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3779 | if (mNotificationsByKey.get(r.sbn.getKey()) == null && isCallerInstantApp(pkg)) { |
| 3780 | // Ephemeral apps have some special constraints for notifications. |
| 3781 | // They are not allowed to create new notifications however they are allowed to |
| 3782 | // update notifications created by the system (e.g. a foreground service |
| 3783 | // notification). |
| 3784 | throw new SecurityException("Instant app " + pkg |
| 3785 | + " cannot create notifications"); |
| 3786 | } |
| 3787 | |
| 3788 | // rate limit updates that aren't completed progress notifications |
| 3789 | if (mNotificationsByKey.get(r.sbn.getKey()) != null |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3790 | && !r.getNotification().hasCompletedProgress() |
| 3791 | && !isAutogroup) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3792 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3793 | final float appEnqueueRate = mUsageStats.getAppEnqueueRate(pkg); |
| 3794 | if (appEnqueueRate > mMaxPackageEnqueueRate) { |
| 3795 | mUsageStats.registerOverRateQuota(pkg); |
| 3796 | final long now = SystemClock.elapsedRealtime(); |
| 3797 | if ((now - mLastOverRateLogTime) > MIN_PACKAGE_OVERRATE_LOG_INTERVAL) { |
| 3798 | Slog.e(TAG, "Package enqueue rate is " + appEnqueueRate |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3799 | + ". Shedding " + r.sbn.getKey() + ". package=" + pkg); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3800 | mLastOverRateLogTime = now; |
| 3801 | } |
| 3802 | return false; |
| 3803 | } |
| 3804 | } |
| 3805 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3806 | // limit the number of outstanding notificationrecords an app can have |
| 3807 | int count = getNotificationCountLocked(pkg, userId, id, tag); |
| 3808 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 3809 | mUsageStats.registerOverCountQuota(pkg); |
| 3810 | Slog.e(TAG, "Package has already posted or enqueued " + count |
| 3811 | + " notifications. Not showing more. package=" + pkg); |
| 3812 | return false; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3813 | } |
| 3814 | } |
| 3815 | } |
| 3816 | |
| 3817 | // snoozed apps |
| 3818 | if (mSnoozeHelper.isSnoozed(userId, pkg, r.getKey())) { |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3819 | MetricsLogger.action(r.getLogMaker() |
| 3820 | .setType(MetricsProto.MetricsEvent.TYPE_UPDATE) |
| 3821 | .setCategory(MetricsProto.MetricsEvent.NOTIFICATION_SNOOZED)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3822 | if (DBG) { |
| 3823 | Slog.d(TAG, "Ignored enqueue for snoozed notification " + r.getKey()); |
| 3824 | } |
| 3825 | mSnoozeHelper.update(userId, r); |
| 3826 | savePolicyFile(); |
| 3827 | return false; |
| 3828 | } |
| 3829 | |
| 3830 | |
| 3831 | // blocked apps |
| 3832 | if (isBlocked(r, mUsageStats)) { |
| 3833 | return false; |
| 3834 | } |
| 3835 | |
| 3836 | return true; |
| 3837 | } |
| 3838 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3839 | protected int getNotificationCountLocked(String pkg, int userId, int excludedId, |
| 3840 | String excludedTag) { |
| 3841 | int count = 0; |
| 3842 | final int N = mNotificationList.size(); |
| 3843 | for (int i = 0; i < N; i++) { |
| 3844 | final NotificationRecord existing = mNotificationList.get(i); |
| 3845 | if (existing.sbn.getPackageName().equals(pkg) |
| 3846 | && existing.sbn.getUserId() == userId) { |
| 3847 | if (existing.sbn.getId() == excludedId |
| 3848 | && TextUtils.equals(existing.sbn.getTag(), excludedTag)) { |
| 3849 | continue; |
| 3850 | } |
| 3851 | count++; |
| 3852 | } |
| 3853 | } |
| 3854 | final int M = mEnqueuedNotifications.size(); |
| 3855 | for (int i = 0; i < M; i++) { |
| 3856 | final NotificationRecord existing = mEnqueuedNotifications.get(i); |
| 3857 | if (existing.sbn.getPackageName().equals(pkg) |
| 3858 | && existing.sbn.getUserId() == userId) { |
| 3859 | count++; |
| 3860 | } |
| 3861 | } |
| 3862 | return count; |
| 3863 | } |
| 3864 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3865 | protected boolean isBlocked(NotificationRecord r, NotificationUsageStats usageStats) { |
| 3866 | final String pkg = r.sbn.getPackageName(); |
| 3867 | final int callingUid = r.sbn.getUid(); |
| 3868 | |
| 3869 | final boolean isPackageSuspended = isPackageSuspendedForUser(pkg, callingUid); |
| 3870 | if (isPackageSuspended) { |
| 3871 | Slog.e(TAG, "Suppressing notification from package due to package " |
| 3872 | + "suspended by administrator."); |
| 3873 | usageStats.registerSuspendedByAdmin(r); |
| 3874 | return isPackageSuspended; |
| 3875 | } |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 3876 | final boolean isBlocked = |
Julia Reynolds | 005c8b9 | 2017-08-24 10:35:53 -0400 | [diff] [blame] | 3877 | mRankingHelper.isGroupBlocked(pkg, callingUid, r.getChannel().getGroup()) |
| 3878 | || mRankingHelper.getImportance(pkg, callingUid) |
| 3879 | == NotificationManager.IMPORTANCE_NONE |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 3880 | || r.getChannel().getImportance() == NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3881 | if (isBlocked) { |
| 3882 | Slog.e(TAG, "Suppressing notification from package by user request."); |
| 3883 | usageStats.registerBlocked(r); |
| 3884 | } |
| 3885 | return isBlocked; |
| 3886 | } |
| 3887 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3888 | protected class SnoozeNotificationRunnable implements Runnable { |
| 3889 | private final String mKey; |
| 3890 | private final long mDuration; |
| 3891 | private final String mSnoozeCriterionId; |
| 3892 | |
| 3893 | SnoozeNotificationRunnable(String key, long duration, String snoozeCriterionId) { |
| 3894 | mKey = key; |
| 3895 | mDuration = duration; |
| 3896 | mSnoozeCriterionId = snoozeCriterionId; |
| 3897 | } |
| 3898 | |
| 3899 | @Override |
| 3900 | public void run() { |
| 3901 | synchronized (mNotificationLock) { |
| 3902 | final NotificationRecord r = findNotificationByKeyLocked(mKey); |
| 3903 | if (r != null) { |
| 3904 | snoozeLocked(r); |
| 3905 | } |
| 3906 | } |
| 3907 | } |
| 3908 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3909 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3910 | void snoozeLocked(NotificationRecord r) { |
| 3911 | if (r.sbn.isGroup()) { |
| 3912 | final List<NotificationRecord> groupNotifications = findGroupNotificationsLocked( |
| 3913 | r.sbn.getPackageName(), r.sbn.getGroupKey(), r.sbn.getUserId()); |
| 3914 | if (r.getNotification().isGroupSummary()) { |
| 3915 | // snooze summary and all children |
| 3916 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 3917 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 3918 | } |
| 3919 | } else { |
| 3920 | // if there is a valid summary for this group, and we are snoozing the only |
| 3921 | // child, also snooze the summary |
| 3922 | if (mSummaryByGroupKey.containsKey(r.sbn.getGroupKey())) { |
| 3923 | if (groupNotifications.size() != 2) { |
| 3924 | snoozeNotificationLocked(r); |
| 3925 | } else { |
| 3926 | // snooze summary and the one child |
| 3927 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 3928 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 3929 | } |
| 3930 | } |
| 3931 | } else { |
| 3932 | snoozeNotificationLocked(r); |
| 3933 | } |
| 3934 | } |
| 3935 | } else { |
| 3936 | // just snooze the one notification |
| 3937 | snoozeNotificationLocked(r); |
| 3938 | } |
| 3939 | } |
| 3940 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3941 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3942 | void snoozeNotificationLocked(NotificationRecord r) { |
| 3943 | MetricsLogger.action(r.getLogMaker() |
| 3944 | .setCategory(MetricsEvent.NOTIFICATION_SNOOZED) |
| 3945 | .setType(MetricsEvent.TYPE_CLOSE) |
Chris Wren | 21a2e72 | 2017-10-02 17:44:53 -0400 | [diff] [blame] | 3946 | .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_SNOOZE_DURATION_MS, |
| 3947 | mDuration) |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3948 | .addTaggedData(MetricsEvent.NOTIFICATION_SNOOZED_CRITERIA, |
| 3949 | mSnoozeCriterionId == null ? 0 : 1)); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 3950 | boolean wasPosted = removeFromNotificationListsLocked(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 3951 | cancelNotificationLocked(r, false, REASON_SNOOZED, wasPosted, null); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3952 | updateLightsLocked(); |
| 3953 | if (mSnoozeCriterionId != null) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3954 | mAssistants.notifyAssistantSnoozedLocked(r.sbn, mSnoozeCriterionId); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3955 | mSnoozeHelper.snooze(r); |
| 3956 | } else { |
| 3957 | mSnoozeHelper.snooze(r, mDuration); |
| 3958 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 3959 | r.recordSnoozed(); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3960 | savePolicyFile(); |
| 3961 | } |
| 3962 | } |
| 3963 | |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 3964 | protected class EnqueueNotificationRunnable implements Runnable { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3965 | private final NotificationRecord r; |
| 3966 | private final int userId; |
| 3967 | |
| 3968 | EnqueueNotificationRunnable(int userId, NotificationRecord r) { |
| 3969 | this.userId = userId; |
| 3970 | this.r = r; |
| 3971 | }; |
| 3972 | |
| 3973 | @Override |
| 3974 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3975 | synchronized (mNotificationLock) { |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 3976 | mEnqueuedNotifications.add(r); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 3977 | scheduleTimeoutLocked(r); |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 3978 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3979 | final StatusBarNotification n = r.sbn; |
| 3980 | if (DBG) Slog.d(TAG, "EnqueueNotificationRunnable.run for: " + n.getKey()); |
| 3981 | NotificationRecord old = mNotificationsByKey.get(n.getKey()); |
| 3982 | if (old != null) { |
| 3983 | // Retain ranking information from previous record |
| 3984 | r.copyRankingInformation(old); |
| 3985 | } |
| 3986 | |
| 3987 | final int callingUid = n.getUid(); |
| 3988 | final int callingPid = n.getInitialPid(); |
| 3989 | final Notification notification = n.getNotification(); |
| 3990 | final String pkg = n.getPackageName(); |
| 3991 | final int id = n.getId(); |
| 3992 | final String tag = n.getTag(); |
| 3993 | |
| 3994 | // Handle grouped notifications and bail out early if we |
| 3995 | // can to avoid extracting signals. |
| 3996 | handleGroupedNotificationLocked(r, old, callingUid, callingPid); |
| 3997 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3998 | // if this is a group child, unsnooze parent summary |
| 3999 | if (n.isGroup() && notification.isGroupChild()) { |
| 4000 | mSnoozeHelper.repostGroupSummary(pkg, r.getUserId(), n.getGroupKey()); |
| 4001 | } |
| 4002 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4003 | // This conditional is a dirty hack to limit the logging done on |
| 4004 | // behalf of the download manager without affecting other apps. |
| 4005 | if (!pkg.equals("com.android.providers.downloads") |
| 4006 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| 4007 | int enqueueStatus = EVENTLOG_ENQUEUE_STATUS_NEW; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4008 | if (old != null) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4009 | enqueueStatus = EVENTLOG_ENQUEUE_STATUS_UPDATE; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4010 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4011 | EventLogTags.writeNotificationEnqueue(callingUid, callingPid, |
| 4012 | pkg, id, tag, userId, notification.toString(), |
| 4013 | enqueueStatus); |
| 4014 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4015 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4016 | mRankingHelper.extractSignals(r); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4017 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4018 | // tell the assistant service about the notification |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4019 | if (mAssistants.isEnabled()) { |
| 4020 | mAssistants.onNotificationEnqueued(r); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4021 | mHandler.postDelayed(new PostNotificationRunnable(r.getKey()), |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4022 | DELAY_FOR_ASSISTANT_TIME); |
| 4023 | } else { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4024 | mHandler.post(new PostNotificationRunnable(r.getKey())); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4025 | } |
| 4026 | } |
| 4027 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4028 | } |
| 4029 | |
| 4030 | protected class PostNotificationRunnable implements Runnable { |
| 4031 | private final String key; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4032 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 4033 | PostNotificationRunnable(String key) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4034 | this.key = key; |
| 4035 | } |
| 4036 | |
| 4037 | @Override |
| 4038 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4039 | synchronized (mNotificationLock) { |
| 4040 | try { |
| 4041 | NotificationRecord r = null; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4042 | int N = mEnqueuedNotifications.size(); |
| 4043 | for (int i = 0; i < N; i++) { |
| 4044 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 4045 | if (Objects.equals(key, enqueued.getKey())) { |
| 4046 | r = enqueued; |
| 4047 | break; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4048 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4049 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4050 | if (r == null) { |
| 4051 | Slog.i(TAG, "Cannot find enqueued record for key: " + key); |
| 4052 | return; |
| 4053 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4054 | NotificationRecord old = mNotificationsByKey.get(key); |
| 4055 | final StatusBarNotification n = r.sbn; |
| 4056 | final Notification notification = n.getNotification(); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4057 | int index = indexOfNotificationLocked(n.getKey()); |
| 4058 | if (index < 0) { |
| 4059 | mNotificationList.add(r); |
| 4060 | mUsageStats.registerPostedByApp(r); |
| 4061 | } else { |
| 4062 | old = mNotificationList.get(index); |
| 4063 | mNotificationList.set(index, r); |
| 4064 | mUsageStats.registerUpdatedByApp(r, old); |
| 4065 | // Make sure we don't lose the foreground service state. |
| 4066 | notification.flags |= |
| 4067 | old.getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE; |
| 4068 | r.isUpdate = true; |
| 4069 | } |
| 4070 | |
| 4071 | mNotificationsByKey.put(n.getKey(), r); |
| 4072 | |
| 4073 | // Ensure if this is a foreground service that the proper additional |
| 4074 | // flags are set. |
| 4075 | if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 4076 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 4077 | | Notification.FLAG_NO_CLEAR; |
| 4078 | } |
| 4079 | |
| 4080 | applyZenModeLocked(r); |
| 4081 | mRankingHelper.sort(mNotificationList); |
| 4082 | |
| 4083 | if (notification.getSmallIcon() != null) { |
| 4084 | StatusBarNotification oldSbn = (old != null) ? old.sbn : null; |
| 4085 | mListeners.notifyPostedLocked(n, oldSbn); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 4086 | if (oldSbn == null || !Objects.equals(oldSbn.getGroup(), n.getGroup())) { |
| 4087 | mHandler.post(new Runnable() { |
| 4088 | @Override |
| 4089 | public void run() { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 4090 | mGroupHelper.onNotificationPosted( |
| 4091 | n, hasAutoGroupSummaryLocked(n)); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 4092 | } |
| 4093 | }); |
| 4094 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4095 | } else { |
| 4096 | Slog.e(TAG, "Not posting notification without small icon: " + notification); |
| 4097 | if (old != null && !old.isCanceled) { |
| 4098 | mListeners.notifyRemovedLocked(n, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4099 | NotificationListenerService.REASON_ERROR, null); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4100 | mHandler.post(new Runnable() { |
| 4101 | @Override |
| 4102 | public void run() { |
| 4103 | mGroupHelper.onNotificationRemoved(n); |
| 4104 | } |
| 4105 | }); |
| 4106 | } |
| 4107 | // ATTENTION: in a future release we will bail out here |
| 4108 | // so that we do not play sounds, show lights, etc. for invalid |
| 4109 | // notifications |
| 4110 | Slog.e(TAG, "WARNING: In a future release this will crash the app: " |
| 4111 | + n.getPackageName()); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4112 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4113 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4114 | buzzBeepBlinkLocked(r); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4115 | } finally { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 4116 | int N = mEnqueuedNotifications.size(); |
| 4117 | for (int i = 0; i < N; i++) { |
| 4118 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 4119 | if (Objects.equals(key, enqueued.getKey())) { |
| 4120 | mEnqueuedNotifications.remove(i); |
| 4121 | break; |
| 4122 | } |
| 4123 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 4124 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4125 | } |
| 4126 | } |
| 4127 | } |
| 4128 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4129 | /** |
| 4130 | * Ensures that grouped notification receive their special treatment. |
| 4131 | * |
| 4132 | * <p>Cancels group children if the new notification causes a group to lose |
| 4133 | * its summary.</p> |
| 4134 | * |
| 4135 | * <p>Updates mSummaryByGroupKey.</p> |
| 4136 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4137 | @GuardedBy("mNotificationLock") |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4138 | private void handleGroupedNotificationLocked(NotificationRecord r, NotificationRecord old, |
| 4139 | int callingUid, int callingPid) { |
| 4140 | StatusBarNotification sbn = r.sbn; |
| 4141 | Notification n = sbn.getNotification(); |
Selim Cinek | 5b03ce9 | 2016-05-18 15:16:58 -0700 | [diff] [blame] | 4142 | if (n.isGroupSummary() && !sbn.isAppGroup()) { |
| 4143 | // notifications without a group shouldn't be a summary, otherwise autobundling can |
| 4144 | // lead to bugs |
| 4145 | n.flags &= ~Notification.FLAG_GROUP_SUMMARY; |
| 4146 | } |
| 4147 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4148 | String group = sbn.getGroupKey(); |
| 4149 | boolean isSummary = n.isGroupSummary(); |
| 4150 | |
| 4151 | Notification oldN = old != null ? old.sbn.getNotification() : null; |
| 4152 | String oldGroup = old != null ? old.sbn.getGroupKey() : null; |
| 4153 | boolean oldIsSummary = old != null && oldN.isGroupSummary(); |
| 4154 | |
| 4155 | if (oldIsSummary) { |
| 4156 | NotificationRecord removedSummary = mSummaryByGroupKey.remove(oldGroup); |
| 4157 | if (removedSummary != old) { |
| 4158 | String removedKey = |
| 4159 | removedSummary != null ? removedSummary.getKey() : "<null>"; |
| 4160 | Slog.w(TAG, "Removed summary didn't match old notification: old=" + old.getKey() + |
| 4161 | ", removed=" + removedKey); |
| 4162 | } |
| 4163 | } |
| 4164 | if (isSummary) { |
| 4165 | mSummaryByGroupKey.put(group, r); |
| 4166 | } |
| 4167 | |
| 4168 | // Clear out group children of the old notification if the update |
| 4169 | // causes the group summary to go away. This happens when the old |
| 4170 | // notification was a summary and the new one isn't, or when the old |
| 4171 | // notification was a summary and its group key changed. |
| 4172 | if (oldIsSummary && (!isSummary || !oldGroup.equals(group))) { |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 4173 | cancelGroupChildrenLocked(old, callingUid, callingPid, null, false /* sendDelete */, |
| 4174 | null); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4175 | } |
| 4176 | } |
| 4177 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4178 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4179 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4180 | void scheduleTimeoutLocked(NotificationRecord record) { |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 4181 | if (record.getNotification().getTimeoutAfter() > 0) { |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4182 | final PendingIntent pi = PendingIntent.getBroadcast(getContext(), |
| 4183 | REQUEST_CODE_TIMEOUT, |
| 4184 | new Intent(ACTION_NOTIFICATION_TIMEOUT) |
| 4185 | .setData(new Uri.Builder().scheme(SCHEME_TIMEOUT) |
| 4186 | .appendPath(record.getKey()).build()) |
| 4187 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 4188 | .putExtra(EXTRA_KEY, record.getKey()), |
| 4189 | PendingIntent.FLAG_UPDATE_CURRENT); |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 4190 | mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 4191 | SystemClock.elapsedRealtime() + record.getNotification().getTimeoutAfter(), pi); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 4192 | } |
| 4193 | } |
| 4194 | |
| 4195 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4196 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4197 | void buzzBeepBlinkLocked(NotificationRecord record) { |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4198 | boolean buzz = false; |
| 4199 | boolean beep = false; |
| 4200 | boolean blink = false; |
| 4201 | |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4202 | final Notification notification = record.sbn.getNotification(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4203 | final String key = record.getKey(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4204 | |
| 4205 | // Should this notification make noise, vibe, or use the LED? |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 4206 | final boolean aboveThreshold = |
| 4207 | record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4208 | |
| 4209 | // Remember if this notification already owns the notification channels. |
| 4210 | boolean wasBeep = key != null && key.equals(mSoundNotificationKey); |
| 4211 | boolean wasBuzz = key != null && key.equals(mVibrateNotificationKey); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4212 | // These are set inside the conditional if the notification is allowed to make noise. |
| 4213 | boolean hasValidVibrate = false; |
| 4214 | boolean hasValidSound = false; |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4215 | boolean sentAccessibilityEvent = false; |
| 4216 | // If the notification will appear in the status bar, it should send an accessibility |
| 4217 | // event |
| 4218 | if (!record.isUpdate && record.getImportance() > IMPORTANCE_MIN) { |
| 4219 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
| 4220 | sentAccessibilityEvent = true; |
| 4221 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4222 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4223 | if (aboveThreshold && isNotificationForCurrentUser(record)) { |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4224 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4225 | if (mSystemReady && mAudioManager != null) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4226 | Uri soundUri = record.getSound(); |
| 4227 | hasValidSound = soundUri != null && !Uri.EMPTY.equals(soundUri); |
| 4228 | long[] vibration = record.getVibration(); |
| 4229 | // Demote sound to vibration if vibration missing & phone in vibration mode. |
| 4230 | if (vibration == null |
| 4231 | && hasValidSound |
| 4232 | && (mAudioManager.getRingerModeInternal() |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 4233 | == AudioManager.RINGER_MODE_VIBRATE) |
| 4234 | && mAudioManager.getStreamVolume( |
| 4235 | AudioAttributes.toLegacyStreamType(record.getAudioAttributes())) == 0) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4236 | vibration = mFallbackVibrationPattern; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4237 | } |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4238 | hasValidVibrate = vibration != null; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4239 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4240 | boolean hasAudibleAlert = hasValidSound || hasValidVibrate; |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4241 | if (hasAudibleAlert && !shouldMuteNotificationLocked(record)) { |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4242 | if (!sentAccessibilityEvent) { |
| 4243 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
| 4244 | sentAccessibilityEvent = true; |
| 4245 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4246 | if (DBG) Slog.v(TAG, "Interrupting!"); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4247 | if (hasValidSound) { |
| 4248 | mSoundNotificationKey = key; |
| 4249 | if (mInCall) { |
| 4250 | playInCallNotification(); |
| 4251 | beep = true; |
| 4252 | } else { |
| 4253 | beep = playSound(record, soundUri); |
| 4254 | } |
| 4255 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4256 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4257 | final boolean ringerModeSilent = |
| 4258 | mAudioManager.getRingerModeInternal() |
| 4259 | == AudioManager.RINGER_MODE_SILENT; |
| 4260 | if (!mInCall && hasValidVibrate && !ringerModeSilent) { |
| 4261 | mVibrateNotificationKey = key; |
| 4262 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4263 | buzz = playVibration(record, vibration, hasValidSound); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4264 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4265 | } |
| 4266 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4267 | } |
| 4268 | // If a notification is updated to remove the actively playing sound or vibrate, |
| 4269 | // cancel that feedback now |
| 4270 | if (wasBeep && !hasValidSound) { |
| 4271 | clearSoundLocked(); |
| 4272 | } |
| 4273 | if (wasBuzz && !hasValidVibrate) { |
| 4274 | clearVibrateLocked(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4275 | } |
| 4276 | |
| 4277 | // light |
| 4278 | // release the light |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4279 | boolean wasShowLights = mLights.remove(key); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 4280 | if (record.getLight() != null && aboveThreshold |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4281 | && ((record.getSuppressedVisualEffects() |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 4282 | & NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF) == 0)) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4283 | mLights.add(key); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4284 | updateLightsLocked(); |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 4285 | if (mUseAttentionLight) { |
| 4286 | mAttentionLight.pulse(); |
| 4287 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4288 | blink = true; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4289 | } else if (wasShowLights) { |
| 4290 | updateLightsLocked(); |
| 4291 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4292 | if (buzz || beep || blink) { |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 4293 | MetricsLogger.action(record.getLogMaker() |
| 4294 | .setCategory(MetricsEvent.NOTIFICATION_ALERT) |
| 4295 | .setType(MetricsEvent.TYPE_OPEN) |
| 4296 | .setSubtype((buzz ? 1 : 0) | (beep ? 2 : 0) | (blink ? 4 : 0))); |
| 4297 | EventLogTags.writeNotificationAlert(key, buzz ? 1 : 0, beep ? 1 : 0, blink ? 1 : 0); |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 4298 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4299 | } |
| 4300 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4301 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4302 | boolean shouldMuteNotificationLocked(final NotificationRecord record) { |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4303 | // Suppressed because it's a silent update |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4304 | final Notification notification = record.getNotification(); |
| 4305 | if(record.isUpdate |
| 4306 | && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) { |
| 4307 | return true; |
| 4308 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4309 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4310 | // muted by listener |
| 4311 | final String disableEffects = disableNotificationEffects(record); |
| 4312 | if (disableEffects != null) { |
| 4313 | ZenLog.traceDisableEffects(record, disableEffects); |
| 4314 | return true; |
| 4315 | } |
| 4316 | |
| 4317 | // suppressed due to DND |
| 4318 | if (record.isIntercepted()) { |
| 4319 | return true; |
| 4320 | } |
| 4321 | |
| 4322 | // Suppressed because another notification in its group handles alerting |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4323 | if (record.sbn.isGroup()) { |
Julia Reynolds | 3020315 | 2017-05-26 13:36:31 -0400 | [diff] [blame] | 4324 | return notification.suppressAlertingDueToGrouping(); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4325 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4326 | |
Julia Reynolds | 65b85cf | 2017-07-20 09:19:20 -0400 | [diff] [blame] | 4327 | // Suppressed for being too recently noisy |
| 4328 | final String pkg = record.sbn.getPackageName(); |
| 4329 | if (mUsageStats.isAlertRateLimited(pkg)) { |
| 4330 | Slog.e(TAG, "Muting recently noisy " + record.getKey()); |
| 4331 | return true; |
| 4332 | } |
| 4333 | |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4334 | return false; |
| 4335 | } |
| 4336 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 4337 | private boolean playSound(final NotificationRecord record, Uri soundUri) { |
| 4338 | boolean looping = (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 4339 | // 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] | 4340 | // or the device is in vibrate mode |
Julia Reynolds | 8589657 | 2017-09-20 12:54:52 -0400 | [diff] [blame] | 4341 | if (!mAudioManager.isAudioFocusExclusive() && (mAudioManager.getRingerModeInternal() |
| 4342 | != AudioManager.RINGER_MODE_VIBRATE || mAudioManager.getStreamVolume( |
| 4343 | AudioAttributes.toLegacyStreamType(record.getAudioAttributes())) != 0)) { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 4344 | final long identity = Binder.clearCallingIdentity(); |
| 4345 | try { |
| 4346 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4347 | if (player != null) { |
| 4348 | if (DBG) Slog.v(TAG, "Playing sound " + soundUri |
| 4349 | + " with attributes " + record.getAudioAttributes()); |
| 4350 | player.playAsync(soundUri, record.sbn.getUser(), looping, |
| 4351 | record.getAudioAttributes()); |
| 4352 | return true; |
| 4353 | } |
| 4354 | } catch (RemoteException e) { |
| 4355 | } finally { |
| 4356 | Binder.restoreCallingIdentity(identity); |
| 4357 | } |
| 4358 | } |
| 4359 | return false; |
| 4360 | } |
| 4361 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4362 | private boolean playVibration(final NotificationRecord record, long[] vibration, |
| 4363 | boolean delayVibForSound) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 4364 | // Escalate privileges so we can use the vibrator even if the |
| 4365 | // notifying app does not have the VIBRATE permission. |
| 4366 | long identity = Binder.clearCallingIdentity(); |
| 4367 | try { |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4368 | final VibrationEffect effect; |
| 4369 | try { |
| 4370 | final boolean insistent = |
| 4371 | (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 4372 | effect = VibrationEffect.createWaveform( |
| 4373 | vibration, insistent ? 0 : -1 /*repeatIndex*/); |
| 4374 | } catch (IllegalArgumentException e) { |
| 4375 | Slog.e(TAG, "Error creating vibration waveform with pattern: " + |
| 4376 | Arrays.toString(vibration)); |
| 4377 | return false; |
| 4378 | } |
| 4379 | if (delayVibForSound) { |
| 4380 | new Thread(() -> { |
| 4381 | // delay the vibration by the same amount as the notification sound |
| 4382 | final int waitMs = mAudioManager.getFocusRampTimeMs( |
| 4383 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 4384 | record.getAudioAttributes()); |
| 4385 | if (DBG) Slog.v(TAG, "Delaying vibration by " + waitMs + "ms"); |
| 4386 | try { |
| 4387 | Thread.sleep(waitMs); |
| 4388 | } catch (InterruptedException e) { } |
| 4389 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 4390 | effect, record.getAudioAttributes()); |
| 4391 | }).start(); |
| 4392 | } else { |
| 4393 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 4394 | effect, record.getAudioAttributes()); |
| 4395 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 4396 | return true; |
| 4397 | } finally{ |
| 4398 | Binder.restoreCallingIdentity(identity); |
| 4399 | } |
| 4400 | } |
| 4401 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4402 | private boolean isNotificationForCurrentUser(NotificationRecord record) { |
| 4403 | final int currentUser; |
| 4404 | final long token = Binder.clearCallingIdentity(); |
| 4405 | try { |
| 4406 | currentUser = ActivityManager.getCurrentUser(); |
| 4407 | } finally { |
| 4408 | Binder.restoreCallingIdentity(token); |
| 4409 | } |
| 4410 | return (record.getUserId() == UserHandle.USER_ALL || |
| 4411 | record.getUserId() == currentUser || |
| 4412 | mUserProfiles.isCurrentProfile(record.getUserId())); |
| 4413 | } |
| 4414 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4415 | protected void playInCallNotification() { |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4416 | new Thread() { |
| 4417 | @Override |
| 4418 | public void run() { |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4419 | final long identity = Binder.clearCallingIdentity(); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4420 | try { |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4421 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4422 | if (player != null) { |
| 4423 | player.play(new Binder(), mInCallNotificationUri, |
| 4424 | mInCallNotificationAudioAttributes, |
| 4425 | mInCallNotificationVolume, false); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4426 | } |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4427 | } catch (RemoteException e) { |
| 4428 | } finally { |
| 4429 | Binder.restoreCallingIdentity(identity); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4430 | } |
| 4431 | } |
| 4432 | }.start(); |
| 4433 | } |
| 4434 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4435 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4436 | void showNextToastLocked() { |
| 4437 | ToastRecord record = mToastQueue.get(0); |
| 4438 | while (record != null) { |
| 4439 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| 4440 | try { |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4441 | record.callback.show(record.token); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4442 | scheduleTimeoutLocked(record); |
| 4443 | return; |
| 4444 | } catch (RemoteException e) { |
| 4445 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| 4446 | + " in package " + record.pkg); |
| 4447 | // remove it from the list and let the process die |
| 4448 | int index = mToastQueue.indexOf(record); |
| 4449 | if (index >= 0) { |
| 4450 | mToastQueue.remove(index); |
| 4451 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4452 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4453 | if (mToastQueue.size() > 0) { |
| 4454 | record = mToastQueue.get(0); |
| 4455 | } else { |
| 4456 | record = null; |
| 4457 | } |
| 4458 | } |
| 4459 | } |
| 4460 | } |
| 4461 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4462 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4463 | void cancelToastLocked(int index) { |
| 4464 | ToastRecord record = mToastQueue.get(index); |
| 4465 | try { |
| 4466 | record.callback.hide(); |
| 4467 | } catch (RemoteException e) { |
| 4468 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| 4469 | + " in package " + record.pkg); |
| 4470 | // don't worry about this, we're about to remove it from |
| 4471 | // the list anyway |
| 4472 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4473 | |
| 4474 | ToastRecord lastToast = mToastQueue.remove(index); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 4475 | mWindowManagerInternal.removeWindowToken(lastToast.token, true, DEFAULT_DISPLAY); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4476 | |
| 4477 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4478 | if (mToastQueue.size() > 0) { |
| 4479 | // Show the next one. If the callback fails, this will remove |
| 4480 | // it from the list, so don't assume that the list hasn't changed |
| 4481 | // after this point. |
| 4482 | showNextToastLocked(); |
| 4483 | } |
| 4484 | } |
| 4485 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4486 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4487 | private void scheduleTimeoutLocked(ToastRecord r) |
| 4488 | { |
| 4489 | mHandler.removeCallbacksAndMessages(r); |
| 4490 | Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r); |
| 4491 | long delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY; |
| 4492 | mHandler.sendMessageDelayed(m, delay); |
| 4493 | } |
| 4494 | |
| 4495 | private void handleTimeout(ToastRecord record) |
| 4496 | { |
| 4497 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| 4498 | synchronized (mToastQueue) { |
| 4499 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 4500 | if (index >= 0) { |
| 4501 | cancelToastLocked(index); |
| 4502 | } |
| 4503 | } |
| 4504 | } |
| 4505 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4506 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4507 | int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 4508 | { |
| 4509 | IBinder cbak = callback.asBinder(); |
| 4510 | ArrayList<ToastRecord> list = mToastQueue; |
| 4511 | int len = list.size(); |
| 4512 | for (int i=0; i<len; i++) { |
| 4513 | ToastRecord r = list.get(i); |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 4514 | if (r.pkg.equals(pkg) && r.callback.asBinder().equals(cbak)) { |
| 4515 | return i; |
| 4516 | } |
| 4517 | } |
| 4518 | return -1; |
| 4519 | } |
| 4520 | |
| 4521 | @GuardedBy("mToastQueue") |
| 4522 | int indexOfToastPackageLocked(String pkg) |
| 4523 | { |
| 4524 | ArrayList<ToastRecord> list = mToastQueue; |
| 4525 | int len = list.size(); |
| 4526 | for (int i=0; i<len; i++) { |
| 4527 | ToastRecord r = list.get(i); |
| 4528 | if (r.pkg.equals(pkg)) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4529 | return i; |
| 4530 | } |
| 4531 | } |
| 4532 | return -1; |
| 4533 | } |
| 4534 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4535 | @GuardedBy("mToastQueue") |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4536 | void keepProcessAliveIfNeededLocked(int pid) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4537 | { |
| 4538 | int toastCount = 0; // toasts from this pid |
| 4539 | ArrayList<ToastRecord> list = mToastQueue; |
| 4540 | int N = list.size(); |
| 4541 | for (int i=0; i<N; i++) { |
| 4542 | ToastRecord r = list.get(i); |
| 4543 | if (r.pid == pid) { |
| 4544 | toastCount++; |
| 4545 | } |
| 4546 | } |
| 4547 | try { |
Dianne Hackborn | f965f40 | 2017-05-04 23:27:23 -0700 | [diff] [blame] | 4548 | mAm.setProcessImportant(mForegroundToken, pid, toastCount > 0, "toast"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4549 | } catch (RemoteException e) { |
| 4550 | // Shouldn't happen. |
| 4551 | } |
| 4552 | } |
| 4553 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4554 | private void handleRankingReconsideration(Message message) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4555 | if (!(message.obj instanceof RankingReconsideration)) return; |
| 4556 | RankingReconsideration recon = (RankingReconsideration) message.obj; |
| 4557 | recon.run(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4558 | boolean changed; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4559 | synchronized (mNotificationLock) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4560 | final NotificationRecord record = mNotificationsByKey.get(recon.getKey()); |
| 4561 | if (record == null) { |
| 4562 | return; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4563 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4564 | int indexBefore = findNotificationRecordIndexLocked(record); |
| 4565 | boolean interceptBefore = record.isIntercepted(); |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4566 | float contactAffinityBefore = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4567 | int visibilityBefore = record.getPackageVisibilityOverride(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4568 | recon.applyChangesLocked(record); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4569 | applyZenModeLocked(record); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4570 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4571 | int indexAfter = findNotificationRecordIndexLocked(record); |
| 4572 | boolean interceptAfter = record.isIntercepted(); |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4573 | float contactAffinityAfter = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4574 | int visibilityAfter = record.getPackageVisibilityOverride(); |
| 4575 | changed = indexBefore != indexAfter || interceptBefore != interceptAfter |
| 4576 | || visibilityBefore != visibilityAfter; |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4577 | if (interceptBefore && !interceptAfter |
| 4578 | && Float.compare(contactAffinityBefore, contactAffinityAfter) != 0) { |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4579 | buzzBeepBlinkLocked(record); |
| 4580 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4581 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4582 | if (changed) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4583 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4584 | } |
| 4585 | } |
| 4586 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4587 | void handleRankingSort() { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 4588 | if (mRankingHelper == null) return; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4589 | synchronized (mNotificationLock) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4590 | final int N = mNotificationList.size(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4591 | // Any field that can change via one of the extractors needs to be added here. |
| 4592 | ArrayList<String> orderBefore = new ArrayList<>(N); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4593 | int[] visibilities = new int[N]; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4594 | boolean[] showBadges = new boolean[N]; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4595 | ArrayList<NotificationChannel> channelBefore = new ArrayList<>(N); |
| 4596 | ArrayList<String> groupKeyBefore = new ArrayList<>(N); |
| 4597 | ArrayList<ArrayList<String>> overridePeopleBefore = new ArrayList<>(N); |
| 4598 | ArrayList<ArrayList<SnoozeCriterion>> snoozeCriteriaBefore = new ArrayList<>(N); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4599 | ArrayList<Integer> userSentimentBefore = new ArrayList<>(N); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4600 | for (int i = 0; i < N; i++) { |
| 4601 | final NotificationRecord r = mNotificationList.get(i); |
| 4602 | orderBefore.add(r.getKey()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4603 | visibilities[i] = r.getPackageVisibilityOverride(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4604 | showBadges[i] = r.canShowBadge(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4605 | channelBefore.add(r.getChannel()); |
| 4606 | groupKeyBefore.add(r.getGroupKey()); |
| 4607 | overridePeopleBefore.add(r.getPeopleOverride()); |
| 4608 | snoozeCriteriaBefore.add(r.getSnoozeCriteria()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4609 | userSentimentBefore.add(r.getUserSentiment()); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4610 | mRankingHelper.extractSignals(r); |
| 4611 | } |
Chris Wren | 19a02b0 | 2015-12-22 10:34:22 -0500 | [diff] [blame] | 4612 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4613 | for (int i = 0; i < N; i++) { |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4614 | final NotificationRecord r = mNotificationList.get(i); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4615 | if (!orderBefore.get(i).equals(r.getKey()) |
Julia Reynolds | 6976669 | 2016-02-01 15:35:08 -0500 | [diff] [blame] | 4616 | || visibilities[i] != r.getPackageVisibilityOverride() |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4617 | || showBadges[i] != r.canShowBadge() |
| 4618 | || !Objects.equals(channelBefore.get(i), r.getChannel()) |
| 4619 | || !Objects.equals(groupKeyBefore.get(i), r.getGroupKey()) |
| 4620 | || !Objects.equals(overridePeopleBefore.get(i), r.getPeopleOverride()) |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4621 | || !Objects.equals(snoozeCriteriaBefore.get(i), r.getSnoozeCriteria()) |
| 4622 | || !Objects.equals(userSentimentBefore.get(i), r.getUserSentiment())) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4623 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4624 | return; |
| 4625 | } |
| 4626 | } |
| 4627 | } |
| 4628 | } |
| 4629 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4630 | @GuardedBy("mNotificationLock") |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 4631 | private void recordCallerLocked(NotificationRecord record) { |
| 4632 | if (mZenModeHelper.isCall(record)) { |
| 4633 | mZenModeHelper.recordCaller(record); |
| 4634 | } |
| 4635 | } |
| 4636 | |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 4637 | // let zen mode evaluate this record |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4638 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4639 | private void applyZenModeLocked(NotificationRecord record) { |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 4640 | record.setIntercepted(mZenModeHelper.shouldIntercept(record)); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4641 | if (record.isIntercepted()) { |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 4642 | int suppressed = (mZenModeHelper.shouldSuppressWhenScreenOff() |
| 4643 | ? SUPPRESSED_EFFECT_SCREEN_OFF : 0) |
| 4644 | | (mZenModeHelper.shouldSuppressWhenScreenOn() |
| 4645 | ? SUPPRESSED_EFFECT_SCREEN_ON : 0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4646 | record.setSuppressedVisualEffects(suppressed); |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 4647 | } else { |
| 4648 | record.setSuppressedVisualEffects(0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4649 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4650 | } |
| 4651 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4652 | @GuardedBy("mNotificationLock") |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4653 | private int findNotificationRecordIndexLocked(NotificationRecord target) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4654 | return mRankingHelper.indexOf(mNotificationList, target); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4655 | } |
| 4656 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4657 | private void handleSendRankingUpdate() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4658 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4659 | mListeners.notifyRankingUpdateLocked(); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4660 | } |
| 4661 | } |
| 4662 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4663 | private void scheduleListenerHintsChanged(int state) { |
| 4664 | mHandler.removeMessages(MESSAGE_LISTENER_HINTS_CHANGED); |
| 4665 | mHandler.obtainMessage(MESSAGE_LISTENER_HINTS_CHANGED, state, 0).sendToTarget(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4666 | } |
| 4667 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4668 | private void scheduleInterruptionFilterChanged(int listenerInterruptionFilter) { |
| 4669 | mHandler.removeMessages(MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED); |
| 4670 | mHandler.obtainMessage( |
| 4671 | MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED, |
| 4672 | listenerInterruptionFilter, |
| 4673 | 0).sendToTarget(); |
| 4674 | } |
| 4675 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4676 | private void handleListenerHintsChanged(int hints) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4677 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4678 | mListeners.notifyListenerHintsChangedLocked(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4679 | } |
| 4680 | } |
| 4681 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4682 | private void handleListenerInterruptionFilterChanged(int interruptionFilter) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4683 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4684 | mListeners.notifyInterruptionFilterChanged(interruptionFilter); |
| 4685 | } |
| 4686 | } |
| 4687 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4688 | protected class WorkerHandler extends Handler |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4689 | { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4690 | public WorkerHandler(Looper looper) { |
| 4691 | super(looper); |
| 4692 | } |
| 4693 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4694 | @Override |
| 4695 | public void handleMessage(Message msg) |
| 4696 | { |
| 4697 | switch (msg.what) |
| 4698 | { |
| 4699 | case MESSAGE_TIMEOUT: |
| 4700 | handleTimeout((ToastRecord)msg.obj); |
| 4701 | break; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 4702 | case MESSAGE_SAVE_POLICY_FILE: |
| 4703 | handleSavePolicyFile(); |
| 4704 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4705 | case MESSAGE_SEND_RANKING_UPDATE: |
| 4706 | handleSendRankingUpdate(); |
| 4707 | break; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4708 | case MESSAGE_LISTENER_HINTS_CHANGED: |
| 4709 | handleListenerHintsChanged(msg.arg1); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4710 | break; |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4711 | case MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED: |
| 4712 | handleListenerInterruptionFilterChanged(msg.arg1); |
| 4713 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4714 | } |
| 4715 | } |
| 4716 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4717 | protected void scheduleSendRankingUpdate() { |
| 4718 | if (!hasMessages(MESSAGE_SEND_RANKING_UPDATE)) { |
| 4719 | Message m = Message.obtain(this, MESSAGE_SEND_RANKING_UPDATE); |
| 4720 | sendMessage(m); |
| 4721 | } |
| 4722 | } |
| 4723 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4724 | } |
| 4725 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4726 | private final class RankingHandlerWorker extends Handler implements RankingHandler |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4727 | { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4728 | public RankingHandlerWorker(Looper looper) { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4729 | super(looper); |
| 4730 | } |
| 4731 | |
| 4732 | @Override |
| 4733 | public void handleMessage(Message msg) { |
| 4734 | switch (msg.what) { |
| 4735 | case MESSAGE_RECONSIDER_RANKING: |
| 4736 | handleRankingReconsideration(msg); |
| 4737 | break; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4738 | case MESSAGE_RANKING_SORT: |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4739 | handleRankingSort(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4740 | break; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4741 | } |
| 4742 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4743 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4744 | public void requestSort() { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4745 | removeMessages(MESSAGE_RANKING_SORT); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4746 | Message msg = Message.obtain(); |
| 4747 | msg.what = MESSAGE_RANKING_SORT; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4748 | sendMessage(msg); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4749 | } |
| 4750 | |
| 4751 | public void requestReconsideration(RankingReconsideration recon) { |
| 4752 | Message m = Message.obtain(this, |
| 4753 | NotificationManagerService.MESSAGE_RECONSIDER_RANKING, recon); |
| 4754 | long delay = recon.getDelay(TimeUnit.MILLISECONDS); |
| 4755 | sendMessageDelayed(m, delay); |
| 4756 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4757 | } |
| 4758 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4759 | // Notifications |
| 4760 | // ============================================================================ |
| 4761 | static int clamp(int x, int low, int high) { |
| 4762 | return (x < low) ? low : ((x > high) ? high : x); |
| 4763 | } |
| 4764 | |
| 4765 | void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
Eugene Susla | d4128ec | 2017-12-04 19:48:41 +0000 | [diff] [blame] | 4766 | if (!mAccessibilityManager.isEnabled()) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 4767 | return; |
| 4768 | } |
| 4769 | |
| 4770 | AccessibilityEvent event = |
| 4771 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 4772 | event.setPackageName(packageName); |
| 4773 | event.setClassName(Notification.class.getName()); |
| 4774 | event.setParcelableData(notification); |
| 4775 | CharSequence tickerText = notification.tickerText; |
| 4776 | if (!TextUtils.isEmpty(tickerText)) { |
| 4777 | event.getText().add(tickerText); |
| 4778 | } |
| 4779 | |
Julia Reynolds | 9418756 | 2017-10-10 13:58:49 -0400 | [diff] [blame] | 4780 | mAccessibilityManager.sendAccessibilityEvent(event); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 4781 | } |
| 4782 | |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4783 | /** |
| 4784 | * Removes all NotificationsRecords with the same key as the given notification record |
| 4785 | * from both lists. Do not call this method while iterating over either list. |
| 4786 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4787 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4788 | private boolean removeFromNotificationListsLocked(NotificationRecord r) { |
| 4789 | // Remove from both lists, either list could have a separate Record for what is |
| 4790 | // effectively the same notification. |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4791 | boolean wasPosted = false; |
| 4792 | NotificationRecord recordInList = null; |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4793 | if ((recordInList = findNotificationByListLocked(mNotificationList, r.getKey())) |
| 4794 | != null) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4795 | mNotificationList.remove(recordInList); |
| 4796 | mNotificationsByKey.remove(recordInList.sbn.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4797 | wasPosted = true; |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4798 | } |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4799 | while ((recordInList = findNotificationByListLocked(mEnqueuedNotifications, r.getKey())) |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4800 | != null) { |
| 4801 | mEnqueuedNotifications.remove(recordInList); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4802 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4803 | return wasPosted; |
| 4804 | } |
| 4805 | |
| 4806 | @GuardedBy("mNotificationLock") |
| 4807 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason, |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4808 | boolean wasPosted, String listenerName) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4809 | final String canceledKey = r.getKey(); |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 4810 | |
| 4811 | // Record caller. |
| 4812 | recordCallerLocked(r); |
| 4813 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4814 | if (r.getStats().getDismissalSurface() == NotificationStats.DISMISSAL_NOT_DISMISSED) { |
| 4815 | r.recordDismissalSurface(NotificationStats.DISMISSAL_OTHER); |
| 4816 | } |
| 4817 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4818 | // tell the app |
| 4819 | if (sendDelete) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4820 | if (r.getNotification().deleteIntent != null) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4821 | try { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4822 | r.getNotification().deleteIntent.send(); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4823 | } catch (PendingIntent.CanceledException ex) { |
| 4824 | // do nothing - there's no relevant way to recover, and |
| 4825 | // no reason to let this propagate |
Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 4826 | Slog.w(TAG, "canceled PendingIntent for " + r.sbn.getPackageName(), ex); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4827 | } |
| 4828 | } |
| 4829 | } |
| 4830 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4831 | // Only cancel these if this notification actually got to be posted. |
| 4832 | if (wasPosted) { |
| 4833 | // status bar |
| 4834 | if (r.getNotification().getSmallIcon() != null) { |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 4835 | if (reason != REASON_SNOOZED) { |
| 4836 | r.isCanceled = true; |
| 4837 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 4838 | mListeners.notifyRemovedLocked(r.sbn, reason, r.getStats()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4839 | mHandler.post(new Runnable() { |
| 4840 | @Override |
| 4841 | public void run() { |
| 4842 | mGroupHelper.onNotificationRemoved(r.sbn); |
| 4843 | } |
| 4844 | }); |
| 4845 | } |
| 4846 | |
| 4847 | // sound |
| 4848 | if (canceledKey.equals(mSoundNotificationKey)) { |
| 4849 | mSoundNotificationKey = null; |
| 4850 | final long identity = Binder.clearCallingIdentity(); |
| 4851 | try { |
| 4852 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4853 | if (player != null) { |
| 4854 | player.stopAsync(); |
| 4855 | } |
| 4856 | } catch (RemoteException e) { |
| 4857 | } finally { |
| 4858 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4859 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4860 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4861 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4862 | // vibrate |
| 4863 | if (canceledKey.equals(mVibrateNotificationKey)) { |
| 4864 | mVibrateNotificationKey = null; |
| 4865 | long identity = Binder.clearCallingIdentity(); |
| 4866 | try { |
| 4867 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 4868 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4869 | finally { |
| 4870 | Binder.restoreCallingIdentity(identity); |
| 4871 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4872 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4873 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4874 | // light |
| 4875 | mLights.remove(canceledKey); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4876 | } |
| 4877 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4878 | // Record usage stats |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4879 | // TODO: add unbundling stats? |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4880 | switch (reason) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 4881 | case REASON_CANCEL: |
| 4882 | case REASON_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4883 | case REASON_LISTENER_CANCEL: |
| 4884 | case REASON_LISTENER_CANCEL_ALL: |
| 4885 | mUsageStats.registerDismissedByUser(r); |
| 4886 | break; |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 4887 | case REASON_APP_CANCEL: |
| 4888 | case REASON_APP_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4889 | mUsageStats.registerRemovedByApp(r); |
| 4890 | break; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4891 | } |
| 4892 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4893 | String groupKey = r.getGroupKey(); |
| 4894 | NotificationRecord groupSummary = mSummaryByGroupKey.get(groupKey); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4895 | if (groupSummary != null && groupSummary.getKey().equals(canceledKey)) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4896 | mSummaryByGroupKey.remove(groupKey); |
| 4897 | } |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 4898 | final ArrayMap<String, String> summaries = mAutobundledSummaries.get(r.sbn.getUserId()); |
| 4899 | if (summaries != null && r.sbn.getKey().equals(summaries.get(r.sbn.getPackageName()))) { |
| 4900 | summaries.remove(r.sbn.getPackageName()); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4901 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 4902 | |
Daniel Sandler | 23d7c70 | 2013-03-07 16:32:06 -0500 | [diff] [blame] | 4903 | // Save it for users of getHistoricalNotifications() |
| 4904 | mArchive.record(r.sbn); |
Christoph Studer | 81e5b5f | 2014-10-22 17:19:56 +0200 | [diff] [blame] | 4905 | |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 4906 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 4907 | MetricsLogger.action(r.getLogMaker(now) |
| 4908 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
| 4909 | .setType(MetricsEvent.TYPE_DISMISS) |
| 4910 | .setSubtype(reason)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 4911 | EventLogTags.writeNotificationCanceled(canceledKey, reason, |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4912 | 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] | 4913 | } |
| 4914 | |
| 4915 | /** |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 4916 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4917 | * and none of the {@code mustNotHaveFlags}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4918 | */ |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 4919 | void cancelNotification(final int callingUid, final int callingPid, |
| 4920 | final String pkg, final String tag, final int id, |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4921 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4922 | final int userId, final int reason, final ManagedServiceInfo listener) { |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4923 | // In enqueueNotificationInternal notifications are added by scheduling the |
| 4924 | // work on the worker handler. Hence, we also schedule the cancel on this |
| 4925 | // handler to avoid a scenario where an add notification call followed by a |
| 4926 | // remove notification call ends up in not removing the notification. |
| 4927 | mHandler.post(new Runnable() { |
| 4928 | @Override |
| 4929 | public void run() { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4930 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Chris Wren | bddb5bc | 2015-03-04 08:47:46 -0800 | [diff] [blame] | 4931 | if (DBG) EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, id, tag, |
| 4932 | userId, mustHaveFlags, mustNotHaveFlags, reason, listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4933 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4934 | synchronized (mNotificationLock) { |
| 4935 | // Look for the notification, searching both the posted and enqueued lists. |
| 4936 | NotificationRecord r = findNotificationLocked(pkg, tag, id, userId); |
| 4937 | if (r != null) { |
| 4938 | // The notification was found, check if it should be removed. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4939 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4940 | // Ideally we'd do this in the caller of this method. However, that would |
| 4941 | // require the caller to also find the notification. |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 4942 | if (reason == REASON_CLICK) { |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4943 | mUsageStats.registerClickedByUser(r); |
| 4944 | } |
| 4945 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4946 | if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) { |
| 4947 | return; |
| 4948 | } |
| 4949 | if ((r.getNotification().flags & mustNotHaveFlags) != 0) { |
| 4950 | return; |
| 4951 | } |
| 4952 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4953 | // Cancel the notification. |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4954 | boolean wasPosted = removeFromNotificationListsLocked(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4955 | cancelNotificationLocked(r, sendDelete, reason, wasPosted, listenerName); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4956 | cancelGroupChildrenLocked(r, callingUid, callingPid, listenerName, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 4957 | sendDelete, null); |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4958 | updateLightsLocked(); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 4959 | } else { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4960 | // No notification was found, assume that it is snoozed and cancel it. |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 4961 | if (reason != REASON_SNOOZED) { |
| 4962 | final boolean wasSnoozed = mSnoozeHelper.cancel(userId, pkg, tag, id); |
| 4963 | if (wasSnoozed) { |
| 4964 | savePolicyFile(); |
| 4965 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 4966 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4967 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4968 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4969 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4970 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4971 | } |
| 4972 | |
| 4973 | /** |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 4974 | * Determine whether the userId applies to the notification in question, either because |
| 4975 | * they match exactly, or one of them is USER_ALL (which is treated as a wildcard). |
| 4976 | */ |
| 4977 | private boolean notificationMatchesUserId(NotificationRecord r, int userId) { |
| 4978 | return |
| 4979 | // looking for USER_ALL notifications? match everything |
| 4980 | userId == UserHandle.USER_ALL |
| 4981 | // a notification sent to USER_ALL matches any query |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4982 | || r.getUserId() == UserHandle.USER_ALL |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 4983 | // an exact user match |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4984 | || r.getUserId() == userId; |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 4985 | } |
| 4986 | |
| 4987 | /** |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4988 | * Determine whether the userId applies to the notification in question, either because |
| 4989 | * 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] | 4990 | * because it matches one of the users profiles. |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4991 | */ |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 4992 | private boolean notificationMatchesCurrentProfiles(NotificationRecord r, int userId) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4993 | return notificationMatchesUserId(r, userId) |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 4994 | || mUserProfiles.isCurrentProfile(r.getUserId()); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4995 | } |
| 4996 | |
| 4997 | /** |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 4998 | * 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] | 4999 | * {@code mustHaveFlags}. |
| 5000 | */ |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5001 | void cancelAllNotificationsInt(int callingUid, int callingPid, String pkg, String channelId, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 5002 | int mustHaveFlags, int mustNotHaveFlags, boolean doit, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5003 | ManagedServiceInfo listener) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5004 | mHandler.post(new Runnable() { |
| 5005 | @Override |
| 5006 | public void run() { |
| 5007 | String listenerName = listener == null ? null : listener.component.toShortString(); |
| 5008 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 5009 | pkg, userId, mustHaveFlags, mustNotHaveFlags, reason, |
| 5010 | listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5011 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5012 | // Why does this parameter exist? Do we actually want to execute the above if doit |
| 5013 | // is false? |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 5014 | if (!doit) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5015 | return; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 5016 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5017 | |
| 5018 | synchronized (mNotificationLock) { |
| 5019 | FlagChecker flagChecker = (int flags) -> { |
| 5020 | if ((flags & mustHaveFlags) != mustHaveFlags) { |
| 5021 | return false; |
| 5022 | } |
| 5023 | if ((flags & mustNotHaveFlags) != 0) { |
| 5024 | return false; |
| 5025 | } |
| 5026 | return true; |
| 5027 | }; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5028 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 5029 | pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, flagChecker, |
| 5030 | false /*includeCurrentProfiles*/, userId, false /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5031 | listenerName, true /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5032 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 5033 | callingPid, pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, |
| 5034 | flagChecker, false /*includeCurrentProfiles*/, userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5035 | false /*sendDelete*/, reason, listenerName, false /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5036 | mSnoozeHelper.cancel(userId, pkg); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5037 | } |
| 5038 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5039 | }); |
| 5040 | } |
| 5041 | |
| 5042 | private interface FlagChecker { |
| 5043 | // Returns false if these flags do not pass the defined flag test. |
| 5044 | public boolean apply(int flags); |
| 5045 | } |
| 5046 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5047 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5048 | private void cancelAllNotificationsByListLocked(ArrayList<NotificationRecord> notificationList, |
| 5049 | int callingUid, int callingPid, String pkg, boolean nullPkgIndicatesUserSwitch, |
| 5050 | String channelId, FlagChecker flagChecker, boolean includeCurrentProfiles, int userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5051 | boolean sendDelete, int reason, String listenerName, boolean wasPosted) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5052 | ArrayList<NotificationRecord> canceledNotifications = null; |
| 5053 | for (int i = notificationList.size() - 1; i >= 0; --i) { |
| 5054 | NotificationRecord r = notificationList.get(i); |
| 5055 | if (includeCurrentProfiles) { |
| 5056 | if (!notificationMatchesCurrentProfiles(r, userId)) { |
| 5057 | continue; |
| 5058 | } |
| 5059 | } else if (!notificationMatchesUserId(r, userId)) { |
| 5060 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5061 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5062 | // Don't remove notifications to all, if there's no package name specified |
| 5063 | if (nullPkgIndicatesUserSwitch && pkg == null && r.getUserId() == UserHandle.USER_ALL) { |
| 5064 | continue; |
| 5065 | } |
| 5066 | if (!flagChecker.apply(r.getFlags())) { |
| 5067 | continue; |
| 5068 | } |
| 5069 | if (pkg != null && !r.sbn.getPackageName().equals(pkg)) { |
| 5070 | continue; |
| 5071 | } |
| 5072 | if (channelId != null && !channelId.equals(r.getChannel().getId())) { |
| 5073 | continue; |
| 5074 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5075 | if (canceledNotifications == null) { |
| 5076 | canceledNotifications = new ArrayList<>(); |
| 5077 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5078 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 5079 | mNotificationsByKey.remove(r.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5080 | canceledNotifications.add(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5081 | cancelNotificationLocked(r, sendDelete, reason, wasPosted, listenerName); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5082 | } |
| 5083 | if (canceledNotifications != null) { |
| 5084 | final int M = canceledNotifications.size(); |
| 5085 | for (int i = 0; i < M; i++) { |
| 5086 | cancelGroupChildrenLocked(canceledNotifications.get(i), callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5087 | listenerName, false /* sendDelete */, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5088 | } |
| 5089 | updateLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5090 | } |
| 5091 | } |
| 5092 | |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 5093 | void snoozeNotificationInt(String key, long duration, String snoozeCriterionId, |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5094 | ManagedServiceInfo listener) { |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5095 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 5096 | if (duration <= 0 && snoozeCriterionId == null || key == null) { |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 5097 | return; |
| 5098 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 5099 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5100 | if (DBG) { |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 5101 | Slog.d(TAG, String.format("snooze event(%s, %d, %s, %s)", key, duration, |
| 5102 | snoozeCriterionId, listenerName)); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5103 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5104 | // Needs to post so that it can cancel notifications not yet enqueued. |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5105 | mHandler.post(new SnoozeNotificationRunnable(key, duration, snoozeCriterionId)); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 5106 | } |
| 5107 | |
| 5108 | void unsnoozeNotificationInt(String key, ManagedServiceInfo listener) { |
| 5109 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 5110 | if (DBG) { |
| 5111 | Slog.d(TAG, String.format("unsnooze event(%s, %s)", key, listenerName)); |
| 5112 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5113 | mSnoozeHelper.repost(key); |
| 5114 | savePolicyFile(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 5115 | } |
| 5116 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5117 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 350159c | 2014-03-27 11:15:11 -0700 | [diff] [blame] | 5118 | void cancelAllLocked(int callingUid, int callingPid, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5119 | ManagedServiceInfo listener, boolean includeCurrentProfiles) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5120 | mHandler.post(new Runnable() { |
| 5121 | @Override |
| 5122 | public void run() { |
| 5123 | synchronized (mNotificationLock) { |
| 5124 | String listenerName = |
| 5125 | listener == null ? null : listener.component.toShortString(); |
| 5126 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 5127 | null, userId, 0, 0, reason, listenerName); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 5128 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5129 | FlagChecker flagChecker = (int flags) -> { |
| 5130 | if ((flags & (Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR)) |
| 5131 | != 0) { |
| 5132 | return false; |
| 5133 | } |
| 5134 | return true; |
| 5135 | }; |
| 5136 | |
| 5137 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 5138 | null, false /*nullPkgIndicatesUserSwitch*/, null, flagChecker, |
| 5139 | includeCurrentProfiles, userId, true /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5140 | listenerName, true); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5141 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 5142 | callingPid, null, false /*nullPkgIndicatesUserSwitch*/, null, |
| 5143 | flagChecker, includeCurrentProfiles, userId, true /*sendDelete*/, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5144 | reason, listenerName, false); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5145 | mSnoozeHelper.cancel(userId, includeCurrentProfiles); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5146 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5147 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5148 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5149 | } |
| 5150 | |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5151 | // Warning: The caller is responsible for invoking updateLightsLocked(). |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5152 | @GuardedBy("mNotificationLock") |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5153 | private void cancelGroupChildrenLocked(NotificationRecord r, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5154 | String listenerName, boolean sendDelete, FlagChecker flagChecker) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5155 | Notification n = r.getNotification(); |
Christoph Studer | 3f31f5d | 2014-07-31 16:55:32 +0200 | [diff] [blame] | 5156 | if (!n.isGroupSummary()) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5157 | return; |
| 5158 | } |
| 5159 | |
| 5160 | String pkg = r.sbn.getPackageName(); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5161 | |
| 5162 | if (pkg == null) { |
| 5163 | if (DBG) Log.e(TAG, "No package for group summary: " + r.getKey()); |
| 5164 | return; |
| 5165 | } |
| 5166 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5167 | cancelGroupChildrenByListLocked(mNotificationList, r, callingUid, callingPid, listenerName, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5168 | sendDelete, true, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5169 | cancelGroupChildrenByListLocked(mEnqueuedNotifications, r, callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5170 | listenerName, sendDelete, false, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5171 | } |
| 5172 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5173 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5174 | private void cancelGroupChildrenByListLocked(ArrayList<NotificationRecord> notificationList, |
| 5175 | NotificationRecord parentNotification, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5176 | String listenerName, boolean sendDelete, boolean wasPosted, FlagChecker flagChecker) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5177 | final String pkg = parentNotification.sbn.getPackageName(); |
| 5178 | final int userId = parentNotification.getUserId(); |
| 5179 | final int reason = REASON_GROUP_SUMMARY_CANCELED; |
| 5180 | for (int i = notificationList.size() - 1; i >= 0; i--) { |
| 5181 | final NotificationRecord childR = notificationList.get(i); |
| 5182 | final StatusBarNotification childSbn = childR.sbn; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5183 | if ((childSbn.isGroup() && !childSbn.getNotification().isGroupSummary()) && |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5184 | childR.getGroupKey().equals(parentNotification.getGroupKey()) |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 5185 | && (childR.getFlags() & Notification.FLAG_FOREGROUND_SERVICE) == 0 |
| 5186 | && (flagChecker == null || flagChecker.apply(childR.getFlags()))) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 5187 | EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, childSbn.getId(), |
| 5188 | childSbn.getTag(), userId, 0, 0, reason, listenerName); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 5189 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 5190 | mNotificationsByKey.remove(childR.getKey()); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 5191 | cancelNotificationLocked(childR, sendDelete, reason, wasPosted, listenerName); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 5192 | } |
| 5193 | } |
| 5194 | } |
| 5195 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5196 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 5197 | void updateLightsLocked() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5198 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5199 | // handle notification lights |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 5200 | NotificationRecord ledNotification = null; |
| 5201 | while (ledNotification == null && !mLights.isEmpty()) { |
| 5202 | final String owner = mLights.get(mLights.size() - 1); |
| 5203 | ledNotification = mNotificationsByKey.get(owner); |
| 5204 | if (ledNotification == null) { |
| 5205 | Slog.wtfStack(TAG, "LED Notification does not exist: " + owner); |
| 5206 | mLights.remove(owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5207 | } |
| 5208 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 5209 | |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 5210 | // 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] | 5211 | if (ledNotification == null || mInCall || mScreenOn) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 5212 | mNotificationLight.turnOff(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5213 | } else { |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 5214 | NotificationRecord.Light light = ledNotification.getLight(); |
| 5215 | if (light != null && mNotificationPulseEnabled) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 5216 | // pulse repeatedly |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 5217 | mNotificationLight.setFlashing(light.color, Light.LIGHT_FLASH_TIMED, |
| 5218 | light.onMs, light.offMs); |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 5219 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 5220 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5221 | } |
| 5222 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5223 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5224 | @NonNull List<NotificationRecord> findGroupNotificationsLocked(String pkg, |
| 5225 | String groupKey, int userId) { |
| 5226 | List<NotificationRecord> records = new ArrayList<>(); |
| 5227 | records.addAll(findGroupNotificationByListLocked(mNotificationList, pkg, groupKey, userId)); |
| 5228 | records.addAll( |
| 5229 | findGroupNotificationByListLocked(mEnqueuedNotifications, pkg, groupKey, userId)); |
| 5230 | return records; |
| 5231 | } |
| 5232 | |
| 5233 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5234 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5235 | private @NonNull List<NotificationRecord> findGroupNotificationByListLocked( |
| 5236 | ArrayList<NotificationRecord> list, String pkg, String groupKey, int userId) { |
| 5237 | List<NotificationRecord> records = new ArrayList<>(); |
| 5238 | final int len = list.size(); |
| 5239 | for (int i = 0; i < len; i++) { |
| 5240 | NotificationRecord r = list.get(i); |
| 5241 | if (notificationMatchesUserId(r, userId) && r.getGroupKey().equals(groupKey) |
| 5242 | && r.sbn.getPackageName().equals(pkg)) { |
| 5243 | records.add(r); |
| 5244 | } |
| 5245 | } |
| 5246 | return records; |
| 5247 | } |
| 5248 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5249 | // Searches both enqueued and posted notifications by key. |
| 5250 | // TODO: need to combine a bunch of these getters with slightly different behavior. |
| 5251 | // TODO: Should enqueuing just add to mNotificationsByKey instead? |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5252 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5253 | private NotificationRecord findNotificationByKeyLocked(String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5254 | NotificationRecord r; |
| 5255 | if ((r = findNotificationByListLocked(mNotificationList, key)) != null) { |
| 5256 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5257 | } |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5258 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, key)) != null) { |
| 5259 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5260 | } |
| 5261 | return null; |
| 5262 | } |
| 5263 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5264 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 5265 | NotificationRecord findNotificationLocked(String pkg, String tag, int id, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5266 | NotificationRecord r; |
| 5267 | if ((r = findNotificationByListLocked(mNotificationList, pkg, tag, id, userId)) != null) { |
| 5268 | return r; |
| 5269 | } |
| 5270 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, pkg, tag, id, userId)) |
| 5271 | != null) { |
| 5272 | return r; |
| 5273 | } |
| 5274 | return null; |
| 5275 | } |
| 5276 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5277 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5278 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5279 | String pkg, String tag, int id, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5280 | final int len = list.size(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5281 | for (int i = 0; i < len; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5282 | NotificationRecord r = list.get(i); |
Vladimir Marko | 2526f33 | 2013-09-11 11:13:55 +0100 | [diff] [blame] | 5283 | if (notificationMatchesUserId(r, userId) && r.sbn.getId() == id && |
| 5284 | TextUtils.equals(r.sbn.getTag(), tag) && r.sbn.getPackageName().equals(pkg)) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5285 | return r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5286 | } |
| 5287 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5288 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5289 | } |
| 5290 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5291 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5292 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5293 | String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5294 | final int N = list.size(); |
| 5295 | for (int i = 0; i < N; i++) { |
| 5296 | if (key.equals(list.get(i).getKey())) { |
| 5297 | return list.get(i); |
| 5298 | } |
| 5299 | } |
| 5300 | return null; |
| 5301 | } |
| 5302 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5303 | @GuardedBy("mNotificationLock") |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 5304 | int indexOfNotificationLocked(String key) { |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 5305 | final int N = mNotificationList.size(); |
| 5306 | for (int i = 0; i < N; i++) { |
| 5307 | if (key.equals(mNotificationList.get(i).getKey())) { |
| 5308 | return i; |
| 5309 | } |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 5310 | } |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 5311 | return -1; |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 5312 | } |
| 5313 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 5314 | private void updateNotificationPulse() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5315 | synchronized (mNotificationLock) { |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 5316 | updateLightsLocked(); |
| 5317 | } |
| 5318 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 5319 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5320 | protected boolean isCallingUidSystem() { |
| 5321 | final int uid = Binder.getCallingUid(); |
| 5322 | return uid == Process.SYSTEM_UID; |
| 5323 | } |
| 5324 | |
| 5325 | protected boolean isUidSystemOrPhone(int uid) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5326 | final int appid = UserHandle.getAppId(uid); |
| 5327 | return (appid == Process.SYSTEM_UID || appid == Process.PHONE_UID || uid == 0); |
| 5328 | } |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 5329 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5330 | // TODO: Most calls should probably move to isCallerSystem. |
| 5331 | protected boolean isCallerSystemOrPhone() { |
| 5332 | return isUidSystemOrPhone(Binder.getCallingUid()); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5333 | } |
| 5334 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5335 | private void checkCallerIsSystemOrShell() { |
| 5336 | if (Binder.getCallingUid() == Process.SHELL_UID) { |
| 5337 | return; |
| 5338 | } |
| 5339 | checkCallerIsSystem(); |
| 5340 | } |
| 5341 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5342 | private void checkCallerIsSystem() { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5343 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5344 | return; |
| 5345 | } |
| 5346 | throw new SecurityException("Disallowed call for uid " + Binder.getCallingUid()); |
| 5347 | } |
| 5348 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5349 | private void checkCallerIsSystemOrSameApp(String pkg) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5350 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5351 | return; |
| 5352 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 5353 | checkCallerIsSameApp(pkg); |
| 5354 | } |
| 5355 | |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 5356 | private boolean isCallerInstantApp(String pkg) { |
| 5357 | // System is always allowed to act for ephemeral apps. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5358 | if (isCallerSystemOrPhone()) { |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 5359 | return false; |
| 5360 | } |
| 5361 | |
| 5362 | mAppOps.checkPackage(Binder.getCallingUid(), pkg); |
| 5363 | |
| 5364 | try { |
| 5365 | ApplicationInfo ai = mPackageManager.getApplicationInfo(pkg, 0, |
| 5366 | UserHandle.getCallingUserId()); |
| 5367 | if (ai == null) { |
| 5368 | throw new SecurityException("Unknown package " + pkg); |
| 5369 | } |
| 5370 | return ai.isInstantApp(); |
| 5371 | } catch (RemoteException re) { |
| 5372 | throw new SecurityException("Unknown package " + pkg, re); |
| 5373 | } |
| 5374 | |
| 5375 | } |
| 5376 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5377 | private void checkCallerIsSameApp(String pkg) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5378 | final int uid = Binder.getCallingUid(); |
| 5379 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5380 | ApplicationInfo ai = mPackageManager.getApplicationInfo( |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5381 | pkg, 0, UserHandle.getCallingUserId()); |
Dan Sandler | 09afc2e | 2014-07-18 14:29:20 -0400 | [diff] [blame] | 5382 | if (ai == null) { |
| 5383 | throw new SecurityException("Unknown package " + pkg); |
| 5384 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5385 | if (!UserHandle.isSameApp(ai.uid, uid)) { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5386 | throw new SecurityException("Calling uid " + uid + " gave package " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5387 | + pkg + " which is owned by uid " + ai.uid); |
| 5388 | } |
| 5389 | } catch (RemoteException re) { |
| 5390 | throw new SecurityException("Unknown package " + pkg + "\n" + re); |
| 5391 | } |
| 5392 | } |
| 5393 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 5394 | private static String callStateToString(int state) { |
| 5395 | switch (state) { |
| 5396 | case TelephonyManager.CALL_STATE_IDLE: return "CALL_STATE_IDLE"; |
| 5397 | case TelephonyManager.CALL_STATE_RINGING: return "CALL_STATE_RINGING"; |
| 5398 | case TelephonyManager.CALL_STATE_OFFHOOK: return "CALL_STATE_OFFHOOK"; |
| 5399 | default: return "CALL_STATE_UNKNOWN_" + state; |
| 5400 | } |
| 5401 | } |
| 5402 | |
| 5403 | private void listenForCallState() { |
| 5404 | TelephonyManager.from(getContext()).listen(new PhoneStateListener() { |
| 5405 | @Override |
| 5406 | public void onCallStateChanged(int state, String incomingNumber) { |
| 5407 | if (mCallState == state) return; |
| 5408 | if (DBG) Slog.d(TAG, "Call state changed: " + callStateToString(state)); |
| 5409 | mCallState = state; |
| 5410 | } |
| 5411 | }, PhoneStateListener.LISTEN_CALL_STATE); |
| 5412 | } |
| 5413 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5414 | /** |
| 5415 | * Generates a NotificationRankingUpdate from 'sbns', considering only |
| 5416 | * notifications visible to the given listener. |
| 5417 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5418 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5419 | private NotificationRankingUpdate makeRankingUpdateLocked(ManagedServiceInfo info) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5420 | final int N = mNotificationList.size(); |
| 5421 | ArrayList<String> keys = new ArrayList<String>(N); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 5422 | ArrayList<String> interceptedKeys = new ArrayList<String>(N); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5423 | ArrayList<Integer> importance = new ArrayList<>(N); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5424 | Bundle overrideGroupKeys = new Bundle(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5425 | Bundle visibilityOverrides = new Bundle(); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 5426 | Bundle suppressedVisualEffects = new Bundle(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5427 | Bundle explanation = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5428 | Bundle channels = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5429 | Bundle overridePeople = new Bundle(); |
| 5430 | Bundle snoozeCriteria = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5431 | Bundle showBadge = new Bundle(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5432 | Bundle userSentiment = new Bundle(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5433 | for (int i = 0; i < N; i++) { |
| 5434 | NotificationRecord record = mNotificationList.get(i); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5435 | if (!isVisibleToListener(record.sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5436 | continue; |
| 5437 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5438 | final String key = record.sbn.getKey(); |
| 5439 | keys.add(key); |
| 5440 | importance.add(record.getImportance()); |
| 5441 | if (record.getImportanceExplanation() != null) { |
| 5442 | explanation.putCharSequence(key, record.getImportanceExplanation()); |
| 5443 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5444 | if (record.isIntercepted()) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5445 | interceptedKeys.add(key); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 5446 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5447 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5448 | suppressedVisualEffects.putInt(key, record.getSuppressedVisualEffects()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5449 | if (record.getPackageVisibilityOverride() |
| 5450 | != NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5451 | visibilityOverrides.putInt(key, record.getPackageVisibilityOverride()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5452 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5453 | overrideGroupKeys.putString(key, record.sbn.getOverrideGroupKey()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5454 | channels.putParcelable(key, record.getChannel()); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5455 | overridePeople.putStringArrayList(key, record.getPeopleOverride()); |
| 5456 | snoozeCriteria.putParcelableArrayList(key, record.getSnoozeCriteria()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5457 | showBadge.putBoolean(key, record.canShowBadge()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5458 | userSentiment.putInt(key, record.getUserSentiment()); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5459 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5460 | final int M = keys.size(); |
| 5461 | String[] keysAr = keys.toArray(new String[M]); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 5462 | String[] interceptedKeysAr = interceptedKeys.toArray(new String[interceptedKeys.size()]); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5463 | int[] importanceAr = new int[M]; |
| 5464 | for (int i = 0; i < M; i++) { |
| 5465 | importanceAr[i] = importance.get(i); |
| 5466 | } |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5467 | return new NotificationRankingUpdate(keysAr, interceptedKeysAr, visibilityOverrides, |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5468 | suppressedVisualEffects, importanceAr, explanation, overrideGroupKeys, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5469 | channels, overridePeople, snoozeCriteria, showBadge, userSentiment); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5470 | } |
| 5471 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5472 | boolean hasCompanionDevice(ManagedServiceInfo info) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5473 | if (mCompanionManager == null) { |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 5474 | mCompanionManager = getCompanionManager(); |
| 5475 | } |
| 5476 | // Companion mgr doesn't exist on all device types |
| 5477 | if (mCompanionManager == null) { |
| 5478 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5479 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5480 | long identity = Binder.clearCallingIdentity(); |
| 5481 | try { |
| 5482 | List<String> associations = mCompanionManager.getAssociations( |
| 5483 | info.component.getPackageName(), info.userid); |
| 5484 | if (!ArrayUtils.isEmpty(associations)) { |
| 5485 | return true; |
| 5486 | } |
| 5487 | } catch (SecurityException se) { |
| 5488 | // Not a privileged listener |
| 5489 | } catch (RemoteException re) { |
| 5490 | Slog.e(TAG, "Cannot reach companion device service", re); |
| 5491 | } catch (Exception e) { |
| 5492 | Slog.e(TAG, "Cannot verify listener " + info, e); |
| 5493 | } finally { |
| 5494 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5495 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5496 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5497 | } |
| 5498 | |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 5499 | protected ICompanionDeviceManager getCompanionManager() { |
| 5500 | return ICompanionDeviceManager.Stub.asInterface( |
| 5501 | ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE)); |
| 5502 | } |
| 5503 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5504 | private boolean isVisibleToListener(StatusBarNotification sbn, ManagedServiceInfo listener) { |
| 5505 | if (!listener.enabledAndUserMatches(sbn.getUserId())) { |
| 5506 | return false; |
| 5507 | } |
Justin Koh | 8d11a5a | 2014-08-04 18:29:49 -0700 | [diff] [blame] | 5508 | // TODO: remove this for older listeners. |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5509 | return true; |
| 5510 | } |
| 5511 | |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 5512 | private boolean isPackageSuspendedForUser(String pkg, int uid) { |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5513 | int userId = UserHandle.getUserId(uid); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5514 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5515 | return mPackageManager.isPackageSuspendedForUser(pkg, userId); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5516 | } catch (RemoteException re) { |
| 5517 | throw new SecurityException("Could not talk to package manager service"); |
Andrei Stingaceanu | efc4a34 | 2016-03-22 14:43:01 +0000 | [diff] [blame] | 5518 | } catch (IllegalArgumentException ex) { |
| 5519 | // Package not found. |
| 5520 | return false; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5521 | } |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5522 | } |
| 5523 | |
Julia Reynolds | e181641 | 2017-10-24 10:39:11 -0400 | [diff] [blame] | 5524 | private boolean canUseManagedServices() { |
| 5525 | return !mActivityManager.isLowRamDevice() |
| 5526 | || mPackageManagerClient.hasSystemFeature(PackageManager.FEATURE_WATCH); |
| 5527 | } |
| 5528 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5529 | private class TrimCache { |
| 5530 | StatusBarNotification heavy; |
| 5531 | StatusBarNotification sbnClone; |
| 5532 | StatusBarNotification sbnCloneLight; |
| 5533 | |
| 5534 | TrimCache(StatusBarNotification sbn) { |
| 5535 | heavy = sbn; |
| 5536 | } |
| 5537 | |
| 5538 | StatusBarNotification ForListener(ManagedServiceInfo info) { |
| 5539 | if (mListeners.getOnNotificationPostedTrim(info) == TRIM_LIGHT) { |
| 5540 | if (sbnCloneLight == null) { |
| 5541 | sbnCloneLight = heavy.cloneLight(); |
| 5542 | } |
| 5543 | return sbnCloneLight; |
| 5544 | } else { |
| 5545 | if (sbnClone == null) { |
| 5546 | sbnClone = heavy.clone(); |
| 5547 | } |
| 5548 | return sbnClone; |
| 5549 | } |
| 5550 | } |
| 5551 | } |
| 5552 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5553 | public class NotificationAssistants extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5554 | static final String TAG_ENABLED_NOTIFICATION_ASSISTANTS = "enabled_assistants"; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5555 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5556 | public NotificationAssistants(IPackageManager pm) { |
| 5557 | super(getContext(), mNotificationLock, mUserProfiles, pm); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5558 | } |
| 5559 | |
| 5560 | @Override |
| 5561 | protected Config getConfig() { |
| 5562 | Config c = new Config(); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5563 | c.caption = "notification assistant"; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5564 | c.serviceInterface = NotificationAssistantService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5565 | c.xmlTag = TAG_ENABLED_NOTIFICATION_ASSISTANTS; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5566 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_ASSISTANT; |
| 5567 | c.bindPermission = Manifest.permission.BIND_NOTIFICATION_ASSISTANT_SERVICE; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5568 | c.settingsAction = Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS; |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 5569 | c.clientLabel = R.string.notification_ranker_binding_label; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5570 | return c; |
| 5571 | } |
| 5572 | |
| 5573 | @Override |
| 5574 | protected IInterface asInterface(IBinder binder) { |
| 5575 | return INotificationListener.Stub.asInterface(binder); |
| 5576 | } |
| 5577 | |
| 5578 | @Override |
| 5579 | protected boolean checkType(IInterface service) { |
| 5580 | return service instanceof INotificationListener; |
| 5581 | } |
| 5582 | |
| 5583 | @Override |
| 5584 | protected void onServiceAdded(ManagedServiceInfo info) { |
| 5585 | mListeners.registerGuestService(info); |
| 5586 | } |
| 5587 | |
| 5588 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5589 | @GuardedBy("mNotificationLock") |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5590 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
| 5591 | mListeners.unregisterService(removed.service, removed.userid); |
| 5592 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5593 | |
| 5594 | public void onNotificationEnqueued(final NotificationRecord r) { |
| 5595 | final StatusBarNotification sbn = r.sbn; |
| 5596 | TrimCache trimCache = new TrimCache(sbn); |
| 5597 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5598 | // There should be only one, but it's a list, so while we enforce |
| 5599 | // singularity elsewhere, we keep it general here, to avoid surprises. |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5600 | for (final ManagedServiceInfo info : NotificationAssistants.this.getServices()) { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5601 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 5602 | if (!sbnVisible) { |
| 5603 | continue; |
| 5604 | } |
| 5605 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5606 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 5607 | mHandler.post(new Runnable() { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5608 | @Override |
| 5609 | public void run() { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5610 | notifyEnqueued(info, sbnToPost); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5611 | } |
| 5612 | }); |
| 5613 | } |
| 5614 | } |
| 5615 | |
| 5616 | private void notifyEnqueued(final ManagedServiceInfo info, |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5617 | final StatusBarNotification sbn) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5618 | final INotificationListener assistant = (INotificationListener) info.service; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5619 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
| 5620 | try { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5621 | assistant.onNotificationEnqueued(sbnHolder); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5622 | } catch (RemoteException ex) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5623 | Log.e(TAG, "unable to notify assistant (enqueued): " + assistant, ex); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5624 | } |
| 5625 | } |
| 5626 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5627 | /** |
| 5628 | * asynchronously notify the assistant that a notification has been snoozed until a |
| 5629 | * context |
| 5630 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5631 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5632 | public void notifyAssistantSnoozedLocked(final StatusBarNotification sbn, |
| 5633 | final String snoozeCriterionId) { |
| 5634 | TrimCache trimCache = new TrimCache(sbn); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5635 | for (final ManagedServiceInfo info : getServices()) { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5636 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 5637 | if (!sbnVisible) { |
| 5638 | continue; |
| 5639 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5640 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
| 5641 | mHandler.post(new Runnable() { |
| 5642 | @Override |
| 5643 | public void run() { |
| 5644 | final INotificationListener assistant = |
| 5645 | (INotificationListener) info.service; |
| 5646 | StatusBarNotificationHolder sbnHolder |
| 5647 | = new StatusBarNotificationHolder(sbnToPost); |
| 5648 | try { |
| 5649 | assistant.onNotificationSnoozedUntilContext( |
| 5650 | sbnHolder, snoozeCriterionId); |
| 5651 | } catch (RemoteException ex) { |
| 5652 | Log.e(TAG, "unable to notify assistant (snoozed): " + assistant, ex); |
| 5653 | } |
| 5654 | } |
| 5655 | }); |
| 5656 | } |
| 5657 | } |
| 5658 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5659 | public boolean isEnabled() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5660 | return !getServices().isEmpty(); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5661 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5662 | } |
| 5663 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5664 | public class NotificationListeners extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5665 | static final String TAG_ENABLED_NOTIFICATION_LISTENERS = "enabled_listeners"; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5666 | |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5667 | private final ArraySet<ManagedServiceInfo> mLightTrimListeners = new ArraySet<>(); |
| 5668 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5669 | public NotificationListeners(IPackageManager pm) { |
| 5670 | super(getContext(), mNotificationLock, mUserProfiles, pm); |
| 5671 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5672 | } |
| 5673 | |
| 5674 | @Override |
| 5675 | protected Config getConfig() { |
| 5676 | Config c = new Config(); |
| 5677 | c.caption = "notification listener"; |
| 5678 | c.serviceInterface = NotificationListenerService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5679 | c.xmlTag = TAG_ENABLED_NOTIFICATION_LISTENERS; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5680 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS; |
| 5681 | c.bindPermission = android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE; |
| 5682 | c.settingsAction = Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS; |
| 5683 | c.clientLabel = R.string.notification_listener_binding_label; |
| 5684 | return c; |
| 5685 | } |
| 5686 | |
| 5687 | @Override |
| 5688 | protected IInterface asInterface(IBinder binder) { |
| 5689 | return INotificationListener.Stub.asInterface(binder); |
| 5690 | } |
| 5691 | |
| 5692 | @Override |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5693 | protected boolean checkType(IInterface service) { |
| 5694 | return service instanceof INotificationListener; |
| 5695 | } |
| 5696 | |
| 5697 | @Override |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 5698 | public void onServiceAdded(ManagedServiceInfo info) { |
| 5699 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5700 | final NotificationRankingUpdate update; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5701 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5702 | update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5703 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5704 | try { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5705 | listener.onListenerConnected(update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5706 | } catch (RemoteException e) { |
| 5707 | // we tried |
| 5708 | } |
| 5709 | } |
| 5710 | |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5711 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5712 | @GuardedBy("mNotificationLock") |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5713 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 5714 | if (removeDisabledHints(removed)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5715 | updateListenerHintsLocked(); |
Christoph Studer | 0d6ef4b | 2014-12-02 15:00:48 +0100 | [diff] [blame] | 5716 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5717 | } |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5718 | mLightTrimListeners.remove(removed); |
| 5719 | } |
| 5720 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5721 | @GuardedBy("mNotificationLock") |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5722 | public void setOnNotificationPostedTrimLocked(ManagedServiceInfo info, int trim) { |
| 5723 | if (trim == TRIM_LIGHT) { |
| 5724 | mLightTrimListeners.add(info); |
| 5725 | } else { |
| 5726 | mLightTrimListeners.remove(info); |
| 5727 | } |
| 5728 | } |
| 5729 | |
| 5730 | public int getOnNotificationPostedTrim(ManagedServiceInfo info) { |
| 5731 | return mLightTrimListeners.contains(info) ? TRIM_LIGHT : TRIM_FULL; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5732 | } |
| 5733 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5734 | /** |
| 5735 | * asynchronously notify all listeners about a new notification |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5736 | * |
| 5737 | * <p> |
| 5738 | * Also takes care of removing a notification that has been visible to a listener before, |
| 5739 | * but isn't anymore. |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5740 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5741 | @GuardedBy("mNotificationLock") |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5742 | public void notifyPostedLocked(StatusBarNotification sbn, StatusBarNotification oldSbn) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5743 | // Lazily initialized snapshots of the notification. |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5744 | TrimCache trimCache = new TrimCache(sbn); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5745 | |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5746 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5747 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 5748 | boolean oldSbnVisible = oldSbn != null ? isVisibleToListener(oldSbn, info) : false; |
| 5749 | // This notification hasn't been and still isn't visible -> ignore. |
| 5750 | if (!oldSbnVisible && !sbnVisible) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5751 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5752 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5753 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5754 | |
| 5755 | // This notification became invisible -> remove the old one. |
| 5756 | if (oldSbnVisible && !sbnVisible) { |
| 5757 | final StatusBarNotification oldSbnLightClone = oldSbn.cloneLight(); |
| 5758 | mHandler.post(new Runnable() { |
| 5759 | @Override |
| 5760 | public void run() { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5761 | notifyRemoved( |
| 5762 | info, oldSbnLightClone, update, null, REASON_USER_STOPPED); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5763 | } |
| 5764 | }); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5765 | continue; |
| 5766 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5767 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5768 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5769 | mHandler.post(new Runnable() { |
| 5770 | @Override |
| 5771 | public void run() { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5772 | notifyPosted(info, sbnToPost, update); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5773 | } |
| 5774 | }); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5775 | } |
| 5776 | } |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5777 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5778 | /** |
| 5779 | * asynchronously notify all listeners about a removed notification |
| 5780 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5781 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5782 | public void notifyRemovedLocked(StatusBarNotification sbn, int reason, |
| 5783 | NotificationStats notificationStats) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5784 | // make a copy in case changes are made to the underlying Notification object |
| 5785 | // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the |
| 5786 | // notification |
| 5787 | final StatusBarNotification sbnLight = sbn.cloneLight(); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5788 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5789 | if (!isVisibleToListener(sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5790 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5791 | } |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5792 | // Only assistants can get stats |
| 5793 | final NotificationStats stats = mAssistants.isServiceTokenValidLocked(info.service) |
| 5794 | ? notificationStats : null; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5795 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5796 | mHandler.post(new Runnable() { |
| 5797 | @Override |
| 5798 | public void run() { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5799 | notifyRemoved(info, sbnLight, update, stats, reason); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5800 | } |
| 5801 | }); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5802 | } |
| 5803 | } |
| 5804 | |
| 5805 | /** |
| 5806 | * asynchronously notify all listeners about a reordering of notifications |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5807 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5808 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5809 | public void notifyRankingUpdateLocked() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5810 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5811 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 5812 | continue; |
| 5813 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5814 | final NotificationRankingUpdate update = makeRankingUpdateLocked(serviceInfo); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5815 | mHandler.post(new Runnable() { |
| 5816 | @Override |
| 5817 | public void run() { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5818 | notifyRankingUpdate(serviceInfo, update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5819 | } |
| 5820 | }); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5821 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5822 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5823 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5824 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5825 | public void notifyListenerHintsChangedLocked(final int hints) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5826 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5827 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 5828 | continue; |
| 5829 | } |
| 5830 | mHandler.post(new Runnable() { |
| 5831 | @Override |
| 5832 | public void run() { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5833 | notifyListenerHintsChanged(serviceInfo, hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5834 | } |
| 5835 | }); |
| 5836 | } |
| 5837 | } |
| 5838 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5839 | public void notifyInterruptionFilterChanged(final int interruptionFilter) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5840 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5841 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 5842 | continue; |
| 5843 | } |
| 5844 | mHandler.post(new Runnable() { |
| 5845 | @Override |
| 5846 | public void run() { |
| 5847 | notifyInterruptionFilterChanged(serviceInfo, interruptionFilter); |
| 5848 | } |
| 5849 | }); |
| 5850 | } |
| 5851 | } |
| 5852 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5853 | protected void notifyNotificationChannelChanged(final String pkg, final UserHandle user, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5854 | final NotificationChannel channel, final int modificationType) { |
| 5855 | if (channel == null) { |
| 5856 | return; |
| 5857 | } |
| 5858 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5859 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5860 | continue; |
| 5861 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5862 | |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 5863 | BackgroundThread.getHandler().post(() -> { |
| 5864 | if (hasCompanionDevice(serviceInfo)) { |
| 5865 | notifyNotificationChannelChanged( |
| 5866 | serviceInfo, pkg, user, channel, modificationType); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5867 | } |
| 5868 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5869 | } |
| 5870 | } |
| 5871 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5872 | protected void notifyNotificationChannelGroupChanged( |
| 5873 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
| 5874 | final int modificationType) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5875 | if (group == null) { |
| 5876 | return; |
| 5877 | } |
| 5878 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5879 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5880 | continue; |
| 5881 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5882 | |
Eugene Susla | a25d17f | 2017-08-24 11:28:08 -0700 | [diff] [blame] | 5883 | BackgroundThread.getHandler().post(() -> { |
| 5884 | if (hasCompanionDevice(serviceInfo)) { |
| 5885 | notifyNotificationChannelGroupChanged( |
| 5886 | serviceInfo, pkg, user, group, modificationType); |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5887 | } |
| 5888 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5889 | } |
| 5890 | } |
| 5891 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5892 | private void notifyPosted(final ManagedServiceInfo info, |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5893 | final StatusBarNotification sbn, NotificationRankingUpdate rankingUpdate) { |
Julia Reynolds | a75c752 | 2017-03-21 17:34:25 -0400 | [diff] [blame] | 5894 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5895 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5896 | try { |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5897 | listener.onNotificationPosted(sbnHolder, rankingUpdate); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5898 | } catch (RemoteException ex) { |
| 5899 | Log.e(TAG, "unable to notify listener (posted): " + listener, ex); |
| 5900 | } |
| 5901 | } |
| 5902 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5903 | private void notifyRemoved(ManagedServiceInfo info, StatusBarNotification sbn, |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5904 | NotificationRankingUpdate rankingUpdate, NotificationStats stats, int reason) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5905 | if (!info.enabledAndUserMatches(sbn.getUserId())) { |
| 5906 | return; |
| 5907 | } |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5908 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5909 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5910 | try { |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 5911 | listener.onNotificationRemoved(sbnHolder, rankingUpdate, stats, reason); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5912 | } catch (RemoteException ex) { |
| 5913 | Log.e(TAG, "unable to notify listener (removed): " + listener, ex); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 5914 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5915 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5916 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5917 | private void notifyRankingUpdate(ManagedServiceInfo info, |
| 5918 | NotificationRankingUpdate rankingUpdate) { |
| 5919 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5920 | try { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5921 | listener.onNotificationRankingUpdate(rankingUpdate); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5922 | } catch (RemoteException ex) { |
| 5923 | Log.e(TAG, "unable to notify listener (ranking update): " + listener, ex); |
| 5924 | } |
| 5925 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5926 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5927 | private void notifyListenerHintsChanged(ManagedServiceInfo info, int hints) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5928 | final INotificationListener listener = (INotificationListener) info.service; |
| 5929 | try { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5930 | listener.onListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5931 | } catch (RemoteException ex) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5932 | Log.e(TAG, "unable to notify listener (listener hints): " + listener, ex); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5933 | } |
| 5934 | } |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 5935 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5936 | private void notifyInterruptionFilterChanged(ManagedServiceInfo info, |
| 5937 | int interruptionFilter) { |
| 5938 | final INotificationListener listener = (INotificationListener) info.service; |
| 5939 | try { |
| 5940 | listener.onInterruptionFilterChanged(interruptionFilter); |
| 5941 | } catch (RemoteException ex) { |
| 5942 | Log.e(TAG, "unable to notify listener (interruption filter): " + listener, ex); |
| 5943 | } |
| 5944 | } |
| 5945 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5946 | void notifyNotificationChannelChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5947 | final String pkg, final UserHandle user, final NotificationChannel channel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5948 | final int modificationType) { |
| 5949 | final INotificationListener listener = (INotificationListener) info.service; |
| 5950 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5951 | listener.onNotificationChannelModification(pkg, user, channel, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5952 | } catch (RemoteException ex) { |
| 5953 | Log.e(TAG, "unable to notify listener (channel changed): " + listener, ex); |
| 5954 | } |
| 5955 | } |
| 5956 | |
| 5957 | private void notifyNotificationChannelGroupChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5958 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5959 | final int modificationType) { |
| 5960 | final INotificationListener listener = (INotificationListener) info.service; |
| 5961 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5962 | listener.onNotificationChannelGroupModification(pkg, user, group, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5963 | } catch (RemoteException ex) { |
| 5964 | Log.e(TAG, "unable to notify listener (channel group changed): " + listener, ex); |
| 5965 | } |
| 5966 | } |
| 5967 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5968 | public boolean isListenerPackage(String packageName) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 5969 | if (packageName == null) { |
| 5970 | return false; |
| 5971 | } |
| 5972 | // TODO: clean up locking object later |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5973 | synchronized (mNotificationLock) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5974 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 5975 | if (packageName.equals(serviceInfo.component.getPackageName())) { |
| 5976 | return true; |
| 5977 | } |
| 5978 | } |
| 5979 | } |
| 5980 | return false; |
| 5981 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5982 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5983 | |
| 5984 | public static final class DumpFilter { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5985 | public boolean filtered = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5986 | public String pkgFilter; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 5987 | public boolean zen; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 5988 | public long since; |
| 5989 | public boolean stats; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5990 | public boolean redact = true; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 5991 | public boolean proto = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5992 | |
Kweku Adams | 887f09c | 2017-11-13 17:12:20 -0800 | [diff] [blame] | 5993 | @NonNull |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5994 | public static DumpFilter parseFromArguments(String[] args) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5995 | final DumpFilter filter = new DumpFilter(); |
| 5996 | for (int ai = 0; ai < args.length; ai++) { |
| 5997 | final String a = args[ai]; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 5998 | if ("--proto".equals(a)) { |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 5999 | filter.proto = true; |
Kweku Adams | 62b4224 | 2017-09-25 12:54:02 -0700 | [diff] [blame] | 6000 | } else if ("--noredact".equals(a) || "--reveal".equals(a)) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6001 | filter.redact = false; |
| 6002 | } else if ("p".equals(a) || "pkg".equals(a) || "--package".equals(a)) { |
| 6003 | if (ai < args.length-1) { |
| 6004 | ai++; |
| 6005 | filter.pkgFilter = args[ai].trim().toLowerCase(); |
| 6006 | if (filter.pkgFilter.isEmpty()) { |
| 6007 | filter.pkgFilter = null; |
| 6008 | } else { |
| 6009 | filter.filtered = true; |
| 6010 | } |
| 6011 | } |
| 6012 | } else if ("--zen".equals(a) || "zen".equals(a)) { |
| 6013 | filter.filtered = true; |
| 6014 | filter.zen = true; |
| 6015 | } else if ("--stats".equals(a)) { |
| 6016 | filter.stats = true; |
| 6017 | if (ai < args.length-1) { |
| 6018 | ai++; |
Tobias Thierer | 28532d0 | 2016-04-21 14:52:10 +0100 | [diff] [blame] | 6019 | filter.since = Long.parseLong(args[ai]); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6020 | } else { |
| 6021 | filter.since = 0; |
| 6022 | } |
| 6023 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6024 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6025 | return filter; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6026 | } |
| 6027 | |
| 6028 | public boolean matches(StatusBarNotification sbn) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6029 | if (!filtered) return true; |
| 6030 | return zen ? true : sbn != null |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 6031 | && (matches(sbn.getPackageName()) || matches(sbn.getOpPkg())); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6032 | } |
| 6033 | |
| 6034 | public boolean matches(ComponentName component) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6035 | if (!filtered) return true; |
| 6036 | return zen ? true : component != null && matches(component.getPackageName()); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6037 | } |
| 6038 | |
| 6039 | public boolean matches(String pkg) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 6040 | if (!filtered) return true; |
| 6041 | return zen ? true : pkg != null && pkg.toLowerCase().contains(pkgFilter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 6042 | } |
| 6043 | |
| 6044 | @Override |
| 6045 | public String toString() { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 6046 | return stats ? "stats" : zen ? "zen" : ('\'' + pkgFilter + '\''); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 6047 | } |
| 6048 | } |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6049 | |
| 6050 | /** |
| 6051 | * Wrapper for a StatusBarNotification object that allows transfer across a oneway |
| 6052 | * binder without sending large amounts of data over a oneway transaction. |
| 6053 | */ |
| 6054 | private static final class StatusBarNotificationHolder |
| 6055 | extends IStatusBarNotificationHolder.Stub { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 6056 | private StatusBarNotification mValue; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6057 | |
| 6058 | public StatusBarNotificationHolder(StatusBarNotification value) { |
| 6059 | mValue = value; |
| 6060 | } |
| 6061 | |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 6062 | /** 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] | 6063 | @Override |
| 6064 | public StatusBarNotification get() { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 6065 | StatusBarNotification value = mValue; |
| 6066 | mValue = null; |
| 6067 | return value; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 6068 | } |
| 6069 | } |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6070 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6071 | private class ShellCmd extends ShellCommand { |
| 6072 | public static final String USAGE = "help\n" |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 6073 | + "allow_listener COMPONENT [user_id]\n" |
| 6074 | + "disallow_listener COMPONENT [user_id]\n" |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6075 | + "set_assistant COMPONENT\n" |
| 6076 | + "remove_assistant COMPONENT\n" |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6077 | + "allow_dnd PACKAGE\n" |
| 6078 | + "disallow_dnd PACKAGE"; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6079 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6080 | @Override |
| 6081 | public int onCommand(String cmd) { |
Felipe Leme | 68d8041 | 2017-07-14 11:18:08 -0700 | [diff] [blame] | 6082 | if (cmd == null) { |
| 6083 | return handleDefaultCommands(cmd); |
| 6084 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 6085 | final PrintWriter pw = getOutPrintWriter(); |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 6086 | try { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6087 | switch (cmd) { |
| 6088 | case "allow_dnd": { |
| 6089 | getBinderService().setNotificationPolicyAccessGranted( |
| 6090 | getNextArgRequired(), true); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6091 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6092 | break; |
| 6093 | |
| 6094 | case "disallow_dnd": { |
| 6095 | getBinderService().setNotificationPolicyAccessGranted( |
| 6096 | getNextArgRequired(), false); |
| 6097 | } |
| 6098 | break; |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 6099 | case "allow_listener": { |
| 6100 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 6101 | if (cn == null) { |
| 6102 | pw.println("Invalid listener - must be a ComponentName"); |
| 6103 | return -1; |
| 6104 | } |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 6105 | String userId = getNextArg(); |
| 6106 | if (userId == null) { |
| 6107 | getBinderService().setNotificationListenerAccessGranted(cn, true); |
| 6108 | } else { |
| 6109 | getBinderService().setNotificationListenerAccessGrantedForUser( |
| 6110 | cn, Integer.parseInt(userId), true); |
| 6111 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 6112 | } |
| 6113 | break; |
| 6114 | case "disallow_listener": { |
| 6115 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 6116 | if (cn == null) { |
| 6117 | pw.println("Invalid listener - must be a ComponentName"); |
| 6118 | return -1; |
| 6119 | } |
Jaewan Kim | 3c45c4c | 2017-09-21 23:32:11 +0900 | [diff] [blame] | 6120 | String userId = getNextArg(); |
| 6121 | if (userId == null) { |
| 6122 | getBinderService().setNotificationListenerAccessGranted(cn, false); |
| 6123 | } else { |
| 6124 | getBinderService().setNotificationListenerAccessGrantedForUser( |
| 6125 | cn, Integer.parseInt(userId), false); |
| 6126 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 6127 | } |
| 6128 | break; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 6129 | case "allow_assistant": { |
| 6130 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 6131 | if (cn == null) { |
| 6132 | pw.println("Invalid assistant - must be a ComponentName"); |
| 6133 | return -1; |
| 6134 | } |
| 6135 | getBinderService().setNotificationAssistantAccessGranted(cn, true); |
| 6136 | } |
| 6137 | break; |
| 6138 | case "disallow_assistant": { |
| 6139 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 6140 | if (cn == null) { |
| 6141 | pw.println("Invalid assistant - must be a ComponentName"); |
| 6142 | return -1; |
| 6143 | } |
| 6144 | getBinderService().setNotificationAssistantAccessGranted(cn, false); |
| 6145 | } |
| 6146 | break; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6147 | |
| 6148 | default: |
| 6149 | return handleDefaultCommands(cmd); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6150 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 6151 | } catch (Exception e) { |
| 6152 | pw.println("Error occurred. Check logcat for details. " + e.getMessage()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6153 | Slog.e(TAG, "Error running shell command", e); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6154 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6155 | return 0; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6156 | } |
| 6157 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 6158 | @Override |
| 6159 | public void onHelp() { |
| 6160 | getOutPrintWriter().println(USAGE); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 6161 | } |
| 6162 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6163 | } |