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 | d71c5a9 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 23 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 24 | import static android.service.notification.NotificationListenerService |
| 25 | .NOTIFICATION_CHANNEL_OR_GROUP_ADDED; |
| 26 | import static android.service.notification.NotificationListenerService |
| 27 | .NOTIFICATION_CHANNEL_OR_GROUP_DELETED; |
| 28 | import static android.service.notification.NotificationListenerService |
| 29 | .NOTIFICATION_CHANNEL_OR_GROUP_UPDATED; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 30 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL; |
| 31 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL; |
| 32 | import static android.service.notification.NotificationListenerService.REASON_CHANNEL_BANNED; |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 33 | import static android.service.notification.NotificationListenerService.REASON_CANCEL; |
| 34 | import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL; |
| 35 | import static android.service.notification.NotificationListenerService.REASON_CLICK; |
| 36 | import static android.service.notification.NotificationListenerService.REASON_ERROR; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 37 | import static android.service.notification.NotificationListenerService.REASON_GROUP_SUMMARY_CANCELED; |
| 38 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL; |
| 39 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL_ALL; |
| 40 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_BANNED; |
| 41 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_CHANGED; |
| 42 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_SUSPENDED; |
| 43 | import static android.service.notification.NotificationListenerService.REASON_PROFILE_TURNED_OFF; |
| 44 | import static android.service.notification.NotificationListenerService.REASON_SNOOZED; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 45 | import static android.service.notification.NotificationListenerService.REASON_TIMEOUT; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 46 | import static android.service.notification.NotificationListenerService.REASON_UNAUTOBUNDLED; |
| 47 | import static android.service.notification.NotificationListenerService.REASON_USER_STOPPED; |
Jason Monk | 6350674 | 2015-12-16 12:06:51 -0500 | [diff] [blame] | 48 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_EFFECTS; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 49 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_NOTIFICATION_EFFECTS; |
| 50 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS; |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 51 | import static android.service.notification.NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF; |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 52 | import static android.service.notification.NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_ON; |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 53 | import static android.service.notification.NotificationListenerService.TRIM_FULL; |
| 54 | import static android.service.notification.NotificationListenerService.TRIM_LIGHT; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 55 | |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 56 | import static android.view.Display.DEFAULT_DISPLAY; |
| 57 | import static android.view.WindowManager.LayoutParams.TYPE_TOAST; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 58 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
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; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 91 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import android.content.pm.PackageManager; |
| 93 | import android.content.pm.PackageManager.NameNotFoundException; |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 94 | import android.content.pm.ParceledListSlice; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.content.res.Resources; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 96 | import android.database.ContentObserver; |
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; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 100 | import android.media.ToneGenerator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | import android.os.Binder; |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 103 | import android.os.Build; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 104 | import android.os.Bundle; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 105 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | import android.os.Handler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 107 | import android.os.HandlerThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import android.os.IBinder; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 109 | import android.os.IInterface; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 110 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | import android.os.Message; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 112 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 113 | import android.os.RemoteException; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 114 | import android.os.ServiceManager; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 115 | import android.os.SystemClock; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 116 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 117 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | import android.os.Vibrator; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 119 | import android.os.VibrationEffect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | import android.provider.Settings; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 121 | import android.service.notification.Adjustment; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 122 | import android.service.notification.Condition; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 123 | import android.service.notification.IConditionProvider; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 124 | import android.service.notification.INotificationListener; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 125 | import android.service.notification.IStatusBarNotificationHolder; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 126 | import android.service.notification.NotificationAssistantService; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 127 | import android.service.notification.NotificationListenerService; |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 128 | import android.service.notification.NotificationRankingUpdate; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 129 | import android.service.notification.NotificationRecordProto; |
| 130 | import android.service.notification.NotificationServiceDumpProto; |
| 131 | import android.service.notification.NotificationServiceProto; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 132 | import android.service.notification.SnoozeCriterion; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 133 | import android.service.notification.StatusBarNotification; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 134 | import android.service.notification.ZenModeConfig; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 135 | import android.service.notification.ZenModeProto; |
Sailesh Nepal | e8bde70 | 2017-07-21 11:44:04 -0700 | [diff] [blame] | 136 | import android.telecom.TelecomManager; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 137 | import android.telephony.PhoneStateListener; |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 138 | import android.telephony.TelephonyManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 139 | import android.text.TextUtils; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 140 | import android.util.ArrayMap; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 141 | import android.util.ArraySet; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 142 | import android.util.AtomicFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | import android.util.Log; |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 144 | import android.util.Slog; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 145 | import android.util.SparseArray; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 146 | import android.util.Xml; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 147 | import android.util.proto.ProtoOutputStream; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 148 | import android.view.WindowManagerInternal; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 149 | import android.view.accessibility.AccessibilityEvent; |
| 150 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | import android.widget.Toast; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 152 | |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 153 | import com.android.internal.R; |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 154 | import com.android.internal.annotations.GuardedBy; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 155 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 156 | import com.android.internal.logging.MetricsLogger; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 157 | import com.android.internal.logging.nano.MetricsProto; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 158 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 159 | import com.android.internal.statusbar.NotificationVisibility; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 160 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 161 | import com.android.internal.util.DumpUtils; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 162 | import com.android.internal.util.FastXmlSerializer; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 163 | import com.android.internal.util.Preconditions; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 164 | import com.android.server.DeviceIdleController; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 165 | import com.android.server.EventLogTags; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 166 | import com.android.server.LocalServices; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 167 | import com.android.server.SystemService; |
| 168 | import com.android.server.lights.Light; |
| 169 | import com.android.server.lights.LightsManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 170 | import com.android.server.notification.ManagedServices.ManagedServiceInfo; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 171 | import com.android.server.policy.PhoneWindowManager; |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 172 | import com.android.server.statusbar.StatusBarManagerInternal; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 173 | import com.android.server.notification.ManagedServices.UserProfiles; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 174 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 175 | import libcore.io.IoUtils; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 176 | |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 177 | import org.json.JSONException; |
| 178 | import org.json.JSONObject; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 179 | import org.xmlpull.v1.XmlPullParser; |
| 180 | import org.xmlpull.v1.XmlPullParserException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 181 | import org.xmlpull.v1.XmlSerializer; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 182 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 183 | import java.io.ByteArrayInputStream; |
| 184 | import java.io.ByteArrayOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 185 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | import java.io.FileDescriptor; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 187 | import java.io.FileInputStream; |
| 188 | import java.io.FileNotFoundException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 189 | import java.io.FileOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 190 | import java.io.IOException; |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 191 | import java.io.InputStream; |
| 192 | import java.io.OutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 194 | import java.nio.charset.StandardCharsets; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 195 | import java.util.ArrayDeque; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | import java.util.ArrayList; |
Michael Wright | bc4d0d9 | 2017-03-23 18:57:57 +0000 | [diff] [blame] | 197 | import java.util.Arrays; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 198 | import java.util.Iterator; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 199 | import java.util.List; |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 200 | import java.util.Map; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 201 | import java.util.Map.Entry; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 202 | import java.util.Objects; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 203 | import java.util.concurrent.TimeUnit; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 204 | |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 205 | /** {@hide} */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 206 | public class NotificationManagerService extends SystemService { |
| 207 | static final String TAG = "NotificationService"; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 208 | static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Selim Cinek | 4041249 | 2015-12-08 18:03:22 -0800 | [diff] [blame] | 209 | public static final boolean ENABLE_CHILD_NOTIFICATIONS |
| 210 | = SystemProperties.getBoolean("debug.child_notifs", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 212 | static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 213 | static final float DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE = 5f; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 214 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | // message codes |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 216 | static final int MESSAGE_TIMEOUT = 2; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 217 | static final int MESSAGE_SAVE_POLICY_FILE = 3; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 218 | static final int MESSAGE_SEND_RANKING_UPDATE = 4; |
| 219 | static final int MESSAGE_LISTENER_HINTS_CHANGED = 5; |
| 220 | static final int MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED = 6; |
| 221 | |
| 222 | // ranking thread messages |
| 223 | private static final int MESSAGE_RECONSIDER_RANKING = 1000; |
| 224 | private static final int MESSAGE_RANKING_SORT = 1001; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 226 | static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 227 | static final int SHORT_DELAY = 2000; // 2 seconds |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 228 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 229 | static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 230 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 231 | static final long SNOOZE_UNTIL_UNSPECIFIED = -1; |
| 232 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 233 | 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] | 234 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 235 | static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION; |
Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 236 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 237 | static final boolean ENABLE_BLOCKED_TOASTS = true; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 238 | |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 239 | // When #matchesCallFilter is called from the ringer, wait at most |
| 240 | // 3s to resolve the contacts. This timeout is required since |
| 241 | // ContactsProvider might take a long time to start up. |
| 242 | // |
| 243 | // Return STARRED_CONTACT when the timeout is hit in order to avoid |
| 244 | // missed calls in ZEN mode "Important". |
| 245 | static final int MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS = 3000; |
| 246 | static final float MATCHES_CALL_FILTER_TIMEOUT_AFFINITY = |
| 247 | ValidateNotificationPeople.STARRED_CONTACT; |
| 248 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 249 | /** notification_enqueue status value for a newly enqueued notification. */ |
| 250 | private static final int EVENTLOG_ENQUEUE_STATUS_NEW = 0; |
| 251 | |
| 252 | /** notification_enqueue status value for an existing notification. */ |
| 253 | private static final int EVENTLOG_ENQUEUE_STATUS_UPDATE = 1; |
| 254 | |
| 255 | /** notification_enqueue status value for an ignored notification. */ |
| 256 | private static final int EVENTLOG_ENQUEUE_STATUS_IGNORED = 2; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 257 | private static final long MIN_PACKAGE_OVERRATE_LOG_INTERVAL = 5000; // milliseconds |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 258 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 259 | private static final long DELAY_FOR_ASSISTANT_TIME = 100; |
| 260 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 261 | private static final String ACTION_NOTIFICATION_TIMEOUT = |
| 262 | NotificationManagerService.class.getSimpleName() + ".TIMEOUT"; |
| 263 | private static final int REQUEST_CODE_TIMEOUT = 1; |
| 264 | private static final String SCHEME_TIMEOUT = "timeout"; |
| 265 | private static final String EXTRA_KEY = "key"; |
| 266 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 267 | private IActivityManager mAm; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 268 | private IPackageManager mPackageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 269 | private PackageManager mPackageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 270 | AudioManager mAudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 271 | AudioManagerInternal mAudioManagerInternal; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 272 | @Nullable StatusBarManagerInternal mStatusBar; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 273 | Vibrator mVibrator; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 274 | private WindowManagerInternal mWindowManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 275 | private AlarmManager mAlarmManager; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 276 | private ICompanionDeviceManager mCompanionManager; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | final IBinder mForegroundToken = new Binder(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 279 | private Handler mHandler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 280 | private final HandlerThread mRankingThread = new HandlerThread("ranker", |
| 281 | Process.THREAD_PRIORITY_BACKGROUND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 283 | private Light mNotificationLight; |
| 284 | Light mAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 285 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 286 | private long[] mFallbackVibrationPattern; |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 287 | private boolean mUseAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 288 | boolean mSystemReady; |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 289 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 290 | private boolean mDisableNotificationEffects; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 291 | private int mCallState; |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 292 | private String mSoundNotificationKey; |
| 293 | private String mVibrateNotificationKey; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 295 | private final SparseArray<ArraySet<ManagedServiceInfo>> mListenersDisablingEffects = |
| 296 | new SparseArray<ArraySet<ManagedServiceInfo>>(); |
| 297 | private List<ComponentName> mEffectsSuppressors = new ArrayList<ComponentName>(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 298 | private int mListenerHints; // right now, all hints are global |
John Spurlock | 8310410 | 2015-02-12 23:25:12 -0500 | [diff] [blame] | 299 | private int mInterruptionFilter = NotificationListenerService.INTERRUPTION_FILTER_UNKNOWN; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 300 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 301 | // for enabling and disabling notification pulse behavior |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 302 | private boolean mScreenOn = true; |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 303 | private boolean mInCall = false; |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 304 | private boolean mNotificationPulseEnabled; |
| 305 | |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 306 | // for generating notification tones in-call |
| 307 | private ToneGenerator mInCallToneGenerator; |
| 308 | private final Object mInCallToneGeneratorLock = new Object(); |
| 309 | |
Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 310 | // used as a mutex for access to all active notifications & listeners |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 311 | final Object mNotificationLock = new Object(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 312 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 313 | final ArrayList<NotificationRecord> mNotificationList = |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 314 | new ArrayList<NotificationRecord>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 315 | @GuardedBy("mNotificationLock") |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 316 | final ArrayMap<String, NotificationRecord> mNotificationsByKey = |
| 317 | new ArrayMap<String, NotificationRecord>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 318 | @GuardedBy("mNotificationLock") |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 319 | final ArrayList<NotificationRecord> mEnqueuedNotifications = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 320 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 321 | final ArrayMap<Integer, ArrayMap<String, String>> mAutobundledSummaries = new ArrayMap<>(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 322 | final ArrayList<ToastRecord> mToastQueue = new ArrayList<ToastRecord>(); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 323 | final ArrayMap<String, NotificationRecord> mSummaryByGroupKey = new ArrayMap<>(); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 324 | final PolicyAccess mPolicyAccess = new PolicyAccess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 326 | // The last key in this list owns the hardware. |
| 327 | ArrayList<String> mLights = new ArrayList<>(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 328 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 329 | private AppOpsManager mAppOps; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 330 | private UsageStatsManagerInternal mAppUsageStats; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 331 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 332 | private Archive mArchive; |
| 333 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 334 | // Persistent storage for notification policy |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 335 | private AtomicFile mPolicyFile; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 336 | |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 337 | private static final int DB_VERSION = 1; |
| 338 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 339 | private static final String TAG_NOTIFICATION_POLICY = "notification-policy"; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 340 | private static final String ATTR_VERSION = "version"; |
| 341 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 342 | private RankingHelper mRankingHelper; |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 343 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 344 | private final UserProfiles mUserProfiles = new UserProfiles(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 345 | private NotificationListeners mListeners; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 346 | private NotificationAssistants mNotificationAssistants; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 347 | private ConditionProviders mConditionProviders; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 348 | private NotificationUsageStats mUsageStats; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 349 | |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 350 | private static final int MY_UID = Process.myUid(); |
| 351 | private static final int MY_PID = Process.myPid(); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 352 | private static final IBinder WHITELIST_TOKEN = new Binder(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 353 | private RankingHandler mRankingHandler; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 354 | private long mLastOverRateLogTime; |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 355 | private float mMaxPackageEnqueueRate = DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE; |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 356 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 357 | private SnoozeHelper mSnoozeHelper; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 358 | private GroupHelper mGroupHelper; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 359 | private boolean mIsTelevision; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 360 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 361 | private static class Archive { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 362 | final int mBufferSize; |
| 363 | final ArrayDeque<StatusBarNotification> mBuffer; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 364 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 365 | public Archive(int size) { |
| 366 | mBufferSize = size; |
| 367 | mBuffer = new ArrayDeque<StatusBarNotification>(mBufferSize); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 368 | } |
Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 369 | |
Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 370 | public String toString() { |
| 371 | final StringBuilder sb = new StringBuilder(); |
| 372 | final int N = mBuffer.size(); |
| 373 | sb.append("Archive ("); |
| 374 | sb.append(N); |
| 375 | sb.append(" notification"); |
| 376 | sb.append((N==1)?")":"s)"); |
| 377 | return sb.toString(); |
| 378 | } |
| 379 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 380 | public void record(StatusBarNotification nr) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 381 | if (mBuffer.size() == mBufferSize) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 382 | mBuffer.removeFirst(); |
| 383 | } |
Daniel Sandler | 26b81d5 | 2013-05-20 20:56:43 -0400 | [diff] [blame] | 384 | |
| 385 | // We don't want to store the heavy bits of the notification in the archive, |
| 386 | // but other clients in the system process might be using the object, so we |
| 387 | // store a (lightened) copy. |
| 388 | mBuffer.addLast(nr.cloneLight()); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 389 | } |
| 390 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 391 | public Iterator<StatusBarNotification> descendingIterator() { |
| 392 | return mBuffer.descendingIterator(); |
| 393 | } |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 394 | |
| 395 | public StatusBarNotification[] getArray(int count) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 396 | if (count == 0) count = mBufferSize; |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 397 | final StatusBarNotification[] a |
| 398 | = new StatusBarNotification[Math.min(count, mBuffer.size())]; |
| 399 | Iterator<StatusBarNotification> iter = descendingIterator(); |
| 400 | int i=0; |
| 401 | while (iter.hasNext() && i < count) { |
| 402 | a[i++] = iter.next(); |
| 403 | } |
| 404 | return a; |
| 405 | } |
| 406 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 407 | } |
| 408 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 409 | private void readPolicyXml(InputStream stream, boolean forRestore) |
| 410 | throws XmlPullParserException, NumberFormatException, IOException { |
| 411 | final XmlPullParser parser = Xml.newPullParser(); |
| 412 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
| 413 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 414 | while (parser.next() != END_DOCUMENT) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 415 | mZenModeHelper.readXml(parser, forRestore); |
| 416 | mRankingHelper.readXml(parser, forRestore); |
| 417 | } |
| 418 | } |
| 419 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 420 | private void loadPolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 421 | if (DBG) Slog.d(TAG, "loadPolicyFile"); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 422 | synchronized (mPolicyFile) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 423 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 424 | FileInputStream infile = null; |
| 425 | try { |
| 426 | infile = mPolicyFile.openRead(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 427 | readPolicyXml(infile, false /*forRestore*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 428 | } catch (FileNotFoundException e) { |
| 429 | // No data yet |
| 430 | } catch (IOException e) { |
| 431 | Log.wtf(TAG, "Unable to read notification policy", e); |
| 432 | } catch (NumberFormatException e) { |
| 433 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 434 | } catch (XmlPullParserException e) { |
| 435 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 436 | } finally { |
| 437 | IoUtils.closeQuietly(infile); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | public void savePolicyFile() { |
| 443 | mHandler.removeMessages(MESSAGE_SAVE_POLICY_FILE); |
| 444 | mHandler.sendEmptyMessage(MESSAGE_SAVE_POLICY_FILE); |
| 445 | } |
| 446 | |
| 447 | private void handleSavePolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 448 | if (DBG) Slog.d(TAG, "handleSavePolicyFile"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 449 | synchronized (mPolicyFile) { |
| 450 | final FileOutputStream stream; |
| 451 | try { |
| 452 | stream = mPolicyFile.startWrite(); |
| 453 | } catch (IOException e) { |
| 454 | Slog.w(TAG, "Failed to save policy file", e); |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | try { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 459 | writePolicyXml(stream, false /*forBackup*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 460 | mPolicyFile.finishWrite(stream); |
| 461 | } catch (IOException e) { |
| 462 | Slog.w(TAG, "Failed to save policy file, restoring backup", e); |
| 463 | mPolicyFile.failWrite(stream); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 464 | } |
| 465 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 466 | BackupManager.dataChanged(getContext().getPackageName()); |
| 467 | } |
| 468 | |
| 469 | private void writePolicyXml(OutputStream stream, boolean forBackup) throws IOException { |
| 470 | final XmlSerializer out = new FastXmlSerializer(); |
| 471 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
| 472 | out.startDocument(null, true); |
| 473 | out.startTag(null, TAG_NOTIFICATION_POLICY); |
| 474 | out.attribute(null, ATTR_VERSION, Integer.toString(DB_VERSION)); |
| 475 | mZenModeHelper.writeXml(out, forBackup); |
| 476 | mRankingHelper.writeXml(out, forBackup); |
| 477 | out.endTag(null, TAG_NOTIFICATION_POLICY); |
| 478 | out.endDocument(); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 479 | } |
| 480 | |
Chris Wren | 66189fc | 2015-06-25 14:04:33 -0400 | [diff] [blame] | 481 | /** Use this to check if a package can post a notification or toast. */ |
| 482 | private boolean checkNotificationOp(String pkg, int uid) { |
| 483 | return mAppOps.checkOp(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg) |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 484 | == AppOpsManager.MODE_ALLOWED && !isPackageSuspendedForUser(pkg, uid); |
Chris Wren | 66189fc | 2015-06-25 14:04:33 -0400 | [diff] [blame] | 485 | } |
| 486 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | private static final class ToastRecord |
| 488 | { |
| 489 | final int pid; |
| 490 | final String pkg; |
| 491 | final ITransientNotification callback; |
| 492 | int duration; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 493 | Binder token; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 495 | ToastRecord(int pid, String pkg, ITransientNotification callback, int duration, |
| 496 | Binder token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 | this.pid = pid; |
| 498 | this.pkg = pkg; |
| 499 | this.callback = callback; |
| 500 | this.duration = duration; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 501 | this.token = token; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | void update(int duration) { |
| 505 | this.duration = duration; |
| 506 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 507 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 508 | void dump(PrintWriter pw, String prefix, DumpFilter filter) { |
| 509 | if (filter != null && !filter.matches(pkg)) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | pw.println(prefix + this); |
| 511 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 512 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | @Override |
| 514 | public final String toString() |
| 515 | { |
| 516 | return "ToastRecord{" |
| 517 | + Integer.toHexString(System.identityHashCode(this)) |
| 518 | + " pkg=" + pkg |
| 519 | + " callback=" + callback |
| 520 | + " duration=" + duration; |
| 521 | } |
| 522 | } |
| 523 | |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 524 | @VisibleForTesting |
| 525 | final NotificationDelegate mNotificationDelegate = new NotificationDelegate() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 527 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | public void onSetDisabled(int status) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 529 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 530 | mDisableNotificationEffects = |
| 531 | (status & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 532 | if (disableNotificationEffects(null) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | // cancel whatever's going on |
| 534 | long identity = Binder.clearCallingIdentity(); |
| 535 | try { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 536 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 537 | if (player != null) { |
| 538 | player.stopAsync(); |
| 539 | } |
| 540 | } catch (RemoteException e) { |
| 541 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | Binder.restoreCallingIdentity(identity); |
| 543 | } |
| 544 | |
| 545 | identity = Binder.clearCallingIdentity(); |
| 546 | try { |
| 547 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 548 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | Binder.restoreCallingIdentity(identity); |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 555 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 556 | public void onClearAll(int callingUid, int callingPid, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 557 | synchronized (mNotificationLock) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 558 | cancelAllLocked(callingUid, callingPid, userId, REASON_CANCEL_ALL, null, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 559 | /*includeCurrentProfiles*/ true); |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 560 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 563 | @Override |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 564 | public void onNotificationClick(int callingUid, int callingPid, String key) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 565 | synchronized (mNotificationLock) { |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 566 | NotificationRecord r = mNotificationsByKey.get(key); |
| 567 | if (r == null) { |
| 568 | Log.w(TAG, "No notification with key: " + key); |
| 569 | return; |
| 570 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 571 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 572 | MetricsLogger.action(r.getLogMaker(now) |
| 573 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
| 574 | .setType(MetricsEvent.TYPE_ACTION)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 575 | EventLogTags.writeNotificationClicked(key, |
| 576 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
| 577 | |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 578 | StatusBarNotification sbn = r.sbn; |
| 579 | cancelNotification(callingUid, callingPid, sbn.getPackageName(), sbn.getTag(), |
| 580 | sbn.getId(), Notification.FLAG_AUTO_CANCEL, |
| 581 | Notification.FLAG_FOREGROUND_SERVICE, false, r.getUserId(), |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 582 | REASON_CLICK, null); |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 583 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 586 | @Override |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 587 | public void onNotificationActionClick(int callingUid, int callingPid, String key, |
| 588 | int actionIndex) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 589 | synchronized (mNotificationLock) { |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 590 | NotificationRecord r = mNotificationsByKey.get(key); |
| 591 | if (r == null) { |
| 592 | Log.w(TAG, "No notification with key: " + key); |
| 593 | return; |
| 594 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 595 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 596 | MetricsLogger.action(r.getLogMaker(now) |
| 597 | .setCategory(MetricsEvent.NOTIFICATION_ITEM_ACTION) |
| 598 | .setType(MetricsEvent.TYPE_ACTION) |
| 599 | .setSubtype(actionIndex)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 600 | EventLogTags.writeNotificationActionClicked(key, actionIndex, |
| 601 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 602 | // TODO: Log action click via UsageStats. |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 607 | public void onNotificationClear(int callingUid, int callingPid, |
| 608 | String pkg, String tag, int id, int userId) { |
| 609 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 610 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 611 | true, userId, REASON_CANCEL, null); |
Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 612 | } |
| 613 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 614 | @Override |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 615 | public void onPanelRevealed(boolean clearEffects, int items) { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 616 | MetricsLogger.visible(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Chris Wren | 621933f | 2017-06-14 15:59:03 -0400 | [diff] [blame] | 617 | MetricsLogger.histogram(getContext(), "note_load", items); |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 618 | EventLogTags.writeNotificationPanelRevealed(items); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 619 | if (clearEffects) { |
| 620 | clearEffects(); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | @Override |
| 625 | public void onPanelHidden() { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 626 | MetricsLogger.hidden(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 627 | EventLogTags.writeNotificationPanelHidden(); |
| 628 | } |
| 629 | |
| 630 | @Override |
| 631 | public void clearEffects() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 632 | synchronized (mNotificationLock) { |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 633 | if (DBG) Slog.d(TAG, "clearEffects"); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 634 | clearSoundLocked(); |
| 635 | clearVibrateLocked(); |
| 636 | clearLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | } |
| 638 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 639 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 640 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 641 | 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] | 642 | int uid, int initialPid, String message, int userId) { |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 643 | Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id |
| 644 | + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")"); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 645 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, 0, false, userId, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 646 | REASON_ERROR, null); |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 647 | long ident = Binder.clearCallingIdentity(); |
| 648 | try { |
Christopher Tate | 8aa8fe1 | 2017-01-20 17:50:32 -0800 | [diff] [blame] | 649 | ActivityManager.getService().crashApplication(uid, initialPid, pkg, -1, |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 650 | "Bad notification posted from package " + pkg |
| 651 | + ": " + message); |
| 652 | } catch (RemoteException e) { |
| 653 | } |
| 654 | Binder.restoreCallingIdentity(ident); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 655 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 656 | |
| 657 | @Override |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 658 | public void onNotificationVisibilityChanged(NotificationVisibility[] newlyVisibleKeys, |
| 659 | NotificationVisibility[] noLongerVisibleKeys) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 660 | synchronized (mNotificationLock) { |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 661 | for (NotificationVisibility nv : newlyVisibleKeys) { |
| 662 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 663 | if (r == null) continue; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 664 | r.setVisibility(true, nv.rank); |
| 665 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 666 | } |
| 667 | // Note that we might receive this event after notifications |
| 668 | // have already left the system, e.g. after dismissing from the |
| 669 | // shade. Hence not finding notifications in |
| 670 | // mNotificationsByKey is not an exceptional condition. |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 671 | for (NotificationVisibility nv : noLongerVisibleKeys) { |
| 672 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 673 | if (r == null) continue; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 674 | r.setVisibility(false, nv.rank); |
| 675 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 676 | } |
| 677 | } |
Christoph Studer | 92b389d | 2014-04-01 18:44:40 +0200 | [diff] [blame] | 678 | } |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 679 | |
| 680 | @Override |
| 681 | public void onNotificationExpansionChanged(String key, |
| 682 | boolean userAction, boolean expanded) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 683 | synchronized (mNotificationLock) { |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 684 | NotificationRecord r = mNotificationsByKey.get(key); |
| 685 | if (r != null) { |
| 686 | r.stats.onExpansionChanged(userAction, expanded); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 687 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 688 | MetricsLogger.action(r.getLogMaker(now) |
| 689 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
| 690 | .setType(MetricsEvent.TYPE_DETAIL)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 691 | EventLogTags.writeNotificationExpansion(key, |
| 692 | userAction ? 1 : 0, expanded ? 1 : 0, |
| 693 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | }; |
| 698 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 699 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 700 | private void clearSoundLocked() { |
| 701 | mSoundNotificationKey = null; |
| 702 | long identity = Binder.clearCallingIdentity(); |
| 703 | try { |
| 704 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 705 | if (player != null) { |
| 706 | player.stopAsync(); |
| 707 | } |
| 708 | } catch (RemoteException e) { |
| 709 | } finally { |
| 710 | Binder.restoreCallingIdentity(identity); |
| 711 | } |
| 712 | } |
| 713 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 714 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 715 | private void clearVibrateLocked() { |
| 716 | mVibrateNotificationKey = null; |
| 717 | long identity = Binder.clearCallingIdentity(); |
| 718 | try { |
| 719 | mVibrator.cancel(); |
| 720 | } finally { |
| 721 | Binder.restoreCallingIdentity(identity); |
| 722 | } |
| 723 | } |
| 724 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 725 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 726 | private void clearLightsLocked() { |
| 727 | // light |
| 728 | mLights.clear(); |
| 729 | updateLightsLocked(); |
| 730 | } |
| 731 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 732 | private final BroadcastReceiver mNotificationTimeoutReceiver = new BroadcastReceiver() { |
| 733 | @Override |
| 734 | public void onReceive(Context context, Intent intent) { |
| 735 | String action = intent.getAction(); |
| 736 | if (action == null) { |
| 737 | return; |
| 738 | } |
| 739 | if (ACTION_NOTIFICATION_TIMEOUT.equals(action)) { |
| 740 | final NotificationRecord record; |
| 741 | synchronized (mNotificationLock) { |
| 742 | record = findNotificationByKeyLocked(intent.getStringExtra(EXTRA_KEY)); |
| 743 | } |
| 744 | if (record != null) { |
| 745 | cancelNotification(record.sbn.getUid(), record.sbn.getInitialPid(), |
| 746 | record.sbn.getPackageName(), record.sbn.getTag(), |
| 747 | record.sbn.getId(), 0, |
| 748 | Notification.FLAG_FOREGROUND_SERVICE, true, record.getUserId(), |
| 749 | REASON_TIMEOUT, null); |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | }; |
| 754 | |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 755 | private final BroadcastReceiver mPackageIntentReceiver = new BroadcastReceiver() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 756 | @Override |
| 757 | public void onReceive(Context context, Intent intent) { |
| 758 | String action = intent.getAction(); |
Dianne Hackborn | 29cd7f1 | 2015-01-08 10:37:05 -0800 | [diff] [blame] | 759 | if (action == null) { |
| 760 | return; |
| 761 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 763 | boolean queryRestart = false; |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 764 | boolean queryRemove = false; |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 765 | boolean packageChanged = false; |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 766 | boolean cancelNotifications = true; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 767 | int reason = REASON_PACKAGE_CHANGED; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 768 | |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 769 | if (action.equals(Intent.ACTION_PACKAGE_ADDED) |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 770 | || (queryRemove=action.equals(Intent.ACTION_PACKAGE_REMOVED)) |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 771 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 772 | || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED)) |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 773 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 774 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE) |
| 775 | || action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 776 | int changeUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 777 | UserHandle.USER_ALL); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 778 | String pkgList[] = null; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 779 | int uidList[] = null; |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 780 | boolean removingPackage = queryRemove && |
| 781 | !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 782 | if (DBG) Slog.i(TAG, "action=" + action + " removing=" + removingPackage); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 783 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 784 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 785 | uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 786 | } else if (action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
| 787 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 788 | reason = REASON_PACKAGE_SUSPENDED; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 789 | } else if (queryRestart) { |
| 790 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 791 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 792 | } else { |
| 793 | Uri uri = intent.getData(); |
| 794 | if (uri == null) { |
| 795 | return; |
| 796 | } |
| 797 | String pkgName = uri.getSchemeSpecificPart(); |
| 798 | if (pkgName == null) { |
| 799 | return; |
| 800 | } |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 801 | if (packageChanged) { |
| 802 | // We cancel notifications for packages which have just been disabled |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 803 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 804 | final int enabled = mPackageManager.getApplicationEnabledSetting( |
| 805 | pkgName, |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 806 | changeUserId != UserHandle.USER_ALL ? changeUserId : |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 807 | UserHandle.USER_SYSTEM); |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 808 | if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 809 | || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 810 | cancelNotifications = false; |
| 811 | } |
| 812 | } catch (IllegalArgumentException e) { |
| 813 | // Package doesn't exist; probably racing with uninstall. |
| 814 | // cancelNotifications is already true, so nothing to do here. |
| 815 | if (DBG) { |
| 816 | Slog.i(TAG, "Exception trying to look up app enabled setting", e); |
| 817 | } |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 818 | } catch (RemoteException e) { |
| 819 | // Failed to talk to PackageManagerService Should never happen! |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 820 | } |
| 821 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 822 | pkgList = new String[]{pkgName}; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 823 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 825 | if (pkgList != null && (pkgList.length > 0)) { |
| 826 | for (String pkgName : pkgList) { |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 827 | if (cancelNotifications) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 828 | cancelAllNotificationsInt(MY_UID, MY_PID, pkgName, null, 0, 0, |
| 829 | !queryRestart, changeUserId, reason, null); |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 830 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 831 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 | } |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 833 | mListeners.onPackagesChanged(removingPackage, pkgList); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 834 | mNotificationAssistants.onPackagesChanged(removingPackage, pkgList); |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 835 | mConditionProviders.onPackagesChanged(removingPackage, pkgList); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 836 | mRankingHelper.onPackagesChanged(removingPackage, changeUserId, pkgList, uidList); |
| 837 | savePolicyFile(); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | }; |
| 841 | |
| 842 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 843 | @Override |
| 844 | public void onReceive(Context context, Intent intent) { |
| 845 | String action = intent.getAction(); |
| 846 | |
| 847 | if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 848 | // Keep track of screen on/off state, but do not turn off the notification light |
| 849 | // until user passes through the lock screen or views the notification. |
| 850 | mScreenOn = true; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 851 | updateNotificationPulse(); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 852 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 853 | mScreenOn = false; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 854 | updateNotificationPulse(); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 855 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
John Spurlock | 5d2eeb1 | 2014-01-16 10:46:36 -0500 | [diff] [blame] | 856 | mInCall = TelephonyManager.EXTRA_STATE_OFFHOOK |
| 857 | .equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE)); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 858 | updateNotificationPulse(); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 859 | synchronized (mInCallToneGeneratorLock) { |
| 860 | if (mInCall) { |
| 861 | if (mInCallToneGenerator == null) { |
| 862 | int relativeToneVolume = getContext().getResources().getInteger( |
| 863 | R.integer.config_inCallNotificationVolumeRelative); |
| 864 | if (relativeToneVolume < ToneGenerator.MIN_VOLUME |
| 865 | || relativeToneVolume > ToneGenerator.MAX_VOLUME) { |
| 866 | relativeToneVolume = ToneGenerator.MAX_VOLUME; |
| 867 | } |
| 868 | try { |
| 869 | mInCallToneGenerator = new ToneGenerator( |
| 870 | AudioManager.STREAM_VOICE_CALL, relativeToneVolume); |
| 871 | } catch (RuntimeException e) { |
| 872 | Log.e(TAG, "Error creating local tone generator: " + e); |
| 873 | mInCallToneGenerator = null; |
| 874 | } |
| 875 | } |
| 876 | } else { |
| 877 | if (mInCallToneGenerator != null) { |
| 878 | mInCallToneGenerator.release(); |
| 879 | mInCallToneGenerator = null; |
| 880 | } |
| 881 | } |
| 882 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 883 | } else if (action.equals(Intent.ACTION_USER_STOPPED)) { |
| 884 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 885 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 886 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 887 | REASON_USER_STOPPED, null); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 888 | } |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 889 | } else if (action.equals(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) { |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 890 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 891 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 892 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 893 | REASON_PROFILE_TURNED_OFF, null); |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 894 | } |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 895 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { |
| 896 | // turn off LED when user passes through lock screen |
| 897 | mNotificationLight.turnOff(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 898 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 899 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 900 | // reload per-user settings |
| 901 | mSettingsObserver.update(null); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 902 | mUserProfiles.updateCache(context); |
Christoph Studer | b53dfd4 | 2014-09-12 14:45:59 +0200 | [diff] [blame] | 903 | // Refresh managed services |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 904 | mConditionProviders.onUserSwitched(user); |
| 905 | mListeners.onUserSwitched(user); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 906 | mNotificationAssistants.onUserSwitched(user); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 907 | mZenModeHelper.onUserSwitched(user); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 908 | } else if (action.equals(Intent.ACTION_USER_ADDED)) { |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 909 | mUserProfiles.updateCache(context); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 910 | } else if (action.equals(Intent.ACTION_USER_REMOVED)) { |
| 911 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 912 | mZenModeHelper.onUserRemoved(user); |
Julia Reynolds | 2e9bf5f | 2017-05-03 13:23:30 -0400 | [diff] [blame] | 913 | mRankingHelper.onUserRemoved(user); |
| 914 | savePolicyFile(); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 915 | } else if (action.equals(Intent.ACTION_USER_UNLOCKED)) { |
| 916 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 917 | mConditionProviders.onUserUnlocked(user); |
| 918 | mListeners.onUserUnlocked(user); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 919 | mNotificationAssistants.onUserUnlocked(user); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 920 | mZenModeHelper.onUserUnlocked(user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | }; |
| 924 | |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 925 | private final class SettingsObserver extends ContentObserver { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 926 | private final Uri NOTIFICATION_BADGING_URI |
| 927 | = Settings.Secure.getUriFor(Settings.Secure.NOTIFICATION_BADGING); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 928 | private final Uri NOTIFICATION_LIGHT_PULSE_URI |
| 929 | = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 930 | private final Uri NOTIFICATION_RATE_LIMIT_URI |
| 931 | = Settings.Global.getUriFor(Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 932 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 933 | SettingsObserver(Handler handler) { |
| 934 | super(handler); |
| 935 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 936 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 937 | void observe() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 938 | ContentResolver resolver = getContext().getContentResolver(); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 939 | resolver.registerContentObserver(NOTIFICATION_BADGING_URI, |
| 940 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 941 | resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 942 | false, this, UserHandle.USER_ALL); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 943 | resolver.registerContentObserver(NOTIFICATION_RATE_LIMIT_URI, |
| 944 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 945 | update(null); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 948 | @Override public void onChange(boolean selfChange, Uri uri) { |
| 949 | update(uri); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 952 | public void update(Uri uri) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 953 | ContentResolver resolver = getContext().getContentResolver(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 954 | if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { |
zhoulei | 7e37697 | 2017-05-17 18:41:25 +0800 | [diff] [blame] | 955 | boolean pulseEnabled = Settings.System.getIntForUser(resolver, |
| 956 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0, UserHandle.USER_CURRENT) != 0; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 957 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 958 | mNotificationPulseEnabled = pulseEnabled; |
| 959 | updateNotificationPulse(); |
| 960 | } |
| 961 | } |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 962 | if (uri == null || NOTIFICATION_RATE_LIMIT_URI.equals(uri)) { |
| 963 | mMaxPackageEnqueueRate = Settings.Global.getFloat(resolver, |
| 964 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, mMaxPackageEnqueueRate); |
| 965 | } |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 966 | if (uri == null || NOTIFICATION_BADGING_URI.equals(uri)) { |
| 967 | mRankingHelper.updateBadgingEnabled(); |
| 968 | } |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 969 | } |
| 970 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 971 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 972 | private SettingsObserver mSettingsObserver; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 973 | private ZenModeHelper mZenModeHelper; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 974 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 975 | static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) { |
| 976 | int[] ar = r.getIntArray(resid); |
| 977 | if (ar == null) { |
| 978 | return def; |
| 979 | } |
| 980 | final int len = ar.length > maxlen ? maxlen : ar.length; |
| 981 | long[] out = new long[len]; |
| 982 | for (int i=0; i<len; i++) { |
| 983 | out[i] = ar[i]; |
| 984 | } |
| 985 | return out; |
| 986 | } |
| 987 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 988 | public NotificationManagerService(Context context) { |
| 989 | super(context); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 990 | Notification.processWhitelistToken = WHITELIST_TOKEN; |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 991 | } |
| 992 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 993 | // TODO - replace these methods with a single VisibleForTesting constructor |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 994 | @VisibleForTesting |
| 995 | void setAudioManager(AudioManager audioMananger) { |
| 996 | mAudioManager = audioMananger; |
| 997 | } |
| 998 | |
| 999 | @VisibleForTesting |
| 1000 | void setVibrator(Vibrator vibrator) { |
| 1001 | mVibrator = vibrator; |
| 1002 | } |
| 1003 | |
| 1004 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1005 | void setLights(Light light) { |
| 1006 | mNotificationLight = light; |
| 1007 | mAttentionLight = light; |
Julia Reynolds | 033a412 | 2017-01-31 16:50:38 -0500 | [diff] [blame] | 1008 | mNotificationPulseEnabled = true; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | @VisibleForTesting |
| 1012 | void setScreenOn(boolean on) { |
| 1013 | mScreenOn = on; |
| 1014 | } |
| 1015 | |
| 1016 | @VisibleForTesting |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 1017 | int getNotificationRecordCount() { |
| 1018 | synchronized (mNotificationLock) { |
| 1019 | int count = mNotificationList.size() + mNotificationsByKey.size() |
| 1020 | + mSummaryByGroupKey.size() + mEnqueuedNotifications.size(); |
| 1021 | // subtract duplicates |
| 1022 | for (NotificationRecord posted : mNotificationList) { |
| 1023 | if (mNotificationsByKey.containsKey(posted.getKey())) { |
| 1024 | count--; |
| 1025 | } |
| 1026 | if (posted.sbn.isGroup() && posted.getNotification().isGroupSummary()) { |
| 1027 | count --; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | return count; |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1036 | void addNotification(NotificationRecord r) { |
| 1037 | mNotificationList.add(r); |
| 1038 | mNotificationsByKey.put(r.sbn.getKey(), r); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1039 | if (r.sbn.isGroup()) { |
| 1040 | mSummaryByGroupKey.put(r.getGroupKey(), r); |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | @VisibleForTesting |
| 1045 | void addEnqueuedNotification(NotificationRecord r) { |
| 1046 | mEnqueuedNotifications.add(r); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | @VisibleForTesting |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1050 | void setSystemReady(boolean systemReady) { |
| 1051 | mSystemReady = systemReady; |
| 1052 | } |
| 1053 | |
| 1054 | @VisibleForTesting |
| 1055 | void setHandler(Handler handler) { |
| 1056 | mHandler = handler; |
| 1057 | } |
| 1058 | |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1059 | @VisibleForTesting |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 1060 | void setFallbackVibrationPattern(long[] vibrationPattern) { |
| 1061 | mFallbackVibrationPattern = vibrationPattern; |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1062 | } |
| 1063 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1064 | @VisibleForTesting |
| 1065 | void setPackageManager(IPackageManager packageManager) { |
| 1066 | mPackageManager = packageManager; |
| 1067 | } |
| 1068 | |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1069 | @VisibleForTesting |
| 1070 | void setRankingHelper(RankingHelper rankingHelper) { |
| 1071 | mRankingHelper = rankingHelper; |
| 1072 | } |
| 1073 | |
| 1074 | @VisibleForTesting |
| 1075 | void setIsTelevision(boolean isTelevision) { |
| 1076 | mIsTelevision = isTelevision; |
| 1077 | } |
| 1078 | |
Julia Reynolds | 5f8e0b88 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 1079 | @VisibleForTesting |
| 1080 | void setUsageStats(NotificationUsageStats us) { |
| 1081 | mUsageStats = us; |
| 1082 | } |
| 1083 | |
Geoffrey Pitsch | ccbfbc7 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1084 | // 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] | 1085 | @VisibleForTesting |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1086 | void init(Looper looper, IPackageManager packageManager, PackageManager packageManagerClient, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1087 | LightsManager lightsManager, NotificationListeners notificationListeners, |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1088 | ICompanionDeviceManager companionManager, SnoozeHelper snoozeHelper, |
| 1089 | NotificationUsageStats usageStats) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1090 | Resources resources = getContext().getResources(); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1091 | mMaxPackageEnqueueRate = Settings.Global.getFloat(getContext().getContentResolver(), |
| 1092 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, |
| 1093 | DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE); |
| 1094 | |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1095 | mAm = ActivityManager.getService(); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1096 | mPackageManager = packageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1097 | mPackageManagerClient = packageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1098 | mAppOps = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE); |
| 1099 | mVibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 1100 | mAppUsageStats = LocalServices.getService(UsageStatsManagerInternal.class); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1101 | mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1102 | mCompanionManager = companionManager; |
San Mehat | 3ee1317 | 2010-02-04 20:54:43 -0800 | [diff] [blame] | 1103 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1104 | mHandler = new WorkerHandler(looper); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1105 | mRankingThread.start(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1106 | String[] extractorNames; |
| 1107 | try { |
| 1108 | extractorNames = resources.getStringArray(R.array.config_notificationSignalExtractors); |
| 1109 | } catch (Resources.NotFoundException e) { |
| 1110 | extractorNames = new String[0]; |
| 1111 | } |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1112 | mUsageStats = usageStats; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1113 | mRankingHandler = new RankingHandlerWorker(mRankingThread.getLooper()); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1114 | mRankingHelper = new RankingHelper(getContext(), |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 1115 | getContext().getPackageManager(), |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1116 | mRankingHandler, |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 1117 | mUsageStats, |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1118 | extractorNames); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1119 | mConditionProviders = new ConditionProviders(getContext(), mHandler, mUserProfiles); |
| 1120 | mZenModeHelper = new ZenModeHelper(getContext(), mHandler.getLooper(), mConditionProviders); |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 1121 | mZenModeHelper.addCallback(new ZenModeHelper.Callback() { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1122 | @Override |
| 1123 | public void onConfigChanged() { |
| 1124 | savePolicyFile(); |
| 1125 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1126 | |
| 1127 | @Override |
| 1128 | void onZenModeChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1129 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED); |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1130 | getContext().sendBroadcastAsUser( |
Jason Monk | 6350674 | 2015-12-16 12:06:51 -0500 | [diff] [blame] | 1131 | new Intent(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL) |
| 1132 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT), |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1133 | UserHandle.ALL, android.Manifest.permission.MANAGE_NOTIFICATIONS); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1134 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1135 | updateInterruptionFilterLocked(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1136 | } |
| 1137 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1138 | |
| 1139 | @Override |
| 1140 | void onPolicyChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1141 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED); |
| 1142 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1143 | }); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1144 | mSnoozeHelper = snoozeHelper; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 1145 | mGroupHelper = new GroupHelper(new GroupHelper.Callback() { |
| 1146 | @Override |
| 1147 | public void addAutoGroup(String key) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1148 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 1149 | addAutogroupKeyLocked(key); |
| 1150 | } |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 1151 | mRankingHandler.requestSort(false); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | @Override |
| 1155 | public void removeAutoGroup(String key) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1156 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 1157 | removeAutogroupKeyLocked(key); |
| 1158 | } |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 1159 | mRankingHandler.requestSort(false); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | @Override |
| 1163 | public void addAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 1164 | createAutoGroupSummary(userId, pkg, triggeringKey); |
| 1165 | } |
| 1166 | |
| 1167 | @Override |
| 1168 | public void removeAutoGroupSummary(int userId, String pkg) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1169 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 1170 | clearAutogroupSummaryLocked(userId, pkg); |
| 1171 | } |
| 1172 | } |
| 1173 | }); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 1174 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1175 | final File systemDir = new File(Environment.getDataDirectory(), "system"); |
| 1176 | mPolicyFile = new AtomicFile(new File(systemDir, "notification_policy.xml")); |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1177 | |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1178 | loadPolicyFile(); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1179 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1180 | // This is a ManagedServices object that keeps track of the listeners. |
| 1181 | mListeners = notificationListeners; |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1182 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 1183 | // This is a MangedServices object that keeps track of the assistant. |
| 1184 | mNotificationAssistants = new NotificationAssistants(); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1185 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1186 | mStatusBar = getLocalService(StatusBarManagerInternal.class); |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 1187 | if (mStatusBar != null) { |
| 1188 | mStatusBar.setNotificationDelegate(mNotificationDelegate); |
| 1189 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1191 | mNotificationLight = lightsManager.getLight(LightsManager.LIGHT_ID_NOTIFICATIONS); |
| 1192 | mAttentionLight = lightsManager.getLight(LightsManager.LIGHT_ID_ATTENTION); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1193 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1194 | mFallbackVibrationPattern = getLongArray(resources, |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 1195 | R.array.config_notificationFallbackVibePattern, |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1196 | VIBRATE_PATTERN_MAXLEN, |
| 1197 | DEFAULT_VIBRATE_PATTERN); |
| 1198 | |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 1199 | mUseAttentionLight = resources.getBoolean(R.bool.config_useAttentionLight); |
| 1200 | |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1201 | // Don't start allowing notifications until the setup wizard has run once. |
| 1202 | // After that, including subsequent boots, init with notifications turned on. |
| 1203 | // This works on the first boot because the setup wizard will toggle this |
| 1204 | // 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] | 1205 | if (0 == Settings.Global.getInt(getContext().getContentResolver(), |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1206 | Settings.Global.DEVICE_PROVISIONED, 0)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1207 | mDisableNotificationEffects = true; |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1208 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1209 | mZenModeHelper.initZenMode(); |
John Spurlock | f370177 | 2015-02-12 13:29:37 -0500 | [diff] [blame] | 1210 | mInterruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1211 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1212 | mUserProfiles.updateCache(getContext()); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 1213 | listenForCallState(); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1214 | |
Geoffrey Pitsch | ccbfbc7 | 2017-07-18 10:35:22 -0400 | [diff] [blame] | 1215 | mSettingsObserver = new SettingsObserver(mHandler); |
| 1216 | |
| 1217 | mArchive = new Archive(resources.getInteger( |
| 1218 | R.integer.config_notificationServiceArchiveSize)); |
| 1219 | |
| 1220 | mIsTelevision = mPackageManagerClient.hasSystemFeature(FEATURE_LEANBACK) |
| 1221 | || mPackageManagerClient.hasSystemFeature(FEATURE_TELEVISION); |
| 1222 | } |
| 1223 | |
| 1224 | @Override |
| 1225 | public void onStart() { |
| 1226 | SnoozeHelper snoozeHelper = new SnoozeHelper(getContext(), new SnoozeHelper.Callback() { |
| 1227 | @Override |
| 1228 | public void repost(int userId, NotificationRecord r) { |
| 1229 | try { |
| 1230 | if (DBG) { |
| 1231 | Slog.d(TAG, "Reposting " + r.getKey()); |
| 1232 | } |
| 1233 | enqueueNotificationInternal(r.sbn.getPackageName(), r.sbn.getOpPkg(), |
| 1234 | r.sbn.getUid(), r.sbn.getInitialPid(), r.sbn.getTag(), r.sbn.getId(), |
| 1235 | r.sbn.getNotification(), userId); |
| 1236 | } catch (Exception e) { |
| 1237 | Slog.e(TAG, "Cannot un-snooze notification", e); |
| 1238 | } |
| 1239 | } |
| 1240 | }, mUserProfiles); |
| 1241 | |
| 1242 | init(Looper.myLooper(), AppGlobals.getPackageManager(), getContext().getPackageManager(), |
| 1243 | getLocalService(LightsManager.class), new NotificationListeners(), |
| 1244 | null, snoozeHelper, new NotificationUsageStats(getContext())); |
| 1245 | |
Mike Lockwood | 35e16bf | 2010-11-30 19:53:36 -0500 | [diff] [blame] | 1246 | // register for various Intents |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1247 | IntentFilter filter = new IntentFilter(); |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1248 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 1249 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1250 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1251 | filter.addAction(Intent.ACTION_USER_PRESENT); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1252 | filter.addAction(Intent.ACTION_USER_STOPPED); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1253 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1254 | filter.addAction(Intent.ACTION_USER_ADDED); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1255 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1256 | filter.addAction(Intent.ACTION_USER_UNLOCKED); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1257 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1258 | getContext().registerReceiver(mIntentReceiver, filter); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1259 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1260 | IntentFilter pkgFilter = new IntentFilter(); |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1261 | pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1262 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
Daniel Sandler | aac0eb0 | 2011-08-06 22:51:56 -0400 | [diff] [blame] | 1263 | pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1264 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 1265 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 1266 | pkgFilter.addDataScheme("package"); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1267 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, pkgFilter, null, |
| 1268 | null); |
| 1269 | |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1270 | IntentFilter suspendedPkgFilter = new IntentFilter(); |
| 1271 | suspendedPkgFilter.addAction(Intent.ACTION_PACKAGES_SUSPENDED); |
| 1272 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, |
| 1273 | suspendedPkgFilter, null, null); |
| 1274 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1275 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1276 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, sdFilter, null, |
| 1277 | null); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1278 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1279 | IntentFilter timeoutFilter = new IntentFilter(ACTION_NOTIFICATION_TIMEOUT); |
| 1280 | timeoutFilter.addDataScheme(SCHEME_TIMEOUT); |
| 1281 | getContext().registerReceiver(mNotificationTimeoutReceiver, timeoutFilter); |
| 1282 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1283 | publishBinderService(Context.NOTIFICATION_SERVICE, mService); |
| 1284 | publishLocalService(NotificationManagerInternal.class, mInternalService); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1287 | private void sendRegisteredOnlyBroadcast(String action) { |
| 1288 | getContext().sendBroadcastAsUser(new Intent(action) |
| 1289 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), UserHandle.ALL, null); |
| 1290 | } |
| 1291 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1292 | @Override |
| 1293 | public void onBootPhase(int phase) { |
| 1294 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 1295 | // no beeping until we're basically done booting |
| 1296 | mSystemReady = true; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1297 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1298 | // Grab our optional AudioService |
| 1299 | mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1300 | mAudioManagerInternal = getLocalService(AudioManagerInternal.class); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1301 | mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1302 | mZenModeHelper.onSystemReady(); |
Adam Lesinski | a6db4ab | 2014-03-24 12:31:45 -0700 | [diff] [blame] | 1303 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 1304 | // This observer will force an update when observe is called, causing us to |
| 1305 | // bind to listener services. |
| 1306 | mSettingsObserver.observe(); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1307 | mListeners.onBootPhaseAppsCanStart(); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 1308 | mNotificationAssistants.onBootPhaseAppsCanStart(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1309 | mConditionProviders.onBootPhaseAppsCanStart(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1310 | } |
| 1311 | } |
| 1312 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1313 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1314 | private void updateListenerHintsLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1315 | final int hints = calculateHints(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1316 | if (hints == mListenerHints) return; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1317 | ZenLog.traceListenerHintsChanged(mListenerHints, hints, mEffectsSuppressors.size()); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1318 | mListenerHints = hints; |
| 1319 | scheduleListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 1320 | } |
| 1321 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1322 | @GuardedBy("mNotificationLock") |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1323 | private void updateEffectsSuppressorLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1324 | final long updatedSuppressedEffects = calculateSuppressedEffects(); |
| 1325 | if (updatedSuppressedEffects == mZenModeHelper.getSuppressedEffects()) return; |
| 1326 | final List<ComponentName> suppressors = getSuppressors(); |
| 1327 | ZenLog.traceEffectsSuppressorChanged(mEffectsSuppressors, suppressors, updatedSuppressedEffects); |
| 1328 | mEffectsSuppressors = suppressors; |
| 1329 | mZenModeHelper.setSuppressedEffects(updatedSuppressedEffects); |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1330 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1331 | } |
| 1332 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1333 | private void updateNotificationChannelInt(String pkg, int uid, NotificationChannel channel, |
| 1334 | boolean fromListener) { |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1335 | if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) { |
| 1336 | // cancel |
| 1337 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channel.getId(), 0, 0, true, |
| 1338 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, |
| 1339 | null); |
| 1340 | } |
Julia Reynolds | 6031533 | 2017-04-04 14:29:07 -0400 | [diff] [blame] | 1341 | mRankingHelper.updateNotificationChannel(pkg, uid, channel); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1342 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1343 | final NotificationChannel modifiedChannel = |
| 1344 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), false); |
| 1345 | |
| 1346 | if (!fromListener) { |
| 1347 | mListeners.notifyNotificationChannelChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1348 | pkg, UserHandle.getUserHandleForUid(uid), |
| 1349 | modifiedChannel, NOTIFICATION_CHANNEL_OR_GROUP_UPDATED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1350 | } |
| 1351 | |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 1352 | synchronized (mNotificationLock) { |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1353 | final int N = mNotificationList.size(); |
| 1354 | for (int i = N - 1; i >= 0; --i) { |
| 1355 | NotificationRecord r = mNotificationList.get(i); |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 1356 | if (r.sbn.getPackageName().equals(pkg) |
| 1357 | && r.sbn.getUid() == uid |
| 1358 | && channel.getId() != null |
| 1359 | && channel.getId().equals(r.getChannel().getId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1360 | r.updateNotificationChannel(modifiedChannel); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | mRankingHandler.requestSort(true); |
| 1365 | savePolicyFile(); |
| 1366 | } |
| 1367 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1368 | private ArrayList<ComponentName> getSuppressors() { |
| 1369 | ArrayList<ComponentName> names = new ArrayList<ComponentName>(); |
| 1370 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1371 | ArraySet<ManagedServiceInfo> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
| 1372 | |
| 1373 | for (ManagedServiceInfo info : serviceInfoList) { |
| 1374 | names.add(info.component); |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | return names; |
| 1379 | } |
| 1380 | |
| 1381 | private boolean removeDisabledHints(ManagedServiceInfo info) { |
| 1382 | return removeDisabledHints(info, 0); |
| 1383 | } |
| 1384 | |
| 1385 | private boolean removeDisabledHints(ManagedServiceInfo info, int hints) { |
| 1386 | boolean removed = false; |
| 1387 | |
| 1388 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1389 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 1390 | final ArraySet<ManagedServiceInfo> listeners = |
| 1391 | mListenersDisablingEffects.valueAt(i); |
| 1392 | |
| 1393 | if (hints == 0 || (hint & hints) == hint) { |
| 1394 | removed = removed || listeners.remove(info); |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | return removed; |
| 1399 | } |
| 1400 | |
| 1401 | private void addDisabledHints(ManagedServiceInfo info, int hints) { |
| 1402 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1403 | addDisabledHint(info, HINT_HOST_DISABLE_EFFECTS); |
| 1404 | } |
| 1405 | |
| 1406 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1407 | addDisabledHint(info, HINT_HOST_DISABLE_NOTIFICATION_EFFECTS); |
| 1408 | } |
| 1409 | |
| 1410 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1411 | addDisabledHint(info, HINT_HOST_DISABLE_CALL_EFFECTS); |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | private void addDisabledHint(ManagedServiceInfo info, int hint) { |
| 1416 | if (mListenersDisablingEffects.indexOfKey(hint) < 0) { |
| 1417 | mListenersDisablingEffects.put(hint, new ArraySet<ManagedServiceInfo>()); |
| 1418 | } |
| 1419 | |
| 1420 | ArraySet<ManagedServiceInfo> hintListeners = mListenersDisablingEffects.get(hint); |
| 1421 | hintListeners.add(info); |
| 1422 | } |
| 1423 | |
| 1424 | private int calculateHints() { |
| 1425 | int hints = 0; |
| 1426 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1427 | int hint = mListenersDisablingEffects.keyAt(i); |
| 1428 | ArraySet<ManagedServiceInfo> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
| 1429 | |
| 1430 | if (!serviceInfoList.isEmpty()) { |
| 1431 | hints |= hint; |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | return hints; |
| 1436 | } |
| 1437 | |
| 1438 | private long calculateSuppressedEffects() { |
| 1439 | int hints = calculateHints(); |
| 1440 | long suppressedEffects = 0; |
| 1441 | |
| 1442 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1443 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_ALL; |
| 1444 | } |
| 1445 | |
| 1446 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1447 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_NOTIFICATIONS; |
| 1448 | } |
| 1449 | |
| 1450 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1451 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_CALLS; |
| 1452 | } |
| 1453 | |
| 1454 | return suppressedEffects; |
| 1455 | } |
| 1456 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1457 | @GuardedBy("mNotificationLock") |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1458 | private void updateInterruptionFilterLocked() { |
| 1459 | int interruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
| 1460 | if (interruptionFilter == mInterruptionFilter) return; |
| 1461 | mInterruptionFilter = interruptionFilter; |
| 1462 | scheduleInterruptionFilterChanged(interruptionFilter); |
| 1463 | } |
| 1464 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1465 | @VisibleForTesting |
| 1466 | INotificationManager getBinderService() { |
| 1467 | return INotificationManager.Stub.asInterface(mService); |
| 1468 | } |
| 1469 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1470 | @VisibleForTesting |
| 1471 | NotificationManagerInternal getInternalService() { |
| 1472 | return mInternalService; |
| 1473 | } |
| 1474 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1475 | private final IBinder mService = new INotificationManager.Stub() { |
| 1476 | // Toasts |
| 1477 | // ============================================================================ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1479 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1480 | public void enqueueToast(String pkg, ITransientNotification callback, int duration) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1481 | { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1482 | if (DBG) { |
| 1483 | Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback |
| 1484 | + " duration=" + duration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1485 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1486 | |
| 1487 | if (pkg == null || callback == null) { |
| 1488 | Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback); |
| 1489 | return ; |
| 1490 | } |
| 1491 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1492 | final boolean isSystemToast = isCallerSystemOrPhone() || ("android".equals(pkg)); |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 1493 | final boolean isPackageSuspended = |
| 1494 | isPackageSuspendedForUser(pkg, Binder.getCallingUid()); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1495 | |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 1496 | if (ENABLE_BLOCKED_TOASTS && !isSystemToast && |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1497 | (!areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()) |
| 1498 | || isPackageSuspended)) { |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 1499 | Slog.e(TAG, "Suppressing toast from package " + pkg |
| 1500 | + (isPackageSuspended |
| 1501 | ? " due to package suspended by administrator." |
| 1502 | : " by user request.")); |
| 1503 | return; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | synchronized (mToastQueue) { |
| 1507 | int callingPid = Binder.getCallingPid(); |
| 1508 | long callingId = Binder.clearCallingIdentity(); |
| 1509 | try { |
| 1510 | ToastRecord record; |
| 1511 | int index = indexOfToastLocked(pkg, callback); |
| 1512 | // If it's already in the queue, we update it in place, we don't |
| 1513 | // move it to the end of the queue. |
| 1514 | if (index >= 0) { |
| 1515 | record = mToastQueue.get(index); |
| 1516 | record.update(duration); |
| 1517 | } else { |
| 1518 | // Limit the number of toasts that any given package except the android |
| 1519 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| 1520 | if (!isSystemToast) { |
| 1521 | int count = 0; |
| 1522 | final int N = mToastQueue.size(); |
| 1523 | for (int i=0; i<N; i++) { |
| 1524 | final ToastRecord r = mToastQueue.get(i); |
| 1525 | if (r.pkg.equals(pkg)) { |
| 1526 | count++; |
| 1527 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 1528 | Slog.e(TAG, "Package has already posted " + count |
| 1529 | + " toasts. Not showing more. Package=" + pkg); |
| 1530 | return; |
| 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 | } |
| 1535 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1536 | Binder token = new Binder(); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 1537 | mWindowManagerInternal.addWindowToken(token, TYPE_TOAST, DEFAULT_DISPLAY); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1538 | record = new ToastRecord(callingPid, pkg, callback, duration, token); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1539 | mToastQueue.add(record); |
| 1540 | index = mToastQueue.size() - 1; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1541 | keepProcessAliveIfNeededLocked(callingPid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1542 | } |
| 1543 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 1544 | // new or just been updated. Call back and tell it to show itself. |
| 1545 | // If the callback fails, this will remove it from the list, so don't |
| 1546 | // assume that it's valid after this. |
| 1547 | if (index == 0) { |
| 1548 | showNextToastLocked(); |
| 1549 | } |
| 1550 | } finally { |
| 1551 | Binder.restoreCallingIdentity(callingId); |
| 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | @Override |
| 1557 | public void cancelToast(String pkg, ITransientNotification callback) { |
| 1558 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| 1559 | |
| 1560 | if (pkg == null || callback == null) { |
| 1561 | Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback); |
| 1562 | return ; |
| 1563 | } |
| 1564 | |
| 1565 | synchronized (mToastQueue) { |
| 1566 | long callingId = Binder.clearCallingIdentity(); |
| 1567 | try { |
| 1568 | int index = indexOfToastLocked(pkg, callback); |
| 1569 | if (index >= 0) { |
| 1570 | cancelToastLocked(index); |
| 1571 | } else { |
| 1572 | Slog.w(TAG, "Toast already cancelled. pkg=" + pkg |
| 1573 | + " callback=" + callback); |
| 1574 | } |
| 1575 | } finally { |
| 1576 | Binder.restoreCallingIdentity(callingId); |
| 1577 | } |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 1582 | public void enqueueNotificationWithTag(String pkg, String opPkg, String tag, int id, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1583 | Notification notification, int userId) throws RemoteException { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 1584 | enqueueNotificationInternal(pkg, opPkg, Binder.getCallingUid(), |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1585 | Binder.getCallingPid(), tag, id, notification, userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | @Override |
| 1589 | public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1590 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1591 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1592 | Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 1593 | // Don't allow client applications to cancel foreground service notis or autobundled |
| 1594 | // summaries. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1595 | final int mustNotHaveFlags = isCallingUidSystem() ? 0 : |
| 1596 | (Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_AUTOGROUP_SUMMARY); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1597 | cancelNotification(Binder.getCallingUid(), Binder.getCallingPid(), pkg, tag, id, 0, |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1598 | mustNotHaveFlags, false, userId, REASON_APP_CANCEL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | @Override |
| 1602 | public void cancelAllNotifications(String pkg, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1603 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1604 | |
| 1605 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1606 | Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg); |
| 1607 | |
| 1608 | // Calling from user space, don't allow the canceling of actively |
| 1609 | // running foreground services. |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1610 | cancelAllNotificationsInt(Binder.getCallingUid(), Binder.getCallingPid(), |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1611 | pkg, null, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1612 | REASON_APP_CANCEL_ALL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | @Override |
| 1616 | public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1617 | checkCallerIsSystem(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1618 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1619 | mRankingHelper.setEnabled(pkg, uid, enabled); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1620 | // 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] | 1621 | if (!enabled) { |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1622 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, null, 0, 0, true, |
| 1623 | UserHandle.getUserId(uid), REASON_PACKAGE_BANNED, null); |
| 1624 | } |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1625 | savePolicyFile(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | /** |
| 1629 | * Use this when you just want to know if notifications are OK for this package. |
| 1630 | */ |
| 1631 | @Override |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1632 | public boolean areNotificationsEnabled(String pkg) { |
| 1633 | return areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()); |
| 1634 | } |
| 1635 | |
| 1636 | /** |
| 1637 | * Use this when you just want to know if notifications are OK for this package. |
| 1638 | */ |
| 1639 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1640 | public boolean areNotificationsEnabledForPackage(String pkg, int uid) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1641 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1642 | |
| 1643 | return mRankingHelper.getImportance(pkg, uid) != IMPORTANCE_NONE; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1644 | } |
| 1645 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1646 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1647 | public int getPackageImportance(String pkg) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1648 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1649 | return mRankingHelper.getImportance(pkg, Binder.getCallingUid()); |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | @Override |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1653 | public boolean canShowBadge(String pkg, int uid) { |
| 1654 | checkCallerIsSystem(); |
| 1655 | return mRankingHelper.canShowBadge(pkg, uid); |
| 1656 | } |
| 1657 | |
| 1658 | @Override |
| 1659 | public void setShowBadge(String pkg, int uid, boolean showBadge) { |
| 1660 | checkCallerIsSystem(); |
| 1661 | mRankingHelper.setShowBadge(pkg, uid, showBadge); |
| 1662 | savePolicyFile(); |
| 1663 | } |
| 1664 | |
| 1665 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1666 | public void createNotificationChannelGroups(String pkg, |
| 1667 | ParceledListSlice channelGroupList) throws RemoteException { |
| 1668 | checkCallerIsSystemOrSameApp(pkg); |
| 1669 | List<NotificationChannelGroup> groups = channelGroupList.getList(); |
| 1670 | final int groupSize = groups.size(); |
| 1671 | for (int i = 0; i < groupSize; i++) { |
| 1672 | final NotificationChannelGroup group = groups.get(i); |
| 1673 | Preconditions.checkNotNull(group, "group in list is null"); |
| 1674 | mRankingHelper.createNotificationChannelGroup(pkg, Binder.getCallingUid(), group, |
| 1675 | true /* fromTargetApp */); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1676 | mListeners.notifyNotificationChannelGroupChanged(pkg, |
| 1677 | UserHandle.of(UserHandle.getCallingUserId()), group, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1678 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1679 | } |
| 1680 | savePolicyFile(); |
| 1681 | } |
| 1682 | |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 1683 | private void createNotificationChannelsImpl(String pkg, int uid, |
| 1684 | ParceledListSlice channelsList) { |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1685 | List<NotificationChannel> channels = channelsList.getList(); |
| 1686 | final int channelsSize = channels.size(); |
| 1687 | for (int i = 0; i < channelsSize; i++) { |
| 1688 | final NotificationChannel channel = channels.get(i); |
| 1689 | Preconditions.checkNotNull(channel, "channel in list is null"); |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 1690 | mRankingHelper.createNotificationChannel(pkg, uid, channel, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1691 | true /* fromTargetApp */); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1692 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1693 | UserHandle.getUserHandleForUid(uid), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1694 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), false), |
| 1695 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1696 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1697 | savePolicyFile(); |
| 1698 | } |
| 1699 | |
| 1700 | @Override |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 1701 | public void createNotificationChannels(String pkg, |
| 1702 | ParceledListSlice channelsList) throws RemoteException { |
| 1703 | checkCallerIsSystemOrSameApp(pkg); |
| 1704 | createNotificationChannelsImpl(pkg, Binder.getCallingUid(), channelsList); |
| 1705 | } |
| 1706 | |
| 1707 | @Override |
| 1708 | public void createNotificationChannelsForPackage(String pkg, int uid, |
| 1709 | ParceledListSlice channelsList) throws RemoteException { |
| 1710 | checkCallerIsSystem(); |
| 1711 | createNotificationChannelsImpl(pkg, uid, channelsList); |
| 1712 | } |
| 1713 | |
| 1714 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1715 | public NotificationChannel getNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1716 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1717 | return mRankingHelper.getNotificationChannel( |
| 1718 | pkg, Binder.getCallingUid(), channelId, false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | @Override |
| 1722 | public NotificationChannel getNotificationChannelForPackage(String pkg, int uid, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1723 | String channelId, boolean includeDeleted) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1724 | checkCallerIsSystem(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1725 | return mRankingHelper.getNotificationChannel(pkg, uid, channelId, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | @Override |
| 1729 | public void deleteNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1730 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1731 | final int callingUid = Binder.getCallingUid(); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1732 | if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 1733 | throw new IllegalArgumentException("Cannot delete default channel"); |
| 1734 | } |
| 1735 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channelId, 0, 0, true, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1736 | UserHandle.getUserId(callingUid), REASON_CHANNEL_BANNED, null); |
| 1737 | mRankingHelper.deleteNotificationChannel(pkg, callingUid, channelId); |
| 1738 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1739 | UserHandle.getUserHandleForUid(callingUid), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1740 | mRankingHelper.getNotificationChannel(pkg, callingUid, channelId, true), |
| 1741 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1742 | savePolicyFile(); |
| 1743 | } |
| 1744 | |
| 1745 | @Override |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1746 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroups( |
| 1747 | String pkg) { |
| 1748 | checkCallerIsSystemOrSameApp(pkg); |
| 1749 | return new ParceledListSlice<>(new ArrayList( |
| 1750 | mRankingHelper.getNotificationChannelGroups(pkg, Binder.getCallingUid()))); |
| 1751 | } |
| 1752 | |
| 1753 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1754 | public void deleteNotificationChannelGroup(String pkg, String groupId) { |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1755 | checkCallerIsSystemOrSameApp(pkg); |
| 1756 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1757 | final int callingUid = Binder.getCallingUid(); |
| 1758 | NotificationChannelGroup groupToDelete = |
| 1759 | mRankingHelper.getNotificationChannelGroup(groupId, pkg, callingUid); |
| 1760 | if (groupToDelete != null) { |
| 1761 | List<NotificationChannel> deletedChannels = |
| 1762 | mRankingHelper.deleteNotificationChannelGroup(pkg, callingUid, groupId); |
| 1763 | for (int i = 0; i < deletedChannels.size(); i++) { |
| 1764 | final NotificationChannel deletedChannel = deletedChannels.get(i); |
| 1765 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, deletedChannel.getId(), 0, 0, |
| 1766 | true, |
| 1767 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, |
| 1768 | null); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1769 | mListeners.notifyNotificationChannelChanged(pkg, |
| 1770 | UserHandle.getUserHandleForUid(callingUid), |
| 1771 | deletedChannel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1772 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
| 1773 | } |
| 1774 | mListeners.notifyNotificationChannelGroupChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1775 | pkg, UserHandle.getUserHandleForUid(callingUid), groupToDelete, |
| 1776 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1777 | savePolicyFile(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1778 | } |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1782 | public void updateNotificationChannelForPackage(String pkg, int uid, |
| 1783 | NotificationChannel channel) { |
Geoffrey Pitsch | 4dd5006 | 2016-12-06 16:41:22 -0500 | [diff] [blame] | 1784 | enforceSystemOrSystemUI("Caller not system or systemui"); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1785 | Preconditions.checkNotNull(channel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1786 | updateNotificationChannelInt(pkg, uid, channel, false); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | @Override |
| 1790 | public ParceledListSlice<NotificationChannel> getNotificationChannelsForPackage(String pkg, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1791 | int uid, boolean includeDeleted) { |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 1792 | enforceSystemOrSystemUI("getNotificationChannelsForPackage"); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1793 | return mRankingHelper.getNotificationChannels(pkg, uid, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 1797 | public int getNumNotificationChannelsForPackage(String pkg, int uid, |
| 1798 | boolean includeDeleted) { |
| 1799 | enforceSystemOrSystemUI("getNumNotificationChannelsForPackage"); |
| 1800 | return mRankingHelper.getNotificationChannels(pkg, uid, includeDeleted) |
| 1801 | .getList().size(); |
| 1802 | } |
| 1803 | |
| 1804 | @Override |
Julia Reynolds | 17717f5 | 2017-05-09 11:46:06 -0400 | [diff] [blame] | 1805 | public boolean onlyHasDefaultChannel(String pkg, int uid) { |
| 1806 | enforceSystemOrSystemUI("onlyHasDefaultChannel"); |
| 1807 | return mRankingHelper.onlyHasDefaultChannel(pkg, uid); |
| 1808 | } |
| 1809 | |
| 1810 | @Override |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 1811 | public int getDeletedChannelCount(String pkg, int uid) { |
| 1812 | enforceSystemOrSystemUI("getDeletedChannelCount"); |
| 1813 | return mRankingHelper.getDeletedChannelCount(pkg, uid); |
| 1814 | } |
| 1815 | |
| 1816 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1817 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroupsForPackage( |
| 1818 | String pkg, int uid, boolean includeDeleted) { |
| 1819 | checkCallerIsSystem(); |
| 1820 | return mRankingHelper.getNotificationChannelGroups(pkg, uid, includeDeleted); |
| 1821 | } |
| 1822 | |
| 1823 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 1824 | public NotificationChannelGroup getNotificationChannelGroupForPackage( |
| 1825 | String groupId, String pkg, int uid) { |
| 1826 | enforceSystemOrSystemUI("getNotificationChannelGroupForPackage"); |
| 1827 | return mRankingHelper.getNotificationChannelGroup(groupId, pkg, uid); |
| 1828 | } |
| 1829 | |
| 1830 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1831 | public ParceledListSlice<NotificationChannel> getNotificationChannels(String pkg) { |
| 1832 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1833 | return mRankingHelper.getNotificationChannels( |
| 1834 | pkg, Binder.getCallingUid(), false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1835 | } |
| 1836 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1837 | @Override |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 1838 | public void clearData(String packageName, int uid, boolean fromApp) throws RemoteException { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1839 | checkCallerIsSystem(); |
| 1840 | |
| 1841 | // Cancel posted notifications |
| 1842 | cancelAllNotificationsInt(MY_UID, MY_PID, packageName, null, 0, 0, true, |
| 1843 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, null); |
| 1844 | |
| 1845 | // Listener & assistant |
| 1846 | mListeners.onPackagesChanged(true, new String[] {packageName}); |
| 1847 | mNotificationAssistants.onPackagesChanged(true, new String[] {packageName}); |
| 1848 | |
| 1849 | // Zen |
| 1850 | mConditionProviders.onPackagesChanged(true, new String[] {packageName}); |
| 1851 | |
| 1852 | // Reset notification preferences |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 1853 | if (!fromApp) { |
| 1854 | mRankingHelper.onPackagesChanged(true, UserHandle.getCallingUserId(), |
| 1855 | new String[]{packageName}, new int[]{uid}); |
| 1856 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1857 | |
| 1858 | savePolicyFile(); |
| 1859 | } |
| 1860 | |
| 1861 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1862 | /** |
| 1863 | * System-only API for getting a list of current (i.e. not cleared) notifications. |
| 1864 | * |
| 1865 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1866 | * @returns A list of all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1867 | */ |
| 1868 | @Override |
| 1869 | public StatusBarNotification[] getActiveNotifications(String callingPkg) { |
| 1870 | // enforce() will ensure the calling uid has the correct permission |
| 1871 | getContext().enforceCallingOrSelfPermission( |
| 1872 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 1873 | "NotificationManagerService.getActiveNotifications"); |
| 1874 | |
| 1875 | StatusBarNotification[] tmp = null; |
| 1876 | int uid = Binder.getCallingUid(); |
| 1877 | |
| 1878 | // noteOp will check to make sure the callingPkg matches the uid |
| 1879 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 1880 | == AppOpsManager.MODE_ALLOWED) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1881 | synchronized (mNotificationLock) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1882 | tmp = new StatusBarNotification[mNotificationList.size()]; |
| 1883 | final int N = mNotificationList.size(); |
| 1884 | for (int i=0; i<N; i++) { |
| 1885 | tmp[i] = mNotificationList.get(i).sbn; |
| 1886 | } |
| 1887 | } |
| 1888 | } |
| 1889 | return tmp; |
| 1890 | } |
| 1891 | |
| 1892 | /** |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1893 | * Public API for getting a list of current notifications for the calling package/uid. |
| 1894 | * |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 1895 | * Note that since notification posting is done asynchronously, this will not return |
| 1896 | * notifications that are in the process of being posted. |
| 1897 | * |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1898 | * @returns A list of all the package's notifications, in natural order. |
| 1899 | */ |
| 1900 | @Override |
| 1901 | public ParceledListSlice<StatusBarNotification> getAppActiveNotifications(String pkg, |
| 1902 | int incomingUserId) { |
| 1903 | checkCallerIsSystemOrSameApp(pkg); |
| 1904 | int userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1905 | Binder.getCallingUid(), incomingUserId, true, false, |
| 1906 | "getAppActiveNotifications", pkg); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1907 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 1908 | final ArrayMap<String, StatusBarNotification> map |
| 1909 | = new ArrayMap<>(mNotificationList.size() + mEnqueuedNotifications.size()); |
Erik Wolsheimer | 2242b4d | 2015-11-24 13:22:04 -0800 | [diff] [blame] | 1910 | final int N = mNotificationList.size(); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1911 | for (int i = 0; i < N; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 1912 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 1913 | mNotificationList.get(i).sbn); |
| 1914 | if (sbn != null) { |
| 1915 | map.put(sbn.getKey(), sbn); |
| 1916 | } |
| 1917 | } |
| 1918 | for(NotificationRecord snoozed: mSnoozeHelper.getSnoozed(userId, pkg)) { |
| 1919 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, snoozed.sbn); |
| 1920 | if (sbn != null) { |
| 1921 | map.put(sbn.getKey(), sbn); |
| 1922 | } |
| 1923 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1924 | final int M = mEnqueuedNotifications.size(); |
| 1925 | for (int i = 0; i < M; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 1926 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 1927 | mEnqueuedNotifications.get(i).sbn); |
| 1928 | if (sbn != null) { |
| 1929 | map.put(sbn.getKey(), sbn); // pending update overwrites existing post here |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1930 | } |
| 1931 | } |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 1932 | final ArrayList<StatusBarNotification> list = new ArrayList<>(map.size()); |
| 1933 | list.addAll(map.values()); |
| 1934 | return new ParceledListSlice<StatusBarNotification>(list); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1935 | } |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1936 | } |
| 1937 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 1938 | private StatusBarNotification sanitizeSbn(String pkg, int userId, |
| 1939 | StatusBarNotification sbn) { |
| 1940 | if (sbn.getPackageName().equals(pkg) && sbn.getUserId() == userId |
| 1941 | && (sbn.getNotification().flags |
| 1942 | & Notification.FLAG_AUTOGROUP_SUMMARY) == 0) { |
| 1943 | // We could pass back a cloneLight() but clients might get confused and |
| 1944 | // try to send this thing back to notify() again, which would not work |
| 1945 | // very well. |
| 1946 | return new StatusBarNotification( |
| 1947 | sbn.getPackageName(), |
| 1948 | sbn.getOpPkg(), |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 1949 | sbn.getId(), sbn.getTag(), sbn.getUid(), sbn.getInitialPid(), |
| 1950 | sbn.getNotification().clone(), |
| 1951 | sbn.getUser(), sbn.getOverrideGroupKey(), sbn.getPostTime()); |
| 1952 | } |
| 1953 | return null; |
| 1954 | } |
| 1955 | |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1956 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1957 | * System-only API for getting a list of recent (cleared, no longer shown) notifications. |
| 1958 | * |
| 1959 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
| 1960 | */ |
| 1961 | @Override |
| 1962 | public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) { |
| 1963 | // enforce() will ensure the calling uid has the correct permission |
| 1964 | getContext().enforceCallingOrSelfPermission( |
| 1965 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 1966 | "NotificationManagerService.getHistoricalNotifications"); |
| 1967 | |
| 1968 | StatusBarNotification[] tmp = null; |
| 1969 | int uid = Binder.getCallingUid(); |
| 1970 | |
| 1971 | // noteOp will check to make sure the callingPkg matches the uid |
| 1972 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 1973 | == AppOpsManager.MODE_ALLOWED) { |
| 1974 | synchronized (mArchive) { |
| 1975 | tmp = mArchive.getArray(count); |
| 1976 | } |
| 1977 | } |
| 1978 | return tmp; |
| 1979 | } |
| 1980 | |
| 1981 | /** |
| 1982 | * Register a listener binder directly with the notification manager. |
| 1983 | * |
| 1984 | * Only works with system callers. Apps should extend |
| 1985 | * {@link android.service.notification.NotificationListenerService}. |
| 1986 | */ |
| 1987 | @Override |
| 1988 | public void registerListener(final INotificationListener listener, |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1989 | final ComponentName component, final int userid) { |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 1990 | enforceSystemOrSystemUI("INotificationManager.registerListener"); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1991 | mListeners.registerService(listener, component, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | /** |
| 1995 | * Remove a listener binder directly |
| 1996 | */ |
| 1997 | @Override |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 1998 | public void unregisterListener(INotificationListener token, int userid) { |
Chris Wren | b7c8109 | 2016-03-10 11:41:10 -0500 | [diff] [blame] | 1999 | mListeners.unregisterService(token, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | /** |
| 2003 | * Allow an INotificationListener to simulate a "clear all" operation. |
| 2004 | * |
| 2005 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications} |
| 2006 | * |
| 2007 | * @param token The binder for the listener, to check that the caller is allowed |
| 2008 | */ |
| 2009 | @Override |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2010 | public void cancelNotificationsFromListener(INotificationListener token, String[] keys) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2011 | final int callingUid = Binder.getCallingUid(); |
| 2012 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2013 | long identity = Binder.clearCallingIdentity(); |
| 2014 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2015 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2016 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2017 | if (keys != null) { |
| 2018 | final int N = keys.length; |
| 2019 | for (int i = 0; i < N; i++) { |
| 2020 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 2021 | if (r == null) continue; |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2022 | final int userId = r.sbn.getUserId(); |
| 2023 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 2024 | !mUserProfiles.isCurrentProfile(userId)) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2025 | throw new SecurityException("Disallowed call from listener: " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2026 | + info.service); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2027 | } |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 2028 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2029 | r.sbn.getPackageName(), r.sbn.getTag(), r.sbn.getId(), |
| 2030 | userId); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2031 | } |
| 2032 | } else { |
| 2033 | cancelAllLocked(callingUid, callingPid, info.userid, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2034 | REASON_LISTENER_CANCEL_ALL, info, info.supportsProfiles()); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2035 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2036 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2037 | } finally { |
| 2038 | Binder.restoreCallingIdentity(identity); |
| 2039 | } |
| 2040 | } |
| 2041 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2042 | /** |
| 2043 | * Handle request from an approved listener to re-enable itself. |
| 2044 | * |
| 2045 | * @param component The componenet to be re-enabled, caller must match package. |
| 2046 | */ |
| 2047 | @Override |
| 2048 | public void requestBindListener(ComponentName component) { |
| 2049 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 2050 | long identity = Binder.clearCallingIdentity(); |
| 2051 | try { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2052 | ManagedServices manager = |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 2053 | mNotificationAssistants.isComponentEnabledForCurrentProfiles(component) |
| 2054 | ? mNotificationAssistants |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2055 | : mListeners; |
| 2056 | manager.setComponentState(component, true); |
| 2057 | } finally { |
| 2058 | Binder.restoreCallingIdentity(identity); |
| 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | @Override |
| 2063 | public void requestUnbindListener(INotificationListener token) { |
| 2064 | long identity = Binder.clearCallingIdentity(); |
| 2065 | try { |
| 2066 | // allow bound services to disable themselves |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2067 | synchronized (mNotificationLock) { |
| 2068 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2069 | info.getOwner().setComponentState(info.component, false); |
| 2070 | } |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2071 | } finally { |
| 2072 | Binder.restoreCallingIdentity(identity); |
| 2073 | } |
| 2074 | } |
| 2075 | |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2076 | @Override |
| 2077 | public void setNotificationsShownFromListener(INotificationListener token, String[] keys) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2078 | long identity = Binder.clearCallingIdentity(); |
| 2079 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2080 | synchronized (mNotificationLock) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2081 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2082 | if (keys != null) { |
| 2083 | final int N = keys.length; |
| 2084 | for (int i = 0; i < N; i++) { |
| 2085 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
| 2086 | if (r == null) continue; |
| 2087 | final int userId = r.sbn.getUserId(); |
| 2088 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
| 2089 | !mUserProfiles.isCurrentProfile(userId)) { |
| 2090 | throw new SecurityException("Disallowed call from listener: " |
| 2091 | + info.service); |
| 2092 | } |
| 2093 | if (!r.isSeen()) { |
| 2094 | if (DBG) Slog.d(TAG, "Marking notification as seen " + keys[i]); |
| 2095 | mAppUsageStats.reportEvent(r.sbn.getPackageName(), |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 2096 | userId == UserHandle.USER_ALL ? UserHandle.USER_SYSTEM |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2097 | : userId, |
Adam Lesinski | c8e8729 | 2015-06-10 15:33:45 -0700 | [diff] [blame] | 2098 | UsageEvents.Event.USER_INTERACTION); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2099 | r.setSeen(); |
| 2100 | } |
| 2101 | } |
| 2102 | } |
| 2103 | } |
| 2104 | } finally { |
| 2105 | Binder.restoreCallingIdentity(identity); |
| 2106 | } |
| 2107 | } |
| 2108 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2109 | /** |
| 2110 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2111 | * |
| 2112 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2113 | * |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2114 | * @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] | 2115 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2116 | @GuardedBy("mNotificationLock") |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2117 | private void cancelNotificationFromListenerLocked(ManagedServiceInfo info, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2118 | int callingUid, int callingPid, String pkg, String tag, int id, int userId) { |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2119 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
| 2120 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
| 2121 | true, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2122 | userId, REASON_LISTENER_CANCEL, info); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2123 | } |
| 2124 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2125 | /** |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2126 | * Allow an INotificationListener to snooze a single notification until a context. |
| 2127 | * |
| 2128 | * @param token The binder for the listener, to check that the caller is allowed |
| 2129 | */ |
| 2130 | @Override |
| 2131 | public void snoozeNotificationUntilContextFromListener(INotificationListener token, |
| 2132 | String key, String snoozeCriterionId) { |
| 2133 | long identity = Binder.clearCallingIdentity(); |
| 2134 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2135 | synchronized (mNotificationLock) { |
| 2136 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2137 | snoozeNotificationInt(key, SNOOZE_UNTIL_UNSPECIFIED, snoozeCriterionId, info); |
| 2138 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2139 | } finally { |
| 2140 | Binder.restoreCallingIdentity(identity); |
| 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | /** |
| 2145 | * Allow an INotificationListener to snooze a single notification until a time. |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2146 | * |
| 2147 | * @param token The binder for the listener, to check that the caller is allowed |
| 2148 | */ |
| 2149 | @Override |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2150 | public void snoozeNotificationUntilFromListener(INotificationListener token, String key, |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 2151 | long duration) { |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2152 | long identity = Binder.clearCallingIdentity(); |
| 2153 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2154 | synchronized (mNotificationLock) { |
| 2155 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2156 | snoozeNotificationInt(key, duration, null, info); |
| 2157 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2158 | } finally { |
| 2159 | Binder.restoreCallingIdentity(identity); |
| 2160 | } |
| 2161 | } |
| 2162 | |
| 2163 | /** |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2164 | * Allows the notification assistant to un-snooze a single notification. |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2165 | * |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2166 | * @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] | 2167 | */ |
| 2168 | @Override |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2169 | public void unsnoozeNotificationFromAssistant(INotificationListener token, String key) { |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2170 | long identity = Binder.clearCallingIdentity(); |
| 2171 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2172 | synchronized (mNotificationLock) { |
| 2173 | final ManagedServiceInfo info = |
| 2174 | mNotificationAssistants.checkServiceTokenLocked(token); |
| 2175 | unsnoozeNotificationInt(key, info); |
| 2176 | } |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2177 | } finally { |
| 2178 | Binder.restoreCallingIdentity(identity); |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2183 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2184 | * |
| 2185 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2186 | * |
| 2187 | * @param token The binder for the listener, to check that the caller is allowed |
| 2188 | */ |
| 2189 | @Override |
| 2190 | public void cancelNotificationFromListener(INotificationListener token, String pkg, |
| 2191 | String tag, int id) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2192 | final int callingUid = Binder.getCallingUid(); |
| 2193 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2194 | long identity = Binder.clearCallingIdentity(); |
| 2195 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2196 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2197 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2198 | if (info.supportsProfiles()) { |
| 2199 | Log.e(TAG, "Ignoring deprecated cancelNotification(pkg, tag, id) " |
| 2200 | + "from " + info.component |
| 2201 | + " use cancelNotification(key) instead."); |
| 2202 | } else { |
| 2203 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2204 | pkg, tag, id, info.userid); |
| 2205 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2206 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2207 | } finally { |
| 2208 | Binder.restoreCallingIdentity(identity); |
| 2209 | } |
| 2210 | } |
| 2211 | |
| 2212 | /** |
| 2213 | * Allow an INotificationListener to request the list of outstanding notifications seen by |
| 2214 | * the current user. Useful when starting up, after which point the listener callbacks |
| 2215 | * should be used. |
| 2216 | * |
| 2217 | * @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] | 2218 | * @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] | 2219 | * @returns The return value will contain the notifications specified in keys, in that |
| 2220 | * order, or if keys is null, all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2221 | */ |
| 2222 | @Override |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2223 | public ParceledListSlice<StatusBarNotification> getActiveNotificationsFromListener( |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2224 | INotificationListener token, String[] keys, int trim) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2225 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2226 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2227 | final boolean getKeys = keys != null; |
| 2228 | final int N = getKeys ? keys.length : mNotificationList.size(); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2229 | final ArrayList<StatusBarNotification> list |
| 2230 | = new ArrayList<StatusBarNotification>(N); |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2231 | for (int i=0; i<N; i++) { |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2232 | final NotificationRecord r = getKeys |
| 2233 | ? mNotificationsByKey.get(keys[i]) |
| 2234 | : mNotificationList.get(i); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2235 | if (r == null) continue; |
| 2236 | StatusBarNotification sbn = r.sbn; |
| 2237 | if (!isVisibleToListener(sbn, info)) continue; |
| 2238 | StatusBarNotification sbnToSend = |
| 2239 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2240 | list.add(sbnToSend); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2241 | } |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2242 | return new ParceledListSlice<StatusBarNotification>(list); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2243 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2244 | } |
| 2245 | |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2246 | /** |
| 2247 | * Allow an INotificationListener to request the list of outstanding snoozed notifications |
| 2248 | * seen by the current user. Useful when starting up, after which point the listener |
| 2249 | * callbacks should be used. |
| 2250 | * |
| 2251 | * @param token The binder for the listener, to check that the caller is allowed |
| 2252 | * @returns The return value will contain the notifications specified in keys, in that |
| 2253 | * order, or if keys is null, all the notifications, in natural order. |
| 2254 | */ |
| 2255 | @Override |
| 2256 | public ParceledListSlice<StatusBarNotification> getSnoozedNotificationsFromListener( |
| 2257 | INotificationListener token, int trim) { |
| 2258 | synchronized (mNotificationLock) { |
| 2259 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2260 | List<NotificationRecord> snoozedRecords = mSnoozeHelper.getSnoozed(); |
| 2261 | final int N = snoozedRecords.size(); |
| 2262 | final ArrayList<StatusBarNotification> list = new ArrayList<>(N); |
| 2263 | for (int i=0; i < N; i++) { |
| 2264 | final NotificationRecord r = snoozedRecords.get(i); |
| 2265 | if (r == null) continue; |
| 2266 | StatusBarNotification sbn = r.sbn; |
| 2267 | if (!isVisibleToListener(sbn, info)) continue; |
| 2268 | StatusBarNotification sbnToSend = |
| 2269 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2270 | list.add(sbnToSend); |
| 2271 | } |
| 2272 | return new ParceledListSlice<>(list); |
| 2273 | } |
| 2274 | } |
| 2275 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2276 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2277 | public void requestHintsFromListener(INotificationListener token, int hints) { |
| 2278 | final long identity = Binder.clearCallingIdentity(); |
| 2279 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2280 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2281 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2282 | final int disableEffectsMask = HINT_HOST_DISABLE_EFFECTS |
| 2283 | | HINT_HOST_DISABLE_NOTIFICATION_EFFECTS |
| 2284 | | HINT_HOST_DISABLE_CALL_EFFECTS; |
| 2285 | final boolean disableEffects = (hints & disableEffectsMask) != 0; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2286 | if (disableEffects) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2287 | addDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2288 | } else { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2289 | removeDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2290 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2291 | updateListenerHintsLocked(); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2292 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2293 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2294 | } finally { |
| 2295 | Binder.restoreCallingIdentity(identity); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2300 | public int getHintsFromListener(INotificationListener token) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2301 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2302 | return mListenerHints; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | @Override |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2307 | public void requestInterruptionFilterFromListener(INotificationListener token, |
| 2308 | int interruptionFilter) throws RemoteException { |
| 2309 | final long identity = Binder.clearCallingIdentity(); |
| 2310 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2311 | synchronized (mNotificationLock) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2312 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2313 | mZenModeHelper.requestFromListener(info.component, interruptionFilter); |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2314 | updateInterruptionFilterLocked(); |
| 2315 | } |
| 2316 | } finally { |
| 2317 | Binder.restoreCallingIdentity(identity); |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | @Override |
| 2322 | public int getInterruptionFilterFromListener(INotificationListener token) |
| 2323 | throws RemoteException { |
| 2324 | synchronized (mNotificationLight) { |
| 2325 | return mInterruptionFilter; |
| 2326 | } |
| 2327 | } |
| 2328 | |
| 2329 | @Override |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2330 | public void setOnNotificationPostedTrimFromListener(INotificationListener token, int trim) |
| 2331 | throws RemoteException { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2332 | synchronized (mNotificationLock) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2333 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2334 | if (info == null) return; |
| 2335 | mListeners.setOnNotificationPostedTrimLocked(info, trim); |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2340 | public int getZenMode() { |
| 2341 | return mZenModeHelper.getZenMode(); |
| 2342 | } |
| 2343 | |
| 2344 | @Override |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 2345 | public ZenModeConfig getZenModeConfig() { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2346 | enforceSystemOrSystemUI("INotificationManager.getZenModeConfig"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 2347 | return mZenModeHelper.getConfig(); |
| 2348 | } |
| 2349 | |
| 2350 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2351 | public void setZenMode(int mode, Uri conditionId, String reason) throws RemoteException { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2352 | enforceSystemOrSystemUI("INotificationManager.setZenMode"); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2353 | final long identity = Binder.clearCallingIdentity(); |
| 2354 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 2355 | mZenModeHelper.setManualZenMode(mode, conditionId, null, reason); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2356 | } finally { |
| 2357 | Binder.restoreCallingIdentity(identity); |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2362 | public List<ZenModeConfig.ZenRule> getZenRules() throws RemoteException { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2363 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRules"); |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2364 | return mZenModeHelper.getZenRules(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2365 | } |
| 2366 | |
| 2367 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2368 | public AutomaticZenRule getAutomaticZenRule(String id) throws RemoteException { |
| 2369 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2370 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRule"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2371 | return mZenModeHelper.getAutomaticZenRule(id); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2375 | public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2376 | throws RemoteException { |
| 2377 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 2378 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 2379 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 2380 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2381 | enforcePolicyAccess(Binder.getCallingUid(), "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2382 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2383 | return mZenModeHelper.addAutomaticZenRule(automaticZenRule, |
| 2384 | "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2388 | public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2389 | throws RemoteException { |
| 2390 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 2391 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 2392 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 2393 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
| 2394 | enforcePolicyAccess(Binder.getCallingUid(), "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2395 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2396 | return mZenModeHelper.updateAutomaticZenRule(id, automaticZenRule, |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2397 | "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2398 | } |
| 2399 | |
| 2400 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2401 | public boolean removeAutomaticZenRule(String id) throws RemoteException { |
| 2402 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2403 | // Verify that they can modify zen rules. |
| 2404 | enforcePolicyAccess(Binder.getCallingUid(), "removeAutomaticZenRule"); |
| 2405 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2406 | return mZenModeHelper.removeAutomaticZenRule(id, "removeAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | @Override |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 2410 | public boolean removeAutomaticZenRules(String packageName) throws RemoteException { |
| 2411 | Preconditions.checkNotNull(packageName, "Package name is null"); |
| 2412 | enforceSystemOrSystemUI("removeAutomaticZenRules"); |
| 2413 | |
| 2414 | return mZenModeHelper.removeAutomaticZenRules(packageName, "removeAutomaticZenRules"); |
| 2415 | } |
| 2416 | |
| 2417 | @Override |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 2418 | public int getRuleInstanceCount(ComponentName owner) throws RemoteException { |
| 2419 | Preconditions.checkNotNull(owner, "Owner is null"); |
| 2420 | enforceSystemOrSystemUI("getRuleInstanceCount"); |
| 2421 | |
| 2422 | return mZenModeHelper.getCurrentInstanceCount(owner); |
| 2423 | } |
| 2424 | |
| 2425 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2426 | public void setInterruptionFilter(String pkg, int filter) throws RemoteException { |
| 2427 | enforcePolicyAccess(pkg, "setInterruptionFilter"); |
| 2428 | final int zen = NotificationManager.zenModeFromInterruptionFilter(filter, -1); |
| 2429 | if (zen == -1) throw new IllegalArgumentException("Invalid filter: " + filter); |
| 2430 | final long identity = Binder.clearCallingIdentity(); |
| 2431 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 2432 | mZenModeHelper.setManualZenMode(zen, null, pkg, "setInterruptionFilter"); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2433 | } finally { |
| 2434 | Binder.restoreCallingIdentity(identity); |
| 2435 | } |
| 2436 | } |
| 2437 | |
| 2438 | @Override |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 2439 | public void notifyConditions(final String pkg, IConditionProvider provider, |
| 2440 | final Condition[] conditions) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2441 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 2442 | checkCallerIsSystemOrSameApp(pkg); |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 2443 | mHandler.post(new Runnable() { |
| 2444 | @Override |
| 2445 | public void run() { |
| 2446 | mConditionProviders.notifyConditions(pkg, info, conditions); |
| 2447 | } |
| 2448 | }); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2449 | } |
| 2450 | |
Julia Reynolds | 38e6ca4 | 2016-08-08 08:38:09 -0400 | [diff] [blame] | 2451 | @Override |
| 2452 | public void requestUnbindProvider(IConditionProvider provider) { |
| 2453 | long identity = Binder.clearCallingIdentity(); |
| 2454 | try { |
| 2455 | // allow bound services to disable themselves |
| 2456 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 2457 | info.getOwner().setComponentState(info.component, false); |
| 2458 | } finally { |
| 2459 | Binder.restoreCallingIdentity(identity); |
| 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | @Override |
| 2464 | public void requestBindProvider(ComponentName component) { |
| 2465 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 2466 | long identity = Binder.clearCallingIdentity(); |
| 2467 | try { |
| 2468 | mConditionProviders.setComponentState(component, true); |
| 2469 | } finally { |
| 2470 | Binder.restoreCallingIdentity(identity); |
| 2471 | } |
| 2472 | } |
| 2473 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2474 | private void enforceSystemOrSystemUI(String message) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 2475 | if (isCallerSystemOrPhone()) return; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2476 | getContext().enforceCallingPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 2477 | message); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2478 | } |
| 2479 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 2480 | private void enforceSystemOrSystemUIOrSamePackage(String pkg, String message) { |
| 2481 | try { |
| 2482 | checkCallerIsSystemOrSameApp(pkg); |
| 2483 | } catch (SecurityException e) { |
| 2484 | getContext().enforceCallingPermission( |
| 2485 | android.Manifest.permission.STATUS_BAR_SERVICE, |
| 2486 | message); |
| 2487 | } |
| 2488 | } |
| 2489 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2490 | private void enforcePolicyAccess(int uid, String method) { |
| 2491 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 2492 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 2493 | return; |
| 2494 | } |
| 2495 | boolean accessAllowed = false; |
| 2496 | String[] packages = getContext().getPackageManager().getPackagesForUid(uid); |
| 2497 | final int packageCount = packages.length; |
| 2498 | for (int i = 0; i < packageCount; i++) { |
| 2499 | if (checkPolicyAccess(packages[i])) { |
| 2500 | accessAllowed = true; |
| 2501 | } |
| 2502 | } |
| 2503 | if (!accessAllowed) { |
| 2504 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 2505 | throw new SecurityException("Notification policy access denied"); |
| 2506 | } |
| 2507 | } |
| 2508 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2509 | private void enforcePolicyAccess(String pkg, String method) { |
Julia Reynolds | 6ee2617 | 2015-09-28 11:34:48 -0400 | [diff] [blame] | 2510 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 2511 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 2512 | return; |
| 2513 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 2514 | checkCallerIsSameApp(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2515 | if (!checkPolicyAccess(pkg)) { |
| 2516 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 2517 | throw new SecurityException("Notification policy access denied"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2518 | } |
| 2519 | } |
| 2520 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2521 | private boolean checkPackagePolicyAccess(String pkg) { |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 2522 | return mPolicyAccess.isPackageGranted(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | private boolean checkPolicyAccess(String pkg) { |
Julia Reynolds | 0867b3a | 2016-03-30 17:29:54 -0400 | [diff] [blame] | 2526 | try { |
| 2527 | int uid = getContext().getPackageManager().getPackageUidAsUser( |
| 2528 | pkg, UserHandle.getCallingUserId()); |
| 2529 | if (PackageManager.PERMISSION_GRANTED == ActivityManager.checkComponentPermission( |
| 2530 | android.Manifest.permission.MANAGE_NOTIFICATIONS, uid, |
| 2531 | -1, true)) { |
| 2532 | return true; |
| 2533 | } |
| 2534 | } catch (NameNotFoundException e) { |
| 2535 | return false; |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 2536 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2537 | return checkPackagePolicyAccess(pkg) || mListeners.isComponentEnabledForPackage(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2538 | } |
| 2539 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2540 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2541 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2542 | if (!DumpUtils.checkDumpAndUsageStatsPermission(getContext(), TAG, pw)) return; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2543 | final DumpFilter filter = DumpFilter.parseFromArguments(args); |
| 2544 | if (filter != null && filter.stats) { |
| 2545 | dumpJson(pw, filter); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 2546 | } else if (filter != null && filter.proto) { |
| 2547 | dumpProto(fd, filter); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2548 | } else { |
| 2549 | dumpImpl(pw, filter); |
| 2550 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2551 | } |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2552 | |
| 2553 | @Override |
| 2554 | public ComponentName getEffectsSuppressor() { |
Bryce Lee | ba3d895 | 2016-04-12 12:39:15 -0700 | [diff] [blame] | 2555 | return !mEffectsSuppressors.isEmpty() ? mEffectsSuppressors.get(0) : null; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2556 | } |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 2557 | |
| 2558 | @Override |
| 2559 | public boolean matchesCallFilter(Bundle extras) { |
| 2560 | enforceSystemOrSystemUI("INotificationManager.matchesCallFilter"); |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 2561 | return mZenModeHelper.matchesCallFilter( |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 2562 | Binder.getCallingUserHandle(), |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 2563 | extras, |
| 2564 | mRankingHelper.findExtractor(ValidateNotificationPeople.class), |
| 2565 | MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS, |
| 2566 | MATCHES_CALL_FILTER_TIMEOUT_AFFINITY); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 2567 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 2568 | |
| 2569 | @Override |
| 2570 | public boolean isSystemConditionProviderEnabled(String path) { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2571 | enforceSystemOrSystemUI("INotificationManager.isSystemConditionProviderEnabled"); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2572 | return mConditionProviders.isSystemProviderEnabled(path); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 2573 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2574 | |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 2575 | // Backup/restore interface |
| 2576 | @Override |
| 2577 | public byte[] getBackupPayload(int user) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2578 | if (DBG) Slog.d(TAG, "getBackupPayload u=" + user); |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 2579 | //TODO: http://b/22388012 |
| 2580 | if (user != UserHandle.USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2581 | Slog.w(TAG, "getBackupPayload: cannot backup policy for user " + user); |
| 2582 | return null; |
| 2583 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 2584 | synchronized(mPolicyFile) { |
| 2585 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 2586 | try { |
| 2587 | writePolicyXml(baos, true /*forBackup*/); |
| 2588 | return baos.toByteArray(); |
| 2589 | } catch (IOException e) { |
| 2590 | Slog.w(TAG, "getBackupPayload: error writing payload for user " + user, e); |
| 2591 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2592 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 2593 | return null; |
| 2594 | } |
| 2595 | |
| 2596 | @Override |
| 2597 | public void applyRestore(byte[] payload, int user) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2598 | if (DBG) Slog.d(TAG, "applyRestore u=" + user + " payload=" |
| 2599 | + (payload != null ? new String(payload, StandardCharsets.UTF_8) : null)); |
| 2600 | if (payload == null) { |
| 2601 | Slog.w(TAG, "applyRestore: no payload to restore for user " + user); |
| 2602 | return; |
| 2603 | } |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 2604 | //TODO: http://b/22388012 |
| 2605 | if (user != UserHandle.USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2606 | Slog.w(TAG, "applyRestore: cannot restore policy for user " + user); |
| 2607 | return; |
| 2608 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 2609 | synchronized(mPolicyFile) { |
| 2610 | final ByteArrayInputStream bais = new ByteArrayInputStream(payload); |
| 2611 | try { |
| 2612 | readPolicyXml(bais, true /*forRestore*/); |
| 2613 | savePolicyFile(); |
| 2614 | } catch (NumberFormatException | XmlPullParserException | IOException e) { |
| 2615 | Slog.w(TAG, "applyRestore: error reading payload", e); |
| 2616 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2617 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 2618 | } |
| 2619 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2620 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2621 | public boolean isNotificationPolicyAccessGranted(String pkg) { |
| 2622 | return checkPolicyAccess(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | @Override |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 2626 | public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {; |
| 2627 | enforceSystemOrSystemUIOrSamePackage(pkg, |
| 2628 | "request policy access status for another package"); |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 2629 | return checkPolicyAccess(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | @Override |
| 2633 | public String[] getPackagesRequestingNotificationPolicyAccess() |
| 2634 | throws RemoteException { |
| 2635 | enforceSystemOrSystemUI("request policy access packages"); |
| 2636 | final long identity = Binder.clearCallingIdentity(); |
| 2637 | try { |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 2638 | return mPolicyAccess.getRequestingPackages(); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2639 | } finally { |
| 2640 | Binder.restoreCallingIdentity(identity); |
| 2641 | } |
| 2642 | } |
| 2643 | |
| 2644 | @Override |
| 2645 | public void setNotificationPolicyAccessGranted(String pkg, boolean granted) |
| 2646 | throws RemoteException { |
| 2647 | enforceSystemOrSystemUI("grant notification policy access"); |
| 2648 | final long identity = Binder.clearCallingIdentity(); |
| 2649 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2650 | synchronized (mNotificationLock) { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2651 | mPolicyAccess.put(pkg, granted); |
| 2652 | } |
| 2653 | } finally { |
| 2654 | Binder.restoreCallingIdentity(identity); |
| 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | @Override |
| 2659 | public Policy getNotificationPolicy(String pkg) { |
| 2660 | enforcePolicyAccess(pkg, "getNotificationPolicy"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2661 | final long identity = Binder.clearCallingIdentity(); |
| 2662 | try { |
| 2663 | return mZenModeHelper.getNotificationPolicy(); |
| 2664 | } finally { |
| 2665 | Binder.restoreCallingIdentity(identity); |
| 2666 | } |
| 2667 | } |
| 2668 | |
| 2669 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2670 | public void setNotificationPolicy(String pkg, Policy policy) { |
| 2671 | enforcePolicyAccess(pkg, "setNotificationPolicy"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2672 | final long identity = Binder.clearCallingIdentity(); |
| 2673 | try { |
| 2674 | mZenModeHelper.setNotificationPolicy(policy); |
| 2675 | } finally { |
| 2676 | Binder.restoreCallingIdentity(identity); |
| 2677 | } |
| 2678 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2679 | |
| 2680 | @Override |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 2681 | public void applyEnqueuedAdjustmentFromAssistant(INotificationListener token, |
| 2682 | Adjustment adjustment) throws RemoteException { |
| 2683 | final long identity = Binder.clearCallingIdentity(); |
| 2684 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2685 | synchronized (mNotificationLock) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 2686 | mNotificationAssistants.checkServiceTokenLocked(token); |
| 2687 | int N = mEnqueuedNotifications.size(); |
| 2688 | for (int i = 0; i < N; i++) { |
| 2689 | final NotificationRecord n = mEnqueuedNotifications.get(i); |
| 2690 | if (Objects.equals(adjustment.getKey(), n.getKey()) |
| 2691 | && Objects.equals(adjustment.getUser(), n.getUserId())) { |
| 2692 | applyAdjustment(n, adjustment); |
| 2693 | break; |
| 2694 | } |
| 2695 | } |
| 2696 | } |
| 2697 | } finally { |
| 2698 | Binder.restoreCallingIdentity(identity); |
| 2699 | } |
| 2700 | } |
| 2701 | |
| 2702 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 2703 | public void applyAdjustmentFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2704 | Adjustment adjustment) throws RemoteException { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2705 | final long identity = Binder.clearCallingIdentity(); |
| 2706 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2707 | synchronized (mNotificationLock) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 2708 | mNotificationAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 2709 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 2710 | applyAdjustment(n, adjustment); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2711 | } |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 2712 | mRankingHandler.requestSort(true); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2713 | } finally { |
| 2714 | Binder.restoreCallingIdentity(identity); |
| 2715 | } |
| 2716 | } |
| 2717 | |
| 2718 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 2719 | public void applyAdjustmentsFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2720 | List<Adjustment> adjustments) throws RemoteException { |
| 2721 | |
| 2722 | final long identity = Binder.clearCallingIdentity(); |
| 2723 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2724 | synchronized (mNotificationLock) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 2725 | mNotificationAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2726 | for (Adjustment adjustment : adjustments) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 2727 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 2728 | applyAdjustment(n, adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2729 | } |
| 2730 | } |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 2731 | mRankingHandler.requestSort(true); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2732 | } finally { |
| 2733 | Binder.restoreCallingIdentity(identity); |
| 2734 | } |
| 2735 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2736 | |
| 2737 | @Override |
| 2738 | public void updateNotificationChannelFromPrivilegedListener(INotificationListener token, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2739 | String pkg, UserHandle user, NotificationChannel channel) throws RemoteException { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2740 | Preconditions.checkNotNull(channel); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2741 | Preconditions.checkNotNull(pkg); |
| 2742 | Preconditions.checkNotNull(user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2743 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2744 | verifyPrivilegedListener(token, user); |
| 2745 | updateNotificationChannelInt(pkg, getUidForPackageAndUser(pkg, user), channel, true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | @Override |
| 2749 | public ParceledListSlice<NotificationChannel> getNotificationChannelsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2750 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 2751 | Preconditions.checkNotNull(pkg); |
| 2752 | Preconditions.checkNotNull(user); |
| 2753 | verifyPrivilegedListener(token, user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2754 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2755 | return mRankingHelper.getNotificationChannels(pkg, getUidForPackageAndUser(pkg, user), |
| 2756 | false /* includeDeleted */); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | @Override |
| 2760 | public ParceledListSlice<NotificationChannelGroup> |
| 2761 | getNotificationChannelGroupsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2762 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 2763 | Preconditions.checkNotNull(pkg); |
| 2764 | Preconditions.checkNotNull(user); |
| 2765 | verifyPrivilegedListener(token, user); |
| 2766 | |
| 2767 | List<NotificationChannelGroup> groups = new ArrayList<>(); |
| 2768 | groups.addAll(mRankingHelper.getNotificationChannelGroups( |
| 2769 | pkg, getUidForPackageAndUser(pkg, user))); |
| 2770 | return new ParceledListSlice<>(groups); |
| 2771 | } |
| 2772 | |
| 2773 | private void verifyPrivilegedListener(INotificationListener token, UserHandle user) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2774 | ManagedServiceInfo info; |
| 2775 | synchronized (mNotificationLock) { |
| 2776 | info = mListeners.checkServiceTokenLocked(token); |
| 2777 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 2778 | if (!hasCompanionDevice(info)) { |
| 2779 | throw new SecurityException(info + " does not have access"); |
| 2780 | } |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2781 | if (!info.enabledAndUserMatches(user.getIdentifier())) { |
| 2782 | throw new SecurityException(info + " does not have access"); |
| 2783 | } |
| 2784 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2785 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2786 | private int getUidForPackageAndUser(String pkg, UserHandle user) throws RemoteException { |
| 2787 | int uid = 0; |
| 2788 | long identity = Binder.clearCallingIdentity(); |
| 2789 | try { |
| 2790 | uid = mPackageManager.getPackageUid(pkg, 0, user.getIdentifier()); |
| 2791 | } finally { |
| 2792 | Binder.restoreCallingIdentity(identity); |
| 2793 | } |
| 2794 | return uid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2795 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2796 | }; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2797 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 2798 | private void applyAdjustment(NotificationRecord n, Adjustment adjustment) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2799 | if (n == null) { |
| 2800 | return; |
| 2801 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2802 | if (adjustment.getSignals() != null) { |
| 2803 | Bundle.setDefusable(adjustment.getSignals(), true); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 2804 | final ArrayList<String> people = |
| 2805 | adjustment.getSignals().getStringArrayList(Adjustment.KEY_PEOPLE); |
| 2806 | final ArrayList<SnoozeCriterion> snoozeCriterionList = |
| 2807 | adjustment.getSignals().getParcelableArrayList(Adjustment.KEY_SNOOZE_CRITERIA); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 2808 | n.setPeopleOverride(people); |
| 2809 | n.setSnoozeCriteria(snoozeCriterionList); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2810 | } |
| 2811 | } |
| 2812 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2813 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2814 | private void addAutogroupKeyLocked(String key) { |
| 2815 | NotificationRecord n = mNotificationsByKey.get(key); |
| 2816 | if (n == null) { |
| 2817 | return; |
| 2818 | } |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 2819 | n.setOverrideGroupKey(GroupHelper.AUTOGROUP_KEY); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2820 | EventLogTags.writeNotificationAutogrouped(key); |
| 2821 | } |
| 2822 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2823 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2824 | private void removeAutogroupKeyLocked(String key) { |
| 2825 | NotificationRecord n = mNotificationsByKey.get(key); |
| 2826 | if (n == null) { |
| 2827 | return; |
| 2828 | } |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 2829 | n.setOverrideGroupKey(null); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2830 | EventLogTags.writeNotificationUnautogrouped(key); |
| 2831 | } |
| 2832 | |
| 2833 | // Clears the 'fake' auto-group summary. |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2834 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2835 | private void clearAutogroupSummaryLocked(int userId, String pkg) { |
| 2836 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 2837 | if (summaries != null && summaries.containsKey(pkg)) { |
| 2838 | // Clear summary. |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2839 | final NotificationRecord removed = findNotificationByKeyLocked(summaries.remove(pkg)); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2840 | if (removed != null) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 2841 | boolean wasPosted = removeFromNotificationListsLocked(removed); |
| 2842 | cancelNotificationLocked(removed, false, REASON_UNAUTOBUNDLED, wasPosted); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | } |
| 2846 | |
| 2847 | // 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] | 2848 | private void createAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 2849 | NotificationRecord summaryRecord = null; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2850 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2851 | NotificationRecord notificationRecord = mNotificationsByKey.get(triggeringKey); |
| 2852 | if (notificationRecord == null) { |
| 2853 | // The notification could have been cancelled again already. A successive |
| 2854 | // adjustment will post a summary if needed. |
| 2855 | return; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2856 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2857 | final StatusBarNotification adjustedSbn = notificationRecord.sbn; |
| 2858 | userId = adjustedSbn.getUser().getIdentifier(); |
| 2859 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 2860 | if (summaries == null) { |
| 2861 | summaries = new ArrayMap<>(); |
| 2862 | } |
| 2863 | mAutobundledSummaries.put(userId, summaries); |
| 2864 | if (!summaries.containsKey(pkg)) { |
| 2865 | // Add summary |
| 2866 | final ApplicationInfo appInfo = |
| 2867 | adjustedSbn.getNotification().extras.getParcelable( |
| 2868 | Notification.EXTRA_BUILDER_APPLICATION_INFO); |
| 2869 | final Bundle extras = new Bundle(); |
| 2870 | extras.putParcelable(Notification.EXTRA_BUILDER_APPLICATION_INFO, appInfo); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 2871 | final String channelId = notificationRecord.getChannel().getId(); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2872 | final Notification summaryNotification = |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 2873 | new Notification.Builder(getContext(), channelId) |
| 2874 | .setSmallIcon(adjustedSbn.getNotification().getSmallIcon()) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2875 | .setGroupSummary(true) |
Julia Reynolds | 9d5786e | 2017-04-28 10:26:32 -0400 | [diff] [blame] | 2876 | .setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2877 | .setGroup(GroupHelper.AUTOGROUP_KEY) |
| 2878 | .setFlag(Notification.FLAG_AUTOGROUP_SUMMARY, true) |
| 2879 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 2880 | .setColor(adjustedSbn.getNotification().color) |
| 2881 | .setLocalOnly(true) |
| 2882 | .build(); |
| 2883 | summaryNotification.extras.putAll(extras); |
| 2884 | Intent appIntent = getContext().getPackageManager().getLaunchIntentForPackage(pkg); |
| 2885 | if (appIntent != null) { |
| 2886 | summaryNotification.contentIntent = PendingIntent.getActivityAsUser( |
| 2887 | getContext(), 0, appIntent, 0, null, UserHandle.of(userId)); |
| 2888 | } |
| 2889 | final StatusBarNotification summarySbn = |
| 2890 | new StatusBarNotification(adjustedSbn.getPackageName(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 2891 | adjustedSbn.getOpPkg(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 2892 | Integer.MAX_VALUE, |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2893 | GroupHelper.AUTOGROUP_KEY, adjustedSbn.getUid(), |
| 2894 | adjustedSbn.getInitialPid(), summaryNotification, |
| 2895 | adjustedSbn.getUser(), GroupHelper.AUTOGROUP_KEY, |
| 2896 | System.currentTimeMillis()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2897 | summaryRecord = new NotificationRecord(getContext(), summarySbn, |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 2898 | notificationRecord.getChannel()); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2899 | summaries.put(pkg, summarySbn.getKey()); |
| 2900 | } |
| 2901 | } |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 2902 | if (summaryRecord != null && checkDisqualifyingFeatures(userId, MY_UID, |
| 2903 | summaryRecord.sbn.getId(), summaryRecord.sbn.getTag(), summaryRecord)) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2904 | mHandler.post(new EnqueueNotificationRunnable(userId, summaryRecord)); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2905 | } |
| 2906 | } |
| 2907 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 2908 | private String disableNotificationEffects(NotificationRecord record) { |
| 2909 | if (mDisableNotificationEffects) { |
| 2910 | return "booleanState"; |
| 2911 | } |
| 2912 | if ((mListenerHints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 2913 | return "listenerHints"; |
| 2914 | } |
| 2915 | if (mCallState != TelephonyManager.CALL_STATE_IDLE && !mZenModeHelper.isCall(record)) { |
| 2916 | return "callState"; |
| 2917 | } |
| 2918 | return null; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2919 | }; |
| 2920 | |
| 2921 | private void dumpJson(PrintWriter pw, DumpFilter filter) { |
| 2922 | JSONObject dump = new JSONObject(); |
| 2923 | try { |
| 2924 | dump.put("service", "Notification Manager"); |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 2925 | dump.put("bans", mRankingHelper.dumpBansJson(filter)); |
| 2926 | dump.put("ranking", mRankingHelper.dumpJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2927 | dump.put("stats", mUsageStats.dumpJson(filter)); |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 2928 | dump.put("channels", mRankingHelper.dumpChannelsJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2929 | } catch (JSONException e) { |
| 2930 | e.printStackTrace(); |
| 2931 | } |
| 2932 | pw.println(dump); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2933 | } |
| 2934 | |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 2935 | private void dumpProto(FileDescriptor fd, DumpFilter filter) { |
| 2936 | final ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 2937 | synchronized (mNotificationLock) { |
| 2938 | long records = proto.start(NotificationServiceDumpProto.RECORDS); |
| 2939 | int N = mNotificationList.size(); |
| 2940 | if (N > 0) { |
| 2941 | for (int i = 0; i < N; i++) { |
| 2942 | final NotificationRecord nr = mNotificationList.get(i); |
| 2943 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 2944 | nr.dump(proto, filter.redact); |
| 2945 | proto.write(NotificationRecordProto.STATE, NotificationServiceProto.POSTED); |
| 2946 | } |
| 2947 | } |
| 2948 | N = mEnqueuedNotifications.size(); |
| 2949 | if (N > 0) { |
| 2950 | for (int i = 0; i < N; i++) { |
| 2951 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 2952 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 2953 | nr.dump(proto, filter.redact); |
| 2954 | proto.write(NotificationRecordProto.STATE, NotificationServiceProto.ENQUEUED); |
| 2955 | } |
| 2956 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 2957 | List<NotificationRecord> snoozed = mSnoozeHelper.getSnoozed(); |
| 2958 | N = snoozed.size(); |
| 2959 | if (N > 0) { |
| 2960 | for (int i = 0; i < N; i++) { |
| 2961 | final NotificationRecord nr = snoozed.get(i); |
| 2962 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 2963 | nr.dump(proto, filter.redact); |
| 2964 | proto.write(NotificationRecordProto.STATE, NotificationServiceProto.SNOOZED); |
| 2965 | } |
| 2966 | } |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 2967 | proto.end(records); |
| 2968 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 2969 | |
| 2970 | long zenLog = proto.start(NotificationServiceDumpProto.ZEN); |
| 2971 | mZenModeHelper.dump(proto); |
| 2972 | for (ComponentName suppressor : mEffectsSuppressors) { |
| 2973 | proto.write(ZenModeProto.SUPPRESSORS, suppressor.toString()); |
| 2974 | } |
| 2975 | proto.end(zenLog); |
| 2976 | |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 2977 | proto.flush(); |
| 2978 | } |
| 2979 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2980 | void dumpImpl(PrintWriter pw, DumpFilter filter) { |
| 2981 | pw.print("Current Notification Manager state"); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 2982 | if (filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2983 | pw.print(" (filtered to "); pw.print(filter); pw.print(")"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2984 | } |
| 2985 | pw.println(':'); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2986 | int N; |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 2987 | final boolean zenOnly = filter.filtered && filter.zen; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2988 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2989 | if (!zenOnly) { |
| 2990 | synchronized (mToastQueue) { |
| 2991 | N = mToastQueue.size(); |
| 2992 | if (N > 0) { |
| 2993 | pw.println(" Toast Queue:"); |
| 2994 | for (int i=0; i<N; i++) { |
| 2995 | mToastQueue.get(i).dump(pw, " ", filter); |
| 2996 | } |
| 2997 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2998 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2999 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3000 | } |
| 3001 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3002 | synchronized (mNotificationLock) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3003 | if (!zenOnly) { |
| 3004 | N = mNotificationList.size(); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3005 | if (N > 0) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3006 | pw.println(" Notification List:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3007 | for (int i=0; i<N; i++) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3008 | final NotificationRecord nr = mNotificationList.get(i); |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3009 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3010 | nr.dump(pw, " ", getContext(), filter.redact); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3011 | } |
| 3012 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3013 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3014 | |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 3015 | if (!filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3016 | N = mLights.size(); |
| 3017 | if (N > 0) { |
| 3018 | pw.println(" Lights List:"); |
| 3019 | for (int i=0; i<N; i++) { |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3020 | if (i == N - 1) { |
| 3021 | pw.print(" > "); |
| 3022 | } else { |
| 3023 | pw.print(" "); |
| 3024 | } |
| 3025 | pw.println(mLights.get(i)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3026 | } |
| 3027 | pw.println(" "); |
| 3028 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 3029 | pw.println(" mUseAttentionLight=" + mUseAttentionLight); |
| 3030 | pw.println(" mNotificationPulseEnabled=" + mNotificationPulseEnabled); |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3031 | pw.println(" mSoundNotificationKey=" + mSoundNotificationKey); |
| 3032 | pw.println(" mVibrateNotificationKey=" + mVibrateNotificationKey); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3033 | pw.println(" mDisableNotificationEffects=" + mDisableNotificationEffects); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3034 | pw.println(" mCallState=" + callStateToString(mCallState)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3035 | pw.println(" mSystemReady=" + mSystemReady); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 3036 | pw.println(" mMaxPackageEnqueueRate=" + mMaxPackageEnqueueRate); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3037 | } |
| 3038 | pw.println(" mArchive=" + mArchive.toString()); |
| 3039 | Iterator<StatusBarNotification> iter = mArchive.descendingIterator(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3040 | int j=0; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3041 | while (iter.hasNext()) { |
| 3042 | final StatusBarNotification sbn = iter.next(); |
| 3043 | if (filter != null && !filter.matches(sbn)) continue; |
| 3044 | pw.println(" " + sbn); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3045 | if (++j >= 5) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3046 | if (iter.hasNext()) pw.println(" ..."); |
| 3047 | break; |
| 3048 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3049 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3050 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3051 | if (!zenOnly) { |
| 3052 | N = mEnqueuedNotifications.size(); |
| 3053 | if (N > 0) { |
| 3054 | pw.println(" Enqueued Notification List:"); |
| 3055 | for (int i = 0; i < N; i++) { |
| 3056 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 3057 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3058 | nr.dump(pw, " ", getContext(), filter.redact); |
| 3059 | } |
| 3060 | pw.println(" "); |
| 3061 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3062 | |
| 3063 | mSnoozeHelper.dump(pw, filter); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3064 | } |
| 3065 | } |
| 3066 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3067 | if (!zenOnly) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3068 | pw.println("\n Ranking Config:"); |
| 3069 | mRankingHelper.dump(pw, " ", filter); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 3070 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3071 | pw.println("\n Notification listeners:"); |
| 3072 | mListeners.dump(pw, filter); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3073 | pw.print(" mListenerHints: "); pw.println(mListenerHints); |
| 3074 | pw.print(" mListenersDisablingEffects: ("); |
| 3075 | N = mListenersDisablingEffects.size(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3076 | for (int i = 0; i < N; i++) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 3077 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 3078 | if (i > 0) pw.print(';'); |
| 3079 | pw.print("hint[" + hint + "]:"); |
| 3080 | |
| 3081 | final ArraySet<ManagedServiceInfo> listeners = |
| 3082 | mListenersDisablingEffects.valueAt(i); |
| 3083 | final int listenerSize = listeners.size(); |
| 3084 | |
| 3085 | for (int j = 0; j < listenerSize; j++) { |
| 3086 | if (i > 0) pw.print(','); |
| 3087 | final ManagedServiceInfo listener = listeners.valueAt(i); |
| 3088 | pw.print(listener.component); |
| 3089 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3090 | } |
| 3091 | pw.println(')'); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 3092 | pw.println("\n Notification assistant services:"); |
| 3093 | mNotificationAssistants.dump(pw, filter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3094 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3095 | |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3096 | if (!filter.filtered || zenOnly) { |
| 3097 | pw.println("\n Zen Mode:"); |
| 3098 | pw.print(" mInterruptionFilter="); pw.println(mInterruptionFilter); |
| 3099 | mZenModeHelper.dump(pw, " "); |
| 3100 | |
| 3101 | pw.println("\n Zen Log:"); |
| 3102 | ZenLog.dump(pw, " "); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3103 | } |
| 3104 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3105 | pw.println("\n Policy access:"); |
| 3106 | pw.print(" mPolicyAccess: "); pw.println(mPolicyAccess); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3107 | |
| 3108 | pw.println("\n Condition providers:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3109 | mConditionProviders.dump(pw, filter); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3110 | |
| 3111 | pw.println("\n Group summaries:"); |
| 3112 | for (Entry<String, NotificationRecord> entry : mSummaryByGroupKey.entrySet()) { |
| 3113 | NotificationRecord r = entry.getValue(); |
| 3114 | pw.println(" " + entry.getKey() + " -> " + r.getKey()); |
| 3115 | if (mNotificationsByKey.get(r.getKey()) != r) { |
| 3116 | pw.println("!!!!!!LEAK: Record not found in mNotificationsByKey."); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3117 | r.dump(pw, " ", getContext(), filter.redact); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3118 | } |
| 3119 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3120 | |
| 3121 | if (!zenOnly) { |
| 3122 | pw.println("\n Usage Stats:"); |
| 3123 | mUsageStats.dump(pw, " ", filter); |
| 3124 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3125 | } |
| 3126 | } |
| 3127 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3128 | /** |
| 3129 | * The private API only accessible to the system process. |
| 3130 | */ |
| 3131 | private final NotificationManagerInternal mInternalService = new NotificationManagerInternal() { |
| 3132 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3133 | public void enqueueNotification(String pkg, String opPkg, int callingUid, int callingPid, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3134 | String tag, int id, Notification notification, int userId) { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3135 | enqueueNotificationInternal(pkg, opPkg, callingUid, callingPid, tag, id, notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3136 | userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3137 | } |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3138 | |
| 3139 | @Override |
| 3140 | public void removeForegroundServiceFlagFromNotification(String pkg, int notificationId, |
| 3141 | int userId) { |
| 3142 | checkCallerIsSystem(); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3143 | mHandler.post(new Runnable() { |
| 3144 | @Override |
| 3145 | public void run() { |
| 3146 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3147 | removeForegroundServiceFlagByListLocked( |
| 3148 | mEnqueuedNotifications, pkg, notificationId, userId); |
| 3149 | removeForegroundServiceFlagByListLocked( |
| 3150 | mNotificationList, pkg, notificationId, userId); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3151 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3152 | } |
| 3153 | }); |
| 3154 | } |
| 3155 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3156 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3157 | private void removeForegroundServiceFlagByListLocked( |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3158 | ArrayList<NotificationRecord> notificationList, String pkg, int notificationId, |
| 3159 | int userId) { |
| 3160 | NotificationRecord r = findNotificationByListLocked( |
| 3161 | notificationList, pkg, null, notificationId, userId); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3162 | if (r == null) { |
| 3163 | return; |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3164 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3165 | StatusBarNotification sbn = r.sbn; |
| 3166 | // NoMan adds flags FLAG_NO_CLEAR and FLAG_ONGOING_EVENT when it sees |
| 3167 | // FLAG_FOREGROUND_SERVICE. Hence it's not enough to remove |
| 3168 | // FLAG_FOREGROUND_SERVICE, we have to revert to the flags we received |
| 3169 | // initially *and* force remove FLAG_FOREGROUND_SERVICE. |
| 3170 | sbn.getNotification().flags = |
| 3171 | (r.mOriginalFlags & ~Notification.FLAG_FOREGROUND_SERVICE); |
| 3172 | mRankingHelper.sort(mNotificationList); |
| 3173 | mListeners.notifyPostedLocked(sbn, sbn /* oldSbn */); |
| 3174 | mGroupHelper.onNotificationPosted(sbn); |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3175 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3176 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3177 | |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3178 | void enqueueNotificationInternal(final String pkg, final String opPkg, final int callingUid, |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 3179 | final int callingPid, final String tag, final int id, final Notification notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3180 | int incomingUserId) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 3181 | if (DBG) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3182 | Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id |
| 3183 | + " notification=" + notification); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 3184 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3185 | checkCallerIsSystemOrSameApp(pkg); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3186 | |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 3187 | final int userId = ActivityManager.handleIncomingUser(callingPid, |
| 3188 | callingUid, incomingUserId, true, false, "enqueueNotification", pkg); |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 3189 | final UserHandle user = new UserHandle(userId); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3190 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3191 | if (pkg == null || notification == null) { |
| 3192 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 3193 | + " id=" + id + " notification=" + notification); |
| 3194 | } |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3195 | |
| 3196 | // The system can post notifications for any package, let us resolve that. |
| 3197 | final int notificationUid = resolveNotificationUid(opPkg, callingUid, userId); |
| 3198 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3199 | // Fix the notification as best we can. |
| 3200 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3201 | final ApplicationInfo ai = mPackageManagerClient.getApplicationInfoAsUser( |
Jeff Sharkey | 012bc7b | 2016-04-11 16:30:27 -0600 | [diff] [blame] | 3202 | pkg, PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 3203 | (userId == UserHandle.USER_ALL) ? UserHandle.USER_SYSTEM : userId); |
Julia Reynolds | e071abd | 2017-03-22 10:52:11 -0400 | [diff] [blame] | 3204 | Notification.addFieldsFromContext(ai, notification); |
Julia Reynolds | d71c5a9 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3205 | |
Dan Sandler | 708a1ca | 2017-08-11 20:30:24 -0400 | [diff] [blame^] | 3206 | boolean canColorize = PERMISSION_GRANTED == mPackageManagerClient.checkPermission( |
| 3207 | "com.android.permission.USE_COLORIZED_NOTIFICATIONS", pkg); |
| 3208 | if (canColorize) { |
Julia Reynolds | d71c5a9 | 2017-06-30 13:34:01 -0400 | [diff] [blame] | 3209 | notification.flags |= Notification.FLAG_CAN_COLORIZE; |
| 3210 | } else { |
| 3211 | notification.flags &= ~Notification.FLAG_CAN_COLORIZE; |
| 3212 | } |
| 3213 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3214 | } catch (NameNotFoundException e) { |
| 3215 | Slog.e(TAG, "Cannot create a context for sending app", e); |
| 3216 | return; |
| 3217 | } |
| 3218 | |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 3219 | mUsageStats.registerEnqueuedByApp(pkg); |
| 3220 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3221 | // setup local book-keeping |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 3222 | String channelId = notification.getChannelId(); |
| 3223 | if (mIsTelevision && (new Notification.TvExtender(notification)).getChannelId() != null) { |
| 3224 | channelId = (new Notification.TvExtender(notification)).getChannelId(); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 3225 | } |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3226 | final NotificationChannel channel = mRankingHelper.getNotificationChannel(pkg, |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3227 | notificationUid, channelId, false /* includeDeleted */); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3228 | if (channel == null) { |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3229 | final String noChannelStr = "No Channel found for " |
| 3230 | + "pkg=" + pkg |
| 3231 | + ", channelId=" + channelId |
Julia Reynolds | f26eb91 | 2017-05-22 15:47:06 -0400 | [diff] [blame] | 3232 | + ", id=" + id |
| 3233 | + ", tag=" + tag |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3234 | + ", opPkg=" + opPkg |
| 3235 | + ", callingUid=" + callingUid |
| 3236 | + ", userId=" + userId |
| 3237 | + ", incomingUserId=" + incomingUserId |
| 3238 | + ", notificationUid=" + notificationUid |
| 3239 | + ", notification=" + notification; |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3240 | Log.e(TAG, noChannelStr); |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 3241 | doChannelWarningToast("Developer warning for package \"" + pkg + "\"\n" + |
Geoffrey Pitsch | cadb5dc | 2017-04-11 11:35:02 -0400 | [diff] [blame] | 3242 | "Failed to post notification on channel \"" + channelId + "\"\n" + |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3243 | "See log for more details"); |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3244 | return; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3245 | } |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3246 | |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 3247 | final StatusBarNotification n = new StatusBarNotification( |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3248 | pkg, opPkg, id, tag, notificationUid, callingPid, notification, |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3249 | user, null, System.currentTimeMillis()); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 3250 | final NotificationRecord r = new NotificationRecord(getContext(), n, channel); |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 3251 | |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 3252 | if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r)) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3253 | return; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 3254 | } |
| 3255 | |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 3256 | // Whitelist pending intents. |
| 3257 | if (notification.allPendingIntents != null) { |
| 3258 | final int intentCount = notification.allPendingIntents.size(); |
| 3259 | if (intentCount > 0) { |
| 3260 | final ActivityManagerInternal am = LocalServices |
| 3261 | .getService(ActivityManagerInternal.class); |
| 3262 | final long duration = LocalServices.getService( |
| 3263 | DeviceIdleController.LocalService.class).getNotificationWhitelistDuration(); |
| 3264 | for (int i = 0; i < intentCount; i++) { |
| 3265 | PendingIntent pendingIntent = notification.allPendingIntents.valueAt(i); |
| 3266 | if (pendingIntent != null) { |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 3267 | am.setPendingIntentWhitelistDuration(pendingIntent.getTarget(), |
| 3268 | WHITELIST_TOKEN, duration); |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 3269 | } |
| 3270 | } |
| 3271 | } |
| 3272 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 3273 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3274 | mHandler.post(new EnqueueNotificationRunnable(userId, r)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3275 | } |
| 3276 | |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 3277 | private void doChannelWarningToast(CharSequence toastText) { |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 3278 | final int defaultWarningEnabled = Build.IS_DEBUGGABLE ? 1 : 0; |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 3279 | final boolean warningEnabled = Settings.Global.getInt(getContext().getContentResolver(), |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 3280 | Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, defaultWarningEnabled) != 0; |
| 3281 | if (warningEnabled) { |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 3282 | Toast toast = Toast.makeText(getContext(), mHandler.getLooper(), toastText, |
Geoffrey Pitsch | 5bdddbd | 2017-05-26 10:50:05 -0400 | [diff] [blame] | 3283 | Toast.LENGTH_SHORT); |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 3284 | toast.show(); |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3285 | } |
| 3286 | } |
| 3287 | |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3288 | private int resolveNotificationUid(String opPackageName, int callingUid, int userId) { |
| 3289 | // The system can post notifications on behalf of any package it wants |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3290 | if (isCallerSystemOrPhone() && opPackageName != null && !"android".equals(opPackageName)) { |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3291 | try { |
| 3292 | return getContext().getPackageManager() |
| 3293 | .getPackageUidAsUser(opPackageName, userId); |
| 3294 | } catch (NameNotFoundException e) { |
| 3295 | /* ignore */ |
| 3296 | } |
| 3297 | } |
| 3298 | return callingUid; |
| 3299 | } |
| 3300 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3301 | /** |
| 3302 | * Checks if a notification can be posted. checks rate limiter, snooze helper, and blocking. |
| 3303 | * |
| 3304 | * Has side effects. |
| 3305 | */ |
| 3306 | private boolean checkDisqualifyingFeatures(int userId, int callingUid, int id, String tag, |
| 3307 | NotificationRecord r) { |
| 3308 | final String pkg = r.sbn.getPackageName(); |
Sailesh Nepal | e8bde70 | 2017-07-21 11:44:04 -0700 | [diff] [blame] | 3309 | final String dialerPackage = |
| 3310 | getContext().getSystemService(TelecomManager.class).getSystemDialerPackage(); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3311 | final boolean isSystemNotification = |
Sailesh Nepal | e8bde70 | 2017-07-21 11:44:04 -0700 | [diff] [blame] | 3312 | isUidSystemOrPhone(callingUid) || ("android".equals(pkg)) |
| 3313 | || TextUtils.equals(pkg, dialerPackage); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3314 | final boolean isNotificationFromListener = mListeners.isListenerPackage(pkg); |
| 3315 | |
| 3316 | // Limit the number of notifications that any given package except the android |
| 3317 | // package or a registered listener can enqueue. Prevents DOS attacks and deals with leaks. |
| 3318 | if (!isSystemNotification && !isNotificationFromListener) { |
| 3319 | synchronized (mNotificationLock) { |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3320 | if (mNotificationsByKey.get(r.sbn.getKey()) == null && isCallerInstantApp(pkg)) { |
| 3321 | // Ephemeral apps have some special constraints for notifications. |
| 3322 | // They are not allowed to create new notifications however they are allowed to |
| 3323 | // update notifications created by the system (e.g. a foreground service |
| 3324 | // notification). |
| 3325 | throw new SecurityException("Instant app " + pkg |
| 3326 | + " cannot create notifications"); |
| 3327 | } |
| 3328 | |
| 3329 | // rate limit updates that aren't completed progress notifications |
| 3330 | if (mNotificationsByKey.get(r.sbn.getKey()) != null |
| 3331 | && !r.getNotification().hasCompletedProgress()) { |
| 3332 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3333 | final float appEnqueueRate = mUsageStats.getAppEnqueueRate(pkg); |
| 3334 | if (appEnqueueRate > mMaxPackageEnqueueRate) { |
| 3335 | mUsageStats.registerOverRateQuota(pkg); |
| 3336 | final long now = SystemClock.elapsedRealtime(); |
| 3337 | if ((now - mLastOverRateLogTime) > MIN_PACKAGE_OVERRATE_LOG_INTERVAL) { |
| 3338 | Slog.e(TAG, "Package enqueue rate is " + appEnqueueRate |
| 3339 | + ". Shedding events. package=" + pkg); |
| 3340 | mLastOverRateLogTime = now; |
| 3341 | } |
| 3342 | return false; |
| 3343 | } |
| 3344 | } |
| 3345 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3346 | // limit the number of outstanding notificationrecords an app can have |
| 3347 | int count = getNotificationCountLocked(pkg, userId, id, tag); |
| 3348 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 3349 | mUsageStats.registerOverCountQuota(pkg); |
| 3350 | Slog.e(TAG, "Package has already posted or enqueued " + count |
| 3351 | + " notifications. Not showing more. package=" + pkg); |
| 3352 | return false; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3353 | } |
| 3354 | } |
| 3355 | } |
| 3356 | |
| 3357 | // snoozed apps |
| 3358 | if (mSnoozeHelper.isSnoozed(userId, pkg, r.getKey())) { |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3359 | MetricsLogger.action(r.getLogMaker() |
| 3360 | .setType(MetricsProto.MetricsEvent.TYPE_UPDATE) |
| 3361 | .setCategory(MetricsProto.MetricsEvent.NOTIFICATION_SNOOZED)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3362 | if (DBG) { |
| 3363 | Slog.d(TAG, "Ignored enqueue for snoozed notification " + r.getKey()); |
| 3364 | } |
| 3365 | mSnoozeHelper.update(userId, r); |
| 3366 | savePolicyFile(); |
| 3367 | return false; |
| 3368 | } |
| 3369 | |
| 3370 | |
| 3371 | // blocked apps |
| 3372 | if (isBlocked(r, mUsageStats)) { |
| 3373 | return false; |
| 3374 | } |
| 3375 | |
| 3376 | return true; |
| 3377 | } |
| 3378 | |
Julia Reynolds | 6ad0aec | 2017-07-05 08:47:03 -0400 | [diff] [blame] | 3379 | protected int getNotificationCountLocked(String pkg, int userId, int excludedId, |
| 3380 | String excludedTag) { |
| 3381 | int count = 0; |
| 3382 | final int N = mNotificationList.size(); |
| 3383 | for (int i = 0; i < N; i++) { |
| 3384 | final NotificationRecord existing = mNotificationList.get(i); |
| 3385 | if (existing.sbn.getPackageName().equals(pkg) |
| 3386 | && existing.sbn.getUserId() == userId) { |
| 3387 | if (existing.sbn.getId() == excludedId |
| 3388 | && TextUtils.equals(existing.sbn.getTag(), excludedTag)) { |
| 3389 | continue; |
| 3390 | } |
| 3391 | count++; |
| 3392 | } |
| 3393 | } |
| 3394 | final int M = mEnqueuedNotifications.size(); |
| 3395 | for (int i = 0; i < M; i++) { |
| 3396 | final NotificationRecord existing = mEnqueuedNotifications.get(i); |
| 3397 | if (existing.sbn.getPackageName().equals(pkg) |
| 3398 | && existing.sbn.getUserId() == userId) { |
| 3399 | count++; |
| 3400 | } |
| 3401 | } |
| 3402 | return count; |
| 3403 | } |
| 3404 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3405 | protected boolean isBlocked(NotificationRecord r, NotificationUsageStats usageStats) { |
| 3406 | final String pkg = r.sbn.getPackageName(); |
| 3407 | final int callingUid = r.sbn.getUid(); |
| 3408 | |
| 3409 | final boolean isPackageSuspended = isPackageSuspendedForUser(pkg, callingUid); |
| 3410 | if (isPackageSuspended) { |
| 3411 | Slog.e(TAG, "Suppressing notification from package due to package " |
| 3412 | + "suspended by administrator."); |
| 3413 | usageStats.registerSuspendedByAdmin(r); |
| 3414 | return isPackageSuspended; |
| 3415 | } |
| 3416 | |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 3417 | final boolean isBlocked = |
| 3418 | mRankingHelper.getImportance(pkg, callingUid) == NotificationManager.IMPORTANCE_NONE |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 3419 | || r.getChannel().getImportance() == NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3420 | if (isBlocked) { |
| 3421 | Slog.e(TAG, "Suppressing notification from package by user request."); |
| 3422 | usageStats.registerBlocked(r); |
| 3423 | } |
| 3424 | return isBlocked; |
| 3425 | } |
| 3426 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3427 | protected class SnoozeNotificationRunnable implements Runnable { |
| 3428 | private final String mKey; |
| 3429 | private final long mDuration; |
| 3430 | private final String mSnoozeCriterionId; |
| 3431 | |
| 3432 | SnoozeNotificationRunnable(String key, long duration, String snoozeCriterionId) { |
| 3433 | mKey = key; |
| 3434 | mDuration = duration; |
| 3435 | mSnoozeCriterionId = snoozeCriterionId; |
| 3436 | } |
| 3437 | |
| 3438 | @Override |
| 3439 | public void run() { |
| 3440 | synchronized (mNotificationLock) { |
| 3441 | final NotificationRecord r = findNotificationByKeyLocked(mKey); |
| 3442 | if (r != null) { |
| 3443 | snoozeLocked(r); |
| 3444 | } |
| 3445 | } |
| 3446 | } |
| 3447 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3448 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3449 | void snoozeLocked(NotificationRecord r) { |
| 3450 | if (r.sbn.isGroup()) { |
| 3451 | final List<NotificationRecord> groupNotifications = findGroupNotificationsLocked( |
| 3452 | r.sbn.getPackageName(), r.sbn.getGroupKey(), r.sbn.getUserId()); |
| 3453 | if (r.getNotification().isGroupSummary()) { |
| 3454 | // snooze summary and all children |
| 3455 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 3456 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 3457 | } |
| 3458 | } else { |
| 3459 | // if there is a valid summary for this group, and we are snoozing the only |
| 3460 | // child, also snooze the summary |
| 3461 | if (mSummaryByGroupKey.containsKey(r.sbn.getGroupKey())) { |
| 3462 | if (groupNotifications.size() != 2) { |
| 3463 | snoozeNotificationLocked(r); |
| 3464 | } else { |
| 3465 | // snooze summary and the one child |
| 3466 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 3467 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 3468 | } |
| 3469 | } |
| 3470 | } else { |
| 3471 | snoozeNotificationLocked(r); |
| 3472 | } |
| 3473 | } |
| 3474 | } else { |
| 3475 | // just snooze the one notification |
| 3476 | snoozeNotificationLocked(r); |
| 3477 | } |
| 3478 | } |
| 3479 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3480 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3481 | void snoozeNotificationLocked(NotificationRecord r) { |
| 3482 | MetricsLogger.action(r.getLogMaker() |
| 3483 | .setCategory(MetricsEvent.NOTIFICATION_SNOOZED) |
| 3484 | .setType(MetricsEvent.TYPE_CLOSE) |
| 3485 | .addTaggedData(MetricsEvent.NOTIFICATION_SNOOZED_CRITERIA, |
| 3486 | mSnoozeCriterionId == null ? 0 : 1)); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 3487 | boolean wasPosted = removeFromNotificationListsLocked(r); |
| 3488 | cancelNotificationLocked(r, false, REASON_SNOOZED, wasPosted); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3489 | updateLightsLocked(); |
| 3490 | if (mSnoozeCriterionId != null) { |
| 3491 | mNotificationAssistants.notifyAssistantSnoozedLocked(r.sbn, mSnoozeCriterionId); |
| 3492 | mSnoozeHelper.snooze(r); |
| 3493 | } else { |
| 3494 | mSnoozeHelper.snooze(r, mDuration); |
| 3495 | } |
| 3496 | savePolicyFile(); |
| 3497 | } |
| 3498 | } |
| 3499 | |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 3500 | protected class EnqueueNotificationRunnable implements Runnable { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3501 | private final NotificationRecord r; |
| 3502 | private final int userId; |
| 3503 | |
| 3504 | EnqueueNotificationRunnable(int userId, NotificationRecord r) { |
| 3505 | this.userId = userId; |
| 3506 | this.r = r; |
| 3507 | }; |
| 3508 | |
| 3509 | @Override |
| 3510 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3511 | synchronized (mNotificationLock) { |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 3512 | mEnqueuedNotifications.add(r); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 3513 | scheduleTimeoutLocked(r); |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 3514 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3515 | final StatusBarNotification n = r.sbn; |
| 3516 | if (DBG) Slog.d(TAG, "EnqueueNotificationRunnable.run for: " + n.getKey()); |
| 3517 | NotificationRecord old = mNotificationsByKey.get(n.getKey()); |
| 3518 | if (old != null) { |
| 3519 | // Retain ranking information from previous record |
| 3520 | r.copyRankingInformation(old); |
| 3521 | } |
| 3522 | |
| 3523 | final int callingUid = n.getUid(); |
| 3524 | final int callingPid = n.getInitialPid(); |
| 3525 | final Notification notification = n.getNotification(); |
| 3526 | final String pkg = n.getPackageName(); |
| 3527 | final int id = n.getId(); |
| 3528 | final String tag = n.getTag(); |
| 3529 | |
| 3530 | // Handle grouped notifications and bail out early if we |
| 3531 | // can to avoid extracting signals. |
| 3532 | handleGroupedNotificationLocked(r, old, callingUid, callingPid); |
| 3533 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3534 | // if this is a group child, unsnooze parent summary |
| 3535 | if (n.isGroup() && notification.isGroupChild()) { |
| 3536 | mSnoozeHelper.repostGroupSummary(pkg, r.getUserId(), n.getGroupKey()); |
| 3537 | } |
| 3538 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3539 | // This conditional is a dirty hack to limit the logging done on |
| 3540 | // behalf of the download manager without affecting other apps. |
| 3541 | if (!pkg.equals("com.android.providers.downloads") |
| 3542 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| 3543 | int enqueueStatus = EVENTLOG_ENQUEUE_STATUS_NEW; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3544 | if (old != null) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3545 | enqueueStatus = EVENTLOG_ENQUEUE_STATUS_UPDATE; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3546 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3547 | EventLogTags.writeNotificationEnqueue(callingUid, callingPid, |
| 3548 | pkg, id, tag, userId, notification.toString(), |
| 3549 | enqueueStatus); |
| 3550 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3551 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3552 | mRankingHelper.extractSignals(r); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3553 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3554 | // tell the assistant service about the notification |
| 3555 | if (mNotificationAssistants.isEnabled()) { |
| 3556 | mNotificationAssistants.onNotificationEnqueued(r); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3557 | mHandler.postDelayed(new PostNotificationRunnable(r.getKey()), |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3558 | DELAY_FOR_ASSISTANT_TIME); |
| 3559 | } else { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3560 | mHandler.post(new PostNotificationRunnable(r.getKey())); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3561 | } |
| 3562 | } |
| 3563 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | protected class PostNotificationRunnable implements Runnable { |
| 3567 | private final String key; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3568 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3569 | PostNotificationRunnable(String key) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3570 | this.key = key; |
| 3571 | } |
| 3572 | |
| 3573 | @Override |
| 3574 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3575 | synchronized (mNotificationLock) { |
| 3576 | try { |
| 3577 | NotificationRecord r = null; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3578 | int N = mEnqueuedNotifications.size(); |
| 3579 | for (int i = 0; i < N; i++) { |
| 3580 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 3581 | if (Objects.equals(key, enqueued.getKey())) { |
| 3582 | r = enqueued; |
| 3583 | break; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3584 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3585 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3586 | if (r == null) { |
| 3587 | Slog.i(TAG, "Cannot find enqueued record for key: " + key); |
| 3588 | return; |
| 3589 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3590 | NotificationRecord old = mNotificationsByKey.get(key); |
| 3591 | final StatusBarNotification n = r.sbn; |
| 3592 | final Notification notification = n.getNotification(); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3593 | int index = indexOfNotificationLocked(n.getKey()); |
| 3594 | if (index < 0) { |
| 3595 | mNotificationList.add(r); |
| 3596 | mUsageStats.registerPostedByApp(r); |
| 3597 | } else { |
| 3598 | old = mNotificationList.get(index); |
| 3599 | mNotificationList.set(index, r); |
| 3600 | mUsageStats.registerUpdatedByApp(r, old); |
| 3601 | // Make sure we don't lose the foreground service state. |
| 3602 | notification.flags |= |
| 3603 | old.getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE; |
| 3604 | r.isUpdate = true; |
| 3605 | } |
| 3606 | |
| 3607 | mNotificationsByKey.put(n.getKey(), r); |
| 3608 | |
| 3609 | // Ensure if this is a foreground service that the proper additional |
| 3610 | // flags are set. |
| 3611 | if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 3612 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 3613 | | Notification.FLAG_NO_CLEAR; |
| 3614 | } |
| 3615 | |
| 3616 | applyZenModeLocked(r); |
| 3617 | mRankingHelper.sort(mNotificationList); |
| 3618 | |
| 3619 | if (notification.getSmallIcon() != null) { |
| 3620 | StatusBarNotification oldSbn = (old != null) ? old.sbn : null; |
| 3621 | mListeners.notifyPostedLocked(n, oldSbn); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3622 | mHandler.post(new Runnable() { |
| 3623 | @Override |
| 3624 | public void run() { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3625 | mGroupHelper.onNotificationPosted(n); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3626 | } |
| 3627 | }); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3628 | } else { |
| 3629 | Slog.e(TAG, "Not posting notification without small icon: " + notification); |
| 3630 | if (old != null && !old.isCanceled) { |
| 3631 | mListeners.notifyRemovedLocked(n, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 3632 | NotificationListenerService.REASON_ERROR); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3633 | mHandler.post(new Runnable() { |
| 3634 | @Override |
| 3635 | public void run() { |
| 3636 | mGroupHelper.onNotificationRemoved(n); |
| 3637 | } |
| 3638 | }); |
| 3639 | } |
| 3640 | // ATTENTION: in a future release we will bail out here |
| 3641 | // so that we do not play sounds, show lights, etc. for invalid |
| 3642 | // notifications |
| 3643 | Slog.e(TAG, "WARNING: In a future release this will crash the app: " |
| 3644 | + n.getPackageName()); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3645 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3646 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3647 | buzzBeepBlinkLocked(r); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3648 | } finally { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3649 | int N = mEnqueuedNotifications.size(); |
| 3650 | for (int i = 0; i < N; i++) { |
| 3651 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 3652 | if (Objects.equals(key, enqueued.getKey())) { |
| 3653 | mEnqueuedNotifications.remove(i); |
| 3654 | break; |
| 3655 | } |
| 3656 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3657 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3658 | } |
| 3659 | } |
| 3660 | } |
| 3661 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3662 | /** |
| 3663 | * Ensures that grouped notification receive their special treatment. |
| 3664 | * |
| 3665 | * <p>Cancels group children if the new notification causes a group to lose |
| 3666 | * its summary.</p> |
| 3667 | * |
| 3668 | * <p>Updates mSummaryByGroupKey.</p> |
| 3669 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3670 | @GuardedBy("mNotificationLock") |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3671 | private void handleGroupedNotificationLocked(NotificationRecord r, NotificationRecord old, |
| 3672 | int callingUid, int callingPid) { |
| 3673 | StatusBarNotification sbn = r.sbn; |
| 3674 | Notification n = sbn.getNotification(); |
Selim Cinek | 5b03ce9 | 2016-05-18 15:16:58 -0700 | [diff] [blame] | 3675 | if (n.isGroupSummary() && !sbn.isAppGroup()) { |
| 3676 | // notifications without a group shouldn't be a summary, otherwise autobundling can |
| 3677 | // lead to bugs |
| 3678 | n.flags &= ~Notification.FLAG_GROUP_SUMMARY; |
| 3679 | } |
| 3680 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3681 | String group = sbn.getGroupKey(); |
| 3682 | boolean isSummary = n.isGroupSummary(); |
| 3683 | |
| 3684 | Notification oldN = old != null ? old.sbn.getNotification() : null; |
| 3685 | String oldGroup = old != null ? old.sbn.getGroupKey() : null; |
| 3686 | boolean oldIsSummary = old != null && oldN.isGroupSummary(); |
| 3687 | |
| 3688 | if (oldIsSummary) { |
| 3689 | NotificationRecord removedSummary = mSummaryByGroupKey.remove(oldGroup); |
| 3690 | if (removedSummary != old) { |
| 3691 | String removedKey = |
| 3692 | removedSummary != null ? removedSummary.getKey() : "<null>"; |
| 3693 | Slog.w(TAG, "Removed summary didn't match old notification: old=" + old.getKey() + |
| 3694 | ", removed=" + removedKey); |
| 3695 | } |
| 3696 | } |
| 3697 | if (isSummary) { |
| 3698 | mSummaryByGroupKey.put(group, r); |
| 3699 | } |
| 3700 | |
| 3701 | // Clear out group children of the old notification if the update |
| 3702 | // causes the group summary to go away. This happens when the old |
| 3703 | // notification was a summary and the new one isn't, or when the old |
| 3704 | // notification was a summary and its group key changed. |
| 3705 | if (oldIsSummary && (!isSummary || !oldGroup.equals(group))) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3706 | cancelGroupChildrenLocked(old, callingUid, callingPid, null, false /* sendDelete */); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3707 | } |
| 3708 | } |
| 3709 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3710 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3711 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 3712 | void scheduleTimeoutLocked(NotificationRecord record) { |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 3713 | if (record.getNotification().getTimeoutAfter() > 0) { |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 3714 | final PendingIntent pi = PendingIntent.getBroadcast(getContext(), |
| 3715 | REQUEST_CODE_TIMEOUT, |
| 3716 | new Intent(ACTION_NOTIFICATION_TIMEOUT) |
| 3717 | .setData(new Uri.Builder().scheme(SCHEME_TIMEOUT) |
| 3718 | .appendPath(record.getKey()).build()) |
| 3719 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 3720 | .putExtra(EXTRA_KEY, record.getKey()), |
| 3721 | PendingIntent.FLAG_UPDATE_CURRENT); |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 3722 | mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 3723 | SystemClock.elapsedRealtime() + record.getNotification().getTimeoutAfter(), pi); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 3724 | } |
| 3725 | } |
| 3726 | |
| 3727 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3728 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3729 | void buzzBeepBlinkLocked(NotificationRecord record) { |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 3730 | boolean buzz = false; |
| 3731 | boolean beep = false; |
| 3732 | boolean blink = false; |
| 3733 | |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3734 | final Notification notification = record.sbn.getNotification(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3735 | final String key = record.getKey(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3736 | |
| 3737 | // Should this notification make noise, vibe, or use the LED? |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 3738 | final boolean aboveThreshold = |
| 3739 | record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3740 | |
| 3741 | // Remember if this notification already owns the notification channels. |
| 3742 | boolean wasBeep = key != null && key.equals(mSoundNotificationKey); |
| 3743 | boolean wasBuzz = key != null && key.equals(mVibrateNotificationKey); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3744 | // These are set inside the conditional if the notification is allowed to make noise. |
| 3745 | boolean hasValidVibrate = false; |
| 3746 | boolean hasValidSound = false; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3747 | |
Dan Sandler | 8ce1be4 | 2017-08-01 20:55:59 -0400 | [diff] [blame] | 3748 | if (isNotificationForCurrentUser(record)) { |
| 3749 | // If the notification icon will appear in the status bar, AND it hasn't been blocked |
| 3750 | // by do-not-disturb, it should generate an accessibility event |
| 3751 | if (!record.isUpdate |
| 3752 | && !record.isIntercepted() |
| 3753 | && record.getImportance() > IMPORTANCE_MIN) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3754 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3755 | } |
Dan Sandler | 8ce1be4 | 2017-08-01 20:55:59 -0400 | [diff] [blame] | 3756 | if (aboveThreshold && mSystemReady && mAudioManager != null) { |
| 3757 | // this notification wants to make noise & is allowed to make noise |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3758 | Uri soundUri = record.getSound(); |
| 3759 | hasValidSound = soundUri != null && !Uri.EMPTY.equals(soundUri); |
Julia Reynolds | 5f8e0b88 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 3760 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3761 | long[] vibration = record.getVibration(); |
| 3762 | // Demote sound to vibration if vibration missing & phone in vibration mode. |
| 3763 | if (vibration == null |
| 3764 | && hasValidSound |
| 3765 | && (mAudioManager.getRingerModeInternal() |
| 3766 | == AudioManager.RINGER_MODE_VIBRATE)) { |
| 3767 | vibration = mFallbackVibrationPattern; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3768 | } |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3769 | hasValidVibrate = vibration != null; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 3770 | |
Julia Reynolds | 5f8e0b88 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 3771 | boolean hasAudibleAlert = hasValidSound || hasValidVibrate; |
| 3772 | |
| 3773 | if (hasAudibleAlert && !shouldMuteNotificationLocked(record)) { |
| 3774 | if (DBG) Slog.v(TAG, "Interrupting!"); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3775 | if (hasValidSound) { |
| 3776 | mSoundNotificationKey = key; |
| 3777 | if (mInCall) { |
| 3778 | playInCallNotification(); |
| 3779 | beep = true; |
| 3780 | } else { |
| 3781 | beep = playSound(record, soundUri); |
| 3782 | } |
| 3783 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3784 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3785 | final boolean ringerModeSilent = |
| 3786 | mAudioManager.getRingerModeInternal() |
| 3787 | == AudioManager.RINGER_MODE_SILENT; |
| 3788 | if (!mInCall && hasValidVibrate && !ringerModeSilent) { |
| 3789 | mVibrateNotificationKey = key; |
| 3790 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 3791 | buzz = playVibration(record, vibration, hasValidSound); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3792 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3793 | } |
| 3794 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3795 | } |
| 3796 | // If a notification is updated to remove the actively playing sound or vibrate, |
| 3797 | // cancel that feedback now |
| 3798 | if (wasBeep && !hasValidSound) { |
| 3799 | clearSoundLocked(); |
| 3800 | } |
| 3801 | if (wasBuzz && !hasValidVibrate) { |
| 3802 | clearVibrateLocked(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | // light |
| 3806 | // release the light |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3807 | boolean wasShowLights = mLights.remove(key); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 3808 | if (record.getLight() != null && aboveThreshold |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 3809 | && ((record.getSuppressedVisualEffects() |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 3810 | & NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF) == 0)) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3811 | mLights.add(key); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3812 | updateLightsLocked(); |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 3813 | if (mUseAttentionLight) { |
| 3814 | mAttentionLight.pulse(); |
| 3815 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 3816 | blink = true; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3817 | } else if (wasShowLights) { |
| 3818 | updateLightsLocked(); |
| 3819 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 3820 | if (buzz || beep || blink) { |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 3821 | MetricsLogger.action(record.getLogMaker() |
| 3822 | .setCategory(MetricsEvent.NOTIFICATION_ALERT) |
| 3823 | .setType(MetricsEvent.TYPE_OPEN) |
| 3824 | .setSubtype((buzz ? 1 : 0) | (beep ? 2 : 0) | (blink ? 4 : 0))); |
| 3825 | EventLogTags.writeNotificationAlert(key, buzz ? 1 : 0, beep ? 1 : 0, blink ? 1 : 0); |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 3826 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3827 | } |
| 3828 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3829 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 3830 | boolean shouldMuteNotificationLocked(final NotificationRecord record) { |
Julia Reynolds | 5f8e0b88 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 3831 | // Suppressed because it's a silent update |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 3832 | final Notification notification = record.getNotification(); |
| 3833 | if(record.isUpdate |
| 3834 | && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) { |
| 3835 | return true; |
| 3836 | } |
Julia Reynolds | 5f8e0b88 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 3837 | |
Julia Reynolds | 5f8e0b88 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 3838 | // muted by listener |
| 3839 | final String disableEffects = disableNotificationEffects(record); |
| 3840 | if (disableEffects != null) { |
| 3841 | ZenLog.traceDisableEffects(record, disableEffects); |
| 3842 | return true; |
| 3843 | } |
| 3844 | |
| 3845 | // suppressed due to DND |
| 3846 | if (record.isIntercepted()) { |
| 3847 | return true; |
| 3848 | } |
| 3849 | |
| 3850 | // Suppressed because another notification in its group handles alerting |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 3851 | if (record.sbn.isGroup()) { |
Julia Reynolds | 3020315 | 2017-05-26 13:36:31 -0400 | [diff] [blame] | 3852 | return notification.suppressAlertingDueToGrouping(); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 3853 | } |
Julia Reynolds | 5f8e0b88 | 2017-06-19 08:16:04 -0400 | [diff] [blame] | 3854 | |
Julia Reynolds | 65b85cf | 2017-07-20 09:19:20 -0400 | [diff] [blame] | 3855 | // Suppressed for being too recently noisy |
| 3856 | final String pkg = record.sbn.getPackageName(); |
| 3857 | if (mUsageStats.isAlertRateLimited(pkg)) { |
| 3858 | Slog.e(TAG, "Muting recently noisy " + record.getKey()); |
| 3859 | return true; |
| 3860 | } |
| 3861 | |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 3862 | return false; |
| 3863 | } |
| 3864 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 3865 | private boolean playSound(final NotificationRecord record, Uri soundUri) { |
| 3866 | boolean looping = (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 3867 | // 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] | 3868 | // or the device is in vibrate mode |
| 3869 | if (!mAudioManager.isAudioFocusExclusive() && mAudioManager.getRingerModeInternal() |
| 3870 | != AudioManager.RINGER_MODE_VIBRATE) { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 3871 | final long identity = Binder.clearCallingIdentity(); |
| 3872 | try { |
| 3873 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 3874 | if (player != null) { |
| 3875 | if (DBG) Slog.v(TAG, "Playing sound " + soundUri |
| 3876 | + " with attributes " + record.getAudioAttributes()); |
| 3877 | player.playAsync(soundUri, record.sbn.getUser(), looping, |
| 3878 | record.getAudioAttributes()); |
| 3879 | return true; |
| 3880 | } |
| 3881 | } catch (RemoteException e) { |
| 3882 | } finally { |
| 3883 | Binder.restoreCallingIdentity(identity); |
| 3884 | } |
| 3885 | } |
| 3886 | return false; |
| 3887 | } |
| 3888 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 3889 | private boolean playVibration(final NotificationRecord record, long[] vibration, |
| 3890 | boolean delayVibForSound) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 3891 | // Escalate privileges so we can use the vibrator even if the |
| 3892 | // notifying app does not have the VIBRATE permission. |
| 3893 | long identity = Binder.clearCallingIdentity(); |
| 3894 | try { |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 3895 | final VibrationEffect effect; |
| 3896 | try { |
| 3897 | final boolean insistent = |
| 3898 | (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 3899 | effect = VibrationEffect.createWaveform( |
| 3900 | vibration, insistent ? 0 : -1 /*repeatIndex*/); |
| 3901 | } catch (IllegalArgumentException e) { |
| 3902 | Slog.e(TAG, "Error creating vibration waveform with pattern: " + |
| 3903 | Arrays.toString(vibration)); |
| 3904 | return false; |
| 3905 | } |
| 3906 | if (delayVibForSound) { |
| 3907 | new Thread(() -> { |
| 3908 | // delay the vibration by the same amount as the notification sound |
| 3909 | final int waitMs = mAudioManager.getFocusRampTimeMs( |
| 3910 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 3911 | record.getAudioAttributes()); |
| 3912 | if (DBG) Slog.v(TAG, "Delaying vibration by " + waitMs + "ms"); |
| 3913 | try { |
| 3914 | Thread.sleep(waitMs); |
| 3915 | } catch (InterruptedException e) { } |
| 3916 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 3917 | effect, record.getAudioAttributes()); |
| 3918 | }).start(); |
| 3919 | } else { |
| 3920 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 3921 | effect, record.getAudioAttributes()); |
| 3922 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 3923 | return true; |
| 3924 | } finally{ |
| 3925 | Binder.restoreCallingIdentity(identity); |
| 3926 | } |
| 3927 | } |
| 3928 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3929 | private boolean isNotificationForCurrentUser(NotificationRecord record) { |
| 3930 | final int currentUser; |
| 3931 | final long token = Binder.clearCallingIdentity(); |
| 3932 | try { |
| 3933 | currentUser = ActivityManager.getCurrentUser(); |
| 3934 | } finally { |
| 3935 | Binder.restoreCallingIdentity(token); |
| 3936 | } |
| 3937 | return (record.getUserId() == UserHandle.USER_ALL || |
| 3938 | record.getUserId() == currentUser || |
| 3939 | mUserProfiles.isCurrentProfile(record.getUserId())); |
| 3940 | } |
| 3941 | |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 3942 | private void playInCallNotification() { |
| 3943 | new Thread() { |
| 3944 | @Override |
| 3945 | public void run() { |
| 3946 | // If toneGenerator creation fails, just continue the call |
| 3947 | // without playing the notification sound. |
| 3948 | try { |
| 3949 | synchronized (mInCallToneGeneratorLock) { |
| 3950 | if (mInCallToneGenerator != null) { |
| 3951 | // limit this tone to 1 second; BEEP2 should in fact be much shorter |
| 3952 | mInCallToneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP2, 1000); |
| 3953 | } |
| 3954 | } |
| 3955 | } catch (RuntimeException e) { |
| 3956 | Log.w(TAG, "Exception from ToneGenerator: " + e); |
| 3957 | } |
| 3958 | } |
| 3959 | }.start(); |
| 3960 | } |
| 3961 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3962 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3963 | void showNextToastLocked() { |
| 3964 | ToastRecord record = mToastQueue.get(0); |
| 3965 | while (record != null) { |
| 3966 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| 3967 | try { |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 3968 | record.callback.show(record.token); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3969 | scheduleTimeoutLocked(record); |
| 3970 | return; |
| 3971 | } catch (RemoteException e) { |
| 3972 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| 3973 | + " in package " + record.pkg); |
| 3974 | // remove it from the list and let the process die |
| 3975 | int index = mToastQueue.indexOf(record); |
| 3976 | if (index >= 0) { |
| 3977 | mToastQueue.remove(index); |
| 3978 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 3979 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3980 | if (mToastQueue.size() > 0) { |
| 3981 | record = mToastQueue.get(0); |
| 3982 | } else { |
| 3983 | record = null; |
| 3984 | } |
| 3985 | } |
| 3986 | } |
| 3987 | } |
| 3988 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3989 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3990 | void cancelToastLocked(int index) { |
| 3991 | ToastRecord record = mToastQueue.get(index); |
| 3992 | try { |
| 3993 | record.callback.hide(); |
| 3994 | } catch (RemoteException e) { |
| 3995 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| 3996 | + " in package " + record.pkg); |
| 3997 | // don't worry about this, we're about to remove it from |
| 3998 | // the list anyway |
| 3999 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4000 | |
| 4001 | ToastRecord lastToast = mToastQueue.remove(index); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 4002 | mWindowManagerInternal.removeWindowToken(lastToast.token, true, DEFAULT_DISPLAY); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4003 | |
| 4004 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4005 | if (mToastQueue.size() > 0) { |
| 4006 | // Show the next one. If the callback fails, this will remove |
| 4007 | // it from the list, so don't assume that the list hasn't changed |
| 4008 | // after this point. |
| 4009 | showNextToastLocked(); |
| 4010 | } |
| 4011 | } |
| 4012 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4013 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4014 | private void scheduleTimeoutLocked(ToastRecord r) |
| 4015 | { |
| 4016 | mHandler.removeCallbacksAndMessages(r); |
| 4017 | Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r); |
| 4018 | long delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY; |
| 4019 | mHandler.sendMessageDelayed(m, delay); |
| 4020 | } |
| 4021 | |
| 4022 | private void handleTimeout(ToastRecord record) |
| 4023 | { |
| 4024 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| 4025 | synchronized (mToastQueue) { |
| 4026 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 4027 | if (index >= 0) { |
| 4028 | cancelToastLocked(index); |
| 4029 | } |
| 4030 | } |
| 4031 | } |
| 4032 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4033 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4034 | int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 4035 | { |
| 4036 | IBinder cbak = callback.asBinder(); |
| 4037 | ArrayList<ToastRecord> list = mToastQueue; |
| 4038 | int len = list.size(); |
| 4039 | for (int i=0; i<len; i++) { |
| 4040 | ToastRecord r = list.get(i); |
| 4041 | if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) { |
| 4042 | return i; |
| 4043 | } |
| 4044 | } |
| 4045 | return -1; |
| 4046 | } |
| 4047 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4048 | @GuardedBy("mToastQueue") |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 4049 | void keepProcessAliveIfNeededLocked(int pid) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4050 | { |
| 4051 | int toastCount = 0; // toasts from this pid |
| 4052 | ArrayList<ToastRecord> list = mToastQueue; |
| 4053 | int N = list.size(); |
| 4054 | for (int i=0; i<N; i++) { |
| 4055 | ToastRecord r = list.get(i); |
| 4056 | if (r.pid == pid) { |
| 4057 | toastCount++; |
| 4058 | } |
| 4059 | } |
| 4060 | try { |
Dianne Hackborn | f965f40 | 2017-05-04 23:27:23 -0700 | [diff] [blame] | 4061 | mAm.setProcessImportant(mForegroundToken, pid, toastCount > 0, "toast"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4062 | } catch (RemoteException e) { |
| 4063 | // Shouldn't happen. |
| 4064 | } |
| 4065 | } |
| 4066 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4067 | private void handleRankingReconsideration(Message message) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4068 | if (!(message.obj instanceof RankingReconsideration)) return; |
| 4069 | RankingReconsideration recon = (RankingReconsideration) message.obj; |
| 4070 | recon.run(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4071 | boolean changed; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4072 | synchronized (mNotificationLock) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4073 | final NotificationRecord record = mNotificationsByKey.get(recon.getKey()); |
| 4074 | if (record == null) { |
| 4075 | return; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4076 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4077 | int indexBefore = findNotificationRecordIndexLocked(record); |
| 4078 | boolean interceptBefore = record.isIntercepted(); |
Julia Reynolds | af4dc28 | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4079 | float contactAffinityBefore = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4080 | int visibilityBefore = record.getPackageVisibilityOverride(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4081 | recon.applyChangesLocked(record); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4082 | applyZenModeLocked(record); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4083 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4084 | int indexAfter = findNotificationRecordIndexLocked(record); |
| 4085 | boolean interceptAfter = record.isIntercepted(); |
Julia Reynolds | af4dc28 | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4086 | float contactAffinityAfter = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4087 | int visibilityAfter = record.getPackageVisibilityOverride(); |
| 4088 | changed = indexBefore != indexAfter || interceptBefore != interceptAfter |
| 4089 | || visibilityBefore != visibilityAfter; |
Julia Reynolds | af4dc28 | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4090 | if (interceptBefore && !interceptAfter |
| 4091 | && Float.compare(contactAffinityBefore, contactAffinityAfter) != 0) { |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4092 | buzzBeepBlinkLocked(record); |
| 4093 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4094 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4095 | if (changed) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4096 | scheduleSendRankingUpdate(); |
| 4097 | } |
| 4098 | } |
| 4099 | |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4100 | private void handleRankingSort(Message msg) { |
| 4101 | if (!(msg.obj instanceof Boolean)) return; |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 4102 | if (mRankingHelper == null) return; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4103 | boolean forceUpdate = ((Boolean) msg.obj == null) ? false : (boolean) msg.obj; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4104 | synchronized (mNotificationLock) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4105 | final int N = mNotificationList.size(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4106 | // Any field that can change via one of the extractors or by the assistant |
| 4107 | // needs to be added here. |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4108 | ArrayList<String> orderBefore = new ArrayList<String>(N); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4109 | ArrayList<String> groupOverrideBefore = new ArrayList<>(N); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4110 | int[] visibilities = new int[N]; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4111 | boolean[] showBadges = new boolean[N]; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4112 | for (int i = 0; i < N; i++) { |
| 4113 | final NotificationRecord r = mNotificationList.get(i); |
| 4114 | orderBefore.add(r.getKey()); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4115 | groupOverrideBefore.add(r.sbn.getGroupKey()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4116 | visibilities[i] = r.getPackageVisibilityOverride(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4117 | showBadges[i] = r.canShowBadge(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4118 | mRankingHelper.extractSignals(r); |
| 4119 | } |
Chris Wren | 19a02b0 | 2015-12-22 10:34:22 -0500 | [diff] [blame] | 4120 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4121 | for (int i = 0; i < N; i++) { |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4122 | final NotificationRecord r = mNotificationList.get(i); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4123 | if (forceUpdate |
| 4124 | || !orderBefore.get(i).equals(r.getKey()) |
Julia Reynolds | 6976669 | 2016-02-01 15:35:08 -0500 | [diff] [blame] | 4125 | || visibilities[i] != r.getPackageVisibilityOverride() |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4126 | || !groupOverrideBefore.get(i).equals(r.sbn.getGroupKey()) |
| 4127 | || showBadges[i] != r.canShowBadge()) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4128 | scheduleSendRankingUpdate(); |
| 4129 | return; |
| 4130 | } |
| 4131 | } |
| 4132 | } |
| 4133 | } |
| 4134 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4135 | @GuardedBy("mNotificationLock") |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 4136 | private void recordCallerLocked(NotificationRecord record) { |
| 4137 | if (mZenModeHelper.isCall(record)) { |
| 4138 | mZenModeHelper.recordCaller(record); |
| 4139 | } |
| 4140 | } |
| 4141 | |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 4142 | // let zen mode evaluate this record |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4143 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4144 | private void applyZenModeLocked(NotificationRecord record) { |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 4145 | record.setIntercepted(mZenModeHelper.shouldIntercept(record)); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4146 | if (record.isIntercepted()) { |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 4147 | int suppressed = (mZenModeHelper.shouldSuppressWhenScreenOff() |
| 4148 | ? SUPPRESSED_EFFECT_SCREEN_OFF : 0) |
| 4149 | | (mZenModeHelper.shouldSuppressWhenScreenOn() |
| 4150 | ? SUPPRESSED_EFFECT_SCREEN_ON : 0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4151 | record.setSuppressedVisualEffects(suppressed); |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 4152 | } else { |
| 4153 | record.setSuppressedVisualEffects(0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4154 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4155 | } |
| 4156 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4157 | @GuardedBy("mNotificationLock") |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4158 | private int findNotificationRecordIndexLocked(NotificationRecord target) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4159 | return mRankingHelper.indexOf(mNotificationList, target); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | private void scheduleSendRankingUpdate() { |
Chris Wren | 5202049 | 2016-04-06 11:12:02 -0400 | [diff] [blame] | 4163 | if (!mHandler.hasMessages(MESSAGE_SEND_RANKING_UPDATE)) { |
| 4164 | Message m = Message.obtain(mHandler, MESSAGE_SEND_RANKING_UPDATE); |
| 4165 | mHandler.sendMessage(m); |
| 4166 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | private void handleSendRankingUpdate() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4170 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4171 | mListeners.notifyRankingUpdateLocked(); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4172 | } |
| 4173 | } |
| 4174 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4175 | private void scheduleListenerHintsChanged(int state) { |
| 4176 | mHandler.removeMessages(MESSAGE_LISTENER_HINTS_CHANGED); |
| 4177 | mHandler.obtainMessage(MESSAGE_LISTENER_HINTS_CHANGED, state, 0).sendToTarget(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4178 | } |
| 4179 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4180 | private void scheduleInterruptionFilterChanged(int listenerInterruptionFilter) { |
| 4181 | mHandler.removeMessages(MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED); |
| 4182 | mHandler.obtainMessage( |
| 4183 | MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED, |
| 4184 | listenerInterruptionFilter, |
| 4185 | 0).sendToTarget(); |
| 4186 | } |
| 4187 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4188 | private void handleListenerHintsChanged(int hints) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4189 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4190 | mListeners.notifyListenerHintsChangedLocked(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4191 | } |
| 4192 | } |
| 4193 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4194 | private void handleListenerInterruptionFilterChanged(int interruptionFilter) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4195 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4196 | mListeners.notifyInterruptionFilterChanged(interruptionFilter); |
| 4197 | } |
| 4198 | } |
| 4199 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4200 | private final class WorkerHandler extends Handler |
| 4201 | { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4202 | public WorkerHandler(Looper looper) { |
| 4203 | super(looper); |
| 4204 | } |
| 4205 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4206 | @Override |
| 4207 | public void handleMessage(Message msg) |
| 4208 | { |
| 4209 | switch (msg.what) |
| 4210 | { |
| 4211 | case MESSAGE_TIMEOUT: |
| 4212 | handleTimeout((ToastRecord)msg.obj); |
| 4213 | break; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 4214 | case MESSAGE_SAVE_POLICY_FILE: |
| 4215 | handleSavePolicyFile(); |
| 4216 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4217 | case MESSAGE_SEND_RANKING_UPDATE: |
| 4218 | handleSendRankingUpdate(); |
| 4219 | break; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4220 | case MESSAGE_LISTENER_HINTS_CHANGED: |
| 4221 | handleListenerHintsChanged(msg.arg1); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4222 | break; |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4223 | case MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED: |
| 4224 | handleListenerInterruptionFilterChanged(msg.arg1); |
| 4225 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4226 | } |
| 4227 | } |
| 4228 | |
| 4229 | } |
| 4230 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4231 | private final class RankingHandlerWorker extends Handler implements RankingHandler |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4232 | { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4233 | public RankingHandlerWorker(Looper looper) { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4234 | super(looper); |
| 4235 | } |
| 4236 | |
| 4237 | @Override |
| 4238 | public void handleMessage(Message msg) { |
| 4239 | switch (msg.what) { |
| 4240 | case MESSAGE_RECONSIDER_RANKING: |
| 4241 | handleRankingReconsideration(msg); |
| 4242 | break; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4243 | case MESSAGE_RANKING_SORT: |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4244 | handleRankingSort(msg); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4245 | break; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4246 | } |
| 4247 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4248 | |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4249 | public void requestSort(boolean forceUpdate) { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4250 | removeMessages(MESSAGE_RANKING_SORT); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4251 | Message msg = Message.obtain(); |
| 4252 | msg.what = MESSAGE_RANKING_SORT; |
| 4253 | msg.obj = forceUpdate; |
| 4254 | sendMessage(msg); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4255 | } |
| 4256 | |
| 4257 | public void requestReconsideration(RankingReconsideration recon) { |
| 4258 | Message m = Message.obtain(this, |
| 4259 | NotificationManagerService.MESSAGE_RECONSIDER_RANKING, recon); |
| 4260 | long delay = recon.getDelay(TimeUnit.MILLISECONDS); |
| 4261 | sendMessageDelayed(m, delay); |
| 4262 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4263 | } |
| 4264 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4265 | // Notifications |
| 4266 | // ============================================================================ |
| 4267 | static int clamp(int x, int low, int high) { |
| 4268 | return (x < low) ? low : ((x > high) ? high : x); |
| 4269 | } |
| 4270 | |
Dan Sandler | 8ce1be4 | 2017-08-01 20:55:59 -0400 | [diff] [blame] | 4271 | @VisibleForTesting |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4272 | void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
Dan Sandler | 8ce1be4 | 2017-08-01 20:55:59 -0400 | [diff] [blame] | 4273 | final AccessibilityManager accessibilityManager |
| 4274 | = AccessibilityManager.getInstance(getContext()); |
| 4275 | if (accessibilityManager == null || !accessibilityManager.isEnabled()) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 4276 | return; |
| 4277 | } |
| 4278 | |
| 4279 | AccessibilityEvent event = |
| 4280 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 4281 | event.setPackageName(packageName); |
| 4282 | event.setClassName(Notification.class.getName()); |
| 4283 | event.setParcelableData(notification); |
| 4284 | CharSequence tickerText = notification.tickerText; |
| 4285 | if (!TextUtils.isEmpty(tickerText)) { |
| 4286 | event.getText().add(tickerText); |
| 4287 | } |
| 4288 | |
Dan Sandler | 8ce1be4 | 2017-08-01 20:55:59 -0400 | [diff] [blame] | 4289 | accessibilityManager.sendAccessibilityEvent(event); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 4290 | } |
| 4291 | |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4292 | /** |
| 4293 | * Removes all NotificationsRecords with the same key as the given notification record |
| 4294 | * from both lists. Do not call this method while iterating over either list. |
| 4295 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4296 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4297 | private boolean removeFromNotificationListsLocked(NotificationRecord r) { |
| 4298 | // Remove from both lists, either list could have a separate Record for what is |
| 4299 | // effectively the same notification. |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4300 | boolean wasPosted = false; |
| 4301 | NotificationRecord recordInList = null; |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4302 | if ((recordInList = findNotificationByListLocked(mNotificationList, r.getKey())) |
| 4303 | != null) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4304 | mNotificationList.remove(recordInList); |
| 4305 | mNotificationsByKey.remove(recordInList.sbn.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4306 | wasPosted = true; |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4307 | } |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4308 | while ((recordInList = findNotificationByListLocked(mEnqueuedNotifications, r.getKey())) |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4309 | != null) { |
| 4310 | mEnqueuedNotifications.remove(recordInList); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4311 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4312 | return wasPosted; |
| 4313 | } |
| 4314 | |
| 4315 | @GuardedBy("mNotificationLock") |
| 4316 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason, |
| 4317 | boolean wasPosted) { |
| 4318 | final String canceledKey = r.getKey(); |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 4319 | |
| 4320 | // Record caller. |
| 4321 | recordCallerLocked(r); |
| 4322 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4323 | // tell the app |
| 4324 | if (sendDelete) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4325 | if (r.getNotification().deleteIntent != null) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4326 | try { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4327 | r.getNotification().deleteIntent.send(); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4328 | } catch (PendingIntent.CanceledException ex) { |
| 4329 | // do nothing - there's no relevant way to recover, and |
| 4330 | // no reason to let this propagate |
Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 4331 | Slog.w(TAG, "canceled PendingIntent for " + r.sbn.getPackageName(), ex); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4332 | } |
| 4333 | } |
| 4334 | } |
| 4335 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4336 | // Only cancel these if this notification actually got to be posted. |
| 4337 | if (wasPosted) { |
| 4338 | // status bar |
| 4339 | if (r.getNotification().getSmallIcon() != null) { |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 4340 | if (reason != REASON_SNOOZED) { |
| 4341 | r.isCanceled = true; |
| 4342 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4343 | mListeners.notifyRemovedLocked(r.sbn, reason); |
| 4344 | mHandler.post(new Runnable() { |
| 4345 | @Override |
| 4346 | public void run() { |
| 4347 | mGroupHelper.onNotificationRemoved(r.sbn); |
| 4348 | } |
| 4349 | }); |
| 4350 | } |
| 4351 | |
| 4352 | // sound |
| 4353 | if (canceledKey.equals(mSoundNotificationKey)) { |
| 4354 | mSoundNotificationKey = null; |
| 4355 | final long identity = Binder.clearCallingIdentity(); |
| 4356 | try { |
| 4357 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4358 | if (player != null) { |
| 4359 | player.stopAsync(); |
| 4360 | } |
| 4361 | } catch (RemoteException e) { |
| 4362 | } finally { |
| 4363 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4364 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4365 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4366 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4367 | // vibrate |
| 4368 | if (canceledKey.equals(mVibrateNotificationKey)) { |
| 4369 | mVibrateNotificationKey = null; |
| 4370 | long identity = Binder.clearCallingIdentity(); |
| 4371 | try { |
| 4372 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 4373 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4374 | finally { |
| 4375 | Binder.restoreCallingIdentity(identity); |
| 4376 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4377 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4378 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4379 | // light |
| 4380 | mLights.remove(canceledKey); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4381 | } |
| 4382 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4383 | // Record usage stats |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4384 | // TODO: add unbundling stats? |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4385 | switch (reason) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 4386 | case REASON_CANCEL: |
| 4387 | case REASON_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4388 | case REASON_LISTENER_CANCEL: |
| 4389 | case REASON_LISTENER_CANCEL_ALL: |
| 4390 | mUsageStats.registerDismissedByUser(r); |
| 4391 | break; |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 4392 | case REASON_APP_CANCEL: |
| 4393 | case REASON_APP_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4394 | mUsageStats.registerRemovedByApp(r); |
| 4395 | break; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4396 | } |
| 4397 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4398 | String groupKey = r.getGroupKey(); |
| 4399 | NotificationRecord groupSummary = mSummaryByGroupKey.get(groupKey); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4400 | if (groupSummary != null && groupSummary.getKey().equals(canceledKey)) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4401 | mSummaryByGroupKey.remove(groupKey); |
| 4402 | } |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 4403 | final ArrayMap<String, String> summaries = mAutobundledSummaries.get(r.sbn.getUserId()); |
| 4404 | if (summaries != null && r.sbn.getKey().equals(summaries.get(r.sbn.getPackageName()))) { |
| 4405 | summaries.remove(r.sbn.getPackageName()); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4406 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 4407 | |
Daniel Sandler | 23d7c70 | 2013-03-07 16:32:06 -0500 | [diff] [blame] | 4408 | // Save it for users of getHistoricalNotifications() |
| 4409 | mArchive.record(r.sbn); |
Christoph Studer | 81e5b5f | 2014-10-22 17:19:56 +0200 | [diff] [blame] | 4410 | |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 4411 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 4412 | MetricsLogger.action(r.getLogMaker(now) |
| 4413 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
| 4414 | .setType(MetricsEvent.TYPE_DISMISS) |
| 4415 | .setSubtype(reason)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 4416 | EventLogTags.writeNotificationCanceled(canceledKey, reason, |
| 4417 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | /** |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 4421 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4422 | * and none of the {@code mustNotHaveFlags}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4423 | */ |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 4424 | void cancelNotification(final int callingUid, final int callingPid, |
| 4425 | final String pkg, final String tag, final int id, |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4426 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4427 | final int userId, final int reason, final ManagedServiceInfo listener) { |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4428 | // In enqueueNotificationInternal notifications are added by scheduling the |
| 4429 | // work on the worker handler. Hence, we also schedule the cancel on this |
| 4430 | // handler to avoid a scenario where an add notification call followed by a |
| 4431 | // remove notification call ends up in not removing the notification. |
| 4432 | mHandler.post(new Runnable() { |
| 4433 | @Override |
| 4434 | public void run() { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4435 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Chris Wren | bddb5bc | 2015-03-04 08:47:46 -0800 | [diff] [blame] | 4436 | if (DBG) EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, id, tag, |
| 4437 | userId, mustHaveFlags, mustNotHaveFlags, reason, listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4438 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4439 | synchronized (mNotificationLock) { |
| 4440 | // Look for the notification, searching both the posted and enqueued lists. |
| 4441 | NotificationRecord r = findNotificationLocked(pkg, tag, id, userId); |
| 4442 | if (r != null) { |
| 4443 | // The notification was found, check if it should be removed. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4444 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4445 | // Ideally we'd do this in the caller of this method. However, that would |
| 4446 | // require the caller to also find the notification. |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 4447 | if (reason == REASON_CLICK) { |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4448 | mUsageStats.registerClickedByUser(r); |
| 4449 | } |
| 4450 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4451 | if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) { |
| 4452 | return; |
| 4453 | } |
| 4454 | if ((r.getNotification().flags & mustNotHaveFlags) != 0) { |
| 4455 | return; |
| 4456 | } |
| 4457 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4458 | // Cancel the notification. |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4459 | boolean wasPosted = removeFromNotificationListsLocked(r); |
| 4460 | cancelNotificationLocked(r, sendDelete, reason, wasPosted); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4461 | cancelGroupChildrenLocked(r, callingUid, callingPid, listenerName, |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4462 | sendDelete); |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4463 | updateLightsLocked(); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 4464 | } else { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4465 | // No notification was found, assume that it is snoozed and cancel it. |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 4466 | if (reason != REASON_SNOOZED) { |
| 4467 | final boolean wasSnoozed = mSnoozeHelper.cancel(userId, pkg, tag, id); |
| 4468 | if (wasSnoozed) { |
| 4469 | savePolicyFile(); |
| 4470 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 4471 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4472 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4473 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4474 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4475 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4476 | } |
| 4477 | |
| 4478 | /** |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 4479 | * Determine whether the userId applies to the notification in question, either because |
| 4480 | * they match exactly, or one of them is USER_ALL (which is treated as a wildcard). |
| 4481 | */ |
| 4482 | private boolean notificationMatchesUserId(NotificationRecord r, int userId) { |
| 4483 | return |
| 4484 | // looking for USER_ALL notifications? match everything |
| 4485 | userId == UserHandle.USER_ALL |
| 4486 | // a notification sent to USER_ALL matches any query |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4487 | || r.getUserId() == UserHandle.USER_ALL |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 4488 | // an exact user match |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4489 | || r.getUserId() == userId; |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | /** |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4493 | * Determine whether the userId applies to the notification in question, either because |
| 4494 | * 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] | 4495 | * because it matches one of the users profiles. |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4496 | */ |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 4497 | private boolean notificationMatchesCurrentProfiles(NotificationRecord r, int userId) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4498 | return notificationMatchesUserId(r, userId) |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 4499 | || mUserProfiles.isCurrentProfile(r.getUserId()); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4500 | } |
| 4501 | |
| 4502 | /** |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 4503 | * 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] | 4504 | * {@code mustHaveFlags}. |
| 4505 | */ |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4506 | void cancelAllNotificationsInt(int callingUid, int callingPid, String pkg, String channelId, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 4507 | int mustHaveFlags, int mustNotHaveFlags, boolean doit, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4508 | ManagedServiceInfo listener) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4509 | mHandler.post(new Runnable() { |
| 4510 | @Override |
| 4511 | public void run() { |
| 4512 | String listenerName = listener == null ? null : listener.component.toShortString(); |
| 4513 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 4514 | pkg, userId, mustHaveFlags, mustNotHaveFlags, reason, |
| 4515 | listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4516 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4517 | // Why does this parameter exist? Do we actually want to execute the above if doit |
| 4518 | // is false? |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4519 | if (!doit) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4520 | return; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4521 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4522 | |
| 4523 | synchronized (mNotificationLock) { |
| 4524 | FlagChecker flagChecker = (int flags) -> { |
| 4525 | if ((flags & mustHaveFlags) != mustHaveFlags) { |
| 4526 | return false; |
| 4527 | } |
| 4528 | if ((flags & mustNotHaveFlags) != 0) { |
| 4529 | return false; |
| 4530 | } |
| 4531 | return true; |
| 4532 | }; |
| 4533 | |
| 4534 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 4535 | pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, flagChecker, |
| 4536 | false /*includeCurrentProfiles*/, userId, false /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4537 | listenerName, true /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4538 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 4539 | callingPid, pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, |
| 4540 | flagChecker, false /*includeCurrentProfiles*/, userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4541 | false /*sendDelete*/, reason, listenerName, false /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4542 | mSnoozeHelper.cancel(userId, pkg); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4543 | } |
| 4544 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4545 | }); |
| 4546 | } |
| 4547 | |
| 4548 | private interface FlagChecker { |
| 4549 | // Returns false if these flags do not pass the defined flag test. |
| 4550 | public boolean apply(int flags); |
| 4551 | } |
| 4552 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4553 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4554 | private void cancelAllNotificationsByListLocked(ArrayList<NotificationRecord> notificationList, |
| 4555 | int callingUid, int callingPid, String pkg, boolean nullPkgIndicatesUserSwitch, |
| 4556 | String channelId, FlagChecker flagChecker, boolean includeCurrentProfiles, int userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4557 | boolean sendDelete, int reason, String listenerName, boolean wasPosted) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4558 | ArrayList<NotificationRecord> canceledNotifications = null; |
| 4559 | for (int i = notificationList.size() - 1; i >= 0; --i) { |
| 4560 | NotificationRecord r = notificationList.get(i); |
| 4561 | if (includeCurrentProfiles) { |
| 4562 | if (!notificationMatchesCurrentProfiles(r, userId)) { |
| 4563 | continue; |
| 4564 | } |
| 4565 | } else if (!notificationMatchesUserId(r, userId)) { |
| 4566 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4567 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4568 | // Don't remove notifications to all, if there's no package name specified |
| 4569 | if (nullPkgIndicatesUserSwitch && pkg == null && r.getUserId() == UserHandle.USER_ALL) { |
| 4570 | continue; |
| 4571 | } |
| 4572 | if (!flagChecker.apply(r.getFlags())) { |
| 4573 | continue; |
| 4574 | } |
| 4575 | if (pkg != null && !r.sbn.getPackageName().equals(pkg)) { |
| 4576 | continue; |
| 4577 | } |
| 4578 | if (channelId != null && !channelId.equals(r.getChannel().getId())) { |
| 4579 | continue; |
| 4580 | } |
| 4581 | |
| 4582 | if (canceledNotifications == null) { |
| 4583 | canceledNotifications = new ArrayList<>(); |
| 4584 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4585 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 4586 | mNotificationsByKey.remove(r.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4587 | canceledNotifications.add(r); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4588 | cancelNotificationLocked(r, sendDelete, reason, wasPosted); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4589 | } |
| 4590 | if (canceledNotifications != null) { |
| 4591 | final int M = canceledNotifications.size(); |
| 4592 | for (int i = 0; i < M; i++) { |
| 4593 | cancelGroupChildrenLocked(canceledNotifications.get(i), callingUid, callingPid, |
| 4594 | listenerName, false /* sendDelete */); |
| 4595 | } |
| 4596 | updateLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4597 | } |
| 4598 | } |
| 4599 | |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 4600 | void snoozeNotificationInt(String key, long duration, String snoozeCriterionId, |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4601 | ManagedServiceInfo listener) { |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 4602 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 4603 | if (duration <= 0 && snoozeCriterionId == null || key == null) { |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 4604 | return; |
| 4605 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4606 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 4607 | if (DBG) { |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 4608 | Slog.d(TAG, String.format("snooze event(%s, %d, %s, %s)", key, duration, |
| 4609 | snoozeCriterionId, listenerName)); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 4610 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4611 | // Needs to post so that it can cancel notifications not yet enqueued. |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4612 | mHandler.post(new SnoozeNotificationRunnable(key, duration, snoozeCriterionId)); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 4613 | } |
| 4614 | |
| 4615 | void unsnoozeNotificationInt(String key, ManagedServiceInfo listener) { |
| 4616 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 4617 | if (DBG) { |
| 4618 | Slog.d(TAG, String.format("unsnooze event(%s, %s)", key, listenerName)); |
| 4619 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 4620 | mSnoozeHelper.repost(key); |
| 4621 | savePolicyFile(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 4622 | } |
| 4623 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4624 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 350159c | 2014-03-27 11:15:11 -0700 | [diff] [blame] | 4625 | void cancelAllLocked(int callingUid, int callingPid, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4626 | ManagedServiceInfo listener, boolean includeCurrentProfiles) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4627 | mHandler.post(new Runnable() { |
| 4628 | @Override |
| 4629 | public void run() { |
| 4630 | synchronized (mNotificationLock) { |
| 4631 | String listenerName = |
| 4632 | listener == null ? null : listener.component.toShortString(); |
| 4633 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 4634 | null, userId, 0, 0, reason, listenerName); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4635 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4636 | FlagChecker flagChecker = (int flags) -> { |
| 4637 | if ((flags & (Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR)) |
| 4638 | != 0) { |
| 4639 | return false; |
| 4640 | } |
| 4641 | return true; |
| 4642 | }; |
| 4643 | |
| 4644 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 4645 | null, false /*nullPkgIndicatesUserSwitch*/, null, flagChecker, |
| 4646 | includeCurrentProfiles, userId, true /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4647 | listenerName, true); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4648 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 4649 | callingPid, null, false /*nullPkgIndicatesUserSwitch*/, null, |
| 4650 | flagChecker, includeCurrentProfiles, userId, true /*sendDelete*/, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4651 | reason, listenerName, false); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4652 | mSnoozeHelper.cancel(userId, includeCurrentProfiles); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4653 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4654 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4655 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4656 | } |
| 4657 | |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4658 | // Warning: The caller is responsible for invoking updateLightsLocked(). |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4659 | @GuardedBy("mNotificationLock") |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4660 | private void cancelGroupChildrenLocked(NotificationRecord r, int callingUid, int callingPid, |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4661 | String listenerName, boolean sendDelete) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4662 | Notification n = r.getNotification(); |
Christoph Studer | 3f31f5d | 2014-07-31 16:55:32 +0200 | [diff] [blame] | 4663 | if (!n.isGroupSummary()) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4664 | return; |
| 4665 | } |
| 4666 | |
| 4667 | String pkg = r.sbn.getPackageName(); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4668 | |
| 4669 | if (pkg == null) { |
| 4670 | if (DBG) Log.e(TAG, "No package for group summary: " + r.getKey()); |
| 4671 | return; |
| 4672 | } |
| 4673 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4674 | cancelGroupChildrenByListLocked(mNotificationList, r, callingUid, callingPid, listenerName, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4675 | sendDelete, true); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4676 | cancelGroupChildrenByListLocked(mEnqueuedNotifications, r, callingUid, callingPid, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4677 | listenerName, sendDelete, false); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4678 | } |
| 4679 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4680 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4681 | private void cancelGroupChildrenByListLocked(ArrayList<NotificationRecord> notificationList, |
| 4682 | NotificationRecord parentNotification, int callingUid, int callingPid, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4683 | String listenerName, boolean sendDelete, boolean wasPosted) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4684 | final String pkg = parentNotification.sbn.getPackageName(); |
| 4685 | final int userId = parentNotification.getUserId(); |
| 4686 | final int reason = REASON_GROUP_SUMMARY_CANCELED; |
| 4687 | for (int i = notificationList.size() - 1; i >= 0; i--) { |
| 4688 | final NotificationRecord childR = notificationList.get(i); |
| 4689 | final StatusBarNotification childSbn = childR.sbn; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4690 | if ((childSbn.isGroup() && !childSbn.getNotification().isGroupSummary()) && |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4691 | childR.getGroupKey().equals(parentNotification.getGroupKey()) |
Selim Cinek | 1d35979 | 2017-01-13 14:43:43 -0800 | [diff] [blame] | 4692 | && (childR.getFlags() & Notification.FLAG_FOREGROUND_SERVICE) == 0) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4693 | EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, childSbn.getId(), |
| 4694 | childSbn.getTag(), userId, 0, 0, reason, listenerName); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4695 | notificationList.remove(i); |
Julia Reynolds | 080361e | 2017-07-13 11:23:12 -0400 | [diff] [blame] | 4696 | mNotificationsByKey.remove(childR.getKey()); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4697 | cancelNotificationLocked(childR, sendDelete, reason, wasPosted); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4698 | } |
| 4699 | } |
| 4700 | } |
| 4701 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4702 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4703 | void updateLightsLocked() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4704 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4705 | // handle notification lights |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 4706 | NotificationRecord ledNotification = null; |
| 4707 | while (ledNotification == null && !mLights.isEmpty()) { |
| 4708 | final String owner = mLights.get(mLights.size() - 1); |
| 4709 | ledNotification = mNotificationsByKey.get(owner); |
| 4710 | if (ledNotification == null) { |
| 4711 | Slog.wtfStack(TAG, "LED Notification does not exist: " + owner); |
| 4712 | mLights.remove(owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4713 | } |
| 4714 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 4715 | |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 4716 | // 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] | 4717 | if (ledNotification == null || mInCall || mScreenOn) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 4718 | mNotificationLight.turnOff(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4719 | } else { |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 4720 | NotificationRecord.Light light = ledNotification.getLight(); |
| 4721 | if (light != null && mNotificationPulseEnabled) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 4722 | // pulse repeatedly |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 4723 | mNotificationLight.setFlashing(light.color, Light.LIGHT_FLASH_TIMED, |
| 4724 | light.onMs, light.offMs); |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 4725 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4726 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4727 | } |
| 4728 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4729 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4730 | @NonNull List<NotificationRecord> findGroupNotificationsLocked(String pkg, |
| 4731 | String groupKey, int userId) { |
| 4732 | List<NotificationRecord> records = new ArrayList<>(); |
| 4733 | records.addAll(findGroupNotificationByListLocked(mNotificationList, pkg, groupKey, userId)); |
| 4734 | records.addAll( |
| 4735 | findGroupNotificationByListLocked(mEnqueuedNotifications, pkg, groupKey, userId)); |
| 4736 | return records; |
| 4737 | } |
| 4738 | |
| 4739 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4740 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4741 | private @NonNull List<NotificationRecord> findGroupNotificationByListLocked( |
| 4742 | ArrayList<NotificationRecord> list, String pkg, String groupKey, int userId) { |
| 4743 | List<NotificationRecord> records = new ArrayList<>(); |
| 4744 | final int len = list.size(); |
| 4745 | for (int i = 0; i < len; i++) { |
| 4746 | NotificationRecord r = list.get(i); |
| 4747 | if (notificationMatchesUserId(r, userId) && r.getGroupKey().equals(groupKey) |
| 4748 | && r.sbn.getPackageName().equals(pkg)) { |
| 4749 | records.add(r); |
| 4750 | } |
| 4751 | } |
| 4752 | return records; |
| 4753 | } |
| 4754 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4755 | // Searches both enqueued and posted notifications by key. |
| 4756 | // TODO: need to combine a bunch of these getters with slightly different behavior. |
| 4757 | // TODO: Should enqueuing just add to mNotificationsByKey instead? |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4758 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4759 | private NotificationRecord findNotificationByKeyLocked(String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4760 | NotificationRecord r; |
| 4761 | if ((r = findNotificationByListLocked(mNotificationList, key)) != null) { |
| 4762 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4763 | } |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4764 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, key)) != null) { |
| 4765 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4766 | } |
| 4767 | return null; |
| 4768 | } |
| 4769 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4770 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4771 | NotificationRecord findNotificationLocked(String pkg, String tag, int id, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4772 | NotificationRecord r; |
| 4773 | if ((r = findNotificationByListLocked(mNotificationList, pkg, tag, id, userId)) != null) { |
| 4774 | return r; |
| 4775 | } |
| 4776 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, pkg, tag, id, userId)) |
| 4777 | != null) { |
| 4778 | return r; |
| 4779 | } |
| 4780 | return null; |
| 4781 | } |
| 4782 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4783 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4784 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4785 | String pkg, String tag, int id, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4786 | final int len = list.size(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4787 | for (int i = 0; i < len; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4788 | NotificationRecord r = list.get(i); |
Vladimir Marko | 2526f33 | 2013-09-11 11:13:55 +0100 | [diff] [blame] | 4789 | if (notificationMatchesUserId(r, userId) && r.sbn.getId() == id && |
| 4790 | TextUtils.equals(r.sbn.getTag(), tag) && r.sbn.getPackageName().equals(pkg)) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4791 | return r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4792 | } |
| 4793 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4794 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4795 | } |
| 4796 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4797 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4798 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4799 | String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4800 | final int N = list.size(); |
| 4801 | for (int i = 0; i < N; i++) { |
| 4802 | if (key.equals(list.get(i).getKey())) { |
| 4803 | return list.get(i); |
| 4804 | } |
| 4805 | } |
| 4806 | return null; |
| 4807 | } |
| 4808 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4809 | @GuardedBy("mNotificationLock") |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 4810 | int indexOfNotificationLocked(String key) { |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 4811 | final int N = mNotificationList.size(); |
| 4812 | for (int i = 0; i < N; i++) { |
| 4813 | if (key.equals(mNotificationList.get(i).getKey())) { |
| 4814 | return i; |
| 4815 | } |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 4816 | } |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 4817 | return -1; |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 4818 | } |
| 4819 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 4820 | private void updateNotificationPulse() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4821 | synchronized (mNotificationLock) { |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 4822 | updateLightsLocked(); |
| 4823 | } |
| 4824 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 4825 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4826 | protected boolean isCallingUidSystem() { |
| 4827 | final int uid = Binder.getCallingUid(); |
| 4828 | return uid == Process.SYSTEM_UID; |
| 4829 | } |
| 4830 | |
| 4831 | protected boolean isUidSystemOrPhone(int uid) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4832 | final int appid = UserHandle.getAppId(uid); |
| 4833 | return (appid == Process.SYSTEM_UID || appid == Process.PHONE_UID || uid == 0); |
| 4834 | } |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 4835 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4836 | // TODO: Most calls should probably move to isCallerSystem. |
| 4837 | protected boolean isCallerSystemOrPhone() { |
| 4838 | return isUidSystemOrPhone(Binder.getCallingUid()); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4839 | } |
| 4840 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4841 | private void checkCallerIsSystem() { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4842 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4843 | return; |
| 4844 | } |
| 4845 | throw new SecurityException("Disallowed call for uid " + Binder.getCallingUid()); |
| 4846 | } |
| 4847 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 4848 | private void checkCallerIsSystemOrSameApp(String pkg) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4849 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4850 | return; |
| 4851 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 4852 | checkCallerIsSameApp(pkg); |
| 4853 | } |
| 4854 | |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 4855 | private boolean isCallerInstantApp(String pkg) { |
| 4856 | // System is always allowed to act for ephemeral apps. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4857 | if (isCallerSystemOrPhone()) { |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 4858 | return false; |
| 4859 | } |
| 4860 | |
| 4861 | mAppOps.checkPackage(Binder.getCallingUid(), pkg); |
| 4862 | |
| 4863 | try { |
| 4864 | ApplicationInfo ai = mPackageManager.getApplicationInfo(pkg, 0, |
| 4865 | UserHandle.getCallingUserId()); |
| 4866 | if (ai == null) { |
| 4867 | throw new SecurityException("Unknown package " + pkg); |
| 4868 | } |
| 4869 | return ai.isInstantApp(); |
| 4870 | } catch (RemoteException re) { |
| 4871 | throw new SecurityException("Unknown package " + pkg, re); |
| 4872 | } |
| 4873 | |
| 4874 | } |
| 4875 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 4876 | private void checkCallerIsSameApp(String pkg) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4877 | final int uid = Binder.getCallingUid(); |
| 4878 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 4879 | ApplicationInfo ai = mPackageManager.getApplicationInfo( |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4880 | pkg, 0, UserHandle.getCallingUserId()); |
Dan Sandler | 09afc2e | 2014-07-18 14:29:20 -0400 | [diff] [blame] | 4881 | if (ai == null) { |
| 4882 | throw new SecurityException("Unknown package " + pkg); |
| 4883 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4884 | if (!UserHandle.isSameApp(ai.uid, uid)) { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 4885 | throw new SecurityException("Calling uid " + uid + " gave package " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4886 | + pkg + " which is owned by uid " + ai.uid); |
| 4887 | } |
| 4888 | } catch (RemoteException re) { |
| 4889 | throw new SecurityException("Unknown package " + pkg + "\n" + re); |
| 4890 | } |
| 4891 | } |
| 4892 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 4893 | private static String callStateToString(int state) { |
| 4894 | switch (state) { |
| 4895 | case TelephonyManager.CALL_STATE_IDLE: return "CALL_STATE_IDLE"; |
| 4896 | case TelephonyManager.CALL_STATE_RINGING: return "CALL_STATE_RINGING"; |
| 4897 | case TelephonyManager.CALL_STATE_OFFHOOK: return "CALL_STATE_OFFHOOK"; |
| 4898 | default: return "CALL_STATE_UNKNOWN_" + state; |
| 4899 | } |
| 4900 | } |
| 4901 | |
| 4902 | private void listenForCallState() { |
| 4903 | TelephonyManager.from(getContext()).listen(new PhoneStateListener() { |
| 4904 | @Override |
| 4905 | public void onCallStateChanged(int state, String incomingNumber) { |
| 4906 | if (mCallState == state) return; |
| 4907 | if (DBG) Slog.d(TAG, "Call state changed: " + callStateToString(state)); |
| 4908 | mCallState = state; |
| 4909 | } |
| 4910 | }, PhoneStateListener.LISTEN_CALL_STATE); |
| 4911 | } |
| 4912 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 4913 | /** |
| 4914 | * Generates a NotificationRankingUpdate from 'sbns', considering only |
| 4915 | * notifications visible to the given listener. |
| 4916 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4917 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4918 | private NotificationRankingUpdate makeRankingUpdateLocked(ManagedServiceInfo info) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4919 | final int N = mNotificationList.size(); |
| 4920 | ArrayList<String> keys = new ArrayList<String>(N); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 4921 | ArrayList<String> interceptedKeys = new ArrayList<String>(N); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4922 | ArrayList<Integer> importance = new ArrayList<>(N); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4923 | Bundle overrideGroupKeys = new Bundle(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4924 | Bundle visibilityOverrides = new Bundle(); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4925 | Bundle suppressedVisualEffects = new Bundle(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4926 | Bundle explanation = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4927 | Bundle channels = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4928 | Bundle overridePeople = new Bundle(); |
| 4929 | Bundle snoozeCriteria = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4930 | Bundle showBadge = new Bundle(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4931 | for (int i = 0; i < N; i++) { |
| 4932 | NotificationRecord record = mNotificationList.get(i); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 4933 | if (!isVisibleToListener(record.sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 4934 | continue; |
| 4935 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4936 | final String key = record.sbn.getKey(); |
| 4937 | keys.add(key); |
| 4938 | importance.add(record.getImportance()); |
| 4939 | if (record.getImportanceExplanation() != null) { |
| 4940 | explanation.putCharSequence(key, record.getImportanceExplanation()); |
| 4941 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4942 | if (record.isIntercepted()) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4943 | interceptedKeys.add(key); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4944 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 4945 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4946 | suppressedVisualEffects.putInt(key, record.getSuppressedVisualEffects()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4947 | if (record.getPackageVisibilityOverride() |
| 4948 | != NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4949 | visibilityOverrides.putInt(key, record.getPackageVisibilityOverride()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4950 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4951 | overrideGroupKeys.putString(key, record.sbn.getOverrideGroupKey()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4952 | channels.putParcelable(key, record.getChannel()); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4953 | overridePeople.putStringArrayList(key, record.getPeopleOverride()); |
| 4954 | snoozeCriteria.putParcelableArrayList(key, record.getSnoozeCriteria()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4955 | showBadge.putBoolean(key, record.canShowBadge()); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 4956 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4957 | final int M = keys.size(); |
| 4958 | String[] keysAr = keys.toArray(new String[M]); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 4959 | String[] interceptedKeysAr = interceptedKeys.toArray(new String[interceptedKeys.size()]); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4960 | int[] importanceAr = new int[M]; |
| 4961 | for (int i = 0; i < M; i++) { |
| 4962 | importanceAr[i] = importance.get(i); |
| 4963 | } |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4964 | return new NotificationRankingUpdate(keysAr, interceptedKeysAr, visibilityOverrides, |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4965 | suppressedVisualEffects, importanceAr, explanation, overrideGroupKeys, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4966 | channels, overridePeople, snoozeCriteria, showBadge); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 4967 | } |
| 4968 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 4969 | boolean hasCompanionDevice(ManagedServiceInfo info) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4970 | if (mCompanionManager == null) { |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 4971 | mCompanionManager = getCompanionManager(); |
| 4972 | } |
| 4973 | // Companion mgr doesn't exist on all device types |
| 4974 | if (mCompanionManager == null) { |
| 4975 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4976 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 4977 | long identity = Binder.clearCallingIdentity(); |
| 4978 | try { |
| 4979 | List<String> associations = mCompanionManager.getAssociations( |
| 4980 | info.component.getPackageName(), info.userid); |
| 4981 | if (!ArrayUtils.isEmpty(associations)) { |
| 4982 | return true; |
| 4983 | } |
| 4984 | } catch (SecurityException se) { |
| 4985 | // Not a privileged listener |
| 4986 | } catch (RemoteException re) { |
| 4987 | Slog.e(TAG, "Cannot reach companion device service", re); |
| 4988 | } catch (Exception e) { |
| 4989 | Slog.e(TAG, "Cannot verify listener " + info, e); |
| 4990 | } finally { |
| 4991 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4992 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 4993 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4994 | } |
| 4995 | |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 4996 | protected ICompanionDeviceManager getCompanionManager() { |
| 4997 | return ICompanionDeviceManager.Stub.asInterface( |
| 4998 | ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE)); |
| 4999 | } |
| 5000 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5001 | private boolean isVisibleToListener(StatusBarNotification sbn, ManagedServiceInfo listener) { |
| 5002 | if (!listener.enabledAndUserMatches(sbn.getUserId())) { |
| 5003 | return false; |
| 5004 | } |
Justin Koh | 8d11a5a | 2014-08-04 18:29:49 -0700 | [diff] [blame] | 5005 | // TODO: remove this for older listeners. |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5006 | return true; |
| 5007 | } |
| 5008 | |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 5009 | private boolean isPackageSuspendedForUser(String pkg, int uid) { |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5010 | int userId = UserHandle.getUserId(uid); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5011 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5012 | return mPackageManager.isPackageSuspendedForUser(pkg, userId); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5013 | } catch (RemoteException re) { |
| 5014 | throw new SecurityException("Could not talk to package manager service"); |
Andrei Stingaceanu | efc4a34 | 2016-03-22 14:43:01 +0000 | [diff] [blame] | 5015 | } catch (IllegalArgumentException ex) { |
| 5016 | // Package not found. |
| 5017 | return false; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5018 | } |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 5019 | } |
| 5020 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5021 | private class TrimCache { |
| 5022 | StatusBarNotification heavy; |
| 5023 | StatusBarNotification sbnClone; |
| 5024 | StatusBarNotification sbnCloneLight; |
| 5025 | |
| 5026 | TrimCache(StatusBarNotification sbn) { |
| 5027 | heavy = sbn; |
| 5028 | } |
| 5029 | |
| 5030 | StatusBarNotification ForListener(ManagedServiceInfo info) { |
| 5031 | if (mListeners.getOnNotificationPostedTrim(info) == TRIM_LIGHT) { |
| 5032 | if (sbnCloneLight == null) { |
| 5033 | sbnCloneLight = heavy.cloneLight(); |
| 5034 | } |
| 5035 | return sbnCloneLight; |
| 5036 | } else { |
| 5037 | if (sbnClone == null) { |
| 5038 | sbnClone = heavy.clone(); |
| 5039 | } |
| 5040 | return sbnClone; |
| 5041 | } |
| 5042 | } |
| 5043 | } |
| 5044 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5045 | public class NotificationAssistants extends ManagedServices { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5046 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5047 | public NotificationAssistants() { |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5048 | super(getContext(), mHandler, mNotificationLock, mUserProfiles); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5049 | } |
| 5050 | |
| 5051 | @Override |
| 5052 | protected Config getConfig() { |
| 5053 | Config c = new Config(); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5054 | c.caption = "notification assistant service"; |
| 5055 | c.serviceInterface = NotificationAssistantService.SERVICE_INTERFACE; |
| 5056 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_ASSISTANT; |
| 5057 | c.bindPermission = Manifest.permission.BIND_NOTIFICATION_ASSISTANT_SERVICE; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5058 | c.settingsAction = Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS; |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 5059 | c.clientLabel = R.string.notification_ranker_binding_label; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5060 | return c; |
| 5061 | } |
| 5062 | |
| 5063 | @Override |
| 5064 | protected IInterface asInterface(IBinder binder) { |
| 5065 | return INotificationListener.Stub.asInterface(binder); |
| 5066 | } |
| 5067 | |
| 5068 | @Override |
| 5069 | protected boolean checkType(IInterface service) { |
| 5070 | return service instanceof INotificationListener; |
| 5071 | } |
| 5072 | |
| 5073 | @Override |
| 5074 | protected void onServiceAdded(ManagedServiceInfo info) { |
| 5075 | mListeners.registerGuestService(info); |
| 5076 | } |
| 5077 | |
| 5078 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5079 | @GuardedBy("mNotificationLock") |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5080 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
| 5081 | mListeners.unregisterService(removed.service, removed.userid); |
| 5082 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5083 | |
| 5084 | public void onNotificationEnqueued(final NotificationRecord r) { |
| 5085 | final StatusBarNotification sbn = r.sbn; |
| 5086 | TrimCache trimCache = new TrimCache(sbn); |
| 5087 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5088 | // There should be only one, but it's a list, so while we enforce |
| 5089 | // singularity elsewhere, we keep it general here, to avoid surprises. |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5090 | for (final ManagedServiceInfo info : NotificationAssistants.this.getServices()) { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5091 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 5092 | if (!sbnVisible) { |
| 5093 | continue; |
| 5094 | } |
| 5095 | |
| 5096 | final int importance = r.getImportance(); |
| 5097 | final boolean fromUser = r.isImportanceFromUser(); |
| 5098 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 5099 | mHandler.post(new Runnable() { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5100 | @Override |
| 5101 | public void run() { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5102 | notifyEnqueued(info, sbnToPost); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5103 | } |
| 5104 | }); |
| 5105 | } |
| 5106 | } |
| 5107 | |
| 5108 | private void notifyEnqueued(final ManagedServiceInfo info, |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5109 | final StatusBarNotification sbn) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5110 | final INotificationListener assistant = (INotificationListener) info.service; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5111 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
| 5112 | try { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5113 | assistant.onNotificationEnqueued(sbnHolder); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5114 | } catch (RemoteException ex) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5115 | Log.e(TAG, "unable to notify assistant (enqueued): " + assistant, ex); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5116 | } |
| 5117 | } |
| 5118 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5119 | /** |
| 5120 | * asynchronously notify the assistant that a notification has been snoozed until a |
| 5121 | * context |
| 5122 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5123 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5124 | public void notifyAssistantSnoozedLocked(final StatusBarNotification sbn, |
| 5125 | final String snoozeCriterionId) { |
| 5126 | TrimCache trimCache = new TrimCache(sbn); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5127 | for (final ManagedServiceInfo info : getServices()) { |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5128 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
| 5129 | mHandler.post(new Runnable() { |
| 5130 | @Override |
| 5131 | public void run() { |
| 5132 | final INotificationListener assistant = |
| 5133 | (INotificationListener) info.service; |
| 5134 | StatusBarNotificationHolder sbnHolder |
| 5135 | = new StatusBarNotificationHolder(sbnToPost); |
| 5136 | try { |
| 5137 | assistant.onNotificationSnoozedUntilContext( |
| 5138 | sbnHolder, snoozeCriterionId); |
| 5139 | } catch (RemoteException ex) { |
| 5140 | Log.e(TAG, "unable to notify assistant (snoozed): " + assistant, ex); |
| 5141 | } |
| 5142 | } |
| 5143 | }); |
| 5144 | } |
| 5145 | } |
| 5146 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5147 | public boolean isEnabled() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5148 | return !getServices().isEmpty(); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5149 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5150 | } |
| 5151 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5152 | public class NotificationListeners extends ManagedServices { |
| 5153 | |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5154 | private final ArraySet<ManagedServiceInfo> mLightTrimListeners = new ArraySet<>(); |
| 5155 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5156 | public NotificationListeners() { |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5157 | super(getContext(), mHandler, mNotificationLock, mUserProfiles); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5158 | } |
| 5159 | |
| 5160 | @Override |
| 5161 | protected Config getConfig() { |
| 5162 | Config c = new Config(); |
| 5163 | c.caption = "notification listener"; |
| 5164 | c.serviceInterface = NotificationListenerService.SERVICE_INTERFACE; |
| 5165 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS; |
| 5166 | c.bindPermission = android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE; |
| 5167 | c.settingsAction = Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS; |
| 5168 | c.clientLabel = R.string.notification_listener_binding_label; |
| 5169 | return c; |
| 5170 | } |
| 5171 | |
| 5172 | @Override |
| 5173 | protected IInterface asInterface(IBinder binder) { |
| 5174 | return INotificationListener.Stub.asInterface(binder); |
| 5175 | } |
| 5176 | |
| 5177 | @Override |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5178 | protected boolean checkType(IInterface service) { |
| 5179 | return service instanceof INotificationListener; |
| 5180 | } |
| 5181 | |
| 5182 | @Override |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 5183 | public void onServiceAdded(ManagedServiceInfo info) { |
| 5184 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5185 | final NotificationRankingUpdate update; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5186 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5187 | update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5188 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5189 | try { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5190 | listener.onListenerConnected(update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5191 | } catch (RemoteException e) { |
| 5192 | // we tried |
| 5193 | } |
| 5194 | } |
| 5195 | |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5196 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5197 | @GuardedBy("mNotificationLock") |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5198 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 5199 | if (removeDisabledHints(removed)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5200 | updateListenerHintsLocked(); |
Christoph Studer | 0d6ef4b | 2014-12-02 15:00:48 +0100 | [diff] [blame] | 5201 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5202 | } |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5203 | mLightTrimListeners.remove(removed); |
| 5204 | } |
| 5205 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5206 | @GuardedBy("mNotificationLock") |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5207 | public void setOnNotificationPostedTrimLocked(ManagedServiceInfo info, int trim) { |
| 5208 | if (trim == TRIM_LIGHT) { |
| 5209 | mLightTrimListeners.add(info); |
| 5210 | } else { |
| 5211 | mLightTrimListeners.remove(info); |
| 5212 | } |
| 5213 | } |
| 5214 | |
| 5215 | public int getOnNotificationPostedTrim(ManagedServiceInfo info) { |
| 5216 | return mLightTrimListeners.contains(info) ? TRIM_LIGHT : TRIM_FULL; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5217 | } |
| 5218 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5219 | /** |
| 5220 | * asynchronously notify all listeners about a new notification |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5221 | * |
| 5222 | * <p> |
| 5223 | * Also takes care of removing a notification that has been visible to a listener before, |
| 5224 | * but isn't anymore. |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5225 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5226 | @GuardedBy("mNotificationLock") |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5227 | public void notifyPostedLocked(StatusBarNotification sbn, StatusBarNotification oldSbn) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5228 | // Lazily initialized snapshots of the notification. |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5229 | TrimCache trimCache = new TrimCache(sbn); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5230 | |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5231 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5232 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 5233 | boolean oldSbnVisible = oldSbn != null ? isVisibleToListener(oldSbn, info) : false; |
| 5234 | // This notification hasn't been and still isn't visible -> ignore. |
| 5235 | if (!oldSbnVisible && !sbnVisible) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5236 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5237 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5238 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5239 | |
| 5240 | // This notification became invisible -> remove the old one. |
| 5241 | if (oldSbnVisible && !sbnVisible) { |
| 5242 | final StatusBarNotification oldSbnLightClone = oldSbn.cloneLight(); |
| 5243 | mHandler.post(new Runnable() { |
| 5244 | @Override |
| 5245 | public void run() { |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 5246 | notifyRemoved(info, oldSbnLightClone, update, REASON_USER_STOPPED); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5247 | } |
| 5248 | }); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5249 | continue; |
| 5250 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5251 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5252 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5253 | mHandler.post(new Runnable() { |
| 5254 | @Override |
| 5255 | public void run() { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5256 | notifyPosted(info, sbnToPost, update); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5257 | } |
| 5258 | }); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5259 | } |
| 5260 | } |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5261 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5262 | /** |
| 5263 | * asynchronously notify all listeners about a removed notification |
| 5264 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5265 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 5266 | public void notifyRemovedLocked(StatusBarNotification sbn, int reason) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5267 | // make a copy in case changes are made to the underlying Notification object |
| 5268 | // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the |
| 5269 | // notification |
| 5270 | final StatusBarNotification sbnLight = sbn.cloneLight(); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5271 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5272 | if (!isVisibleToListener(sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5273 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5274 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5275 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5276 | mHandler.post(new Runnable() { |
| 5277 | @Override |
| 5278 | public void run() { |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 5279 | notifyRemoved(info, sbnLight, update, reason); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5280 | } |
| 5281 | }); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5282 | } |
| 5283 | } |
| 5284 | |
| 5285 | /** |
| 5286 | * asynchronously notify all listeners about a reordering of notifications |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5287 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5288 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5289 | public void notifyRankingUpdateLocked() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5290 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5291 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 5292 | continue; |
| 5293 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5294 | final NotificationRankingUpdate update = makeRankingUpdateLocked(serviceInfo); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5295 | mHandler.post(new Runnable() { |
| 5296 | @Override |
| 5297 | public void run() { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5298 | notifyRankingUpdate(serviceInfo, update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5299 | } |
| 5300 | }); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5301 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5302 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5303 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5304 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5305 | public void notifyListenerHintsChangedLocked(final int hints) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5306 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5307 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 5308 | continue; |
| 5309 | } |
| 5310 | mHandler.post(new Runnable() { |
| 5311 | @Override |
| 5312 | public void run() { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5313 | notifyListenerHintsChanged(serviceInfo, hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5314 | } |
| 5315 | }); |
| 5316 | } |
| 5317 | } |
| 5318 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5319 | public void notifyInterruptionFilterChanged(final int interruptionFilter) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5320 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5321 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 5322 | continue; |
| 5323 | } |
| 5324 | mHandler.post(new Runnable() { |
| 5325 | @Override |
| 5326 | public void run() { |
| 5327 | notifyInterruptionFilterChanged(serviceInfo, interruptionFilter); |
| 5328 | } |
| 5329 | }); |
| 5330 | } |
| 5331 | } |
| 5332 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5333 | protected void notifyNotificationChannelChanged(final String pkg, final UserHandle user, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5334 | final NotificationChannel channel, final int modificationType) { |
| 5335 | if (channel == null) { |
| 5336 | return; |
| 5337 | } |
| 5338 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5339 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5340 | continue; |
| 5341 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5342 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5343 | mHandler.post(new Runnable() { |
| 5344 | @Override |
| 5345 | public void run() { |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5346 | if (hasCompanionDevice(serviceInfo)) { |
| 5347 | notifyNotificationChannelChanged( |
| 5348 | serviceInfo, pkg, user, channel, modificationType); |
| 5349 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5350 | } |
| 5351 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5352 | } |
| 5353 | } |
| 5354 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5355 | protected void notifyNotificationChannelGroupChanged( |
| 5356 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
| 5357 | final int modificationType) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5358 | if (group == null) { |
| 5359 | return; |
| 5360 | } |
| 5361 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5362 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5363 | continue; |
| 5364 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5365 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5366 | mHandler.post(new Runnable() { |
| 5367 | @Override |
| 5368 | public void run() { |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5369 | if (hasCompanionDevice(serviceInfo)) { |
| 5370 | notifyNotificationChannelGroupChanged( |
| 5371 | serviceInfo, pkg, user, group, modificationType); |
| 5372 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5373 | } |
| 5374 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5375 | } |
| 5376 | } |
| 5377 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5378 | private void notifyPosted(final ManagedServiceInfo info, |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5379 | final StatusBarNotification sbn, NotificationRankingUpdate rankingUpdate) { |
Julia Reynolds | a75c752 | 2017-03-21 17:34:25 -0400 | [diff] [blame] | 5380 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5381 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5382 | try { |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5383 | listener.onNotificationPosted(sbnHolder, rankingUpdate); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5384 | } catch (RemoteException ex) { |
| 5385 | Log.e(TAG, "unable to notify listener (posted): " + listener, ex); |
| 5386 | } |
| 5387 | } |
| 5388 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5389 | private void notifyRemoved(ManagedServiceInfo info, StatusBarNotification sbn, |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 5390 | NotificationRankingUpdate rankingUpdate, int reason) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5391 | if (!info.enabledAndUserMatches(sbn.getUserId())) { |
| 5392 | return; |
| 5393 | } |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5394 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5395 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5396 | try { |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 5397 | listener.onNotificationRemoved(sbnHolder, rankingUpdate, reason); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5398 | } catch (RemoteException ex) { |
| 5399 | Log.e(TAG, "unable to notify listener (removed): " + listener, ex); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 5400 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5401 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5402 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5403 | private void notifyRankingUpdate(ManagedServiceInfo info, |
| 5404 | NotificationRankingUpdate rankingUpdate) { |
| 5405 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5406 | try { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5407 | listener.onNotificationRankingUpdate(rankingUpdate); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5408 | } catch (RemoteException ex) { |
| 5409 | Log.e(TAG, "unable to notify listener (ranking update): " + listener, ex); |
| 5410 | } |
| 5411 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5412 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5413 | private void notifyListenerHintsChanged(ManagedServiceInfo info, int hints) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5414 | final INotificationListener listener = (INotificationListener) info.service; |
| 5415 | try { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5416 | listener.onListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5417 | } catch (RemoteException ex) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5418 | Log.e(TAG, "unable to notify listener (listener hints): " + listener, ex); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5419 | } |
| 5420 | } |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 5421 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5422 | private void notifyInterruptionFilterChanged(ManagedServiceInfo info, |
| 5423 | int interruptionFilter) { |
| 5424 | final INotificationListener listener = (INotificationListener) info.service; |
| 5425 | try { |
| 5426 | listener.onInterruptionFilterChanged(interruptionFilter); |
| 5427 | } catch (RemoteException ex) { |
| 5428 | Log.e(TAG, "unable to notify listener (interruption filter): " + listener, ex); |
| 5429 | } |
| 5430 | } |
| 5431 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5432 | void notifyNotificationChannelChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5433 | final String pkg, final UserHandle user, final NotificationChannel channel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5434 | final int modificationType) { |
| 5435 | final INotificationListener listener = (INotificationListener) info.service; |
| 5436 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5437 | listener.onNotificationChannelModification(pkg, user, channel, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5438 | } catch (RemoteException ex) { |
| 5439 | Log.e(TAG, "unable to notify listener (channel changed): " + listener, ex); |
| 5440 | } |
| 5441 | } |
| 5442 | |
| 5443 | private void notifyNotificationChannelGroupChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5444 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5445 | final int modificationType) { |
| 5446 | final INotificationListener listener = (INotificationListener) info.service; |
| 5447 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5448 | listener.onNotificationChannelGroupModification(pkg, user, group, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5449 | } catch (RemoteException ex) { |
| 5450 | Log.e(TAG, "unable to notify listener (channel group changed): " + listener, ex); |
| 5451 | } |
| 5452 | } |
| 5453 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5454 | public boolean isListenerPackage(String packageName) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 5455 | if (packageName == null) { |
| 5456 | return false; |
| 5457 | } |
| 5458 | // TODO: clean up locking object later |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5459 | synchronized (mNotificationLock) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5460 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 5461 | if (packageName.equals(serviceInfo.component.getPackageName())) { |
| 5462 | return true; |
| 5463 | } |
| 5464 | } |
| 5465 | } |
| 5466 | return false; |
| 5467 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5468 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5469 | |
| 5470 | public static final class DumpFilter { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5471 | public boolean filtered = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5472 | public String pkgFilter; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 5473 | public boolean zen; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 5474 | public long since; |
| 5475 | public boolean stats; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5476 | public boolean redact = true; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 5477 | public boolean proto = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5478 | |
| 5479 | public static DumpFilter parseFromArguments(String[] args) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5480 | final DumpFilter filter = new DumpFilter(); |
| 5481 | for (int ai = 0; ai < args.length; ai++) { |
| 5482 | final String a = args[ai]; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 5483 | if ("--proto".equals(args[0])) { |
| 5484 | filter.proto = true; |
| 5485 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5486 | if ("--noredact".equals(a) || "--reveal".equals(a)) { |
| 5487 | filter.redact = false; |
| 5488 | } else if ("p".equals(a) || "pkg".equals(a) || "--package".equals(a)) { |
| 5489 | if (ai < args.length-1) { |
| 5490 | ai++; |
| 5491 | filter.pkgFilter = args[ai].trim().toLowerCase(); |
| 5492 | if (filter.pkgFilter.isEmpty()) { |
| 5493 | filter.pkgFilter = null; |
| 5494 | } else { |
| 5495 | filter.filtered = true; |
| 5496 | } |
| 5497 | } |
| 5498 | } else if ("--zen".equals(a) || "zen".equals(a)) { |
| 5499 | filter.filtered = true; |
| 5500 | filter.zen = true; |
| 5501 | } else if ("--stats".equals(a)) { |
| 5502 | filter.stats = true; |
| 5503 | if (ai < args.length-1) { |
| 5504 | ai++; |
Tobias Thierer | 28532d0 | 2016-04-21 14:52:10 +0100 | [diff] [blame] | 5505 | filter.since = Long.parseLong(args[ai]); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5506 | } else { |
| 5507 | filter.since = 0; |
| 5508 | } |
| 5509 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5510 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5511 | return filter; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5512 | } |
| 5513 | |
| 5514 | public boolean matches(StatusBarNotification sbn) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5515 | if (!filtered) return true; |
| 5516 | return zen ? true : sbn != null |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 5517 | && (matches(sbn.getPackageName()) || matches(sbn.getOpPkg())); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5518 | } |
| 5519 | |
| 5520 | public boolean matches(ComponentName component) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5521 | if (!filtered) return true; |
| 5522 | return zen ? true : component != null && matches(component.getPackageName()); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5523 | } |
| 5524 | |
| 5525 | public boolean matches(String pkg) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5526 | if (!filtered) return true; |
| 5527 | return zen ? true : pkg != null && pkg.toLowerCase().contains(pkgFilter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 5528 | } |
| 5529 | |
| 5530 | @Override |
| 5531 | public String toString() { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 5532 | return stats ? "stats" : zen ? "zen" : ('\'' + pkgFilter + '\''); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5533 | } |
| 5534 | } |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5535 | |
| 5536 | /** |
| 5537 | * Wrapper for a StatusBarNotification object that allows transfer across a oneway |
| 5538 | * binder without sending large amounts of data over a oneway transaction. |
| 5539 | */ |
| 5540 | private static final class StatusBarNotificationHolder |
| 5541 | extends IStatusBarNotificationHolder.Stub { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 5542 | private StatusBarNotification mValue; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5543 | |
| 5544 | public StatusBarNotificationHolder(StatusBarNotification value) { |
| 5545 | mValue = value; |
| 5546 | } |
| 5547 | |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 5548 | /** 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] | 5549 | @Override |
| 5550 | public StatusBarNotification get() { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 5551 | StatusBarNotification value = mValue; |
| 5552 | mValue = null; |
| 5553 | return value; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5554 | } |
| 5555 | } |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5556 | |
| 5557 | private final class PolicyAccess { |
| 5558 | private static final String SEPARATOR = ":"; |
| 5559 | private final String[] PERM = { |
| 5560 | android.Manifest.permission.ACCESS_NOTIFICATION_POLICY |
| 5561 | }; |
| 5562 | |
| 5563 | public boolean isPackageGranted(String pkg) { |
| 5564 | return pkg != null && getGrantedPackages().contains(pkg); |
| 5565 | } |
| 5566 | |
| 5567 | public void put(String pkg, boolean granted) { |
| 5568 | if (pkg == null) return; |
| 5569 | final ArraySet<String> pkgs = getGrantedPackages(); |
| 5570 | boolean changed; |
| 5571 | if (granted) { |
| 5572 | changed = pkgs.add(pkg); |
| 5573 | } else { |
| 5574 | changed = pkgs.remove(pkg); |
| 5575 | } |
| 5576 | if (!changed) return; |
| 5577 | final String setting = TextUtils.join(SEPARATOR, pkgs); |
| 5578 | final int currentUser = ActivityManager.getCurrentUser(); |
| 5579 | Settings.Secure.putStringForUser(getContext().getContentResolver(), |
| 5580 | Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES, |
| 5581 | setting, |
| 5582 | currentUser); |
| 5583 | getContext().sendBroadcastAsUser(new Intent(NotificationManager |
| 5584 | .ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 5585 | .setPackage(pkg) |
| 5586 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), new UserHandle(currentUser), null); |
| 5587 | } |
| 5588 | |
| 5589 | public ArraySet<String> getGrantedPackages() { |
| 5590 | final ArraySet<String> pkgs = new ArraySet<>(); |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 5591 | |
| 5592 | long identity = Binder.clearCallingIdentity(); |
| 5593 | try { |
| 5594 | final String setting = Settings.Secure.getStringForUser( |
| 5595 | getContext().getContentResolver(), |
| 5596 | Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES, |
| 5597 | ActivityManager.getCurrentUser()); |
| 5598 | if (setting != null) { |
| 5599 | final String[] tokens = setting.split(SEPARATOR); |
| 5600 | for (int i = 0; i < tokens.length; i++) { |
| 5601 | String token = tokens[i]; |
| 5602 | if (token != null) { |
Andreas Gampe | 1ed71f3 | 2015-12-11 15:49:07 -0800 | [diff] [blame] | 5603 | token = token.trim(); |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 5604 | } |
| 5605 | if (TextUtils.isEmpty(token)) { |
| 5606 | continue; |
| 5607 | } |
| 5608 | pkgs.add(token); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5609 | } |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5610 | } |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 5611 | } finally { |
| 5612 | Binder.restoreCallingIdentity(identity); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5613 | } |
| 5614 | return pkgs; |
| 5615 | } |
| 5616 | |
| 5617 | public String[] getRequestingPackages() throws RemoteException { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5618 | final ParceledListSlice list = mPackageManager |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5619 | .getPackagesHoldingPermissions(PERM, 0 /*flags*/, |
| 5620 | ActivityManager.getCurrentUser()); |
| 5621 | final List<PackageInfo> pkgs = list.getList(); |
| 5622 | if (pkgs == null || pkgs.isEmpty()) return new String[0]; |
| 5623 | final int N = pkgs.size(); |
| 5624 | final String[] rt = new String[N]; |
| 5625 | for (int i = 0; i < N; i++) { |
| 5626 | rt[i] = pkgs.get(i).packageName; |
| 5627 | } |
| 5628 | return rt; |
| 5629 | } |
| 5630 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5631 | } |