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 | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 23 | import static android.service.notification.NotificationListenerService |
| 24 | .NOTIFICATION_CHANNEL_OR_GROUP_ADDED; |
| 25 | import static android.service.notification.NotificationListenerService |
| 26 | .NOTIFICATION_CHANNEL_OR_GROUP_DELETED; |
| 27 | import static android.service.notification.NotificationListenerService |
| 28 | .NOTIFICATION_CHANNEL_OR_GROUP_UPDATED; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 29 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL; |
| 30 | import static android.service.notification.NotificationListenerService.REASON_APP_CANCEL_ALL; |
| 31 | import static android.service.notification.NotificationListenerService.REASON_CHANNEL_BANNED; |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 32 | import static android.service.notification.NotificationListenerService.REASON_CANCEL; |
| 33 | import static android.service.notification.NotificationListenerService.REASON_CANCEL_ALL; |
| 34 | import static android.service.notification.NotificationListenerService.REASON_CLICK; |
| 35 | import static android.service.notification.NotificationListenerService.REASON_ERROR; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 36 | import static android.service.notification.NotificationListenerService.REASON_GROUP_SUMMARY_CANCELED; |
| 37 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL; |
| 38 | import static android.service.notification.NotificationListenerService.REASON_LISTENER_CANCEL_ALL; |
| 39 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_BANNED; |
| 40 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_CHANGED; |
| 41 | import static android.service.notification.NotificationListenerService.REASON_PACKAGE_SUSPENDED; |
| 42 | import static android.service.notification.NotificationListenerService.REASON_PROFILE_TURNED_OFF; |
| 43 | import static android.service.notification.NotificationListenerService.REASON_SNOOZED; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 44 | import static android.service.notification.NotificationListenerService.REASON_TIMEOUT; |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 45 | import static android.service.notification.NotificationListenerService.REASON_UNAUTOBUNDLED; |
| 46 | import static android.service.notification.NotificationListenerService.REASON_USER_STOPPED; |
Jason Monk | 6350674 | 2015-12-16 12:06:51 -0500 | [diff] [blame] | 47 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_EFFECTS; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 48 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_NOTIFICATION_EFFECTS; |
| 49 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS; |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 50 | import static android.service.notification.NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF; |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 51 | import static android.service.notification.NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_ON; |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 52 | import static android.service.notification.NotificationListenerService.TRIM_FULL; |
| 53 | import static android.service.notification.NotificationListenerService.TRIM_LIGHT; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 54 | |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 55 | import static android.view.Display.DEFAULT_DISPLAY; |
| 56 | import static android.view.WindowManager.LayoutParams.TYPE_TOAST; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 57 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 58 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 59 | import android.Manifest; |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 60 | import android.annotation.NonNull; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 61 | import android.annotation.Nullable; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 62 | import android.app.ActivityManager; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 63 | import android.app.ActivityManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 64 | import android.app.AlarmManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 65 | import android.app.AppGlobals; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 66 | import android.app.AppOpsManager; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 67 | import android.app.AutomaticZenRule; |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 68 | import android.app.NotificationChannelGroup; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 69 | import android.app.backup.BackupManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | import android.app.IActivityManager; |
| 71 | import android.app.INotificationManager; |
| 72 | import android.app.ITransientNotification; |
| 73 | import android.app.Notification; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 74 | import android.app.NotificationChannel; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 75 | import android.app.NotificationManager.Policy; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 76 | import android.app.NotificationManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | import android.app.PendingIntent; |
| 78 | import android.app.StatusBarManager; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 79 | import android.app.usage.UsageEvents; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 80 | import android.app.usage.UsageStatsManagerInternal; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 81 | import android.companion.ICompanionDeviceManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | import android.content.BroadcastReceiver; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 83 | import android.content.ComponentName; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 84 | import android.content.ContentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | import android.content.Context; |
| 86 | import android.content.Intent; |
| 87 | import android.content.IntentFilter; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 88 | import android.content.pm.ApplicationInfo; |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 89 | import android.content.pm.IPackageManager; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 90 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | import android.content.pm.PackageManager; |
| 92 | import android.content.pm.PackageManager.NameNotFoundException; |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 93 | import android.content.pm.ParceledListSlice; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | import android.content.res.Resources; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 95 | import android.database.ContentObserver; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 96 | import android.media.AudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 97 | import android.media.AudioManagerInternal; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 98 | import android.media.IRingtonePlayer; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 99 | import android.media.ToneGenerator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | import android.os.Binder; |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 102 | import android.os.Build; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 103 | import android.os.Bundle; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 104 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | import android.os.Handler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 106 | import android.os.HandlerThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | import android.os.IBinder; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 108 | import android.os.IInterface; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 109 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import android.os.Message; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 111 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 112 | import android.os.RemoteException; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 113 | import android.os.ServiceManager; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 114 | import android.os.SystemClock; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 115 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 116 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | import android.os.Vibrator; |
Michael Wright | 7121697 | 2017-01-31 18:33:54 +0000 | [diff] [blame] | 118 | import android.os.VibrationEffect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | import android.provider.Settings; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 120 | import android.service.notification.Adjustment; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 121 | import android.service.notification.Condition; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 122 | import android.service.notification.IConditionProvider; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 123 | import android.service.notification.INotificationListener; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 124 | import android.service.notification.IStatusBarNotificationHolder; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 125 | import android.service.notification.NotificationAssistantService; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 126 | import android.service.notification.NotificationListenerService; |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 127 | import android.service.notification.NotificationRankingUpdate; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 128 | import android.service.notification.NotificationRecordProto; |
| 129 | import android.service.notification.NotificationServiceDumpProto; |
| 130 | import android.service.notification.NotificationServiceProto; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 131 | import android.service.notification.SnoozeCriterion; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 132 | import android.service.notification.StatusBarNotification; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 133 | import android.service.notification.ZenModeConfig; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 134 | import android.service.notification.ZenModeProto; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 135 | import android.telephony.PhoneStateListener; |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 136 | import android.telephony.TelephonyManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 137 | import android.text.TextUtils; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 138 | import android.util.ArrayMap; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 139 | import android.util.ArraySet; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 140 | import android.util.AtomicFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | import android.util.Log; |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 142 | import android.util.Slog; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 143 | import android.util.SparseArray; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 144 | import android.util.Xml; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 145 | import android.util.proto.ProtoOutputStream; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 146 | import android.view.WindowManagerInternal; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 147 | import android.view.accessibility.AccessibilityEvent; |
| 148 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | import android.widget.Toast; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 150 | |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 151 | import com.android.internal.R; |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 152 | import com.android.internal.annotations.GuardedBy; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 153 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 154 | import com.android.internal.logging.MetricsLogger; |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 155 | import com.android.internal.logging.nano.MetricsProto; |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 156 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 157 | import com.android.internal.statusbar.NotificationVisibility; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 158 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 159 | import com.android.internal.util.DumpUtils; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 160 | import com.android.internal.util.FastXmlSerializer; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 161 | import com.android.internal.util.Preconditions; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 162 | import com.android.server.DeviceIdleController; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 163 | import com.android.server.EventLogTags; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 164 | import com.android.server.LocalServices; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 165 | import com.android.server.SystemService; |
| 166 | import com.android.server.lights.Light; |
| 167 | import com.android.server.lights.LightsManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 168 | import com.android.server.notification.ManagedServices.ManagedServiceInfo; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 169 | import com.android.server.policy.PhoneWindowManager; |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 170 | import com.android.server.statusbar.StatusBarManagerInternal; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 171 | import com.android.server.notification.ManagedServices.UserProfiles; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 172 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 173 | import libcore.io.IoUtils; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 174 | |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 175 | import org.json.JSONException; |
| 176 | import org.json.JSONObject; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 177 | import org.xmlpull.v1.XmlPullParser; |
| 178 | import org.xmlpull.v1.XmlPullParserException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 179 | import org.xmlpull.v1.XmlSerializer; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 180 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 181 | import java.io.ByteArrayInputStream; |
| 182 | import java.io.ByteArrayOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 183 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | import java.io.FileDescriptor; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 185 | import java.io.FileInputStream; |
| 186 | import java.io.FileNotFoundException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 187 | import java.io.FileOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 188 | import java.io.IOException; |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 189 | import java.io.InputStream; |
| 190 | import java.io.OutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 192 | import java.nio.charset.StandardCharsets; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 193 | import java.util.ArrayDeque; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | import java.util.ArrayList; |
Michael Wright | bc4d0d9 | 2017-03-23 18:57:57 +0000 | [diff] [blame] | 195 | import java.util.Arrays; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 196 | import java.util.Iterator; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 197 | import java.util.List; |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 198 | import java.util.Map; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 199 | import java.util.Map.Entry; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 200 | import java.util.Objects; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 201 | import java.util.concurrent.TimeUnit; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 202 | |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 203 | /** {@hide} */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 204 | public class NotificationManagerService extends SystemService { |
| 205 | static final String TAG = "NotificationService"; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 206 | static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Selim Cinek | 4041249 | 2015-12-08 18:03:22 -0800 | [diff] [blame] | 207 | public static final boolean ENABLE_CHILD_NOTIFICATIONS |
| 208 | = SystemProperties.getBoolean("debug.child_notifs", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 210 | static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 211 | static final float DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE = 10f; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 212 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | // message codes |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 214 | static final int MESSAGE_TIMEOUT = 2; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 215 | static final int MESSAGE_SAVE_POLICY_FILE = 3; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 216 | static final int MESSAGE_SEND_RANKING_UPDATE = 4; |
| 217 | static final int MESSAGE_LISTENER_HINTS_CHANGED = 5; |
| 218 | static final int MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED = 6; |
| 219 | |
| 220 | // ranking thread messages |
| 221 | private static final int MESSAGE_RECONSIDER_RANKING = 1000; |
| 222 | private static final int MESSAGE_RANKING_SORT = 1001; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 224 | static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 225 | static final int SHORT_DELAY = 2000; // 2 seconds |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 226 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 227 | static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 228 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 229 | static final long SNOOZE_UNTIL_UNSPECIFIED = -1; |
| 230 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 231 | 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] | 232 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 233 | static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION; |
Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 234 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 235 | static final boolean ENABLE_BLOCKED_TOASTS = true; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 236 | |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 237 | // When #matchesCallFilter is called from the ringer, wait at most |
| 238 | // 3s to resolve the contacts. This timeout is required since |
| 239 | // ContactsProvider might take a long time to start up. |
| 240 | // |
| 241 | // Return STARRED_CONTACT when the timeout is hit in order to avoid |
| 242 | // missed calls in ZEN mode "Important". |
| 243 | static final int MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS = 3000; |
| 244 | static final float MATCHES_CALL_FILTER_TIMEOUT_AFFINITY = |
| 245 | ValidateNotificationPeople.STARRED_CONTACT; |
| 246 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 247 | /** notification_enqueue status value for a newly enqueued notification. */ |
| 248 | private static final int EVENTLOG_ENQUEUE_STATUS_NEW = 0; |
| 249 | |
| 250 | /** notification_enqueue status value for an existing notification. */ |
| 251 | private static final int EVENTLOG_ENQUEUE_STATUS_UPDATE = 1; |
| 252 | |
| 253 | /** notification_enqueue status value for an ignored notification. */ |
| 254 | private static final int EVENTLOG_ENQUEUE_STATUS_IGNORED = 2; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 255 | private static final long MIN_PACKAGE_OVERRATE_LOG_INTERVAL = 5000; // milliseconds |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 256 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 257 | private static final long DELAY_FOR_ASSISTANT_TIME = 100; |
| 258 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 259 | private static final String ACTION_NOTIFICATION_TIMEOUT = |
| 260 | NotificationManagerService.class.getSimpleName() + ".TIMEOUT"; |
| 261 | private static final int REQUEST_CODE_TIMEOUT = 1; |
| 262 | private static final String SCHEME_TIMEOUT = "timeout"; |
| 263 | private static final String EXTRA_KEY = "key"; |
| 264 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 265 | private IActivityManager mAm; |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 266 | private IPackageManager mPackageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 267 | private PackageManager mPackageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 268 | AudioManager mAudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 269 | AudioManagerInternal mAudioManagerInternal; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 270 | @Nullable StatusBarManagerInternal mStatusBar; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 271 | Vibrator mVibrator; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 272 | private WindowManagerInternal mWindowManagerInternal; |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 273 | private AlarmManager mAlarmManager; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 274 | private ICompanionDeviceManager mCompanionManager; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 275 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | final IBinder mForegroundToken = new Binder(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 277 | private Handler mHandler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 278 | private final HandlerThread mRankingThread = new HandlerThread("ranker", |
| 279 | Process.THREAD_PRIORITY_BACKGROUND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 281 | private Light mNotificationLight; |
| 282 | Light mAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 283 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 284 | private long[] mFallbackVibrationPattern; |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 285 | private boolean mUseAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 286 | boolean mSystemReady; |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 287 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 288 | private boolean mDisableNotificationEffects; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 289 | private int mCallState; |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 290 | private String mSoundNotificationKey; |
| 291 | private String mVibrateNotificationKey; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 293 | private final SparseArray<ArraySet<ManagedServiceInfo>> mListenersDisablingEffects = |
| 294 | new SparseArray<ArraySet<ManagedServiceInfo>>(); |
| 295 | private List<ComponentName> mEffectsSuppressors = new ArrayList<ComponentName>(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 296 | private int mListenerHints; // right now, all hints are global |
John Spurlock | 8310410 | 2015-02-12 23:25:12 -0500 | [diff] [blame] | 297 | private int mInterruptionFilter = NotificationListenerService.INTERRUPTION_FILTER_UNKNOWN; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 298 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 299 | // for enabling and disabling notification pulse behavior |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 300 | private boolean mScreenOn = true; |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 301 | private boolean mInCall = false; |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 302 | private boolean mNotificationPulseEnabled; |
| 303 | |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 304 | // for generating notification tones in-call |
| 305 | private ToneGenerator mInCallToneGenerator; |
| 306 | private final Object mInCallToneGeneratorLock = new Object(); |
| 307 | |
Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 308 | // used as a mutex for access to all active notifications & listeners |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 309 | final Object mNotificationLock = new Object(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 310 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 311 | final ArrayList<NotificationRecord> mNotificationList = |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 312 | new ArrayList<NotificationRecord>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 313 | @GuardedBy("mNotificationLock") |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 314 | final ArrayMap<String, NotificationRecord> mNotificationsByKey = |
| 315 | new ArrayMap<String, NotificationRecord>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 316 | @GuardedBy("mNotificationLock") |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 317 | final ArrayList<NotificationRecord> mEnqueuedNotifications = new ArrayList<>(); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 318 | @GuardedBy("mNotificationLock") |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 319 | final ArrayMap<Integer, ArrayMap<String, String>> mAutobundledSummaries = new ArrayMap<>(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 320 | final ArrayList<ToastRecord> mToastQueue = new ArrayList<ToastRecord>(); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 321 | final ArrayMap<String, NotificationRecord> mSummaryByGroupKey = new ArrayMap<>(); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 322 | final PolicyAccess mPolicyAccess = new PolicyAccess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 324 | // The last key in this list owns the hardware. |
| 325 | ArrayList<String> mLights = new ArrayList<>(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 326 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 327 | private AppOpsManager mAppOps; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 328 | private UsageStatsManagerInternal mAppUsageStats; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 329 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 330 | private Archive mArchive; |
| 331 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 332 | // Persistent storage for notification policy |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 333 | private AtomicFile mPolicyFile; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 334 | |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 335 | private static final int DB_VERSION = 1; |
| 336 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 337 | private static final String TAG_NOTIFICATION_POLICY = "notification-policy"; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 338 | private static final String ATTR_VERSION = "version"; |
| 339 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 340 | private RankingHelper mRankingHelper; |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 341 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 342 | private final UserProfiles mUserProfiles = new UserProfiles(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 343 | private NotificationListeners mListeners; |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 344 | private NotificationAssistants mNotificationAssistants; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 345 | private ConditionProviders mConditionProviders; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 346 | private NotificationUsageStats mUsageStats; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 347 | |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 348 | private static final int MY_UID = Process.myUid(); |
| 349 | private static final int MY_PID = Process.myPid(); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 350 | private static final IBinder WHITELIST_TOKEN = new Binder(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 351 | private RankingHandler mRankingHandler; |
Chris Wren | c8673a8 | 2016-05-17 17:11:29 -0400 | [diff] [blame] | 352 | private long mLastOverRateLogTime; |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 353 | private float mMaxPackageEnqueueRate = DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE; |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 354 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 355 | private SnoozeHelper mSnoozeHelper; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 356 | private GroupHelper mGroupHelper; |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 357 | private boolean mIsTelevision; |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 358 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 359 | private static class Archive { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 360 | final int mBufferSize; |
| 361 | final ArrayDeque<StatusBarNotification> mBuffer; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 362 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 363 | public Archive(int size) { |
| 364 | mBufferSize = size; |
| 365 | mBuffer = new ArrayDeque<StatusBarNotification>(mBufferSize); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 366 | } |
Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 367 | |
Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 368 | public String toString() { |
| 369 | final StringBuilder sb = new StringBuilder(); |
| 370 | final int N = mBuffer.size(); |
| 371 | sb.append("Archive ("); |
| 372 | sb.append(N); |
| 373 | sb.append(" notification"); |
| 374 | sb.append((N==1)?")":"s)"); |
| 375 | return sb.toString(); |
| 376 | } |
| 377 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 378 | public void record(StatusBarNotification nr) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 379 | if (mBuffer.size() == mBufferSize) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 380 | mBuffer.removeFirst(); |
| 381 | } |
Daniel Sandler | 26b81d5 | 2013-05-20 20:56:43 -0400 | [diff] [blame] | 382 | |
| 383 | // We don't want to store the heavy bits of the notification in the archive, |
| 384 | // but other clients in the system process might be using the object, so we |
| 385 | // store a (lightened) copy. |
| 386 | mBuffer.addLast(nr.cloneLight()); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 387 | } |
| 388 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 389 | public Iterator<StatusBarNotification> descendingIterator() { |
| 390 | return mBuffer.descendingIterator(); |
| 391 | } |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 392 | |
| 393 | public StatusBarNotification[] getArray(int count) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 394 | if (count == 0) count = mBufferSize; |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 395 | final StatusBarNotification[] a |
| 396 | = new StatusBarNotification[Math.min(count, mBuffer.size())]; |
| 397 | Iterator<StatusBarNotification> iter = descendingIterator(); |
| 398 | int i=0; |
| 399 | while (iter.hasNext() && i < count) { |
| 400 | a[i++] = iter.next(); |
| 401 | } |
| 402 | return a; |
| 403 | } |
| 404 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 405 | } |
| 406 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 407 | private void readPolicyXml(InputStream stream, boolean forRestore) |
| 408 | throws XmlPullParserException, NumberFormatException, IOException { |
| 409 | final XmlPullParser parser = Xml.newPullParser(); |
| 410 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
| 411 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 412 | while (parser.next() != END_DOCUMENT) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 413 | mZenModeHelper.readXml(parser, forRestore); |
| 414 | mRankingHelper.readXml(parser, forRestore); |
| 415 | } |
| 416 | } |
| 417 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 418 | private void loadPolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 419 | if (DBG) Slog.d(TAG, "loadPolicyFile"); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 420 | synchronized (mPolicyFile) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 421 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 422 | FileInputStream infile = null; |
| 423 | try { |
| 424 | infile = mPolicyFile.openRead(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 425 | readPolicyXml(infile, false /*forRestore*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 426 | } catch (FileNotFoundException e) { |
| 427 | // No data yet |
| 428 | } catch (IOException e) { |
| 429 | Log.wtf(TAG, "Unable to read notification policy", e); |
| 430 | } catch (NumberFormatException e) { |
| 431 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 432 | } catch (XmlPullParserException e) { |
| 433 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 434 | } finally { |
| 435 | IoUtils.closeQuietly(infile); |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | public void savePolicyFile() { |
| 441 | mHandler.removeMessages(MESSAGE_SAVE_POLICY_FILE); |
| 442 | mHandler.sendEmptyMessage(MESSAGE_SAVE_POLICY_FILE); |
| 443 | } |
| 444 | |
| 445 | private void handleSavePolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 446 | if (DBG) Slog.d(TAG, "handleSavePolicyFile"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 447 | synchronized (mPolicyFile) { |
| 448 | final FileOutputStream stream; |
| 449 | try { |
| 450 | stream = mPolicyFile.startWrite(); |
| 451 | } catch (IOException e) { |
| 452 | Slog.w(TAG, "Failed to save policy file", e); |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | try { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 457 | writePolicyXml(stream, false /*forBackup*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 458 | mPolicyFile.finishWrite(stream); |
| 459 | } catch (IOException e) { |
| 460 | Slog.w(TAG, "Failed to save policy file, restoring backup", e); |
| 461 | mPolicyFile.failWrite(stream); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 462 | } |
| 463 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 464 | BackupManager.dataChanged(getContext().getPackageName()); |
| 465 | } |
| 466 | |
| 467 | private void writePolicyXml(OutputStream stream, boolean forBackup) throws IOException { |
| 468 | final XmlSerializer out = new FastXmlSerializer(); |
| 469 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
| 470 | out.startDocument(null, true); |
| 471 | out.startTag(null, TAG_NOTIFICATION_POLICY); |
| 472 | out.attribute(null, ATTR_VERSION, Integer.toString(DB_VERSION)); |
| 473 | mZenModeHelper.writeXml(out, forBackup); |
| 474 | mRankingHelper.writeXml(out, forBackup); |
| 475 | out.endTag(null, TAG_NOTIFICATION_POLICY); |
| 476 | out.endDocument(); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 477 | } |
| 478 | |
Chris Wren | 66189fc | 2015-06-25 14:04:33 -0400 | [diff] [blame] | 479 | /** Use this to check if a package can post a notification or toast. */ |
| 480 | private boolean checkNotificationOp(String pkg, int uid) { |
| 481 | return mAppOps.checkOp(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg) |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 482 | == AppOpsManager.MODE_ALLOWED && !isPackageSuspendedForUser(pkg, uid); |
Chris Wren | 66189fc | 2015-06-25 14:04:33 -0400 | [diff] [blame] | 483 | } |
| 484 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | private static final class ToastRecord |
| 486 | { |
| 487 | final int pid; |
| 488 | final String pkg; |
| 489 | final ITransientNotification callback; |
| 490 | int duration; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 491 | Binder token; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 493 | ToastRecord(int pid, String pkg, ITransientNotification callback, int duration, |
| 494 | Binder token) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | this.pid = pid; |
| 496 | this.pkg = pkg; |
| 497 | this.callback = callback; |
| 498 | this.duration = duration; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 499 | this.token = token; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | void update(int duration) { |
| 503 | this.duration = duration; |
| 504 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 505 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 506 | void dump(PrintWriter pw, String prefix, DumpFilter filter) { |
| 507 | if (filter != null && !filter.matches(pkg)) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | pw.println(prefix + this); |
| 509 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 510 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | @Override |
| 512 | public final String toString() |
| 513 | { |
| 514 | return "ToastRecord{" |
| 515 | + Integer.toHexString(System.identityHashCode(this)) |
| 516 | + " pkg=" + pkg |
| 517 | + " callback=" + callback |
| 518 | + " duration=" + duration; |
| 519 | } |
| 520 | } |
| 521 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 522 | private final NotificationDelegate mNotificationDelegate = new NotificationDelegate() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 524 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | public void onSetDisabled(int status) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 526 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 527 | mDisableNotificationEffects = |
| 528 | (status & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 529 | if (disableNotificationEffects(null) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | // cancel whatever's going on |
| 531 | long identity = Binder.clearCallingIdentity(); |
| 532 | try { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 533 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 534 | if (player != null) { |
| 535 | player.stopAsync(); |
| 536 | } |
| 537 | } catch (RemoteException e) { |
| 538 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 539 | Binder.restoreCallingIdentity(identity); |
| 540 | } |
| 541 | |
| 542 | identity = Binder.clearCallingIdentity(); |
| 543 | try { |
| 544 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 545 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | Binder.restoreCallingIdentity(identity); |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 552 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 553 | public void onClearAll(int callingUid, int callingPid, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 554 | synchronized (mNotificationLock) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 555 | cancelAllLocked(callingUid, callingPid, userId, REASON_CANCEL_ALL, null, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 556 | /*includeCurrentProfiles*/ true); |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 557 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | } |
| 559 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 560 | @Override |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 561 | public void onNotificationClick(int callingUid, int callingPid, String key) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 562 | synchronized (mNotificationLock) { |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 563 | NotificationRecord r = mNotificationsByKey.get(key); |
| 564 | if (r == null) { |
| 565 | Log.w(TAG, "No notification with key: " + key); |
| 566 | return; |
| 567 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 568 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 569 | MetricsLogger.action(r.getLogMaker(now) |
| 570 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
| 571 | .setType(MetricsEvent.TYPE_ACTION)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 572 | EventLogTags.writeNotificationClicked(key, |
| 573 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
| 574 | |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 575 | StatusBarNotification sbn = r.sbn; |
| 576 | cancelNotification(callingUid, callingPid, sbn.getPackageName(), sbn.getTag(), |
| 577 | sbn.getId(), Notification.FLAG_AUTO_CANCEL, |
| 578 | Notification.FLAG_FOREGROUND_SERVICE, false, r.getUserId(), |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 579 | REASON_CLICK, null); |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 580 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 583 | @Override |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 584 | public void onNotificationActionClick(int callingUid, int callingPid, String key, |
| 585 | int actionIndex) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 586 | synchronized (mNotificationLock) { |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 587 | NotificationRecord r = mNotificationsByKey.get(key); |
| 588 | if (r == null) { |
| 589 | Log.w(TAG, "No notification with key: " + key); |
| 590 | return; |
| 591 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 592 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 593 | MetricsLogger.action(r.getLogMaker(now) |
| 594 | .setCategory(MetricsEvent.NOTIFICATION_ITEM_ACTION) |
| 595 | .setType(MetricsEvent.TYPE_ACTION) |
| 596 | .setSubtype(actionIndex)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 597 | EventLogTags.writeNotificationActionClicked(key, actionIndex, |
| 598 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 599 | // TODO: Log action click via UsageStats. |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 604 | public void onNotificationClear(int callingUid, int callingPid, |
| 605 | String pkg, String tag, int id, int userId) { |
| 606 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 607 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 608 | true, userId, REASON_CANCEL, null); |
Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 609 | } |
| 610 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 611 | @Override |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 612 | public void onPanelRevealed(boolean clearEffects, int items) { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 613 | MetricsLogger.visible(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Chris Wren | 621933f | 2017-06-14 15:59:03 -0400 | [diff] [blame] | 614 | MetricsLogger.histogram(getContext(), "note_load", items); |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 615 | EventLogTags.writeNotificationPanelRevealed(items); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 616 | if (clearEffects) { |
| 617 | clearEffects(); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | @Override |
| 622 | public void onPanelHidden() { |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 623 | MetricsLogger.hidden(getContext(), MetricsEvent.NOTIFICATION_PANEL); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 624 | EventLogTags.writeNotificationPanelHidden(); |
| 625 | } |
| 626 | |
| 627 | @Override |
| 628 | public void clearEffects() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 629 | synchronized (mNotificationLock) { |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 630 | if (DBG) Slog.d(TAG, "clearEffects"); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 631 | clearSoundLocked(); |
| 632 | clearVibrateLocked(); |
| 633 | clearLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | } |
| 635 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 636 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 637 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 638 | 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] | 639 | int uid, int initialPid, String message, int userId) { |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 640 | Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id |
| 641 | + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")"); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 642 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, 0, false, userId, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 643 | REASON_ERROR, null); |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 644 | long ident = Binder.clearCallingIdentity(); |
| 645 | try { |
Christopher Tate | 8aa8fe1 | 2017-01-20 17:50:32 -0800 | [diff] [blame] | 646 | ActivityManager.getService().crashApplication(uid, initialPid, pkg, -1, |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 647 | "Bad notification posted from package " + pkg |
| 648 | + ": " + message); |
| 649 | } catch (RemoteException e) { |
| 650 | } |
| 651 | Binder.restoreCallingIdentity(ident); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 652 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 653 | |
| 654 | @Override |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 655 | public void onNotificationVisibilityChanged(NotificationVisibility[] newlyVisibleKeys, |
| 656 | NotificationVisibility[] noLongerVisibleKeys) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 657 | synchronized (mNotificationLock) { |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 658 | for (NotificationVisibility nv : newlyVisibleKeys) { |
| 659 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 660 | if (r == null) continue; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 661 | r.setVisibility(true, nv.rank); |
| 662 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 663 | } |
| 664 | // Note that we might receive this event after notifications |
| 665 | // have already left the system, e.g. after dismissing from the |
| 666 | // shade. Hence not finding notifications in |
| 667 | // mNotificationsByKey is not an exceptional condition. |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 668 | for (NotificationVisibility nv : noLongerVisibleKeys) { |
| 669 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 670 | if (r == null) continue; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 671 | r.setVisibility(false, nv.rank); |
| 672 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 673 | } |
| 674 | } |
Christoph Studer | 92b389d | 2014-04-01 18:44:40 +0200 | [diff] [blame] | 675 | } |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 676 | |
| 677 | @Override |
| 678 | public void onNotificationExpansionChanged(String key, |
| 679 | boolean userAction, boolean expanded) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 680 | synchronized (mNotificationLock) { |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 681 | NotificationRecord r = mNotificationsByKey.get(key); |
| 682 | if (r != null) { |
| 683 | r.stats.onExpansionChanged(userAction, expanded); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 684 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 685 | MetricsLogger.action(r.getLogMaker(now) |
| 686 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
| 687 | .setType(MetricsEvent.TYPE_DETAIL)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 688 | EventLogTags.writeNotificationExpansion(key, |
| 689 | userAction ? 1 : 0, expanded ? 1 : 0, |
| 690 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | }; |
| 695 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 696 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 697 | private void clearSoundLocked() { |
| 698 | mSoundNotificationKey = null; |
| 699 | long identity = Binder.clearCallingIdentity(); |
| 700 | try { |
| 701 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 702 | if (player != null) { |
| 703 | player.stopAsync(); |
| 704 | } |
| 705 | } catch (RemoteException e) { |
| 706 | } finally { |
| 707 | Binder.restoreCallingIdentity(identity); |
| 708 | } |
| 709 | } |
| 710 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 711 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 712 | private void clearVibrateLocked() { |
| 713 | mVibrateNotificationKey = null; |
| 714 | long identity = Binder.clearCallingIdentity(); |
| 715 | try { |
| 716 | mVibrator.cancel(); |
| 717 | } finally { |
| 718 | Binder.restoreCallingIdentity(identity); |
| 719 | } |
| 720 | } |
| 721 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 722 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 723 | private void clearLightsLocked() { |
| 724 | // light |
| 725 | mLights.clear(); |
| 726 | updateLightsLocked(); |
| 727 | } |
| 728 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 729 | private final BroadcastReceiver mNotificationTimeoutReceiver = new BroadcastReceiver() { |
| 730 | @Override |
| 731 | public void onReceive(Context context, Intent intent) { |
| 732 | String action = intent.getAction(); |
| 733 | if (action == null) { |
| 734 | return; |
| 735 | } |
| 736 | if (ACTION_NOTIFICATION_TIMEOUT.equals(action)) { |
| 737 | final NotificationRecord record; |
| 738 | synchronized (mNotificationLock) { |
| 739 | record = findNotificationByKeyLocked(intent.getStringExtra(EXTRA_KEY)); |
| 740 | } |
| 741 | if (record != null) { |
| 742 | cancelNotification(record.sbn.getUid(), record.sbn.getInitialPid(), |
| 743 | record.sbn.getPackageName(), record.sbn.getTag(), |
| 744 | record.sbn.getId(), 0, |
| 745 | Notification.FLAG_FOREGROUND_SERVICE, true, record.getUserId(), |
| 746 | REASON_TIMEOUT, null); |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | }; |
| 751 | |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 752 | private final BroadcastReceiver mPackageIntentReceiver = new BroadcastReceiver() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | @Override |
| 754 | public void onReceive(Context context, Intent intent) { |
| 755 | String action = intent.getAction(); |
Dianne Hackborn | 29cd7f1 | 2015-01-08 10:37:05 -0800 | [diff] [blame] | 756 | if (action == null) { |
| 757 | return; |
| 758 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 759 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 760 | boolean queryRestart = false; |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 761 | boolean queryRemove = false; |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 762 | boolean packageChanged = false; |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 763 | boolean cancelNotifications = true; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 764 | int reason = REASON_PACKAGE_CHANGED; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 765 | |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 766 | if (action.equals(Intent.ACTION_PACKAGE_ADDED) |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 767 | || (queryRemove=action.equals(Intent.ACTION_PACKAGE_REMOVED)) |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 768 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 769 | || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED)) |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 770 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 771 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE) |
| 772 | || action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 773 | int changeUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 774 | UserHandle.USER_ALL); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 775 | String pkgList[] = null; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 776 | int uidList[] = null; |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 777 | boolean removingPackage = queryRemove && |
| 778 | !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 779 | if (DBG) Slog.i(TAG, "action=" + action + " removing=" + removingPackage); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 780 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 781 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 782 | uidList = intent.getIntArrayExtra(Intent.EXTRA_CHANGED_UID_LIST); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 783 | } else if (action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
| 784 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 785 | reason = REASON_PACKAGE_SUSPENDED; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 786 | } else if (queryRestart) { |
| 787 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 788 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 789 | } else { |
| 790 | Uri uri = intent.getData(); |
| 791 | if (uri == null) { |
| 792 | return; |
| 793 | } |
| 794 | String pkgName = uri.getSchemeSpecificPart(); |
| 795 | if (pkgName == null) { |
| 796 | return; |
| 797 | } |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 798 | if (packageChanged) { |
| 799 | // We cancel notifications for packages which have just been disabled |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 800 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 801 | final int enabled = mPackageManager.getApplicationEnabledSetting( |
| 802 | pkgName, |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 803 | changeUserId != UserHandle.USER_ALL ? changeUserId : |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 804 | UserHandle.USER_SYSTEM); |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 805 | if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 806 | || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 807 | cancelNotifications = false; |
| 808 | } |
| 809 | } catch (IllegalArgumentException e) { |
| 810 | // Package doesn't exist; probably racing with uninstall. |
| 811 | // cancelNotifications is already true, so nothing to do here. |
| 812 | if (DBG) { |
| 813 | Slog.i(TAG, "Exception trying to look up app enabled setting", e); |
| 814 | } |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 815 | } catch (RemoteException e) { |
| 816 | // Failed to talk to PackageManagerService Should never happen! |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 817 | } |
| 818 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 819 | pkgList = new String[]{pkgName}; |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 820 | uidList = new int[] {intent.getIntExtra(Intent.EXTRA_UID, -1)}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 822 | if (pkgList != null && (pkgList.length > 0)) { |
| 823 | for (String pkgName : pkgList) { |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 824 | if (cancelNotifications) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 825 | cancelAllNotificationsInt(MY_UID, MY_PID, pkgName, null, 0, 0, |
| 826 | !queryRestart, changeUserId, reason, null); |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 827 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 828 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | } |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 830 | mListeners.onPackagesChanged(removingPackage, pkgList); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 831 | mNotificationAssistants.onPackagesChanged(removingPackage, pkgList); |
Julia Reynolds | 6434eb2 | 2016-08-08 17:19:26 -0400 | [diff] [blame] | 832 | mConditionProviders.onPackagesChanged(removingPackage, pkgList); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 833 | mRankingHelper.onPackagesChanged(removingPackage, changeUserId, pkgList, uidList); |
| 834 | savePolicyFile(); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | }; |
| 838 | |
| 839 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 840 | @Override |
| 841 | public void onReceive(Context context, Intent intent) { |
| 842 | String action = intent.getAction(); |
| 843 | |
| 844 | if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 845 | // Keep track of screen on/off state, but do not turn off the notification light |
| 846 | // until user passes through the lock screen or views the notification. |
| 847 | mScreenOn = true; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 848 | updateNotificationPulse(); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 849 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 850 | mScreenOn = false; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 851 | updateNotificationPulse(); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 852 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
John Spurlock | 5d2eeb1 | 2014-01-16 10:46:36 -0500 | [diff] [blame] | 853 | mInCall = TelephonyManager.EXTRA_STATE_OFFHOOK |
| 854 | .equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE)); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 855 | updateNotificationPulse(); |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 856 | synchronized (mInCallToneGeneratorLock) { |
| 857 | if (mInCall) { |
| 858 | if (mInCallToneGenerator == null) { |
| 859 | int relativeToneVolume = getContext().getResources().getInteger( |
| 860 | R.integer.config_inCallNotificationVolumeRelative); |
| 861 | if (relativeToneVolume < ToneGenerator.MIN_VOLUME |
| 862 | || relativeToneVolume > ToneGenerator.MAX_VOLUME) { |
| 863 | relativeToneVolume = ToneGenerator.MAX_VOLUME; |
| 864 | } |
| 865 | try { |
| 866 | mInCallToneGenerator = new ToneGenerator( |
| 867 | AudioManager.STREAM_VOICE_CALL, relativeToneVolume); |
| 868 | } catch (RuntimeException e) { |
| 869 | Log.e(TAG, "Error creating local tone generator: " + e); |
| 870 | mInCallToneGenerator = null; |
| 871 | } |
| 872 | } |
| 873 | } else { |
| 874 | if (mInCallToneGenerator != null) { |
| 875 | mInCallToneGenerator.release(); |
| 876 | mInCallToneGenerator = null; |
| 877 | } |
| 878 | } |
| 879 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 880 | } else if (action.equals(Intent.ACTION_USER_STOPPED)) { |
| 881 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 882 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 883 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 884 | REASON_USER_STOPPED, null); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 885 | } |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 886 | } else if (action.equals(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)) { |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 887 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 888 | if (userHandle >= 0) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 889 | cancelAllNotificationsInt(MY_UID, MY_PID, null, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 890 | REASON_PROFILE_TURNED_OFF, null); |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 891 | } |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 892 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { |
| 893 | // turn off LED when user passes through lock screen |
| 894 | mNotificationLight.turnOff(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 895 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 896 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 897 | // reload per-user settings |
| 898 | mSettingsObserver.update(null); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 899 | mUserProfiles.updateCache(context); |
Christoph Studer | b53dfd4 | 2014-09-12 14:45:59 +0200 | [diff] [blame] | 900 | // Refresh managed services |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 901 | mConditionProviders.onUserSwitched(user); |
| 902 | mListeners.onUserSwitched(user); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 903 | mNotificationAssistants.onUserSwitched(user); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 904 | mZenModeHelper.onUserSwitched(user); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 905 | } else if (action.equals(Intent.ACTION_USER_ADDED)) { |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 906 | mUserProfiles.updateCache(context); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 907 | } else if (action.equals(Intent.ACTION_USER_REMOVED)) { |
| 908 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 909 | mZenModeHelper.onUserRemoved(user); |
Julia Reynolds | 2e9bf5f | 2017-05-03 13:23:30 -0400 | [diff] [blame] | 910 | mRankingHelper.onUserRemoved(user); |
| 911 | savePolicyFile(); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 912 | } else if (action.equals(Intent.ACTION_USER_UNLOCKED)) { |
| 913 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 914 | mConditionProviders.onUserUnlocked(user); |
| 915 | mListeners.onUserUnlocked(user); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 916 | mNotificationAssistants.onUserUnlocked(user); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 917 | mZenModeHelper.onUserUnlocked(user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | }; |
| 921 | |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 922 | private final class SettingsObserver extends ContentObserver { |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 923 | private final Uri NOTIFICATION_BADGING_URI |
| 924 | = Settings.Secure.getUriFor(Settings.Secure.NOTIFICATION_BADGING); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 925 | private final Uri NOTIFICATION_LIGHT_PULSE_URI |
| 926 | = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 927 | private final Uri NOTIFICATION_RATE_LIMIT_URI |
| 928 | = Settings.Global.getUriFor(Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 929 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 930 | SettingsObserver(Handler handler) { |
| 931 | super(handler); |
| 932 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 933 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 934 | void observe() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 935 | ContentResolver resolver = getContext().getContentResolver(); |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 936 | resolver.registerContentObserver(NOTIFICATION_BADGING_URI, |
| 937 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 938 | resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 939 | false, this, UserHandle.USER_ALL); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 940 | resolver.registerContentObserver(NOTIFICATION_RATE_LIMIT_URI, |
| 941 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 942 | update(null); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 945 | @Override public void onChange(boolean selfChange, Uri uri) { |
| 946 | update(uri); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 949 | public void update(Uri uri) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 950 | ContentResolver resolver = getContext().getContentResolver(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 951 | if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { |
zhoulei | 7e37697 | 2017-05-17 18:41:25 +0800 | [diff] [blame] | 952 | boolean pulseEnabled = Settings.System.getIntForUser(resolver, |
| 953 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0, UserHandle.USER_CURRENT) != 0; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 954 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 955 | mNotificationPulseEnabled = pulseEnabled; |
| 956 | updateNotificationPulse(); |
| 957 | } |
| 958 | } |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 959 | if (uri == null || NOTIFICATION_RATE_LIMIT_URI.equals(uri)) { |
| 960 | mMaxPackageEnqueueRate = Settings.Global.getFloat(resolver, |
| 961 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, mMaxPackageEnqueueRate); |
| 962 | } |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 963 | if (uri == null || NOTIFICATION_BADGING_URI.equals(uri)) { |
| 964 | mRankingHelper.updateBadgingEnabled(); |
| 965 | } |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 966 | } |
| 967 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 968 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 969 | private SettingsObserver mSettingsObserver; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 970 | private ZenModeHelper mZenModeHelper; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 971 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 972 | static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) { |
| 973 | int[] ar = r.getIntArray(resid); |
| 974 | if (ar == null) { |
| 975 | return def; |
| 976 | } |
| 977 | final int len = ar.length > maxlen ? maxlen : ar.length; |
| 978 | long[] out = new long[len]; |
| 979 | for (int i=0; i<len; i++) { |
| 980 | out[i] = ar[i]; |
| 981 | } |
| 982 | return out; |
| 983 | } |
| 984 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 985 | public NotificationManagerService(Context context) { |
| 986 | super(context); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 987 | Notification.processWhitelistToken = WHITELIST_TOKEN; |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 988 | } |
| 989 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 990 | // TODO - replace these methods with a single VisibleForTesting constructor |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 991 | @VisibleForTesting |
| 992 | void setAudioManager(AudioManager audioMananger) { |
| 993 | mAudioManager = audioMananger; |
| 994 | } |
| 995 | |
| 996 | @VisibleForTesting |
| 997 | void setVibrator(Vibrator vibrator) { |
| 998 | mVibrator = vibrator; |
| 999 | } |
| 1000 | |
| 1001 | @VisibleForTesting |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1002 | void setLights(Light light) { |
| 1003 | mNotificationLight = light; |
| 1004 | mAttentionLight = light; |
Julia Reynolds | 033a412 | 2017-01-31 16:50:38 -0500 | [diff] [blame] | 1005 | mNotificationPulseEnabled = true; |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | @VisibleForTesting |
| 1009 | void setScreenOn(boolean on) { |
| 1010 | mScreenOn = on; |
| 1011 | } |
| 1012 | |
| 1013 | @VisibleForTesting |
| 1014 | void addNotification(NotificationRecord r) { |
| 1015 | mNotificationList.add(r); |
| 1016 | mNotificationsByKey.put(r.sbn.getKey(), r); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1017 | if (r.sbn.isGroup()) { |
| 1018 | mSummaryByGroupKey.put(r.getGroupKey(), r); |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | @VisibleForTesting |
| 1023 | void addEnqueuedNotification(NotificationRecord r) { |
| 1024 | mEnqueuedNotifications.add(r); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | @VisibleForTesting |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 1028 | void setSystemReady(boolean systemReady) { |
| 1029 | mSystemReady = systemReady; |
| 1030 | } |
| 1031 | |
| 1032 | @VisibleForTesting |
| 1033 | void setHandler(Handler handler) { |
| 1034 | mHandler = handler; |
| 1035 | } |
| 1036 | |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1037 | @VisibleForTesting |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 1038 | void setFallbackVibrationPattern(long[] vibrationPattern) { |
| 1039 | mFallbackVibrationPattern = vibrationPattern; |
Chris Wren | d405431 | 2016-06-24 17:07:40 -0400 | [diff] [blame] | 1040 | } |
| 1041 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1042 | @VisibleForTesting |
| 1043 | void setPackageManager(IPackageManager packageManager) { |
| 1044 | mPackageManager = packageManager; |
| 1045 | } |
| 1046 | |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1047 | @VisibleForTesting |
| 1048 | void setRankingHelper(RankingHelper rankingHelper) { |
| 1049 | mRankingHelper = rankingHelper; |
| 1050 | } |
| 1051 | |
| 1052 | @VisibleForTesting |
| 1053 | void setIsTelevision(boolean isTelevision) { |
| 1054 | mIsTelevision = isTelevision; |
| 1055 | } |
| 1056 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1057 | // TODO: Tests should call onStart instead once the methods above are removed. |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1058 | @VisibleForTesting |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1059 | void init(Looper looper, IPackageManager packageManager, PackageManager packageManagerClient, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1060 | LightsManager lightsManager, NotificationListeners notificationListeners, |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1061 | ICompanionDeviceManager companionManager, SnoozeHelper snoozeHelper, |
| 1062 | NotificationUsageStats usageStats) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1063 | Resources resources = getContext().getResources(); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 1064 | mMaxPackageEnqueueRate = Settings.Global.getFloat(getContext().getContentResolver(), |
| 1065 | Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, |
| 1066 | DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE); |
| 1067 | |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1068 | mAm = ActivityManager.getService(); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1069 | mPackageManager = packageManager; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1070 | mPackageManagerClient = packageManagerClient; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1071 | mAppOps = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE); |
| 1072 | mVibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 1073 | mAppUsageStats = LocalServices.getService(UsageStatsManagerInternal.class); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1074 | mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1075 | mCompanionManager = companionManager; |
San Mehat | 3ee1317 | 2010-02-04 20:54:43 -0800 | [diff] [blame] | 1076 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1077 | mHandler = new WorkerHandler(looper); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1078 | mRankingThread.start(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1079 | String[] extractorNames; |
| 1080 | try { |
| 1081 | extractorNames = resources.getStringArray(R.array.config_notificationSignalExtractors); |
| 1082 | } catch (Resources.NotFoundException e) { |
| 1083 | extractorNames = new String[0]; |
| 1084 | } |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1085 | mUsageStats = usageStats; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1086 | mRankingHandler = new RankingHandlerWorker(mRankingThread.getLooper()); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1087 | mRankingHelper = new RankingHelper(getContext(), |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 1088 | getContext().getPackageManager(), |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 1089 | mRankingHandler, |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 1090 | mUsageStats, |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1091 | extractorNames); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1092 | mConditionProviders = new ConditionProviders(getContext(), mHandler, mUserProfiles); |
| 1093 | mZenModeHelper = new ZenModeHelper(getContext(), mHandler.getLooper(), mConditionProviders); |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 1094 | mZenModeHelper.addCallback(new ZenModeHelper.Callback() { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1095 | @Override |
| 1096 | public void onConfigChanged() { |
| 1097 | savePolicyFile(); |
| 1098 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1099 | |
| 1100 | @Override |
| 1101 | void onZenModeChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1102 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED); |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1103 | getContext().sendBroadcastAsUser( |
Jason Monk | 6350674 | 2015-12-16 12:06:51 -0500 | [diff] [blame] | 1104 | new Intent(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL) |
| 1105 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT), |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 1106 | UserHandle.ALL, android.Manifest.permission.MANAGE_NOTIFICATIONS); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1107 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1108 | updateInterruptionFilterLocked(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1109 | } |
| 1110 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1111 | |
| 1112 | @Override |
| 1113 | void onPolicyChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1114 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED); |
| 1115 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1116 | }); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1117 | mSnoozeHelper = snoozeHelper; |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 1118 | mGroupHelper = new GroupHelper(new GroupHelper.Callback() { |
| 1119 | @Override |
| 1120 | public void addAutoGroup(String key) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1121 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 1122 | addAutogroupKeyLocked(key); |
| 1123 | } |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 1124 | mRankingHandler.requestSort(false); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | @Override |
| 1128 | public void removeAutoGroup(String key) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1129 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 1130 | removeAutogroupKeyLocked(key); |
| 1131 | } |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 1132 | mRankingHandler.requestSort(false); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | @Override |
| 1136 | public void addAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 1137 | createAutoGroupSummary(userId, pkg, triggeringKey); |
| 1138 | } |
| 1139 | |
| 1140 | @Override |
| 1141 | public void removeAutoGroupSummary(int userId, String pkg) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1142 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 1143 | clearAutogroupSummaryLocked(userId, pkg); |
| 1144 | } |
| 1145 | } |
| 1146 | }); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 1147 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1148 | final File systemDir = new File(Environment.getDataDirectory(), "system"); |
| 1149 | mPolicyFile = new AtomicFile(new File(systemDir, "notification_policy.xml")); |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1150 | |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1151 | loadPolicyFile(); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1152 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1153 | // This is a ManagedServices object that keeps track of the listeners. |
| 1154 | mListeners = notificationListeners; |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1155 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 1156 | // This is a MangedServices object that keeps track of the assistant. |
| 1157 | mNotificationAssistants = new NotificationAssistants(); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1158 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1159 | mStatusBar = getLocalService(StatusBarManagerInternal.class); |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 1160 | if (mStatusBar != null) { |
| 1161 | mStatusBar.setNotificationDelegate(mNotificationDelegate); |
| 1162 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1164 | mNotificationLight = lightsManager.getLight(LightsManager.LIGHT_ID_NOTIFICATIONS); |
| 1165 | mAttentionLight = lightsManager.getLight(LightsManager.LIGHT_ID_ATTENTION); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1166 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1167 | mFallbackVibrationPattern = getLongArray(resources, |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 1168 | R.array.config_notificationFallbackVibePattern, |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1169 | VIBRATE_PATTERN_MAXLEN, |
| 1170 | DEFAULT_VIBRATE_PATTERN); |
| 1171 | |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 1172 | mUseAttentionLight = resources.getBoolean(R.bool.config_useAttentionLight); |
| 1173 | |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1174 | // Don't start allowing notifications until the setup wizard has run once. |
| 1175 | // After that, including subsequent boots, init with notifications turned on. |
| 1176 | // This works on the first boot because the setup wizard will toggle this |
| 1177 | // 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] | 1178 | if (0 == Settings.Global.getInt(getContext().getContentResolver(), |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1179 | Settings.Global.DEVICE_PROVISIONED, 0)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1180 | mDisableNotificationEffects = true; |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1181 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1182 | mZenModeHelper.initZenMode(); |
John Spurlock | f370177 | 2015-02-12 13:29:37 -0500 | [diff] [blame] | 1183 | mInterruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1184 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1185 | mUserProfiles.updateCache(getContext()); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 1186 | listenForCallState(); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1187 | |
Mike Lockwood | 35e16bf | 2010-11-30 19:53:36 -0500 | [diff] [blame] | 1188 | // register for various Intents |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | IntentFilter filter = new IntentFilter(); |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1190 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 1191 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1192 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1193 | filter.addAction(Intent.ACTION_USER_PRESENT); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1194 | filter.addAction(Intent.ACTION_USER_STOPPED); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1195 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1196 | filter.addAction(Intent.ACTION_USER_ADDED); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1197 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1198 | filter.addAction(Intent.ACTION_USER_UNLOCKED); |
Rubin Xu | e95057a | 2016-04-01 16:49:25 +0100 | [diff] [blame] | 1199 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1200 | getContext().registerReceiver(mIntentReceiver, filter); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1201 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1202 | IntentFilter pkgFilter = new IntentFilter(); |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1203 | pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1204 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
Daniel Sandler | aac0eb0 | 2011-08-06 22:51:56 -0400 | [diff] [blame] | 1205 | pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1206 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 1207 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 1208 | pkgFilter.addDataScheme("package"); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1209 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, pkgFilter, null, |
| 1210 | null); |
| 1211 | |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1212 | IntentFilter suspendedPkgFilter = new IntentFilter(); |
| 1213 | suspendedPkgFilter.addAction(Intent.ACTION_PACKAGES_SUSPENDED); |
| 1214 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, |
| 1215 | suspendedPkgFilter, null, null); |
| 1216 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1217 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1218 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, sdFilter, null, |
| 1219 | null); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1220 | |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 1221 | IntentFilter timeoutFilter = new IntentFilter(ACTION_NOTIFICATION_TIMEOUT); |
| 1222 | timeoutFilter.addDataScheme(SCHEME_TIMEOUT); |
| 1223 | getContext().registerReceiver(mNotificationTimeoutReceiver, timeoutFilter); |
| 1224 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1225 | mSettingsObserver = new SettingsObserver(mHandler); |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 1226 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 1227 | mArchive = new Archive(resources.getInteger( |
| 1228 | R.integer.config_notificationServiceArchiveSize)); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 1229 | |
| 1230 | mIsTelevision = mPackageManagerClient.hasSystemFeature(FEATURE_LEANBACK) |
| 1231 | || mPackageManagerClient.hasSystemFeature(FEATURE_TELEVISION); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1232 | } |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 1233 | |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1234 | @Override |
| 1235 | public void onStart() { |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 1236 | SnoozeHelper snoozeHelper = new SnoozeHelper(getContext(), new SnoozeHelper.Callback() { |
| 1237 | @Override |
| 1238 | public void repost(int userId, NotificationRecord r) { |
| 1239 | try { |
| 1240 | if (DBG) { |
| 1241 | Slog.d(TAG, "Reposting " + r.getKey()); |
| 1242 | } |
| 1243 | enqueueNotificationInternal(r.sbn.getPackageName(), r.sbn.getOpPkg(), |
| 1244 | r.sbn.getUid(), r.sbn.getInitialPid(), r.sbn.getTag(), r.sbn.getId(), |
| 1245 | r.sbn.getNotification(), userId); |
| 1246 | } catch (Exception e) { |
| 1247 | Slog.e(TAG, "Cannot un-snooze notification", e); |
| 1248 | } |
| 1249 | } |
| 1250 | }, mUserProfiles); |
| 1251 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1252 | init(Looper.myLooper(), AppGlobals.getPackageManager(), getContext().getPackageManager(), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1253 | getLocalService(LightsManager.class), new NotificationListeners(), |
Geoffrey Pitsch | d5bcf21 | 2017-06-01 15:45:35 -0400 | [diff] [blame] | 1254 | null, snoozeHelper, new NotificationUsageStats(getContext())); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1255 | publishBinderService(Context.NOTIFICATION_SERVICE, mService); |
| 1256 | publishLocalService(NotificationManagerInternal.class, mInternalService); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1257 | } |
| 1258 | |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1259 | private void sendRegisteredOnlyBroadcast(String action) { |
| 1260 | getContext().sendBroadcastAsUser(new Intent(action) |
| 1261 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), UserHandle.ALL, null); |
| 1262 | } |
| 1263 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1264 | @Override |
| 1265 | public void onBootPhase(int phase) { |
| 1266 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 1267 | // no beeping until we're basically done booting |
| 1268 | mSystemReady = true; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1269 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1270 | // Grab our optional AudioService |
| 1271 | mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1272 | mAudioManagerInternal = getLocalService(AudioManagerInternal.class); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1273 | mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1274 | mZenModeHelper.onSystemReady(); |
Adam Lesinski | a6db4ab | 2014-03-24 12:31:45 -0700 | [diff] [blame] | 1275 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 1276 | // This observer will force an update when observe is called, causing us to |
| 1277 | // bind to listener services. |
| 1278 | mSettingsObserver.observe(); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1279 | mListeners.onBootPhaseAppsCanStart(); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 1280 | mNotificationAssistants.onBootPhaseAppsCanStart(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1281 | mConditionProviders.onBootPhaseAppsCanStart(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1282 | } |
| 1283 | } |
| 1284 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1285 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1286 | private void updateListenerHintsLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1287 | final int hints = calculateHints(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1288 | if (hints == mListenerHints) return; |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1289 | ZenLog.traceListenerHintsChanged(mListenerHints, hints, mEffectsSuppressors.size()); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1290 | mListenerHints = hints; |
| 1291 | scheduleListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 1292 | } |
| 1293 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1294 | @GuardedBy("mNotificationLock") |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1295 | private void updateEffectsSuppressorLocked() { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1296 | final long updatedSuppressedEffects = calculateSuppressedEffects(); |
| 1297 | if (updatedSuppressedEffects == mZenModeHelper.getSuppressedEffects()) return; |
| 1298 | final List<ComponentName> suppressors = getSuppressors(); |
| 1299 | ZenLog.traceEffectsSuppressorChanged(mEffectsSuppressors, suppressors, updatedSuppressedEffects); |
| 1300 | mEffectsSuppressors = suppressors; |
| 1301 | mZenModeHelper.setSuppressedEffects(updatedSuppressedEffects); |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1302 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1303 | } |
| 1304 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1305 | private void updateNotificationChannelInt(String pkg, int uid, NotificationChannel channel, |
| 1306 | boolean fromListener) { |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1307 | if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) { |
| 1308 | // cancel |
| 1309 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channel.getId(), 0, 0, true, |
| 1310 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, |
| 1311 | null); |
| 1312 | } |
Julia Reynolds | 6031533 | 2017-04-04 14:29:07 -0400 | [diff] [blame] | 1313 | mRankingHelper.updateNotificationChannel(pkg, uid, channel); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1314 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1315 | final NotificationChannel modifiedChannel = |
| 1316 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), false); |
| 1317 | |
| 1318 | if (!fromListener) { |
| 1319 | mListeners.notifyNotificationChannelChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1320 | pkg, UserHandle.getUserHandleForUid(uid), |
| 1321 | modifiedChannel, NOTIFICATION_CHANNEL_OR_GROUP_UPDATED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1322 | } |
| 1323 | |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 1324 | synchronized (mNotificationLock) { |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1325 | final int N = mNotificationList.size(); |
| 1326 | for (int i = N - 1; i >= 0; --i) { |
| 1327 | NotificationRecord r = mNotificationList.get(i); |
Julia Reynolds | a917a11 | 2017-03-21 11:09:14 -0400 | [diff] [blame] | 1328 | if (r.sbn.getPackageName().equals(pkg) |
| 1329 | && r.sbn.getUid() == uid |
| 1330 | && channel.getId() != null |
| 1331 | && channel.getId().equals(r.getChannel().getId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1332 | r.updateNotificationChannel(modifiedChannel); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1333 | } |
| 1334 | } |
| 1335 | } |
| 1336 | mRankingHandler.requestSort(true); |
| 1337 | savePolicyFile(); |
| 1338 | } |
| 1339 | |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 1340 | private ArrayList<ComponentName> getSuppressors() { |
| 1341 | ArrayList<ComponentName> names = new ArrayList<ComponentName>(); |
| 1342 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1343 | ArraySet<ManagedServiceInfo> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
| 1344 | |
| 1345 | for (ManagedServiceInfo info : serviceInfoList) { |
| 1346 | names.add(info.component); |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | return names; |
| 1351 | } |
| 1352 | |
| 1353 | private boolean removeDisabledHints(ManagedServiceInfo info) { |
| 1354 | return removeDisabledHints(info, 0); |
| 1355 | } |
| 1356 | |
| 1357 | private boolean removeDisabledHints(ManagedServiceInfo info, int hints) { |
| 1358 | boolean removed = false; |
| 1359 | |
| 1360 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1361 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 1362 | final ArraySet<ManagedServiceInfo> listeners = |
| 1363 | mListenersDisablingEffects.valueAt(i); |
| 1364 | |
| 1365 | if (hints == 0 || (hint & hints) == hint) { |
| 1366 | removed = removed || listeners.remove(info); |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | return removed; |
| 1371 | } |
| 1372 | |
| 1373 | private void addDisabledHints(ManagedServiceInfo info, int hints) { |
| 1374 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1375 | addDisabledHint(info, HINT_HOST_DISABLE_EFFECTS); |
| 1376 | } |
| 1377 | |
| 1378 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1379 | addDisabledHint(info, HINT_HOST_DISABLE_NOTIFICATION_EFFECTS); |
| 1380 | } |
| 1381 | |
| 1382 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1383 | addDisabledHint(info, HINT_HOST_DISABLE_CALL_EFFECTS); |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | private void addDisabledHint(ManagedServiceInfo info, int hint) { |
| 1388 | if (mListenersDisablingEffects.indexOfKey(hint) < 0) { |
| 1389 | mListenersDisablingEffects.put(hint, new ArraySet<ManagedServiceInfo>()); |
| 1390 | } |
| 1391 | |
| 1392 | ArraySet<ManagedServiceInfo> hintListeners = mListenersDisablingEffects.get(hint); |
| 1393 | hintListeners.add(info); |
| 1394 | } |
| 1395 | |
| 1396 | private int calculateHints() { |
| 1397 | int hints = 0; |
| 1398 | for (int i = mListenersDisablingEffects.size() - 1; i >= 0; --i) { |
| 1399 | int hint = mListenersDisablingEffects.keyAt(i); |
| 1400 | ArraySet<ManagedServiceInfo> serviceInfoList = mListenersDisablingEffects.valueAt(i); |
| 1401 | |
| 1402 | if (!serviceInfoList.isEmpty()) { |
| 1403 | hints |= hint; |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | return hints; |
| 1408 | } |
| 1409 | |
| 1410 | private long calculateSuppressedEffects() { |
| 1411 | int hints = calculateHints(); |
| 1412 | long suppressedEffects = 0; |
| 1413 | |
| 1414 | if ((hints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 1415 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_ALL; |
| 1416 | } |
| 1417 | |
| 1418 | if ((hints & HINT_HOST_DISABLE_NOTIFICATION_EFFECTS) != 0) { |
| 1419 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_NOTIFICATIONS; |
| 1420 | } |
| 1421 | |
| 1422 | if ((hints & HINT_HOST_DISABLE_CALL_EFFECTS) != 0) { |
| 1423 | suppressedEffects |= ZenModeHelper.SUPPRESSED_EFFECT_CALLS; |
| 1424 | } |
| 1425 | |
| 1426 | return suppressedEffects; |
| 1427 | } |
| 1428 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 1429 | @GuardedBy("mNotificationLock") |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1430 | private void updateInterruptionFilterLocked() { |
| 1431 | int interruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
| 1432 | if (interruptionFilter == mInterruptionFilter) return; |
| 1433 | mInterruptionFilter = interruptionFilter; |
| 1434 | scheduleInterruptionFilterChanged(interruptionFilter); |
| 1435 | } |
| 1436 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 1437 | @VisibleForTesting |
| 1438 | INotificationManager getBinderService() { |
| 1439 | return INotificationManager.Stub.asInterface(mService); |
| 1440 | } |
| 1441 | |
Geoffrey Pitsch | 415e454 | 2017-04-10 13:12:58 -0400 | [diff] [blame] | 1442 | @VisibleForTesting |
| 1443 | NotificationManagerInternal getInternalService() { |
| 1444 | return mInternalService; |
| 1445 | } |
| 1446 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1447 | private final IBinder mService = new INotificationManager.Stub() { |
| 1448 | // Toasts |
| 1449 | // ============================================================================ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1451 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1452 | public void enqueueToast(String pkg, ITransientNotification callback, int duration) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1453 | { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1454 | if (DBG) { |
| 1455 | Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback |
| 1456 | + " duration=" + duration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1457 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1458 | |
| 1459 | if (pkg == null || callback == null) { |
| 1460 | Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback); |
| 1461 | return ; |
| 1462 | } |
| 1463 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1464 | final boolean isSystemToast = isCallerSystemOrPhone() || ("android".equals(pkg)); |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 1465 | final boolean isPackageSuspended = |
| 1466 | isPackageSuspendedForUser(pkg, Binder.getCallingUid()); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1467 | |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 1468 | if (ENABLE_BLOCKED_TOASTS && !isSystemToast && |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1469 | (!areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()) |
| 1470 | || isPackageSuspended)) { |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 1471 | Slog.e(TAG, "Suppressing toast from package " + pkg |
| 1472 | + (isPackageSuspended |
| 1473 | ? " due to package suspended by administrator." |
| 1474 | : " by user request.")); |
| 1475 | return; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | synchronized (mToastQueue) { |
| 1479 | int callingPid = Binder.getCallingPid(); |
| 1480 | long callingId = Binder.clearCallingIdentity(); |
| 1481 | try { |
| 1482 | ToastRecord record; |
| 1483 | int index = indexOfToastLocked(pkg, callback); |
| 1484 | // If it's already in the queue, we update it in place, we don't |
| 1485 | // move it to the end of the queue. |
| 1486 | if (index >= 0) { |
| 1487 | record = mToastQueue.get(index); |
| 1488 | record.update(duration); |
| 1489 | } else { |
| 1490 | // Limit the number of toasts that any given package except the android |
| 1491 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| 1492 | if (!isSystemToast) { |
| 1493 | int count = 0; |
| 1494 | final int N = mToastQueue.size(); |
| 1495 | for (int i=0; i<N; i++) { |
| 1496 | final ToastRecord r = mToastQueue.get(i); |
| 1497 | if (r.pkg.equals(pkg)) { |
| 1498 | count++; |
| 1499 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 1500 | Slog.e(TAG, "Package has already posted " + count |
| 1501 | + " toasts. Not showing more. Package=" + pkg); |
| 1502 | return; |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | } |
| 1507 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1508 | Binder token = new Binder(); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 1509 | mWindowManagerInternal.addWindowToken(token, TYPE_TOAST, DEFAULT_DISPLAY); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1510 | record = new ToastRecord(callingPid, pkg, callback, duration, token); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1511 | mToastQueue.add(record); |
| 1512 | index = mToastQueue.size() - 1; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 1513 | keepProcessAliveIfNeededLocked(callingPid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1514 | } |
| 1515 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 1516 | // new or just been updated. Call back and tell it to show itself. |
| 1517 | // If the callback fails, this will remove it from the list, so don't |
| 1518 | // assume that it's valid after this. |
| 1519 | if (index == 0) { |
| 1520 | showNextToastLocked(); |
| 1521 | } |
| 1522 | } finally { |
| 1523 | Binder.restoreCallingIdentity(callingId); |
| 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | @Override |
| 1529 | public void cancelToast(String pkg, ITransientNotification callback) { |
| 1530 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| 1531 | |
| 1532 | if (pkg == null || callback == null) { |
| 1533 | Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback); |
| 1534 | return ; |
| 1535 | } |
| 1536 | |
| 1537 | synchronized (mToastQueue) { |
| 1538 | long callingId = Binder.clearCallingIdentity(); |
| 1539 | try { |
| 1540 | int index = indexOfToastLocked(pkg, callback); |
| 1541 | if (index >= 0) { |
| 1542 | cancelToastLocked(index); |
| 1543 | } else { |
| 1544 | Slog.w(TAG, "Toast already cancelled. pkg=" + pkg |
| 1545 | + " callback=" + callback); |
| 1546 | } |
| 1547 | } finally { |
| 1548 | Binder.restoreCallingIdentity(callingId); |
| 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 1554 | public void enqueueNotificationWithTag(String pkg, String opPkg, String tag, int id, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1555 | Notification notification, int userId) throws RemoteException { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 1556 | enqueueNotificationInternal(pkg, opPkg, Binder.getCallingUid(), |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 1557 | Binder.getCallingPid(), tag, id, notification, userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | @Override |
| 1561 | public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1562 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1563 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1564 | Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 1565 | // Don't allow client applications to cancel foreground service notis or autobundled |
| 1566 | // summaries. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1567 | final int mustNotHaveFlags = isCallingUidSystem() ? 0 : |
| 1568 | (Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_AUTOGROUP_SUMMARY); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1569 | cancelNotification(Binder.getCallingUid(), Binder.getCallingPid(), pkg, tag, id, 0, |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 1570 | mustNotHaveFlags, false, userId, REASON_APP_CANCEL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | @Override |
| 1574 | public void cancelAllNotifications(String pkg, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1575 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1576 | |
| 1577 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1578 | Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg); |
| 1579 | |
| 1580 | // Calling from user space, don't allow the canceling of actively |
| 1581 | // running foreground services. |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1582 | cancelAllNotificationsInt(Binder.getCallingUid(), Binder.getCallingPid(), |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1583 | pkg, null, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1584 | REASON_APP_CANCEL_ALL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | @Override |
| 1588 | public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1589 | checkCallerIsSystem(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1590 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1591 | mRankingHelper.setEnabled(pkg, uid, enabled); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1592 | // 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] | 1593 | if (!enabled) { |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1594 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, null, 0, 0, true, |
| 1595 | UserHandle.getUserId(uid), REASON_PACKAGE_BANNED, null); |
| 1596 | } |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1597 | savePolicyFile(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | /** |
| 1601 | * Use this when you just want to know if notifications are OK for this package. |
| 1602 | */ |
| 1603 | @Override |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1604 | public boolean areNotificationsEnabled(String pkg) { |
| 1605 | return areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()); |
| 1606 | } |
| 1607 | |
| 1608 | /** |
| 1609 | * Use this when you just want to know if notifications are OK for this package. |
| 1610 | */ |
| 1611 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1612 | public boolean areNotificationsEnabledForPackage(String pkg, int uid) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1613 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 1614 | |
| 1615 | return mRankingHelper.getImportance(pkg, uid) != IMPORTANCE_NONE; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1616 | } |
| 1617 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1618 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1619 | public int getPackageImportance(String pkg) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1620 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1621 | return mRankingHelper.getImportance(pkg, Binder.getCallingUid()); |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | @Override |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1625 | public boolean canShowBadge(String pkg, int uid) { |
| 1626 | checkCallerIsSystem(); |
| 1627 | return mRankingHelper.canShowBadge(pkg, uid); |
| 1628 | } |
| 1629 | |
| 1630 | @Override |
| 1631 | public void setShowBadge(String pkg, int uid, boolean showBadge) { |
| 1632 | checkCallerIsSystem(); |
| 1633 | mRankingHelper.setShowBadge(pkg, uid, showBadge); |
| 1634 | savePolicyFile(); |
| 1635 | } |
| 1636 | |
| 1637 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1638 | public void createNotificationChannelGroups(String pkg, |
| 1639 | ParceledListSlice channelGroupList) throws RemoteException { |
| 1640 | checkCallerIsSystemOrSameApp(pkg); |
| 1641 | List<NotificationChannelGroup> groups = channelGroupList.getList(); |
| 1642 | final int groupSize = groups.size(); |
| 1643 | for (int i = 0; i < groupSize; i++) { |
| 1644 | final NotificationChannelGroup group = groups.get(i); |
| 1645 | Preconditions.checkNotNull(group, "group in list is null"); |
| 1646 | mRankingHelper.createNotificationChannelGroup(pkg, Binder.getCallingUid(), group, |
| 1647 | true /* fromTargetApp */); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1648 | mListeners.notifyNotificationChannelGroupChanged(pkg, |
| 1649 | UserHandle.of(UserHandle.getCallingUserId()), group, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1650 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1651 | } |
| 1652 | savePolicyFile(); |
| 1653 | } |
| 1654 | |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 1655 | private void createNotificationChannelsImpl(String pkg, int uid, |
| 1656 | ParceledListSlice channelsList) { |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1657 | List<NotificationChannel> channels = channelsList.getList(); |
| 1658 | final int channelsSize = channels.size(); |
| 1659 | for (int i = 0; i < channelsSize; i++) { |
| 1660 | final NotificationChannel channel = channels.get(i); |
| 1661 | Preconditions.checkNotNull(channel, "channel in list is null"); |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 1662 | mRankingHelper.createNotificationChannel(pkg, uid, channel, |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1663 | true /* fromTargetApp */); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1664 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1665 | UserHandle.getUserHandleForUid(uid), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1666 | mRankingHelper.getNotificationChannel(pkg, uid, channel.getId(), false), |
| 1667 | NOTIFICATION_CHANNEL_OR_GROUP_ADDED); |
Geoffrey Pitsch | 0353371 | 2017-01-05 10:30:07 -0500 | [diff] [blame] | 1668 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1669 | savePolicyFile(); |
| 1670 | } |
| 1671 | |
| 1672 | @Override |
Geoffrey Pitsch | 3560f84 | 2017-03-22 16:42:43 -0400 | [diff] [blame] | 1673 | public void createNotificationChannels(String pkg, |
| 1674 | ParceledListSlice channelsList) throws RemoteException { |
| 1675 | checkCallerIsSystemOrSameApp(pkg); |
| 1676 | createNotificationChannelsImpl(pkg, Binder.getCallingUid(), channelsList); |
| 1677 | } |
| 1678 | |
| 1679 | @Override |
| 1680 | public void createNotificationChannelsForPackage(String pkg, int uid, |
| 1681 | ParceledListSlice channelsList) throws RemoteException { |
| 1682 | checkCallerIsSystem(); |
| 1683 | createNotificationChannelsImpl(pkg, uid, channelsList); |
| 1684 | } |
| 1685 | |
| 1686 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1687 | public NotificationChannel getNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1688 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1689 | return mRankingHelper.getNotificationChannel( |
| 1690 | pkg, Binder.getCallingUid(), channelId, false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | @Override |
| 1694 | public NotificationChannel getNotificationChannelForPackage(String pkg, int uid, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1695 | String channelId, boolean includeDeleted) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1696 | checkCallerIsSystem(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1697 | return mRankingHelper.getNotificationChannel(pkg, uid, channelId, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | @Override |
| 1701 | public void deleteNotificationChannel(String pkg, String channelId) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1702 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1703 | final int callingUid = Binder.getCallingUid(); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1704 | if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(channelId)) { |
| 1705 | throw new IllegalArgumentException("Cannot delete default channel"); |
| 1706 | } |
| 1707 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, channelId, 0, 0, true, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1708 | UserHandle.getUserId(callingUid), REASON_CHANNEL_BANNED, null); |
| 1709 | mRankingHelper.deleteNotificationChannel(pkg, callingUid, channelId); |
| 1710 | mListeners.notifyNotificationChannelChanged(pkg, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1711 | UserHandle.getUserHandleForUid(callingUid), |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1712 | mRankingHelper.getNotificationChannel(pkg, callingUid, channelId, true), |
| 1713 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1714 | savePolicyFile(); |
| 1715 | } |
| 1716 | |
| 1717 | @Override |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1718 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroups( |
| 1719 | String pkg) { |
| 1720 | checkCallerIsSystemOrSameApp(pkg); |
| 1721 | return new ParceledListSlice<>(new ArrayList( |
| 1722 | mRankingHelper.getNotificationChannelGroups(pkg, Binder.getCallingUid()))); |
| 1723 | } |
| 1724 | |
| 1725 | @Override |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1726 | public void deleteNotificationChannelGroup(String pkg, String groupId) { |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1727 | checkCallerIsSystemOrSameApp(pkg); |
| 1728 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1729 | final int callingUid = Binder.getCallingUid(); |
| 1730 | NotificationChannelGroup groupToDelete = |
| 1731 | mRankingHelper.getNotificationChannelGroup(groupId, pkg, callingUid); |
| 1732 | if (groupToDelete != null) { |
| 1733 | List<NotificationChannel> deletedChannels = |
| 1734 | mRankingHelper.deleteNotificationChannelGroup(pkg, callingUid, groupId); |
| 1735 | for (int i = 0; i < deletedChannels.size(); i++) { |
| 1736 | final NotificationChannel deletedChannel = deletedChannels.get(i); |
| 1737 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, deletedChannel.getId(), 0, 0, |
| 1738 | true, |
| 1739 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, |
| 1740 | null); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1741 | mListeners.notifyNotificationChannelChanged(pkg, |
| 1742 | UserHandle.getUserHandleForUid(callingUid), |
| 1743 | deletedChannel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1744 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
| 1745 | } |
| 1746 | mListeners.notifyNotificationChannelGroupChanged( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 1747 | pkg, UserHandle.getUserHandleForUid(callingUid), groupToDelete, |
| 1748 | NOTIFICATION_CHANNEL_OR_GROUP_DELETED); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1749 | savePolicyFile(); |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1750 | } |
Julia Reynolds | 9bfba59 | 2017-03-15 14:03:55 -0400 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1754 | public void updateNotificationChannelForPackage(String pkg, int uid, |
| 1755 | NotificationChannel channel) { |
Geoffrey Pitsch | 4dd5006 | 2016-12-06 16:41:22 -0500 | [diff] [blame] | 1756 | enforceSystemOrSystemUI("Caller not system or systemui"); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 1757 | Preconditions.checkNotNull(channel); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 1758 | updateNotificationChannelInt(pkg, uid, channel, false); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | @Override |
| 1762 | public ParceledListSlice<NotificationChannel> getNotificationChannelsForPackage(String pkg, |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1763 | int uid, boolean includeDeleted) { |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 1764 | enforceSystemOrSystemUI("getNotificationChannelsForPackage"); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1765 | return mRankingHelper.getNotificationChannels(pkg, uid, includeDeleted); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 1769 | public int getNumNotificationChannelsForPackage(String pkg, int uid, |
| 1770 | boolean includeDeleted) { |
| 1771 | enforceSystemOrSystemUI("getNumNotificationChannelsForPackage"); |
| 1772 | return mRankingHelper.getNotificationChannels(pkg, uid, includeDeleted) |
| 1773 | .getList().size(); |
| 1774 | } |
| 1775 | |
| 1776 | @Override |
Julia Reynolds | 17717f5 | 2017-05-09 11:46:06 -0400 | [diff] [blame] | 1777 | public boolean onlyHasDefaultChannel(String pkg, int uid) { |
| 1778 | enforceSystemOrSystemUI("onlyHasDefaultChannel"); |
| 1779 | return mRankingHelper.onlyHasDefaultChannel(pkg, uid); |
| 1780 | } |
| 1781 | |
| 1782 | @Override |
Julia Reynolds | 41103f4 | 2017-03-15 11:36:35 -0400 | [diff] [blame] | 1783 | public int getDeletedChannelCount(String pkg, int uid) { |
| 1784 | enforceSystemOrSystemUI("getDeletedChannelCount"); |
| 1785 | return mRankingHelper.getDeletedChannelCount(pkg, uid); |
| 1786 | } |
| 1787 | |
| 1788 | @Override |
Julia Reynolds | 59e152e | 2017-01-25 17:42:53 -0500 | [diff] [blame] | 1789 | public ParceledListSlice<NotificationChannelGroup> getNotificationChannelGroupsForPackage( |
| 1790 | String pkg, int uid, boolean includeDeleted) { |
| 1791 | checkCallerIsSystem(); |
| 1792 | return mRankingHelper.getNotificationChannelGroups(pkg, uid, includeDeleted); |
| 1793 | } |
| 1794 | |
| 1795 | @Override |
Geoffrey Pitsch | df44b60 | 2017-02-03 13:31:50 -0500 | [diff] [blame] | 1796 | public NotificationChannelGroup getNotificationChannelGroupForPackage( |
| 1797 | String groupId, String pkg, int uid) { |
| 1798 | enforceSystemOrSystemUI("getNotificationChannelGroupForPackage"); |
| 1799 | return mRankingHelper.getNotificationChannelGroup(groupId, pkg, uid); |
| 1800 | } |
| 1801 | |
| 1802 | @Override |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1803 | public ParceledListSlice<NotificationChannel> getNotificationChannels(String pkg) { |
| 1804 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1805 | return mRankingHelper.getNotificationChannels( |
| 1806 | pkg, Binder.getCallingUid(), false /* includeDeleted */); |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 1807 | } |
| 1808 | |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1809 | @Override |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 1810 | public void clearData(String packageName, int uid, boolean fromApp) throws RemoteException { |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1811 | checkCallerIsSystem(); |
| 1812 | |
| 1813 | // Cancel posted notifications |
| 1814 | cancelAllNotificationsInt(MY_UID, MY_PID, packageName, null, 0, 0, true, |
| 1815 | UserHandle.getUserId(Binder.getCallingUid()), REASON_CHANNEL_BANNED, null); |
| 1816 | |
| 1817 | // Listener & assistant |
| 1818 | mListeners.onPackagesChanged(true, new String[] {packageName}); |
| 1819 | mNotificationAssistants.onPackagesChanged(true, new String[] {packageName}); |
| 1820 | |
| 1821 | // Zen |
| 1822 | mConditionProviders.onPackagesChanged(true, new String[] {packageName}); |
| 1823 | |
| 1824 | // Reset notification preferences |
Julia Reynolds | 5355e85 | 2017-02-07 14:54:13 -0500 | [diff] [blame] | 1825 | if (!fromApp) { |
| 1826 | mRankingHelper.onPackagesChanged(true, UserHandle.getCallingUserId(), |
| 1827 | new String[]{packageName}, new int[]{uid}); |
| 1828 | } |
Julia Reynolds | 4036e8d | 2017-01-13 09:50:05 -0500 | [diff] [blame] | 1829 | |
| 1830 | savePolicyFile(); |
| 1831 | } |
| 1832 | |
| 1833 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1834 | /** |
| 1835 | * System-only API for getting a list of current (i.e. not cleared) notifications. |
| 1836 | * |
| 1837 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1838 | * @returns A list of all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1839 | */ |
| 1840 | @Override |
| 1841 | public StatusBarNotification[] getActiveNotifications(String callingPkg) { |
| 1842 | // enforce() will ensure the calling uid has the correct permission |
| 1843 | getContext().enforceCallingOrSelfPermission( |
| 1844 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 1845 | "NotificationManagerService.getActiveNotifications"); |
| 1846 | |
| 1847 | StatusBarNotification[] tmp = null; |
| 1848 | int uid = Binder.getCallingUid(); |
| 1849 | |
| 1850 | // noteOp will check to make sure the callingPkg matches the uid |
| 1851 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 1852 | == AppOpsManager.MODE_ALLOWED) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1853 | synchronized (mNotificationLock) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1854 | tmp = new StatusBarNotification[mNotificationList.size()]; |
| 1855 | final int N = mNotificationList.size(); |
| 1856 | for (int i=0; i<N; i++) { |
| 1857 | tmp[i] = mNotificationList.get(i).sbn; |
| 1858 | } |
| 1859 | } |
| 1860 | } |
| 1861 | return tmp; |
| 1862 | } |
| 1863 | |
| 1864 | /** |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1865 | * Public API for getting a list of current notifications for the calling package/uid. |
| 1866 | * |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 1867 | * Note that since notification posting is done asynchronously, this will not return |
| 1868 | * notifications that are in the process of being posted. |
| 1869 | * |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1870 | * @returns A list of all the package's notifications, in natural order. |
| 1871 | */ |
| 1872 | @Override |
| 1873 | public ParceledListSlice<StatusBarNotification> getAppActiveNotifications(String pkg, |
| 1874 | int incomingUserId) { |
| 1875 | checkCallerIsSystemOrSameApp(pkg); |
| 1876 | int userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1877 | Binder.getCallingUid(), incomingUserId, true, false, |
| 1878 | "getAppActiveNotifications", pkg); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1879 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 1880 | final ArrayMap<String, StatusBarNotification> map |
| 1881 | = new ArrayMap<>(mNotificationList.size() + mEnqueuedNotifications.size()); |
Erik Wolsheimer | 2242b4d | 2015-11-24 13:22:04 -0800 | [diff] [blame] | 1882 | final int N = mNotificationList.size(); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1883 | for (int i = 0; i < N; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 1884 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 1885 | mNotificationList.get(i).sbn); |
| 1886 | if (sbn != null) { |
| 1887 | map.put(sbn.getKey(), sbn); |
| 1888 | } |
| 1889 | } |
| 1890 | for(NotificationRecord snoozed: mSnoozeHelper.getSnoozed(userId, pkg)) { |
| 1891 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, snoozed.sbn); |
| 1892 | if (sbn != null) { |
| 1893 | map.put(sbn.getKey(), sbn); |
| 1894 | } |
| 1895 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1896 | final int M = mEnqueuedNotifications.size(); |
| 1897 | for (int i = 0; i < M; i++) { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 1898 | StatusBarNotification sbn = sanitizeSbn(pkg, userId, |
| 1899 | mEnqueuedNotifications.get(i).sbn); |
| 1900 | if (sbn != null) { |
| 1901 | map.put(sbn.getKey(), sbn); // pending update overwrites existing post here |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1902 | } |
| 1903 | } |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 1904 | final ArrayList<StatusBarNotification> list = new ArrayList<>(map.size()); |
| 1905 | list.addAll(map.values()); |
| 1906 | return new ParceledListSlice<StatusBarNotification>(list); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1907 | } |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1908 | } |
| 1909 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 1910 | private StatusBarNotification sanitizeSbn(String pkg, int userId, |
| 1911 | StatusBarNotification sbn) { |
| 1912 | if (sbn.getPackageName().equals(pkg) && sbn.getUserId() == userId |
| 1913 | && (sbn.getNotification().flags |
| 1914 | & Notification.FLAG_AUTOGROUP_SUMMARY) == 0) { |
| 1915 | // We could pass back a cloneLight() but clients might get confused and |
| 1916 | // try to send this thing back to notify() again, which would not work |
| 1917 | // very well. |
| 1918 | return new StatusBarNotification( |
| 1919 | sbn.getPackageName(), |
| 1920 | sbn.getOpPkg(), |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 1921 | sbn.getId(), sbn.getTag(), sbn.getUid(), sbn.getInitialPid(), |
| 1922 | sbn.getNotification().clone(), |
| 1923 | sbn.getUser(), sbn.getOverrideGroupKey(), sbn.getPostTime()); |
| 1924 | } |
| 1925 | return null; |
| 1926 | } |
| 1927 | |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1928 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1929 | * System-only API for getting a list of recent (cleared, no longer shown) notifications. |
| 1930 | * |
| 1931 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
| 1932 | */ |
| 1933 | @Override |
| 1934 | public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) { |
| 1935 | // enforce() will ensure the calling uid has the correct permission |
| 1936 | getContext().enforceCallingOrSelfPermission( |
| 1937 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 1938 | "NotificationManagerService.getHistoricalNotifications"); |
| 1939 | |
| 1940 | StatusBarNotification[] tmp = null; |
| 1941 | int uid = Binder.getCallingUid(); |
| 1942 | |
| 1943 | // noteOp will check to make sure the callingPkg matches the uid |
| 1944 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 1945 | == AppOpsManager.MODE_ALLOWED) { |
| 1946 | synchronized (mArchive) { |
| 1947 | tmp = mArchive.getArray(count); |
| 1948 | } |
| 1949 | } |
| 1950 | return tmp; |
| 1951 | } |
| 1952 | |
| 1953 | /** |
| 1954 | * Register a listener binder directly with the notification manager. |
| 1955 | * |
| 1956 | * Only works with system callers. Apps should extend |
| 1957 | * {@link android.service.notification.NotificationListenerService}. |
| 1958 | */ |
| 1959 | @Override |
| 1960 | public void registerListener(final INotificationListener listener, |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1961 | final ComponentName component, final int userid) { |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 1962 | enforceSystemOrSystemUI("INotificationManager.registerListener"); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1963 | mListeners.registerService(listener, component, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | /** |
| 1967 | * Remove a listener binder directly |
| 1968 | */ |
| 1969 | @Override |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 1970 | public void unregisterListener(INotificationListener token, int userid) { |
Chris Wren | b7c8109 | 2016-03-10 11:41:10 -0500 | [diff] [blame] | 1971 | mListeners.unregisterService(token, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | /** |
| 1975 | * Allow an INotificationListener to simulate a "clear all" operation. |
| 1976 | * |
| 1977 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications} |
| 1978 | * |
| 1979 | * @param token The binder for the listener, to check that the caller is allowed |
| 1980 | */ |
| 1981 | @Override |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 1982 | public void cancelNotificationsFromListener(INotificationListener token, String[] keys) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1983 | final int callingUid = Binder.getCallingUid(); |
| 1984 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1985 | long identity = Binder.clearCallingIdentity(); |
| 1986 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 1987 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1988 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 1989 | if (keys != null) { |
| 1990 | final int N = keys.length; |
| 1991 | for (int i = 0; i < N; i++) { |
| 1992 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 1993 | if (r == null) continue; |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1994 | final int userId = r.sbn.getUserId(); |
| 1995 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1996 | !mUserProfiles.isCurrentProfile(userId)) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1997 | throw new SecurityException("Disallowed call from listener: " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1998 | + info.service); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1999 | } |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 2000 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2001 | r.sbn.getPackageName(), r.sbn.getTag(), r.sbn.getId(), |
| 2002 | userId); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2003 | } |
| 2004 | } else { |
| 2005 | cancelAllLocked(callingUid, callingPid, info.userid, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2006 | REASON_LISTENER_CANCEL_ALL, info, info.supportsProfiles()); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2007 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2008 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2009 | } finally { |
| 2010 | Binder.restoreCallingIdentity(identity); |
| 2011 | } |
| 2012 | } |
| 2013 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2014 | /** |
| 2015 | * Handle request from an approved listener to re-enable itself. |
| 2016 | * |
| 2017 | * @param component The componenet to be re-enabled, caller must match package. |
| 2018 | */ |
| 2019 | @Override |
| 2020 | public void requestBindListener(ComponentName component) { |
| 2021 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 2022 | long identity = Binder.clearCallingIdentity(); |
| 2023 | try { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2024 | ManagedServices manager = |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 2025 | mNotificationAssistants.isComponentEnabledForCurrentProfiles(component) |
| 2026 | ? mNotificationAssistants |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2027 | : mListeners; |
| 2028 | manager.setComponentState(component, true); |
| 2029 | } finally { |
| 2030 | Binder.restoreCallingIdentity(identity); |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | @Override |
| 2035 | public void requestUnbindListener(INotificationListener token) { |
| 2036 | long identity = Binder.clearCallingIdentity(); |
| 2037 | try { |
| 2038 | // allow bound services to disable themselves |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2039 | synchronized (mNotificationLock) { |
| 2040 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2041 | info.getOwner().setComponentState(info.component, false); |
| 2042 | } |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 2043 | } finally { |
| 2044 | Binder.restoreCallingIdentity(identity); |
| 2045 | } |
| 2046 | } |
| 2047 | |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2048 | @Override |
| 2049 | public void setNotificationsShownFromListener(INotificationListener token, String[] keys) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2050 | long identity = Binder.clearCallingIdentity(); |
| 2051 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2052 | synchronized (mNotificationLock) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2053 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2054 | if (keys != null) { |
| 2055 | final int N = keys.length; |
| 2056 | for (int i = 0; i < N; i++) { |
| 2057 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
| 2058 | if (r == null) continue; |
| 2059 | final int userId = r.sbn.getUserId(); |
| 2060 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
| 2061 | !mUserProfiles.isCurrentProfile(userId)) { |
| 2062 | throw new SecurityException("Disallowed call from listener: " |
| 2063 | + info.service); |
| 2064 | } |
| 2065 | if (!r.isSeen()) { |
| 2066 | if (DBG) Slog.d(TAG, "Marking notification as seen " + keys[i]); |
| 2067 | mAppUsageStats.reportEvent(r.sbn.getPackageName(), |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 2068 | userId == UserHandle.USER_ALL ? UserHandle.USER_SYSTEM |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2069 | : userId, |
Adam Lesinski | c8e8729 | 2015-06-10 15:33:45 -0700 | [diff] [blame] | 2070 | UsageEvents.Event.USER_INTERACTION); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 2071 | r.setSeen(); |
| 2072 | } |
| 2073 | } |
| 2074 | } |
| 2075 | } |
| 2076 | } finally { |
| 2077 | Binder.restoreCallingIdentity(identity); |
| 2078 | } |
| 2079 | } |
| 2080 | |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2081 | /** |
| 2082 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2083 | * |
| 2084 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2085 | * |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2086 | * @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] | 2087 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2088 | @GuardedBy("mNotificationLock") |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2089 | private void cancelNotificationFromListenerLocked(ManagedServiceInfo info, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2090 | int callingUid, int callingPid, String pkg, String tag, int id, int userId) { |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2091 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
| 2092 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
| 2093 | true, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2094 | userId, REASON_LISTENER_CANCEL, info); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2095 | } |
| 2096 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2097 | /** |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2098 | * Allow an INotificationListener to snooze a single notification until a context. |
| 2099 | * |
| 2100 | * @param token The binder for the listener, to check that the caller is allowed |
| 2101 | */ |
| 2102 | @Override |
| 2103 | public void snoozeNotificationUntilContextFromListener(INotificationListener token, |
| 2104 | String key, String snoozeCriterionId) { |
| 2105 | long identity = Binder.clearCallingIdentity(); |
| 2106 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2107 | synchronized (mNotificationLock) { |
| 2108 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2109 | snoozeNotificationInt(key, SNOOZE_UNTIL_UNSPECIFIED, snoozeCriterionId, info); |
| 2110 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 2111 | } finally { |
| 2112 | Binder.restoreCallingIdentity(identity); |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | /** |
| 2117 | * Allow an INotificationListener to snooze a single notification until a time. |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2118 | * |
| 2119 | * @param token The binder for the listener, to check that the caller is allowed |
| 2120 | */ |
| 2121 | @Override |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2122 | public void snoozeNotificationUntilFromListener(INotificationListener token, String key, |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 2123 | long duration) { |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2124 | long identity = Binder.clearCallingIdentity(); |
| 2125 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2126 | synchronized (mNotificationLock) { |
| 2127 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2128 | snoozeNotificationInt(key, duration, null, info); |
| 2129 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 2130 | } finally { |
| 2131 | Binder.restoreCallingIdentity(identity); |
| 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | /** |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2136 | * Allows the notification assistant to un-snooze a single notification. |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2137 | * |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2138 | * @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] | 2139 | */ |
| 2140 | @Override |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2141 | public void unsnoozeNotificationFromAssistant(INotificationListener token, String key) { |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2142 | long identity = Binder.clearCallingIdentity(); |
| 2143 | try { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2144 | synchronized (mNotificationLock) { |
| 2145 | final ManagedServiceInfo info = |
| 2146 | mNotificationAssistants.checkServiceTokenLocked(token); |
| 2147 | unsnoozeNotificationInt(key, info); |
| 2148 | } |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 2149 | } finally { |
| 2150 | Binder.restoreCallingIdentity(identity); |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2155 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 2156 | * |
| 2157 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 2158 | * |
| 2159 | * @param token The binder for the listener, to check that the caller is allowed |
| 2160 | */ |
| 2161 | @Override |
| 2162 | public void cancelNotificationFromListener(INotificationListener token, String pkg, |
| 2163 | String tag, int id) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 2164 | final int callingUid = Binder.getCallingUid(); |
| 2165 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2166 | long identity = Binder.clearCallingIdentity(); |
| 2167 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2168 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2169 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 2170 | if (info.supportsProfiles()) { |
| 2171 | Log.e(TAG, "Ignoring deprecated cancelNotification(pkg, tag, id) " |
| 2172 | + "from " + info.component |
| 2173 | + " use cancelNotification(key) instead."); |
| 2174 | } else { |
| 2175 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 2176 | pkg, tag, id, info.userid); |
| 2177 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 2178 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2179 | } finally { |
| 2180 | Binder.restoreCallingIdentity(identity); |
| 2181 | } |
| 2182 | } |
| 2183 | |
| 2184 | /** |
| 2185 | * Allow an INotificationListener to request the list of outstanding notifications seen by |
| 2186 | * the current user. Useful when starting up, after which point the listener callbacks |
| 2187 | * should be used. |
| 2188 | * |
| 2189 | * @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] | 2190 | * @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] | 2191 | * @returns The return value will contain the notifications specified in keys, in that |
| 2192 | * order, or if keys is null, all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2193 | */ |
| 2194 | @Override |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2195 | public ParceledListSlice<StatusBarNotification> getActiveNotificationsFromListener( |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2196 | INotificationListener token, String[] keys, int trim) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2197 | synchronized (mNotificationLock) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2198 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2199 | final boolean getKeys = keys != null; |
| 2200 | final int N = getKeys ? keys.length : mNotificationList.size(); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2201 | final ArrayList<StatusBarNotification> list |
| 2202 | = new ArrayList<StatusBarNotification>(N); |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2203 | for (int i=0; i<N; i++) { |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 2204 | final NotificationRecord r = getKeys |
| 2205 | ? mNotificationsByKey.get(keys[i]) |
| 2206 | : mNotificationList.get(i); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2207 | if (r == null) continue; |
| 2208 | StatusBarNotification sbn = r.sbn; |
| 2209 | if (!isVisibleToListener(sbn, info)) continue; |
| 2210 | StatusBarNotification sbnToSend = |
| 2211 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2212 | list.add(sbnToSend); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2213 | } |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 2214 | return new ParceledListSlice<StatusBarNotification>(list); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2215 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2216 | } |
| 2217 | |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 2218 | /** |
| 2219 | * Allow an INotificationListener to request the list of outstanding snoozed notifications |
| 2220 | * seen by the current user. Useful when starting up, after which point the listener |
| 2221 | * callbacks should be used. |
| 2222 | * |
| 2223 | * @param token The binder for the listener, to check that the caller is allowed |
| 2224 | * @returns The return value will contain the notifications specified in keys, in that |
| 2225 | * order, or if keys is null, all the notifications, in natural order. |
| 2226 | */ |
| 2227 | @Override |
| 2228 | public ParceledListSlice<StatusBarNotification> getSnoozedNotificationsFromListener( |
| 2229 | INotificationListener token, int trim) { |
| 2230 | synchronized (mNotificationLock) { |
| 2231 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2232 | List<NotificationRecord> snoozedRecords = mSnoozeHelper.getSnoozed(); |
| 2233 | final int N = snoozedRecords.size(); |
| 2234 | final ArrayList<StatusBarNotification> list = new ArrayList<>(N); |
| 2235 | for (int i=0; i < N; i++) { |
| 2236 | final NotificationRecord r = snoozedRecords.get(i); |
| 2237 | if (r == null) continue; |
| 2238 | StatusBarNotification sbn = r.sbn; |
| 2239 | if (!isVisibleToListener(sbn, info)) continue; |
| 2240 | StatusBarNotification sbnToSend = |
| 2241 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 2242 | list.add(sbnToSend); |
| 2243 | } |
| 2244 | return new ParceledListSlice<>(list); |
| 2245 | } |
| 2246 | } |
| 2247 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2248 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2249 | public void requestHintsFromListener(INotificationListener token, int hints) { |
| 2250 | final long identity = Binder.clearCallingIdentity(); |
| 2251 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2252 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2253 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2254 | final int disableEffectsMask = HINT_HOST_DISABLE_EFFECTS |
| 2255 | | HINT_HOST_DISABLE_NOTIFICATION_EFFECTS |
| 2256 | | HINT_HOST_DISABLE_CALL_EFFECTS; |
| 2257 | final boolean disableEffects = (hints & disableEffectsMask) != 0; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2258 | if (disableEffects) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2259 | addDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2260 | } else { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 2261 | removeDisabledHints(info, hints); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2262 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2263 | updateListenerHintsLocked(); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2264 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2265 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2266 | } finally { |
| 2267 | Binder.restoreCallingIdentity(identity); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2272 | public int getHintsFromListener(INotificationListener token) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2273 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2274 | return mListenerHints; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2275 | } |
| 2276 | } |
| 2277 | |
| 2278 | @Override |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2279 | public void requestInterruptionFilterFromListener(INotificationListener token, |
| 2280 | int interruptionFilter) throws RemoteException { |
| 2281 | final long identity = Binder.clearCallingIdentity(); |
| 2282 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2283 | synchronized (mNotificationLock) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 2284 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2285 | mZenModeHelper.requestFromListener(info.component, interruptionFilter); |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2286 | updateInterruptionFilterLocked(); |
| 2287 | } |
| 2288 | } finally { |
| 2289 | Binder.restoreCallingIdentity(identity); |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | @Override |
| 2294 | public int getInterruptionFilterFromListener(INotificationListener token) |
| 2295 | throws RemoteException { |
| 2296 | synchronized (mNotificationLight) { |
| 2297 | return mInterruptionFilter; |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | @Override |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2302 | public void setOnNotificationPostedTrimFromListener(INotificationListener token, int trim) |
| 2303 | throws RemoteException { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2304 | synchronized (mNotificationLock) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 2305 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 2306 | if (info == null) return; |
| 2307 | mListeners.setOnNotificationPostedTrimLocked(info, trim); |
| 2308 | } |
| 2309 | } |
| 2310 | |
| 2311 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2312 | public int getZenMode() { |
| 2313 | return mZenModeHelper.getZenMode(); |
| 2314 | } |
| 2315 | |
| 2316 | @Override |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 2317 | public ZenModeConfig getZenModeConfig() { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2318 | enforceSystemOrSystemUI("INotificationManager.getZenModeConfig"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 2319 | return mZenModeHelper.getConfig(); |
| 2320 | } |
| 2321 | |
| 2322 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2323 | public void setZenMode(int mode, Uri conditionId, String reason) throws RemoteException { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2324 | enforceSystemOrSystemUI("INotificationManager.setZenMode"); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2325 | final long identity = Binder.clearCallingIdentity(); |
| 2326 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 2327 | mZenModeHelper.setManualZenMode(mode, conditionId, null, reason); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 2328 | } finally { |
| 2329 | Binder.restoreCallingIdentity(identity); |
| 2330 | } |
| 2331 | } |
| 2332 | |
| 2333 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2334 | public List<ZenModeConfig.ZenRule> getZenRules() throws RemoteException { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2335 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRules"); |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2336 | return mZenModeHelper.getZenRules(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2340 | public AutomaticZenRule getAutomaticZenRule(String id) throws RemoteException { |
| 2341 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2342 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRule"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2343 | return mZenModeHelper.getAutomaticZenRule(id); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2347 | public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2348 | throws RemoteException { |
| 2349 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 2350 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 2351 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 2352 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2353 | enforcePolicyAccess(Binder.getCallingUid(), "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2354 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2355 | return mZenModeHelper.addAutomaticZenRule(automaticZenRule, |
| 2356 | "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2360 | public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2361 | throws RemoteException { |
| 2362 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 2363 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 2364 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 2365 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
| 2366 | enforcePolicyAccess(Binder.getCallingUid(), "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2367 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 2368 | return mZenModeHelper.updateAutomaticZenRule(id, automaticZenRule, |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2369 | "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2373 | public boolean removeAutomaticZenRule(String id) throws RemoteException { |
| 2374 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2375 | // Verify that they can modify zen rules. |
| 2376 | enforcePolicyAccess(Binder.getCallingUid(), "removeAutomaticZenRule"); |
| 2377 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 2378 | return mZenModeHelper.removeAutomaticZenRule(id, "removeAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | @Override |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 2382 | public boolean removeAutomaticZenRules(String packageName) throws RemoteException { |
| 2383 | Preconditions.checkNotNull(packageName, "Package name is null"); |
| 2384 | enforceSystemOrSystemUI("removeAutomaticZenRules"); |
| 2385 | |
| 2386 | return mZenModeHelper.removeAutomaticZenRules(packageName, "removeAutomaticZenRules"); |
| 2387 | } |
| 2388 | |
| 2389 | @Override |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 2390 | public int getRuleInstanceCount(ComponentName owner) throws RemoteException { |
| 2391 | Preconditions.checkNotNull(owner, "Owner is null"); |
| 2392 | enforceSystemOrSystemUI("getRuleInstanceCount"); |
| 2393 | |
| 2394 | return mZenModeHelper.getCurrentInstanceCount(owner); |
| 2395 | } |
| 2396 | |
| 2397 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2398 | public void setInterruptionFilter(String pkg, int filter) throws RemoteException { |
| 2399 | enforcePolicyAccess(pkg, "setInterruptionFilter"); |
| 2400 | final int zen = NotificationManager.zenModeFromInterruptionFilter(filter, -1); |
| 2401 | if (zen == -1) throw new IllegalArgumentException("Invalid filter: " + filter); |
| 2402 | final long identity = Binder.clearCallingIdentity(); |
| 2403 | try { |
Julia Reynolds | 44ad6ff | 2016-07-06 09:47:45 -0400 | [diff] [blame] | 2404 | mZenModeHelper.setManualZenMode(zen, null, pkg, "setInterruptionFilter"); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2405 | } finally { |
| 2406 | Binder.restoreCallingIdentity(identity); |
| 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | @Override |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 2411 | public void notifyConditions(final String pkg, IConditionProvider provider, |
| 2412 | final Condition[] conditions) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2413 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 2414 | checkCallerIsSystemOrSameApp(pkg); |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 2415 | mHandler.post(new Runnable() { |
| 2416 | @Override |
| 2417 | public void run() { |
| 2418 | mConditionProviders.notifyConditions(pkg, info, conditions); |
| 2419 | } |
| 2420 | }); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2421 | } |
| 2422 | |
Julia Reynolds | 38e6ca4 | 2016-08-08 08:38:09 -0400 | [diff] [blame] | 2423 | @Override |
| 2424 | public void requestUnbindProvider(IConditionProvider provider) { |
| 2425 | long identity = Binder.clearCallingIdentity(); |
| 2426 | try { |
| 2427 | // allow bound services to disable themselves |
| 2428 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 2429 | info.getOwner().setComponentState(info.component, false); |
| 2430 | } finally { |
| 2431 | Binder.restoreCallingIdentity(identity); |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | @Override |
| 2436 | public void requestBindProvider(ComponentName component) { |
| 2437 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 2438 | long identity = Binder.clearCallingIdentity(); |
| 2439 | try { |
| 2440 | mConditionProviders.setComponentState(component, true); |
| 2441 | } finally { |
| 2442 | Binder.restoreCallingIdentity(identity); |
| 2443 | } |
| 2444 | } |
| 2445 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2446 | private void enforceSystemOrSystemUI(String message) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 2447 | if (isCallerSystemOrPhone()) return; |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2448 | getContext().enforceCallingPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 2449 | message); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2450 | } |
| 2451 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 2452 | private void enforceSystemOrSystemUIOrSamePackage(String pkg, String message) { |
| 2453 | try { |
| 2454 | checkCallerIsSystemOrSameApp(pkg); |
| 2455 | } catch (SecurityException e) { |
| 2456 | getContext().enforceCallingPermission( |
| 2457 | android.Manifest.permission.STATUS_BAR_SERVICE, |
| 2458 | message); |
| 2459 | } |
| 2460 | } |
| 2461 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 2462 | private void enforcePolicyAccess(int uid, String method) { |
| 2463 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 2464 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 2465 | return; |
| 2466 | } |
| 2467 | boolean accessAllowed = false; |
| 2468 | String[] packages = getContext().getPackageManager().getPackagesForUid(uid); |
| 2469 | final int packageCount = packages.length; |
| 2470 | for (int i = 0; i < packageCount; i++) { |
| 2471 | if (checkPolicyAccess(packages[i])) { |
| 2472 | accessAllowed = true; |
| 2473 | } |
| 2474 | } |
| 2475 | if (!accessAllowed) { |
| 2476 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 2477 | throw new SecurityException("Notification policy access denied"); |
| 2478 | } |
| 2479 | } |
| 2480 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2481 | private void enforcePolicyAccess(String pkg, String method) { |
Julia Reynolds | 6ee2617 | 2015-09-28 11:34:48 -0400 | [diff] [blame] | 2482 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 2483 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 2484 | return; |
| 2485 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 2486 | checkCallerIsSameApp(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2487 | if (!checkPolicyAccess(pkg)) { |
| 2488 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 2489 | throw new SecurityException("Notification policy access denied"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2490 | } |
| 2491 | } |
| 2492 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2493 | private boolean checkPackagePolicyAccess(String pkg) { |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 2494 | return mPolicyAccess.isPackageGranted(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2495 | } |
| 2496 | |
| 2497 | private boolean checkPolicyAccess(String pkg) { |
Julia Reynolds | 0867b3a | 2016-03-30 17:29:54 -0400 | [diff] [blame] | 2498 | try { |
| 2499 | int uid = getContext().getPackageManager().getPackageUidAsUser( |
| 2500 | pkg, UserHandle.getCallingUserId()); |
| 2501 | if (PackageManager.PERMISSION_GRANTED == ActivityManager.checkComponentPermission( |
| 2502 | android.Manifest.permission.MANAGE_NOTIFICATIONS, uid, |
| 2503 | -1, true)) { |
| 2504 | return true; |
| 2505 | } |
| 2506 | } catch (NameNotFoundException e) { |
| 2507 | return false; |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 2508 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2509 | return checkPackagePolicyAccess(pkg) || mListeners.isComponentEnabledForPackage(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2510 | } |
| 2511 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2512 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2513 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2514 | if (!DumpUtils.checkDumpAndUsageStatsPermission(getContext(), TAG, pw)) return; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2515 | final DumpFilter filter = DumpFilter.parseFromArguments(args); |
| 2516 | if (filter != null && filter.stats) { |
| 2517 | dumpJson(pw, filter); |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 2518 | } else if (filter != null && filter.proto) { |
| 2519 | dumpProto(fd, filter); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2520 | } else { |
| 2521 | dumpImpl(pw, filter); |
| 2522 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2523 | } |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2524 | |
| 2525 | @Override |
| 2526 | public ComponentName getEffectsSuppressor() { |
Bryce Lee | ba3d895 | 2016-04-12 12:39:15 -0700 | [diff] [blame] | 2527 | return !mEffectsSuppressors.isEmpty() ? mEffectsSuppressors.get(0) : null; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 2528 | } |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 2529 | |
| 2530 | @Override |
| 2531 | public boolean matchesCallFilter(Bundle extras) { |
| 2532 | enforceSystemOrSystemUI("INotificationManager.matchesCallFilter"); |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 2533 | return mZenModeHelper.matchesCallFilter( |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 2534 | Binder.getCallingUserHandle(), |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 2535 | extras, |
| 2536 | mRankingHelper.findExtractor(ValidateNotificationPeople.class), |
| 2537 | MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS, |
| 2538 | MATCHES_CALL_FILTER_TIMEOUT_AFFINITY); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 2539 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 2540 | |
| 2541 | @Override |
| 2542 | public boolean isSystemConditionProviderEnabled(String path) { |
Julia Reynolds | bb983d20 | 2017-01-06 09:54:20 -0500 | [diff] [blame] | 2543 | enforceSystemOrSystemUI("INotificationManager.isSystemConditionProviderEnabled"); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 2544 | return mConditionProviders.isSystemProviderEnabled(path); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 2545 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2546 | |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 2547 | // Backup/restore interface |
| 2548 | @Override |
| 2549 | public byte[] getBackupPayload(int user) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2550 | if (DBG) Slog.d(TAG, "getBackupPayload u=" + user); |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 2551 | //TODO: http://b/22388012 |
| 2552 | if (user != UserHandle.USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2553 | Slog.w(TAG, "getBackupPayload: cannot backup policy for user " + user); |
| 2554 | return null; |
| 2555 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 2556 | synchronized(mPolicyFile) { |
| 2557 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 2558 | try { |
| 2559 | writePolicyXml(baos, true /*forBackup*/); |
| 2560 | return baos.toByteArray(); |
| 2561 | } catch (IOException e) { |
| 2562 | Slog.w(TAG, "getBackupPayload: error writing payload for user " + user, e); |
| 2563 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2564 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 2565 | return null; |
| 2566 | } |
| 2567 | |
| 2568 | @Override |
| 2569 | public void applyRestore(byte[] payload, int user) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2570 | if (DBG) Slog.d(TAG, "applyRestore u=" + user + " payload=" |
| 2571 | + (payload != null ? new String(payload, StandardCharsets.UTF_8) : null)); |
| 2572 | if (payload == null) { |
| 2573 | Slog.w(TAG, "applyRestore: no payload to restore for user " + user); |
| 2574 | return; |
| 2575 | } |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 2576 | //TODO: http://b/22388012 |
| 2577 | if (user != UserHandle.USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2578 | Slog.w(TAG, "applyRestore: cannot restore policy for user " + user); |
| 2579 | return; |
| 2580 | } |
songjinshi | 9bf2271 | 2017-02-04 10:47:45 +0800 | [diff] [blame] | 2581 | synchronized(mPolicyFile) { |
| 2582 | final ByteArrayInputStream bais = new ByteArrayInputStream(payload); |
| 2583 | try { |
| 2584 | readPolicyXml(bais, true /*forRestore*/); |
| 2585 | savePolicyFile(); |
| 2586 | } catch (NumberFormatException | XmlPullParserException | IOException e) { |
| 2587 | Slog.w(TAG, "applyRestore: error reading payload", e); |
| 2588 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 2589 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 2590 | } |
| 2591 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2592 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2593 | public boolean isNotificationPolicyAccessGranted(String pkg) { |
| 2594 | return checkPolicyAccess(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | @Override |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 2598 | public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {; |
| 2599 | enforceSystemOrSystemUIOrSamePackage(pkg, |
| 2600 | "request policy access status for another package"); |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 2601 | return checkPolicyAccess(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2602 | } |
| 2603 | |
| 2604 | @Override |
| 2605 | public String[] getPackagesRequestingNotificationPolicyAccess() |
| 2606 | throws RemoteException { |
| 2607 | enforceSystemOrSystemUI("request policy access packages"); |
| 2608 | final long identity = Binder.clearCallingIdentity(); |
| 2609 | try { |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 2610 | return mPolicyAccess.getRequestingPackages(); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2611 | } finally { |
| 2612 | Binder.restoreCallingIdentity(identity); |
| 2613 | } |
| 2614 | } |
| 2615 | |
| 2616 | @Override |
| 2617 | public void setNotificationPolicyAccessGranted(String pkg, boolean granted) |
| 2618 | throws RemoteException { |
| 2619 | enforceSystemOrSystemUI("grant notification policy access"); |
| 2620 | final long identity = Binder.clearCallingIdentity(); |
| 2621 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2622 | synchronized (mNotificationLock) { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2623 | mPolicyAccess.put(pkg, granted); |
| 2624 | } |
| 2625 | } finally { |
| 2626 | Binder.restoreCallingIdentity(identity); |
| 2627 | } |
| 2628 | } |
| 2629 | |
| 2630 | @Override |
| 2631 | public Policy getNotificationPolicy(String pkg) { |
| 2632 | enforcePolicyAccess(pkg, "getNotificationPolicy"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2633 | final long identity = Binder.clearCallingIdentity(); |
| 2634 | try { |
| 2635 | return mZenModeHelper.getNotificationPolicy(); |
| 2636 | } finally { |
| 2637 | Binder.restoreCallingIdentity(identity); |
| 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2642 | public void setNotificationPolicy(String pkg, Policy policy) { |
| 2643 | enforcePolicyAccess(pkg, "setNotificationPolicy"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2644 | final long identity = Binder.clearCallingIdentity(); |
| 2645 | try { |
| 2646 | mZenModeHelper.setNotificationPolicy(policy); |
| 2647 | } finally { |
| 2648 | Binder.restoreCallingIdentity(identity); |
| 2649 | } |
| 2650 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2651 | |
| 2652 | @Override |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 2653 | public void applyEnqueuedAdjustmentFromAssistant(INotificationListener token, |
| 2654 | Adjustment adjustment) throws RemoteException { |
| 2655 | final long identity = Binder.clearCallingIdentity(); |
| 2656 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2657 | synchronized (mNotificationLock) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 2658 | mNotificationAssistants.checkServiceTokenLocked(token); |
| 2659 | int N = mEnqueuedNotifications.size(); |
| 2660 | for (int i = 0; i < N; i++) { |
| 2661 | final NotificationRecord n = mEnqueuedNotifications.get(i); |
| 2662 | if (Objects.equals(adjustment.getKey(), n.getKey()) |
| 2663 | && Objects.equals(adjustment.getUser(), n.getUserId())) { |
| 2664 | applyAdjustment(n, adjustment); |
| 2665 | break; |
| 2666 | } |
| 2667 | } |
| 2668 | } |
| 2669 | } finally { |
| 2670 | Binder.restoreCallingIdentity(identity); |
| 2671 | } |
| 2672 | } |
| 2673 | |
| 2674 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 2675 | public void applyAdjustmentFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2676 | Adjustment adjustment) throws RemoteException { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2677 | final long identity = Binder.clearCallingIdentity(); |
| 2678 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2679 | synchronized (mNotificationLock) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 2680 | mNotificationAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 2681 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 2682 | applyAdjustment(n, adjustment); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2683 | } |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 2684 | mRankingHandler.requestSort(true); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2685 | } finally { |
| 2686 | Binder.restoreCallingIdentity(identity); |
| 2687 | } |
| 2688 | } |
| 2689 | |
| 2690 | @Override |
Julia Reynolds | 52e64d0 | 2016-12-09 15:36:12 -0500 | [diff] [blame] | 2691 | public void applyAdjustmentsFromAssistant(INotificationListener token, |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2692 | List<Adjustment> adjustments) throws RemoteException { |
| 2693 | |
| 2694 | final long identity = Binder.clearCallingIdentity(); |
| 2695 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2696 | synchronized (mNotificationLock) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 2697 | mNotificationAssistants.checkServiceTokenLocked(token); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2698 | for (Adjustment adjustment : adjustments) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 2699 | NotificationRecord n = mNotificationsByKey.get(adjustment.getKey()); |
| 2700 | applyAdjustment(n, adjustment); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2701 | } |
| 2702 | } |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 2703 | mRankingHandler.requestSort(true); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2704 | } finally { |
| 2705 | Binder.restoreCallingIdentity(identity); |
| 2706 | } |
| 2707 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2708 | |
| 2709 | @Override |
| 2710 | public void updateNotificationChannelFromPrivilegedListener(INotificationListener token, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2711 | String pkg, UserHandle user, NotificationChannel channel) throws RemoteException { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2712 | Preconditions.checkNotNull(channel); |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2713 | Preconditions.checkNotNull(pkg); |
| 2714 | Preconditions.checkNotNull(user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2715 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2716 | verifyPrivilegedListener(token, user); |
| 2717 | updateNotificationChannelInt(pkg, getUidForPackageAndUser(pkg, user), channel, true); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2718 | } |
| 2719 | |
| 2720 | @Override |
| 2721 | public ParceledListSlice<NotificationChannel> getNotificationChannelsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2722 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 2723 | Preconditions.checkNotNull(pkg); |
| 2724 | Preconditions.checkNotNull(user); |
| 2725 | verifyPrivilegedListener(token, user); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2726 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2727 | return mRankingHelper.getNotificationChannels(pkg, getUidForPackageAndUser(pkg, user), |
| 2728 | false /* includeDeleted */); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | @Override |
| 2732 | public ParceledListSlice<NotificationChannelGroup> |
| 2733 | getNotificationChannelGroupsFromPrivilegedListener( |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2734 | INotificationListener token, String pkg, UserHandle user) throws RemoteException { |
| 2735 | Preconditions.checkNotNull(pkg); |
| 2736 | Preconditions.checkNotNull(user); |
| 2737 | verifyPrivilegedListener(token, user); |
| 2738 | |
| 2739 | List<NotificationChannelGroup> groups = new ArrayList<>(); |
| 2740 | groups.addAll(mRankingHelper.getNotificationChannelGroups( |
| 2741 | pkg, getUidForPackageAndUser(pkg, user))); |
| 2742 | return new ParceledListSlice<>(groups); |
| 2743 | } |
| 2744 | |
| 2745 | private void verifyPrivilegedListener(INotificationListener token, UserHandle user) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 2746 | ManagedServiceInfo info; |
| 2747 | synchronized (mNotificationLock) { |
| 2748 | info = mListeners.checkServiceTokenLocked(token); |
| 2749 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 2750 | if (!hasCompanionDevice(info)) { |
| 2751 | throw new SecurityException(info + " does not have access"); |
| 2752 | } |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2753 | if (!info.enabledAndUserMatches(user.getIdentifier())) { |
| 2754 | throw new SecurityException(info + " does not have access"); |
| 2755 | } |
| 2756 | } |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2757 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 2758 | private int getUidForPackageAndUser(String pkg, UserHandle user) throws RemoteException { |
| 2759 | int uid = 0; |
| 2760 | long identity = Binder.clearCallingIdentity(); |
| 2761 | try { |
| 2762 | uid = mPackageManager.getPackageUid(pkg, 0, user.getIdentifier()); |
| 2763 | } finally { |
| 2764 | Binder.restoreCallingIdentity(identity); |
| 2765 | } |
| 2766 | return uid; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 2767 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2768 | }; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2769 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 2770 | private void applyAdjustment(NotificationRecord n, Adjustment adjustment) { |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2771 | if (n == null) { |
| 2772 | return; |
| 2773 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2774 | if (adjustment.getSignals() != null) { |
| 2775 | Bundle.setDefusable(adjustment.getSignals(), true); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 2776 | final ArrayList<String> people = |
| 2777 | adjustment.getSignals().getStringArrayList(Adjustment.KEY_PEOPLE); |
| 2778 | final ArrayList<SnoozeCriterion> snoozeCriterionList = |
| 2779 | adjustment.getSignals().getParcelableArrayList(Adjustment.KEY_SNOOZE_CRITERIA); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 2780 | n.setPeopleOverride(people); |
| 2781 | n.setSnoozeCriteria(snoozeCriterionList); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2782 | } |
| 2783 | } |
| 2784 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2785 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2786 | private void addAutogroupKeyLocked(String key) { |
| 2787 | NotificationRecord n = mNotificationsByKey.get(key); |
| 2788 | if (n == null) { |
| 2789 | return; |
| 2790 | } |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 2791 | n.setOverrideGroupKey(GroupHelper.AUTOGROUP_KEY); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2792 | EventLogTags.writeNotificationAutogrouped(key); |
| 2793 | } |
| 2794 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2795 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2796 | private void removeAutogroupKeyLocked(String key) { |
| 2797 | NotificationRecord n = mNotificationsByKey.get(key); |
| 2798 | if (n == null) { |
| 2799 | return; |
| 2800 | } |
Chris Wren | b392179 | 2017-06-01 13:34:46 -0400 | [diff] [blame] | 2801 | n.setOverrideGroupKey(null); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2802 | EventLogTags.writeNotificationUnautogrouped(key); |
| 2803 | } |
| 2804 | |
| 2805 | // Clears the 'fake' auto-group summary. |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 2806 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2807 | private void clearAutogroupSummaryLocked(int userId, String pkg) { |
| 2808 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 2809 | if (summaries != null && summaries.containsKey(pkg)) { |
| 2810 | // Clear summary. |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2811 | final NotificationRecord removed = findNotificationByKeyLocked(summaries.remove(pkg)); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2812 | if (removed != null) { |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 2813 | boolean wasPosted = removeFromNotificationListsLocked(removed); |
| 2814 | cancelNotificationLocked(removed, false, REASON_UNAUTOBUNDLED, wasPosted); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2815 | } |
| 2816 | } |
| 2817 | } |
| 2818 | |
| 2819 | // 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] | 2820 | private void createAutoGroupSummary(int userId, String pkg, String triggeringKey) { |
| 2821 | NotificationRecord summaryRecord = null; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2822 | synchronized (mNotificationLock) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2823 | NotificationRecord notificationRecord = mNotificationsByKey.get(triggeringKey); |
| 2824 | if (notificationRecord == null) { |
| 2825 | // The notification could have been cancelled again already. A successive |
| 2826 | // adjustment will post a summary if needed. |
| 2827 | return; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2828 | } |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2829 | final StatusBarNotification adjustedSbn = notificationRecord.sbn; |
| 2830 | userId = adjustedSbn.getUser().getIdentifier(); |
| 2831 | ArrayMap<String, String> summaries = mAutobundledSummaries.get(userId); |
| 2832 | if (summaries == null) { |
| 2833 | summaries = new ArrayMap<>(); |
| 2834 | } |
| 2835 | mAutobundledSummaries.put(userId, summaries); |
| 2836 | if (!summaries.containsKey(pkg)) { |
| 2837 | // Add summary |
| 2838 | final ApplicationInfo appInfo = |
| 2839 | adjustedSbn.getNotification().extras.getParcelable( |
| 2840 | Notification.EXTRA_BUILDER_APPLICATION_INFO); |
| 2841 | final Bundle extras = new Bundle(); |
| 2842 | extras.putParcelable(Notification.EXTRA_BUILDER_APPLICATION_INFO, appInfo); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 2843 | final String channelId = notificationRecord.getChannel().getId(); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2844 | final Notification summaryNotification = |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 2845 | new Notification.Builder(getContext(), channelId) |
| 2846 | .setSmallIcon(adjustedSbn.getNotification().getSmallIcon()) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2847 | .setGroupSummary(true) |
Julia Reynolds | 9d5786e | 2017-04-28 10:26:32 -0400 | [diff] [blame] | 2848 | .setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN) |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2849 | .setGroup(GroupHelper.AUTOGROUP_KEY) |
| 2850 | .setFlag(Notification.FLAG_AUTOGROUP_SUMMARY, true) |
| 2851 | .setFlag(Notification.FLAG_GROUP_SUMMARY, true) |
| 2852 | .setColor(adjustedSbn.getNotification().color) |
| 2853 | .setLocalOnly(true) |
| 2854 | .build(); |
| 2855 | summaryNotification.extras.putAll(extras); |
| 2856 | Intent appIntent = getContext().getPackageManager().getLaunchIntentForPackage(pkg); |
| 2857 | if (appIntent != null) { |
| 2858 | summaryNotification.contentIntent = PendingIntent.getActivityAsUser( |
| 2859 | getContext(), 0, appIntent, 0, null, UserHandle.of(userId)); |
| 2860 | } |
| 2861 | final StatusBarNotification summarySbn = |
| 2862 | new StatusBarNotification(adjustedSbn.getPackageName(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 2863 | adjustedSbn.getOpPkg(), |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 2864 | Integer.MAX_VALUE, |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2865 | GroupHelper.AUTOGROUP_KEY, adjustedSbn.getUid(), |
| 2866 | adjustedSbn.getInitialPid(), summaryNotification, |
| 2867 | adjustedSbn.getUser(), GroupHelper.AUTOGROUP_KEY, |
| 2868 | System.currentTimeMillis()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 2869 | summaryRecord = new NotificationRecord(getContext(), summarySbn, |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 2870 | notificationRecord.getChannel()); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2871 | summaries.put(pkg, summarySbn.getKey()); |
| 2872 | } |
| 2873 | } |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 2874 | if (summaryRecord != null && checkDisqualifyingFeatures(userId, MY_UID, |
| 2875 | summaryRecord.sbn.getId(), summaryRecord.sbn.getTag(), summaryRecord)) { |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 2876 | mHandler.post(new EnqueueNotificationRunnable(userId, summaryRecord)); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 2877 | } |
| 2878 | } |
| 2879 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 2880 | private String disableNotificationEffects(NotificationRecord record) { |
| 2881 | if (mDisableNotificationEffects) { |
| 2882 | return "booleanState"; |
| 2883 | } |
| 2884 | if ((mListenerHints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 2885 | return "listenerHints"; |
| 2886 | } |
| 2887 | if (mCallState != TelephonyManager.CALL_STATE_IDLE && !mZenModeHelper.isCall(record)) { |
| 2888 | return "callState"; |
| 2889 | } |
| 2890 | return null; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2891 | }; |
| 2892 | |
| 2893 | private void dumpJson(PrintWriter pw, DumpFilter filter) { |
| 2894 | JSONObject dump = new JSONObject(); |
| 2895 | try { |
| 2896 | dump.put("service", "Notification Manager"); |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 2897 | dump.put("bans", mRankingHelper.dumpBansJson(filter)); |
| 2898 | dump.put("ranking", mRankingHelper.dumpJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2899 | dump.put("stats", mUsageStats.dumpJson(filter)); |
Julia Reynolds | d373d78 | 2017-03-03 13:32:57 -0500 | [diff] [blame] | 2900 | dump.put("channels", mRankingHelper.dumpChannelsJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2901 | } catch (JSONException e) { |
| 2902 | e.printStackTrace(); |
| 2903 | } |
| 2904 | pw.println(dump); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2905 | } |
| 2906 | |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 2907 | private void dumpProto(FileDescriptor fd, DumpFilter filter) { |
| 2908 | final ProtoOutputStream proto = new ProtoOutputStream(fd); |
| 2909 | synchronized (mNotificationLock) { |
| 2910 | long records = proto.start(NotificationServiceDumpProto.RECORDS); |
| 2911 | int N = mNotificationList.size(); |
| 2912 | if (N > 0) { |
| 2913 | for (int i = 0; i < N; i++) { |
| 2914 | final NotificationRecord nr = mNotificationList.get(i); |
| 2915 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 2916 | nr.dump(proto, filter.redact); |
| 2917 | proto.write(NotificationRecordProto.STATE, NotificationServiceProto.POSTED); |
| 2918 | } |
| 2919 | } |
| 2920 | N = mEnqueuedNotifications.size(); |
| 2921 | if (N > 0) { |
| 2922 | for (int i = 0; i < N; i++) { |
| 2923 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 2924 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 2925 | nr.dump(proto, filter.redact); |
| 2926 | proto.write(NotificationRecordProto.STATE, NotificationServiceProto.ENQUEUED); |
| 2927 | } |
| 2928 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 2929 | List<NotificationRecord> snoozed = mSnoozeHelper.getSnoozed(); |
| 2930 | N = snoozed.size(); |
| 2931 | if (N > 0) { |
| 2932 | for (int i = 0; i < N; i++) { |
| 2933 | final NotificationRecord nr = snoozed.get(i); |
| 2934 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 2935 | nr.dump(proto, filter.redact); |
| 2936 | proto.write(NotificationRecordProto.STATE, NotificationServiceProto.SNOOZED); |
| 2937 | } |
| 2938 | } |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 2939 | proto.end(records); |
| 2940 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 2941 | |
| 2942 | long zenLog = proto.start(NotificationServiceDumpProto.ZEN); |
| 2943 | mZenModeHelper.dump(proto); |
| 2944 | for (ComponentName suppressor : mEffectsSuppressors) { |
| 2945 | proto.write(ZenModeProto.SUPPRESSORS, suppressor.toString()); |
| 2946 | } |
| 2947 | proto.end(zenLog); |
| 2948 | |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 2949 | proto.flush(); |
| 2950 | } |
| 2951 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2952 | void dumpImpl(PrintWriter pw, DumpFilter filter) { |
| 2953 | pw.print("Current Notification Manager state"); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 2954 | if (filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2955 | pw.print(" (filtered to "); pw.print(filter); pw.print(")"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2956 | } |
| 2957 | pw.println(':'); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2958 | int N; |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 2959 | final boolean zenOnly = filter.filtered && filter.zen; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2960 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2961 | if (!zenOnly) { |
| 2962 | synchronized (mToastQueue) { |
| 2963 | N = mToastQueue.size(); |
| 2964 | if (N > 0) { |
| 2965 | pw.println(" Toast Queue:"); |
| 2966 | for (int i=0; i<N; i++) { |
| 2967 | mToastQueue.get(i).dump(pw, " ", filter); |
| 2968 | } |
| 2969 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2970 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2971 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2972 | } |
| 2973 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 2974 | synchronized (mNotificationLock) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2975 | if (!zenOnly) { |
| 2976 | N = mNotificationList.size(); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2977 | if (N > 0) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2978 | pw.println(" Notification List:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2979 | for (int i=0; i<N; i++) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2980 | final NotificationRecord nr = mNotificationList.get(i); |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 2981 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 2982 | nr.dump(pw, " ", getContext(), filter.redact); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2983 | } |
| 2984 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2985 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2986 | |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 2987 | if (!filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2988 | N = mLights.size(); |
| 2989 | if (N > 0) { |
| 2990 | pw.println(" Lights List:"); |
| 2991 | for (int i=0; i<N; i++) { |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 2992 | if (i == N - 1) { |
| 2993 | pw.print(" > "); |
| 2994 | } else { |
| 2995 | pw.print(" "); |
| 2996 | } |
| 2997 | pw.println(mLights.get(i)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2998 | } |
| 2999 | pw.println(" "); |
| 3000 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 3001 | pw.println(" mUseAttentionLight=" + mUseAttentionLight); |
| 3002 | pw.println(" mNotificationPulseEnabled=" + mNotificationPulseEnabled); |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3003 | pw.println(" mSoundNotificationKey=" + mSoundNotificationKey); |
| 3004 | pw.println(" mVibrateNotificationKey=" + mVibrateNotificationKey); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3005 | pw.println(" mDisableNotificationEffects=" + mDisableNotificationEffects); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3006 | pw.println(" mCallState=" + callStateToString(mCallState)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3007 | pw.println(" mSystemReady=" + mSystemReady); |
Chris Wren | 763a9bb | 2016-05-31 17:14:12 -0400 | [diff] [blame] | 3008 | pw.println(" mMaxPackageEnqueueRate=" + mMaxPackageEnqueueRate); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3009 | } |
| 3010 | pw.println(" mArchive=" + mArchive.toString()); |
| 3011 | Iterator<StatusBarNotification> iter = mArchive.descendingIterator(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3012 | int j=0; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3013 | while (iter.hasNext()) { |
| 3014 | final StatusBarNotification sbn = iter.next(); |
| 3015 | if (filter != null && !filter.matches(sbn)) continue; |
| 3016 | pw.println(" " + sbn); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3017 | if (++j >= 5) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3018 | if (iter.hasNext()) pw.println(" ..."); |
| 3019 | break; |
| 3020 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3021 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3022 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3023 | if (!zenOnly) { |
| 3024 | N = mEnqueuedNotifications.size(); |
| 3025 | if (N > 0) { |
| 3026 | pw.println(" Enqueued Notification List:"); |
| 3027 | for (int i = 0; i < N; i++) { |
| 3028 | final NotificationRecord nr = mEnqueuedNotifications.get(i); |
| 3029 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
| 3030 | nr.dump(pw, " ", getContext(), filter.redact); |
| 3031 | } |
| 3032 | pw.println(" "); |
| 3033 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3034 | |
| 3035 | mSnoozeHelper.dump(pw, filter); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3036 | } |
| 3037 | } |
| 3038 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3039 | if (!zenOnly) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3040 | pw.println("\n Ranking Config:"); |
| 3041 | mRankingHelper.dump(pw, " ", filter); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 3042 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3043 | pw.println("\n Notification listeners:"); |
| 3044 | mListeners.dump(pw, filter); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3045 | pw.print(" mListenerHints: "); pw.println(mListenerHints); |
| 3046 | pw.print(" mListenersDisablingEffects: ("); |
| 3047 | N = mListenersDisablingEffects.size(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3048 | for (int i = 0; i < N; i++) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 3049 | final int hint = mListenersDisablingEffects.keyAt(i); |
| 3050 | if (i > 0) pw.print(';'); |
| 3051 | pw.print("hint[" + hint + "]:"); |
| 3052 | |
| 3053 | final ArraySet<ManagedServiceInfo> listeners = |
| 3054 | mListenersDisablingEffects.valueAt(i); |
| 3055 | final int listenerSize = listeners.size(); |
| 3056 | |
| 3057 | for (int j = 0; j < listenerSize; j++) { |
| 3058 | if (i > 0) pw.print(','); |
| 3059 | final ManagedServiceInfo listener = listeners.valueAt(i); |
| 3060 | pw.print(listener.component); |
| 3061 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3062 | } |
| 3063 | pw.println(')'); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 3064 | pw.println("\n Notification assistant services:"); |
| 3065 | mNotificationAssistants.dump(pw, filter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3066 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3067 | |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3068 | if (!filter.filtered || zenOnly) { |
| 3069 | pw.println("\n Zen Mode:"); |
| 3070 | pw.print(" mInterruptionFilter="); pw.println(mInterruptionFilter); |
| 3071 | mZenModeHelper.dump(pw, " "); |
| 3072 | |
| 3073 | pw.println("\n Zen Log:"); |
| 3074 | ZenLog.dump(pw, " "); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 3075 | } |
| 3076 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 3077 | pw.println("\n Policy access:"); |
| 3078 | pw.print(" mPolicyAccess: "); pw.println(mPolicyAccess); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 3079 | |
| 3080 | pw.println("\n Condition providers:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3081 | mConditionProviders.dump(pw, filter); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3082 | |
| 3083 | pw.println("\n Group summaries:"); |
| 3084 | for (Entry<String, NotificationRecord> entry : mSummaryByGroupKey.entrySet()) { |
| 3085 | NotificationRecord r = entry.getValue(); |
| 3086 | pw.println(" " + entry.getKey() + " -> " + r.getKey()); |
| 3087 | if (mNotificationsByKey.get(r.getKey()) != r) { |
| 3088 | pw.println("!!!!!!LEAK: Record not found in mNotificationsByKey."); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3089 | r.dump(pw, " ", getContext(), filter.redact); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3090 | } |
| 3091 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3092 | |
| 3093 | if (!zenOnly) { |
| 3094 | pw.println("\n Usage Stats:"); |
| 3095 | mUsageStats.dump(pw, " ", filter); |
| 3096 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3097 | } |
| 3098 | } |
| 3099 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3100 | /** |
| 3101 | * The private API only accessible to the system process. |
| 3102 | */ |
| 3103 | private final NotificationManagerInternal mInternalService = new NotificationManagerInternal() { |
| 3104 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3105 | public void enqueueNotification(String pkg, String opPkg, int callingUid, int callingPid, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3106 | String tag, int id, Notification notification, int userId) { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3107 | enqueueNotificationInternal(pkg, opPkg, callingUid, callingPid, tag, id, notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3108 | userId); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3109 | } |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3110 | |
| 3111 | @Override |
| 3112 | public void removeForegroundServiceFlagFromNotification(String pkg, int notificationId, |
| 3113 | int userId) { |
| 3114 | checkCallerIsSystem(); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3115 | mHandler.post(new Runnable() { |
| 3116 | @Override |
| 3117 | public void run() { |
| 3118 | synchronized (mNotificationLock) { |
Julia Reynolds | feb7341 | 2017-04-18 09:28:22 -0400 | [diff] [blame] | 3119 | removeForegroundServiceFlagByListLocked( |
| 3120 | mEnqueuedNotifications, pkg, notificationId, userId); |
| 3121 | removeForegroundServiceFlagByListLocked( |
| 3122 | mNotificationList, pkg, notificationId, userId); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3123 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3124 | } |
| 3125 | }); |
| 3126 | } |
| 3127 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3128 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3129 | private void removeForegroundServiceFlagByListLocked( |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3130 | ArrayList<NotificationRecord> notificationList, String pkg, int notificationId, |
| 3131 | int userId) { |
| 3132 | NotificationRecord r = findNotificationByListLocked( |
| 3133 | notificationList, pkg, null, notificationId, userId); |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3134 | if (r == null) { |
| 3135 | return; |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3136 | } |
Geoffrey Pitsch | 87237d7 | 2017-04-13 13:44:09 -0400 | [diff] [blame] | 3137 | StatusBarNotification sbn = r.sbn; |
| 3138 | // NoMan adds flags FLAG_NO_CLEAR and FLAG_ONGOING_EVENT when it sees |
| 3139 | // FLAG_FOREGROUND_SERVICE. Hence it's not enough to remove |
| 3140 | // FLAG_FOREGROUND_SERVICE, we have to revert to the flags we received |
| 3141 | // initially *and* force remove FLAG_FOREGROUND_SERVICE. |
| 3142 | sbn.getNotification().flags = |
| 3143 | (r.mOriginalFlags & ~Notification.FLAG_FOREGROUND_SERVICE); |
| 3144 | mRankingHelper.sort(mNotificationList); |
| 3145 | mListeners.notifyPostedLocked(sbn, sbn /* oldSbn */); |
| 3146 | mGroupHelper.onNotificationPosted(sbn); |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 3147 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3148 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3149 | |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 3150 | void enqueueNotificationInternal(final String pkg, final String opPkg, final int callingUid, |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 3151 | final int callingPid, final String tag, final int id, final Notification notification, |
Julia Reynolds | fea6f7b | 2017-04-19 13:50:12 -0400 | [diff] [blame] | 3152 | int incomingUserId) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 3153 | if (DBG) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3154 | Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id |
| 3155 | + " notification=" + notification); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 3156 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3157 | checkCallerIsSystemOrSameApp(pkg); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3158 | |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 3159 | final int userId = ActivityManager.handleIncomingUser(callingPid, |
| 3160 | callingUid, incomingUserId, true, false, "enqueueNotification", pkg); |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 3161 | final UserHandle user = new UserHandle(userId); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3162 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3163 | if (pkg == null || notification == null) { |
| 3164 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 3165 | + " id=" + id + " notification=" + notification); |
| 3166 | } |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3167 | |
| 3168 | // The system can post notifications for any package, let us resolve that. |
| 3169 | final int notificationUid = resolveNotificationUid(opPkg, callingUid, userId); |
| 3170 | |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3171 | // Fix the notification as best we can. |
| 3172 | try { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3173 | final ApplicationInfo ai = mPackageManagerClient.getApplicationInfoAsUser( |
Jeff Sharkey | 012bc7b | 2016-04-11 16:30:27 -0600 | [diff] [blame] | 3174 | pkg, PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 3175 | (userId == UserHandle.USER_ALL) ? UserHandle.USER_SYSTEM : userId); |
Julia Reynolds | e071abd | 2017-03-22 10:52:11 -0400 | [diff] [blame] | 3176 | Notification.addFieldsFromContext(ai, notification); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 3177 | } catch (NameNotFoundException e) { |
| 3178 | Slog.e(TAG, "Cannot create a context for sending app", e); |
| 3179 | return; |
| 3180 | } |
| 3181 | |
Chris Wren | 888b7a8 | 2016-06-17 15:47:19 -0400 | [diff] [blame] | 3182 | mUsageStats.registerEnqueuedByApp(pkg); |
| 3183 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3184 | // setup local book-keeping |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 3185 | String channelId = notification.getChannelId(); |
| 3186 | if (mIsTelevision && (new Notification.TvExtender(notification)).getChannelId() != null) { |
| 3187 | channelId = (new Notification.TvExtender(notification)).getChannelId(); |
Julia Reynolds | 5f20e9f | 2017-01-30 08:54:53 -0500 | [diff] [blame] | 3188 | } |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3189 | final NotificationChannel channel = mRankingHelper.getNotificationChannel(pkg, |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3190 | notificationUid, channelId, false /* includeDeleted */); |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3191 | if (channel == null) { |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3192 | final String noChannelStr = "No Channel found for " |
| 3193 | + "pkg=" + pkg |
| 3194 | + ", channelId=" + channelId |
Julia Reynolds | f26eb91 | 2017-05-22 15:47:06 -0400 | [diff] [blame] | 3195 | + ", id=" + id |
| 3196 | + ", tag=" + tag |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3197 | + ", opPkg=" + opPkg |
| 3198 | + ", callingUid=" + callingUid |
| 3199 | + ", userId=" + userId |
| 3200 | + ", incomingUserId=" + incomingUserId |
| 3201 | + ", notificationUid=" + notificationUid |
| 3202 | + ", notification=" + notification; |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3203 | Log.e(TAG, noChannelStr); |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 3204 | doChannelWarningToast("Developer warning for package \"" + pkg + "\"\n" + |
Geoffrey Pitsch | cadb5dc | 2017-04-11 11:35:02 -0400 | [diff] [blame] | 3205 | "Failed to post notification on channel \"" + channelId + "\"\n" + |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3206 | "See log for more details"); |
Geoffrey Pitsch | 96cac7f | 2017-03-23 17:57:12 -0400 | [diff] [blame] | 3207 | return; |
Geoffrey Pitsch | 1f17e02 | 2017-01-03 16:44:20 -0500 | [diff] [blame] | 3208 | } |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3209 | |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 3210 | final StatusBarNotification n = new StatusBarNotification( |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3211 | pkg, opPkg, id, tag, notificationUid, callingPid, notification, |
Julia Reynolds | 423b9fc | 2016-11-09 09:51:08 -0500 | [diff] [blame] | 3212 | user, null, System.currentTimeMillis()); |
Geoffrey Pitsch | a22f644 | 2017-05-05 16:47:38 +0000 | [diff] [blame] | 3213 | final NotificationRecord r = new NotificationRecord(getContext(), n, channel); |
Chris Wren | a61f179 | 2016-08-04 11:24:42 -0400 | [diff] [blame] | 3214 | |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 3215 | if (!checkDisqualifyingFeatures(userId, notificationUid, id, tag, r)) { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3216 | return; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 3217 | } |
| 3218 | |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 3219 | // Whitelist pending intents. |
| 3220 | if (notification.allPendingIntents != null) { |
| 3221 | final int intentCount = notification.allPendingIntents.size(); |
| 3222 | if (intentCount > 0) { |
| 3223 | final ActivityManagerInternal am = LocalServices |
| 3224 | .getService(ActivityManagerInternal.class); |
| 3225 | final long duration = LocalServices.getService( |
| 3226 | DeviceIdleController.LocalService.class).getNotificationWhitelistDuration(); |
| 3227 | for (int i = 0; i < intentCount; i++) { |
| 3228 | PendingIntent pendingIntent = notification.allPendingIntents.valueAt(i); |
| 3229 | if (pendingIntent != null) { |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 3230 | am.setPendingIntentWhitelistDuration(pendingIntent.getTarget(), |
| 3231 | WHITELIST_TOKEN, duration); |
Felipe Leme | dd85da6 | 2016-06-28 11:29:54 -0700 | [diff] [blame] | 3232 | } |
| 3233 | } |
| 3234 | } |
| 3235 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 3236 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3237 | mHandler.post(new EnqueueNotificationRunnable(userId, r)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3238 | } |
| 3239 | |
Geoffrey Pitsch | 4c6eef2 | 2017-04-19 10:26:45 -0400 | [diff] [blame] | 3240 | private void doChannelWarningToast(CharSequence toastText) { |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 3241 | final int defaultWarningEnabled = Build.IS_DEBUGGABLE ? 1 : 0; |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 3242 | final boolean warningEnabled = Settings.Global.getInt(getContext().getContentResolver(), |
Geoffrey Pitsch | 507822d | 2017-05-11 12:57:22 -0400 | [diff] [blame] | 3243 | Settings.Global.SHOW_NOTIFICATION_CHANNEL_WARNINGS, defaultWarningEnabled) != 0; |
| 3244 | if (warningEnabled) { |
Geoffrey Pitsch | 2486f89 | 2017-05-22 10:53:44 -0400 | [diff] [blame] | 3245 | Toast toast = Toast.makeText(getContext(), mHandler.getLooper(), toastText, |
Geoffrey Pitsch | 5bdddbd | 2017-05-26 10:50:05 -0400 | [diff] [blame] | 3246 | Toast.LENGTH_SHORT); |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 3247 | toast.show(); |
Geoffrey Pitsch | 86c11e60 | 2017-04-17 15:28:40 -0400 | [diff] [blame] | 3248 | } |
| 3249 | } |
| 3250 | |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3251 | private int resolveNotificationUid(String opPackageName, int callingUid, int userId) { |
| 3252 | // The system can post notifications on behalf of any package it wants |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3253 | if (isCallerSystemOrPhone() && opPackageName != null && !"android".equals(opPackageName)) { |
Svetoslav Ganov | 2a67ee8 | 2017-02-16 18:57:57 -0800 | [diff] [blame] | 3254 | try { |
| 3255 | return getContext().getPackageManager() |
| 3256 | .getPackageUidAsUser(opPackageName, userId); |
| 3257 | } catch (NameNotFoundException e) { |
| 3258 | /* ignore */ |
| 3259 | } |
| 3260 | } |
| 3261 | return callingUid; |
| 3262 | } |
| 3263 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3264 | /** |
| 3265 | * Checks if a notification can be posted. checks rate limiter, snooze helper, and blocking. |
| 3266 | * |
| 3267 | * Has side effects. |
| 3268 | */ |
| 3269 | private boolean checkDisqualifyingFeatures(int userId, int callingUid, int id, String tag, |
| 3270 | NotificationRecord r) { |
| 3271 | final String pkg = r.sbn.getPackageName(); |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 3272 | final boolean isSystemNotification = |
| 3273 | isUidSystemOrPhone(callingUid) || ("android".equals(pkg)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3274 | final boolean isNotificationFromListener = mListeners.isListenerPackage(pkg); |
| 3275 | |
| 3276 | // Limit the number of notifications that any given package except the android |
| 3277 | // package or a registered listener can enqueue. Prevents DOS attacks and deals with leaks. |
| 3278 | if (!isSystemNotification && !isNotificationFromListener) { |
| 3279 | synchronized (mNotificationLock) { |
| 3280 | if (mNotificationsByKey.get(r.sbn.getKey()) != null) { |
| 3281 | // this is an update, rate limit updates only |
| 3282 | final float appEnqueueRate = mUsageStats.getAppEnqueueRate(pkg); |
| 3283 | if (appEnqueueRate > mMaxPackageEnqueueRate) { |
| 3284 | mUsageStats.registerOverRateQuota(pkg); |
| 3285 | final long now = SystemClock.elapsedRealtime(); |
| 3286 | if ((now - mLastOverRateLogTime) > MIN_PACKAGE_OVERRATE_LOG_INTERVAL) { |
| 3287 | Slog.e(TAG, "Package enqueue rate is " + appEnqueueRate |
| 3288 | + ". Shedding events. package=" + pkg); |
| 3289 | mLastOverRateLogTime = now; |
| 3290 | } |
| 3291 | return false; |
| 3292 | } |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 3293 | } else if (isCallerInstantApp(pkg)) { |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3294 | // Ephemeral apps have some special constraints for notifications. |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 3295 | // They are not allowed to create new notifications however they are allowed to |
| 3296 | // update notifications created by the system (e.g. a foreground service |
| 3297 | // notification). |
| 3298 | throw new SecurityException("Instant app " + pkg |
| 3299 | + " cannot create notifications"); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | int count = 0; |
| 3303 | final int N = mNotificationList.size(); |
| 3304 | for (int i=0; i<N; i++) { |
| 3305 | final NotificationRecord existing = mNotificationList.get(i); |
| 3306 | if (existing.sbn.getPackageName().equals(pkg) |
| 3307 | && existing.sbn.getUserId() == userId) { |
| 3308 | if (existing.sbn.getId() == id |
| 3309 | && TextUtils.equals(existing.sbn.getTag(), tag)) { |
| 3310 | break; // Allow updating existing notification |
| 3311 | } |
| 3312 | count++; |
| 3313 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 3314 | mUsageStats.registerOverCountQuota(pkg); |
| 3315 | Slog.e(TAG, "Package has already posted " + count |
| 3316 | + " notifications. Not showing more. package=" + pkg); |
| 3317 | return false; |
| 3318 | } |
| 3319 | } |
| 3320 | } |
| 3321 | } |
| 3322 | } |
| 3323 | |
| 3324 | // snoozed apps |
| 3325 | if (mSnoozeHelper.isSnoozed(userId, pkg, r.getKey())) { |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 3326 | MetricsLogger.action(r.getLogMaker() |
| 3327 | .setType(MetricsProto.MetricsEvent.TYPE_UPDATE) |
| 3328 | .setCategory(MetricsProto.MetricsEvent.NOTIFICATION_SNOOZED)); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3329 | if (DBG) { |
| 3330 | Slog.d(TAG, "Ignored enqueue for snoozed notification " + r.getKey()); |
| 3331 | } |
| 3332 | mSnoozeHelper.update(userId, r); |
| 3333 | savePolicyFile(); |
| 3334 | return false; |
| 3335 | } |
| 3336 | |
| 3337 | |
| 3338 | // blocked apps |
| 3339 | if (isBlocked(r, mUsageStats)) { |
| 3340 | return false; |
| 3341 | } |
| 3342 | |
| 3343 | return true; |
| 3344 | } |
| 3345 | |
| 3346 | protected boolean isBlocked(NotificationRecord r, NotificationUsageStats usageStats) { |
| 3347 | final String pkg = r.sbn.getPackageName(); |
| 3348 | final int callingUid = r.sbn.getUid(); |
| 3349 | |
| 3350 | final boolean isPackageSuspended = isPackageSuspendedForUser(pkg, callingUid); |
| 3351 | if (isPackageSuspended) { |
| 3352 | Slog.e(TAG, "Suppressing notification from package due to package " |
| 3353 | + "suspended by administrator."); |
| 3354 | usageStats.registerSuspendedByAdmin(r); |
| 3355 | return isPackageSuspended; |
| 3356 | } |
| 3357 | |
Julia Reynolds | 4da7970 | 2017-06-01 11:06:10 -0400 | [diff] [blame] | 3358 | final boolean isBlocked = |
| 3359 | mRankingHelper.getImportance(pkg, callingUid) == NotificationManager.IMPORTANCE_NONE |
Julia Reynolds | 5fe2eae | 2017-05-22 08:45:27 -0400 | [diff] [blame] | 3360 | || r.getChannel().getImportance() == NotificationManager.IMPORTANCE_NONE; |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3361 | if (isBlocked) { |
| 3362 | Slog.e(TAG, "Suppressing notification from package by user request."); |
| 3363 | usageStats.registerBlocked(r); |
| 3364 | } |
| 3365 | return isBlocked; |
| 3366 | } |
| 3367 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3368 | protected class SnoozeNotificationRunnable implements Runnable { |
| 3369 | private final String mKey; |
| 3370 | private final long mDuration; |
| 3371 | private final String mSnoozeCriterionId; |
| 3372 | |
| 3373 | SnoozeNotificationRunnable(String key, long duration, String snoozeCriterionId) { |
| 3374 | mKey = key; |
| 3375 | mDuration = duration; |
| 3376 | mSnoozeCriterionId = snoozeCriterionId; |
| 3377 | } |
| 3378 | |
| 3379 | @Override |
| 3380 | public void run() { |
| 3381 | synchronized (mNotificationLock) { |
| 3382 | final NotificationRecord r = findNotificationByKeyLocked(mKey); |
| 3383 | if (r != null) { |
| 3384 | snoozeLocked(r); |
| 3385 | } |
| 3386 | } |
| 3387 | } |
| 3388 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3389 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3390 | void snoozeLocked(NotificationRecord r) { |
| 3391 | if (r.sbn.isGroup()) { |
| 3392 | final List<NotificationRecord> groupNotifications = findGroupNotificationsLocked( |
| 3393 | r.sbn.getPackageName(), r.sbn.getGroupKey(), r.sbn.getUserId()); |
| 3394 | if (r.getNotification().isGroupSummary()) { |
| 3395 | // snooze summary and all children |
| 3396 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 3397 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 3398 | } |
| 3399 | } else { |
| 3400 | // if there is a valid summary for this group, and we are snoozing the only |
| 3401 | // child, also snooze the summary |
| 3402 | if (mSummaryByGroupKey.containsKey(r.sbn.getGroupKey())) { |
| 3403 | if (groupNotifications.size() != 2) { |
| 3404 | snoozeNotificationLocked(r); |
| 3405 | } else { |
| 3406 | // snooze summary and the one child |
| 3407 | for (int i = 0; i < groupNotifications.size(); i++) { |
| 3408 | snoozeNotificationLocked(groupNotifications.get(i)); |
| 3409 | } |
| 3410 | } |
| 3411 | } else { |
| 3412 | snoozeNotificationLocked(r); |
| 3413 | } |
| 3414 | } |
| 3415 | } else { |
| 3416 | // just snooze the one notification |
| 3417 | snoozeNotificationLocked(r); |
| 3418 | } |
| 3419 | } |
| 3420 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3421 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3422 | void snoozeNotificationLocked(NotificationRecord r) { |
| 3423 | MetricsLogger.action(r.getLogMaker() |
| 3424 | .setCategory(MetricsEvent.NOTIFICATION_SNOOZED) |
| 3425 | .setType(MetricsEvent.TYPE_CLOSE) |
| 3426 | .addTaggedData(MetricsEvent.NOTIFICATION_SNOOZED_CRITERIA, |
| 3427 | mSnoozeCriterionId == null ? 0 : 1)); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 3428 | boolean wasPosted = removeFromNotificationListsLocked(r); |
| 3429 | cancelNotificationLocked(r, false, REASON_SNOOZED, wasPosted); |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3430 | updateLightsLocked(); |
| 3431 | if (mSnoozeCriterionId != null) { |
| 3432 | mNotificationAssistants.notifyAssistantSnoozedLocked(r.sbn, mSnoozeCriterionId); |
| 3433 | mSnoozeHelper.snooze(r); |
| 3434 | } else { |
| 3435 | mSnoozeHelper.snooze(r, mDuration); |
| 3436 | } |
| 3437 | savePolicyFile(); |
| 3438 | } |
| 3439 | } |
| 3440 | |
Julia Reynolds | baff400 | 2016-12-15 11:34:26 -0500 | [diff] [blame] | 3441 | protected class EnqueueNotificationRunnable implements Runnable { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3442 | private final NotificationRecord r; |
| 3443 | private final int userId; |
| 3444 | |
| 3445 | EnqueueNotificationRunnable(int userId, NotificationRecord r) { |
| 3446 | this.userId = userId; |
| 3447 | this.r = r; |
| 3448 | }; |
| 3449 | |
| 3450 | @Override |
| 3451 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3452 | synchronized (mNotificationLock) { |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 3453 | mEnqueuedNotifications.add(r); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 3454 | scheduleTimeoutLocked(r); |
Julia Reynolds | 573c653 | 2017-01-24 17:44:38 -0500 | [diff] [blame] | 3455 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3456 | final StatusBarNotification n = r.sbn; |
| 3457 | if (DBG) Slog.d(TAG, "EnqueueNotificationRunnable.run for: " + n.getKey()); |
| 3458 | NotificationRecord old = mNotificationsByKey.get(n.getKey()); |
| 3459 | if (old != null) { |
| 3460 | // Retain ranking information from previous record |
| 3461 | r.copyRankingInformation(old); |
| 3462 | } |
| 3463 | |
| 3464 | final int callingUid = n.getUid(); |
| 3465 | final int callingPid = n.getInitialPid(); |
| 3466 | final Notification notification = n.getNotification(); |
| 3467 | final String pkg = n.getPackageName(); |
| 3468 | final int id = n.getId(); |
| 3469 | final String tag = n.getTag(); |
| 3470 | |
| 3471 | // Handle grouped notifications and bail out early if we |
| 3472 | // can to avoid extracting signals. |
| 3473 | handleGroupedNotificationLocked(r, old, callingUid, callingPid); |
| 3474 | |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 3475 | // if this is a group child, unsnooze parent summary |
| 3476 | if (n.isGroup() && notification.isGroupChild()) { |
| 3477 | mSnoozeHelper.repostGroupSummary(pkg, r.getUserId(), n.getGroupKey()); |
| 3478 | } |
| 3479 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3480 | // This conditional is a dirty hack to limit the logging done on |
| 3481 | // behalf of the download manager without affecting other apps. |
| 3482 | if (!pkg.equals("com.android.providers.downloads") |
| 3483 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| 3484 | int enqueueStatus = EVENTLOG_ENQUEUE_STATUS_NEW; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3485 | if (old != null) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3486 | enqueueStatus = EVENTLOG_ENQUEUE_STATUS_UPDATE; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3487 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3488 | EventLogTags.writeNotificationEnqueue(callingUid, callingPid, |
| 3489 | pkg, id, tag, userId, notification.toString(), |
| 3490 | enqueueStatus); |
| 3491 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3492 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3493 | mRankingHelper.extractSignals(r); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3494 | |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3495 | // tell the assistant service about the notification |
| 3496 | if (mNotificationAssistants.isEnabled()) { |
| 3497 | mNotificationAssistants.onNotificationEnqueued(r); |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3498 | mHandler.postDelayed(new PostNotificationRunnable(r.getKey()), |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3499 | DELAY_FOR_ASSISTANT_TIME); |
| 3500 | } else { |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3501 | mHandler.post(new PostNotificationRunnable(r.getKey())); |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3502 | } |
| 3503 | } |
| 3504 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | protected class PostNotificationRunnable implements Runnable { |
| 3508 | private final String key; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3509 | |
Julia Reynolds | d94054f | 2017-02-01 11:11:06 -0500 | [diff] [blame] | 3510 | PostNotificationRunnable(String key) { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3511 | this.key = key; |
| 3512 | } |
| 3513 | |
| 3514 | @Override |
| 3515 | public void run() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3516 | synchronized (mNotificationLock) { |
| 3517 | try { |
| 3518 | NotificationRecord r = null; |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3519 | int N = mEnqueuedNotifications.size(); |
| 3520 | for (int i = 0; i < N; i++) { |
| 3521 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 3522 | if (Objects.equals(key, enqueued.getKey())) { |
| 3523 | r = enqueued; |
| 3524 | break; |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3525 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3526 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3527 | if (r == null) { |
| 3528 | Slog.i(TAG, "Cannot find enqueued record for key: " + key); |
| 3529 | return; |
| 3530 | } |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3531 | NotificationRecord old = mNotificationsByKey.get(key); |
| 3532 | final StatusBarNotification n = r.sbn; |
| 3533 | final Notification notification = n.getNotification(); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3534 | int index = indexOfNotificationLocked(n.getKey()); |
| 3535 | if (index < 0) { |
| 3536 | mNotificationList.add(r); |
| 3537 | mUsageStats.registerPostedByApp(r); |
| 3538 | } else { |
| 3539 | old = mNotificationList.get(index); |
| 3540 | mNotificationList.set(index, r); |
| 3541 | mUsageStats.registerUpdatedByApp(r, old); |
| 3542 | // Make sure we don't lose the foreground service state. |
| 3543 | notification.flags |= |
| 3544 | old.getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE; |
| 3545 | r.isUpdate = true; |
| 3546 | } |
| 3547 | |
| 3548 | mNotificationsByKey.put(n.getKey(), r); |
| 3549 | |
| 3550 | // Ensure if this is a foreground service that the proper additional |
| 3551 | // flags are set. |
| 3552 | if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 3553 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 3554 | | Notification.FLAG_NO_CLEAR; |
| 3555 | } |
| 3556 | |
| 3557 | applyZenModeLocked(r); |
| 3558 | mRankingHelper.sort(mNotificationList); |
| 3559 | |
| 3560 | if (notification.getSmallIcon() != null) { |
| 3561 | StatusBarNotification oldSbn = (old != null) ? old.sbn : null; |
| 3562 | mListeners.notifyPostedLocked(n, oldSbn); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3563 | mHandler.post(new Runnable() { |
| 3564 | @Override |
| 3565 | public void run() { |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3566 | mGroupHelper.onNotificationPosted(n); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 3567 | } |
| 3568 | }); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3569 | } else { |
| 3570 | Slog.e(TAG, "Not posting notification without small icon: " + notification); |
| 3571 | if (old != null && !old.isCanceled) { |
| 3572 | mListeners.notifyRemovedLocked(n, |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 3573 | NotificationListenerService.REASON_ERROR); |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3574 | mHandler.post(new Runnable() { |
| 3575 | @Override |
| 3576 | public void run() { |
| 3577 | mGroupHelper.onNotificationRemoved(n); |
| 3578 | } |
| 3579 | }); |
| 3580 | } |
| 3581 | // ATTENTION: in a future release we will bail out here |
| 3582 | // so that we do not play sounds, show lights, etc. for invalid |
| 3583 | // notifications |
| 3584 | Slog.e(TAG, "WARNING: In a future release this will crash the app: " |
| 3585 | + n.getPackageName()); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3586 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3587 | |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3588 | buzzBeepBlinkLocked(r); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3589 | } finally { |
Julia Reynolds | 4b82f6d | 2017-01-04 10:47:41 -0500 | [diff] [blame] | 3590 | int N = mEnqueuedNotifications.size(); |
| 3591 | for (int i = 0; i < N; i++) { |
| 3592 | final NotificationRecord enqueued = mEnqueuedNotifications.get(i); |
| 3593 | if (Objects.equals(key, enqueued.getKey())) { |
| 3594 | mEnqueuedNotifications.remove(i); |
| 3595 | break; |
| 3596 | } |
| 3597 | } |
Chris Wren | 6676dab | 2016-12-21 18:26:27 -0500 | [diff] [blame] | 3598 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3599 | } |
| 3600 | } |
| 3601 | } |
| 3602 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3603 | /** |
| 3604 | * Ensures that grouped notification receive their special treatment. |
| 3605 | * |
| 3606 | * <p>Cancels group children if the new notification causes a group to lose |
| 3607 | * its summary.</p> |
| 3608 | * |
| 3609 | * <p>Updates mSummaryByGroupKey.</p> |
| 3610 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3611 | @GuardedBy("mNotificationLock") |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3612 | private void handleGroupedNotificationLocked(NotificationRecord r, NotificationRecord old, |
| 3613 | int callingUid, int callingPid) { |
| 3614 | StatusBarNotification sbn = r.sbn; |
| 3615 | Notification n = sbn.getNotification(); |
Selim Cinek | 5b03ce9 | 2016-05-18 15:16:58 -0700 | [diff] [blame] | 3616 | if (n.isGroupSummary() && !sbn.isAppGroup()) { |
| 3617 | // notifications without a group shouldn't be a summary, otherwise autobundling can |
| 3618 | // lead to bugs |
| 3619 | n.flags &= ~Notification.FLAG_GROUP_SUMMARY; |
| 3620 | } |
| 3621 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3622 | String group = sbn.getGroupKey(); |
| 3623 | boolean isSummary = n.isGroupSummary(); |
| 3624 | |
| 3625 | Notification oldN = old != null ? old.sbn.getNotification() : null; |
| 3626 | String oldGroup = old != null ? old.sbn.getGroupKey() : null; |
| 3627 | boolean oldIsSummary = old != null && oldN.isGroupSummary(); |
| 3628 | |
| 3629 | if (oldIsSummary) { |
| 3630 | NotificationRecord removedSummary = mSummaryByGroupKey.remove(oldGroup); |
| 3631 | if (removedSummary != old) { |
| 3632 | String removedKey = |
| 3633 | removedSummary != null ? removedSummary.getKey() : "<null>"; |
| 3634 | Slog.w(TAG, "Removed summary didn't match old notification: old=" + old.getKey() + |
| 3635 | ", removed=" + removedKey); |
| 3636 | } |
| 3637 | } |
| 3638 | if (isSummary) { |
| 3639 | mSummaryByGroupKey.put(group, r); |
| 3640 | } |
| 3641 | |
| 3642 | // Clear out group children of the old notification if the update |
| 3643 | // causes the group summary to go away. This happens when the old |
| 3644 | // notification was a summary and the new one isn't, or when the old |
| 3645 | // notification was a summary and its group key changed. |
| 3646 | if (oldIsSummary && (!isSummary || !oldGroup.equals(group))) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 3647 | cancelGroupChildrenLocked(old, callingUid, callingPid, null, false /* sendDelete */); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3648 | } |
| 3649 | } |
| 3650 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3651 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3652 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 3653 | void scheduleTimeoutLocked(NotificationRecord record) { |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 3654 | if (record.getNotification().getTimeoutAfter() > 0) { |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 3655 | final PendingIntent pi = PendingIntent.getBroadcast(getContext(), |
| 3656 | REQUEST_CODE_TIMEOUT, |
| 3657 | new Intent(ACTION_NOTIFICATION_TIMEOUT) |
| 3658 | .setData(new Uri.Builder().scheme(SCHEME_TIMEOUT) |
| 3659 | .appendPath(record.getKey()).build()) |
| 3660 | .addFlags(Intent.FLAG_RECEIVER_FOREGROUND) |
| 3661 | .putExtra(EXTRA_KEY, record.getKey()), |
| 3662 | PendingIntent.FLAG_UPDATE_CURRENT); |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 3663 | mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, |
Julia Reynolds | bad4297 | 2017-04-25 13:52:49 -0400 | [diff] [blame] | 3664 | SystemClock.elapsedRealtime() + record.getNotification().getTimeoutAfter(), pi); |
Julia Reynolds | 2a12874 | 2016-11-28 14:29:25 -0500 | [diff] [blame] | 3665 | } |
| 3666 | } |
| 3667 | |
| 3668 | @VisibleForTesting |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3669 | @GuardedBy("mNotificationLock") |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3670 | void buzzBeepBlinkLocked(NotificationRecord record) { |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 3671 | boolean buzz = false; |
| 3672 | boolean beep = false; |
| 3673 | boolean blink = false; |
| 3674 | |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3675 | final Notification notification = record.sbn.getNotification(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3676 | final String key = record.getKey(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3677 | |
| 3678 | // Should this notification make noise, vibe, or use the LED? |
Julia Reynolds | 8576991 | 2016-10-25 09:08:57 -0400 | [diff] [blame] | 3679 | final boolean aboveThreshold = |
| 3680 | record.getImportance() >= NotificationManager.IMPORTANCE_DEFAULT; |
Chris Wren | ce00a23 | 2014-11-21 16:25:19 -0500 | [diff] [blame] | 3681 | final boolean canInterrupt = aboveThreshold && !record.isIntercepted(); |
Julia Reynolds | bb1d7d2 | 2017-04-18 10:30:51 -0400 | [diff] [blame] | 3682 | if (DBG) |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3683 | Slog.v(TAG, |
| 3684 | "pkg=" + record.sbn.getPackageName() + " canInterrupt=" + canInterrupt + |
| 3685 | " intercept=" + record.isIntercepted() |
| 3686 | ); |
| 3687 | |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3688 | // If we're not supposed to beep, vibrate, etc. then don't. |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3689 | final String disableEffects = disableNotificationEffects(record); |
| 3690 | if (disableEffects != null) { |
| 3691 | ZenLog.traceDisableEffects(record, disableEffects); |
| 3692 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3693 | |
| 3694 | // Remember if this notification already owns the notification channels. |
| 3695 | boolean wasBeep = key != null && key.equals(mSoundNotificationKey); |
| 3696 | boolean wasBuzz = key != null && key.equals(mVibrateNotificationKey); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3697 | // These are set inside the conditional if the notification is allowed to make noise. |
| 3698 | boolean hasValidVibrate = false; |
| 3699 | boolean hasValidSound = false; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3700 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3701 | if (isNotificationForCurrentUser(record)) { |
| 3702 | // If the notification will appear in the status bar, it should send an accessibility |
| 3703 | // event |
| 3704 | if (!record.isUpdate && record.getImportance() > IMPORTANCE_MIN) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3705 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3706 | } |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 3707 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3708 | if (disableEffects == null |
| 3709 | && canInterrupt |
| 3710 | && mSystemReady |
| 3711 | && mAudioManager != null) { |
| 3712 | if (DBG) Slog.v(TAG, "Interrupting!"); |
| 3713 | Uri soundUri = record.getSound(); |
| 3714 | hasValidSound = soundUri != null && !Uri.EMPTY.equals(soundUri); |
| 3715 | long[] vibration = record.getVibration(); |
| 3716 | // Demote sound to vibration if vibration missing & phone in vibration mode. |
| 3717 | if (vibration == null |
| 3718 | && hasValidSound |
| 3719 | && (mAudioManager.getRingerModeInternal() |
| 3720 | == AudioManager.RINGER_MODE_VIBRATE)) { |
| 3721 | vibration = mFallbackVibrationPattern; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3722 | } |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3723 | hasValidVibrate = vibration != null; |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 3724 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3725 | if (!shouldMuteNotificationLocked(record)) { |
| 3726 | if (hasValidSound) { |
| 3727 | mSoundNotificationKey = key; |
| 3728 | if (mInCall) { |
| 3729 | playInCallNotification(); |
| 3730 | beep = true; |
| 3731 | } else { |
| 3732 | beep = playSound(record, soundUri); |
| 3733 | } |
| 3734 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3735 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3736 | final boolean ringerModeSilent = |
| 3737 | mAudioManager.getRingerModeInternal() |
| 3738 | == AudioManager.RINGER_MODE_SILENT; |
| 3739 | if (!mInCall && hasValidVibrate && !ringerModeSilent) { |
| 3740 | mVibrateNotificationKey = key; |
| 3741 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 3742 | buzz = playVibration(record, vibration, hasValidSound); |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3743 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3744 | } |
| 3745 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3746 | } |
| 3747 | // If a notification is updated to remove the actively playing sound or vibrate, |
| 3748 | // cancel that feedback now |
| 3749 | if (wasBeep && !hasValidSound) { |
| 3750 | clearSoundLocked(); |
| 3751 | } |
| 3752 | if (wasBuzz && !hasValidVibrate) { |
| 3753 | clearVibrateLocked(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3754 | } |
| 3755 | |
| 3756 | // light |
| 3757 | // release the light |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3758 | boolean wasShowLights = mLights.remove(key); |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 3759 | if (record.getLight() != null && aboveThreshold |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 3760 | && ((record.getSuppressedVisualEffects() |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 3761 | & NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF) == 0)) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 3762 | mLights.add(key); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3763 | updateLightsLocked(); |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 3764 | if (mUseAttentionLight) { |
| 3765 | mAttentionLight.pulse(); |
| 3766 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 3767 | blink = true; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3768 | } else if (wasShowLights) { |
| 3769 | updateLightsLocked(); |
| 3770 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 3771 | if (buzz || beep || blink) { |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 3772 | MetricsLogger.action(record.getLogMaker() |
| 3773 | .setCategory(MetricsEvent.NOTIFICATION_ALERT) |
| 3774 | .setType(MetricsEvent.TYPE_OPEN) |
| 3775 | .setSubtype((buzz ? 1 : 0) | (beep ? 2 : 0) | (blink ? 4 : 0))); |
| 3776 | EventLogTags.writeNotificationAlert(key, buzz ? 1 : 0, beep ? 1 : 0, blink ? 1 : 0); |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 3777 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 3778 | } |
| 3779 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3780 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 3781 | boolean shouldMuteNotificationLocked(final NotificationRecord record) { |
| 3782 | final Notification notification = record.getNotification(); |
| 3783 | if(record.isUpdate |
| 3784 | && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0) { |
| 3785 | return true; |
| 3786 | } |
| 3787 | if (record.sbn.isGroup()) { |
Julia Reynolds | 3020315 | 2017-05-26 13:36:31 -0400 | [diff] [blame] | 3788 | return notification.suppressAlertingDueToGrouping(); |
Julia Reynolds | a79c371 | 2017-04-21 10:29:57 -0400 | [diff] [blame] | 3789 | } |
| 3790 | return false; |
| 3791 | } |
| 3792 | |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 3793 | private boolean playSound(final NotificationRecord record, Uri soundUri) { |
| 3794 | boolean looping = (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 3795 | // 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] | 3796 | // or the device is in vibrate mode |
| 3797 | if (!mAudioManager.isAudioFocusExclusive() && mAudioManager.getRingerModeInternal() |
| 3798 | != AudioManager.RINGER_MODE_VIBRATE) { |
Julia Reynolds | 0c299d4 | 2016-11-15 14:37:04 -0500 | [diff] [blame] | 3799 | final long identity = Binder.clearCallingIdentity(); |
| 3800 | try { |
| 3801 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 3802 | if (player != null) { |
| 3803 | if (DBG) Slog.v(TAG, "Playing sound " + soundUri |
| 3804 | + " with attributes " + record.getAudioAttributes()); |
| 3805 | player.playAsync(soundUri, record.sbn.getUser(), looping, |
| 3806 | record.getAudioAttributes()); |
| 3807 | return true; |
| 3808 | } |
| 3809 | } catch (RemoteException e) { |
| 3810 | } finally { |
| 3811 | Binder.restoreCallingIdentity(identity); |
| 3812 | } |
| 3813 | } |
| 3814 | return false; |
| 3815 | } |
| 3816 | |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 3817 | private boolean playVibration(final NotificationRecord record, long[] vibration, |
| 3818 | boolean delayVibForSound) { |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 3819 | // Escalate privileges so we can use the vibrator even if the |
| 3820 | // notifying app does not have the VIBRATE permission. |
| 3821 | long identity = Binder.clearCallingIdentity(); |
| 3822 | try { |
Jean-Michel Trivi | ea0eb5f | 2017-05-25 18:32:40 -0700 | [diff] [blame] | 3823 | final VibrationEffect effect; |
| 3824 | try { |
| 3825 | final boolean insistent = |
| 3826 | (record.getNotification().flags & Notification.FLAG_INSISTENT) != 0; |
| 3827 | effect = VibrationEffect.createWaveform( |
| 3828 | vibration, insistent ? 0 : -1 /*repeatIndex*/); |
| 3829 | } catch (IllegalArgumentException e) { |
| 3830 | Slog.e(TAG, "Error creating vibration waveform with pattern: " + |
| 3831 | Arrays.toString(vibration)); |
| 3832 | return false; |
| 3833 | } |
| 3834 | if (delayVibForSound) { |
| 3835 | new Thread(() -> { |
| 3836 | // delay the vibration by the same amount as the notification sound |
| 3837 | final int waitMs = mAudioManager.getFocusRampTimeMs( |
| 3838 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, |
| 3839 | record.getAudioAttributes()); |
| 3840 | if (DBG) Slog.v(TAG, "Delaying vibration by " + waitMs + "ms"); |
| 3841 | try { |
| 3842 | Thread.sleep(waitMs); |
| 3843 | } catch (InterruptedException e) { } |
| 3844 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 3845 | effect, record.getAudioAttributes()); |
| 3846 | }).start(); |
| 3847 | } else { |
| 3848 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 3849 | effect, record.getAudioAttributes()); |
| 3850 | } |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 3851 | return true; |
| 3852 | } finally{ |
| 3853 | Binder.restoreCallingIdentity(identity); |
| 3854 | } |
| 3855 | } |
| 3856 | |
Julia Reynolds | 7c96b58 | 2017-05-25 12:35:36 -0400 | [diff] [blame] | 3857 | private boolean isNotificationForCurrentUser(NotificationRecord record) { |
| 3858 | final int currentUser; |
| 3859 | final long token = Binder.clearCallingIdentity(); |
| 3860 | try { |
| 3861 | currentUser = ActivityManager.getCurrentUser(); |
| 3862 | } finally { |
| 3863 | Binder.restoreCallingIdentity(token); |
| 3864 | } |
| 3865 | return (record.getUserId() == UserHandle.USER_ALL || |
| 3866 | record.getUserId() == currentUser || |
| 3867 | mUserProfiles.isCurrentProfile(record.getUserId())); |
| 3868 | } |
| 3869 | |
Marta Białka | 39c992f | 2011-03-10 10:27:24 +0100 | [diff] [blame] | 3870 | private void playInCallNotification() { |
| 3871 | new Thread() { |
| 3872 | @Override |
| 3873 | public void run() { |
| 3874 | // If toneGenerator creation fails, just continue the call |
| 3875 | // without playing the notification sound. |
| 3876 | try { |
| 3877 | synchronized (mInCallToneGeneratorLock) { |
| 3878 | if (mInCallToneGenerator != null) { |
| 3879 | // limit this tone to 1 second; BEEP2 should in fact be much shorter |
| 3880 | mInCallToneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP2, 1000); |
| 3881 | } |
| 3882 | } |
| 3883 | } catch (RuntimeException e) { |
| 3884 | Log.w(TAG, "Exception from ToneGenerator: " + e); |
| 3885 | } |
| 3886 | } |
| 3887 | }.start(); |
| 3888 | } |
| 3889 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3890 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3891 | void showNextToastLocked() { |
| 3892 | ToastRecord record = mToastQueue.get(0); |
| 3893 | while (record != null) { |
| 3894 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| 3895 | try { |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 3896 | record.callback.show(record.token); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3897 | scheduleTimeoutLocked(record); |
| 3898 | return; |
| 3899 | } catch (RemoteException e) { |
| 3900 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| 3901 | + " in package " + record.pkg); |
| 3902 | // remove it from the list and let the process die |
| 3903 | int index = mToastQueue.indexOf(record); |
| 3904 | if (index >= 0) { |
| 3905 | mToastQueue.remove(index); |
| 3906 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 3907 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3908 | if (mToastQueue.size() > 0) { |
| 3909 | record = mToastQueue.get(0); |
| 3910 | } else { |
| 3911 | record = null; |
| 3912 | } |
| 3913 | } |
| 3914 | } |
| 3915 | } |
| 3916 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3917 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3918 | void cancelToastLocked(int index) { |
| 3919 | ToastRecord record = mToastQueue.get(index); |
| 3920 | try { |
| 3921 | record.callback.hide(); |
| 3922 | } catch (RemoteException e) { |
| 3923 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| 3924 | + " in package " + record.pkg); |
| 3925 | // don't worry about this, we're about to remove it from |
| 3926 | // the list anyway |
| 3927 | } |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 3928 | |
| 3929 | ToastRecord lastToast = mToastQueue.remove(index); |
Wale Ogunwale | ac2561e | 2016-11-01 15:43:46 -0700 | [diff] [blame] | 3930 | mWindowManagerInternal.removeWindowToken(lastToast.token, true, DEFAULT_DISPLAY); |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 3931 | |
| 3932 | keepProcessAliveIfNeededLocked(record.pid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3933 | if (mToastQueue.size() > 0) { |
| 3934 | // Show the next one. If the callback fails, this will remove |
| 3935 | // it from the list, so don't assume that the list hasn't changed |
| 3936 | // after this point. |
| 3937 | showNextToastLocked(); |
| 3938 | } |
| 3939 | } |
| 3940 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3941 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3942 | private void scheduleTimeoutLocked(ToastRecord r) |
| 3943 | { |
| 3944 | mHandler.removeCallbacksAndMessages(r); |
| 3945 | Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r); |
| 3946 | long delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY; |
| 3947 | mHandler.sendMessageDelayed(m, delay); |
| 3948 | } |
| 3949 | |
| 3950 | private void handleTimeout(ToastRecord record) |
| 3951 | { |
| 3952 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| 3953 | synchronized (mToastQueue) { |
| 3954 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 3955 | if (index >= 0) { |
| 3956 | cancelToastLocked(index); |
| 3957 | } |
| 3958 | } |
| 3959 | } |
| 3960 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3961 | @GuardedBy("mToastQueue") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3962 | int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 3963 | { |
| 3964 | IBinder cbak = callback.asBinder(); |
| 3965 | ArrayList<ToastRecord> list = mToastQueue; |
| 3966 | int len = list.size(); |
| 3967 | for (int i=0; i<len; i++) { |
| 3968 | ToastRecord r = list.get(i); |
| 3969 | if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) { |
| 3970 | return i; |
| 3971 | } |
| 3972 | } |
| 3973 | return -1; |
| 3974 | } |
| 3975 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 3976 | @GuardedBy("mToastQueue") |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 3977 | void keepProcessAliveIfNeededLocked(int pid) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3978 | { |
| 3979 | int toastCount = 0; // toasts from this pid |
| 3980 | ArrayList<ToastRecord> list = mToastQueue; |
| 3981 | int N = list.size(); |
| 3982 | for (int i=0; i<N; i++) { |
| 3983 | ToastRecord r = list.get(i); |
| 3984 | if (r.pid == pid) { |
| 3985 | toastCount++; |
| 3986 | } |
| 3987 | } |
| 3988 | try { |
Dianne Hackborn | f965f40 | 2017-05-04 23:27:23 -0700 | [diff] [blame] | 3989 | mAm.setProcessImportant(mForegroundToken, pid, toastCount > 0, "toast"); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3990 | } catch (RemoteException e) { |
| 3991 | // Shouldn't happen. |
| 3992 | } |
| 3993 | } |
| 3994 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 3995 | private void handleRankingReconsideration(Message message) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 3996 | if (!(message.obj instanceof RankingReconsideration)) return; |
| 3997 | RankingReconsideration recon = (RankingReconsideration) message.obj; |
| 3998 | recon.run(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 3999 | boolean changed; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4000 | synchronized (mNotificationLock) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4001 | final NotificationRecord record = mNotificationsByKey.get(recon.getKey()); |
| 4002 | if (record == null) { |
| 4003 | return; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4004 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4005 | int indexBefore = findNotificationRecordIndexLocked(record); |
| 4006 | boolean interceptBefore = record.isIntercepted(); |
Julia Reynolds | af4dc28 | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4007 | float contactAffinityBefore = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4008 | int visibilityBefore = record.getPackageVisibilityOverride(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4009 | recon.applyChangesLocked(record); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4010 | applyZenModeLocked(record); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4011 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4012 | int indexAfter = findNotificationRecordIndexLocked(record); |
| 4013 | boolean interceptAfter = record.isIntercepted(); |
Julia Reynolds | af4dc28 | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4014 | float contactAffinityAfter = record.getContactAffinity(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4015 | int visibilityAfter = record.getPackageVisibilityOverride(); |
| 4016 | changed = indexBefore != indexAfter || interceptBefore != interceptAfter |
| 4017 | || visibilityBefore != visibilityAfter; |
Julia Reynolds | af4dc28 | 2017-06-23 16:13:20 -0400 | [diff] [blame] | 4018 | if (interceptBefore && !interceptAfter |
| 4019 | && Float.compare(contactAffinityBefore, contactAffinityAfter) != 0) { |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 4020 | buzzBeepBlinkLocked(record); |
| 4021 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4022 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4023 | if (changed) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4024 | scheduleSendRankingUpdate(); |
| 4025 | } |
| 4026 | } |
| 4027 | |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4028 | private void handleRankingSort(Message msg) { |
| 4029 | if (!(msg.obj instanceof Boolean)) return; |
Chris Wren | 89aa226 | 2017-05-05 18:05:56 -0400 | [diff] [blame] | 4030 | if (mRankingHelper == null) return; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4031 | boolean forceUpdate = ((Boolean) msg.obj == null) ? false : (boolean) msg.obj; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4032 | synchronized (mNotificationLock) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4033 | final int N = mNotificationList.size(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4034 | // Any field that can change via one of the extractors or by the assistant |
| 4035 | // needs to be added here. |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4036 | ArrayList<String> orderBefore = new ArrayList<String>(N); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4037 | ArrayList<String> groupOverrideBefore = new ArrayList<>(N); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4038 | int[] visibilities = new int[N]; |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4039 | boolean[] showBadges = new boolean[N]; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4040 | for (int i = 0; i < N; i++) { |
| 4041 | final NotificationRecord r = mNotificationList.get(i); |
| 4042 | orderBefore.add(r.getKey()); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4043 | groupOverrideBefore.add(r.sbn.getGroupKey()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4044 | visibilities[i] = r.getPackageVisibilityOverride(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4045 | showBadges[i] = r.canShowBadge(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4046 | mRankingHelper.extractSignals(r); |
| 4047 | } |
Chris Wren | 19a02b0 | 2015-12-22 10:34:22 -0500 | [diff] [blame] | 4048 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4049 | for (int i = 0; i < N; i++) { |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4050 | final NotificationRecord r = mNotificationList.get(i); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4051 | if (forceUpdate |
| 4052 | || !orderBefore.get(i).equals(r.getKey()) |
Julia Reynolds | 6976669 | 2016-02-01 15:35:08 -0500 | [diff] [blame] | 4053 | || visibilities[i] != r.getPackageVisibilityOverride() |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4054 | || !groupOverrideBefore.get(i).equals(r.sbn.getGroupKey()) |
| 4055 | || showBadges[i] != r.canShowBadge()) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4056 | scheduleSendRankingUpdate(); |
| 4057 | return; |
| 4058 | } |
| 4059 | } |
| 4060 | } |
| 4061 | } |
| 4062 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4063 | @GuardedBy("mNotificationLock") |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 4064 | private void recordCallerLocked(NotificationRecord record) { |
| 4065 | if (mZenModeHelper.isCall(record)) { |
| 4066 | mZenModeHelper.recordCaller(record); |
| 4067 | } |
| 4068 | } |
| 4069 | |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 4070 | // let zen mode evaluate this record |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4071 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4072 | private void applyZenModeLocked(NotificationRecord record) { |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 4073 | record.setIntercepted(mZenModeHelper.shouldIntercept(record)); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4074 | if (record.isIntercepted()) { |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 4075 | int suppressed = (mZenModeHelper.shouldSuppressWhenScreenOff() |
| 4076 | ? SUPPRESSED_EFFECT_SCREEN_OFF : 0) |
| 4077 | | (mZenModeHelper.shouldSuppressWhenScreenOn() |
| 4078 | ? SUPPRESSED_EFFECT_SCREEN_ON : 0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4079 | record.setSuppressedVisualEffects(suppressed); |
Julia Reynolds | 445cfa8 | 2017-05-08 15:41:45 -0400 | [diff] [blame] | 4080 | } else { |
| 4081 | record.setSuppressedVisualEffects(0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4082 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4083 | } |
| 4084 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4085 | @GuardedBy("mNotificationLock") |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 4086 | private int findNotificationRecordIndexLocked(NotificationRecord target) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4087 | return mRankingHelper.indexOf(mNotificationList, target); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4088 | } |
| 4089 | |
| 4090 | private void scheduleSendRankingUpdate() { |
Chris Wren | 5202049 | 2016-04-06 11:12:02 -0400 | [diff] [blame] | 4091 | if (!mHandler.hasMessages(MESSAGE_SEND_RANKING_UPDATE)) { |
| 4092 | Message m = Message.obtain(mHandler, MESSAGE_SEND_RANKING_UPDATE); |
| 4093 | mHandler.sendMessage(m); |
| 4094 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | private void handleSendRankingUpdate() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4098 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4099 | mListeners.notifyRankingUpdateLocked(); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4100 | } |
| 4101 | } |
| 4102 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4103 | private void scheduleListenerHintsChanged(int state) { |
| 4104 | mHandler.removeMessages(MESSAGE_LISTENER_HINTS_CHANGED); |
| 4105 | mHandler.obtainMessage(MESSAGE_LISTENER_HINTS_CHANGED, state, 0).sendToTarget(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4106 | } |
| 4107 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4108 | private void scheduleInterruptionFilterChanged(int listenerInterruptionFilter) { |
| 4109 | mHandler.removeMessages(MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED); |
| 4110 | mHandler.obtainMessage( |
| 4111 | MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED, |
| 4112 | listenerInterruptionFilter, |
| 4113 | 0).sendToTarget(); |
| 4114 | } |
| 4115 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4116 | private void handleListenerHintsChanged(int hints) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4117 | synchronized (mNotificationLock) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4118 | mListeners.notifyListenerHintsChangedLocked(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4119 | } |
| 4120 | } |
| 4121 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4122 | private void handleListenerInterruptionFilterChanged(int interruptionFilter) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4123 | synchronized (mNotificationLock) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4124 | mListeners.notifyInterruptionFilterChanged(interruptionFilter); |
| 4125 | } |
| 4126 | } |
| 4127 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4128 | private final class WorkerHandler extends Handler |
| 4129 | { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4130 | public WorkerHandler(Looper looper) { |
| 4131 | super(looper); |
| 4132 | } |
| 4133 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4134 | @Override |
| 4135 | public void handleMessage(Message msg) |
| 4136 | { |
| 4137 | switch (msg.what) |
| 4138 | { |
| 4139 | case MESSAGE_TIMEOUT: |
| 4140 | handleTimeout((ToastRecord)msg.obj); |
| 4141 | break; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 4142 | case MESSAGE_SAVE_POLICY_FILE: |
| 4143 | handleSavePolicyFile(); |
| 4144 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4145 | case MESSAGE_SEND_RANKING_UPDATE: |
| 4146 | handleSendRankingUpdate(); |
| 4147 | break; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 4148 | case MESSAGE_LISTENER_HINTS_CHANGED: |
| 4149 | handleListenerHintsChanged(msg.arg1); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 4150 | break; |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 4151 | case MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED: |
| 4152 | handleListenerInterruptionFilterChanged(msg.arg1); |
| 4153 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4154 | } |
| 4155 | } |
| 4156 | |
| 4157 | } |
| 4158 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4159 | private final class RankingHandlerWorker extends Handler implements RankingHandler |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4160 | { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4161 | public RankingHandlerWorker(Looper looper) { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 4162 | super(looper); |
| 4163 | } |
| 4164 | |
| 4165 | @Override |
| 4166 | public void handleMessage(Message msg) { |
| 4167 | switch (msg.what) { |
| 4168 | case MESSAGE_RECONSIDER_RANKING: |
| 4169 | handleRankingReconsideration(msg); |
| 4170 | break; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4171 | case MESSAGE_RANKING_SORT: |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4172 | handleRankingSort(msg); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 4173 | break; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4174 | } |
| 4175 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4176 | |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4177 | public void requestSort(boolean forceUpdate) { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4178 | removeMessages(MESSAGE_RANKING_SORT); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4179 | Message msg = Message.obtain(); |
| 4180 | msg.what = MESSAGE_RANKING_SORT; |
| 4181 | msg.obj = forceUpdate; |
| 4182 | sendMessage(msg); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | public void requestReconsideration(RankingReconsideration recon) { |
| 4186 | Message m = Message.obtain(this, |
| 4187 | NotificationManagerService.MESSAGE_RECONSIDER_RANKING, recon); |
| 4188 | long delay = recon.getDelay(TimeUnit.MILLISECONDS); |
| 4189 | sendMessageDelayed(m, delay); |
| 4190 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4191 | } |
| 4192 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4193 | // Notifications |
| 4194 | // ============================================================================ |
| 4195 | static int clamp(int x, int low, int high) { |
| 4196 | return (x < low) ? low : ((x > high) ? high : x); |
| 4197 | } |
| 4198 | |
| 4199 | void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
| 4200 | AccessibilityManager manager = AccessibilityManager.getInstance(getContext()); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 4201 | if (!manager.isEnabled()) { |
| 4202 | return; |
| 4203 | } |
| 4204 | |
| 4205 | AccessibilityEvent event = |
| 4206 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 4207 | event.setPackageName(packageName); |
| 4208 | event.setClassName(Notification.class.getName()); |
| 4209 | event.setParcelableData(notification); |
| 4210 | CharSequence tickerText = notification.tickerText; |
| 4211 | if (!TextUtils.isEmpty(tickerText)) { |
| 4212 | event.getText().add(tickerText); |
| 4213 | } |
| 4214 | |
| 4215 | manager.sendAccessibilityEvent(event); |
| 4216 | } |
| 4217 | |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4218 | /** |
| 4219 | * Removes all NotificationsRecords with the same key as the given notification record |
| 4220 | * from both lists. Do not call this method while iterating over either list. |
| 4221 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4222 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4223 | private boolean removeFromNotificationListsLocked(NotificationRecord r) { |
| 4224 | // Remove from both lists, either list could have a separate Record for what is |
| 4225 | // effectively the same notification. |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4226 | boolean wasPosted = false; |
| 4227 | NotificationRecord recordInList = null; |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4228 | if ((recordInList = findNotificationByListLocked(mNotificationList, r.getKey())) |
| 4229 | != null) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4230 | mNotificationList.remove(recordInList); |
| 4231 | mNotificationsByKey.remove(recordInList.sbn.getKey()); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4232 | wasPosted = true; |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4233 | } |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4234 | while ((recordInList = findNotificationByListLocked(mEnqueuedNotifications, r.getKey())) |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4235 | != null) { |
| 4236 | mEnqueuedNotifications.remove(recordInList); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4237 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4238 | return wasPosted; |
| 4239 | } |
| 4240 | |
| 4241 | @GuardedBy("mNotificationLock") |
| 4242 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason, |
| 4243 | boolean wasPosted) { |
| 4244 | final String canceledKey = r.getKey(); |
Julia Reynolds | c6b371b | 2016-06-14 08:31:03 -0400 | [diff] [blame] | 4245 | |
| 4246 | // Record caller. |
| 4247 | recordCallerLocked(r); |
| 4248 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4249 | // tell the app |
| 4250 | if (sendDelete) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4251 | if (r.getNotification().deleteIntent != null) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4252 | try { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4253 | r.getNotification().deleteIntent.send(); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4254 | } catch (PendingIntent.CanceledException ex) { |
| 4255 | // do nothing - there's no relevant way to recover, and |
| 4256 | // no reason to let this propagate |
Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 4257 | Slog.w(TAG, "canceled PendingIntent for " + r.sbn.getPackageName(), ex); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 4258 | } |
| 4259 | } |
| 4260 | } |
| 4261 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4262 | // Only cancel these if this notification actually got to be posted. |
| 4263 | if (wasPosted) { |
| 4264 | // status bar |
| 4265 | if (r.getNotification().getSmallIcon() != null) { |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 4266 | if (reason != REASON_SNOOZED) { |
| 4267 | r.isCanceled = true; |
| 4268 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4269 | mListeners.notifyRemovedLocked(r.sbn, reason); |
| 4270 | mHandler.post(new Runnable() { |
| 4271 | @Override |
| 4272 | public void run() { |
| 4273 | mGroupHelper.onNotificationRemoved(r.sbn); |
| 4274 | } |
| 4275 | }); |
| 4276 | } |
| 4277 | |
| 4278 | // sound |
| 4279 | if (canceledKey.equals(mSoundNotificationKey)) { |
| 4280 | mSoundNotificationKey = null; |
| 4281 | final long identity = Binder.clearCallingIdentity(); |
| 4282 | try { |
| 4283 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 4284 | if (player != null) { |
| 4285 | player.stopAsync(); |
| 4286 | } |
| 4287 | } catch (RemoteException e) { |
| 4288 | } finally { |
| 4289 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 8f488d3 | 2016-10-14 10:59:01 -0400 | [diff] [blame] | 4290 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4291 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4292 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4293 | // vibrate |
| 4294 | if (canceledKey.equals(mVibrateNotificationKey)) { |
| 4295 | mVibrateNotificationKey = null; |
| 4296 | long identity = Binder.clearCallingIdentity(); |
| 4297 | try { |
| 4298 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 4299 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4300 | finally { |
| 4301 | Binder.restoreCallingIdentity(identity); |
| 4302 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4303 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4304 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4305 | // light |
| 4306 | mLights.remove(canceledKey); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4307 | } |
| 4308 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4309 | // Record usage stats |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4310 | // TODO: add unbundling stats? |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4311 | switch (reason) { |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 4312 | case REASON_CANCEL: |
| 4313 | case REASON_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4314 | case REASON_LISTENER_CANCEL: |
| 4315 | case REASON_LISTENER_CANCEL_ALL: |
| 4316 | mUsageStats.registerDismissedByUser(r); |
| 4317 | break; |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 4318 | case REASON_APP_CANCEL: |
| 4319 | case REASON_APP_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4320 | mUsageStats.registerRemovedByApp(r); |
| 4321 | break; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4322 | } |
| 4323 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4324 | String groupKey = r.getGroupKey(); |
| 4325 | NotificationRecord groupSummary = mSummaryByGroupKey.get(groupKey); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4326 | if (groupSummary != null && groupSummary.getKey().equals(canceledKey)) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4327 | mSummaryByGroupKey.remove(groupKey); |
| 4328 | } |
Julia Reynolds | eae43fb | 2016-05-09 12:42:58 -0400 | [diff] [blame] | 4329 | final ArrayMap<String, String> summaries = mAutobundledSummaries.get(r.sbn.getUserId()); |
| 4330 | if (summaries != null && r.sbn.getKey().equals(summaries.get(r.sbn.getPackageName()))) { |
| 4331 | summaries.remove(r.sbn.getPackageName()); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4332 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 4333 | |
Daniel Sandler | 23d7c70 | 2013-03-07 16:32:06 -0500 | [diff] [blame] | 4334 | // Save it for users of getHistoricalNotifications() |
| 4335 | mArchive.record(r.sbn); |
Christoph Studer | 81e5b5f | 2014-10-22 17:19:56 +0200 | [diff] [blame] | 4336 | |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 4337 | final long now = System.currentTimeMillis(); |
Chris Wren | 9eb5e10 | 2017-01-26 13:15:06 -0500 | [diff] [blame] | 4338 | MetricsLogger.action(r.getLogMaker(now) |
| 4339 | .setCategory(MetricsEvent.NOTIFICATION_ITEM) |
| 4340 | .setType(MetricsEvent.TYPE_DISMISS) |
| 4341 | .setSubtype(reason)); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 4342 | EventLogTags.writeNotificationCanceled(canceledKey, reason, |
| 4343 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4344 | } |
| 4345 | |
| 4346 | /** |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 4347 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4348 | * and none of the {@code mustNotHaveFlags}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4349 | */ |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 4350 | void cancelNotification(final int callingUid, final int callingPid, |
| 4351 | final String pkg, final String tag, final int id, |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4352 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4353 | final int userId, final int reason, final ManagedServiceInfo listener) { |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4354 | // In enqueueNotificationInternal notifications are added by scheduling the |
| 4355 | // work on the worker handler. Hence, we also schedule the cancel on this |
| 4356 | // handler to avoid a scenario where an add notification call followed by a |
| 4357 | // remove notification call ends up in not removing the notification. |
| 4358 | mHandler.post(new Runnable() { |
| 4359 | @Override |
| 4360 | public void run() { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4361 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Chris Wren | bddb5bc | 2015-03-04 08:47:46 -0800 | [diff] [blame] | 4362 | if (DBG) EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, id, tag, |
| 4363 | userId, mustHaveFlags, mustNotHaveFlags, reason, listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4364 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4365 | synchronized (mNotificationLock) { |
| 4366 | // Look for the notification, searching both the posted and enqueued lists. |
| 4367 | NotificationRecord r = findNotificationLocked(pkg, tag, id, userId); |
| 4368 | if (r != null) { |
| 4369 | // The notification was found, check if it should be removed. |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 4370 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4371 | // Ideally we'd do this in the caller of this method. However, that would |
| 4372 | // require the caller to also find the notification. |
Julia Reynolds | f619bc5 | 2017-03-17 08:32:23 -0400 | [diff] [blame] | 4373 | if (reason == REASON_CLICK) { |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4374 | mUsageStats.registerClickedByUser(r); |
| 4375 | } |
| 4376 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4377 | if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) { |
| 4378 | return; |
| 4379 | } |
| 4380 | if ((r.getNotification().flags & mustNotHaveFlags) != 0) { |
| 4381 | return; |
| 4382 | } |
| 4383 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4384 | // Cancel the notification. |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4385 | boolean wasPosted = removeFromNotificationListsLocked(r); |
| 4386 | cancelNotificationLocked(r, sendDelete, reason, wasPosted); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4387 | cancelGroupChildrenLocked(r, callingUid, callingPid, listenerName, |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4388 | sendDelete); |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4389 | updateLightsLocked(); |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 4390 | } else { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4391 | // No notification was found, assume that it is snoozed and cancel it. |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 4392 | if (reason != REASON_SNOOZED) { |
| 4393 | final boolean wasSnoozed = mSnoozeHelper.cancel(userId, pkg, tag, id); |
| 4394 | if (wasSnoozed) { |
| 4395 | savePolicyFile(); |
| 4396 | } |
Julia Reynolds | 72f1cbb | 2016-09-19 14:57:31 -0400 | [diff] [blame] | 4397 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4398 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4399 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4400 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 4401 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4402 | } |
| 4403 | |
| 4404 | /** |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 4405 | * Determine whether the userId applies to the notification in question, either because |
| 4406 | * they match exactly, or one of them is USER_ALL (which is treated as a wildcard). |
| 4407 | */ |
| 4408 | private boolean notificationMatchesUserId(NotificationRecord r, int userId) { |
| 4409 | return |
| 4410 | // looking for USER_ALL notifications? match everything |
| 4411 | userId == UserHandle.USER_ALL |
| 4412 | // a notification sent to USER_ALL matches any query |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4413 | || r.getUserId() == UserHandle.USER_ALL |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 4414 | // an exact user match |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 4415 | || r.getUserId() == userId; |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | /** |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4419 | * Determine whether the userId applies to the notification in question, either because |
| 4420 | * 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] | 4421 | * because it matches one of the users profiles. |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4422 | */ |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 4423 | private boolean notificationMatchesCurrentProfiles(NotificationRecord r, int userId) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4424 | return notificationMatchesUserId(r, userId) |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 4425 | || mUserProfiles.isCurrentProfile(r.getUserId()); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | /** |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 4429 | * 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] | 4430 | * {@code mustHaveFlags}. |
| 4431 | */ |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4432 | void cancelAllNotificationsInt(int callingUid, int callingPid, String pkg, String channelId, |
Julia Reynolds | b5e44b7 | 2016-08-16 15:00:25 -0400 | [diff] [blame] | 4433 | int mustHaveFlags, int mustNotHaveFlags, boolean doit, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4434 | ManagedServiceInfo listener) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4435 | mHandler.post(new Runnable() { |
| 4436 | @Override |
| 4437 | public void run() { |
| 4438 | String listenerName = listener == null ? null : listener.component.toShortString(); |
| 4439 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 4440 | pkg, userId, mustHaveFlags, mustNotHaveFlags, reason, |
| 4441 | listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4442 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4443 | // Why does this parameter exist? Do we actually want to execute the above if doit |
| 4444 | // is false? |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4445 | if (!doit) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4446 | return; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 4447 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4448 | |
| 4449 | synchronized (mNotificationLock) { |
| 4450 | FlagChecker flagChecker = (int flags) -> { |
| 4451 | if ((flags & mustHaveFlags) != mustHaveFlags) { |
| 4452 | return false; |
| 4453 | } |
| 4454 | if ((flags & mustNotHaveFlags) != 0) { |
| 4455 | return false; |
| 4456 | } |
| 4457 | return true; |
| 4458 | }; |
| 4459 | |
| 4460 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 4461 | pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, flagChecker, |
| 4462 | false /*includeCurrentProfiles*/, userId, false /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4463 | listenerName, true /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4464 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 4465 | callingPid, pkg, true /*nullPkgIndicatesUserSwitch*/, channelId, |
| 4466 | flagChecker, false /*includeCurrentProfiles*/, userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4467 | false /*sendDelete*/, reason, listenerName, false /* wasPosted */); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4468 | mSnoozeHelper.cancel(userId, pkg); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4469 | } |
| 4470 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4471 | }); |
| 4472 | } |
| 4473 | |
| 4474 | private interface FlagChecker { |
| 4475 | // Returns false if these flags do not pass the defined flag test. |
| 4476 | public boolean apply(int flags); |
| 4477 | } |
| 4478 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4479 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4480 | private void cancelAllNotificationsByListLocked(ArrayList<NotificationRecord> notificationList, |
| 4481 | int callingUid, int callingPid, String pkg, boolean nullPkgIndicatesUserSwitch, |
| 4482 | String channelId, FlagChecker flagChecker, boolean includeCurrentProfiles, int userId, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4483 | boolean sendDelete, int reason, String listenerName, boolean wasPosted) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4484 | ArrayList<NotificationRecord> canceledNotifications = null; |
| 4485 | for (int i = notificationList.size() - 1; i >= 0; --i) { |
| 4486 | NotificationRecord r = notificationList.get(i); |
| 4487 | if (includeCurrentProfiles) { |
| 4488 | if (!notificationMatchesCurrentProfiles(r, userId)) { |
| 4489 | continue; |
| 4490 | } |
| 4491 | } else if (!notificationMatchesUserId(r, userId)) { |
| 4492 | continue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4493 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4494 | // Don't remove notifications to all, if there's no package name specified |
| 4495 | if (nullPkgIndicatesUserSwitch && pkg == null && r.getUserId() == UserHandle.USER_ALL) { |
| 4496 | continue; |
| 4497 | } |
| 4498 | if (!flagChecker.apply(r.getFlags())) { |
| 4499 | continue; |
| 4500 | } |
| 4501 | if (pkg != null && !r.sbn.getPackageName().equals(pkg)) { |
| 4502 | continue; |
| 4503 | } |
| 4504 | if (channelId != null && !channelId.equals(r.getChannel().getId())) { |
| 4505 | continue; |
| 4506 | } |
| 4507 | |
| 4508 | if (canceledNotifications == null) { |
| 4509 | canceledNotifications = new ArrayList<>(); |
| 4510 | } |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4511 | notificationList.remove(i); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4512 | canceledNotifications.add(r); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4513 | cancelNotificationLocked(r, sendDelete, reason, wasPosted); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4514 | } |
| 4515 | if (canceledNotifications != null) { |
| 4516 | final int M = canceledNotifications.size(); |
| 4517 | for (int i = 0; i < M; i++) { |
| 4518 | cancelGroupChildrenLocked(canceledNotifications.get(i), callingUid, callingPid, |
| 4519 | listenerName, false /* sendDelete */); |
| 4520 | } |
| 4521 | updateLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4522 | } |
| 4523 | } |
| 4524 | |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 4525 | void snoozeNotificationInt(String key, long duration, String snoozeCriterionId, |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4526 | ManagedServiceInfo listener) { |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 4527 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | a8b766f | 2017-03-07 16:30:21 -0500 | [diff] [blame] | 4528 | if (duration <= 0 && snoozeCriterionId == null || key == null) { |
Julia Reynolds | cf63ff1 | 2017-01-24 13:55:48 -0500 | [diff] [blame] | 4529 | return; |
| 4530 | } |
Julia Reynolds | 520df6e | 2017-02-13 09:05:10 -0500 | [diff] [blame] | 4531 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 4532 | if (DBG) { |
Julia Reynolds | 5098977 | 2017-02-23 14:32:16 -0500 | [diff] [blame] | 4533 | Slog.d(TAG, String.format("snooze event(%s, %d, %s, %s)", key, duration, |
| 4534 | snoozeCriterionId, listenerName)); |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 4535 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4536 | // Needs to post so that it can cancel notifications not yet enqueued. |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4537 | mHandler.post(new SnoozeNotificationRunnable(key, duration, snoozeCriterionId)); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 4538 | } |
| 4539 | |
| 4540 | void unsnoozeNotificationInt(String key, ManagedServiceInfo listener) { |
| 4541 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 4542 | if (DBG) { |
| 4543 | Slog.d(TAG, String.format("unsnooze event(%s, %s)", key, listenerName)); |
| 4544 | } |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 4545 | mSnoozeHelper.repost(key); |
| 4546 | savePolicyFile(); |
Julia Reynolds | b6c1f99 | 2016-11-22 09:26:46 -0500 | [diff] [blame] | 4547 | } |
| 4548 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4549 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 350159c | 2014-03-27 11:15:11 -0700 | [diff] [blame] | 4550 | void cancelAllLocked(int callingUid, int callingPid, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4551 | ManagedServiceInfo listener, boolean includeCurrentProfiles) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4552 | mHandler.post(new Runnable() { |
| 4553 | @Override |
| 4554 | public void run() { |
| 4555 | synchronized (mNotificationLock) { |
| 4556 | String listenerName = |
| 4557 | listener == null ? null : listener.component.toShortString(); |
| 4558 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 4559 | null, userId, 0, 0, reason, listenerName); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 4560 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4561 | FlagChecker flagChecker = (int flags) -> { |
| 4562 | if ((flags & (Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR)) |
| 4563 | != 0) { |
| 4564 | return false; |
| 4565 | } |
| 4566 | return true; |
| 4567 | }; |
| 4568 | |
| 4569 | cancelAllNotificationsByListLocked(mNotificationList, callingUid, callingPid, |
| 4570 | null, false /*nullPkgIndicatesUserSwitch*/, null, flagChecker, |
| 4571 | includeCurrentProfiles, userId, true /*sendDelete*/, reason, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4572 | listenerName, true); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4573 | cancelAllNotificationsByListLocked(mEnqueuedNotifications, callingUid, |
| 4574 | callingPid, null, false /*nullPkgIndicatesUserSwitch*/, null, |
| 4575 | flagChecker, includeCurrentProfiles, userId, true /*sendDelete*/, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4576 | reason, listenerName, false); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4577 | mSnoozeHelper.cancel(userId, includeCurrentProfiles); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 4578 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4579 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4580 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4581 | } |
| 4582 | |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4583 | // Warning: The caller is responsible for invoking updateLightsLocked(). |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4584 | @GuardedBy("mNotificationLock") |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4585 | private void cancelGroupChildrenLocked(NotificationRecord r, int callingUid, int callingPid, |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4586 | String listenerName, boolean sendDelete) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4587 | Notification n = r.getNotification(); |
Christoph Studer | 3f31f5d | 2014-07-31 16:55:32 +0200 | [diff] [blame] | 4588 | if (!n.isGroupSummary()) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4589 | return; |
| 4590 | } |
| 4591 | |
| 4592 | String pkg = r.sbn.getPackageName(); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4593 | |
| 4594 | if (pkg == null) { |
| 4595 | if (DBG) Log.e(TAG, "No package for group summary: " + r.getKey()); |
| 4596 | return; |
| 4597 | } |
| 4598 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4599 | cancelGroupChildrenByListLocked(mNotificationList, r, callingUid, callingPid, listenerName, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4600 | sendDelete, true); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4601 | cancelGroupChildrenByListLocked(mEnqueuedNotifications, r, callingUid, callingPid, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4602 | listenerName, sendDelete, false); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4603 | } |
| 4604 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4605 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4606 | private void cancelGroupChildrenByListLocked(ArrayList<NotificationRecord> notificationList, |
| 4607 | NotificationRecord parentNotification, int callingUid, int callingPid, |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4608 | String listenerName, boolean sendDelete, boolean wasPosted) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4609 | final String pkg = parentNotification.sbn.getPackageName(); |
| 4610 | final int userId = parentNotification.getUserId(); |
| 4611 | final int reason = REASON_GROUP_SUMMARY_CANCELED; |
| 4612 | for (int i = notificationList.size() - 1; i >= 0; i--) { |
| 4613 | final NotificationRecord childR = notificationList.get(i); |
| 4614 | final StatusBarNotification childSbn = childR.sbn; |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4615 | if ((childSbn.isGroup() && !childSbn.getNotification().isGroupSummary()) && |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4616 | childR.getGroupKey().equals(parentNotification.getGroupKey()) |
Selim Cinek | 1d35979 | 2017-01-13 14:43:43 -0800 | [diff] [blame] | 4617 | && (childR.getFlags() & Notification.FLAG_FOREGROUND_SERVICE) == 0) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 4618 | EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, childSbn.getId(), |
| 4619 | childSbn.getTag(), userId, 0, 0, reason, listenerName); |
Julia Reynolds | 0839c02 | 2017-06-15 15:24:01 -0400 | [diff] [blame] | 4620 | notificationList.remove(i); |
| 4621 | cancelNotificationLocked(childR, sendDelete, reason, wasPosted); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 4622 | } |
| 4623 | } |
| 4624 | } |
| 4625 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4626 | @GuardedBy("mNotificationLock") |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 4627 | void updateLightsLocked() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4628 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4629 | // handle notification lights |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 4630 | NotificationRecord ledNotification = null; |
| 4631 | while (ledNotification == null && !mLights.isEmpty()) { |
| 4632 | final String owner = mLights.get(mLights.size() - 1); |
| 4633 | ledNotification = mNotificationsByKey.get(owner); |
| 4634 | if (ledNotification == null) { |
| 4635 | Slog.wtfStack(TAG, "LED Notification does not exist: " + owner); |
| 4636 | mLights.remove(owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4637 | } |
| 4638 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 4639 | |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 4640 | // 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] | 4641 | if (ledNotification == null || mInCall || mScreenOn) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 4642 | mNotificationLight.turnOff(); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4643 | } else { |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 4644 | NotificationRecord.Light light = ledNotification.getLight(); |
| 4645 | if (light != null && mNotificationPulseEnabled) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 4646 | // pulse repeatedly |
Julia Reynolds | a33f5c4 | 2017-01-31 16:53:35 -0500 | [diff] [blame] | 4647 | mNotificationLight.setFlashing(light.color, Light.LIGHT_FLASH_TIMED, |
| 4648 | light.onMs, light.offMs); |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 4649 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 4650 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4651 | } |
| 4652 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4653 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4654 | @NonNull List<NotificationRecord> findGroupNotificationsLocked(String pkg, |
| 4655 | String groupKey, int userId) { |
| 4656 | List<NotificationRecord> records = new ArrayList<>(); |
| 4657 | records.addAll(findGroupNotificationByListLocked(mNotificationList, pkg, groupKey, userId)); |
| 4658 | records.addAll( |
| 4659 | findGroupNotificationByListLocked(mEnqueuedNotifications, pkg, groupKey, userId)); |
| 4660 | return records; |
| 4661 | } |
| 4662 | |
| 4663 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4664 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4665 | private @NonNull List<NotificationRecord> findGroupNotificationByListLocked( |
| 4666 | ArrayList<NotificationRecord> list, String pkg, String groupKey, int userId) { |
| 4667 | List<NotificationRecord> records = new ArrayList<>(); |
| 4668 | final int len = list.size(); |
| 4669 | for (int i = 0; i < len; i++) { |
| 4670 | NotificationRecord r = list.get(i); |
| 4671 | if (notificationMatchesUserId(r, userId) && r.getGroupKey().equals(groupKey) |
| 4672 | && r.sbn.getPackageName().equals(pkg)) { |
| 4673 | records.add(r); |
| 4674 | } |
| 4675 | } |
| 4676 | return records; |
| 4677 | } |
| 4678 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4679 | // Searches both enqueued and posted notifications by key. |
| 4680 | // TODO: need to combine a bunch of these getters with slightly different behavior. |
| 4681 | // TODO: Should enqueuing just add to mNotificationsByKey instead? |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4682 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4683 | private NotificationRecord findNotificationByKeyLocked(String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4684 | NotificationRecord r; |
| 4685 | if ((r = findNotificationByListLocked(mNotificationList, key)) != null) { |
| 4686 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4687 | } |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4688 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, key)) != null) { |
| 4689 | return r; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4690 | } |
| 4691 | return null; |
| 4692 | } |
| 4693 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4694 | @GuardedBy("mNotificationLock") |
Julia Reynolds | a78cdff | 2017-04-26 10:19:25 -0400 | [diff] [blame] | 4695 | NotificationRecord findNotificationLocked(String pkg, String tag, int id, int userId) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4696 | NotificationRecord r; |
| 4697 | if ((r = findNotificationByListLocked(mNotificationList, pkg, tag, id, userId)) != null) { |
| 4698 | return r; |
| 4699 | } |
| 4700 | if ((r = findNotificationByListLocked(mEnqueuedNotifications, pkg, tag, id, userId)) |
| 4701 | != null) { |
| 4702 | return r; |
| 4703 | } |
| 4704 | return null; |
| 4705 | } |
| 4706 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4707 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4708 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4709 | String pkg, String tag, int id, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4710 | final int len = list.size(); |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4711 | for (int i = 0; i < len; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4712 | NotificationRecord r = list.get(i); |
Vladimir Marko | 2526f33 | 2013-09-11 11:13:55 +0100 | [diff] [blame] | 4713 | if (notificationMatchesUserId(r, userId) && r.sbn.getId() == id && |
| 4714 | TextUtils.equals(r.sbn.getTag(), tag) && r.sbn.getPackageName().equals(pkg)) { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4715 | return r; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4716 | } |
| 4717 | } |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4718 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4719 | } |
| 4720 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4721 | @GuardedBy("mNotificationLock") |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4722 | private NotificationRecord findNotificationByListLocked(ArrayList<NotificationRecord> list, |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4723 | String key) { |
Geoffrey Pitsch | ccc0b97 | 2017-02-15 10:52:26 -0500 | [diff] [blame] | 4724 | final int N = list.size(); |
| 4725 | for (int i = 0; i < N; i++) { |
| 4726 | if (key.equals(list.get(i).getKey())) { |
| 4727 | return list.get(i); |
| 4728 | } |
| 4729 | } |
| 4730 | return null; |
| 4731 | } |
| 4732 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4733 | @GuardedBy("mNotificationLock") |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 4734 | int indexOfNotificationLocked(String key) { |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 4735 | final int N = mNotificationList.size(); |
| 4736 | for (int i = 0; i < N; i++) { |
| 4737 | if (key.equals(mNotificationList.get(i).getKey())) { |
| 4738 | return i; |
| 4739 | } |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 4740 | } |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 4741 | return -1; |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 4742 | } |
| 4743 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 4744 | private void updateNotificationPulse() { |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 4745 | synchronized (mNotificationLock) { |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 4746 | updateLightsLocked(); |
| 4747 | } |
| 4748 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 4749 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4750 | protected boolean isCallingUidSystem() { |
| 4751 | final int uid = Binder.getCallingUid(); |
| 4752 | return uid == Process.SYSTEM_UID; |
| 4753 | } |
| 4754 | |
| 4755 | protected boolean isUidSystemOrPhone(int uid) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4756 | final int appid = UserHandle.getAppId(uid); |
| 4757 | return (appid == Process.SYSTEM_UID || appid == Process.PHONE_UID || uid == 0); |
| 4758 | } |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 4759 | |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4760 | // TODO: Most calls should probably move to isCallerSystem. |
| 4761 | protected boolean isCallerSystemOrPhone() { |
| 4762 | return isUidSystemOrPhone(Binder.getCallingUid()); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4763 | } |
| 4764 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4765 | private void checkCallerIsSystem() { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4766 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4767 | return; |
| 4768 | } |
| 4769 | throw new SecurityException("Disallowed call for uid " + Binder.getCallingUid()); |
| 4770 | } |
| 4771 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 4772 | private void checkCallerIsSystemOrSameApp(String pkg) { |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4773 | if (isCallerSystemOrPhone()) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4774 | return; |
| 4775 | } |
Julia Reynolds | 0cd1b78 | 2016-06-29 08:43:00 -0400 | [diff] [blame] | 4776 | checkCallerIsSameApp(pkg); |
| 4777 | } |
| 4778 | |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 4779 | private boolean isCallerInstantApp(String pkg) { |
| 4780 | // System is always allowed to act for ephemeral apps. |
Geoffrey Pitsch | 2768415 | 2017-05-02 11:41:31 -0400 | [diff] [blame] | 4781 | if (isCallerSystemOrPhone()) { |
Chad Brubaker | 6b68f10 | 2017-01-27 13:39:00 -0800 | [diff] [blame] | 4782 | return false; |
| 4783 | } |
| 4784 | |
| 4785 | mAppOps.checkPackage(Binder.getCallingUid(), pkg); |
| 4786 | |
| 4787 | try { |
| 4788 | ApplicationInfo ai = mPackageManager.getApplicationInfo(pkg, 0, |
| 4789 | UserHandle.getCallingUserId()); |
| 4790 | if (ai == null) { |
| 4791 | throw new SecurityException("Unknown package " + pkg); |
| 4792 | } |
| 4793 | return ai.isInstantApp(); |
| 4794 | } catch (RemoteException re) { |
| 4795 | throw new SecurityException("Unknown package " + pkg, re); |
| 4796 | } |
| 4797 | |
| 4798 | } |
| 4799 | |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 4800 | private void checkCallerIsSameApp(String pkg) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4801 | final int uid = Binder.getCallingUid(); |
| 4802 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 4803 | ApplicationInfo ai = mPackageManager.getApplicationInfo( |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4804 | pkg, 0, UserHandle.getCallingUserId()); |
Dan Sandler | 09afc2e | 2014-07-18 14:29:20 -0400 | [diff] [blame] | 4805 | if (ai == null) { |
| 4806 | throw new SecurityException("Unknown package " + pkg); |
| 4807 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4808 | if (!UserHandle.isSameApp(ai.uid, uid)) { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 4809 | throw new SecurityException("Calling uid " + uid + " gave package " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 4810 | + pkg + " which is owned by uid " + ai.uid); |
| 4811 | } |
| 4812 | } catch (RemoteException re) { |
| 4813 | throw new SecurityException("Unknown package " + pkg + "\n" + re); |
| 4814 | } |
| 4815 | } |
| 4816 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 4817 | private static String callStateToString(int state) { |
| 4818 | switch (state) { |
| 4819 | case TelephonyManager.CALL_STATE_IDLE: return "CALL_STATE_IDLE"; |
| 4820 | case TelephonyManager.CALL_STATE_RINGING: return "CALL_STATE_RINGING"; |
| 4821 | case TelephonyManager.CALL_STATE_OFFHOOK: return "CALL_STATE_OFFHOOK"; |
| 4822 | default: return "CALL_STATE_UNKNOWN_" + state; |
| 4823 | } |
| 4824 | } |
| 4825 | |
| 4826 | private void listenForCallState() { |
| 4827 | TelephonyManager.from(getContext()).listen(new PhoneStateListener() { |
| 4828 | @Override |
| 4829 | public void onCallStateChanged(int state, String incomingNumber) { |
| 4830 | if (mCallState == state) return; |
| 4831 | if (DBG) Slog.d(TAG, "Call state changed: " + callStateToString(state)); |
| 4832 | mCallState = state; |
| 4833 | } |
| 4834 | }, PhoneStateListener.LISTEN_CALL_STATE); |
| 4835 | } |
| 4836 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 4837 | /** |
| 4838 | * Generates a NotificationRankingUpdate from 'sbns', considering only |
| 4839 | * notifications visible to the given listener. |
| 4840 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4841 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4842 | private NotificationRankingUpdate makeRankingUpdateLocked(ManagedServiceInfo info) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4843 | final int N = mNotificationList.size(); |
| 4844 | ArrayList<String> keys = new ArrayList<String>(N); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 4845 | ArrayList<String> interceptedKeys = new ArrayList<String>(N); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4846 | ArrayList<Integer> importance = new ArrayList<>(N); |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4847 | Bundle overrideGroupKeys = new Bundle(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4848 | Bundle visibilityOverrides = new Bundle(); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4849 | Bundle suppressedVisualEffects = new Bundle(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4850 | Bundle explanation = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4851 | Bundle channels = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4852 | Bundle overridePeople = new Bundle(); |
| 4853 | Bundle snoozeCriteria = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4854 | Bundle showBadge = new Bundle(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4855 | for (int i = 0; i < N; i++) { |
| 4856 | NotificationRecord record = mNotificationList.get(i); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 4857 | if (!isVisibleToListener(record.sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 4858 | continue; |
| 4859 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4860 | final String key = record.sbn.getKey(); |
| 4861 | keys.add(key); |
| 4862 | importance.add(record.getImportance()); |
| 4863 | if (record.getImportanceExplanation() != null) { |
| 4864 | explanation.putCharSequence(key, record.getImportanceExplanation()); |
| 4865 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 4866 | if (record.isIntercepted()) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4867 | interceptedKeys.add(key); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 4868 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 4869 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4870 | suppressedVisualEffects.putInt(key, record.getSuppressedVisualEffects()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4871 | if (record.getPackageVisibilityOverride() |
| 4872 | != NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4873 | visibilityOverrides.putInt(key, record.getPackageVisibilityOverride()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4874 | } |
Julia Reynolds | e46bb37 | 2016-03-17 11:05:58 -0400 | [diff] [blame] | 4875 | overrideGroupKeys.putString(key, record.sbn.getOverrideGroupKey()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4876 | channels.putParcelable(key, record.getChannel()); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4877 | overridePeople.putStringArrayList(key, record.getPeopleOverride()); |
| 4878 | snoozeCriteria.putParcelableArrayList(key, record.getSnoozeCriteria()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4879 | showBadge.putBoolean(key, record.canShowBadge()); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 4880 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4881 | final int M = keys.size(); |
| 4882 | String[] keysAr = keys.toArray(new String[M]); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 4883 | String[] interceptedKeysAr = interceptedKeys.toArray(new String[interceptedKeys.size()]); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 4884 | int[] importanceAr = new int[M]; |
| 4885 | for (int i = 0; i < M; i++) { |
| 4886 | importanceAr[i] = importance.get(i); |
| 4887 | } |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 4888 | return new NotificationRankingUpdate(keysAr, interceptedKeysAr, visibilityOverrides, |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 4889 | suppressedVisualEffects, importanceAr, explanation, overrideGroupKeys, |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 4890 | channels, overridePeople, snoozeCriteria, showBadge); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 4891 | } |
| 4892 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 4893 | boolean hasCompanionDevice(ManagedServiceInfo info) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4894 | if (mCompanionManager == null) { |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 4895 | mCompanionManager = getCompanionManager(); |
| 4896 | } |
| 4897 | // Companion mgr doesn't exist on all device types |
| 4898 | if (mCompanionManager == null) { |
| 4899 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4900 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 4901 | long identity = Binder.clearCallingIdentity(); |
| 4902 | try { |
| 4903 | List<String> associations = mCompanionManager.getAssociations( |
| 4904 | info.component.getPackageName(), info.userid); |
| 4905 | if (!ArrayUtils.isEmpty(associations)) { |
| 4906 | return true; |
| 4907 | } |
| 4908 | } catch (SecurityException se) { |
| 4909 | // Not a privileged listener |
| 4910 | } catch (RemoteException re) { |
| 4911 | Slog.e(TAG, "Cannot reach companion device service", re); |
| 4912 | } catch (Exception e) { |
| 4913 | Slog.e(TAG, "Cannot verify listener " + info, e); |
| 4914 | } finally { |
| 4915 | Binder.restoreCallingIdentity(identity); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4916 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 4917 | return false; |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 4918 | } |
| 4919 | |
Julia Reynolds | 727a728 | 2017-04-13 10:54:01 -0400 | [diff] [blame] | 4920 | protected ICompanionDeviceManager getCompanionManager() { |
| 4921 | return ICompanionDeviceManager.Stub.asInterface( |
| 4922 | ServiceManager.getService(Context.COMPANION_DEVICE_SERVICE)); |
| 4923 | } |
| 4924 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 4925 | private boolean isVisibleToListener(StatusBarNotification sbn, ManagedServiceInfo listener) { |
| 4926 | if (!listener.enabledAndUserMatches(sbn.getUserId())) { |
| 4927 | return false; |
| 4928 | } |
Justin Koh | 8d11a5a | 2014-08-04 18:29:49 -0700 | [diff] [blame] | 4929 | // TODO: remove this for older listeners. |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 4930 | return true; |
| 4931 | } |
| 4932 | |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 4933 | private boolean isPackageSuspendedForUser(String pkg, int uid) { |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 4934 | int userId = UserHandle.getUserId(uid); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 4935 | try { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 4936 | return mPackageManager.isPackageSuspendedForUser(pkg, userId); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 4937 | } catch (RemoteException re) { |
| 4938 | throw new SecurityException("Could not talk to package manager service"); |
Andrei Stingaceanu | efc4a34 | 2016-03-22 14:43:01 +0000 | [diff] [blame] | 4939 | } catch (IllegalArgumentException ex) { |
| 4940 | // Package not found. |
| 4941 | return false; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 4942 | } |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 4943 | } |
| 4944 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 4945 | private class TrimCache { |
| 4946 | StatusBarNotification heavy; |
| 4947 | StatusBarNotification sbnClone; |
| 4948 | StatusBarNotification sbnCloneLight; |
| 4949 | |
| 4950 | TrimCache(StatusBarNotification sbn) { |
| 4951 | heavy = sbn; |
| 4952 | } |
| 4953 | |
| 4954 | StatusBarNotification ForListener(ManagedServiceInfo info) { |
| 4955 | if (mListeners.getOnNotificationPostedTrim(info) == TRIM_LIGHT) { |
| 4956 | if (sbnCloneLight == null) { |
| 4957 | sbnCloneLight = heavy.cloneLight(); |
| 4958 | } |
| 4959 | return sbnCloneLight; |
| 4960 | } else { |
| 4961 | if (sbnClone == null) { |
| 4962 | sbnClone = heavy.clone(); |
| 4963 | } |
| 4964 | return sbnClone; |
| 4965 | } |
| 4966 | } |
| 4967 | } |
| 4968 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 4969 | public class NotificationAssistants extends ManagedServices { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4970 | |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 4971 | public NotificationAssistants() { |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 4972 | super(getContext(), mHandler, mNotificationLock, mUserProfiles); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | @Override |
| 4976 | protected Config getConfig() { |
| 4977 | Config c = new Config(); |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 4978 | c.caption = "notification assistant service"; |
| 4979 | c.serviceInterface = NotificationAssistantService.SERVICE_INTERFACE; |
| 4980 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_ASSISTANT; |
| 4981 | c.bindPermission = Manifest.permission.BIND_NOTIFICATION_ASSISTANT_SERVICE; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4982 | c.settingsAction = Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS; |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 4983 | c.clientLabel = R.string.notification_ranker_binding_label; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 4984 | return c; |
| 4985 | } |
| 4986 | |
| 4987 | @Override |
| 4988 | protected IInterface asInterface(IBinder binder) { |
| 4989 | return INotificationListener.Stub.asInterface(binder); |
| 4990 | } |
| 4991 | |
| 4992 | @Override |
| 4993 | protected boolean checkType(IInterface service) { |
| 4994 | return service instanceof INotificationListener; |
| 4995 | } |
| 4996 | |
| 4997 | @Override |
| 4998 | protected void onServiceAdded(ManagedServiceInfo info) { |
| 4999 | mListeners.registerGuestService(info); |
| 5000 | } |
| 5001 | |
| 5002 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5003 | @GuardedBy("mNotificationLock") |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5004 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
| 5005 | mListeners.unregisterService(removed.service, removed.userid); |
| 5006 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5007 | |
| 5008 | public void onNotificationEnqueued(final NotificationRecord r) { |
| 5009 | final StatusBarNotification sbn = r.sbn; |
| 5010 | TrimCache trimCache = new TrimCache(sbn); |
| 5011 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5012 | // There should be only one, but it's a list, so while we enforce |
| 5013 | // singularity elsewhere, we keep it general here, to avoid surprises. |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5014 | for (final ManagedServiceInfo info : NotificationAssistants.this.getServices()) { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5015 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 5016 | if (!sbnVisible) { |
| 5017 | continue; |
| 5018 | } |
| 5019 | |
| 5020 | final int importance = r.getImportance(); |
| 5021 | final boolean fromUser = r.isImportanceFromUser(); |
| 5022 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 5023 | mHandler.post(new Runnable() { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5024 | @Override |
| 5025 | public void run() { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5026 | notifyEnqueued(info, sbnToPost); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5027 | } |
| 5028 | }); |
| 5029 | } |
| 5030 | } |
| 5031 | |
| 5032 | private void notifyEnqueued(final ManagedServiceInfo info, |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5033 | final StatusBarNotification sbn) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5034 | final INotificationListener assistant = (INotificationListener) info.service; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5035 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
| 5036 | try { |
Julia Reynolds | ceecfcf | 2017-01-31 09:44:26 -0500 | [diff] [blame] | 5037 | assistant.onNotificationEnqueued(sbnHolder); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5038 | } catch (RemoteException ex) { |
Julia Reynolds | 77b2cc9 | 2016-11-08 14:41:09 -0500 | [diff] [blame] | 5039 | Log.e(TAG, "unable to notify assistant (enqueued): " + assistant, ex); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5040 | } |
| 5041 | } |
| 5042 | |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5043 | /** |
| 5044 | * asynchronously notify the assistant that a notification has been snoozed until a |
| 5045 | * context |
| 5046 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5047 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5048 | public void notifyAssistantSnoozedLocked(final StatusBarNotification sbn, |
| 5049 | final String snoozeCriterionId) { |
| 5050 | TrimCache trimCache = new TrimCache(sbn); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5051 | for (final ManagedServiceInfo info : getServices()) { |
Julia Reynolds | 7967230 | 2017-01-12 08:30:16 -0500 | [diff] [blame] | 5052 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
| 5053 | mHandler.post(new Runnable() { |
| 5054 | @Override |
| 5055 | public void run() { |
| 5056 | final INotificationListener assistant = |
| 5057 | (INotificationListener) info.service; |
| 5058 | StatusBarNotificationHolder sbnHolder |
| 5059 | = new StatusBarNotificationHolder(sbnToPost); |
| 5060 | try { |
| 5061 | assistant.onNotificationSnoozedUntilContext( |
| 5062 | sbnHolder, snoozeCriterionId); |
| 5063 | } catch (RemoteException ex) { |
| 5064 | Log.e(TAG, "unable to notify assistant (snoozed): " + assistant, ex); |
| 5065 | } |
| 5066 | } |
| 5067 | }); |
| 5068 | } |
| 5069 | } |
| 5070 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5071 | public boolean isEnabled() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5072 | return !getServices().isEmpty(); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5073 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5074 | } |
| 5075 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5076 | public class NotificationListeners extends ManagedServices { |
| 5077 | |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5078 | private final ArraySet<ManagedServiceInfo> mLightTrimListeners = new ArraySet<>(); |
| 5079 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5080 | public NotificationListeners() { |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5081 | super(getContext(), mHandler, mNotificationLock, mUserProfiles); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5082 | } |
| 5083 | |
| 5084 | @Override |
| 5085 | protected Config getConfig() { |
| 5086 | Config c = new Config(); |
| 5087 | c.caption = "notification listener"; |
| 5088 | c.serviceInterface = NotificationListenerService.SERVICE_INTERFACE; |
| 5089 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS; |
| 5090 | c.bindPermission = android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE; |
| 5091 | c.settingsAction = Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS; |
| 5092 | c.clientLabel = R.string.notification_listener_binding_label; |
| 5093 | return c; |
| 5094 | } |
| 5095 | |
| 5096 | @Override |
| 5097 | protected IInterface asInterface(IBinder binder) { |
| 5098 | return INotificationListener.Stub.asInterface(binder); |
| 5099 | } |
| 5100 | |
| 5101 | @Override |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 5102 | protected boolean checkType(IInterface service) { |
| 5103 | return service instanceof INotificationListener; |
| 5104 | } |
| 5105 | |
| 5106 | @Override |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 5107 | public void onServiceAdded(ManagedServiceInfo info) { |
| 5108 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5109 | final NotificationRankingUpdate update; |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5110 | synchronized (mNotificationLock) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5111 | update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5112 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5113 | try { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5114 | listener.onListenerConnected(update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5115 | } catch (RemoteException e) { |
| 5116 | // we tried |
| 5117 | } |
| 5118 | } |
| 5119 | |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5120 | @Override |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5121 | @GuardedBy("mNotificationLock") |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5122 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
Bryce Lee | 7219ada | 2016-04-08 10:54:23 -0700 | [diff] [blame] | 5123 | if (removeDisabledHints(removed)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5124 | updateListenerHintsLocked(); |
Christoph Studer | 0d6ef4b | 2014-12-02 15:00:48 +0100 | [diff] [blame] | 5125 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5126 | } |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5127 | mLightTrimListeners.remove(removed); |
| 5128 | } |
| 5129 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5130 | @GuardedBy("mNotificationLock") |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5131 | public void setOnNotificationPostedTrimLocked(ManagedServiceInfo info, int trim) { |
| 5132 | if (trim == TRIM_LIGHT) { |
| 5133 | mLightTrimListeners.add(info); |
| 5134 | } else { |
| 5135 | mLightTrimListeners.remove(info); |
| 5136 | } |
| 5137 | } |
| 5138 | |
| 5139 | public int getOnNotificationPostedTrim(ManagedServiceInfo info) { |
| 5140 | return mLightTrimListeners.contains(info) ? TRIM_LIGHT : TRIM_FULL; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5141 | } |
| 5142 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5143 | /** |
| 5144 | * asynchronously notify all listeners about a new notification |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5145 | * |
| 5146 | * <p> |
| 5147 | * Also takes care of removing a notification that has been visible to a listener before, |
| 5148 | * but isn't anymore. |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5149 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5150 | @GuardedBy("mNotificationLock") |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5151 | public void notifyPostedLocked(StatusBarNotification sbn, StatusBarNotification oldSbn) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5152 | // Lazily initialized snapshots of the notification. |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5153 | TrimCache trimCache = new TrimCache(sbn); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5154 | |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5155 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5156 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 5157 | boolean oldSbnVisible = oldSbn != null ? isVisibleToListener(oldSbn, info) : false; |
| 5158 | // This notification hasn't been and still isn't visible -> ignore. |
| 5159 | if (!oldSbnVisible && !sbnVisible) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5160 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5161 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5162 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5163 | |
| 5164 | // This notification became invisible -> remove the old one. |
| 5165 | if (oldSbnVisible && !sbnVisible) { |
| 5166 | final StatusBarNotification oldSbnLightClone = oldSbn.cloneLight(); |
| 5167 | mHandler.post(new Runnable() { |
| 5168 | @Override |
| 5169 | public void run() { |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 5170 | notifyRemoved(info, oldSbnLightClone, update, REASON_USER_STOPPED); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5171 | } |
| 5172 | }); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5173 | continue; |
| 5174 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5175 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 5176 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5177 | mHandler.post(new Runnable() { |
| 5178 | @Override |
| 5179 | public void run() { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 5180 | notifyPosted(info, sbnToPost, update); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5181 | } |
| 5182 | }); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5183 | } |
| 5184 | } |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5185 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5186 | /** |
| 5187 | * asynchronously notify all listeners about a removed notification |
| 5188 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5189 | @GuardedBy("mNotificationLock") |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 5190 | public void notifyRemovedLocked(StatusBarNotification sbn, int reason) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5191 | // make a copy in case changes are made to the underlying Notification object |
| 5192 | // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the |
| 5193 | // notification |
| 5194 | final StatusBarNotification sbnLight = sbn.cloneLight(); |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5195 | for (final ManagedServiceInfo info : getServices()) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5196 | if (!isVisibleToListener(sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5197 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5198 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5199 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5200 | mHandler.post(new Runnable() { |
| 5201 | @Override |
| 5202 | public void run() { |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 5203 | notifyRemoved(info, sbnLight, update, reason); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5204 | } |
| 5205 | }); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5206 | } |
| 5207 | } |
| 5208 | |
| 5209 | /** |
| 5210 | * asynchronously notify all listeners about a reordering of notifications |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5211 | */ |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5212 | @GuardedBy("mNotificationLock") |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5213 | public void notifyRankingUpdateLocked() { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5214 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5215 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 5216 | continue; |
| 5217 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5218 | final NotificationRankingUpdate update = makeRankingUpdateLocked(serviceInfo); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5219 | mHandler.post(new Runnable() { |
| 5220 | @Override |
| 5221 | public void run() { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 5222 | notifyRankingUpdate(serviceInfo, update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5223 | } |
| 5224 | }); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5225 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5226 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5227 | |
Julia Reynolds | 88860ce | 2017-06-01 16:55:49 -0400 | [diff] [blame] | 5228 | @GuardedBy("mNotificationLock") |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5229 | public void notifyListenerHintsChangedLocked(final int hints) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5230 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5231 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 5232 | continue; |
| 5233 | } |
| 5234 | mHandler.post(new Runnable() { |
| 5235 | @Override |
| 5236 | public void run() { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5237 | notifyListenerHintsChanged(serviceInfo, hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5238 | } |
| 5239 | }); |
| 5240 | } |
| 5241 | } |
| 5242 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5243 | public void notifyInterruptionFilterChanged(final int interruptionFilter) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5244 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5245 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 5246 | continue; |
| 5247 | } |
| 5248 | mHandler.post(new Runnable() { |
| 5249 | @Override |
| 5250 | public void run() { |
| 5251 | notifyInterruptionFilterChanged(serviceInfo, interruptionFilter); |
| 5252 | } |
| 5253 | }); |
| 5254 | } |
| 5255 | } |
| 5256 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5257 | protected void notifyNotificationChannelChanged(final String pkg, final UserHandle user, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5258 | final NotificationChannel channel, final int modificationType) { |
| 5259 | if (channel == null) { |
| 5260 | return; |
| 5261 | } |
| 5262 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5263 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5264 | continue; |
| 5265 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5266 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5267 | mHandler.post(new Runnable() { |
| 5268 | @Override |
| 5269 | public void run() { |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5270 | if (hasCompanionDevice(serviceInfo)) { |
| 5271 | notifyNotificationChannelChanged( |
| 5272 | serviceInfo, pkg, user, channel, modificationType); |
| 5273 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5274 | } |
| 5275 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5276 | } |
| 5277 | } |
| 5278 | |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5279 | protected void notifyNotificationChannelGroupChanged( |
| 5280 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
| 5281 | final int modificationType) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5282 | if (group == null) { |
| 5283 | return; |
| 5284 | } |
| 5285 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5286 | if (!serviceInfo.enabledAndUserMatches(UserHandle.getCallingUserId())) { |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5287 | continue; |
| 5288 | } |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5289 | |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5290 | mHandler.post(new Runnable() { |
| 5291 | @Override |
| 5292 | public void run() { |
Julia Reynolds | 018aa62 | 2017-04-20 11:31:30 -0400 | [diff] [blame] | 5293 | if (hasCompanionDevice(serviceInfo)) { |
| 5294 | notifyNotificationChannelGroupChanged( |
| 5295 | serviceInfo, pkg, user, group, modificationType); |
| 5296 | } |
Julia Reynolds | da78147 | 2017-04-12 09:41:16 -0400 | [diff] [blame] | 5297 | } |
| 5298 | }); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5299 | } |
| 5300 | } |
| 5301 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5302 | private void notifyPosted(final ManagedServiceInfo info, |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5303 | final StatusBarNotification sbn, NotificationRankingUpdate rankingUpdate) { |
Julia Reynolds | a75c752 | 2017-03-21 17:34:25 -0400 | [diff] [blame] | 5304 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5305 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5306 | try { |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5307 | listener.onNotificationPosted(sbnHolder, rankingUpdate); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5308 | } catch (RemoteException ex) { |
| 5309 | Log.e(TAG, "unable to notify listener (posted): " + listener, ex); |
| 5310 | } |
| 5311 | } |
| 5312 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 5313 | private void notifyRemoved(ManagedServiceInfo info, StatusBarNotification sbn, |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 5314 | NotificationRankingUpdate rankingUpdate, int reason) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5315 | if (!info.enabledAndUserMatches(sbn.getUserId())) { |
| 5316 | return; |
| 5317 | } |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5318 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5319 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5320 | try { |
Julia Reynolds | 3aa5f1e | 2016-11-09 15:43:49 -0500 | [diff] [blame] | 5321 | listener.onNotificationRemoved(sbnHolder, rankingUpdate, reason); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 5322 | } catch (RemoteException ex) { |
| 5323 | Log.e(TAG, "unable to notify listener (removed): " + listener, ex); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 5324 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5325 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5326 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5327 | private void notifyRankingUpdate(ManagedServiceInfo info, |
| 5328 | NotificationRankingUpdate rankingUpdate) { |
| 5329 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5330 | try { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 5331 | listener.onNotificationRankingUpdate(rankingUpdate); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 5332 | } catch (RemoteException ex) { |
| 5333 | Log.e(TAG, "unable to notify listener (ranking update): " + listener, ex); |
| 5334 | } |
| 5335 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5336 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5337 | private void notifyListenerHintsChanged(ManagedServiceInfo info, int hints) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5338 | final INotificationListener listener = (INotificationListener) info.service; |
| 5339 | try { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5340 | listener.onListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5341 | } catch (RemoteException ex) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 5342 | Log.e(TAG, "unable to notify listener (listener hints): " + listener, ex); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 5343 | } |
| 5344 | } |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 5345 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 5346 | private void notifyInterruptionFilterChanged(ManagedServiceInfo info, |
| 5347 | int interruptionFilter) { |
| 5348 | final INotificationListener listener = (INotificationListener) info.service; |
| 5349 | try { |
| 5350 | listener.onInterruptionFilterChanged(interruptionFilter); |
| 5351 | } catch (RemoteException ex) { |
| 5352 | Log.e(TAG, "unable to notify listener (interruption filter): " + listener, ex); |
| 5353 | } |
| 5354 | } |
| 5355 | |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5356 | void notifyNotificationChannelChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5357 | final String pkg, final UserHandle user, final NotificationChannel channel, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5358 | final int modificationType) { |
| 5359 | final INotificationListener listener = (INotificationListener) info.service; |
| 5360 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5361 | listener.onNotificationChannelModification(pkg, user, channel, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5362 | } catch (RemoteException ex) { |
| 5363 | Log.e(TAG, "unable to notify listener (channel changed): " + listener, ex); |
| 5364 | } |
| 5365 | } |
| 5366 | |
| 5367 | private void notifyNotificationChannelGroupChanged(ManagedServiceInfo info, |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5368 | final String pkg, final UserHandle user, final NotificationChannelGroup group, |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5369 | final int modificationType) { |
| 5370 | final INotificationListener listener = (INotificationListener) info.service; |
| 5371 | try { |
Julia Reynolds | f27d6b2 | 2017-04-13 15:48:16 -0400 | [diff] [blame] | 5372 | listener.onNotificationChannelGroupModification(pkg, user, group, modificationType); |
Julia Reynolds | 73ed76b | 2017-04-04 17:04:38 -0400 | [diff] [blame] | 5373 | } catch (RemoteException ex) { |
| 5374 | Log.e(TAG, "unable to notify listener (channel group changed): " + listener, ex); |
| 5375 | } |
| 5376 | } |
| 5377 | |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5378 | public boolean isListenerPackage(String packageName) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 5379 | if (packageName == null) { |
| 5380 | return false; |
| 5381 | } |
| 5382 | // TODO: clean up locking object later |
Geoffrey Pitsch | 331a64d | 2017-01-17 14:00:47 -0500 | [diff] [blame] | 5383 | synchronized (mNotificationLock) { |
Julia Reynolds | 00314d9 | 2017-04-14 10:01:24 -0400 | [diff] [blame] | 5384 | for (final ManagedServiceInfo serviceInfo : getServices()) { |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 5385 | if (packageName.equals(serviceInfo.component.getPackageName())) { |
| 5386 | return true; |
| 5387 | } |
| 5388 | } |
| 5389 | } |
| 5390 | return false; |
| 5391 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 5392 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5393 | |
| 5394 | public static final class DumpFilter { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5395 | public boolean filtered = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5396 | public String pkgFilter; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 5397 | public boolean zen; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 5398 | public long since; |
| 5399 | public boolean stats; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5400 | public boolean redact = true; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 5401 | public boolean proto = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5402 | |
| 5403 | public static DumpFilter parseFromArguments(String[] args) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5404 | final DumpFilter filter = new DumpFilter(); |
| 5405 | for (int ai = 0; ai < args.length; ai++) { |
| 5406 | final String a = args[ai]; |
Julia Reynolds | c9842c1 | 2017-02-07 12:46:41 -0500 | [diff] [blame] | 5407 | if ("--proto".equals(args[0])) { |
| 5408 | filter.proto = true; |
| 5409 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5410 | if ("--noredact".equals(a) || "--reveal".equals(a)) { |
| 5411 | filter.redact = false; |
| 5412 | } else if ("p".equals(a) || "pkg".equals(a) || "--package".equals(a)) { |
| 5413 | if (ai < args.length-1) { |
| 5414 | ai++; |
| 5415 | filter.pkgFilter = args[ai].trim().toLowerCase(); |
| 5416 | if (filter.pkgFilter.isEmpty()) { |
| 5417 | filter.pkgFilter = null; |
| 5418 | } else { |
| 5419 | filter.filtered = true; |
| 5420 | } |
| 5421 | } |
| 5422 | } else if ("--zen".equals(a) || "zen".equals(a)) { |
| 5423 | filter.filtered = true; |
| 5424 | filter.zen = true; |
| 5425 | } else if ("--stats".equals(a)) { |
| 5426 | filter.stats = true; |
| 5427 | if (ai < args.length-1) { |
| 5428 | ai++; |
Tobias Thierer | 28532d0 | 2016-04-21 14:52:10 +0100 | [diff] [blame] | 5429 | filter.since = Long.parseLong(args[ai]); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5430 | } else { |
| 5431 | filter.since = 0; |
| 5432 | } |
| 5433 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5434 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5435 | return filter; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5436 | } |
| 5437 | |
| 5438 | public boolean matches(StatusBarNotification sbn) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5439 | if (!filtered) return true; |
| 5440 | return zen ? true : sbn != null |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 5441 | && (matches(sbn.getPackageName()) || matches(sbn.getOpPkg())); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5442 | } |
| 5443 | |
| 5444 | public boolean matches(ComponentName component) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5445 | if (!filtered) return true; |
| 5446 | return zen ? true : component != null && matches(component.getPackageName()); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5447 | } |
| 5448 | |
| 5449 | public boolean matches(String pkg) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 5450 | if (!filtered) return true; |
| 5451 | return zen ? true : pkg != null && pkg.toLowerCase().contains(pkgFilter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 5452 | } |
| 5453 | |
| 5454 | @Override |
| 5455 | public String toString() { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 5456 | return stats ? "stats" : zen ? "zen" : ('\'' + pkgFilter + '\''); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 5457 | } |
| 5458 | } |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5459 | |
| 5460 | /** |
| 5461 | * Wrapper for a StatusBarNotification object that allows transfer across a oneway |
| 5462 | * binder without sending large amounts of data over a oneway transaction. |
| 5463 | */ |
| 5464 | private static final class StatusBarNotificationHolder |
| 5465 | extends IStatusBarNotificationHolder.Stub { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 5466 | private StatusBarNotification mValue; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5467 | |
| 5468 | public StatusBarNotificationHolder(StatusBarNotification value) { |
| 5469 | mValue = value; |
| 5470 | } |
| 5471 | |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 5472 | /** 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] | 5473 | @Override |
| 5474 | public StatusBarNotification get() { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 5475 | StatusBarNotification value = mValue; |
| 5476 | mValue = null; |
| 5477 | return value; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 5478 | } |
| 5479 | } |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5480 | |
| 5481 | private final class PolicyAccess { |
| 5482 | private static final String SEPARATOR = ":"; |
| 5483 | private final String[] PERM = { |
| 5484 | android.Manifest.permission.ACCESS_NOTIFICATION_POLICY |
| 5485 | }; |
| 5486 | |
| 5487 | public boolean isPackageGranted(String pkg) { |
| 5488 | return pkg != null && getGrantedPackages().contains(pkg); |
| 5489 | } |
| 5490 | |
| 5491 | public void put(String pkg, boolean granted) { |
| 5492 | if (pkg == null) return; |
| 5493 | final ArraySet<String> pkgs = getGrantedPackages(); |
| 5494 | boolean changed; |
| 5495 | if (granted) { |
| 5496 | changed = pkgs.add(pkg); |
| 5497 | } else { |
| 5498 | changed = pkgs.remove(pkg); |
| 5499 | } |
| 5500 | if (!changed) return; |
| 5501 | final String setting = TextUtils.join(SEPARATOR, pkgs); |
| 5502 | final int currentUser = ActivityManager.getCurrentUser(); |
| 5503 | Settings.Secure.putStringForUser(getContext().getContentResolver(), |
| 5504 | Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES, |
| 5505 | setting, |
| 5506 | currentUser); |
| 5507 | getContext().sendBroadcastAsUser(new Intent(NotificationManager |
| 5508 | .ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 5509 | .setPackage(pkg) |
| 5510 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), new UserHandle(currentUser), null); |
| 5511 | } |
| 5512 | |
| 5513 | public ArraySet<String> getGrantedPackages() { |
| 5514 | final ArraySet<String> pkgs = new ArraySet<>(); |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 5515 | |
| 5516 | long identity = Binder.clearCallingIdentity(); |
| 5517 | try { |
| 5518 | final String setting = Settings.Secure.getStringForUser( |
| 5519 | getContext().getContentResolver(), |
| 5520 | Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES, |
| 5521 | ActivityManager.getCurrentUser()); |
| 5522 | if (setting != null) { |
| 5523 | final String[] tokens = setting.split(SEPARATOR); |
| 5524 | for (int i = 0; i < tokens.length; i++) { |
| 5525 | String token = tokens[i]; |
| 5526 | if (token != null) { |
Andreas Gampe | 1ed71f3 | 2015-12-11 15:49:07 -0800 | [diff] [blame] | 5527 | token = token.trim(); |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 5528 | } |
| 5529 | if (TextUtils.isEmpty(token)) { |
| 5530 | continue; |
| 5531 | } |
| 5532 | pkgs.add(token); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5533 | } |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5534 | } |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 5535 | } finally { |
| 5536 | Binder.restoreCallingIdentity(identity); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5537 | } |
| 5538 | return pkgs; |
| 5539 | } |
| 5540 | |
| 5541 | public String[] getRequestingPackages() throws RemoteException { |
Geoffrey Pitsch | e75a66e | 2016-11-22 11:12:11 -0500 | [diff] [blame] | 5542 | final ParceledListSlice list = mPackageManager |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 5543 | .getPackagesHoldingPermissions(PERM, 0 /*flags*/, |
| 5544 | ActivityManager.getCurrentUser()); |
| 5545 | final List<PackageInfo> pkgs = list.getList(); |
| 5546 | if (pkgs == null || pkgs.isEmpty()) return new String[0]; |
| 5547 | final int N = pkgs.size(); |
| 5548 | final String[] rt = new String[N]; |
| 5549 | for (int i = 0; i < N; i++) { |
| 5550 | rt[i] = pkgs.get(i).packageName; |
| 5551 | } |
| 5552 | return rt; |
| 5553 | } |
| 5554 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5555 | } |