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 | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 19 | import static android.app.NotificationManager.IMPORTANCE_MIN; |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 20 | import static android.app.NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 21 | import static android.content.pm.PackageManager.FEATURE_LEANBACK; |
| 22 | import static android.content.pm.PackageManager.FEATURE_TELEVISION; |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 23 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 24 | import static android.os.UserHandle.USER_NULL; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 25 | import static android.service.notification.NotificationListenerService |
| 26 | .NOTIFICATION_CHANNEL_OR_GROUP_ADDED; |
| 27 | import static android.service.notification.NotificationListenerService |
| 28 | .NOTIFICATION_CHANNEL_OR_GROUP_DELETED; |
| 29 | import static android.service.notification.NotificationListenerService |
| 30 | .NOTIFICATION_CHANNEL_OR_GROUP_UPDATED; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 31 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL; |
| 32 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL; |
| 33 | import static android.service.notification.NotificationListenerService.REASON_CHANNEL_BANNED; |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 34 | import static android.service.notification.NotificationListenerService.REASON_CANCEL; |
| 35 | import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL; |
| 36 | import static android.service.notification.NotificationListenerService.REASON_CLICK; |
| 37 | import static android.service.notification.NotificationListenerService.REASON_ERROR; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 38 | import static android.service.notification.NotificationListenerService.REASON_GROUP_SUMMARY_CANCELED; |
| 39 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL; |
| 40 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL_ALL; |
| 41 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_BANNED; |
| 42 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_CHANGED; |
| 43 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_SUSPENDED; |
| 44 | import static android.service.notification.NotificationListenerService.REASON_PROFILE_TURNED_OFF; |
| 45 | import static android.service.notification.NotificationListenerService.REASON_SNOOZED; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 46 | import static android.service.notification.NotificationListenerService.REASON_TIMEOUT; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 47 | import static android.service.notification.NotificationListenerService.REASON_UNAUTOBUNDLED; |
| 48 | import static android.service.notification.NotificationListenerService.REASON_USER_STOPPED; |
Jason Monk | 6350674 | 2015-12-16 12:06:51 -0500 | [diff] [blame] | 49 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_EFFECTS; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 50 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_NOTIFICATION_EFFECTS; |
| 51 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS; |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 52 | import static android.service.notification.NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF; |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 53 | import static android.service.notification.NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_ON; |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 54 | import static android.service.notification.NotificationListenerService.TRIM_FULL; |
| 55 | import static android.service.notification.NotificationListenerService.TRIM_LIGHT; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 56 | |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 57 | import static android.view.Display.DEFAULT_DISPLAY; |
| 58 | import static android.view.WindowManager.LayoutParams.TYPE_TOAST; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 59 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 60 | import android.Manifest; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 61 | import android.annotation.NonNull; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 62 | import android.annotation.Nullable; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 63 | import android.app.ActivityManager; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 64 | import android.app.ActivityManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 65 | import android.app.AlarmManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 66 | import android.app.AppGlobals; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 67 | import android.app.AppOpsManager; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 68 | import android.app.AutomaticZenRule; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 69 | import android.app.NotificationChannelGroup; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 70 | import android.app.backup.BackupManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | import android.app.IActivityManager; |
| 72 | import android.app.INotificationManager; |
| 73 | import android.app.ITransientNotification; |
| 74 | import android.app.Notification; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 75 | import android.app.NotificationChannel; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 76 | import android.app.NotificationManager.Policy; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 77 | import android.app.NotificationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | import android.app.PendingIntent; |
| 79 | import android.app.StatusBarManager; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 80 | import android.app.usage.UsageEvents; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 81 | import android.app.usage.UsageStatsManagerInternal; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 82 | import android.companion.ICompanionDeviceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | import android.content.BroadcastReceiver; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 84 | import android.content.ComponentName; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 85 | import android.content.ContentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | import android.content.Context; |
| 87 | import android.content.Intent; |
| 88 | import android.content.IntentFilter; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 89 | import android.content.pm.ApplicationInfo; |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 90 | import android.content.pm.IPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | import android.content.pm.PackageManager; |
| 92 | import android.content.pm.PackageManager.NameNotFoundException; |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 93 | import android.content.pm.ParceledListSlice; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | import android.content.res.Resources; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 95 | import android.database.ContentObserver; |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 96 | import android.media.AudioAttributes; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 97 | import android.media.AudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 98 | import android.media.AudioManagerInternal; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 99 | import android.media.IRingtonePlayer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | import android.os.Binder; |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 102 | import android.os.Build; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 103 | import android.os.Bundle; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 104 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | import android.os.Handler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 106 | import android.os.HandlerThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | import android.os.IBinder; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 108 | import android.os.IInterface; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 109 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import android.os.Message; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 111 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 112 | import android.os.RemoteException; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 113 | import android.os.ResultReceiver; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 114 | import android.os.ServiceManager; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 115 | import android.os.ShellCallback; |
| 116 | import android.os.ShellCommand; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 117 | import android.os.SystemClock; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 118 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 119 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | import android.os.Vibrator; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 121 | import android.os.VibrationEffect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | import android.provider.Settings; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 123 | import android.service.notification.Adjustment; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 124 | import android.service.notification.Condition; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 125 | import android.service.notification.IConditionProvider; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 126 | import android.service.notification.INotificationListener; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 127 | import android.service.notification.IStatusBarNotificationHolder; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 128 | import android.service.notification.NotificationAssistantService; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 129 | import android.service.notification.NotificationListenerService; |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 130 | import android.service.notification.NotificationRankingUpdate; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 131 | import android.service.notification.NotificationRecordProto; |
| 132 | import android.service.notification.NotificationServiceDumpProto; |
| 133 | import android.service.notification.NotificationServiceProto; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 134 | import android.service.notification.SnoozeCriterion; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 135 | import android.service.notification.StatusBarNotification; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 136 | import android.service.notification.ZenModeConfig; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 137 | import android.service.notification.ZenModeProto; |
Sailesh Nepal | e8bde70 | 2017-07-21 11:44:04 -0700 | [diff] [blame] | 138 | import android.telecom.TelecomManager; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 139 | import android.telephony.PhoneStateListener; |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 140 | import android.telephony.TelephonyManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 141 | import android.text.TextUtils; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 142 | import android.util.ArrayMap; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 143 | import android.util.ArraySet; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 144 | import android.util.AtomicFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | import android.util.Log; |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 146 | import android.util.Slog; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 147 | import android.util.SparseArray; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 148 | import android.util.Xml; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 149 | import android.util.proto.ProtoOutputStream; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 150 | import android.view.WindowManagerInternal; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 151 | import android.view.accessibility.AccessibilityEvent; |
| 152 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | import android.widget.Toast; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 154 | |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 155 | import com.android.internal.R; |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 156 | import com.android.internal.annotations.GuardedBy; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 157 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 158 | import com.android.internal.logging.MetricsLogger; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 159 | import com.android.internal.logging.nano.MetricsProto; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 160 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 161 | import com.android.internal.statusbar.NotificationVisibility; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 162 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 163 | import com.android.internal.util.DumpUtils; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 164 | import com.android.internal.util.FastXmlSerializer; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 165 | import com.android.internal.util.Preconditions; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 166 | import com.android.internal.util.XmlUtils; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 167 | import com.android.server.DeviceIdleController; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 168 | import com.android.server.EventLogTags; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 169 | import com.android.server.LocalServices; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 170 | import com.android.server.SystemService; |
| 171 | import com.android.server.lights.Light; |
| 172 | import com.android.server.lights.LightsManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 173 | import com.android.server.notification.ManagedServices.ManagedServiceInfo; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 174 | import com.android.server.policy.PhoneWindowManager; |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 175 | import com.android.server.statusbar.StatusBarManagerInternal; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 176 | import com.android.server.notification.ManagedServices.UserProfiles; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 177 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 178 | import libcore.io.IoUtils; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 179 | |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 180 | import org.json.JSONException; |
| 181 | import org.json.JSONObject; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 182 | import org.xmlpull.v1.XmlPullParser; |
| 183 | import org.xmlpull.v1.XmlPullParserException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 184 | import org.xmlpull.v1.XmlSerializer; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 185 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 186 | import java.io.ByteArrayInputStream; |
| 187 | import java.io.ByteArrayOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 188 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | import java.io.FileDescriptor; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 190 | import java.io.FileNotFoundException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 191 | import java.io.FileOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 192 | import java.io.IOException; |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 193 | import java.io.InputStream; |
| 194 | import java.io.OutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | import java.io.PrintWriter; |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 196 | import java.net.URI; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 197 | import java.nio.charset.StandardCharsets; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 198 | import java.util.ArrayDeque; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | import java.util.ArrayList; |
Michael Wright | bc4d0d9 | 2017-03-23 18:57:57 +0000 | [diff] [blame] | 200 | import java.util.Arrays; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 201 | import java.util.Iterator; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 202 | import java.util.List; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 203 | import java.util.Map.Entry; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 204 | import java.util.Objects; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 205 | import java.util.Set; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 206 | import java.util.concurrent.TimeUnit; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 207 | |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 208 | /** {@hide} */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 209 | public class NotificationManagerService extends SystemService { |
| 210 | static final String TAG = "NotificationService"; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 211 | static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Selim Cinek | 4041249 | 2015-12-08 18:03:22 -0800 | [diff] [blame] | 212 | public static final boolean ENABLE_CHILD_NOTIFICATIONS |
| 213 | = SystemProperties.getBoolean("debug.child_notifs", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 215 | static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 216 | static final float DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE = 5f; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 217 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | // message codes |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 219 | static final int MESSAGE_TIMEOUT = 2; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 220 | static final int MESSAGE_SAVE_POLICY_FILE = 3; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 221 | static final int MESSAGE_SEND_RANKING_UPDATE = 4; |
| 222 | static final int MESSAGE_LISTENER_HINTS_CHANGED = 5; |
| 223 | static final int MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED = 6; |
| 224 | |
| 225 | // ranking thread messages |
| 226 | private static final int MESSAGE_RECONSIDER_RANKING = 1000; |
| 227 | private static final int MESSAGE_RANKING_SORT = 1001; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 229 | static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 230 | static final int SHORT_DELAY = 2000; // 2 seconds |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 231 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 232 | static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 233 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 234 | static final long SNOOZE_UNTIL_UNSPECIFIED = -1; |
| 235 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 236 | 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] | 237 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 238 | static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION; |
Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 239 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 240 | static final boolean ENABLE_BLOCKED_TOASTS = true; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 241 | |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 242 | // When #matchesCallFilter is called from the ringer, wait at most |
| 243 | // 3s to resolve the contacts. This timeout is required since |
| 244 | // ContactsProvider might take a long time to start up. |
| 245 | // |
| 246 | // Return STARRED_CONTACT when the timeout is hit in order to avoid |
| 247 | // missed calls in ZEN mode "Important". |
| 248 | static final int MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS = 3000; |
| 249 | static final float MATCHES_CALL_FILTER_TIMEOUT_AFFINITY = |
| 250 | ValidateNotificationPeople.STARRED_CONTACT; |
| 251 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 252 | /** notification_enqueue status value for a newly enqueued notification. */ |
| 253 | private static final int EVENTLOG_ENQUEUE_STATUS_NEW = 0; |
| 254 | |
| 255 | /** notification_enqueue status value for an existing notification. */ |
| 256 | private static final int EVENTLOG_ENQUEUE_STATUS_UPDATE = 1; |
| 257 | |
| 258 | /** notification_enqueue status value for an ignored notification. */ |
| 259 | private static final int EVENTLOG_ENQUEUE_STATUS_IGNORED = 2; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 260 | private static final long MIN_PACKAGE_OVERRATE_LOG_INTERVAL = 5000; // milliseconds |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 261 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 262 | private static final long DELAY_FOR_ASSISTANT_TIME = 100; |
| 263 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 264 | private static final String ACTION_NOTIFICATION_TIMEOUT = |
| 265 | NotificationManagerService.class.getSimpleName() + ".TIMEOUT"; |
| 266 | private static final int REQUEST_CODE_TIMEOUT = 1; |
| 267 | private static final String SCHEME_TIMEOUT = "timeout"; |
| 268 | private static final String EXTRA_KEY = "key"; |
| 269 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 270 | private IActivityManager mAm; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 271 | private ActivityManager mActivityManager; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 272 | private IPackageManager mPackageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 273 | private PackageManager mPackageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 274 | AudioManager mAudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 275 | AudioManagerInternal mAudioManagerInternal; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 276 | @Nullable StatusBarManagerInternal mStatusBar; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 277 | Vibrator mVibrator; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 278 | private WindowManagerInternal mWindowManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 279 | private AlarmManager mAlarmManager; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 280 | private ICompanionDeviceManager mCompanionManager; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 281 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | final IBinder mForegroundToken = new Binder(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 283 | private WorkerHandler mHandler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 284 | private final HandlerThread mRankingThread = new HandlerThread("ranker", |
| 285 | Process.THREAD_PRIORITY_BACKGROUND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 287 | private Light mNotificationLight; |
| 288 | Light mAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 289 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 290 | private long[] mFallbackVibrationPattern; |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 291 | private boolean mUseAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 292 | boolean mSystemReady; |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 293 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 294 | private boolean mDisableNotificationEffects; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 295 | private int mCallState; |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 296 | private String mSoundNotificationKey; |
| 297 | private String mVibrateNotificationKey; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 299 | private final SparseArray<ArraySet<ManagedServiceInfo>> mListenersDisablingEffects = |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 300 | new SparseArray<>(); |
| 301 | private List<ComponentName> mEffectsSuppressors = new ArrayList<>(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 302 | private int mListenerHints; // right now, all hints are global |
John Spurlock | 8310410 | 2015-02-12 23:25:12 -0500 | [diff] [blame] | 303 | private int mInterruptionFilter = NotificationListenerService.INTERRUPTION_FILTER_UNKNOWN; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 304 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 305 | // for enabling and disabling notification pulse behavior |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 306 | private boolean mScreenOn = true; |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 307 | protected boolean mInCall = false; |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 308 | private boolean mNotificationPulseEnabled; |
| 309 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 310 | private Uri mInCallNotificationUri; |
| 311 | private AudioAttributes mInCallNotificationAudioAttributes; |
| 312 | private float mInCallNotificationVolume; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 313 | |
Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 314 | // used as a mutex for access to all active notifications & listeners |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 315 | final Object mNotificationLock = new Object(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 316 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 317 | final ArrayList<NotificationRecord> mNotificationList = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 318 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 319 | final ArrayMap<String, NotificationRecord> mNotificationsByKey = new ArrayMap<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 320 | @GuardedBy("mNotificationLock") |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 321 | final ArrayList<NotificationRecord> mEnqueuedNotifications = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 322 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 323 | final ArrayMap<Integer, ArrayMap<String, String>> mAutobundledSummaries = new ArrayMap<>(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 324 | final ArrayList<ToastRecord> mToastQueue = new ArrayList<>(); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 325 | final ArrayMap<String, NotificationRecord> mSummaryByGroupKey = new ArrayMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 327 | // The last key in this list owns the hardware. |
| 328 | ArrayList<String> mLights = new ArrayList<>(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 329 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 330 | private AppOpsManager mAppOps; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 331 | private UsageStatsManagerInternal mAppUsageStats; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 332 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 333 | private Archive mArchive; |
| 334 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 335 | // Persistent storage for notification policy |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 336 | private AtomicFile mPolicyFile; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 337 | |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 338 | private static final int DB_VERSION = 1; |
| 339 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 340 | private static final String TAG_NOTIFICATION_POLICY = "notification-policy"; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 341 | private static final String ATTR_VERSION = "version"; |
| 342 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 343 | private RankingHelper mRankingHelper; |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 344 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 345 | private final UserProfiles mUserProfiles = new UserProfiles(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 346 | private NotificationListeners mListeners; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 347 | private NotificationAssistants mAssistants; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 348 | private ConditionProviders mConditionProviders; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 349 | private NotificationUsageStats mUsageStats; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 350 | |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 351 | private static final int MY_UID = Process.myUid(); |
| 352 | private static final int MY_PID = Process.myPid(); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 353 | private static final IBinder WHITELIST_TOKEN = new Binder(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 354 | private RankingHandler mRankingHandler; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 355 | private long mLastOverRateLogTime; |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 356 | private float mMaxPackageEnqueueRate = DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE; |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 357 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 358 | private SnoozeHelper mSnoozeHelper; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 359 | private GroupHelper mGroupHelper; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 360 | private boolean mIsTelevision; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 361 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 362 | private static class Archive { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 363 | final int mBufferSize; |
| 364 | final ArrayDeque<StatusBarNotification> mBuffer; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 365 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 366 | public Archive(int size) { |
| 367 | mBufferSize = size; |
| 368 | mBuffer = new ArrayDeque<StatusBarNotification>(mBufferSize); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 369 | } |
Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 370 | |
Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 371 | public String toString() { |
| 372 | final StringBuilder sb = new StringBuilder(); |
| 373 | final int N = mBuffer.size(); |
| 374 | sb.append("Archive ("); |
| 375 | sb.append(N); |
| 376 | sb.append(" notification"); |
| 377 | sb.append((N==1)?")":"s)"); |
| 378 | return sb.toString(); |
| 379 | } |
| 380 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 381 | public void record(StatusBarNotification nr) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 382 | if (mBuffer.size() == mBufferSize) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 383 | mBuffer.removeFirst(); |
| 384 | } |
Daniel Sandler | 26b81d5 | 2013-05-20 20:56:43 -0400 | [diff] [blame] | 385 | |
| 386 | // We don't want to store the heavy bits of the notification in the archive, |
| 387 | // but other clients in the system process might be using the object, so we |
| 388 | // store a (lightened) copy. |
| 389 | mBuffer.addLast(nr.cloneLight()); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 390 | } |
| 391 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 392 | public Iterator<StatusBarNotification> descendingIterator() { |
| 393 | return mBuffer.descendingIterator(); |
| 394 | } |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 395 | |
| 396 | public StatusBarNotification[] getArray(int count) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 397 | if (count == 0) count = mBufferSize; |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 398 | final StatusBarNotification[] a |
| 399 | = new StatusBarNotification[Math.min(count, mBuffer.size())]; |
| 400 | Iterator<StatusBarNotification> iter = descendingIterator(); |
| 401 | int i=0; |
| 402 | while (iter.hasNext() && i < count) { |
| 403 | a[i++] = iter.next(); |
| 404 | } |
| 405 | return a; |
| 406 | } |
| 407 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 408 | } |
| 409 | |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 410 | protected void readDefaultApprovedServices(int userId) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 411 | String defaultListenerAccess = getContext().getResources().getString( |
| 412 | com.android.internal.R.string.config_defaultListenerAccessPackages); |
| 413 | if (defaultListenerAccess != null) { |
| 414 | for (String whitelisted : |
| 415 | defaultListenerAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR)) { |
| 416 | // Gather all notification listener components for candidate pkgs. |
| 417 | Set<ComponentName> approvedListeners = |
| 418 | mListeners.queryPackageForServices(whitelisted, |
| 419 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 420 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, userId); |
| 421 | for (ComponentName cn : approvedListeners) { |
| 422 | try { |
| 423 | getBinderService().setNotificationListenerAccessGrantedForUser(cn, |
| 424 | userId, true); |
| 425 | } catch (RemoteException e) { |
| 426 | e.printStackTrace(); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | String defaultDndAccess = getContext().getResources().getString( |
| 432 | com.android.internal.R.string.config_defaultDndAccessPackages); |
| 433 | if (defaultListenerAccess != null) { |
| 434 | for (String whitelisted : |
| 435 | defaultDndAccess.split(ManagedServices.ENABLED_SERVICES_SEPARATOR)) { |
| 436 | try { |
| 437 | getBinderService().setNotificationPolicyAccessGranted(whitelisted, true); |
| 438 | } catch (RemoteException e) { |
| 439 | e.printStackTrace(); |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 445 | void readPolicyXml(InputStream stream, boolean forRestore) |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 446 | throws XmlPullParserException, NumberFormatException, IOException { |
| 447 | final XmlPullParser parser = Xml.newPullParser(); |
| 448 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 449 | XmlUtils.beginDocument(parser, TAG_NOTIFICATION_POLICY); |
| 450 | boolean migratedManagedServices = false; |
| 451 | int outerDepth = parser.getDepth(); |
| 452 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 453 | if (ZenModeConfig.ZEN_TAG.equals(parser.getName())) { |
| 454 | mZenModeHelper.readXml(parser, forRestore); |
| 455 | } else if (RankingHelper.TAG_RANKING.equals(parser.getName())){ |
| 456 | mRankingHelper.readXml(parser, forRestore); |
| 457 | } |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 458 | // No non-system managed services are allowed on low ram devices |
| 459 | if (!ActivityManager.isLowRamDeviceStatic()) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 460 | if (mListeners.getConfig().xmlTag.equals(parser.getName())) { |
| 461 | mListeners.readXml(parser); |
| 462 | migratedManagedServices = true; |
| 463 | } else if (mAssistants.getConfig().xmlTag.equals(parser.getName())) { |
| 464 | mAssistants.readXml(parser); |
| 465 | migratedManagedServices = true; |
| 466 | } else if (mConditionProviders.getConfig().xmlTag.equals(parser.getName())) { |
| 467 | mConditionProviders.readXml(parser); |
| 468 | migratedManagedServices = true; |
| 469 | } |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 470 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 471 | } |
| 472 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 473 | if (!migratedManagedServices) { |
| 474 | mListeners.migrateToXml(); |
| 475 | mAssistants.migrateToXml(); |
| 476 | mConditionProviders.migrateToXml(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 477 | savePolicyFile(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 481 | private void loadPolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 482 | if (DBG) Slog.d(TAG, "loadPolicyFile"); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 483 | synchronized (mPolicyFile) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 484 | |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 485 | InputStream infile = null; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 486 | try { |
| 487 | infile = mPolicyFile.openRead(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 488 | readPolicyXml(infile, false /*forRestore*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 489 | } catch (FileNotFoundException e) { |
| 490 | // No data yet |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 491 | // Load default managed services approvals |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 492 | readDefaultApprovedServices(UserHandle.USER_SYSTEM); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 493 | } catch (IOException e) { |
| 494 | Log.wtf(TAG, "Unable to read notification policy", e); |
| 495 | } catch (NumberFormatException e) { |
| 496 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 497 | } catch (XmlPullParserException e) { |
| 498 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 499 | } finally { |
| 500 | IoUtils.closeQuietly(infile); |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | public void savePolicyFile() { |
| 506 | mHandler.removeMessages(MESSAGE_SAVE_POLICY_FILE); |
| 507 | mHandler.sendEmptyMessage(MESSAGE_SAVE_POLICY_FILE); |
| 508 | } |
| 509 | |
| 510 | private void handleSavePolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 511 | if (DBG) Slog.d(TAG, "handleSavePolicyFile"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 512 | synchronized (mPolicyFile) { |
| 513 | final FileOutputStream stream; |
| 514 | try { |
| 515 | stream = mPolicyFile.startWrite(); |
| 516 | } catch (IOException e) { |
| 517 | Slog.w(TAG, "Failed to save policy file", e); |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | try { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 522 | writePolicyXml(stream, false /*forBackup*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 523 | mPolicyFile.finishWrite(stream); |
| 524 | } catch (IOException e) { |
| 525 | Slog.w(TAG, "Failed to save policy file, restoring backup", e); |
| 526 | mPolicyFile.failWrite(stream); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 527 | } |
| 528 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 529 | BackupManager.dataChanged(getContext().getPackageName()); |
| 530 | } |
| 531 | |
| 532 | private void writePolicyXml(OutputStream stream, boolean forBackup) throws IOException { |
| 533 | final XmlSerializer out = new FastXmlSerializer(); |
| 534 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
| 535 | out.startDocument(null, true); |
| 536 | out.startTag(null, TAG_NOTIFICATION_POLICY); |
| 537 | out.attribute(null, ATTR_VERSION, Integer.toString(DB_VERSION)); |
| 538 | mZenModeHelper.writeXml(out, forBackup); |
| 539 | mRankingHelper.writeXml(out, forBackup); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 540 | mListeners.writeXml(out, forBackup); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 541 | mAssistants.writeXml(out, forBackup); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 542 | mConditionProviders.writeXml(out, forBackup); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 543 | out.endTag(null, TAG_NOTIFICATION_POLICY); |
| 544 | out.endDocument(); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 545 | } |
| 546 | |
Chris Wren | 66189fc | 2015-06-25 14:04:33 -0400 | [diff] [blame] | 547 | /** Use this to check if a package can post a notification or toast. */ |
| 548 | private boolean checkNotificationOp(String pkg, int uid) { |
| 549 | return mAppOps.checkOp(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg) |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 550 | == AppOpsManager.MODE_ALLOWED && !isPackageSuspendedForUser(pkg, uid); |
Chris Wren | 66189fc | 2015-06-25 14:04:33 -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, |
| 678 | String pkg, String tag, int id, int userId) { |
| 679 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 680 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 681 | true, userId, REASON_CANCEL, null); |
Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 682 | } |
| 683 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 684 | @Override |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 685 | public void onPanelRevealed(boolean clearEffects, int items) { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 686 | MetricsLogger.visible(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Chris Wren | 621933f | 2017-06-14 15:59:03 -0400 | [diff] [blame] | 687 | MetricsLogger.histogram(getContext(), "note_load", items); |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 688 | EventLogTags.writeNotificationPanelRevealed(items); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 689 | if (clearEffects) { |
| 690 | clearEffects(); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | @Override |
| 695 | public void onPanelHidden() { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 696 | MetricsLogger.hidden(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 697 | EventLogTags.writeNotificationPanelHidden(); |
| 698 | } |
| 699 | |
| 700 | @Override |
| 701 | public void clearEffects() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 702 | synchronized (mNotificationLock) { |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 703 | if (DBG) Slog.d(TAG, "clearEffects"); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 704 | clearSoundLocked(); |
| 705 | clearVibrateLocked(); |
| 706 | clearLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 707 | } |
| 708 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 709 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 710 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 711 | 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] | 712 | int uid, int initialPid, String message, int userId) { |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 713 | Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id |
| 714 | + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")"); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 715 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, 0, false, userId, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 716 | REASON_ERROR, null); |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 717 | long ident = Binder.clearCallingIdentity(); |
| 718 | try { |
Christopher Tate | 8aa8fe1 | 2017-01-20 17:50:32 -0800 | [diff] [blame] | 719 | ActivityManager.getService().crashApplication(uid, initialPid, pkg, -1, |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 720 | "Bad notification posted from package " + pkg |
| 721 | + ": " + message); |
| 722 | } catch (RemoteException e) { |
| 723 | } |
| 724 | Binder.restoreCallingIdentity(ident); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 725 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 726 | |
| 727 | @Override |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 728 | public void onNotificationVisibilityChanged(NotificationVisibility[] newlyVisibleKeys, |
| 729 | NotificationVisibility[] noLongerVisibleKeys) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 730 | synchronized (mNotificationLock) { |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 731 | for (NotificationVisibility nv : newlyVisibleKeys) { |
| 732 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 733 | if (r == null) continue; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 734 | r.setVisibility(true, nv.rank); |
| 735 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 736 | } |
| 737 | // Note that we might receive this event after notifications |
| 738 | // have already left the system, e.g. after dismissing from the |
| 739 | // shade. Hence not finding notifications in |
| 740 | // mNotificationsByKey is not an exceptional condition. |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 741 | for (NotificationVisibility nv : noLongerVisibleKeys) { |
| 742 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 743 | if (r == null) continue; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 744 | r.setVisibility(false, nv.rank); |
| 745 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 746 | } |
| 747 | } |
Christoph Studer | 92b389d | 2014-04-01 18:44:40 +0200 | [diff] [blame] | 748 | } |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 749 | |
| 750 | @Override |
| 751 | public void onNotificationExpansionChanged(String key, |
| 752 | boolean userAction, boolean expanded) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 753 | synchronized (mNotificationLock) { |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 754 | NotificationRecord r = mNotificationsByKey.get(key); |
| 755 | if (r != null) { |
| 756 | r.stats.onExpansionChanged(userAction, expanded); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 757 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 758 | MetricsLogger.action(r.getLogMaker(now) |
| 759 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
| 760 | .setType(MetricsEvent.TYPE_DETAIL)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 761 | EventLogTags.writeNotificationExpansion(key, |
| 762 | userAction ? 1 : 0, expanded ? 1 : 0, |
| 763 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 767 | }; |
| 768 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 769 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 770 | private void clearSoundLocked() { |
| 771 | mSoundNotificationKey = null; |
| 772 | long identity = Binder.clearCallingIdentity(); |
| 773 | try { |
| 774 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 775 | if (player != null) { |
| 776 | player.stopAsync(); |
| 777 | } |
| 778 | } catch (RemoteException e) { |
| 779 | } finally { |
| 780 | Binder.restoreCallingIdentity(identity); |
| 781 | } |
| 782 | } |
| 783 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 784 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 785 | private void clearVibrateLocked() { |
| 786 | mVibrateNotificationKey = null; |
| 787 | long identity = Binder.clearCallingIdentity(); |
| 788 | try { |
| 789 | mVibrator.cancel(); |
| 790 | } finally { |
| 791 | Binder.restoreCallingIdentity(identity); |
| 792 | } |
| 793 | } |
| 794 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 795 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 796 | private void clearLightsLocked() { |
| 797 | // light |
| 798 | mLights.clear(); |
| 799 | updateLightsLocked(); |
| 800 | } |
| 801 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 802 | protected final BroadcastReceiver mLocaleChangeReceiver = new BroadcastReceiver() { |
| 803 | @Override |
| 804 | public void onReceive(Context context, Intent intent) { |
| 805 | if (Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) { |
| 806 | mZenModeHelper.updateDefaultZenRules(); |
Julia Reynolds | 816797a | 2017-08-11 15:47:09 -0400 | [diff] [blame] | 807 | mRankingHelper.onLocaleChanged(context, ActivityManager.getCurrentUser()); |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | }; |
| 811 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 812 | private final BroadcastReceiver mRestoreReceiver = new BroadcastReceiver() { |
| 813 | @Override |
| 814 | public void onReceive(Context context, Intent intent) { |
| 815 | if (Intent.ACTION_SETTING_RESTORED.equals(intent.getAction())) { |
| 816 | try { |
| 817 | String element = intent.getStringExtra(Intent.EXTRA_SETTING_NAME); |
| 818 | String newValue = intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE); |
Michal Karpinski | 6135a26 | 2017-08-11 10:45:58 +0100 | [diff] [blame] | 819 | int restoredFromSdkInt = intent.getIntExtra( |
| 820 | Intent.EXTRA_SETTING_RESTORED_FROM_SDK_INT, 0); |
Julia Reynolds | fa206a4 | 2017-08-14 13:22:23 -0400 | [diff] [blame] | 821 | mListeners.onSettingRestored( |
| 822 | element, newValue, restoredFromSdkInt, getSendingUserId()); |
| 823 | mConditionProviders.onSettingRestored( |
| 824 | element, newValue, restoredFromSdkInt, getSendingUserId()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 825 | } catch (Exception e) { |
| 826 | Slog.wtf(TAG, "Cannot restore managed services from settings", e); |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | }; |
| 831 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 832 | private final BroadcastReceiver mNotificationTimeoutReceiver = new BroadcastReceiver() { |
| 833 | @Override |
| 834 | public void onReceive(Context context, Intent intent) { |
| 835 | String action = intent.getAction(); |
| 836 | if (action == null) { |
| 837 | return; |
| 838 | } |
| 839 | if (ACTION_NOTIFICATION_TIMEOUT.equals(action)) { |
| 840 | final NotificationRecord record; |
| 841 | synchronized (mNotificationLock) { |
| 842 | record = findNotificationByKeyLocked(intent.getStringExtra(EXTRA_KEY)); |
| 843 | } |
| 844 | if (record != null) { |
| 845 | cancelNotification(record.sbn.getUid(), record.sbn.getInitialPid(), |
| 846 | record.sbn.getPackageName(), record.sbn.getTag(), |
| 847 | record.sbn.getId(), 0, |
| 848 | Notification.FLAG_FOREGROUND_SERVICE, true, record.getUserId(), |
| 849 | REASON_TIMEOUT, null); |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | }; |
| 854 | |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 855 | private final BroadcastReceiver mPackageIntentReceiver = new BroadcastReceiver() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | @Override |
| 857 | public void onReceive(Context context, Intent intent) { |
| 858 | String action = intent.getAction(); |
Dianne Hackborn | 29cd7f1 | 2015-01-08 10:37:05 -0800 | [diff] [blame] | 859 | if (action == null) { |
| 860 | return; |
| 861 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 863 | boolean queryRestart = false; |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 864 | boolean queryRemove = false; |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 865 | boolean packageChanged = false; |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 866 | boolean cancelNotifications = true; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 867 | int reason = REASON_PACKAGE_CHANGED; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 868 | |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 869 | if (action.equals(Intent.ACTION_PACKAGE_ADDED) |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 870 | || (queryRemove=action.equals(Intent.ACTION_PACKAGE_REMOVED)) |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 871 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 872 | || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED)) |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 873 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 874 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE) |
| 875 | || action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 876 | int changeUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 877 | UserHandle.USER_ALL); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 878 | String pkgList[] = null; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 879 | int uidList[] = null; |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 880 | boolean removingPackage = queryRemove && |
| 881 | !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 882 | if (DBG) Slog.i(TAG, "action=" + action + " removing=" + removingPackage); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 883 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 884 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 885 | uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 886 | } else if (action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
| 887 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 888 | reason = REASON_PACKAGE_SUSPENDED; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 889 | } else if (queryRestart) { |
| 890 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 891 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 892 | } else { |
| 893 | Uri uri = intent.getData(); |
| 894 | if (uri == null) { |
| 895 | return; |
| 896 | } |
| 897 | String pkgName = uri.getSchemeSpecificPart(); |
| 898 | if (pkgName == null) { |
| 899 | return; |
| 900 | } |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 901 | if (packageChanged) { |
| 902 | // We cancel notifications for packages which have just been disabled |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 903 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 904 | final int enabled = mPackageManager.getApplicationEnabledSetting( |
| 905 | pkgName, |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 906 | changeUserId != UserHandle.USER_ALL ? changeUserId : |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 907 | UserHandle.USER_SYSTEM); |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 908 | if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 909 | || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 910 | cancelNotifications = false; |
| 911 | } |
| 912 | } catch (IllegalArgumentException e) { |
| 913 | // Package doesn't exist; probably racing with uninstall. |
| 914 | // cancelNotifications is already true, so nothing to do here. |
| 915 | if (DBG) { |
| 916 | Slog.i(TAG, "Exception trying to look up app enabled setting", e); |
| 917 | } |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 918 | } catch (RemoteException e) { |
| 919 | // Failed to talk to PackageManagerService Should never happen! |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 920 | } |
| 921 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 922 | pkgList = new String[]{pkgName}; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 923 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 925 | if (pkgList != null && (pkgList.length > 0)) { |
| 926 | for (String pkgName : pkgList) { |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 927 | if (cancelNotifications) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 928 | cancelAllNotificationsInt(MY_UID, MY_PID, pkgName, null, 0, 0, |
| 929 | !queryRestart, changeUserId, reason, null); |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 930 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 931 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 933 | mListeners.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 934 | mAssistants.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 935 | mConditionProviders.onPackagesChanged(removingPackage, pkgList, uidList); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 936 | mRankingHelper.onPackagesChanged(removingPackage, changeUserId, pkgList, uidList); |
| 937 | savePolicyFile(); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | }; |
| 941 | |
| 942 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 943 | @Override |
| 944 | public void onReceive(Context context, Intent intent) { |
| 945 | String action = intent.getAction(); |
| 946 | |
| 947 | if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 948 | // Keep track of screen on/off state, but do not turn off the notification light |
| 949 | // until user passes through the lock screen or views the notification. |
| 950 | mScreenOn = true; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 951 | updateNotificationPulse(); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 952 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 953 | mScreenOn = false; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 954 | updateNotificationPulse(); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 955 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
John Spurlock | 5d2eeb1 | 2014-01-16 10:46:36 -0500 | [diff] [blame] | 956 | mInCall = TelephonyManager.EXTRA_STATE_OFFHOOK |
| 957 | .equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE)); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 958 | updateNotificationPulse(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 959 | } else if (action.equals(Intent.ACTION_USER_STOPPED)) { |
| 960 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 961 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 962 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 963 | REASON_USER_STOPPED, null); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 964 | } |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 965 | } else if (action.equals(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) { |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 966 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 967 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 968 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 969 | REASON_PROFILE_TURNED_OFF, null); |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 970 | } |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 971 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { |
| 972 | // turn off LED when user passes through lock screen |
| 973 | mNotificationLight.turnOff(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 974 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 975 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 976 | // reload per-user settings |
| 977 | mSettingsObserver.update(null); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 978 | mUserProfiles.updateCache(context); |
Christoph Studer | b53dfd4 | 2014-09-12 14:45:59 +0200 | [diff] [blame] | 979 | // Refresh managed services |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 980 | mConditionProviders.onUserSwitched(user); |
| 981 | mListeners.onUserSwitched(user); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 982 | mAssistants.onUserSwitched(user); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 983 | mZenModeHelper.onUserSwitched(user); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 984 | } else if (action.equals(Intent.ACTION_USER_ADDED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 985 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
| 986 | if (userId != USER_NULL) { |
| 987 | mUserProfiles.updateCache(context); |
| 988 | readDefaultApprovedServices(userId); |
| 989 | } |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 990 | } else if (action.equals(Intent.ACTION_USER_REMOVED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 991 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 992 | mZenModeHelper.onUserRemoved(user); |
Julia Reynolds | 2e9bf5f | 2017-05-03 13:23:30 -0400 | [diff] [blame] | 993 | mRankingHelper.onUserRemoved(user); |
| 994 | savePolicyFile(); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 995 | } else if (action.equals(Intent.ACTION_USER_UNLOCKED)) { |
Julia Reynolds | 88a879f | 2017-07-26 17:06:46 -0400 | [diff] [blame] | 996 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 997 | mConditionProviders.onUserUnlocked(user); |
| 998 | mListeners.onUserUnlocked(user); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 999 | mAssistants.onUserUnlocked(user); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1000 | mZenModeHelper.onUserUnlocked(user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | } |
| 1002 | } |
| 1003 | }; |
| 1004 | |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 1005 | private final class SettingsObserver extends ContentObserver { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1006 | private final Uri NOTIFICATION_BADGING_URI |
| 1007 | = Settings.Secure.getUriFor(Settings.Secure.NOTIFICATION_BADGING); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1008 | private final Uri NOTIFICATION_LIGHT_PULSE_URI |
| 1009 | = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1010 | private final Uri NOTIFICATION_RATE_LIMIT_URI |
| 1011 | = Settings.Global.getUriFor(Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1012 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1013 | SettingsObserver(Handler handler) { |
| 1014 | super(handler); |
| 1015 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1016 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1017 | void observe() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1018 | ContentResolver resolver = getContext().getContentResolver(); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1019 | resolver.registerContentObserver(NOTIFICATION_BADGING_URI, |
| 1020 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1021 | resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1022 | false, this, UserHandle.USER_ALL); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1023 | resolver.registerContentObserver(NOTIFICATION_RATE_LIMIT_URI, |
| 1024 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1025 | update(null); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1028 | @Override public void onChange(boolean selfChange, Uri uri) { |
| 1029 | update(uri); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1032 | public void update(Uri uri) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1033 | ContentResolver resolver = getContext().getContentResolver(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1034 | if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { |
zhoulei | 7e37697 | 2017-05-17 18:41:25 +0800 | [diff] [blame] | 1035 | boolean pulseEnabled = Settings.System.getIntForUser(resolver, |
| 1036 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0, UserHandle.USER_CURRENT) != 0; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1037 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 1038 | mNotificationPulseEnabled = pulseEnabled; |
| 1039 | updateNotificationPulse(); |
| 1040 | } |
| 1041 | } |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1042 | if (uri == null || NOTIFICATION_RATE_LIMIT_URI.equals(uri)) { |
| 1043 | mMaxPackageEnqueueRate = Settings.Global.getFloat(resolver, |
| 1044 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, mMaxPackageEnqueueRate); |
| 1045 | } |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 1046 | if (uri == null || NOTIFICATION_BADGING_URI.equals(uri)) { |
| 1047 | mRankingHelper.updateBadgingEnabled(); |
| 1048 | } |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1049 | } |
| 1050 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1051 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1052 | private SettingsObserver mSettingsObserver; |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1053 | protected ZenModeHelper mZenModeHelper; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1054 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1055 | static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) { |
| 1056 | int[] ar = r.getIntArray(resid); |
| 1057 | if (ar == null) { |
| 1058 | return def; |
| 1059 | } |
| 1060 | final int len = ar.length > maxlen ? maxlen : ar.length; |
| 1061 | long[] out = new long[len]; |
| 1062 | for (int i=0; i<len; i++) { |
| 1063 | out[i] = ar[i]; |
| 1064 | } |
| 1065 | return out; |
| 1066 | } |
| 1067 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 1068 | public NotificationManagerService(Context context) { |
| 1069 | super(context); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 1070 | Notification.processWhitelistToken = WHITELIST_TOKEN; |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 1071 | } |
| 1072 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1073 | // TODO - replace these methods with a single VisibleForTesting constructor |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1074 | @VisibleForTesting |
| 1075 | void setAudioManager(AudioManager audioMananger) { |
| 1076 | mAudioManager = audioMananger; |
| 1077 | } |
| 1078 | |
| 1079 | @VisibleForTesting |
| 1080 | void setVibrator(Vibrator vibrator) { |
| 1081 | mVibrator = vibrator; |
| 1082 | } |
| 1083 | |
| 1084 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1085 | void setLights(Light light) { |
| 1086 | mNotificationLight = light; |
| 1087 | mAttentionLight = light; |
Julia Reynolds | 033a412 | 2017-01-31 16:50:38 -0500 | [diff] [blame] | 1088 | mNotificationPulseEnabled = true; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | @VisibleForTesting |
| 1092 | void setScreenOn(boolean on) { |
| 1093 | mScreenOn = on; |
| 1094 | } |
| 1095 | |
| 1096 | @VisibleForTesting |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1097 | int getNotificationRecordCount() { |
| 1098 | synchronized (mNotificationLock) { |
| 1099 | int count = mNotificationList.size() + mNotificationsByKey.size() |
| 1100 | + mSummaryByGroupKey.size() + mEnqueuedNotifications.size(); |
| 1101 | // subtract duplicates |
| 1102 | for (NotificationRecord posted : mNotificationList) { |
| 1103 | if (mNotificationsByKey.containsKey(posted.getKey())) { |
| 1104 | count--; |
| 1105 | } |
| 1106 | if (posted.sbn.isGroup() && posted.getNotification().isGroupSummary()) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1107 | count--; |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | return count; |
| 1112 | } |
| 1113 | } |
| 1114 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1115 | void clearNotifications() { |
| 1116 | mEnqueuedNotifications.clear(); |
| 1117 | mNotificationList.clear(); |
| 1118 | mNotificationsByKey.clear(); |
| 1119 | mSummaryByGroupKey.clear(); |
| 1120 | } |
| 1121 | |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1122 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1123 | void addNotification(NotificationRecord r) { |
| 1124 | mNotificationList.add(r); |
| 1125 | mNotificationsByKey.put(r.sbn.getKey(), r); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1126 | if (r.sbn.isGroup()) { |
| 1127 | mSummaryByGroupKey.put(r.getGroupKey(), r); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | @VisibleForTesting |
| 1132 | void addEnqueuedNotification(NotificationRecord r) { |
| 1133 | mEnqueuedNotifications.add(r); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | @VisibleForTesting |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1137 | void setSystemReady(boolean systemReady) { |
| 1138 | mSystemReady = systemReady; |
| 1139 | } |
| 1140 | |
| 1141 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1142 | void setHandler(WorkerHandler handler) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1143 | mHandler = handler; |
| 1144 | } |
| 1145 | |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1146 | @VisibleForTesting |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 1147 | void setFallbackVibrationPattern(long[] vibrationPattern) { |
| 1148 | mFallbackVibrationPattern = vibrationPattern; |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1149 | } |
| 1150 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1151 | @VisibleForTesting |
| 1152 | void setPackageManager(IPackageManager packageManager) { |
| 1153 | mPackageManager = packageManager; |
| 1154 | } |
| 1155 | |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1156 | @VisibleForTesting |
| 1157 | void setRankingHelper(RankingHelper rankingHelper) { |
| 1158 | mRankingHelper = rankingHelper; |
| 1159 | } |
| 1160 | |
| 1161 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1162 | void setRankingHandler(RankingHandler rankingHandler) { |
| 1163 | mRankingHandler = rankingHandler; |
| 1164 | } |
| 1165 | |
| 1166 | @VisibleForTesting |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1167 | void setIsTelevision(boolean isTelevision) { |
| 1168 | mIsTelevision = isTelevision; |
| 1169 | } |
| 1170 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 1171 | @VisibleForTesting |
| 1172 | void setUsageStats(NotificationUsageStats us) { |
| 1173 | mUsageStats = us; |
| 1174 | } |
| 1175 | |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1176 | // 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] | 1177 | @VisibleForTesting |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1178 | void init(Looper looper, IPackageManager packageManager, |
| 1179 | PackageManager packageManagerClient, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1180 | LightsManager lightsManager, NotificationListeners notificationListeners, |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1181 | NotificationAssistants notificationAssistants, ConditionProviders conditionProviders, |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1182 | ICompanionDeviceManager companionManager, SnoozeHelper snoozeHelper, |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1183 | NotificationUsageStats usageStats, AtomicFile policyFile, |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1184 | ActivityManager activityManager, GroupHelper groupHelper) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1185 | Resources resources = getContext().getResources(); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1186 | mMaxPackageEnqueueRate = Settings.Global.getFloat(getContext().getContentResolver(), |
| 1187 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, |
| 1188 | DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE); |
| 1189 | |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1190 | mAm = ActivityManager.getService(); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1191 | mPackageManager = packageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1192 | mPackageManagerClient = packageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1193 | mAppOps = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE); |
| 1194 | mVibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 1195 | mAppUsageStats = LocalServices.getService(UsageStatsManagerInternal.class); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1196 | mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1197 | mCompanionManager = companionManager; |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 1198 | mActivityManager = activityManager; |
San Mehat | 3ee1317 | 2010-02-04 20:54:43 -0800 | [diff] [blame] | 1199 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1200 | mHandler = new WorkerHandler(looper); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1201 | mRankingThread.start(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1202 | String[] extractorNames; |
| 1203 | try { |
| 1204 | extractorNames = resources.getStringArray(R.array.config_notificationSignalExtractors); |
| 1205 | } catch (Resources.NotFoundException e) { |
| 1206 | extractorNames = new String[0]; |
| 1207 | } |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1208 | mUsageStats = usageStats; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1209 | mRankingHandler = new RankingHandlerWorker(mRankingThread.getLooper()); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1210 | mRankingHelper = new RankingHelper(getContext(), |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 1211 | getContext().getPackageManager(), |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1212 | mRankingHandler, |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 1213 | mUsageStats, |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1214 | extractorNames); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1215 | mConditionProviders = conditionProviders; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1216 | mZenModeHelper = new ZenModeHelper(getContext(), mHandler.getLooper(), mConditionProviders); |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 1217 | mZenModeHelper.addCallback(new ZenModeHelper.Callback() { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1218 | @Override |
| 1219 | public void onConfigChanged() { |
| 1220 | savePolicyFile(); |
| 1221 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1222 | |
| 1223 | @Override |
| 1224 | void onZenModeChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1225 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED); |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1226 | getContext().sendBroadcastAsUser( |
Jason Monk | 6350674 | 2015-12-16 12:06:51 -0500 | [diff] [blame] | 1227 | new Intent(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL) |
| 1228 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT), |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1229 | UserHandle.ALL, android.Manifest.permission.MANAGE_NOTIFICATIONS); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1230 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1231 | updateInterruptionFilterLocked(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1232 | } |
| 1233 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1234 | |
| 1235 | @Override |
| 1236 | void onPolicyChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1237 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED); |
| 1238 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1239 | }); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1240 | mSnoozeHelper = snoozeHelper; |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1241 | mGroupHelper = groupHelper; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 1242 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1243 | // This is a ManagedServices object that keeps track of the listeners. |
| 1244 | mListeners = notificationListeners; |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1245 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 1246 | // This is a MangedServices object that keeps track of the assistant. |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1247 | mAssistants = notificationAssistants; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1248 | |
| 1249 | mPolicyFile = policyFile; |
| 1250 | loadPolicyFile(); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1251 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1252 | mStatusBar = getLocalService(StatusBarManagerInternal.class); |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 1253 | if (mStatusBar != null) { |
| 1254 | mStatusBar.setNotificationDelegate(mNotificationDelegate); |
| 1255 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1256 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1257 | mNotificationLight = lightsManager.getLight(LightsManager.LIGHT_ID_NOTIFICATIONS); |
| 1258 | mAttentionLight = lightsManager.getLight(LightsManager.LIGHT_ID_ATTENTION); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1259 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1260 | mFallbackVibrationPattern = getLongArray(resources, |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 1261 | R.array.config_notificationFallbackVibePattern, |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1262 | VIBRATE_PATTERN_MAXLEN, |
| 1263 | DEFAULT_VIBRATE_PATTERN); |
| 1264 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 1265 | mInCallNotificationUri = Uri.parse("file://" + |
| 1266 | resources.getString(R.string.config_inCallNotificationSound)); |
| 1267 | mInCallNotificationAudioAttributes = new AudioAttributes.Builder() |
| 1268 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 1269 | .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION) |
| 1270 | .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED) |
| 1271 | .build(); |
| 1272 | mInCallNotificationVolume = resources.getFloat(R.dimen.config_inCallNotificationVolume); |
| 1273 | |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 1274 | mUseAttentionLight = resources.getBoolean(R.bool.config_useAttentionLight); |
| 1275 | |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1276 | // Don't start allowing notifications until the setup wizard has run once. |
| 1277 | // After that, including subsequent boots, init with notifications turned on. |
| 1278 | // This works on the first boot because the setup wizard will toggle this |
| 1279 | // 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] | 1280 | if (0 == Settings.Global.getInt(getContext().getContentResolver(), |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1281 | Settings.Global.DEVICE_PROVISIONED, 0)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1282 | mDisableNotificationEffects = true; |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1283 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1284 | mZenModeHelper.initZenMode(); |
John Spurlock | f370177 | 2015-02-12 13:29:37 -0500 | [diff] [blame] | 1285 | mInterruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1286 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1287 | mUserProfiles.updateCache(getContext()); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 1288 | listenForCallState(); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1289 | |
Geoffrey Pitsch | afc0072 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1290 | mSettingsObserver = new SettingsObserver(mHandler); |
| 1291 | |
| 1292 | mArchive = new Archive(resources.getInteger( |
| 1293 | R.integer.config_notificationServiceArchiveSize)); |
| 1294 | |
| 1295 | mIsTelevision = mPackageManagerClient.hasSystemFeature(FEATURE_LEANBACK) |
| 1296 | || mPackageManagerClient.hasSystemFeature(FEATURE_TELEVISION); |
| 1297 | } |
| 1298 | |
| 1299 | @Override |
| 1300 | public void onStart() { |
| 1301 | SnoozeHelper snoozeHelper = new SnoozeHelper(getContext(), new SnoozeHelper.Callback() { |
| 1302 | @Override |
| 1303 | public void repost(int userId, NotificationRecord r) { |
| 1304 | try { |
| 1305 | if (DBG) { |
| 1306 | Slog.d(TAG, "Reposting " + r.getKey()); |
| 1307 | } |
| 1308 | enqueueNotificationInternal(r.sbn.getPackageName(), r.sbn.getOpPkg(), |
| 1309 | r.sbn.getUid(), r.sbn.getInitialPid(), r.sbn.getTag(), r.sbn.getId(), |
| 1310 | r.sbn.getNotification(), userId); |
| 1311 | } catch (Exception e) { |
| 1312 | Slog.e(TAG, "Cannot un-snooze notification", e); |
| 1313 | } |
| 1314 | } |
| 1315 | }, mUserProfiles); |
| 1316 | |
| 1317 | final File systemDir = new File(Environment.getDataDirectory(), "system"); |
| 1318 | |
| 1319 | init(Looper.myLooper(), |
| 1320 | AppGlobals.getPackageManager(), getContext().getPackageManager(), |
| 1321 | getLocalService(LightsManager.class), |
| 1322 | new NotificationListeners(AppGlobals.getPackageManager()), |
| 1323 | new NotificationAssistants(AppGlobals.getPackageManager()), |
| 1324 | new ConditionProviders(getContext(), mUserProfiles, AppGlobals.getPackageManager()), |
| 1325 | null, snoozeHelper, new NotificationUsageStats(getContext()), |
| 1326 | new AtomicFile(new File(systemDir, "notification_policy.xml")), |
| 1327 | (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE), |
| 1328 | getGroupHelper()); |
| 1329 | |
Mike Lockwood | 35e16bf | 2010-11-30 19:53:36 -0500 | [diff] [blame] | 1330 | // register for various Intents |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1331 | IntentFilter filter = new IntentFilter(); |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1332 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 1333 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1334 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1335 | filter.addAction(Intent.ACTION_USER_PRESENT); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1336 | filter.addAction(Intent.ACTION_USER_STOPPED); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1337 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1338 | filter.addAction(Intent.ACTION_USER_ADDED); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1339 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1340 | filter.addAction(Intent.ACTION_USER_UNLOCKED); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1341 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1342 | getContext().registerReceiver(mIntentReceiver, filter); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1343 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1344 | IntentFilter pkgFilter = new IntentFilter(); |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1345 | pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1346 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
Daniel Sandler | aac0eb0 | 2011-08-06 22:51:56 -0400 | [diff] [blame] | 1347 | pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1348 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 1349 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 1350 | pkgFilter.addDataScheme("package"); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1351 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, pkgFilter, null, |
| 1352 | null); |
| 1353 | |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1354 | IntentFilter suspendedPkgFilter = new IntentFilter(); |
| 1355 | suspendedPkgFilter.addAction(Intent.ACTION_PACKAGES_SUSPENDED); |
| 1356 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, |
| 1357 | suspendedPkgFilter, null, null); |
| 1358 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1359 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1360 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, sdFilter, null, |
| 1361 | null); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1362 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1363 | IntentFilter timeoutFilter = new IntentFilter(ACTION_NOTIFICATION_TIMEOUT); |
| 1364 | timeoutFilter.addDataScheme(SCHEME_TIMEOUT); |
| 1365 | getContext().registerReceiver(mNotificationTimeoutReceiver, timeoutFilter); |
| 1366 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1367 | IntentFilter settingsRestoredFilter = new IntentFilter(Intent.ACTION_SETTING_RESTORED); |
| 1368 | getContext().registerReceiver(mRestoreReceiver, settingsRestoredFilter); |
| 1369 | |
Beverly | d4f9649 | 2017-08-02 13:36:11 -0400 | [diff] [blame] | 1370 | IntentFilter localeChangedFilter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED); |
| 1371 | getContext().registerReceiver(mLocaleChangeReceiver, localeChangedFilter); |
| 1372 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1373 | publishBinderService(Context.NOTIFICATION_SERVICE, mService); |
| 1374 | publishLocalService(NotificationManagerInternal.class, mInternalService); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1375 | } |
| 1376 | |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1377 | private GroupHelper getGroupHelper() { |
| 1378 | return new GroupHelper(new GroupHelper.Callback() { |
| 1379 | @Override |
| 1380 | public void addAutoGroup(String key) { |
| 1381 | synchronized (mNotificationLock) { |
| 1382 | addAutogroupKeyLocked(key); |
| 1383 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | @Override |
| 1387 | public void removeAutoGroup(String key) { |
| 1388 | synchronized (mNotificationLock) { |
| 1389 | removeAutogroupKeyLocked(key); |
| 1390 | } |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | @Override |
| 1394 | public void addAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 1395 | createAutoGroupSummary(userId, pkg, triggeringKey); |
| 1396 | } |
| 1397 | |
| 1398 | @Override |
| 1399 | public void removeAutoGroupSummary(int userId, String pkg) { |
| 1400 | synchronized (mNotificationLock) { |
| 1401 | clearAutogroupSummaryLocked(userId, pkg); |
| 1402 | } |
| 1403 | } |
| 1404 | }); |
| 1405 | } |
| 1406 | |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1407 | private void sendRegisteredOnlyBroadcast(String action) { |
| 1408 | getContext().sendBroadcastAsUser(new Intent(action) |
| 1409 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), UserHandle.ALL, null); |
| 1410 | } |
| 1411 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1412 | @Override |
| 1413 | public void onBootPhase(int phase) { |
| 1414 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 1415 | // no beeping until we're basically done booting |
| 1416 | mSystemReady = true; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1417 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1418 | // Grab our optional AudioService |
| 1419 | mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1420 | mAudioManagerInternal = getLocalService(AudioManagerInternal.class); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1421 | mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1422 | mZenModeHelper.onSystemReady(); |
Adam Lesinski | a6db4ab | 2014-03-24 12:31:45 -0700 | [diff] [blame] | 1423 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 1424 | // This observer will force an update when observe is called, causing us to |
| 1425 | // bind to listener services. |
| 1426 | mSettingsObserver.observe(); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1427 | mListeners.onBootPhaseAppsCanStart(); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1428 | mAssistants.onBootPhaseAppsCanStart(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1429 | mConditionProviders.onBootPhaseAppsCanStart(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1433 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1434 | private void updateListenerHintsLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1435 | final int hints = calculateHints(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1436 | if (hints == mListenerHints) return; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1437 | ZenLog.traceListenerHintsChanged(mListenerHints, hints, mEffectsSuppressors.size()); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1438 | mListenerHints = hints; |
| 1439 | scheduleListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 1440 | } |
| 1441 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1442 | @GuardedBy("mNotificationLock") |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1443 | private void updateEffectsSuppressorLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1444 | final long updatedSuppressedEffects = calculateSuppressedEffects(); |
| 1445 | if (updatedSuppressedEffects == mZenModeHelper.getSuppressedEffects()) return; |
| 1446 | final List<ComponentName> suppressors = getSuppressors(); |
| 1447 | ZenLog.traceEffectsSuppressorChanged(mEffectsSuppressors, suppressors, updatedSuppressedEffects); |
| 1448 | mEffectsSuppressors = suppressors; |
| 1449 | mZenModeHelper.setSuppressedEffects(updatedSuppressedEffects); |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1450 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1451 | } |
| 1452 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1453 | private void updateNotificationChannelInt(String pkg, int uid, NotificationChannel channel, |
| 1454 | boolean fromListener) { |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1455 | if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) { |
| 1456 | // cancel |
| 1457 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channel.getId(), 0, 0, true, |
| 1458 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, |
| 1459 | null); |
| 1460 | } |
Julia Reynolds | 6031533 | 2017-04-04 14:29:07 -0400 | [diff] [blame] | 1461 | mRankingHelper.updateNotificationChannel(pkg, uid, channel); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1462 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1463 | if (!fromListener) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1464 | final NotificationChannel modifiedChannel = |
| 1465 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), false); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1466 | mListeners.notifyNotificationChannelChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1467 | pkg, UserHandle.getUserHandleForUid(uid), |
| 1468 | modifiedChannel, NOTIFICATION_CHANNEL_OR_GROUP_UPDATED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1469 | } |
| 1470 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1471 | savePolicyFile(); |
| 1472 | } |
| 1473 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1474 | private ArrayList<ComponentName> getSuppressors() { |
| 1475 | ArrayList<ComponentName> names = new ArrayList<ComponentName>(); |
| 1476 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1477 | ArraySet<ManagedServiceInfo> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
| 1478 | |
| 1479 | for (ManagedServiceInfo info : serviceInfoList) { |
| 1480 | names.add(info.component); |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | return names; |
| 1485 | } |
| 1486 | |
| 1487 | private boolean removeDisabledHints(ManagedServiceInfo info) { |
| 1488 | return removeDisabledHints(info, 0); |
| 1489 | } |
| 1490 | |
| 1491 | private boolean removeDisabledHints(ManagedServiceInfo info, int hints) { |
| 1492 | boolean removed = false; |
| 1493 | |
| 1494 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1495 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 1496 | final ArraySet<ManagedServiceInfo> listeners = |
| 1497 | mListenersDisablingEffects.valueAt(i); |
| 1498 | |
| 1499 | if (hints == 0 || (hint & hints) == hint) { |
| 1500 | removed = removed || listeners.remove(info); |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | return removed; |
| 1505 | } |
| 1506 | |
| 1507 | private void addDisabledHints(ManagedServiceInfo info, int hints) { |
| 1508 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1509 | addDisabledHint(info, HINT_HOST_DISABLE_EFFECTS); |
| 1510 | } |
| 1511 | |
| 1512 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1513 | addDisabledHint(info, HINT_HOST_DISABLE_NOTIFICATION_EFFECTS); |
| 1514 | } |
| 1515 | |
| 1516 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1517 | addDisabledHint(info, HINT_HOST_DISABLE_CALL_EFFECTS); |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | private void addDisabledHint(ManagedServiceInfo info, int hint) { |
| 1522 | if (mListenersDisablingEffects.indexOfKey(hint) < 0) { |
| 1523 | mListenersDisablingEffects.put(hint, new ArraySet<ManagedServiceInfo>()); |
| 1524 | } |
| 1525 | |
| 1526 | ArraySet<ManagedServiceInfo> hintListeners = mListenersDisablingEffects.get(hint); |
| 1527 | hintListeners.add(info); |
| 1528 | } |
| 1529 | |
| 1530 | private int calculateHints() { |
| 1531 | int hints = 0; |
| 1532 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1533 | int hint = mListenersDisablingEffects.keyAt(i); |
| 1534 | ArraySet<ManagedServiceInfo> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
| 1535 | |
| 1536 | if (!serviceInfoList.isEmpty()) { |
| 1537 | hints |= hint; |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | return hints; |
| 1542 | } |
| 1543 | |
| 1544 | private long calculateSuppressedEffects() { |
| 1545 | int hints = calculateHints(); |
| 1546 | long suppressedEffects = 0; |
| 1547 | |
| 1548 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1549 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_ALL; |
| 1550 | } |
| 1551 | |
| 1552 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1553 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_NOTIFICATIONS; |
| 1554 | } |
| 1555 | |
| 1556 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1557 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_CALLS; |
| 1558 | } |
| 1559 | |
| 1560 | return suppressedEffects; |
| 1561 | } |
| 1562 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1563 | @GuardedBy("mNotificationLock") |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1564 | private void updateInterruptionFilterLocked() { |
| 1565 | int interruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
| 1566 | if (interruptionFilter == mInterruptionFilter) return; |
| 1567 | mInterruptionFilter = interruptionFilter; |
| 1568 | scheduleInterruptionFilterChanged(interruptionFilter); |
| 1569 | } |
| 1570 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1571 | @VisibleForTesting |
| 1572 | INotificationManager getBinderService() { |
| 1573 | return INotificationManager.Stub.asInterface(mService); |
| 1574 | } |
| 1575 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1576 | @VisibleForTesting |
| 1577 | NotificationManagerInternal getInternalService() { |
| 1578 | return mInternalService; |
| 1579 | } |
| 1580 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1581 | private final IBinder mService = new INotificationManager.Stub() { |
| 1582 | // Toasts |
| 1583 | // ============================================================================ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1584 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1585 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1586 | public void enqueueToast(String pkg, ITransientNotification callback, int duration) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1587 | { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1588 | if (DBG) { |
| 1589 | Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback |
| 1590 | + " duration=" + duration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1591 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1592 | |
| 1593 | if (pkg == null || callback == null) { |
| 1594 | Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback); |
| 1595 | return ; |
| 1596 | } |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1597 | final boolean isSystemToast = isCallerSystemOrPhone() || ("android".equals(pkg)); |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 1598 | final boolean isPackageSuspended = |
| 1599 | isPackageSuspendedForUser(pkg, Binder.getCallingUid()); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1600 | |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 1601 | if (ENABLE_BLOCKED_TOASTS && !isSystemToast && |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1602 | (!areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()) |
| 1603 | || isPackageSuspended)) { |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 1604 | Slog.e(TAG, "Suppressing toast from package " + pkg |
| 1605 | + (isPackageSuspended |
| 1606 | ? " due to package suspended by administrator." |
| 1607 | : " by user request.")); |
| 1608 | return; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | synchronized (mToastQueue) { |
| 1612 | int callingPid = Binder.getCallingPid(); |
| 1613 | long callingId = Binder.clearCallingIdentity(); |
| 1614 | try { |
| 1615 | ToastRecord record; |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 1616 | int index; |
| 1617 | // All packages aside from the android package can enqueue one toast at a time |
| 1618 | if (!isSystemToast) { |
| 1619 | index = indexOfToastPackageLocked(pkg); |
| 1620 | } else { |
| 1621 | index = indexOfToastLocked(pkg, callback); |
| 1622 | } |
| 1623 | |
| 1624 | // If the package already has a toast, we update its toast |
| 1625 | // 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] | 1626 | if (index >= 0) { |
| 1627 | record = mToastQueue.get(index); |
| 1628 | record.update(duration); |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 1629 | record.update(callback); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1630 | } else { |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1631 | Binder token = new Binder(); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 1632 | mWindowManagerInternal.addWindowToken(token, TYPE_TOAST, DEFAULT_DISPLAY); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1633 | record = new ToastRecord(callingPid, pkg, callback, duration, token); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1634 | mToastQueue.add(record); |
| 1635 | index = mToastQueue.size() - 1; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1636 | } |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 1637 | keepProcessAliveIfNeededLocked(callingPid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1638 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 1639 | // new or just been updated. Call back and tell it to show itself. |
| 1640 | // If the callback fails, this will remove it from the list, so don't |
| 1641 | // assume that it's valid after this. |
| 1642 | if (index == 0) { |
| 1643 | showNextToastLocked(); |
| 1644 | } |
| 1645 | } finally { |
| 1646 | Binder.restoreCallingIdentity(callingId); |
| 1647 | } |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | @Override |
| 1652 | public void cancelToast(String pkg, ITransientNotification callback) { |
| 1653 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| 1654 | |
| 1655 | if (pkg == null || callback == null) { |
| 1656 | Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback); |
| 1657 | return ; |
| 1658 | } |
| 1659 | |
| 1660 | synchronized (mToastQueue) { |
| 1661 | long callingId = Binder.clearCallingIdentity(); |
| 1662 | try { |
| 1663 | int index = indexOfToastLocked(pkg, callback); |
| 1664 | if (index >= 0) { |
| 1665 | cancelToastLocked(index); |
| 1666 | } else { |
| 1667 | Slog.w(TAG, "Toast already cancelled. pkg=" + pkg |
| 1668 | + " callback=" + callback); |
| 1669 | } |
| 1670 | } finally { |
| 1671 | Binder.restoreCallingIdentity(callingId); |
| 1672 | } |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 1677 | public void enqueueNotificationWithTag(String pkg, String opPkg, String tag, int id, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1678 | Notification notification, int userId) throws RemoteException { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 1679 | enqueueNotificationInternal(pkg, opPkg, Binder.getCallingUid(), |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1680 | Binder.getCallingPid(), tag, id, notification, userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | @Override |
| 1684 | public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1685 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1686 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1687 | Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 1688 | // Don't allow client applications to cancel foreground service notis or autobundled |
| 1689 | // summaries. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1690 | final int mustNotHaveFlags = isCallingUidSystem() ? 0 : |
| 1691 | (Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_AUTOGROUP_SUMMARY); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1692 | cancelNotification(Binder.getCallingUid(), Binder.getCallingPid(), pkg, tag, id, 0, |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1693 | mustNotHaveFlags, false, userId, REASON_APP_CANCEL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | @Override |
| 1697 | public void cancelAllNotifications(String pkg, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1698 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1699 | |
| 1700 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1701 | Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg); |
| 1702 | |
| 1703 | // Calling from user space, don't allow the canceling of actively |
| 1704 | // running foreground services. |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1705 | cancelAllNotificationsInt(Binder.getCallingUid(), Binder.getCallingPid(), |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1706 | pkg, null, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1707 | REASON_APP_CANCEL_ALL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | @Override |
| 1711 | public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1712 | checkCallerIsSystem(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1713 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1714 | mRankingHelper.setEnabled(pkg, uid, enabled); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1715 | // 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] | 1716 | if (!enabled) { |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1717 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, null, 0, 0, true, |
| 1718 | UserHandle.getUserId(uid), REASON_PACKAGE_BANNED, null); |
| 1719 | } |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1720 | savePolicyFile(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | /** |
| 1724 | * Use this when you just want to know if notifications are OK for this package. |
| 1725 | */ |
| 1726 | @Override |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1727 | public boolean areNotificationsEnabled(String pkg) { |
| 1728 | return areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()); |
| 1729 | } |
| 1730 | |
| 1731 | /** |
| 1732 | * Use this when you just want to know if notifications are OK for this package. |
| 1733 | */ |
| 1734 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1735 | public boolean areNotificationsEnabledForPackage(String pkg, int uid) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1736 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1737 | |
| 1738 | return mRankingHelper.getImportance(pkg, uid) != IMPORTANCE_NONE; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1739 | } |
| 1740 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1741 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1742 | public int getPackageImportance(String pkg) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1743 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1744 | return mRankingHelper.getImportance(pkg, Binder.getCallingUid()); |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | @Override |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1748 | public boolean canShowBadge(String pkg, int uid) { |
| 1749 | checkCallerIsSystem(); |
| 1750 | return mRankingHelper.canShowBadge(pkg, uid); |
| 1751 | } |
| 1752 | |
| 1753 | @Override |
| 1754 | public void setShowBadge(String pkg, int uid, boolean showBadge) { |
| 1755 | checkCallerIsSystem(); |
| 1756 | mRankingHelper.setShowBadge(pkg, uid, showBadge); |
| 1757 | savePolicyFile(); |
| 1758 | } |
| 1759 | |
| 1760 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1761 | public void createNotificationChannelGroups(String pkg, |
| 1762 | ParceledListSlice channelGroupList) throws RemoteException { |
| 1763 | checkCallerIsSystemOrSameApp(pkg); |
| 1764 | List<NotificationChannelGroup> groups = channelGroupList.getList(); |
| 1765 | final int groupSize = groups.size(); |
| 1766 | for (int i = 0; i < groupSize; i++) { |
| 1767 | final NotificationChannelGroup group = groups.get(i); |
| 1768 | Preconditions.checkNotNull(group, "group in list is null"); |
| 1769 | mRankingHelper.createNotificationChannelGroup(pkg, Binder.getCallingUid(), group, |
| 1770 | true /* fromTargetApp */); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1771 | mListeners.notifyNotificationChannelGroupChanged(pkg, |
| 1772 | UserHandle.of(UserHandle.getCallingUserId()), group, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1773 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1774 | } |
| 1775 | savePolicyFile(); |
| 1776 | } |
| 1777 | |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 1778 | private void createNotificationChannelsImpl(String pkg, int uid, |
| 1779 | ParceledListSlice channelsList) { |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1780 | List<NotificationChannel> channels = channelsList.getList(); |
| 1781 | final int channelsSize = channels.size(); |
| 1782 | for (int i = 0; i < channelsSize; i++) { |
| 1783 | final NotificationChannel channel = channels.get(i); |
| 1784 | Preconditions.checkNotNull(channel, "channel in list is null"); |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 1785 | mRankingHelper.createNotificationChannel(pkg, uid, channel, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1786 | true /* fromTargetApp */); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1787 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1788 | UserHandle.getUserHandleForUid(uid), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1789 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), false), |
| 1790 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1791 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1792 | savePolicyFile(); |
| 1793 | } |
| 1794 | |
| 1795 | @Override |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 1796 | public void createNotificationChannels(String pkg, |
| 1797 | ParceledListSlice channelsList) throws RemoteException { |
| 1798 | checkCallerIsSystemOrSameApp(pkg); |
| 1799 | createNotificationChannelsImpl(pkg, Binder.getCallingUid(), channelsList); |
| 1800 | } |
| 1801 | |
| 1802 | @Override |
| 1803 | public void createNotificationChannelsForPackage(String pkg, int uid, |
| 1804 | ParceledListSlice channelsList) throws RemoteException { |
| 1805 | checkCallerIsSystem(); |
| 1806 | createNotificationChannelsImpl(pkg, uid, channelsList); |
| 1807 | } |
| 1808 | |
| 1809 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1810 | public NotificationChannel getNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1811 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1812 | return mRankingHelper.getNotificationChannel( |
| 1813 | pkg, Binder.getCallingUid(), channelId, false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | @Override |
| 1817 | public NotificationChannel getNotificationChannelForPackage(String pkg, int uid, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1818 | String channelId, boolean includeDeleted) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1819 | checkCallerIsSystem(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1820 | return mRankingHelper.getNotificationChannel(pkg, uid, channelId, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | @Override |
| 1824 | public void deleteNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1825 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1826 | final int callingUid = Binder.getCallingUid(); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1827 | if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 1828 | throw new IllegalArgumentException("Cannot delete default channel"); |
| 1829 | } |
| 1830 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channelId, 0, 0, true, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1831 | UserHandle.getUserId(callingUid), REASON_CHANNEL_BANNED, null); |
| 1832 | mRankingHelper.deleteNotificationChannel(pkg, callingUid, channelId); |
| 1833 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1834 | UserHandle.getUserHandleForUid(callingUid), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1835 | mRankingHelper.getNotificationChannel(pkg, callingUid, channelId, true), |
| 1836 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1837 | savePolicyFile(); |
| 1838 | } |
| 1839 | |
| 1840 | @Override |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1841 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroups( |
| 1842 | String pkg) { |
| 1843 | checkCallerIsSystemOrSameApp(pkg); |
| 1844 | return new ParceledListSlice<>(new ArrayList( |
| 1845 | mRankingHelper.getNotificationChannelGroups(pkg, Binder.getCallingUid()))); |
| 1846 | } |
| 1847 | |
| 1848 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1849 | public void deleteNotificationChannelGroup(String pkg, String groupId) { |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1850 | checkCallerIsSystemOrSameApp(pkg); |
| 1851 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1852 | final int callingUid = Binder.getCallingUid(); |
| 1853 | NotificationChannelGroup groupToDelete = |
| 1854 | mRankingHelper.getNotificationChannelGroup(groupId, pkg, callingUid); |
| 1855 | if (groupToDelete != null) { |
| 1856 | List<NotificationChannel> deletedChannels = |
| 1857 | mRankingHelper.deleteNotificationChannelGroup(pkg, callingUid, groupId); |
| 1858 | for (int i = 0; i < deletedChannels.size(); i++) { |
| 1859 | final NotificationChannel deletedChannel = deletedChannels.get(i); |
| 1860 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, deletedChannel.getId(), 0, 0, |
| 1861 | true, |
| 1862 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, |
| 1863 | null); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1864 | mListeners.notifyNotificationChannelChanged(pkg, |
| 1865 | UserHandle.getUserHandleForUid(callingUid), |
| 1866 | deletedChannel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1867 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
| 1868 | } |
| 1869 | mListeners.notifyNotificationChannelGroupChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1870 | pkg, UserHandle.getUserHandleForUid(callingUid), groupToDelete, |
| 1871 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1872 | savePolicyFile(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1873 | } |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1877 | public void updateNotificationChannelForPackage(String pkg, int uid, |
| 1878 | NotificationChannel channel) { |
Geoffrey Pitsch | 4dd5006 | 2016-12-06 16:41:22 -0500 | [diff] [blame] | 1879 | enforceSystemOrSystemUI("Caller not system or systemui"); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1880 | Preconditions.checkNotNull(channel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1881 | updateNotificationChannelInt(pkg, uid, channel, false); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | @Override |
| 1885 | public ParceledListSlice<NotificationChannel> getNotificationChannelsForPackage(String pkg, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1886 | int uid, boolean includeDeleted) { |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 1887 | enforceSystemOrSystemUI("getNotificationChannelsForPackage"); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1888 | return mRankingHelper.getNotificationChannels(pkg, uid, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 1892 | public int getNumNotificationChannelsForPackage(String pkg, int uid, |
| 1893 | boolean includeDeleted) { |
| 1894 | enforceSystemOrSystemUI("getNumNotificationChannelsForPackage"); |
| 1895 | return mRankingHelper.getNotificationChannels(pkg, uid, includeDeleted) |
| 1896 | .getList().size(); |
| 1897 | } |
| 1898 | |
| 1899 | @Override |
Julia Reynolds | 17717f5 | 2017-05-09 11:46:06 -0400 | [diff] [blame] | 1900 | public boolean onlyHasDefaultChannel(String pkg, int uid) { |
| 1901 | enforceSystemOrSystemUI("onlyHasDefaultChannel"); |
| 1902 | return mRankingHelper.onlyHasDefaultChannel(pkg, uid); |
| 1903 | } |
| 1904 | |
| 1905 | @Override |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 1906 | public int getDeletedChannelCount(String pkg, int uid) { |
| 1907 | enforceSystemOrSystemUI("getDeletedChannelCount"); |
| 1908 | return mRankingHelper.getDeletedChannelCount(pkg, uid); |
| 1909 | } |
| 1910 | |
| 1911 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1912 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroupsForPackage( |
| 1913 | String pkg, int uid, boolean includeDeleted) { |
| 1914 | checkCallerIsSystem(); |
| 1915 | return mRankingHelper.getNotificationChannelGroups(pkg, uid, includeDeleted); |
| 1916 | } |
| 1917 | |
| 1918 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 1919 | public NotificationChannelGroup getNotificationChannelGroupForPackage( |
| 1920 | String groupId, String pkg, int uid) { |
| 1921 | enforceSystemOrSystemUI("getNotificationChannelGroupForPackage"); |
| 1922 | return mRankingHelper.getNotificationChannelGroup(groupId, pkg, uid); |
| 1923 | } |
| 1924 | |
| 1925 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1926 | public ParceledListSlice<NotificationChannel> getNotificationChannels(String pkg) { |
| 1927 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1928 | return mRankingHelper.getNotificationChannels( |
| 1929 | pkg, Binder.getCallingUid(), false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1930 | } |
| 1931 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1932 | @Override |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 1933 | public void clearData(String packageName, int uid, boolean fromApp) throws RemoteException { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1934 | checkCallerIsSystem(); |
| 1935 | |
| 1936 | // Cancel posted notifications |
| 1937 | cancelAllNotificationsInt(MY_UID, MY_PID, packageName, null, 0, 0, true, |
| 1938 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, null); |
| 1939 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1940 | final String[] packages = new String[] {packageName}; |
| 1941 | final int[] uids = new int[] {uid}; |
| 1942 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1943 | // Listener & assistant |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1944 | mListeners.onPackagesChanged(true, packages, uids); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1945 | mAssistants.onPackagesChanged(true, packages, uids); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1946 | |
| 1947 | // Zen |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1948 | mConditionProviders.onPackagesChanged(true, packages, uids); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1949 | |
| 1950 | // Reset notification preferences |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 1951 | if (!fromApp) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1952 | mRankingHelper.onPackagesChanged( |
| 1953 | true, UserHandle.getCallingUserId(), packages, uids); |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 1954 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1955 | |
| 1956 | savePolicyFile(); |
| 1957 | } |
| 1958 | |
| 1959 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1960 | /** |
| 1961 | * System-only API for getting a list of current (i.e. not cleared) notifications. |
| 1962 | * |
| 1963 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1964 | * @returns A list of all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1965 | */ |
| 1966 | @Override |
| 1967 | public StatusBarNotification[] getActiveNotifications(String callingPkg) { |
| 1968 | // enforce() will ensure the calling uid has the correct permission |
| 1969 | getContext().enforceCallingOrSelfPermission( |
| 1970 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 1971 | "NotificationManagerService.getActiveNotifications"); |
| 1972 | |
| 1973 | StatusBarNotification[] tmp = null; |
| 1974 | int uid = Binder.getCallingUid(); |
| 1975 | |
| 1976 | // noteOp will check to make sure the callingPkg matches the uid |
| 1977 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 1978 | == AppOpsManager.MODE_ALLOWED) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1979 | synchronized (mNotificationLock) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1980 | tmp = new StatusBarNotification[mNotificationList.size()]; |
| 1981 | final int N = mNotificationList.size(); |
| 1982 | for (int i=0; i<N; i++) { |
| 1983 | tmp[i] = mNotificationList.get(i).sbn; |
| 1984 | } |
| 1985 | } |
| 1986 | } |
| 1987 | return tmp; |
| 1988 | } |
| 1989 | |
| 1990 | /** |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1991 | * Public API for getting a list of current notifications for the calling package/uid. |
| 1992 | * |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 1993 | * Note that since notification posting is done asynchronously, this will not return |
| 1994 | * notifications that are in the process of being posted. |
| 1995 | * |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1996 | * @returns A list of all the package's notifications, in natural order. |
| 1997 | */ |
| 1998 | @Override |
| 1999 | public ParceledListSlice<StatusBarNotification> getAppActiveNotifications(String pkg, |
| 2000 | int incomingUserId) { |
| 2001 | checkCallerIsSystemOrSameApp(pkg); |
| 2002 | int userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 2003 | Binder.getCallingUid(), incomingUserId, true, false, |
| 2004 | "getAppActiveNotifications", pkg); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2005 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2006 | final ArrayMap<String, StatusBarNotification> map |
| 2007 | = new ArrayMap<>(mNotificationList.size() + mEnqueuedNotifications.size()); |
Erik Wolsheimer | 2242b4d | 2015-11-24 13:22:04 -0800 | [diff] [blame] | 2008 | final int N = mNotificationList.size(); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2009 | for (int i = 0; i < N; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2010 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 2011 | mNotificationList.get(i).sbn); |
| 2012 | if (sbn != null) { |
| 2013 | map.put(sbn.getKey(), sbn); |
| 2014 | } |
| 2015 | } |
| 2016 | for(NotificationRecord snoozed: mSnoozeHelper.getSnoozed(userId, pkg)) { |
| 2017 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, snoozed.sbn); |
| 2018 | if (sbn != null) { |
| 2019 | map.put(sbn.getKey(), sbn); |
| 2020 | } |
| 2021 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2022 | final int M = mEnqueuedNotifications.size(); |
| 2023 | for (int i = 0; i < M; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2024 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 2025 | mEnqueuedNotifications.get(i).sbn); |
| 2026 | if (sbn != null) { |
| 2027 | map.put(sbn.getKey(), sbn); // pending update overwrites existing post here |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2028 | } |
| 2029 | } |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2030 | final ArrayList<StatusBarNotification> list = new ArrayList<>(map.size()); |
| 2031 | list.addAll(map.values()); |
| 2032 | return new ParceledListSlice<StatusBarNotification>(list); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2033 | } |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2034 | } |
| 2035 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2036 | private StatusBarNotification sanitizeSbn(String pkg, int userId, |
| 2037 | StatusBarNotification sbn) { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 2038 | if (sbn.getPackageName().equals(pkg) && sbn.getUserId() == userId) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2039 | // We could pass back a cloneLight() but clients might get confused and |
| 2040 | // try to send this thing back to notify() again, which would not work |
| 2041 | // very well. |
| 2042 | return new StatusBarNotification( |
| 2043 | sbn.getPackageName(), |
| 2044 | sbn.getOpPkg(), |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 2045 | sbn.getId(), sbn.getTag(), sbn.getUid(), sbn.getInitialPid(), |
| 2046 | sbn.getNotification().clone(), |
| 2047 | sbn.getUser(), sbn.getOverrideGroupKey(), sbn.getPostTime()); |
| 2048 | } |
| 2049 | return null; |
| 2050 | } |
| 2051 | |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 2052 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2053 | * System-only API for getting a list of recent (cleared, no longer shown) notifications. |
| 2054 | * |
| 2055 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
| 2056 | */ |
| 2057 | @Override |
| 2058 | public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) { |
| 2059 | // enforce() will ensure the calling uid has the correct permission |
| 2060 | getContext().enforceCallingOrSelfPermission( |
| 2061 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 2062 | "NotificationManagerService.getHistoricalNotifications"); |
| 2063 | |
| 2064 | StatusBarNotification[] tmp = null; |
| 2065 | int uid = Binder.getCallingUid(); |
| 2066 | |
| 2067 | // noteOp will check to make sure the callingPkg matches the uid |
| 2068 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 2069 | == AppOpsManager.MODE_ALLOWED) { |
| 2070 | synchronized (mArchive) { |
| 2071 | tmp = mArchive.getArray(count); |
| 2072 | } |
| 2073 | } |
| 2074 | return tmp; |
| 2075 | } |
| 2076 | |
| 2077 | /** |
| 2078 | * Register a listener binder directly with the notification manager. |
| 2079 | * |
| 2080 | * Only works with system callers. Apps should extend |
| 2081 | * {@link android.service.notification.NotificationListenerService}. |
| 2082 | */ |
| 2083 | @Override |
| 2084 | public void registerListener(final INotificationListener listener, |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 2085 | final ComponentName component, final int userid) { |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 2086 | enforceSystemOrSystemUI("INotificationManager.registerListener"); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 2087 | mListeners.registerService(listener, component, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | /** |
| 2091 | * Remove a listener binder directly |
| 2092 | */ |
| 2093 | @Override |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 2094 | public void unregisterListener(INotificationListener token, int userid) { |
Chris Wren | b7c8109 | 2016-03-10 11:41:10 -0500 | [diff] [blame] | 2095 | mListeners.unregisterService(token, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | /** |
| 2099 | * Allow an INotificationListener to simulate a "clear all" operation. |
| 2100 | * |
| 2101 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications} |
| 2102 | * |
| 2103 | * @param token The binder for the listener, to check that the caller is allowed |
| 2104 | */ |
| 2105 | @Override |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2106 | public void cancelNotificationsFromListener(INotificationListener token, String[] keys) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2107 | final int callingUid = Binder.getCallingUid(); |
| 2108 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2109 | long identity = Binder.clearCallingIdentity(); |
| 2110 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2111 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2112 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2113 | if (keys != null) { |
| 2114 | final int N = keys.length; |
| 2115 | for (int i = 0; i < N; i++) { |
| 2116 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 2117 | if (r == null) continue; |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2118 | final int userId = r.sbn.getUserId(); |
| 2119 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 2120 | !mUserProfiles.isCurrentProfile(userId)) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2121 | throw new SecurityException("Disallowed call from listener: " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2122 | + info.service); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2123 | } |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 2124 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2125 | r.sbn.getPackageName(), r.sbn.getTag(), r.sbn.getId(), |
| 2126 | userId); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2127 | } |
| 2128 | } else { |
| 2129 | cancelAllLocked(callingUid, callingPid, info.userid, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2130 | REASON_LISTENER_CANCEL_ALL, info, info.supportsProfiles()); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2131 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2132 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2133 | } finally { |
| 2134 | Binder.restoreCallingIdentity(identity); |
| 2135 | } |
| 2136 | } |
| 2137 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2138 | /** |
| 2139 | * Handle request from an approved listener to re-enable itself. |
| 2140 | * |
| 2141 | * @param component The componenet to be re-enabled, caller must match package. |
| 2142 | */ |
| 2143 | @Override |
| 2144 | public void requestBindListener(ComponentName component) { |
| 2145 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 2146 | long identity = Binder.clearCallingIdentity(); |
| 2147 | try { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2148 | ManagedServices manager = |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2149 | mAssistants.isComponentEnabledForCurrentProfiles(component) |
| 2150 | ? mAssistants |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2151 | : mListeners; |
| 2152 | manager.setComponentState(component, true); |
| 2153 | } finally { |
| 2154 | Binder.restoreCallingIdentity(identity); |
| 2155 | } |
| 2156 | } |
| 2157 | |
| 2158 | @Override |
| 2159 | public void requestUnbindListener(INotificationListener token) { |
| 2160 | long identity = Binder.clearCallingIdentity(); |
| 2161 | try { |
| 2162 | // allow bound services to disable themselves |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2163 | synchronized (mNotificationLock) { |
| 2164 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2165 | info.getOwner().setComponentState(info.component, false); |
| 2166 | } |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2167 | } finally { |
| 2168 | Binder.restoreCallingIdentity(identity); |
| 2169 | } |
| 2170 | } |
| 2171 | |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2172 | @Override |
| 2173 | public void setNotificationsShownFromListener(INotificationListener token, String[] keys) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2174 | long identity = Binder.clearCallingIdentity(); |
| 2175 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2176 | synchronized (mNotificationLock) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2177 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2178 | if (keys != null) { |
| 2179 | final int N = keys.length; |
| 2180 | for (int i = 0; i < N; i++) { |
| 2181 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
| 2182 | if (r == null) continue; |
| 2183 | final int userId = r.sbn.getUserId(); |
| 2184 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
| 2185 | !mUserProfiles.isCurrentProfile(userId)) { |
| 2186 | throw new SecurityException("Disallowed call from listener: " |
| 2187 | + info.service); |
| 2188 | } |
| 2189 | if (!r.isSeen()) { |
| 2190 | if (DBG) Slog.d(TAG, "Marking notification as seen " + keys[i]); |
| 2191 | mAppUsageStats.reportEvent(r.sbn.getPackageName(), |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 2192 | userId == UserHandle.USER_ALL ? UserHandle.USER_SYSTEM |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2193 | : userId, |
Adam Lesinski | c8e8729 | 2015-06-10 15:33:45 -0700 | [diff] [blame] | 2194 | UsageEvents.Event.USER_INTERACTION); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2195 | r.setSeen(); |
| 2196 | } |
| 2197 | } |
| 2198 | } |
| 2199 | } |
| 2200 | } finally { |
| 2201 | Binder.restoreCallingIdentity(identity); |
| 2202 | } |
| 2203 | } |
| 2204 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2205 | /** |
| 2206 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2207 | * |
| 2208 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2209 | * |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2210 | * @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] | 2211 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2212 | @GuardedBy("mNotificationLock") |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2213 | private void cancelNotificationFromListenerLocked(ManagedServiceInfo info, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2214 | int callingUid, int callingPid, String pkg, String tag, int id, int userId) { |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2215 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
| 2216 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
| 2217 | true, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2218 | userId, REASON_LISTENER_CANCEL, info); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2219 | } |
| 2220 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2221 | /** |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2222 | * Allow an INotificationListener to snooze a single notification until a context. |
| 2223 | * |
| 2224 | * @param token The binder for the listener, to check that the caller is allowed |
| 2225 | */ |
| 2226 | @Override |
| 2227 | public void snoozeNotificationUntilContextFromListener(INotificationListener token, |
| 2228 | String key, String snoozeCriterionId) { |
| 2229 | long identity = Binder.clearCallingIdentity(); |
| 2230 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2231 | synchronized (mNotificationLock) { |
| 2232 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2233 | snoozeNotificationInt(key, SNOOZE_UNTIL_UNSPECIFIED, snoozeCriterionId, info); |
| 2234 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2235 | } finally { |
| 2236 | Binder.restoreCallingIdentity(identity); |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | /** |
| 2241 | * Allow an INotificationListener to snooze a single notification until a time. |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2242 | * |
| 2243 | * @param token The binder for the listener, to check that the caller is allowed |
| 2244 | */ |
| 2245 | @Override |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2246 | public void snoozeNotificationUntilFromListener(INotificationListener token, String key, |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 2247 | long duration) { |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2248 | long identity = Binder.clearCallingIdentity(); |
| 2249 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2250 | synchronized (mNotificationLock) { |
| 2251 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2252 | snoozeNotificationInt(key, duration, null, info); |
| 2253 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2254 | } finally { |
| 2255 | Binder.restoreCallingIdentity(identity); |
| 2256 | } |
| 2257 | } |
| 2258 | |
| 2259 | /** |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2260 | * Allows the notification assistant to un-snooze a single notification. |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2261 | * |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2262 | * @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] | 2263 | */ |
| 2264 | @Override |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2265 | public void unsnoozeNotificationFromAssistant(INotificationListener token, String key) { |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2266 | long identity = Binder.clearCallingIdentity(); |
| 2267 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2268 | synchronized (mNotificationLock) { |
| 2269 | final ManagedServiceInfo info = |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2270 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2271 | unsnoozeNotificationInt(key, info); |
| 2272 | } |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2273 | } finally { |
| 2274 | Binder.restoreCallingIdentity(identity); |
| 2275 | } |
| 2276 | } |
| 2277 | |
| 2278 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2279 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2280 | * |
| 2281 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2282 | * |
| 2283 | * @param token The binder for the listener, to check that the caller is allowed |
| 2284 | */ |
| 2285 | @Override |
| 2286 | public void cancelNotificationFromListener(INotificationListener token, String pkg, |
| 2287 | String tag, int id) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2288 | final int callingUid = Binder.getCallingUid(); |
| 2289 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2290 | long identity = Binder.clearCallingIdentity(); |
| 2291 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2292 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2293 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2294 | if (info.supportsProfiles()) { |
| 2295 | Log.e(TAG, "Ignoring deprecated cancelNotification(pkg, tag, id) " |
| 2296 | + "from " + info.component |
| 2297 | + " use cancelNotification(key) instead."); |
| 2298 | } else { |
| 2299 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2300 | pkg, tag, id, info.userid); |
| 2301 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2302 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2303 | } finally { |
| 2304 | Binder.restoreCallingIdentity(identity); |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | /** |
| 2309 | * Allow an INotificationListener to request the list of outstanding notifications seen by |
| 2310 | * the current user. Useful when starting up, after which point the listener callbacks |
| 2311 | * should be used. |
| 2312 | * |
| 2313 | * @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] | 2314 | * @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] | 2315 | * @returns The return value will contain the notifications specified in keys, in that |
| 2316 | * order, or if keys is null, all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2317 | */ |
| 2318 | @Override |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2319 | public ParceledListSlice<StatusBarNotification> getActiveNotificationsFromListener( |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2320 | INotificationListener token, String[] keys, int trim) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2321 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2322 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2323 | final boolean getKeys = keys != null; |
| 2324 | final int N = getKeys ? keys.length : mNotificationList.size(); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2325 | final ArrayList<StatusBarNotification> list |
| 2326 | = new ArrayList<StatusBarNotification>(N); |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2327 | for (int i=0; i<N; i++) { |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2328 | final NotificationRecord r = getKeys |
| 2329 | ? mNotificationsByKey.get(keys[i]) |
| 2330 | : mNotificationList.get(i); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2331 | if (r == null) continue; |
| 2332 | StatusBarNotification sbn = r.sbn; |
| 2333 | if (!isVisibleToListener(sbn, info)) continue; |
| 2334 | StatusBarNotification sbnToSend = |
| 2335 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2336 | list.add(sbnToSend); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2337 | } |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2338 | return new ParceledListSlice<StatusBarNotification>(list); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2339 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2340 | } |
| 2341 | |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2342 | /** |
| 2343 | * Allow an INotificationListener to request the list of outstanding snoozed notifications |
| 2344 | * seen by the current user. Useful when starting up, after which point the listener |
| 2345 | * callbacks should be used. |
| 2346 | * |
| 2347 | * @param token The binder for the listener, to check that the caller is allowed |
| 2348 | * @returns The return value will contain the notifications specified in keys, in that |
| 2349 | * order, or if keys is null, all the notifications, in natural order. |
| 2350 | */ |
| 2351 | @Override |
| 2352 | public ParceledListSlice<StatusBarNotification> getSnoozedNotificationsFromListener( |
| 2353 | INotificationListener token, int trim) { |
| 2354 | synchronized (mNotificationLock) { |
| 2355 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2356 | List<NotificationRecord> snoozedRecords = mSnoozeHelper.getSnoozed(); |
| 2357 | final int N = snoozedRecords.size(); |
| 2358 | final ArrayList<StatusBarNotification> list = new ArrayList<>(N); |
| 2359 | for (int i=0; i < N; i++) { |
| 2360 | final NotificationRecord r = snoozedRecords.get(i); |
| 2361 | if (r == null) continue; |
| 2362 | StatusBarNotification sbn = r.sbn; |
| 2363 | if (!isVisibleToListener(sbn, info)) continue; |
| 2364 | StatusBarNotification sbnToSend = |
| 2365 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2366 | list.add(sbnToSend); |
| 2367 | } |
| 2368 | return new ParceledListSlice<>(list); |
| 2369 | } |
| 2370 | } |
| 2371 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2372 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2373 | public void requestHintsFromListener(INotificationListener token, int hints) { |
| 2374 | final long identity = Binder.clearCallingIdentity(); |
| 2375 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2376 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2377 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2378 | final int disableEffectsMask = HINT_HOST_DISABLE_EFFECTS |
| 2379 | | HINT_HOST_DISABLE_NOTIFICATION_EFFECTS |
| 2380 | | HINT_HOST_DISABLE_CALL_EFFECTS; |
| 2381 | final boolean disableEffects = (hints & disableEffectsMask) != 0; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2382 | if (disableEffects) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2383 | addDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2384 | } else { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2385 | removeDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2386 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2387 | updateListenerHintsLocked(); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2388 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2389 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2390 | } finally { |
| 2391 | Binder.restoreCallingIdentity(identity); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2392 | } |
| 2393 | } |
| 2394 | |
| 2395 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2396 | public int getHintsFromListener(INotificationListener token) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2397 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2398 | return mListenerHints; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2399 | } |
| 2400 | } |
| 2401 | |
| 2402 | @Override |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2403 | public void requestInterruptionFilterFromListener(INotificationListener token, |
| 2404 | int interruptionFilter) throws RemoteException { |
| 2405 | final long identity = Binder.clearCallingIdentity(); |
| 2406 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2407 | synchronized (mNotificationLock) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2408 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2409 | mZenModeHelper.requestFromListener(info.component, interruptionFilter); |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2410 | updateInterruptionFilterLocked(); |
| 2411 | } |
| 2412 | } finally { |
| 2413 | Binder.restoreCallingIdentity(identity); |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | @Override |
| 2418 | public int getInterruptionFilterFromListener(INotificationListener token) |
| 2419 | throws RemoteException { |
| 2420 | synchronized (mNotificationLight) { |
| 2421 | return mInterruptionFilter; |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | @Override |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2426 | public void setOnNotificationPostedTrimFromListener(INotificationListener token, int trim) |
| 2427 | throws RemoteException { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2428 | synchronized (mNotificationLock) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2429 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2430 | if (info == null) return; |
| 2431 | mListeners.setOnNotificationPostedTrimLocked(info, trim); |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2436 | public int getZenMode() { |
| 2437 | return mZenModeHelper.getZenMode(); |
| 2438 | } |
| 2439 | |
| 2440 | @Override |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 2441 | public ZenModeConfig getZenModeConfig() { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2442 | enforceSystemOrSystemUI("INotificationManager.getZenModeConfig"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 2443 | return mZenModeHelper.getConfig(); |
| 2444 | } |
| 2445 | |
| 2446 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2447 | public void setZenMode(int mode, Uri conditionId, String reason) throws RemoteException { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2448 | enforceSystemOrSystemUI("INotificationManager.setZenMode"); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2449 | final long identity = Binder.clearCallingIdentity(); |
| 2450 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 2451 | mZenModeHelper.setManualZenMode(mode, conditionId, null, reason); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2452 | } finally { |
| 2453 | Binder.restoreCallingIdentity(identity); |
| 2454 | } |
| 2455 | } |
| 2456 | |
| 2457 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2458 | public List<ZenModeConfig.ZenRule> getZenRules() throws RemoteException { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2459 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRules"); |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2460 | return mZenModeHelper.getZenRules(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2461 | } |
| 2462 | |
| 2463 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2464 | public AutomaticZenRule getAutomaticZenRule(String id) throws RemoteException { |
| 2465 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2466 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRule"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2467 | return mZenModeHelper.getAutomaticZenRule(id); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2471 | public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2472 | throws RemoteException { |
| 2473 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 2474 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 2475 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 2476 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2477 | enforcePolicyAccess(Binder.getCallingUid(), "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2478 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2479 | return mZenModeHelper.addAutomaticZenRule(automaticZenRule, |
| 2480 | "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2484 | public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2485 | throws RemoteException { |
| 2486 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 2487 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 2488 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 2489 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
| 2490 | enforcePolicyAccess(Binder.getCallingUid(), "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2491 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2492 | return mZenModeHelper.updateAutomaticZenRule(id, automaticZenRule, |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2493 | "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2494 | } |
| 2495 | |
| 2496 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2497 | public boolean removeAutomaticZenRule(String id) throws RemoteException { |
| 2498 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2499 | // Verify that they can modify zen rules. |
| 2500 | enforcePolicyAccess(Binder.getCallingUid(), "removeAutomaticZenRule"); |
| 2501 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2502 | return mZenModeHelper.removeAutomaticZenRule(id, "removeAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | @Override |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 2506 | public boolean removeAutomaticZenRules(String packageName) throws RemoteException { |
| 2507 | Preconditions.checkNotNull(packageName, "Package name is null"); |
| 2508 | enforceSystemOrSystemUI("removeAutomaticZenRules"); |
| 2509 | |
| 2510 | return mZenModeHelper.removeAutomaticZenRules(packageName, "removeAutomaticZenRules"); |
| 2511 | } |
| 2512 | |
| 2513 | @Override |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 2514 | public int getRuleInstanceCount(ComponentName owner) throws RemoteException { |
| 2515 | Preconditions.checkNotNull(owner, "Owner is null"); |
| 2516 | enforceSystemOrSystemUI("getRuleInstanceCount"); |
| 2517 | |
| 2518 | return mZenModeHelper.getCurrentInstanceCount(owner); |
| 2519 | } |
| 2520 | |
| 2521 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2522 | public void setInterruptionFilter(String pkg, int filter) throws RemoteException { |
| 2523 | enforcePolicyAccess(pkg, "setInterruptionFilter"); |
| 2524 | final int zen = NotificationManager.zenModeFromInterruptionFilter(filter, -1); |
| 2525 | if (zen == -1) throw new IllegalArgumentException("Invalid filter: " + filter); |
| 2526 | final long identity = Binder.clearCallingIdentity(); |
| 2527 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 2528 | mZenModeHelper.setManualZenMode(zen, null, pkg, "setInterruptionFilter"); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2529 | } finally { |
| 2530 | Binder.restoreCallingIdentity(identity); |
| 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | @Override |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 2535 | public void notifyConditions(final String pkg, IConditionProvider provider, |
| 2536 | final Condition[] conditions) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2537 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 2538 | checkCallerIsSystemOrSameApp(pkg); |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 2539 | mHandler.post(new Runnable() { |
| 2540 | @Override |
| 2541 | public void run() { |
| 2542 | mConditionProviders.notifyConditions(pkg, info, conditions); |
| 2543 | } |
| 2544 | }); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2545 | } |
| 2546 | |
Julia Reynolds | 38e6ca4 | 2016-08-08 08:38:09 -0400 | [diff] [blame] | 2547 | @Override |
| 2548 | public void requestUnbindProvider(IConditionProvider provider) { |
| 2549 | long identity = Binder.clearCallingIdentity(); |
| 2550 | try { |
| 2551 | // allow bound services to disable themselves |
| 2552 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 2553 | info.getOwner().setComponentState(info.component, false); |
| 2554 | } finally { |
| 2555 | Binder.restoreCallingIdentity(identity); |
| 2556 | } |
| 2557 | } |
| 2558 | |
| 2559 | @Override |
| 2560 | public void requestBindProvider(ComponentName component) { |
| 2561 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 2562 | long identity = Binder.clearCallingIdentity(); |
| 2563 | try { |
| 2564 | mConditionProviders.setComponentState(component, true); |
| 2565 | } finally { |
| 2566 | Binder.restoreCallingIdentity(identity); |
| 2567 | } |
| 2568 | } |
| 2569 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2570 | private void enforceSystemOrSystemUI(String message) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 2571 | if (isCallerSystemOrPhone()) return; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2572 | getContext().enforceCallingPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 2573 | message); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2574 | } |
| 2575 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 2576 | private void enforceSystemOrSystemUIOrSamePackage(String pkg, String message) { |
| 2577 | try { |
| 2578 | checkCallerIsSystemOrSameApp(pkg); |
| 2579 | } catch (SecurityException e) { |
| 2580 | getContext().enforceCallingPermission( |
| 2581 | android.Manifest.permission.STATUS_BAR_SERVICE, |
| 2582 | message); |
| 2583 | } |
| 2584 | } |
| 2585 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2586 | private void enforcePolicyAccess(int uid, String method) { |
| 2587 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 2588 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 2589 | return; |
| 2590 | } |
| 2591 | boolean accessAllowed = false; |
| 2592 | String[] packages = getContext().getPackageManager().getPackagesForUid(uid); |
| 2593 | final int packageCount = packages.length; |
| 2594 | for (int i = 0; i < packageCount; i++) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2595 | if (mConditionProviders.isPackageOrComponentAllowed( |
| 2596 | packages[i], UserHandle.getUserId(uid))) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2597 | accessAllowed = true; |
| 2598 | } |
| 2599 | } |
| 2600 | if (!accessAllowed) { |
| 2601 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 2602 | throw new SecurityException("Notification policy access denied"); |
| 2603 | } |
| 2604 | } |
| 2605 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2606 | private void enforcePolicyAccess(String pkg, String method) { |
Julia Reynolds | 6ee2617 | 2015-09-28 11:34:48 -0400 | [diff] [blame] | 2607 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 2608 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 2609 | return; |
| 2610 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 2611 | checkCallerIsSameApp(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2612 | if (!checkPolicyAccess(pkg)) { |
| 2613 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 2614 | throw new SecurityException("Notification policy access denied"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2615 | } |
| 2616 | } |
| 2617 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2618 | private boolean checkPackagePolicyAccess(String pkg) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2619 | return mConditionProviders.isPackageOrComponentAllowed( |
| 2620 | pkg, getCallingUserHandle().getIdentifier()); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | private boolean checkPolicyAccess(String pkg) { |
Julia Reynolds | 0867b3a | 2016-03-30 17:29:54 -0400 | [diff] [blame] | 2624 | try { |
| 2625 | int uid = getContext().getPackageManager().getPackageUidAsUser( |
| 2626 | pkg, UserHandle.getCallingUserId()); |
| 2627 | if (PackageManager.PERMISSION_GRANTED == ActivityManager.checkComponentPermission( |
| 2628 | android.Manifest.permission.MANAGE_NOTIFICATIONS, uid, |
| 2629 | -1, true)) { |
| 2630 | return true; |
| 2631 | } |
| 2632 | } catch (NameNotFoundException e) { |
| 2633 | return false; |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 2634 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2635 | return checkPackagePolicyAccess(pkg) || mListeners.isComponentEnabledForPackage(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2636 | } |
| 2637 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2638 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2639 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2640 | if (!DumpUtils.checkDumpAndUsageStatsPermission(getContext(), TAG, pw)) return; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2641 | final DumpFilter filter = DumpFilter.parseFromArguments(args); |
| 2642 | if (filter != null && filter.stats) { |
| 2643 | dumpJson(pw, filter); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 2644 | } else if (filter != null && filter.proto) { |
| 2645 | dumpProto(fd, filter); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2646 | } else { |
| 2647 | dumpImpl(pw, filter); |
| 2648 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2649 | } |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2650 | |
| 2651 | @Override |
| 2652 | public ComponentName getEffectsSuppressor() { |
Bryce Lee | ba3d895 | 2016-04-12 12:39:15 -0700 | [diff] [blame] | 2653 | return !mEffectsSuppressors.isEmpty() ? mEffectsSuppressors.get(0) : null; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2654 | } |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 2655 | |
| 2656 | @Override |
| 2657 | public boolean matchesCallFilter(Bundle extras) { |
| 2658 | enforceSystemOrSystemUI("INotificationManager.matchesCallFilter"); |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 2659 | return mZenModeHelper.matchesCallFilter( |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 2660 | Binder.getCallingUserHandle(), |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 2661 | extras, |
| 2662 | mRankingHelper.findExtractor(ValidateNotificationPeople.class), |
| 2663 | MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS, |
| 2664 | MATCHES_CALL_FILTER_TIMEOUT_AFFINITY); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 2665 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 2666 | |
| 2667 | @Override |
| 2668 | public boolean isSystemConditionProviderEnabled(String path) { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2669 | enforceSystemOrSystemUI("INotificationManager.isSystemConditionProviderEnabled"); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2670 | return mConditionProviders.isSystemProviderEnabled(path); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 2671 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2672 | |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 2673 | // Backup/restore interface |
| 2674 | @Override |
| 2675 | public byte[] getBackupPayload(int user) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2676 | if (DBG) Slog.d(TAG, "getBackupPayload u=" + user); |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 2677 | //TODO: http://b/22388012 |
| 2678 | if (user != UserHandle.USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2679 | Slog.w(TAG, "getBackupPayload: cannot backup policy for user " + user); |
| 2680 | return null; |
| 2681 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 2682 | synchronized(mPolicyFile) { |
| 2683 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 2684 | try { |
| 2685 | writePolicyXml(baos, true /*forBackup*/); |
| 2686 | return baos.toByteArray(); |
| 2687 | } catch (IOException e) { |
| 2688 | Slog.w(TAG, "getBackupPayload: error writing payload for user " + user, e); |
| 2689 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2690 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 2691 | return null; |
| 2692 | } |
| 2693 | |
| 2694 | @Override |
| 2695 | public void applyRestore(byte[] payload, int user) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2696 | if (DBG) Slog.d(TAG, "applyRestore u=" + user + " payload=" |
| 2697 | + (payload != null ? new String(payload, StandardCharsets.UTF_8) : null)); |
| 2698 | if (payload == null) { |
| 2699 | Slog.w(TAG, "applyRestore: no payload to restore for user " + user); |
| 2700 | return; |
| 2701 | } |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 2702 | //TODO: http://b/22388012 |
| 2703 | if (user != UserHandle.USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2704 | Slog.w(TAG, "applyRestore: cannot restore policy for user " + user); |
| 2705 | return; |
| 2706 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 2707 | synchronized(mPolicyFile) { |
| 2708 | final ByteArrayInputStream bais = new ByteArrayInputStream(payload); |
| 2709 | try { |
| 2710 | readPolicyXml(bais, true /*forRestore*/); |
| 2711 | savePolicyFile(); |
| 2712 | } catch (NumberFormatException | XmlPullParserException | IOException e) { |
| 2713 | Slog.w(TAG, "applyRestore: error reading payload", e); |
| 2714 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2715 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 2716 | } |
| 2717 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2718 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2719 | public boolean isNotificationPolicyAccessGranted(String pkg) { |
| 2720 | return checkPolicyAccess(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | @Override |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 2724 | public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {; |
| 2725 | enforceSystemOrSystemUIOrSamePackage(pkg, |
| 2726 | "request policy access status for another package"); |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 2727 | return checkPolicyAccess(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2731 | public void setNotificationPolicyAccessGranted(String pkg, boolean granted) |
| 2732 | throws RemoteException { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2733 | checkCallerIsSystemOrShell(); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2734 | if (!mActivityManager.isLowRamDevice()) { |
| 2735 | mConditionProviders.setPackageOrComponentEnabled( |
| 2736 | pkg, getCallingUserHandle().getIdentifier(), true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2737 | |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2738 | getContext().sendBroadcastAsUser(new Intent( |
| 2739 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 2740 | .setPackage(pkg) |
| 2741 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
| 2742 | getCallingUserHandle(), null); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2743 | |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2744 | savePolicyFile(); |
| 2745 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | @Override |
| 2749 | public Policy getNotificationPolicy(String pkg) { |
| 2750 | enforcePolicyAccess(pkg, "getNotificationPolicy"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2751 | final long identity = Binder.clearCallingIdentity(); |
| 2752 | try { |
| 2753 | return mZenModeHelper.getNotificationPolicy(); |
| 2754 | } finally { |
| 2755 | Binder.restoreCallingIdentity(identity); |
| 2756 | } |
| 2757 | } |
| 2758 | |
| 2759 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2760 | public void setNotificationPolicy(String pkg, Policy policy) { |
| 2761 | enforcePolicyAccess(pkg, "setNotificationPolicy"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2762 | final long identity = Binder.clearCallingIdentity(); |
| 2763 | try { |
| 2764 | mZenModeHelper.setNotificationPolicy(policy); |
| 2765 | } finally { |
| 2766 | Binder.restoreCallingIdentity(identity); |
| 2767 | } |
| 2768 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2769 | |
| 2770 | @Override |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2771 | public List<String> getEnabledNotificationListenerPackages() { |
| 2772 | checkCallerIsSystem(); |
| 2773 | return mListeners.getAllowedPackages(getCallingUserHandle().getIdentifier()); |
| 2774 | } |
| 2775 | |
| 2776 | @Override |
| 2777 | public List<ComponentName> getEnabledNotificationListeners(int userId) { |
| 2778 | checkCallerIsSystem(); |
| 2779 | return mListeners.getAllowedComponents(userId); |
| 2780 | } |
| 2781 | |
| 2782 | @Override |
| 2783 | public boolean isNotificationListenerAccessGranted(ComponentName listener) { |
| 2784 | Preconditions.checkNotNull(listener); |
| 2785 | checkCallerIsSystemOrSameApp(listener.getPackageName()); |
| 2786 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 2787 | getCallingUserHandle().getIdentifier()); |
| 2788 | } |
| 2789 | |
| 2790 | @Override |
| 2791 | public boolean isNotificationListenerAccessGrantedForUser(ComponentName listener, |
| 2792 | int userId) { |
| 2793 | Preconditions.checkNotNull(listener); |
| 2794 | checkCallerIsSystem(); |
| 2795 | return mListeners.isPackageOrComponentAllowed(listener.flattenToString(), |
| 2796 | userId); |
| 2797 | } |
| 2798 | |
| 2799 | @Override |
| 2800 | public boolean isNotificationAssistantAccessGranted(ComponentName assistant) { |
| 2801 | Preconditions.checkNotNull(assistant); |
| 2802 | checkCallerIsSystemOrSameApp(assistant.getPackageName()); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2803 | return mAssistants.isPackageOrComponentAllowed(assistant.flattenToString(), |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2804 | getCallingUserHandle().getIdentifier()); |
| 2805 | } |
| 2806 | |
| 2807 | @Override |
| 2808 | public void setNotificationListenerAccessGranted(ComponentName listener, |
| 2809 | boolean granted) throws RemoteException { |
| 2810 | setNotificationListenerAccessGrantedForUser( |
| 2811 | listener, getCallingUserHandle().getIdentifier(), granted); |
| 2812 | } |
| 2813 | |
| 2814 | @Override |
| 2815 | public void setNotificationAssistantAccessGranted(ComponentName assistant, |
| 2816 | boolean granted) throws RemoteException { |
| 2817 | setNotificationAssistantAccessGrantedForUser( |
| 2818 | assistant, getCallingUserHandle().getIdentifier(), granted); |
| 2819 | } |
| 2820 | |
| 2821 | @Override |
| 2822 | public void setNotificationListenerAccessGrantedForUser(ComponentName listener, int userId, |
| 2823 | boolean granted) throws RemoteException { |
| 2824 | Preconditions.checkNotNull(listener); |
Julia Reynolds | 0d21764 | 2017-08-11 11:26:04 -0400 | [diff] [blame] | 2825 | checkCallerIsSystemOrShell(); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2826 | if (!mActivityManager.isLowRamDevice()) { |
| 2827 | mConditionProviders.setPackageOrComponentEnabled(listener.flattenToString(), |
| 2828 | userId, false, granted); |
| 2829 | mListeners.setPackageOrComponentEnabled(listener.flattenToString(), |
| 2830 | userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2831 | |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2832 | getContext().sendBroadcastAsUser(new Intent( |
| 2833 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 2834 | .setPackage(listener.getPackageName()) |
| 2835 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
| 2836 | getCallingUserHandle(), null); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2837 | |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2838 | savePolicyFile(); |
| 2839 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2840 | } |
| 2841 | |
| 2842 | @Override |
| 2843 | public void setNotificationAssistantAccessGrantedForUser(ComponentName assistant, |
| 2844 | int userId, boolean granted) throws RemoteException { |
| 2845 | Preconditions.checkNotNull(assistant); |
Julia Reynolds | 0d21764 | 2017-08-11 11:26:04 -0400 | [diff] [blame] | 2846 | checkCallerIsSystemOrShell(); |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2847 | if (!mActivityManager.isLowRamDevice()) { |
| 2848 | mConditionProviders.setPackageOrComponentEnabled(assistant.flattenToString(), |
| 2849 | userId, false, granted); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2850 | mAssistants.setPackageOrComponentEnabled(assistant.flattenToString(), |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2851 | userId, true, granted); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2852 | |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2853 | getContext().sendBroadcastAsUser(new Intent( |
| 2854 | NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 2855 | .setPackage(assistant.getPackageName()) |
| 2856 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), |
| 2857 | getCallingUserHandle(), null); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2858 | |
Julia Reynolds | 68263d1 | 2017-06-21 14:21:19 -0400 | [diff] [blame] | 2859 | savePolicyFile(); |
| 2860 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | @Override |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 2864 | public void applyEnqueuedAdjustmentFromAssistant(INotificationListener token, |
| 2865 | Adjustment adjustment) throws RemoteException { |
| 2866 | final long identity = Binder.clearCallingIdentity(); |
| 2867 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2868 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2869 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 2870 | int N = mEnqueuedNotifications.size(); |
| 2871 | for (int i = 0; i < N; i++) { |
| 2872 | final NotificationRecord n = mEnqueuedNotifications.get(i); |
| 2873 | if (Objects.equals(adjustment.getKey(), n.getKey()) |
| 2874 | && Objects.equals(adjustment.getUser(), n.getUserId())) { |
| 2875 | applyAdjustment(n, adjustment); |
| 2876 | break; |
| 2877 | } |
| 2878 | } |
| 2879 | } |
| 2880 | } finally { |
| 2881 | Binder.restoreCallingIdentity(identity); |
| 2882 | } |
| 2883 | } |
| 2884 | |
| 2885 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 2886 | public void applyAdjustmentFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2887 | Adjustment adjustment) throws RemoteException { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2888 | final long identity = Binder.clearCallingIdentity(); |
| 2889 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2890 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2891 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 2892 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 2893 | applyAdjustment(n, adjustment); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2894 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2895 | mRankingHandler.requestSort(); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2896 | } finally { |
| 2897 | Binder.restoreCallingIdentity(identity); |
| 2898 | } |
| 2899 | } |
| 2900 | |
| 2901 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 2902 | public void applyAdjustmentsFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2903 | List<Adjustment> adjustments) throws RemoteException { |
| 2904 | |
| 2905 | final long identity = Binder.clearCallingIdentity(); |
| 2906 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2907 | synchronized (mNotificationLock) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2908 | mAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2909 | for (Adjustment adjustment : adjustments) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 2910 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 2911 | applyAdjustment(n, adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2912 | } |
| 2913 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2914 | mRankingHandler.requestSort(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2915 | } finally { |
| 2916 | Binder.restoreCallingIdentity(identity); |
| 2917 | } |
| 2918 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2919 | |
| 2920 | @Override |
| 2921 | public void updateNotificationChannelFromPrivilegedListener(INotificationListener token, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2922 | String pkg, UserHandle user, NotificationChannel channel) throws RemoteException { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2923 | Preconditions.checkNotNull(channel); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2924 | Preconditions.checkNotNull(pkg); |
| 2925 | Preconditions.checkNotNull(user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2926 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2927 | verifyPrivilegedListener(token, user); |
| 2928 | updateNotificationChannelInt(pkg, getUidForPackageAndUser(pkg, user), channel, true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | @Override |
| 2932 | public ParceledListSlice<NotificationChannel> getNotificationChannelsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2933 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 2934 | Preconditions.checkNotNull(pkg); |
| 2935 | Preconditions.checkNotNull(user); |
| 2936 | verifyPrivilegedListener(token, user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2937 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2938 | return mRankingHelper.getNotificationChannels(pkg, getUidForPackageAndUser(pkg, user), |
| 2939 | false /* includeDeleted */); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | @Override |
| 2943 | public ParceledListSlice<NotificationChannelGroup> |
| 2944 | getNotificationChannelGroupsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2945 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 2946 | Preconditions.checkNotNull(pkg); |
| 2947 | Preconditions.checkNotNull(user); |
| 2948 | verifyPrivilegedListener(token, user); |
| 2949 | |
| 2950 | List<NotificationChannelGroup> groups = new ArrayList<>(); |
| 2951 | groups.addAll(mRankingHelper.getNotificationChannelGroups( |
| 2952 | pkg, getUidForPackageAndUser(pkg, user))); |
| 2953 | return new ParceledListSlice<>(groups); |
| 2954 | } |
| 2955 | |
| 2956 | private void verifyPrivilegedListener(INotificationListener token, UserHandle user) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2957 | ManagedServiceInfo info; |
| 2958 | synchronized (mNotificationLock) { |
| 2959 | info = mListeners.checkServiceTokenLocked(token); |
| 2960 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 2961 | if (!hasCompanionDevice(info)) { |
| 2962 | throw new SecurityException(info + " does not have access"); |
| 2963 | } |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2964 | if (!info.enabledAndUserMatches(user.getIdentifier())) { |
| 2965 | throw new SecurityException(info + " does not have access"); |
| 2966 | } |
| 2967 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2968 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2969 | private int getUidForPackageAndUser(String pkg, UserHandle user) throws RemoteException { |
| 2970 | int uid = 0; |
| 2971 | long identity = Binder.clearCallingIdentity(); |
| 2972 | try { |
| 2973 | uid = mPackageManager.getPackageUid(pkg, 0, user.getIdentifier()); |
| 2974 | } finally { |
| 2975 | Binder.restoreCallingIdentity(identity); |
| 2976 | } |
| 2977 | return uid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2978 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 2979 | |
| 2980 | @Override |
| 2981 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 2982 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) |
| 2983 | throws RemoteException { |
| 2984 | new ShellCmd().exec(this, in, out, err, args, callback, resultReceiver); |
| 2985 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2986 | }; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2987 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2988 | private void applyAdjustment(NotificationRecord r, Adjustment adjustment) { |
| 2989 | if (r == null) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2990 | return; |
| 2991 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2992 | if (adjustment.getSignals() != null) { |
| 2993 | Bundle.setDefusable(adjustment.getSignals(), true); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2994 | r.addAdjustment(adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2995 | } |
| 2996 | } |
| 2997 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2998 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 2999 | void addAutogroupKeyLocked(String key) { |
| 3000 | NotificationRecord r = mNotificationsByKey.get(key); |
| 3001 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3002 | return; |
| 3003 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3004 | if (r.sbn.getOverrideGroupKey() == null) { |
| 3005 | addAutoGroupAdjustment(r, GroupHelper.AUTOGROUP_KEY); |
| 3006 | EventLogTags.writeNotificationAutogrouped(key); |
| 3007 | mRankingHandler.requestSort(); |
| 3008 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3009 | } |
| 3010 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3011 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3012 | void removeAutogroupKeyLocked(String key) { |
| 3013 | NotificationRecord r = mNotificationsByKey.get(key); |
| 3014 | if (r == null) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3015 | return; |
| 3016 | } |
Julia Reynolds | 5171071 | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 3017 | if (r.sbn.getOverrideGroupKey() != null) { |
| 3018 | addAutoGroupAdjustment(r, null); |
| 3019 | EventLogTags.writeNotificationUnautogrouped(key); |
| 3020 | mRankingHandler.requestSort(); |
| 3021 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3022 | } |
| 3023 | |
| 3024 | private void addAutoGroupAdjustment(NotificationRecord r, String overrideGroupKey) { |
| 3025 | Bundle signals = new Bundle(); |
| 3026 | signals.putString(Adjustment.KEY_GROUP_KEY, overrideGroupKey); |
| 3027 | Adjustment adjustment = |
| 3028 | new Adjustment(r.sbn.getPackageName(), r.getKey(), signals, "", r.sbn.getUserId()); |
| 3029 | r.addAdjustment(adjustment); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | // Clears the 'fake' auto-group summary. |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3033 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3034 | private void clearAutogroupSummaryLocked(int userId, String pkg) { |
| 3035 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 3036 | if (summaries != null && summaries.containsKey(pkg)) { |
| 3037 | // Clear summary. |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3038 | final NotificationRecord removed = findNotificationByKeyLocked(summaries.remove(pkg)); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3039 | if (removed != null) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 3040 | boolean wasPosted = removeFromNotificationListsLocked(removed); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 3041 | cancelNotificationLocked(removed, false, REASON_UNAUTOBUNDLED, wasPosted, null); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3042 | } |
| 3043 | } |
| 3044 | } |
| 3045 | |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 3046 | @GuardedBy("mNotificationLock") |
| 3047 | private boolean hasAutoGroupSummaryLocked(StatusBarNotification sbn) { |
| 3048 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(sbn.getUserId()); |
| 3049 | return summaries != null && summaries.containsKey(sbn.getPackageName()); |
| 3050 | } |
| 3051 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3052 | // 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] | 3053 | private void createAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 3054 | NotificationRecord summaryRecord = null; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3055 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3056 | NotificationRecord notificationRecord = mNotificationsByKey.get(triggeringKey); |
| 3057 | if (notificationRecord == null) { |
| 3058 | // The notification could have been cancelled again already. A successive |
| 3059 | // adjustment will post a summary if needed. |
| 3060 | return; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3061 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3062 | final StatusBarNotification adjustedSbn = notificationRecord.sbn; |
| 3063 | userId = adjustedSbn.getUser().getIdentifier(); |
| 3064 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 3065 | if (summaries == null) { |
| 3066 | summaries = new ArrayMap<>(); |
| 3067 | } |
| 3068 | mAutobundledSummaries.put(userId, summaries); |
| 3069 | if (!summaries.containsKey(pkg)) { |
| 3070 | // Add summary |
| 3071 | final ApplicationInfo appInfo = |
| 3072 | adjustedSbn.getNotification().extras.getParcelable( |
| 3073 | Notification.EXTRA_BUILDER_APPLICATION_INFO); |
| 3074 | final Bundle extras = new Bundle(); |
| 3075 | extras.putParcelable(Notification.EXTRA_BUILDER_APPLICATION_INFO, appInfo); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3076 | final String channelId = notificationRecord.getChannel().getId(); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3077 | final Notification summaryNotification = |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3078 | new Notification.Builder(getContext(), channelId) |
| 3079 | .setSmallIcon(adjustedSbn.getNotification().getSmallIcon()) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3080 | .setGroupSummary(true) |
Julia Reynolds | 9d5786e | 2017-04-28 10:26:32 -0400 | [diff] [blame] | 3081 | .setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3082 | .setGroup(GroupHelper.AUTOGROUP_KEY) |
| 3083 | .setFlag(Notification.FLAG_AUTOGROUP_SUMMARY, true) |
| 3084 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 3085 | .setColor(adjustedSbn.getNotification().color) |
| 3086 | .setLocalOnly(true) |
| 3087 | .build(); |
| 3088 | summaryNotification.extras.putAll(extras); |
| 3089 | Intent appIntent = getContext().getPackageManager().getLaunchIntentForPackage(pkg); |
| 3090 | if (appIntent != null) { |
| 3091 | summaryNotification.contentIntent = PendingIntent.getActivityAsUser( |
| 3092 | getContext(), 0, appIntent, 0, null, UserHandle.of(userId)); |
| 3093 | } |
| 3094 | final StatusBarNotification summarySbn = |
| 3095 | new StatusBarNotification(adjustedSbn.getPackageName(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3096 | adjustedSbn.getOpPkg(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3097 | Integer.MAX_VALUE, |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3098 | GroupHelper.AUTOGROUP_KEY, adjustedSbn.getUid(), |
| 3099 | adjustedSbn.getInitialPid(), summaryNotification, |
| 3100 | adjustedSbn.getUser(), GroupHelper.AUTOGROUP_KEY, |
| 3101 | System.currentTimeMillis()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 3102 | summaryRecord = new NotificationRecord(getContext(), summarySbn, |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 3103 | notificationRecord.getChannel()); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3104 | summaries.put(pkg, summarySbn.getKey()); |
| 3105 | } |
| 3106 | } |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3107 | if (summaryRecord != null && checkDisqualifyingFeatures(userId, MY_UID, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3108 | summaryRecord.sbn.getId(), summaryRecord.sbn.getTag(), summaryRecord, true)) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3109 | mHandler.post(new EnqueueNotificationRunnable(userId, summaryRecord)); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3110 | } |
| 3111 | } |
| 3112 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3113 | private String disableNotificationEffects(NotificationRecord record) { |
| 3114 | if (mDisableNotificationEffects) { |
| 3115 | return "booleanState"; |
| 3116 | } |
| 3117 | if ((mListenerHints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 3118 | return "listenerHints"; |
| 3119 | } |
| 3120 | if (mCallState != TelephonyManager.CALL_STATE_IDLE && !mZenModeHelper.isCall(record)) { |
| 3121 | return "callState"; |
| 3122 | } |
| 3123 | return null; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3124 | }; |
| 3125 | |
| 3126 | private void dumpJson(PrintWriter pw, DumpFilter filter) { |
| 3127 | JSONObject dump = new JSONObject(); |
| 3128 | try { |
| 3129 | dump.put("service", "Notification Manager"); |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 3130 | dump.put("bans", mRankingHelper.dumpBansJson(filter)); |
| 3131 | dump.put("ranking", mRankingHelper.dumpJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3132 | dump.put("stats", mUsageStats.dumpJson(filter)); |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 3133 | dump.put("channels", mRankingHelper.dumpChannelsJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3134 | } catch (JSONException e) { |
| 3135 | e.printStackTrace(); |
| 3136 | } |
| 3137 | pw.println(dump); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3138 | } |
| 3139 | |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3140 | private void dumpProto(FileDescriptor fd, DumpFilter filter) { |
| 3141 | final ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 3142 | synchronized (mNotificationLock) { |
| 3143 | long records = proto.start(NotificationServiceDumpProto.RECORDS); |
| 3144 | int N = mNotificationList.size(); |
| 3145 | if (N > 0) { |
| 3146 | for (int i = 0; i < N; i++) { |
| 3147 | final NotificationRecord nr = mNotificationList.get(i); |
| 3148 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3149 | nr.dump(proto, filter.redact); |
| 3150 | proto.write(NotificationRecordProto.STATE, NotificationServiceProto.POSTED); |
| 3151 | } |
| 3152 | } |
| 3153 | N = mEnqueuedNotifications.size(); |
| 3154 | if (N > 0) { |
| 3155 | for (int i = 0; i < N; i++) { |
| 3156 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 3157 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3158 | nr.dump(proto, filter.redact); |
| 3159 | proto.write(NotificationRecordProto.STATE, NotificationServiceProto.ENQUEUED); |
| 3160 | } |
| 3161 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3162 | List<NotificationRecord> snoozed = mSnoozeHelper.getSnoozed(); |
| 3163 | N = snoozed.size(); |
| 3164 | if (N > 0) { |
| 3165 | for (int i = 0; i < N; i++) { |
| 3166 | final NotificationRecord nr = snoozed.get(i); |
| 3167 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3168 | nr.dump(proto, filter.redact); |
| 3169 | proto.write(NotificationRecordProto.STATE, NotificationServiceProto.SNOOZED); |
| 3170 | } |
| 3171 | } |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3172 | proto.end(records); |
| 3173 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3174 | |
| 3175 | long zenLog = proto.start(NotificationServiceDumpProto.ZEN); |
| 3176 | mZenModeHelper.dump(proto); |
| 3177 | for (ComponentName suppressor : mEffectsSuppressors) { |
| 3178 | proto.write(ZenModeProto.SUPPRESSORS, suppressor.toString()); |
| 3179 | } |
| 3180 | proto.end(zenLog); |
| 3181 | |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 3182 | proto.flush(); |
| 3183 | } |
| 3184 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3185 | void dumpImpl(PrintWriter pw, DumpFilter filter) { |
| 3186 | pw.print("Current Notification Manager state"); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3187 | if (filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3188 | pw.print(" (filtered to "); pw.print(filter); pw.print(")"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3189 | } |
| 3190 | pw.println(':'); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3191 | int N; |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3192 | final boolean zenOnly = filter.filtered && filter.zen; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3193 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3194 | if (!zenOnly) { |
| 3195 | synchronized (mToastQueue) { |
| 3196 | N = mToastQueue.size(); |
| 3197 | if (N > 0) { |
| 3198 | pw.println(" Toast Queue:"); |
| 3199 | for (int i=0; i<N; i++) { |
| 3200 | mToastQueue.get(i).dump(pw, " ", filter); |
| 3201 | } |
| 3202 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3203 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3204 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3205 | } |
| 3206 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3207 | synchronized (mNotificationLock) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3208 | if (!zenOnly) { |
| 3209 | N = mNotificationList.size(); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3210 | if (N > 0) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3211 | pw.println(" Notification List:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3212 | for (int i=0; i<N; i++) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3213 | final NotificationRecord nr = mNotificationList.get(i); |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3214 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3215 | nr.dump(pw, " ", getContext(), filter.redact); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3216 | } |
| 3217 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3218 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3219 | |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3220 | if (!filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3221 | N = mLights.size(); |
| 3222 | if (N > 0) { |
| 3223 | pw.println(" Lights List:"); |
| 3224 | for (int i=0; i<N; i++) { |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3225 | if (i == N - 1) { |
| 3226 | pw.print(" > "); |
| 3227 | } else { |
| 3228 | pw.print(" "); |
| 3229 | } |
| 3230 | pw.println(mLights.get(i)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3231 | } |
| 3232 | pw.println(" "); |
| 3233 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 3234 | pw.println(" mUseAttentionLight=" + mUseAttentionLight); |
| 3235 | pw.println(" mNotificationPulseEnabled=" + mNotificationPulseEnabled); |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3236 | pw.println(" mSoundNotificationKey=" + mSoundNotificationKey); |
| 3237 | pw.println(" mVibrateNotificationKey=" + mVibrateNotificationKey); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3238 | pw.println(" mDisableNotificationEffects=" + mDisableNotificationEffects); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3239 | pw.println(" mCallState=" + callStateToString(mCallState)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3240 | pw.println(" mSystemReady=" + mSystemReady); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 3241 | pw.println(" mMaxPackageEnqueueRate=" + mMaxPackageEnqueueRate); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3242 | } |
| 3243 | pw.println(" mArchive=" + mArchive.toString()); |
| 3244 | Iterator<StatusBarNotification> iter = mArchive.descendingIterator(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3245 | int j=0; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3246 | while (iter.hasNext()) { |
| 3247 | final StatusBarNotification sbn = iter.next(); |
| 3248 | if (filter != null && !filter.matches(sbn)) continue; |
| 3249 | pw.println(" " + sbn); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3250 | if (++j >= 5) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3251 | if (iter.hasNext()) pw.println(" ..."); |
| 3252 | break; |
| 3253 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3254 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3255 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3256 | if (!zenOnly) { |
| 3257 | N = mEnqueuedNotifications.size(); |
| 3258 | if (N > 0) { |
| 3259 | pw.println(" Enqueued Notification List:"); |
| 3260 | for (int i = 0; i < N; i++) { |
| 3261 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 3262 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3263 | nr.dump(pw, " ", getContext(), filter.redact); |
| 3264 | } |
| 3265 | pw.println(" "); |
| 3266 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3267 | |
| 3268 | mSnoozeHelper.dump(pw, filter); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3269 | } |
| 3270 | } |
| 3271 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3272 | if (!zenOnly) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3273 | pw.println("\n Ranking Config:"); |
| 3274 | mRankingHelper.dump(pw, " ", filter); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 3275 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3276 | pw.println("\n Notification listeners:"); |
| 3277 | mListeners.dump(pw, filter); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3278 | pw.print(" mListenerHints: "); pw.println(mListenerHints); |
| 3279 | pw.print(" mListenersDisablingEffects: ("); |
| 3280 | N = mListenersDisablingEffects.size(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3281 | for (int i = 0; i < N; i++) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 3282 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 3283 | if (i > 0) pw.print(';'); |
| 3284 | pw.print("hint[" + hint + "]:"); |
| 3285 | |
| 3286 | final ArraySet<ManagedServiceInfo> listeners = |
| 3287 | mListenersDisablingEffects.valueAt(i); |
| 3288 | final int listenerSize = listeners.size(); |
| 3289 | |
| 3290 | for (int j = 0; j < listenerSize; j++) { |
| 3291 | if (i > 0) pw.print(','); |
| 3292 | final ManagedServiceInfo listener = listeners.valueAt(i); |
| 3293 | pw.print(listener.component); |
| 3294 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3295 | } |
| 3296 | pw.println(')'); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 3297 | pw.println("\n Notification assistant services:"); |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3298 | mAssistants.dump(pw, filter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3299 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3300 | |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3301 | if (!filter.filtered || zenOnly) { |
| 3302 | pw.println("\n Zen Mode:"); |
| 3303 | pw.print(" mInterruptionFilter="); pw.println(mInterruptionFilter); |
| 3304 | mZenModeHelper.dump(pw, " "); |
| 3305 | |
| 3306 | pw.println("\n Zen Log:"); |
| 3307 | ZenLog.dump(pw, " "); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3308 | } |
| 3309 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3310 | pw.println("\n Condition providers:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3311 | mConditionProviders.dump(pw, filter); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3312 | |
| 3313 | pw.println("\n Group summaries:"); |
| 3314 | for (Entry<String, NotificationRecord> entry : mSummaryByGroupKey.entrySet()) { |
| 3315 | NotificationRecord r = entry.getValue(); |
| 3316 | pw.println(" " + entry.getKey() + " -> " + r.getKey()); |
| 3317 | if (mNotificationsByKey.get(r.getKey()) != r) { |
| 3318 | pw.println("!!!!!!LEAK: Record not found in mNotificationsByKey."); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3319 | r.dump(pw, " ", getContext(), filter.redact); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3320 | } |
| 3321 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3322 | |
| 3323 | if (!zenOnly) { |
| 3324 | pw.println("\n Usage Stats:"); |
| 3325 | mUsageStats.dump(pw, " ", filter); |
| 3326 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3327 | } |
| 3328 | } |
| 3329 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3330 | /** |
| 3331 | * The private API only accessible to the system process. |
| 3332 | */ |
| 3333 | private final NotificationManagerInternal mInternalService = new NotificationManagerInternal() { |
| 3334 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3335 | public void enqueueNotification(String pkg, String opPkg, int callingUid, int callingPid, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3336 | String tag, int id, Notification notification, int userId) { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3337 | enqueueNotificationInternal(pkg, opPkg, callingUid, callingPid, tag, id, notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3338 | userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3339 | } |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3340 | |
| 3341 | @Override |
| 3342 | public void removeForegroundServiceFlagFromNotification(String pkg, int notificationId, |
| 3343 | int userId) { |
| 3344 | checkCallerIsSystem(); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3345 | mHandler.post(new Runnable() { |
| 3346 | @Override |
| 3347 | public void run() { |
| 3348 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3349 | removeForegroundServiceFlagByListLocked( |
| 3350 | mEnqueuedNotifications, pkg, notificationId, userId); |
| 3351 | removeForegroundServiceFlagByListLocked( |
| 3352 | mNotificationList, pkg, notificationId, userId); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3353 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3354 | } |
| 3355 | }); |
| 3356 | } |
| 3357 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3358 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3359 | private void removeForegroundServiceFlagByListLocked( |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3360 | ArrayList<NotificationRecord> notificationList, String pkg, int notificationId, |
| 3361 | int userId) { |
| 3362 | NotificationRecord r = findNotificationByListLocked( |
| 3363 | notificationList, pkg, null, notificationId, userId); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3364 | if (r == null) { |
| 3365 | return; |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3366 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3367 | StatusBarNotification sbn = r.sbn; |
| 3368 | // NoMan adds flags FLAG_NO_CLEAR and FLAG_ONGOING_EVENT when it sees |
| 3369 | // FLAG_FOREGROUND_SERVICE. Hence it's not enough to remove |
| 3370 | // FLAG_FOREGROUND_SERVICE, we have to revert to the flags we received |
| 3371 | // initially *and* force remove FLAG_FOREGROUND_SERVICE. |
| 3372 | sbn.getNotification().flags = |
| 3373 | (r.mOriginalFlags & ~Notification.FLAG_FOREGROUND_SERVICE); |
| 3374 | mRankingHelper.sort(mNotificationList); |
| 3375 | mListeners.notifyPostedLocked(sbn, sbn /* oldSbn */); |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3376 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3377 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3378 | |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3379 | void enqueueNotificationInternal(final String pkg, final String opPkg, final int callingUid, |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 3380 | final int callingPid, final String tag, final int id, final Notification notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3381 | int incomingUserId) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 3382 | if (DBG) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3383 | Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id |
| 3384 | + " notification=" + notification); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 3385 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3386 | checkCallerIsSystemOrSameApp(pkg); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3387 | |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 3388 | final int userId = ActivityManager.handleIncomingUser(callingPid, |
| 3389 | callingUid, incomingUserId, true, false, "enqueueNotification", pkg); |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 3390 | final UserHandle user = new UserHandle(userId); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3391 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3392 | if (pkg == null || notification == null) { |
| 3393 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 3394 | + " id=" + id + " notification=" + notification); |
| 3395 | } |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3396 | |
| 3397 | // The system can post notifications for any package, let us resolve that. |
| 3398 | final int notificationUid = resolveNotificationUid(opPkg, callingUid, userId); |
| 3399 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3400 | // Fix the notification as best we can. |
| 3401 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3402 | final ApplicationInfo ai = mPackageManagerClient.getApplicationInfoAsUser( |
Jeff Sharkey | 012bc7b | 2016-04-11 16:30:27 -0600 | [diff] [blame] | 3403 | pkg, PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 3404 | (userId == UserHandle.USER_ALL) ? UserHandle.USER_SYSTEM : userId); |
Julia Reynolds | e071abd | 2017-03-22 10:52:11 -0400 | [diff] [blame] | 3405 | Notification.addFieldsFromContext(ai, notification); |
Julia Reynolds | 4db5955 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3406 | |
| 3407 | int canColorize = mPackageManagerClient.checkPermission( |
| 3408 | android.Manifest.permission.USE_COLORIZED_NOTIFICATIONS, pkg); |
| 3409 | if (canColorize == PERMISSION_GRANTED) { |
| 3410 | notification.flags |= Notification.FLAG_CAN_COLORIZE; |
| 3411 | } else { |
| 3412 | notification.flags &= ~Notification.FLAG_CAN_COLORIZE; |
| 3413 | } |
| 3414 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3415 | } catch (NameNotFoundException e) { |
| 3416 | Slog.e(TAG, "Cannot create a context for sending app", e); |
| 3417 | return; |
| 3418 | } |
| 3419 | |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 3420 | mUsageStats.registerEnqueuedByApp(pkg); |
| 3421 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3422 | // setup local book-keeping |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 3423 | String channelId = notification.getChannelId(); |
| 3424 | if (mIsTelevision && (new Notification.TvExtender(notification)).getChannelId() != null) { |
| 3425 | channelId = (new Notification.TvExtender(notification)).getChannelId(); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 3426 | } |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3427 | final NotificationChannel channel = mRankingHelper.getNotificationChannel(pkg, |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3428 | notificationUid, channelId, false /* includeDeleted */); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3429 | if (channel == null) { |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3430 | final String noChannelStr = "No Channel found for " |
| 3431 | + "pkg=" + pkg |
| 3432 | + ", channelId=" + channelId |
Julia Reynolds | f26eb91 | 2017-05-22 15:47:06 -0400 | [diff] [blame] | 3433 | + ", id=" + id |
| 3434 | + ", tag=" + tag |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3435 | + ", opPkg=" + opPkg |
| 3436 | + ", callingUid=" + callingUid |
| 3437 | + ", userId=" + userId |
| 3438 | + ", incomingUserId=" + incomingUserId |
| 3439 | + ", notificationUid=" + notificationUid |
| 3440 | + ", notification=" + notification; |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3441 | Log.e(TAG, noChannelStr); |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 3442 | doChannelWarningToast("Developer warning for package \"" + pkg + "\"\n" + |
Geoffrey Pitsch | cadb5dc | 2017-04-11 11:35:02 -0400 | [diff] [blame] | 3443 | "Failed to post notification on channel \"" + channelId + "\"\n" + |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3444 | "See log for more details"); |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3445 | return; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3446 | } |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3447 | |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 3448 | final StatusBarNotification n = new StatusBarNotification( |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3449 | pkg, opPkg, id, tag, notificationUid, callingPid, notification, |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3450 | user, null, System.currentTimeMillis()); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 3451 | final NotificationRecord r = new NotificationRecord(getContext(), n, channel); |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 3452 | |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3453 | if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r, |
| 3454 | r.sbn.getOverrideGroupKey() != null)) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3455 | return; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 3456 | } |
| 3457 | |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 3458 | // Whitelist pending intents. |
| 3459 | if (notification.allPendingIntents != null) { |
| 3460 | final int intentCount = notification.allPendingIntents.size(); |
| 3461 | if (intentCount > 0) { |
| 3462 | final ActivityManagerInternal am = LocalServices |
| 3463 | .getService(ActivityManagerInternal.class); |
| 3464 | final long duration = LocalServices.getService( |
| 3465 | DeviceIdleController.LocalService.class).getNotificationWhitelistDuration(); |
| 3466 | for (int i = 0; i < intentCount; i++) { |
| 3467 | PendingIntent pendingIntent = notification.allPendingIntents.valueAt(i); |
| 3468 | if (pendingIntent != null) { |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 3469 | am.setPendingIntentWhitelistDuration(pendingIntent.getTarget(), |
| 3470 | WHITELIST_TOKEN, duration); |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 3471 | } |
| 3472 | } |
| 3473 | } |
| 3474 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 3475 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3476 | mHandler.post(new EnqueueNotificationRunnable(userId, r)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3477 | } |
| 3478 | |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 3479 | private void doChannelWarningToast(CharSequence toastText) { |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 3480 | final int defaultWarningEnabled = Build.IS_DEBUGGABLE ? 1 : 0; |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 3481 | final boolean warningEnabled = Settings.Global.getInt(getContext().getContentResolver(), |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 3482 | Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, defaultWarningEnabled) != 0; |
| 3483 | if (warningEnabled) { |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 3484 | Toast toast = Toast.makeText(getContext(), mHandler.getLooper(), toastText, |
Geoffrey Pitsch | 5bdddbd | 2017-05-26 10:50:05 -0400 | [diff] [blame] | 3485 | Toast.LENGTH_SHORT); |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 3486 | toast.show(); |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3487 | } |
| 3488 | } |
| 3489 | |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3490 | private int resolveNotificationUid(String opPackageName, int callingUid, int userId) { |
| 3491 | // The system can post notifications on behalf of any package it wants |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3492 | if (isCallerSystemOrPhone() && opPackageName != null && !"android".equals(opPackageName)) { |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3493 | try { |
| 3494 | return getContext().getPackageManager() |
| 3495 | .getPackageUidAsUser(opPackageName, userId); |
| 3496 | } catch (NameNotFoundException e) { |
| 3497 | /* ignore */ |
| 3498 | } |
| 3499 | } |
| 3500 | return callingUid; |
| 3501 | } |
| 3502 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3503 | /** |
| 3504 | * Checks if a notification can be posted. checks rate limiter, snooze helper, and blocking. |
| 3505 | * |
| 3506 | * Has side effects. |
| 3507 | */ |
| 3508 | private boolean checkDisqualifyingFeatures(int userId, int callingUid, int id, String tag, |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3509 | NotificationRecord r, boolean isAutogroup) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3510 | final String pkg = r.sbn.getPackageName(); |
Sailesh Nepal | e8bde70 | 2017-07-21 11:44:04 -0700 | [diff] [blame] | 3511 | final String dialerPackage = |
| 3512 | getContext().getSystemService(TelecomManager.class).getSystemDialerPackage(); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3513 | final boolean isSystemNotification = |
Sailesh Nepal | e8bde70 | 2017-07-21 11:44:04 -0700 | [diff] [blame] | 3514 | isUidSystemOrPhone(callingUid) || ("android".equals(pkg)) |
| 3515 | || TextUtils.equals(pkg, dialerPackage); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3516 | final boolean isNotificationFromListener = mListeners.isListenerPackage(pkg); |
| 3517 | |
| 3518 | // Limit the number of notifications that any given package except the android |
| 3519 | // package or a registered listener can enqueue. Prevents DOS attacks and deals with leaks. |
| 3520 | if (!isSystemNotification && !isNotificationFromListener) { |
| 3521 | synchronized (mNotificationLock) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3522 | if (mNotificationsByKey.get(r.sbn.getKey()) == null && isCallerInstantApp(pkg)) { |
| 3523 | // Ephemeral apps have some special constraints for notifications. |
| 3524 | // They are not allowed to create new notifications however they are allowed to |
| 3525 | // update notifications created by the system (e.g. a foreground service |
| 3526 | // notification). |
| 3527 | throw new SecurityException("Instant app " + pkg |
| 3528 | + " cannot create notifications"); |
| 3529 | } |
| 3530 | |
| 3531 | // rate limit updates that aren't completed progress notifications |
| 3532 | if (mNotificationsByKey.get(r.sbn.getKey()) != null |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3533 | && !r.getNotification().hasCompletedProgress() |
| 3534 | && !isAutogroup) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3535 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3536 | final float appEnqueueRate = mUsageStats.getAppEnqueueRate(pkg); |
| 3537 | if (appEnqueueRate > mMaxPackageEnqueueRate) { |
| 3538 | mUsageStats.registerOverRateQuota(pkg); |
| 3539 | final long now = SystemClock.elapsedRealtime(); |
| 3540 | if ((now - mLastOverRateLogTime) > MIN_PACKAGE_OVERRATE_LOG_INTERVAL) { |
| 3541 | Slog.e(TAG, "Package enqueue rate is " + appEnqueueRate |
Julia Reynolds | 5e70219 | 2017-08-18 09:22:40 -0400 | [diff] [blame] | 3542 | + ". Shedding " + r.sbn.getKey() + ". package=" + pkg); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3543 | mLastOverRateLogTime = now; |
| 3544 | } |
| 3545 | return false; |
| 3546 | } |
| 3547 | } |
| 3548 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3549 | // limit the number of outstanding notificationrecords an app can have |
| 3550 | int count = getNotificationCountLocked(pkg, userId, id, tag); |
| 3551 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 3552 | mUsageStats.registerOverCountQuota(pkg); |
| 3553 | Slog.e(TAG, "Package has already posted or enqueued " + count |
| 3554 | + " notifications. Not showing more. package=" + pkg); |
| 3555 | return false; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3556 | } |
| 3557 | } |
| 3558 | } |
| 3559 | |
| 3560 | // snoozed apps |
| 3561 | if (mSnoozeHelper.isSnoozed(userId, pkg, r.getKey())) { |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3562 | MetricsLogger.action(r.getLogMaker() |
| 3563 | .setType(MetricsProto.MetricsEvent.TYPE_UPDATE) |
| 3564 | .setCategory(MetricsProto.MetricsEvent.NOTIFICATION_SNOOZED)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3565 | if (DBG) { |
| 3566 | Slog.d(TAG, "Ignored enqueue for snoozed notification " + r.getKey()); |
| 3567 | } |
| 3568 | mSnoozeHelper.update(userId, r); |
| 3569 | savePolicyFile(); |
| 3570 | return false; |
| 3571 | } |
| 3572 | |
| 3573 | |
| 3574 | // blocked apps |
| 3575 | if (isBlocked(r, mUsageStats)) { |
| 3576 | return false; |
| 3577 | } |
| 3578 | |
| 3579 | return true; |
| 3580 | } |
| 3581 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3582 | protected int getNotificationCountLocked(String pkg, int userId, int excludedId, |
| 3583 | String excludedTag) { |
| 3584 | int count = 0; |
| 3585 | final int N = mNotificationList.size(); |
| 3586 | for (int i = 0; i < N; i++) { |
| 3587 | final NotificationRecord existing = mNotificationList.get(i); |
| 3588 | if (existing.sbn.getPackageName().equals(pkg) |
| 3589 | && existing.sbn.getUserId() == userId) { |
| 3590 | if (existing.sbn.getId() == excludedId |
| 3591 | && TextUtils.equals(existing.sbn.getTag(), excludedTag)) { |
| 3592 | continue; |
| 3593 | } |
| 3594 | count++; |
| 3595 | } |
| 3596 | } |
| 3597 | final int M = mEnqueuedNotifications.size(); |
| 3598 | for (int i = 0; i < M; i++) { |
| 3599 | final NotificationRecord existing = mEnqueuedNotifications.get(i); |
| 3600 | if (existing.sbn.getPackageName().equals(pkg) |
| 3601 | && existing.sbn.getUserId() == userId) { |
| 3602 | count++; |
| 3603 | } |
| 3604 | } |
| 3605 | return count; |
| 3606 | } |
| 3607 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3608 | protected boolean isBlocked(NotificationRecord r, NotificationUsageStats usageStats) { |
| 3609 | final String pkg = r.sbn.getPackageName(); |
| 3610 | final int callingUid = r.sbn.getUid(); |
| 3611 | |
| 3612 | final boolean isPackageSuspended = isPackageSuspendedForUser(pkg, callingUid); |
| 3613 | if (isPackageSuspended) { |
| 3614 | Slog.e(TAG, "Suppressing notification from package due to package " |
| 3615 | + "suspended by administrator."); |
| 3616 | usageStats.registerSuspendedByAdmin(r); |
| 3617 | return isPackageSuspended; |
| 3618 | } |
| 3619 | |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 3620 | final boolean isBlocked = |
| 3621 | mRankingHelper.getImportance(pkg, callingUid) == NotificationManager.IMPORTANCE_NONE |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 3622 | || r.getChannel().getImportance() == NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3623 | if (isBlocked) { |
| 3624 | Slog.e(TAG, "Suppressing notification from package by user request."); |
| 3625 | usageStats.registerBlocked(r); |
| 3626 | } |
| 3627 | return isBlocked; |
| 3628 | } |
| 3629 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3630 | protected class SnoozeNotificationRunnable implements Runnable { |
| 3631 | private final String mKey; |
| 3632 | private final long mDuration; |
| 3633 | private final String mSnoozeCriterionId; |
| 3634 | |
| 3635 | SnoozeNotificationRunnable(String key, long duration, String snoozeCriterionId) { |
| 3636 | mKey = key; |
| 3637 | mDuration = duration; |
| 3638 | mSnoozeCriterionId = snoozeCriterionId; |
| 3639 | } |
| 3640 | |
| 3641 | @Override |
| 3642 | public void run() { |
| 3643 | synchronized (mNotificationLock) { |
| 3644 | final NotificationRecord r = findNotificationByKeyLocked(mKey); |
| 3645 | if (r != null) { |
| 3646 | snoozeLocked(r); |
| 3647 | } |
| 3648 | } |
| 3649 | } |
| 3650 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3651 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3652 | void snoozeLocked(NotificationRecord r) { |
| 3653 | if (r.sbn.isGroup()) { |
| 3654 | final List<NotificationRecord> groupNotifications = findGroupNotificationsLocked( |
| 3655 | r.sbn.getPackageName(), r.sbn.getGroupKey(), r.sbn.getUserId()); |
| 3656 | if (r.getNotification().isGroupSummary()) { |
| 3657 | // snooze summary and all children |
| 3658 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 3659 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 3660 | } |
| 3661 | } else { |
| 3662 | // if there is a valid summary for this group, and we are snoozing the only |
| 3663 | // child, also snooze the summary |
| 3664 | if (mSummaryByGroupKey.containsKey(r.sbn.getGroupKey())) { |
| 3665 | if (groupNotifications.size() != 2) { |
| 3666 | snoozeNotificationLocked(r); |
| 3667 | } else { |
| 3668 | // snooze summary and the one child |
| 3669 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 3670 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 3671 | } |
| 3672 | } |
| 3673 | } else { |
| 3674 | snoozeNotificationLocked(r); |
| 3675 | } |
| 3676 | } |
| 3677 | } else { |
| 3678 | // just snooze the one notification |
| 3679 | snoozeNotificationLocked(r); |
| 3680 | } |
| 3681 | } |
| 3682 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3683 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3684 | void snoozeNotificationLocked(NotificationRecord r) { |
| 3685 | MetricsLogger.action(r.getLogMaker() |
| 3686 | .setCategory(MetricsEvent.NOTIFICATION_SNOOZED) |
| 3687 | .setType(MetricsEvent.TYPE_CLOSE) |
| 3688 | .addTaggedData(MetricsEvent.NOTIFICATION_SNOOZED_CRITERIA, |
| 3689 | mSnoozeCriterionId == null ? 0 : 1)); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 3690 | boolean wasPosted = removeFromNotificationListsLocked(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 3691 | cancelNotificationLocked(r, false, REASON_SNOOZED, wasPosted, null); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3692 | updateLightsLocked(); |
| 3693 | if (mSnoozeCriterionId != null) { |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3694 | mAssistants.notifyAssistantSnoozedLocked(r.sbn, mSnoozeCriterionId); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3695 | mSnoozeHelper.snooze(r); |
| 3696 | } else { |
| 3697 | mSnoozeHelper.snooze(r, mDuration); |
| 3698 | } |
| 3699 | savePolicyFile(); |
| 3700 | } |
| 3701 | } |
| 3702 | |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 3703 | protected class EnqueueNotificationRunnable implements Runnable { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3704 | private final NotificationRecord r; |
| 3705 | private final int userId; |
| 3706 | |
| 3707 | EnqueueNotificationRunnable(int userId, NotificationRecord r) { |
| 3708 | this.userId = userId; |
| 3709 | this.r = r; |
| 3710 | }; |
| 3711 | |
| 3712 | @Override |
| 3713 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3714 | synchronized (mNotificationLock) { |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 3715 | mEnqueuedNotifications.add(r); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 3716 | scheduleTimeoutLocked(r); |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 3717 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3718 | final StatusBarNotification n = r.sbn; |
| 3719 | if (DBG) Slog.d(TAG, "EnqueueNotificationRunnable.run for: " + n.getKey()); |
| 3720 | NotificationRecord old = mNotificationsByKey.get(n.getKey()); |
| 3721 | if (old != null) { |
| 3722 | // Retain ranking information from previous record |
| 3723 | r.copyRankingInformation(old); |
| 3724 | } |
| 3725 | |
| 3726 | final int callingUid = n.getUid(); |
| 3727 | final int callingPid = n.getInitialPid(); |
| 3728 | final Notification notification = n.getNotification(); |
| 3729 | final String pkg = n.getPackageName(); |
| 3730 | final int id = n.getId(); |
| 3731 | final String tag = n.getTag(); |
| 3732 | |
| 3733 | // Handle grouped notifications and bail out early if we |
| 3734 | // can to avoid extracting signals. |
| 3735 | handleGroupedNotificationLocked(r, old, callingUid, callingPid); |
| 3736 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3737 | // if this is a group child, unsnooze parent summary |
| 3738 | if (n.isGroup() && notification.isGroupChild()) { |
| 3739 | mSnoozeHelper.repostGroupSummary(pkg, r.getUserId(), n.getGroupKey()); |
| 3740 | } |
| 3741 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3742 | // This conditional is a dirty hack to limit the logging done on |
| 3743 | // behalf of the download manager without affecting other apps. |
| 3744 | if (!pkg.equals("com.android.providers.downloads") |
| 3745 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| 3746 | int enqueueStatus = EVENTLOG_ENQUEUE_STATUS_NEW; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3747 | if (old != null) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3748 | enqueueStatus = EVENTLOG_ENQUEUE_STATUS_UPDATE; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3749 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3750 | EventLogTags.writeNotificationEnqueue(callingUid, callingPid, |
| 3751 | pkg, id, tag, userId, notification.toString(), |
| 3752 | enqueueStatus); |
| 3753 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3754 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3755 | mRankingHelper.extractSignals(r); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3756 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3757 | // tell the assistant service about the notification |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 3758 | if (mAssistants.isEnabled()) { |
| 3759 | mAssistants.onNotificationEnqueued(r); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3760 | mHandler.postDelayed(new PostNotificationRunnable(r.getKey()), |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3761 | DELAY_FOR_ASSISTANT_TIME); |
| 3762 | } else { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3763 | mHandler.post(new PostNotificationRunnable(r.getKey())); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3764 | } |
| 3765 | } |
| 3766 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3767 | } |
| 3768 | |
| 3769 | protected class PostNotificationRunnable implements Runnable { |
| 3770 | private final String key; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3771 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3772 | PostNotificationRunnable(String key) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3773 | this.key = key; |
| 3774 | } |
| 3775 | |
| 3776 | @Override |
| 3777 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3778 | synchronized (mNotificationLock) { |
| 3779 | try { |
| 3780 | NotificationRecord r = null; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3781 | int N = mEnqueuedNotifications.size(); |
| 3782 | for (int i = 0; i < N; i++) { |
| 3783 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 3784 | if (Objects.equals(key, enqueued.getKey())) { |
| 3785 | r = enqueued; |
| 3786 | break; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3787 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3788 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3789 | if (r == null) { |
| 3790 | Slog.i(TAG, "Cannot find enqueued record for key: " + key); |
| 3791 | return; |
| 3792 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3793 | NotificationRecord old = mNotificationsByKey.get(key); |
| 3794 | final StatusBarNotification n = r.sbn; |
| 3795 | final Notification notification = n.getNotification(); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3796 | int index = indexOfNotificationLocked(n.getKey()); |
| 3797 | if (index < 0) { |
| 3798 | mNotificationList.add(r); |
| 3799 | mUsageStats.registerPostedByApp(r); |
| 3800 | } else { |
| 3801 | old = mNotificationList.get(index); |
| 3802 | mNotificationList.set(index, r); |
| 3803 | mUsageStats.registerUpdatedByApp(r, old); |
| 3804 | // Make sure we don't lose the foreground service state. |
| 3805 | notification.flags |= |
| 3806 | old.getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE; |
| 3807 | r.isUpdate = true; |
| 3808 | } |
| 3809 | |
| 3810 | mNotificationsByKey.put(n.getKey(), r); |
| 3811 | |
| 3812 | // Ensure if this is a foreground service that the proper additional |
| 3813 | // flags are set. |
| 3814 | if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 3815 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 3816 | | Notification.FLAG_NO_CLEAR; |
| 3817 | } |
| 3818 | |
| 3819 | applyZenModeLocked(r); |
| 3820 | mRankingHelper.sort(mNotificationList); |
| 3821 | |
| 3822 | if (notification.getSmallIcon() != null) { |
| 3823 | StatusBarNotification oldSbn = (old != null) ? old.sbn : null; |
| 3824 | mListeners.notifyPostedLocked(n, oldSbn); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3825 | if (oldSbn == null || !Objects.equals(oldSbn.getGroup(), n.getGroup())) { |
| 3826 | mHandler.post(new Runnable() { |
| 3827 | @Override |
| 3828 | public void run() { |
Julia Reynolds | a13b3e2 | 2017-08-10 16:58:54 -0400 | [diff] [blame] | 3829 | mGroupHelper.onNotificationPosted( |
| 3830 | n, hasAutoGroupSummaryLocked(n)); |
Julia Reynolds | 8aebf35 | 2017-06-26 11:35:33 -0400 | [diff] [blame] | 3831 | } |
| 3832 | }); |
| 3833 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3834 | } else { |
| 3835 | Slog.e(TAG, "Not posting notification without small icon: " + notification); |
| 3836 | if (old != null && !old.isCanceled) { |
| 3837 | mListeners.notifyRemovedLocked(n, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 3838 | NotificationListenerService.REASON_ERROR); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3839 | mHandler.post(new Runnable() { |
| 3840 | @Override |
| 3841 | public void run() { |
| 3842 | mGroupHelper.onNotificationRemoved(n); |
| 3843 | } |
| 3844 | }); |
| 3845 | } |
| 3846 | // ATTENTION: in a future release we will bail out here |
| 3847 | // so that we do not play sounds, show lights, etc. for invalid |
| 3848 | // notifications |
| 3849 | Slog.e(TAG, "WARNING: In a future release this will crash the app: " |
| 3850 | + n.getPackageName()); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3851 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3852 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3853 | buzzBeepBlinkLocked(r); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3854 | } finally { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3855 | int N = mEnqueuedNotifications.size(); |
| 3856 | for (int i = 0; i < N; i++) { |
| 3857 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 3858 | if (Objects.equals(key, enqueued.getKey())) { |
| 3859 | mEnqueuedNotifications.remove(i); |
| 3860 | break; |
| 3861 | } |
| 3862 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3863 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3864 | } |
| 3865 | } |
| 3866 | } |
| 3867 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3868 | /** |
| 3869 | * Ensures that grouped notification receive their special treatment. |
| 3870 | * |
| 3871 | * <p>Cancels group children if the new notification causes a group to lose |
| 3872 | * its summary.</p> |
| 3873 | * |
| 3874 | * <p>Updates mSummaryByGroupKey.</p> |
| 3875 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3876 | @GuardedBy("mNotificationLock") |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3877 | private void handleGroupedNotificationLocked(NotificationRecord r, NotificationRecord old, |
| 3878 | int callingUid, int callingPid) { |
| 3879 | StatusBarNotification sbn = r.sbn; |
| 3880 | Notification n = sbn.getNotification(); |
Selim Cinek | 5b03ce9 | 2016-05-18 15:16:58 -0700 | [diff] [blame] | 3881 | if (n.isGroupSummary() && !sbn.isAppGroup()) { |
| 3882 | // notifications without a group shouldn't be a summary, otherwise autobundling can |
| 3883 | // lead to bugs |
| 3884 | n.flags &= ~Notification.FLAG_GROUP_SUMMARY; |
| 3885 | } |
| 3886 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3887 | String group = sbn.getGroupKey(); |
| 3888 | boolean isSummary = n.isGroupSummary(); |
| 3889 | |
| 3890 | Notification oldN = old != null ? old.sbn.getNotification() : null; |
| 3891 | String oldGroup = old != null ? old.sbn.getGroupKey() : null; |
| 3892 | boolean oldIsSummary = old != null && oldN.isGroupSummary(); |
| 3893 | |
| 3894 | if (oldIsSummary) { |
| 3895 | NotificationRecord removedSummary = mSummaryByGroupKey.remove(oldGroup); |
| 3896 | if (removedSummary != old) { |
| 3897 | String removedKey = |
| 3898 | removedSummary != null ? removedSummary.getKey() : "<null>"; |
| 3899 | Slog.w(TAG, "Removed summary didn't match old notification: old=" + old.getKey() + |
| 3900 | ", removed=" + removedKey); |
| 3901 | } |
| 3902 | } |
| 3903 | if (isSummary) { |
| 3904 | mSummaryByGroupKey.put(group, r); |
| 3905 | } |
| 3906 | |
| 3907 | // Clear out group children of the old notification if the update |
| 3908 | // causes the group summary to go away. This happens when the old |
| 3909 | // notification was a summary and the new one isn't, or when the old |
| 3910 | // notification was a summary and its group key changed. |
| 3911 | if (oldIsSummary && (!isSummary || !oldGroup.equals(group))) { |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 3912 | cancelGroupChildrenLocked(old, callingUid, callingPid, null, false /* sendDelete */, |
| 3913 | null); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3914 | } |
| 3915 | } |
| 3916 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3917 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3918 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 3919 | void scheduleTimeoutLocked(NotificationRecord record) { |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 3920 | if (record.getNotification().getTimeoutAfter() > 0) { |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 3921 | final PendingIntent pi = PendingIntent.getBroadcast(getContext(), |
| 3922 | REQUEST_CODE_TIMEOUT, |
| 3923 | new Intent(ACTION_NOTIFICATION_TIMEOUT) |
| 3924 | .setData(new Uri.Builder().scheme(SCHEME_TIMEOUT) |
| 3925 | .appendPath(record.getKey()).build()) |
| 3926 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 3927 | .putExtra(EXTRA_KEY, record.getKey()), |
| 3928 | PendingIntent.FLAG_UPDATE_CURRENT); |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 3929 | mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 3930 | SystemClock.elapsedRealtime() + record.getNotification().getTimeoutAfter(), pi); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 3931 | } |
| 3932 | } |
| 3933 | |
| 3934 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3935 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3936 | void buzzBeepBlinkLocked(NotificationRecord record) { |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 3937 | boolean buzz = false; |
| 3938 | boolean beep = false; |
| 3939 | boolean blink = false; |
| 3940 | |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3941 | final Notification notification = record.sbn.getNotification(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3942 | final String key = record.getKey(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3943 | |
| 3944 | // Should this notification make noise, vibe, or use the LED? |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 3945 | final boolean aboveThreshold = |
| 3946 | record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3947 | |
| 3948 | // Remember if this notification already owns the notification channels. |
| 3949 | boolean wasBeep = key != null && key.equals(mSoundNotificationKey); |
| 3950 | boolean wasBuzz = key != null && key.equals(mVibrateNotificationKey); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3951 | // These are set inside the conditional if the notification is allowed to make noise. |
| 3952 | boolean hasValidVibrate = false; |
| 3953 | boolean hasValidSound = false; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3954 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 3955 | if (aboveThreshold && isNotificationForCurrentUser(record)) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3956 | // If the notification will appear in the status bar, it should send an accessibility |
| 3957 | // event |
| 3958 | if (!record.isUpdate && record.getImportance() > IMPORTANCE_MIN) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3959 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3960 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 3961 | if (mSystemReady && mAudioManager != null) { |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3962 | Uri soundUri = record.getSound(); |
| 3963 | hasValidSound = soundUri != null && !Uri.EMPTY.equals(soundUri); |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 3964 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3965 | long[] vibration = record.getVibration(); |
| 3966 | // Demote sound to vibration if vibration missing & phone in vibration mode. |
| 3967 | if (vibration == null |
| 3968 | && hasValidSound |
| 3969 | && (mAudioManager.getRingerModeInternal() |
| 3970 | == AudioManager.RINGER_MODE_VIBRATE)) { |
| 3971 | vibration = mFallbackVibrationPattern; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3972 | } |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3973 | hasValidVibrate = vibration != null; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 3974 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 3975 | boolean hasAudibleAlert = hasValidSound || hasValidVibrate; |
| 3976 | |
| 3977 | if (hasAudibleAlert && !shouldMuteNotificationLocked(record)) { |
| 3978 | if (DBG) Slog.v(TAG, "Interrupting!"); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3979 | if (hasValidSound) { |
| 3980 | mSoundNotificationKey = key; |
| 3981 | if (mInCall) { |
| 3982 | playInCallNotification(); |
| 3983 | beep = true; |
| 3984 | } else { |
| 3985 | beep = playSound(record, soundUri); |
| 3986 | } |
| 3987 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3988 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3989 | final boolean ringerModeSilent = |
| 3990 | mAudioManager.getRingerModeInternal() |
| 3991 | == AudioManager.RINGER_MODE_SILENT; |
| 3992 | if (!mInCall && hasValidVibrate && !ringerModeSilent) { |
| 3993 | mVibrateNotificationKey = key; |
| 3994 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 3995 | buzz = playVibration(record, vibration, hasValidSound); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3996 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3997 | } |
| 3998 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3999 | } |
| 4000 | // If a notification is updated to remove the actively playing sound or vibrate, |
| 4001 | // cancel that feedback now |
| 4002 | if (wasBeep && !hasValidSound) { |
| 4003 | clearSoundLocked(); |
| 4004 | } |
| 4005 | if (wasBuzz && !hasValidVibrate) { |
| 4006 | clearVibrateLocked(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4007 | } |
| 4008 | |
| 4009 | // light |
| 4010 | // release the light |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4011 | boolean wasShowLights = mLights.remove(key); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 4012 | if (record.getLight() != null && aboveThreshold |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4013 | && ((record.getSuppressedVisualEffects() |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 4014 | & NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF) == 0)) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 4015 | mLights.add(key); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4016 | updateLightsLocked(); |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 4017 | if (mUseAttentionLight) { |
| 4018 | mAttentionLight.pulse(); |
| 4019 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4020 | blink = true; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4021 | } else if (wasShowLights) { |
| 4022 | updateLightsLocked(); |
| 4023 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 4024 | if (buzz || beep || blink) { |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 4025 | MetricsLogger.action(record.getLogMaker() |
| 4026 | .setCategory(MetricsEvent.NOTIFICATION_ALERT) |
| 4027 | .setType(MetricsEvent.TYPE_OPEN) |
| 4028 | .setSubtype((buzz ? 1 : 0) | (beep ? 2 : 0) | (blink ? 4 : 0))); |
| 4029 | EventLogTags.writeNotificationAlert(key, buzz ? 1 : 0, beep ? 1 : 0, blink ? 1 : 0); |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 4030 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4031 | } |
| 4032 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4033 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4034 | boolean shouldMuteNotificationLocked(final NotificationRecord record) { |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4035 | // Suppressed because it's a silent update |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4036 | final Notification notification = record.getNotification(); |
| 4037 | if(record.isUpdate |
| 4038 | && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) { |
| 4039 | return true; |
| 4040 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4041 | |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4042 | // muted by listener |
| 4043 | final String disableEffects = disableNotificationEffects(record); |
| 4044 | if (disableEffects != null) { |
| 4045 | ZenLog.traceDisableEffects(record, disableEffects); |
| 4046 | return true; |
| 4047 | } |
| 4048 | |
| 4049 | // suppressed due to DND |
| 4050 | if (record.isIntercepted()) { |
| 4051 | return true; |
| 4052 | } |
| 4053 | |
| 4054 | // Suppressed because another notification in its group handles alerting |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4055 | if (record.sbn.isGroup()) { |
Julia Reynolds | 3020315 | 2017-05-26 13:36:31 -0400 | [diff] [blame] | 4056 | return notification.suppressAlertingDueToGrouping(); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4057 | } |
Julia Reynolds | 76c096d | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 4058 | |
Julia Reynolds | 65b85cf | 2017-07-20 09:19:20 -0400 | [diff] [blame] | 4059 | // Suppressed for being too recently noisy |
| 4060 | final String pkg = record.sbn.getPackageName(); |
| 4061 | if (mUsageStats.isAlertRateLimited(pkg)) { |
| 4062 | Slog.e(TAG, "Muting recently noisy " + record.getKey()); |
| 4063 | return true; |
| 4064 | } |
| 4065 | |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 4066 | return false; |
| 4067 | } |
| 4068 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 4069 | private boolean playSound(final NotificationRecord record, Uri soundUri) { |
| 4070 | boolean looping = (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 4071 | // 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] | 4072 | // or the device is in vibrate mode |
| 4073 | if (!mAudioManager.isAudioFocusExclusive() && mAudioManager.getRingerModeInternal() |
| 4074 | != AudioManager.RINGER_MODE_VIBRATE) { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 4075 | final long identity = Binder.clearCallingIdentity(); |
| 4076 | try { |
| 4077 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4078 | if (player != null) { |
| 4079 | if (DBG) Slog.v(TAG, "Playing sound " + soundUri |
| 4080 | + " with attributes " + record.getAudioAttributes()); |
| 4081 | player.playAsync(soundUri, record.sbn.getUser(), looping, |
| 4082 | record.getAudioAttributes()); |
| 4083 | return true; |
| 4084 | } |
| 4085 | } catch (RemoteException e) { |
| 4086 | } finally { |
| 4087 | Binder.restoreCallingIdentity(identity); |
| 4088 | } |
| 4089 | } |
| 4090 | return false; |
| 4091 | } |
| 4092 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4093 | private boolean playVibration(final NotificationRecord record, long[] vibration, |
| 4094 | boolean delayVibForSound) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 4095 | // Escalate privileges so we can use the vibrator even if the |
| 4096 | // notifying app does not have the VIBRATE permission. |
| 4097 | long identity = Binder.clearCallingIdentity(); |
| 4098 | try { |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 4099 | final VibrationEffect effect; |
| 4100 | try { |
| 4101 | final boolean insistent = |
| 4102 | (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 4103 | effect = VibrationEffect.createWaveform( |
| 4104 | vibration, insistent ? 0 : -1 /*repeatIndex*/); |
| 4105 | } catch (IllegalArgumentException e) { |
| 4106 | Slog.e(TAG, "Error creating vibration waveform with pattern: " + |
| 4107 | Arrays.toString(vibration)); |
| 4108 | return false; |
| 4109 | } |
| 4110 | if (delayVibForSound) { |
| 4111 | new Thread(() -> { |
| 4112 | // delay the vibration by the same amount as the notification sound |
| 4113 | final int waitMs = mAudioManager.getFocusRampTimeMs( |
| 4114 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 4115 | record.getAudioAttributes()); |
| 4116 | if (DBG) Slog.v(TAG, "Delaying vibration by " + waitMs + "ms"); |
| 4117 | try { |
| 4118 | Thread.sleep(waitMs); |
| 4119 | } catch (InterruptedException e) { } |
| 4120 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 4121 | effect, record.getAudioAttributes()); |
| 4122 | }).start(); |
| 4123 | } else { |
| 4124 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 4125 | effect, record.getAudioAttributes()); |
| 4126 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 4127 | return true; |
| 4128 | } finally{ |
| 4129 | Binder.restoreCallingIdentity(identity); |
| 4130 | } |
| 4131 | } |
| 4132 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 4133 | private boolean isNotificationForCurrentUser(NotificationRecord record) { |
| 4134 | final int currentUser; |
| 4135 | final long token = Binder.clearCallingIdentity(); |
| 4136 | try { |
| 4137 | currentUser = ActivityManager.getCurrentUser(); |
| 4138 | } finally { |
| 4139 | Binder.restoreCallingIdentity(token); |
| 4140 | } |
| 4141 | return (record.getUserId() == UserHandle.USER_ALL || |
| 4142 | record.getUserId() == currentUser || |
| 4143 | mUserProfiles.isCurrentProfile(record.getUserId())); |
| 4144 | } |
| 4145 | |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4146 | protected void playInCallNotification() { |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4147 | new Thread() { |
| 4148 | @Override |
| 4149 | public void run() { |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4150 | final long identity = Binder.clearCallingIdentity(); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4151 | try { |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4152 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4153 | if (player != null) { |
| 4154 | player.play(new Binder(), mInCallNotificationUri, |
| 4155 | mInCallNotificationAudioAttributes, |
| 4156 | mInCallNotificationVolume, false); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4157 | } |
Beverly | 5d463b6 | 2017-07-26 14:13:40 -0400 | [diff] [blame] | 4158 | } catch (RemoteException e) { |
| 4159 | } finally { |
| 4160 | Binder.restoreCallingIdentity(identity); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 4161 | } |
| 4162 | } |
| 4163 | }.start(); |
| 4164 | } |
| 4165 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4166 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4167 | void showNextToastLocked() { |
| 4168 | ToastRecord record = mToastQueue.get(0); |
| 4169 | while (record != null) { |
| 4170 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| 4171 | try { |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4172 | record.callback.show(record.token); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4173 | scheduleTimeoutLocked(record); |
| 4174 | return; |
| 4175 | } catch (RemoteException e) { |
| 4176 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| 4177 | + " in package " + record.pkg); |
| 4178 | // remove it from the list and let the process die |
| 4179 | int index = mToastQueue.indexOf(record); |
| 4180 | if (index >= 0) { |
| 4181 | mToastQueue.remove(index); |
| 4182 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4183 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4184 | if (mToastQueue.size() > 0) { |
| 4185 | record = mToastQueue.get(0); |
| 4186 | } else { |
| 4187 | record = null; |
| 4188 | } |
| 4189 | } |
| 4190 | } |
| 4191 | } |
| 4192 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4193 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4194 | void cancelToastLocked(int index) { |
| 4195 | ToastRecord record = mToastQueue.get(index); |
| 4196 | try { |
| 4197 | record.callback.hide(); |
| 4198 | } catch (RemoteException e) { |
| 4199 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| 4200 | + " in package " + record.pkg); |
| 4201 | // don't worry about this, we're about to remove it from |
| 4202 | // the list anyway |
| 4203 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4204 | |
| 4205 | ToastRecord lastToast = mToastQueue.remove(index); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 4206 | mWindowManagerInternal.removeWindowToken(lastToast.token, true, DEFAULT_DISPLAY); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4207 | |
| 4208 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4209 | if (mToastQueue.size() > 0) { |
| 4210 | // Show the next one. If the callback fails, this will remove |
| 4211 | // it from the list, so don't assume that the list hasn't changed |
| 4212 | // after this point. |
| 4213 | showNextToastLocked(); |
| 4214 | } |
| 4215 | } |
| 4216 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4217 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4218 | private void scheduleTimeoutLocked(ToastRecord r) |
| 4219 | { |
| 4220 | mHandler.removeCallbacksAndMessages(r); |
| 4221 | Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r); |
| 4222 | long delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY; |
| 4223 | mHandler.sendMessageDelayed(m, delay); |
| 4224 | } |
| 4225 | |
| 4226 | private void handleTimeout(ToastRecord record) |
| 4227 | { |
| 4228 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| 4229 | synchronized (mToastQueue) { |
| 4230 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 4231 | if (index >= 0) { |
| 4232 | cancelToastLocked(index); |
| 4233 | } |
| 4234 | } |
| 4235 | } |
| 4236 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4237 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4238 | int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 4239 | { |
| 4240 | IBinder cbak = callback.asBinder(); |
| 4241 | ArrayList<ToastRecord> list = mToastQueue; |
| 4242 | int len = list.size(); |
| 4243 | for (int i=0; i<len; i++) { |
| 4244 | ToastRecord r = list.get(i); |
Beverly | 4ee785b | 2017-08-11 12:49:56 -0400 | [diff] [blame] | 4245 | if (r.pkg.equals(pkg) && r.callback.asBinder().equals(cbak)) { |
| 4246 | return i; |
| 4247 | } |
| 4248 | } |
| 4249 | return -1; |
| 4250 | } |
| 4251 | |
| 4252 | @GuardedBy("mToastQueue") |
| 4253 | int indexOfToastPackageLocked(String pkg) |
| 4254 | { |
| 4255 | ArrayList<ToastRecord> list = mToastQueue; |
| 4256 | int len = list.size(); |
| 4257 | for (int i=0; i<len; i++) { |
| 4258 | ToastRecord r = list.get(i); |
| 4259 | if (r.pkg.equals(pkg)) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4260 | return i; |
| 4261 | } |
| 4262 | } |
| 4263 | return -1; |
| 4264 | } |
| 4265 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4266 | @GuardedBy("mToastQueue") |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4267 | void keepProcessAliveIfNeededLocked(int pid) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4268 | { |
| 4269 | int toastCount = 0; // toasts from this pid |
| 4270 | ArrayList<ToastRecord> list = mToastQueue; |
| 4271 | int N = list.size(); |
| 4272 | for (int i=0; i<N; i++) { |
| 4273 | ToastRecord r = list.get(i); |
| 4274 | if (r.pid == pid) { |
| 4275 | toastCount++; |
| 4276 | } |
| 4277 | } |
| 4278 | try { |
Dianne Hackborn | f965f40 | 2017-05-04 23:27:23 -0700 | [diff] [blame] | 4279 | mAm.setProcessImportant(mForegroundToken, pid, toastCount > 0, "toast"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4280 | } catch (RemoteException e) { |
| 4281 | // Shouldn't happen. |
| 4282 | } |
| 4283 | } |
| 4284 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4285 | private void handleRankingReconsideration(Message message) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4286 | if (!(message.obj instanceof RankingReconsideration)) return; |
| 4287 | RankingReconsideration recon = (RankingReconsideration) message.obj; |
| 4288 | recon.run(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4289 | boolean changed; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4290 | synchronized (mNotificationLock) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4291 | final NotificationRecord record = mNotificationsByKey.get(recon.getKey()); |
| 4292 | if (record == null) { |
| 4293 | return; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4294 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4295 | int indexBefore = findNotificationRecordIndexLocked(record); |
| 4296 | boolean interceptBefore = record.isIntercepted(); |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4297 | float contactAffinityBefore = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4298 | int visibilityBefore = record.getPackageVisibilityOverride(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4299 | recon.applyChangesLocked(record); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4300 | applyZenModeLocked(record); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4301 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4302 | int indexAfter = findNotificationRecordIndexLocked(record); |
| 4303 | boolean interceptAfter = record.isIntercepted(); |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4304 | float contactAffinityAfter = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4305 | int visibilityAfter = record.getPackageVisibilityOverride(); |
| 4306 | changed = indexBefore != indexAfter || interceptBefore != interceptAfter |
| 4307 | || visibilityBefore != visibilityAfter; |
Julia Reynolds | 16eb52a | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4308 | if (interceptBefore && !interceptAfter |
| 4309 | && Float.compare(contactAffinityBefore, contactAffinityAfter) != 0) { |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4310 | buzzBeepBlinkLocked(record); |
| 4311 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4312 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4313 | if (changed) { |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4314 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4315 | } |
| 4316 | } |
| 4317 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4318 | void handleRankingSort() { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 4319 | if (mRankingHelper == null) return; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4320 | synchronized (mNotificationLock) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4321 | final int N = mNotificationList.size(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4322 | // Any field that can change via one of the extractors needs to be added here. |
| 4323 | ArrayList<String> orderBefore = new ArrayList<>(N); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4324 | int[] visibilities = new int[N]; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4325 | boolean[] showBadges = new boolean[N]; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4326 | ArrayList<NotificationChannel> channelBefore = new ArrayList<>(N); |
| 4327 | ArrayList<String> groupKeyBefore = new ArrayList<>(N); |
| 4328 | ArrayList<ArrayList<String>> overridePeopleBefore = new ArrayList<>(N); |
| 4329 | ArrayList<ArrayList<SnoozeCriterion>> snoozeCriteriaBefore = new ArrayList<>(N); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4330 | for (int i = 0; i < N; i++) { |
| 4331 | final NotificationRecord r = mNotificationList.get(i); |
| 4332 | orderBefore.add(r.getKey()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4333 | visibilities[i] = r.getPackageVisibilityOverride(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4334 | showBadges[i] = r.canShowBadge(); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4335 | channelBefore.add(r.getChannel()); |
| 4336 | groupKeyBefore.add(r.getGroupKey()); |
| 4337 | overridePeopleBefore.add(r.getPeopleOverride()); |
| 4338 | snoozeCriteriaBefore.add(r.getSnoozeCriteria()); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4339 | mRankingHelper.extractSignals(r); |
| 4340 | } |
Chris Wren | 19a02b0 | 2015-12-22 10:34:22 -0500 | [diff] [blame] | 4341 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4342 | for (int i = 0; i < N; i++) { |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4343 | final NotificationRecord r = mNotificationList.get(i); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4344 | if (!orderBefore.get(i).equals(r.getKey()) |
Julia Reynolds | 6976669 | 2016-02-01 15:35:08 -0500 | [diff] [blame] | 4345 | || visibilities[i] != r.getPackageVisibilityOverride() |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4346 | || showBadges[i] != r.canShowBadge() |
| 4347 | || !Objects.equals(channelBefore.get(i), r.getChannel()) |
| 4348 | || !Objects.equals(groupKeyBefore.get(i), r.getGroupKey()) |
| 4349 | || !Objects.equals(overridePeopleBefore.get(i), r.getPeopleOverride()) |
| 4350 | || !Objects.equals(snoozeCriteriaBefore.get(i), r.getSnoozeCriteria())) { |
| 4351 | mHandler.scheduleSendRankingUpdate(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4352 | return; |
| 4353 | } |
| 4354 | } |
| 4355 | } |
| 4356 | } |
| 4357 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4358 | @GuardedBy("mNotificationLock") |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 4359 | private void recordCallerLocked(NotificationRecord record) { |
| 4360 | if (mZenModeHelper.isCall(record)) { |
| 4361 | mZenModeHelper.recordCaller(record); |
| 4362 | } |
| 4363 | } |
| 4364 | |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 4365 | // let zen mode evaluate this record |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4366 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4367 | private void applyZenModeLocked(NotificationRecord record) { |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 4368 | record.setIntercepted(mZenModeHelper.shouldIntercept(record)); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4369 | if (record.isIntercepted()) { |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 4370 | int suppressed = (mZenModeHelper.shouldSuppressWhenScreenOff() |
| 4371 | ? SUPPRESSED_EFFECT_SCREEN_OFF : 0) |
| 4372 | | (mZenModeHelper.shouldSuppressWhenScreenOn() |
| 4373 | ? SUPPRESSED_EFFECT_SCREEN_ON : 0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4374 | record.setSuppressedVisualEffects(suppressed); |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 4375 | } else { |
| 4376 | record.setSuppressedVisualEffects(0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4377 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4378 | } |
| 4379 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4380 | @GuardedBy("mNotificationLock") |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4381 | private int findNotificationRecordIndexLocked(NotificationRecord target) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4382 | return mRankingHelper.indexOf(mNotificationList, target); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4383 | } |
| 4384 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4385 | private void handleSendRankingUpdate() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4386 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4387 | mListeners.notifyRankingUpdateLocked(); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4388 | } |
| 4389 | } |
| 4390 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4391 | private void scheduleListenerHintsChanged(int state) { |
| 4392 | mHandler.removeMessages(MESSAGE_LISTENER_HINTS_CHANGED); |
| 4393 | mHandler.obtainMessage(MESSAGE_LISTENER_HINTS_CHANGED, state, 0).sendToTarget(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4394 | } |
| 4395 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4396 | private void scheduleInterruptionFilterChanged(int listenerInterruptionFilter) { |
| 4397 | mHandler.removeMessages(MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED); |
| 4398 | mHandler.obtainMessage( |
| 4399 | MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED, |
| 4400 | listenerInterruptionFilter, |
| 4401 | 0).sendToTarget(); |
| 4402 | } |
| 4403 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4404 | private void handleListenerHintsChanged(int hints) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4405 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4406 | mListeners.notifyListenerHintsChangedLocked(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4407 | } |
| 4408 | } |
| 4409 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4410 | private void handleListenerInterruptionFilterChanged(int interruptionFilter) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4411 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4412 | mListeners.notifyInterruptionFilterChanged(interruptionFilter); |
| 4413 | } |
| 4414 | } |
| 4415 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4416 | protected class WorkerHandler extends Handler |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4417 | { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4418 | public WorkerHandler(Looper looper) { |
| 4419 | super(looper); |
| 4420 | } |
| 4421 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4422 | @Override |
| 4423 | public void handleMessage(Message msg) |
| 4424 | { |
| 4425 | switch (msg.what) |
| 4426 | { |
| 4427 | case MESSAGE_TIMEOUT: |
| 4428 | handleTimeout((ToastRecord)msg.obj); |
| 4429 | break; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 4430 | case MESSAGE_SAVE_POLICY_FILE: |
| 4431 | handleSavePolicyFile(); |
| 4432 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4433 | case MESSAGE_SEND_RANKING_UPDATE: |
| 4434 | handleSendRankingUpdate(); |
| 4435 | break; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4436 | case MESSAGE_LISTENER_HINTS_CHANGED: |
| 4437 | handleListenerHintsChanged(msg.arg1); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4438 | break; |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4439 | case MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED: |
| 4440 | handleListenerInterruptionFilterChanged(msg.arg1); |
| 4441 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4442 | } |
| 4443 | } |
| 4444 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4445 | protected void scheduleSendRankingUpdate() { |
| 4446 | if (!hasMessages(MESSAGE_SEND_RANKING_UPDATE)) { |
| 4447 | Message m = Message.obtain(this, MESSAGE_SEND_RANKING_UPDATE); |
| 4448 | sendMessage(m); |
| 4449 | } |
| 4450 | } |
| 4451 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4452 | } |
| 4453 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4454 | private final class RankingHandlerWorker extends Handler implements RankingHandler |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4455 | { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4456 | public RankingHandlerWorker(Looper looper) { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4457 | super(looper); |
| 4458 | } |
| 4459 | |
| 4460 | @Override |
| 4461 | public void handleMessage(Message msg) { |
| 4462 | switch (msg.what) { |
| 4463 | case MESSAGE_RECONSIDER_RANKING: |
| 4464 | handleRankingReconsideration(msg); |
| 4465 | break; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4466 | case MESSAGE_RANKING_SORT: |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4467 | handleRankingSort(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4468 | break; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4469 | } |
| 4470 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4471 | |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 4472 | public void requestSort() { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4473 | removeMessages(MESSAGE_RANKING_SORT); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4474 | Message msg = Message.obtain(); |
| 4475 | msg.what = MESSAGE_RANKING_SORT; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4476 | sendMessage(msg); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4477 | } |
| 4478 | |
| 4479 | public void requestReconsideration(RankingReconsideration recon) { |
| 4480 | Message m = Message.obtain(this, |
| 4481 | NotificationManagerService.MESSAGE_RECONSIDER_RANKING, recon); |
| 4482 | long delay = recon.getDelay(TimeUnit.MILLISECONDS); |
| 4483 | sendMessageDelayed(m, delay); |
| 4484 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4485 | } |
| 4486 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4487 | // Notifications |
| 4488 | // ============================================================================ |
| 4489 | static int clamp(int x, int low, int high) { |
| 4490 | return (x < low) ? low : ((x > high) ? high : x); |
| 4491 | } |
| 4492 | |
| 4493 | void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
| 4494 | AccessibilityManager manager = AccessibilityManager.getInstance(getContext()); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 4495 | if (!manager.isEnabled()) { |
| 4496 | return; |
| 4497 | } |
| 4498 | |
| 4499 | AccessibilityEvent event = |
| 4500 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 4501 | event.setPackageName(packageName); |
| 4502 | event.setClassName(Notification.class.getName()); |
| 4503 | event.setParcelableData(notification); |
| 4504 | CharSequence tickerText = notification.tickerText; |
| 4505 | if (!TextUtils.isEmpty(tickerText)) { |
| 4506 | event.getText().add(tickerText); |
| 4507 | } |
| 4508 | |
| 4509 | manager.sendAccessibilityEvent(event); |
| 4510 | } |
| 4511 | |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4512 | /** |
| 4513 | * Removes all NotificationsRecords with the same key as the given notification record |
| 4514 | * from both lists. Do not call this method while iterating over either list. |
| 4515 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4516 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4517 | private boolean removeFromNotificationListsLocked(NotificationRecord r) { |
| 4518 | // Remove from both lists, either list could have a separate Record for what is |
| 4519 | // effectively the same notification. |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4520 | boolean wasPosted = false; |
| 4521 | NotificationRecord recordInList = null; |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4522 | if ((recordInList = findNotificationByListLocked(mNotificationList, r.getKey())) |
| 4523 | != null) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4524 | mNotificationList.remove(recordInList); |
| 4525 | mNotificationsByKey.remove(recordInList.sbn.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4526 | wasPosted = true; |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4527 | } |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4528 | while ((recordInList = findNotificationByListLocked(mEnqueuedNotifications, r.getKey())) |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4529 | != null) { |
| 4530 | mEnqueuedNotifications.remove(recordInList); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4531 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4532 | return wasPosted; |
| 4533 | } |
| 4534 | |
| 4535 | @GuardedBy("mNotificationLock") |
| 4536 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason, |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4537 | boolean wasPosted, String listenerName) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4538 | final String canceledKey = r.getKey(); |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 4539 | |
| 4540 | // Record caller. |
| 4541 | recordCallerLocked(r); |
| 4542 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4543 | // tell the app |
| 4544 | if (sendDelete) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4545 | if (r.getNotification().deleteIntent != null) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4546 | try { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4547 | r.getNotification().deleteIntent.send(); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4548 | } catch (PendingIntent.CanceledException ex) { |
| 4549 | // do nothing - there's no relevant way to recover, and |
| 4550 | // no reason to let this propagate |
Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 4551 | Slog.w(TAG, "canceled PendingIntent for " + r.sbn.getPackageName(), ex); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4552 | } |
| 4553 | } |
| 4554 | } |
| 4555 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4556 | // Only cancel these if this notification actually got to be posted. |
| 4557 | if (wasPosted) { |
| 4558 | // status bar |
| 4559 | if (r.getNotification().getSmallIcon() != null) { |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 4560 | if (reason != REASON_SNOOZED) { |
| 4561 | r.isCanceled = true; |
| 4562 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4563 | mListeners.notifyRemovedLocked(r.sbn, reason); |
| 4564 | mHandler.post(new Runnable() { |
| 4565 | @Override |
| 4566 | public void run() { |
| 4567 | mGroupHelper.onNotificationRemoved(r.sbn); |
| 4568 | } |
| 4569 | }); |
| 4570 | } |
| 4571 | |
| 4572 | // sound |
| 4573 | if (canceledKey.equals(mSoundNotificationKey)) { |
| 4574 | mSoundNotificationKey = null; |
| 4575 | final long identity = Binder.clearCallingIdentity(); |
| 4576 | try { |
| 4577 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4578 | if (player != null) { |
| 4579 | player.stopAsync(); |
| 4580 | } |
| 4581 | } catch (RemoteException e) { |
| 4582 | } finally { |
| 4583 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4584 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4585 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4586 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4587 | // vibrate |
| 4588 | if (canceledKey.equals(mVibrateNotificationKey)) { |
| 4589 | mVibrateNotificationKey = null; |
| 4590 | long identity = Binder.clearCallingIdentity(); |
| 4591 | try { |
| 4592 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 4593 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4594 | finally { |
| 4595 | Binder.restoreCallingIdentity(identity); |
| 4596 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4597 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4598 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4599 | // light |
| 4600 | mLights.remove(canceledKey); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4601 | } |
| 4602 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4603 | // Record usage stats |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4604 | // TODO: add unbundling stats? |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4605 | switch (reason) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 4606 | case REASON_CANCEL: |
| 4607 | case REASON_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4608 | case REASON_LISTENER_CANCEL: |
| 4609 | case REASON_LISTENER_CANCEL_ALL: |
| 4610 | mUsageStats.registerDismissedByUser(r); |
| 4611 | break; |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 4612 | case REASON_APP_CANCEL: |
| 4613 | case REASON_APP_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4614 | mUsageStats.registerRemovedByApp(r); |
| 4615 | break; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4616 | } |
| 4617 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4618 | String groupKey = r.getGroupKey(); |
| 4619 | NotificationRecord groupSummary = mSummaryByGroupKey.get(groupKey); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4620 | if (groupSummary != null && groupSummary.getKey().equals(canceledKey)) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4621 | mSummaryByGroupKey.remove(groupKey); |
| 4622 | } |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 4623 | final ArrayMap<String, String> summaries = mAutobundledSummaries.get(r.sbn.getUserId()); |
| 4624 | if (summaries != null && r.sbn.getKey().equals(summaries.get(r.sbn.getPackageName()))) { |
| 4625 | summaries.remove(r.sbn.getPackageName()); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4626 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 4627 | |
Daniel Sandler | 23d7c70 | 2013-03-07 16:32:06 -0500 | [diff] [blame] | 4628 | // Save it for users of getHistoricalNotifications() |
| 4629 | mArchive.record(r.sbn); |
Christoph Studer | 81e5b5f | 2014-10-22 17:19:56 +0200 | [diff] [blame] | 4630 | |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 4631 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 4632 | MetricsLogger.action(r.getLogMaker(now) |
| 4633 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
| 4634 | .setType(MetricsEvent.TYPE_DISMISS) |
| 4635 | .setSubtype(reason)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 4636 | EventLogTags.writeNotificationCanceled(canceledKey, reason, |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4637 | 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] | 4638 | } |
| 4639 | |
| 4640 | /** |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 4641 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4642 | * and none of the {@code mustNotHaveFlags}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4643 | */ |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 4644 | void cancelNotification(final int callingUid, final int callingPid, |
| 4645 | final String pkg, final String tag, final int id, |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4646 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4647 | final int userId, final int reason, final ManagedServiceInfo listener) { |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4648 | // In enqueueNotificationInternal notifications are added by scheduling the |
| 4649 | // work on the worker handler. Hence, we also schedule the cancel on this |
| 4650 | // handler to avoid a scenario where an add notification call followed by a |
| 4651 | // remove notification call ends up in not removing the notification. |
| 4652 | mHandler.post(new Runnable() { |
| 4653 | @Override |
| 4654 | public void run() { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4655 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Chris Wren | bddb5bc | 2015-03-04 08:47:46 -0800 | [diff] [blame] | 4656 | if (DBG) EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, id, tag, |
| 4657 | userId, mustHaveFlags, mustNotHaveFlags, reason, listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4658 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4659 | synchronized (mNotificationLock) { |
| 4660 | // Look for the notification, searching both the posted and enqueued lists. |
| 4661 | NotificationRecord r = findNotificationLocked(pkg, tag, id, userId); |
| 4662 | if (r != null) { |
| 4663 | // The notification was found, check if it should be removed. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4664 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4665 | // Ideally we'd do this in the caller of this method. However, that would |
| 4666 | // require the caller to also find the notification. |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 4667 | if (reason == REASON_CLICK) { |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4668 | mUsageStats.registerClickedByUser(r); |
| 4669 | } |
| 4670 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4671 | if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) { |
| 4672 | return; |
| 4673 | } |
| 4674 | if ((r.getNotification().flags & mustNotHaveFlags) != 0) { |
| 4675 | return; |
| 4676 | } |
| 4677 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4678 | // Cancel the notification. |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4679 | boolean wasPosted = removeFromNotificationListsLocked(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4680 | cancelNotificationLocked(r, sendDelete, reason, wasPosted, listenerName); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4681 | cancelGroupChildrenLocked(r, callingUid, callingPid, listenerName, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 4682 | sendDelete, null); |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4683 | updateLightsLocked(); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 4684 | } else { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4685 | // No notification was found, assume that it is snoozed and cancel it. |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 4686 | if (reason != REASON_SNOOZED) { |
| 4687 | final boolean wasSnoozed = mSnoozeHelper.cancel(userId, pkg, tag, id); |
| 4688 | if (wasSnoozed) { |
| 4689 | savePolicyFile(); |
| 4690 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 4691 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4692 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4693 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4694 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4695 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4696 | } |
| 4697 | |
| 4698 | /** |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 4699 | * Determine whether the userId applies to the notification in question, either because |
| 4700 | * they match exactly, or one of them is USER_ALL (which is treated as a wildcard). |
| 4701 | */ |
| 4702 | private boolean notificationMatchesUserId(NotificationRecord r, int userId) { |
| 4703 | return |
| 4704 | // looking for USER_ALL notifications? match everything |
| 4705 | userId == UserHandle.USER_ALL |
| 4706 | // a notification sent to USER_ALL matches any query |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4707 | || r.getUserId() == UserHandle.USER_ALL |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 4708 | // an exact user match |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4709 | || r.getUserId() == userId; |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | /** |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4713 | * Determine whether the userId applies to the notification in question, either because |
| 4714 | * 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] | 4715 | * because it matches one of the users profiles. |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4716 | */ |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 4717 | private boolean notificationMatchesCurrentProfiles(NotificationRecord r, int userId) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4718 | return notificationMatchesUserId(r, userId) |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 4719 | || mUserProfiles.isCurrentProfile(r.getUserId()); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
| 4722 | /** |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 4723 | * 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] | 4724 | * {@code mustHaveFlags}. |
| 4725 | */ |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4726 | void cancelAllNotificationsInt(int callingUid, int callingPid, String pkg, String channelId, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 4727 | int mustHaveFlags, int mustNotHaveFlags, boolean doit, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4728 | ManagedServiceInfo listener) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4729 | mHandler.post(new Runnable() { |
| 4730 | @Override |
| 4731 | public void run() { |
| 4732 | String listenerName = listener == null ? null : listener.component.toShortString(); |
| 4733 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 4734 | pkg, userId, mustHaveFlags, mustNotHaveFlags, reason, |
| 4735 | listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4736 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4737 | // Why does this parameter exist? Do we actually want to execute the above if doit |
| 4738 | // is false? |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4739 | if (!doit) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4740 | return; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4741 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4742 | |
| 4743 | synchronized (mNotificationLock) { |
| 4744 | FlagChecker flagChecker = (int flags) -> { |
| 4745 | if ((flags & mustHaveFlags) != mustHaveFlags) { |
| 4746 | return false; |
| 4747 | } |
| 4748 | if ((flags & mustNotHaveFlags) != 0) { |
| 4749 | return false; |
| 4750 | } |
| 4751 | return true; |
| 4752 | }; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4753 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 4754 | pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, flagChecker, |
| 4755 | false /*includeCurrentProfiles*/, userId, false /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4756 | listenerName, true /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4757 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 4758 | callingPid, pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, |
| 4759 | flagChecker, false /*includeCurrentProfiles*/, userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4760 | false /*sendDelete*/, reason, listenerName, false /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4761 | mSnoozeHelper.cancel(userId, pkg); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4762 | } |
| 4763 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4764 | }); |
| 4765 | } |
| 4766 | |
| 4767 | private interface FlagChecker { |
| 4768 | // Returns false if these flags do not pass the defined flag test. |
| 4769 | public boolean apply(int flags); |
| 4770 | } |
| 4771 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4772 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4773 | private void cancelAllNotificationsByListLocked(ArrayList<NotificationRecord> notificationList, |
| 4774 | int callingUid, int callingPid, String pkg, boolean nullPkgIndicatesUserSwitch, |
| 4775 | String channelId, FlagChecker flagChecker, boolean includeCurrentProfiles, int userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4776 | boolean sendDelete, int reason, String listenerName, boolean wasPosted) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4777 | ArrayList<NotificationRecord> canceledNotifications = null; |
| 4778 | for (int i = notificationList.size() - 1; i >= 0; --i) { |
| 4779 | NotificationRecord r = notificationList.get(i); |
| 4780 | if (includeCurrentProfiles) { |
| 4781 | if (!notificationMatchesCurrentProfiles(r, userId)) { |
| 4782 | continue; |
| 4783 | } |
| 4784 | } else if (!notificationMatchesUserId(r, userId)) { |
| 4785 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4786 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4787 | // Don't remove notifications to all, if there's no package name specified |
| 4788 | if (nullPkgIndicatesUserSwitch && pkg == null && r.getUserId() == UserHandle.USER_ALL) { |
| 4789 | continue; |
| 4790 | } |
| 4791 | if (!flagChecker.apply(r.getFlags())) { |
| 4792 | continue; |
| 4793 | } |
| 4794 | if (pkg != null && !r.sbn.getPackageName().equals(pkg)) { |
| 4795 | continue; |
| 4796 | } |
| 4797 | if (channelId != null && !channelId.equals(r.getChannel().getId())) { |
| 4798 | continue; |
| 4799 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4800 | if (canceledNotifications == null) { |
| 4801 | canceledNotifications = new ArrayList<>(); |
| 4802 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4803 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 4804 | mNotificationsByKey.remove(r.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4805 | canceledNotifications.add(r); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4806 | cancelNotificationLocked(r, sendDelete, reason, wasPosted, listenerName); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4807 | } |
| 4808 | if (canceledNotifications != null) { |
| 4809 | final int M = canceledNotifications.size(); |
| 4810 | for (int i = 0; i < M; i++) { |
| 4811 | cancelGroupChildrenLocked(canceledNotifications.get(i), callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 4812 | listenerName, false /* sendDelete */, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4813 | } |
| 4814 | updateLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4815 | } |
| 4816 | } |
| 4817 | |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 4818 | void snoozeNotificationInt(String key, long duration, String snoozeCriterionId, |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4819 | ManagedServiceInfo listener) { |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 4820 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 4821 | if (duration <= 0 && snoozeCriterionId == null || key == null) { |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 4822 | return; |
| 4823 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4824 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 4825 | if (DBG) { |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 4826 | Slog.d(TAG, String.format("snooze event(%s, %d, %s, %s)", key, duration, |
| 4827 | snoozeCriterionId, listenerName)); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 4828 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4829 | // Needs to post so that it can cancel notifications not yet enqueued. |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4830 | mHandler.post(new SnoozeNotificationRunnable(key, duration, snoozeCriterionId)); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 4831 | } |
| 4832 | |
| 4833 | void unsnoozeNotificationInt(String key, ManagedServiceInfo listener) { |
| 4834 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 4835 | if (DBG) { |
| 4836 | Slog.d(TAG, String.format("unsnooze event(%s, %s)", key, listenerName)); |
| 4837 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 4838 | mSnoozeHelper.repost(key); |
| 4839 | savePolicyFile(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 4840 | } |
| 4841 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4842 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 350159c | 2014-03-27 11:15:11 -0700 | [diff] [blame] | 4843 | void cancelAllLocked(int callingUid, int callingPid, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4844 | ManagedServiceInfo listener, boolean includeCurrentProfiles) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4845 | mHandler.post(new Runnable() { |
| 4846 | @Override |
| 4847 | public void run() { |
| 4848 | synchronized (mNotificationLock) { |
| 4849 | String listenerName = |
| 4850 | listener == null ? null : listener.component.toShortString(); |
| 4851 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 4852 | null, userId, 0, 0, reason, listenerName); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4853 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4854 | FlagChecker flagChecker = (int flags) -> { |
| 4855 | if ((flags & (Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR)) |
| 4856 | != 0) { |
| 4857 | return false; |
| 4858 | } |
| 4859 | return true; |
| 4860 | }; |
| 4861 | |
| 4862 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 4863 | null, false /*nullPkgIndicatesUserSwitch*/, null, flagChecker, |
| 4864 | includeCurrentProfiles, userId, true /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4865 | listenerName, true); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4866 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 4867 | callingPid, null, false /*nullPkgIndicatesUserSwitch*/, null, |
| 4868 | flagChecker, includeCurrentProfiles, userId, true /*sendDelete*/, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4869 | reason, listenerName, false); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4870 | mSnoozeHelper.cancel(userId, includeCurrentProfiles); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4871 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4872 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4873 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4874 | } |
| 4875 | |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4876 | // Warning: The caller is responsible for invoking updateLightsLocked(). |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4877 | @GuardedBy("mNotificationLock") |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4878 | private void cancelGroupChildrenLocked(NotificationRecord r, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 4879 | String listenerName, boolean sendDelete, FlagChecker flagChecker) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4880 | Notification n = r.getNotification(); |
Christoph Studer | 3f31f5d | 2014-07-31 16:55:32 +0200 | [diff] [blame] | 4881 | if (!n.isGroupSummary()) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4882 | return; |
| 4883 | } |
| 4884 | |
| 4885 | String pkg = r.sbn.getPackageName(); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4886 | |
| 4887 | if (pkg == null) { |
| 4888 | if (DBG) Log.e(TAG, "No package for group summary: " + r.getKey()); |
| 4889 | return; |
| 4890 | } |
| 4891 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4892 | cancelGroupChildrenByListLocked(mNotificationList, r, callingUid, callingPid, listenerName, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 4893 | sendDelete, true, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4894 | cancelGroupChildrenByListLocked(mEnqueuedNotifications, r, callingUid, callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 4895 | listenerName, sendDelete, false, flagChecker); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4896 | } |
| 4897 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4898 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4899 | private void cancelGroupChildrenByListLocked(ArrayList<NotificationRecord> notificationList, |
| 4900 | NotificationRecord parentNotification, int callingUid, int callingPid, |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 4901 | String listenerName, boolean sendDelete, boolean wasPosted, FlagChecker flagChecker) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4902 | final String pkg = parentNotification.sbn.getPackageName(); |
| 4903 | final int userId = parentNotification.getUserId(); |
| 4904 | final int reason = REASON_GROUP_SUMMARY_CANCELED; |
| 4905 | for (int i = notificationList.size() - 1; i >= 0; i--) { |
| 4906 | final NotificationRecord childR = notificationList.get(i); |
| 4907 | final StatusBarNotification childSbn = childR.sbn; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4908 | if ((childSbn.isGroup() && !childSbn.getNotification().isGroupSummary()) && |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4909 | childR.getGroupKey().equals(parentNotification.getGroupKey()) |
Beverly | 40239d9 | 2017-07-07 10:20:41 -0400 | [diff] [blame] | 4910 | && (childR.getFlags() & Notification.FLAG_FOREGROUND_SERVICE) == 0 |
| 4911 | && (flagChecker == null || flagChecker.apply(childR.getFlags()))) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4912 | EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, childSbn.getId(), |
| 4913 | childSbn.getTag(), userId, 0, 0, reason, listenerName); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4914 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 4915 | mNotificationsByKey.remove(childR.getKey()); |
Julia Reynolds | 359e9b1 | 2017-08-08 12:40:04 -0400 | [diff] [blame] | 4916 | cancelNotificationLocked(childR, sendDelete, reason, wasPosted, listenerName); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4917 | } |
| 4918 | } |
| 4919 | } |
| 4920 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4921 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4922 | void updateLightsLocked() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4923 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4924 | // handle notification lights |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 4925 | NotificationRecord ledNotification = null; |
| 4926 | while (ledNotification == null && !mLights.isEmpty()) { |
| 4927 | final String owner = mLights.get(mLights.size() - 1); |
| 4928 | ledNotification = mNotificationsByKey.get(owner); |
| 4929 | if (ledNotification == null) { |
| 4930 | Slog.wtfStack(TAG, "LED Notification does not exist: " + owner); |
| 4931 | mLights.remove(owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4932 | } |
| 4933 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 4934 | |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 4935 | // 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] | 4936 | if (ledNotification == null || mInCall || mScreenOn) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 4937 | mNotificationLight.turnOff(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4938 | } else { |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 4939 | NotificationRecord.Light light = ledNotification.getLight(); |
| 4940 | if (light != null && mNotificationPulseEnabled) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 4941 | // pulse repeatedly |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 4942 | mNotificationLight.setFlashing(light.color, Light.LIGHT_FLASH_TIMED, |
| 4943 | light.onMs, light.offMs); |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 4944 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4945 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4946 | } |
| 4947 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4948 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4949 | @NonNull List<NotificationRecord> findGroupNotificationsLocked(String pkg, |
| 4950 | String groupKey, int userId) { |
| 4951 | List<NotificationRecord> records = new ArrayList<>(); |
| 4952 | records.addAll(findGroupNotificationByListLocked(mNotificationList, pkg, groupKey, userId)); |
| 4953 | records.addAll( |
| 4954 | findGroupNotificationByListLocked(mEnqueuedNotifications, pkg, groupKey, userId)); |
| 4955 | return records; |
| 4956 | } |
| 4957 | |
| 4958 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4959 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4960 | private @NonNull List<NotificationRecord> findGroupNotificationByListLocked( |
| 4961 | ArrayList<NotificationRecord> list, String pkg, String groupKey, int userId) { |
| 4962 | List<NotificationRecord> records = new ArrayList<>(); |
| 4963 | final int len = list.size(); |
| 4964 | for (int i = 0; i < len; i++) { |
| 4965 | NotificationRecord r = list.get(i); |
| 4966 | if (notificationMatchesUserId(r, userId) && r.getGroupKey().equals(groupKey) |
| 4967 | && r.sbn.getPackageName().equals(pkg)) { |
| 4968 | records.add(r); |
| 4969 | } |
| 4970 | } |
| 4971 | return records; |
| 4972 | } |
| 4973 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4974 | // Searches both enqueued and posted notifications by key. |
| 4975 | // TODO: need to combine a bunch of these getters with slightly different behavior. |
| 4976 | // TODO: Should enqueuing just add to mNotificationsByKey instead? |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4977 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4978 | private NotificationRecord findNotificationByKeyLocked(String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4979 | NotificationRecord r; |
| 4980 | if ((r = findNotificationByListLocked(mNotificationList, key)) != null) { |
| 4981 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4982 | } |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4983 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, key)) != null) { |
| 4984 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4985 | } |
| 4986 | return null; |
| 4987 | } |
| 4988 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4989 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4990 | NotificationRecord findNotificationLocked(String pkg, String tag, int id, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4991 | NotificationRecord r; |
| 4992 | if ((r = findNotificationByListLocked(mNotificationList, pkg, tag, id, userId)) != null) { |
| 4993 | return r; |
| 4994 | } |
| 4995 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, pkg, tag, id, userId)) |
| 4996 | != null) { |
| 4997 | return r; |
| 4998 | } |
| 4999 | return null; |
| 5000 | } |
| 5001 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5002 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5003 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5004 | String pkg, String tag, int id, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5005 | final int len = list.size(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5006 | for (int i = 0; i < len; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5007 | NotificationRecord r = list.get(i); |
Vladimir Marko | 2526f33 | 2013-09-11 11:13:55 +0100 | [diff] [blame] | 5008 | if (notificationMatchesUserId(r, userId) && r.sbn.getId() == id && |
| 5009 | TextUtils.equals(r.sbn.getTag(), tag) && r.sbn.getPackageName().equals(pkg)) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5010 | return r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5011 | } |
| 5012 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5013 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5014 | } |
| 5015 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5016 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5017 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5018 | String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 5019 | final int N = list.size(); |
| 5020 | for (int i = 0; i < N; i++) { |
| 5021 | if (key.equals(list.get(i).getKey())) { |
| 5022 | return list.get(i); |
| 5023 | } |
| 5024 | } |
| 5025 | return null; |
| 5026 | } |
| 5027 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5028 | @GuardedBy("mNotificationLock") |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 5029 | int indexOfNotificationLocked(String key) { |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 5030 | final int N = mNotificationList.size(); |
| 5031 | for (int i = 0; i < N; i++) { |
| 5032 | if (key.equals(mNotificationList.get(i).getKey())) { |
| 5033 | return i; |
| 5034 | } |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 5035 | } |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 5036 | return -1; |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 5037 | } |
| 5038 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 5039 | private void updateNotificationPulse() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5040 | synchronized (mNotificationLock) { |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 5041 | updateLightsLocked(); |
| 5042 | } |
| 5043 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 5044 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5045 | protected boolean isCallingUidSystem() { |
| 5046 | final int uid = Binder.getCallingUid(); |
| 5047 | return uid == Process.SYSTEM_UID; |
| 5048 | } |
| 5049 | |
| 5050 | protected boolean isUidSystemOrPhone(int uid) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5051 | final int appid = UserHandle.getAppId(uid); |
| 5052 | return (appid == Process.SYSTEM_UID || appid == Process.PHONE_UID || uid == 0); |
| 5053 | } |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 5054 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5055 | // TODO: Most calls should probably move to isCallerSystem. |
| 5056 | protected boolean isCallerSystemOrPhone() { |
| 5057 | return isUidSystemOrPhone(Binder.getCallingUid()); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5058 | } |
| 5059 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5060 | private void checkCallerIsSystemOrShell() { |
| 5061 | if (Binder.getCallingUid() == Process.SHELL_UID) { |
| 5062 | return; |
| 5063 | } |
| 5064 | checkCallerIsSystem(); |
| 5065 | } |
| 5066 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5067 | private void checkCallerIsSystem() { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5068 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5069 | return; |
| 5070 | } |
| 5071 | throw new SecurityException("Disallowed call for uid " + Binder.getCallingUid()); |
| 5072 | } |
| 5073 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5074 | private void checkCallerIsSystemOrSameApp(String pkg) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5075 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5076 | return; |
| 5077 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 5078 | checkCallerIsSameApp(pkg); |
| 5079 | } |
| 5080 | |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 5081 | private boolean isCallerInstantApp(String pkg) { |
| 5082 | // System is always allowed to act for ephemeral apps. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 5083 | if (isCallerSystemOrPhone()) { |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 5084 | return false; |
| 5085 | } |
| 5086 | |
| 5087 | mAppOps.checkPackage(Binder.getCallingUid(), pkg); |
| 5088 | |
| 5089 | try { |
| 5090 | ApplicationInfo ai = mPackageManager.getApplicationInfo(pkg, 0, |
| 5091 | UserHandle.getCallingUserId()); |
| 5092 | if (ai == null) { |
| 5093 | throw new SecurityException("Unknown package " + pkg); |
| 5094 | } |
| 5095 | return ai.isInstantApp(); |
| 5096 | } catch (RemoteException re) { |
| 5097 | throw new SecurityException("Unknown package " + pkg, re); |
| 5098 | } |
| 5099 | |
| 5100 | } |
| 5101 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5102 | private void checkCallerIsSameApp(String pkg) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5103 | final int uid = Binder.getCallingUid(); |
| 5104 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5105 | ApplicationInfo ai = mPackageManager.getApplicationInfo( |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5106 | pkg, 0, UserHandle.getCallingUserId()); |
Dan Sandler | 09afc2e | 2014-07-18 14:29:20 -0400 | [diff] [blame] | 5107 | if (ai == null) { |
| 5108 | throw new SecurityException("Unknown package " + pkg); |
| 5109 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5110 | if (!UserHandle.isSameApp(ai.uid, uid)) { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5111 | throw new SecurityException("Calling uid " + uid + " gave package " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5112 | + pkg + " which is owned by uid " + ai.uid); |
| 5113 | } |
| 5114 | } catch (RemoteException re) { |
| 5115 | throw new SecurityException("Unknown package " + pkg + "\n" + re); |
| 5116 | } |
| 5117 | } |
| 5118 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 5119 | private static String callStateToString(int state) { |
| 5120 | switch (state) { |
| 5121 | case TelephonyManager.CALL_STATE_IDLE: return "CALL_STATE_IDLE"; |
| 5122 | case TelephonyManager.CALL_STATE_RINGING: return "CALL_STATE_RINGING"; |
| 5123 | case TelephonyManager.CALL_STATE_OFFHOOK: return "CALL_STATE_OFFHOOK"; |
| 5124 | default: return "CALL_STATE_UNKNOWN_" + state; |
| 5125 | } |
| 5126 | } |
| 5127 | |
| 5128 | private void listenForCallState() { |
| 5129 | TelephonyManager.from(getContext()).listen(new PhoneStateListener() { |
| 5130 | @Override |
| 5131 | public void onCallStateChanged(int state, String incomingNumber) { |
| 5132 | if (mCallState == state) return; |
| 5133 | if (DBG) Slog.d(TAG, "Call state changed: " + callStateToString(state)); |
| 5134 | mCallState = state; |
| 5135 | } |
| 5136 | }, PhoneStateListener.LISTEN_CALL_STATE); |
| 5137 | } |
| 5138 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5139 | /** |
| 5140 | * Generates a NotificationRankingUpdate from 'sbns', considering only |
| 5141 | * notifications visible to the given listener. |
| 5142 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5143 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5144 | private NotificationRankingUpdate makeRankingUpdateLocked(ManagedServiceInfo info) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5145 | final int N = mNotificationList.size(); |
| 5146 | ArrayList<String> keys = new ArrayList<String>(N); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 5147 | ArrayList<String> interceptedKeys = new ArrayList<String>(N); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5148 | ArrayList<Integer> importance = new ArrayList<>(N); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5149 | Bundle overrideGroupKeys = new Bundle(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5150 | Bundle visibilityOverrides = new Bundle(); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 5151 | Bundle suppressedVisualEffects = new Bundle(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5152 | Bundle explanation = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5153 | Bundle channels = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5154 | Bundle overridePeople = new Bundle(); |
| 5155 | Bundle snoozeCriteria = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5156 | Bundle showBadge = new Bundle(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5157 | for (int i = 0; i < N; i++) { |
| 5158 | NotificationRecord record = mNotificationList.get(i); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5159 | if (!isVisibleToListener(record.sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5160 | continue; |
| 5161 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5162 | final String key = record.sbn.getKey(); |
| 5163 | keys.add(key); |
| 5164 | importance.add(record.getImportance()); |
| 5165 | if (record.getImportanceExplanation() != null) { |
| 5166 | explanation.putCharSequence(key, record.getImportanceExplanation()); |
| 5167 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5168 | if (record.isIntercepted()) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5169 | interceptedKeys.add(key); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 5170 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5171 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5172 | suppressedVisualEffects.putInt(key, record.getSuppressedVisualEffects()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5173 | if (record.getPackageVisibilityOverride() |
| 5174 | != NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5175 | visibilityOverrides.putInt(key, record.getPackageVisibilityOverride()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5176 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 5177 | overrideGroupKeys.putString(key, record.sbn.getOverrideGroupKey()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5178 | channels.putParcelable(key, record.getChannel()); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5179 | overridePeople.putStringArrayList(key, record.getPeopleOverride()); |
| 5180 | snoozeCriteria.putParcelableArrayList(key, record.getSnoozeCriteria()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5181 | showBadge.putBoolean(key, record.canShowBadge()); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5182 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5183 | final int M = keys.size(); |
| 5184 | String[] keysAr = keys.toArray(new String[M]); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 5185 | String[] interceptedKeysAr = interceptedKeys.toArray(new String[interceptedKeys.size()]); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 5186 | int[] importanceAr = new int[M]; |
| 5187 | for (int i = 0; i < M; i++) { |
| 5188 | importanceAr[i] = importance.get(i); |
| 5189 | } |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 5190 | return new NotificationRankingUpdate(keysAr, interceptedKeysAr, visibilityOverrides, |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 5191 | suppressedVisualEffects, importanceAr, explanation, overrideGroupKeys, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 5192 | channels, overridePeople, snoozeCriteria, showBadge); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5193 | } |
| 5194 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5195 | boolean hasCompanionDevice(ManagedServiceInfo info) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5196 | if (mCompanionManager == null) { |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 5197 | mCompanionManager = getCompanionManager(); |
| 5198 | } |
| 5199 | // Companion mgr doesn't exist on all device types |
| 5200 | if (mCompanionManager == null) { |
| 5201 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5202 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5203 | long identity = Binder.clearCallingIdentity(); |
| 5204 | try { |
| 5205 | List<String> associations = mCompanionManager.getAssociations( |
| 5206 | info.component.getPackageName(), info.userid); |
| 5207 | if (!ArrayUtils.isEmpty(associations)) { |
| 5208 | return true; |
| 5209 | } |
| 5210 | } catch (SecurityException se) { |
| 5211 | // Not a privileged listener |
| 5212 | } catch (RemoteException re) { |
| 5213 | Slog.e(TAG, "Cannot reach companion device service", re); |
| 5214 | } catch (Exception e) { |
| 5215 | Slog.e(TAG, "Cannot verify listener " + info, e); |
| 5216 | } finally { |
| 5217 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5218 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5219 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5220 | } |
| 5221 | |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 5222 | protected ICompanionDeviceManager getCompanionManager() { |
| 5223 | return ICompanionDeviceManager.Stub.asInterface( |
| 5224 | ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE)); |
| 5225 | } |
| 5226 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5227 | private boolean isVisibleToListener(StatusBarNotification sbn, ManagedServiceInfo listener) { |
| 5228 | if (!listener.enabledAndUserMatches(sbn.getUserId())) { |
| 5229 | return false; |
| 5230 | } |
Justin Koh | 8d11a5a | 2014-08-04 18:29:49 -0700 | [diff] [blame] | 5231 | // TODO: remove this for older listeners. |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5232 | return true; |
| 5233 | } |
| 5234 | |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 5235 | private boolean isPackageSuspendedForUser(String pkg, int uid) { |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5236 | int userId = UserHandle.getUserId(uid); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5237 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5238 | return mPackageManager.isPackageSuspendedForUser(pkg, userId); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5239 | } catch (RemoteException re) { |
| 5240 | throw new SecurityException("Could not talk to package manager service"); |
Andrei Stingaceanu | efc4a34 | 2016-03-22 14:43:01 +0000 | [diff] [blame] | 5241 | } catch (IllegalArgumentException ex) { |
| 5242 | // Package not found. |
| 5243 | return false; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5244 | } |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5245 | } |
| 5246 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5247 | private class TrimCache { |
| 5248 | StatusBarNotification heavy; |
| 5249 | StatusBarNotification sbnClone; |
| 5250 | StatusBarNotification sbnCloneLight; |
| 5251 | |
| 5252 | TrimCache(StatusBarNotification sbn) { |
| 5253 | heavy = sbn; |
| 5254 | } |
| 5255 | |
| 5256 | StatusBarNotification ForListener(ManagedServiceInfo info) { |
| 5257 | if (mListeners.getOnNotificationPostedTrim(info) == TRIM_LIGHT) { |
| 5258 | if (sbnCloneLight == null) { |
| 5259 | sbnCloneLight = heavy.cloneLight(); |
| 5260 | } |
| 5261 | return sbnCloneLight; |
| 5262 | } else { |
| 5263 | if (sbnClone == null) { |
| 5264 | sbnClone = heavy.clone(); |
| 5265 | } |
| 5266 | return sbnClone; |
| 5267 | } |
| 5268 | } |
| 5269 | } |
| 5270 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5271 | public class NotificationAssistants extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5272 | static final String TAG_ENABLED_NOTIFICATION_ASSISTANTS = "enabled_assistants"; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5273 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5274 | public NotificationAssistants(IPackageManager pm) { |
| 5275 | super(getContext(), mNotificationLock, mUserProfiles, pm); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | @Override |
| 5279 | protected Config getConfig() { |
| 5280 | Config c = new Config(); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5281 | c.caption = "notification assistant service"; |
| 5282 | c.serviceInterface = NotificationAssistantService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5283 | c.xmlTag = TAG_ENABLED_NOTIFICATION_ASSISTANTS; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5284 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_ASSISTANT; |
| 5285 | c.bindPermission = Manifest.permission.BIND_NOTIFICATION_ASSISTANT_SERVICE; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5286 | c.settingsAction = Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS; |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 5287 | c.clientLabel = R.string.notification_ranker_binding_label; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5288 | return c; |
| 5289 | } |
| 5290 | |
| 5291 | @Override |
| 5292 | protected IInterface asInterface(IBinder binder) { |
| 5293 | return INotificationListener.Stub.asInterface(binder); |
| 5294 | } |
| 5295 | |
| 5296 | @Override |
| 5297 | protected boolean checkType(IInterface service) { |
| 5298 | return service instanceof INotificationListener; |
| 5299 | } |
| 5300 | |
| 5301 | @Override |
| 5302 | protected void onServiceAdded(ManagedServiceInfo info) { |
| 5303 | mListeners.registerGuestService(info); |
| 5304 | } |
| 5305 | |
| 5306 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5307 | @GuardedBy("mNotificationLock") |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5308 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
| 5309 | mListeners.unregisterService(removed.service, removed.userid); |
| 5310 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5311 | |
| 5312 | public void onNotificationEnqueued(final NotificationRecord r) { |
| 5313 | final StatusBarNotification sbn = r.sbn; |
| 5314 | TrimCache trimCache = new TrimCache(sbn); |
| 5315 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5316 | // There should be only one, but it's a list, so while we enforce |
| 5317 | // singularity elsewhere, we keep it general here, to avoid surprises. |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5318 | for (final ManagedServiceInfo info : NotificationAssistants.this.getServices()) { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5319 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 5320 | if (!sbnVisible) { |
| 5321 | continue; |
| 5322 | } |
| 5323 | |
| 5324 | final int importance = r.getImportance(); |
| 5325 | final boolean fromUser = r.isImportanceFromUser(); |
| 5326 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 5327 | mHandler.post(new Runnable() { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5328 | @Override |
| 5329 | public void run() { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5330 | notifyEnqueued(info, sbnToPost); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5331 | } |
| 5332 | }); |
| 5333 | } |
| 5334 | } |
| 5335 | |
| 5336 | private void notifyEnqueued(final ManagedServiceInfo info, |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5337 | final StatusBarNotification sbn) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5338 | final INotificationListener assistant = (INotificationListener) info.service; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5339 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
| 5340 | try { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5341 | assistant.onNotificationEnqueued(sbnHolder); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5342 | } catch (RemoteException ex) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5343 | Log.e(TAG, "unable to notify assistant (enqueued): " + assistant, ex); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5344 | } |
| 5345 | } |
| 5346 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5347 | /** |
| 5348 | * asynchronously notify the assistant that a notification has been snoozed until a |
| 5349 | * context |
| 5350 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5351 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5352 | public void notifyAssistantSnoozedLocked(final StatusBarNotification sbn, |
| 5353 | final String snoozeCriterionId) { |
| 5354 | TrimCache trimCache = new TrimCache(sbn); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5355 | for (final ManagedServiceInfo info : getServices()) { |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5356 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
| 5357 | mHandler.post(new Runnable() { |
| 5358 | @Override |
| 5359 | public void run() { |
| 5360 | final INotificationListener assistant = |
| 5361 | (INotificationListener) info.service; |
| 5362 | StatusBarNotificationHolder sbnHolder |
| 5363 | = new StatusBarNotificationHolder(sbnToPost); |
| 5364 | try { |
| 5365 | assistant.onNotificationSnoozedUntilContext( |
| 5366 | sbnHolder, snoozeCriterionId); |
| 5367 | } catch (RemoteException ex) { |
| 5368 | Log.e(TAG, "unable to notify assistant (snoozed): " + assistant, ex); |
| 5369 | } |
| 5370 | } |
| 5371 | }); |
| 5372 | } |
| 5373 | } |
| 5374 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5375 | public boolean isEnabled() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5376 | return !getServices().isEmpty(); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5377 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5378 | } |
| 5379 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5380 | public class NotificationListeners extends ManagedServices { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5381 | static final String TAG_ENABLED_NOTIFICATION_LISTENERS = "enabled_listeners"; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5382 | |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5383 | private final ArraySet<ManagedServiceInfo> mLightTrimListeners = new ArraySet<>(); |
| 5384 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5385 | public NotificationListeners(IPackageManager pm) { |
| 5386 | super(getContext(), mNotificationLock, mUserProfiles, pm); |
| 5387 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | @Override |
| 5391 | protected Config getConfig() { |
| 5392 | Config c = new Config(); |
| 5393 | c.caption = "notification listener"; |
| 5394 | c.serviceInterface = NotificationListenerService.SERVICE_INTERFACE; |
Julia Reynolds | d1bf5f0 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5395 | c.xmlTag = TAG_ENABLED_NOTIFICATION_LISTENERS; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5396 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS; |
| 5397 | c.bindPermission = android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE; |
| 5398 | c.settingsAction = Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS; |
| 5399 | c.clientLabel = R.string.notification_listener_binding_label; |
| 5400 | return c; |
| 5401 | } |
| 5402 | |
| 5403 | @Override |
| 5404 | protected IInterface asInterface(IBinder binder) { |
| 5405 | return INotificationListener.Stub.asInterface(binder); |
| 5406 | } |
| 5407 | |
| 5408 | @Override |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5409 | protected boolean checkType(IInterface service) { |
| 5410 | return service instanceof INotificationListener; |
| 5411 | } |
| 5412 | |
| 5413 | @Override |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 5414 | public void onServiceAdded(ManagedServiceInfo info) { |
| 5415 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5416 | final NotificationRankingUpdate update; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5417 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5418 | update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5419 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5420 | try { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5421 | listener.onListenerConnected(update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5422 | } catch (RemoteException e) { |
| 5423 | // we tried |
| 5424 | } |
| 5425 | } |
| 5426 | |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5427 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5428 | @GuardedBy("mNotificationLock") |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5429 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 5430 | if (removeDisabledHints(removed)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5431 | updateListenerHintsLocked(); |
Christoph Studer | 0d6ef4b | 2014-12-02 15:00:48 +0100 | [diff] [blame] | 5432 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5433 | } |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5434 | mLightTrimListeners.remove(removed); |
| 5435 | } |
| 5436 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5437 | @GuardedBy("mNotificationLock") |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5438 | public void setOnNotificationPostedTrimLocked(ManagedServiceInfo info, int trim) { |
| 5439 | if (trim == TRIM_LIGHT) { |
| 5440 | mLightTrimListeners.add(info); |
| 5441 | } else { |
| 5442 | mLightTrimListeners.remove(info); |
| 5443 | } |
| 5444 | } |
| 5445 | |
| 5446 | public int getOnNotificationPostedTrim(ManagedServiceInfo info) { |
| 5447 | return mLightTrimListeners.contains(info) ? TRIM_LIGHT : TRIM_FULL; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5448 | } |
| 5449 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5450 | /** |
| 5451 | * asynchronously notify all listeners about a new notification |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5452 | * |
| 5453 | * <p> |
| 5454 | * Also takes care of removing a notification that has been visible to a listener before, |
| 5455 | * but isn't anymore. |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5456 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5457 | @GuardedBy("mNotificationLock") |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5458 | public void notifyPostedLocked(StatusBarNotification sbn, StatusBarNotification oldSbn) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5459 | // Lazily initialized snapshots of the notification. |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5460 | TrimCache trimCache = new TrimCache(sbn); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5461 | |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5462 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5463 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 5464 | boolean oldSbnVisible = oldSbn != null ? isVisibleToListener(oldSbn, info) : false; |
| 5465 | // This notification hasn't been and still isn't visible -> ignore. |
| 5466 | if (!oldSbnVisible && !sbnVisible) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5467 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5468 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5469 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5470 | |
| 5471 | // This notification became invisible -> remove the old one. |
| 5472 | if (oldSbnVisible && !sbnVisible) { |
| 5473 | final StatusBarNotification oldSbnLightClone = oldSbn.cloneLight(); |
| 5474 | mHandler.post(new Runnable() { |
| 5475 | @Override |
| 5476 | public void run() { |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 5477 | notifyRemoved(info, oldSbnLightClone, update, REASON_USER_STOPPED); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5478 | } |
| 5479 | }); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5480 | continue; |
| 5481 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5482 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5483 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5484 | mHandler.post(new Runnable() { |
| 5485 | @Override |
| 5486 | public void run() { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5487 | notifyPosted(info, sbnToPost, update); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5488 | } |
| 5489 | }); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5490 | } |
| 5491 | } |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5492 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5493 | /** |
| 5494 | * asynchronously notify all listeners about a removed notification |
| 5495 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5496 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 5497 | public void notifyRemovedLocked(StatusBarNotification sbn, int reason) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5498 | // make a copy in case changes are made to the underlying Notification object |
| 5499 | // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the |
| 5500 | // notification |
| 5501 | final StatusBarNotification sbnLight = sbn.cloneLight(); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5502 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5503 | if (!isVisibleToListener(sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5504 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5505 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5506 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5507 | mHandler.post(new Runnable() { |
| 5508 | @Override |
| 5509 | public void run() { |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 5510 | notifyRemoved(info, sbnLight, update, reason); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5511 | } |
| 5512 | }); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5513 | } |
| 5514 | } |
| 5515 | |
| 5516 | /** |
| 5517 | * asynchronously notify all listeners about a reordering of notifications |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5518 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5519 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5520 | public void notifyRankingUpdateLocked() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5521 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5522 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 5523 | continue; |
| 5524 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5525 | final NotificationRankingUpdate update = makeRankingUpdateLocked(serviceInfo); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5526 | mHandler.post(new Runnable() { |
| 5527 | @Override |
| 5528 | public void run() { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5529 | notifyRankingUpdate(serviceInfo, update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5530 | } |
| 5531 | }); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5532 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5533 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5534 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5535 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5536 | public void notifyListenerHintsChangedLocked(final int hints) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5537 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5538 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 5539 | continue; |
| 5540 | } |
| 5541 | mHandler.post(new Runnable() { |
| 5542 | @Override |
| 5543 | public void run() { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5544 | notifyListenerHintsChanged(serviceInfo, hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5545 | } |
| 5546 | }); |
| 5547 | } |
| 5548 | } |
| 5549 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5550 | public void notifyInterruptionFilterChanged(final int interruptionFilter) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5551 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5552 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 5553 | continue; |
| 5554 | } |
| 5555 | mHandler.post(new Runnable() { |
| 5556 | @Override |
| 5557 | public void run() { |
| 5558 | notifyInterruptionFilterChanged(serviceInfo, interruptionFilter); |
| 5559 | } |
| 5560 | }); |
| 5561 | } |
| 5562 | } |
| 5563 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5564 | protected void notifyNotificationChannelChanged(final String pkg, final UserHandle user, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5565 | final NotificationChannel channel, final int modificationType) { |
| 5566 | if (channel == null) { |
| 5567 | return; |
| 5568 | } |
| 5569 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5570 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5571 | continue; |
| 5572 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5573 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5574 | mHandler.post(new Runnable() { |
| 5575 | @Override |
| 5576 | public void run() { |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5577 | if (hasCompanionDevice(serviceInfo)) { |
| 5578 | notifyNotificationChannelChanged( |
| 5579 | serviceInfo, pkg, user, channel, modificationType); |
| 5580 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5581 | } |
| 5582 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5583 | } |
| 5584 | } |
| 5585 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5586 | protected void notifyNotificationChannelGroupChanged( |
| 5587 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
| 5588 | final int modificationType) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5589 | if (group == null) { |
| 5590 | return; |
| 5591 | } |
| 5592 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5593 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5594 | continue; |
| 5595 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5596 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5597 | mHandler.post(new Runnable() { |
| 5598 | @Override |
| 5599 | public void run() { |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5600 | if (hasCompanionDevice(serviceInfo)) { |
| 5601 | notifyNotificationChannelGroupChanged( |
| 5602 | serviceInfo, pkg, user, group, modificationType); |
| 5603 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5604 | } |
| 5605 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5606 | } |
| 5607 | } |
| 5608 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5609 | private void notifyPosted(final ManagedServiceInfo info, |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5610 | final StatusBarNotification sbn, NotificationRankingUpdate rankingUpdate) { |
Julia Reynolds | a75c752 | 2017-03-21 17:34:25 -0400 | [diff] [blame] | 5611 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5612 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5613 | try { |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5614 | listener.onNotificationPosted(sbnHolder, rankingUpdate); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5615 | } catch (RemoteException ex) { |
| 5616 | Log.e(TAG, "unable to notify listener (posted): " + listener, ex); |
| 5617 | } |
| 5618 | } |
| 5619 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5620 | private void notifyRemoved(ManagedServiceInfo info, StatusBarNotification sbn, |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 5621 | NotificationRankingUpdate rankingUpdate, int reason) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5622 | if (!info.enabledAndUserMatches(sbn.getUserId())) { |
| 5623 | return; |
| 5624 | } |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5625 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5626 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5627 | try { |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 5628 | listener.onNotificationRemoved(sbnHolder, rankingUpdate, reason); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5629 | } catch (RemoteException ex) { |
| 5630 | Log.e(TAG, "unable to notify listener (removed): " + listener, ex); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 5631 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5632 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5633 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5634 | private void notifyRankingUpdate(ManagedServiceInfo info, |
| 5635 | NotificationRankingUpdate rankingUpdate) { |
| 5636 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5637 | try { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5638 | listener.onNotificationRankingUpdate(rankingUpdate); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5639 | } catch (RemoteException ex) { |
| 5640 | Log.e(TAG, "unable to notify listener (ranking update): " + listener, ex); |
| 5641 | } |
| 5642 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5643 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5644 | private void notifyListenerHintsChanged(ManagedServiceInfo info, int hints) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5645 | final INotificationListener listener = (INotificationListener) info.service; |
| 5646 | try { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5647 | listener.onListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5648 | } catch (RemoteException ex) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5649 | Log.e(TAG, "unable to notify listener (listener hints): " + listener, ex); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5650 | } |
| 5651 | } |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 5652 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5653 | private void notifyInterruptionFilterChanged(ManagedServiceInfo info, |
| 5654 | int interruptionFilter) { |
| 5655 | final INotificationListener listener = (INotificationListener) info.service; |
| 5656 | try { |
| 5657 | listener.onInterruptionFilterChanged(interruptionFilter); |
| 5658 | } catch (RemoteException ex) { |
| 5659 | Log.e(TAG, "unable to notify listener (interruption filter): " + listener, ex); |
| 5660 | } |
| 5661 | } |
| 5662 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5663 | void notifyNotificationChannelChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5664 | final String pkg, final UserHandle user, final NotificationChannel channel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5665 | final int modificationType) { |
| 5666 | final INotificationListener listener = (INotificationListener) info.service; |
| 5667 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5668 | listener.onNotificationChannelModification(pkg, user, channel, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5669 | } catch (RemoteException ex) { |
| 5670 | Log.e(TAG, "unable to notify listener (channel changed): " + listener, ex); |
| 5671 | } |
| 5672 | } |
| 5673 | |
| 5674 | private void notifyNotificationChannelGroupChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5675 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5676 | final int modificationType) { |
| 5677 | final INotificationListener listener = (INotificationListener) info.service; |
| 5678 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5679 | listener.onNotificationChannelGroupModification(pkg, user, group, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5680 | } catch (RemoteException ex) { |
| 5681 | Log.e(TAG, "unable to notify listener (channel group changed): " + listener, ex); |
| 5682 | } |
| 5683 | } |
| 5684 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5685 | public boolean isListenerPackage(String packageName) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 5686 | if (packageName == null) { |
| 5687 | return false; |
| 5688 | } |
| 5689 | // TODO: clean up locking object later |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5690 | synchronized (mNotificationLock) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5691 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 5692 | if (packageName.equals(serviceInfo.component.getPackageName())) { |
| 5693 | return true; |
| 5694 | } |
| 5695 | } |
| 5696 | } |
| 5697 | return false; |
| 5698 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5699 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5700 | |
| 5701 | public static final class DumpFilter { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5702 | public boolean filtered = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5703 | public String pkgFilter; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 5704 | public boolean zen; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 5705 | public long since; |
| 5706 | public boolean stats; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5707 | public boolean redact = true; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 5708 | public boolean proto = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5709 | |
| 5710 | public static DumpFilter parseFromArguments(String[] args) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5711 | final DumpFilter filter = new DumpFilter(); |
| 5712 | for (int ai = 0; ai < args.length; ai++) { |
| 5713 | final String a = args[ai]; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 5714 | if ("--proto".equals(args[0])) { |
| 5715 | filter.proto = true; |
| 5716 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5717 | if ("--noredact".equals(a) || "--reveal".equals(a)) { |
| 5718 | filter.redact = false; |
| 5719 | } else if ("p".equals(a) || "pkg".equals(a) || "--package".equals(a)) { |
| 5720 | if (ai < args.length-1) { |
| 5721 | ai++; |
| 5722 | filter.pkgFilter = args[ai].trim().toLowerCase(); |
| 5723 | if (filter.pkgFilter.isEmpty()) { |
| 5724 | filter.pkgFilter = null; |
| 5725 | } else { |
| 5726 | filter.filtered = true; |
| 5727 | } |
| 5728 | } |
| 5729 | } else if ("--zen".equals(a) || "zen".equals(a)) { |
| 5730 | filter.filtered = true; |
| 5731 | filter.zen = true; |
| 5732 | } else if ("--stats".equals(a)) { |
| 5733 | filter.stats = true; |
| 5734 | if (ai < args.length-1) { |
| 5735 | ai++; |
Tobias Thierer | 28532d0 | 2016-04-21 14:52:10 +0100 | [diff] [blame] | 5736 | filter.since = Long.parseLong(args[ai]); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5737 | } else { |
| 5738 | filter.since = 0; |
| 5739 | } |
| 5740 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5741 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5742 | return filter; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5743 | } |
| 5744 | |
| 5745 | public boolean matches(StatusBarNotification sbn) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5746 | if (!filtered) return true; |
| 5747 | return zen ? true : sbn != null |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 5748 | && (matches(sbn.getPackageName()) || matches(sbn.getOpPkg())); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5749 | } |
| 5750 | |
| 5751 | public boolean matches(ComponentName component) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5752 | if (!filtered) return true; |
| 5753 | return zen ? true : component != null && matches(component.getPackageName()); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5754 | } |
| 5755 | |
| 5756 | public boolean matches(String pkg) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5757 | if (!filtered) return true; |
| 5758 | return zen ? true : pkg != null && pkg.toLowerCase().contains(pkgFilter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 5759 | } |
| 5760 | |
| 5761 | @Override |
| 5762 | public String toString() { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 5763 | return stats ? "stats" : zen ? "zen" : ('\'' + pkgFilter + '\''); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5764 | } |
| 5765 | } |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5766 | |
| 5767 | /** |
| 5768 | * Wrapper for a StatusBarNotification object that allows transfer across a oneway |
| 5769 | * binder without sending large amounts of data over a oneway transaction. |
| 5770 | */ |
| 5771 | private static final class StatusBarNotificationHolder |
| 5772 | extends IStatusBarNotificationHolder.Stub { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 5773 | private StatusBarNotification mValue; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5774 | |
| 5775 | public StatusBarNotificationHolder(StatusBarNotification value) { |
| 5776 | mValue = value; |
| 5777 | } |
| 5778 | |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 5779 | /** 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] | 5780 | @Override |
| 5781 | public StatusBarNotification get() { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 5782 | StatusBarNotification value = mValue; |
| 5783 | mValue = null; |
| 5784 | return value; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5785 | } |
| 5786 | } |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5787 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5788 | private class ShellCmd extends ShellCommand { |
| 5789 | public static final String USAGE = "help\n" |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 5790 | + "allow_listener COMPONENT\n" |
| 5791 | + "disallow_listener COMPONENT\n" |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5792 | + "set_assistant COMPONENT\n" |
| 5793 | + "remove_assistant COMPONENT\n" |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5794 | + "allow_dnd PACKAGE\n" |
| 5795 | + "disallow_dnd PACKAGE"; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5796 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5797 | @Override |
| 5798 | public int onCommand(String cmd) { |
Felipe Leme | 68d8041 | 2017-07-14 11:18:08 -0700 | [diff] [blame] | 5799 | if (cmd == null) { |
| 5800 | return handleDefaultCommands(cmd); |
| 5801 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 5802 | final PrintWriter pw = getOutPrintWriter(); |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 5803 | try { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5804 | switch (cmd) { |
| 5805 | case "allow_dnd": { |
| 5806 | getBinderService().setNotificationPolicyAccessGranted( |
| 5807 | getNextArgRequired(), true); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5808 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5809 | break; |
| 5810 | |
| 5811 | case "disallow_dnd": { |
| 5812 | getBinderService().setNotificationPolicyAccessGranted( |
| 5813 | getNextArgRequired(), false); |
| 5814 | } |
| 5815 | break; |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 5816 | case "allow_listener": { |
| 5817 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 5818 | if (cn == null) { |
| 5819 | pw.println("Invalid listener - must be a ComponentName"); |
| 5820 | return -1; |
| 5821 | } |
| 5822 | getBinderService().setNotificationListenerAccessGranted(cn, true); |
| 5823 | } |
| 5824 | break; |
| 5825 | case "disallow_listener": { |
| 5826 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 5827 | if (cn == null) { |
| 5828 | pw.println("Invalid listener - must be a ComponentName"); |
| 5829 | return -1; |
| 5830 | } |
| 5831 | getBinderService().setNotificationListenerAccessGranted(cn, false); |
| 5832 | } |
| 5833 | break; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 5834 | case "allow_assistant": { |
| 5835 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 5836 | if (cn == null) { |
| 5837 | pw.println("Invalid assistant - must be a ComponentName"); |
| 5838 | return -1; |
| 5839 | } |
| 5840 | getBinderService().setNotificationAssistantAccessGranted(cn, true); |
| 5841 | } |
| 5842 | break; |
| 5843 | case "disallow_assistant": { |
| 5844 | ComponentName cn = ComponentName.unflattenFromString(getNextArgRequired()); |
| 5845 | if (cn == null) { |
| 5846 | pw.println("Invalid assistant - must be a ComponentName"); |
| 5847 | return -1; |
| 5848 | } |
| 5849 | getBinderService().setNotificationAssistantAccessGranted(cn, false); |
| 5850 | } |
| 5851 | break; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5852 | |
| 5853 | default: |
| 5854 | return handleDefaultCommands(cmd); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5855 | } |
Julia Reynolds | b71f1d3 | 2017-06-23 13:32:09 -0400 | [diff] [blame] | 5856 | } catch (Exception e) { |
| 5857 | pw.println("Error occurred. Check logcat for details. " + e.getMessage()); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5858 | Slog.e(TAG, "Error running shell command", e); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5859 | } |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5860 | return 0; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5861 | } |
| 5862 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 5863 | @Override |
| 5864 | public void onHelp() { |
| 5865 | getOutPrintWriter().println(USAGE); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5866 | } |
| 5867 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5868 | } |