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 | |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 19 | import static android.service.notification.NotificationRankerService.REASON_APP_CANCEL; |
| 20 | import static android.service.notification.NotificationRankerService.REASON_APP_CANCEL_ALL; |
| 21 | import static android.service.notification.NotificationRankerService.REASON_DELEGATE_CANCEL; |
| 22 | import static android.service.notification.NotificationRankerService.REASON_DELEGATE_CANCEL_ALL; |
| 23 | import static android.service.notification.NotificationRankerService.REASON_DELEGATE_CLICK; |
| 24 | import static android.service.notification.NotificationRankerService.REASON_DELEGATE_ERROR; |
| 25 | import static android.service.notification.NotificationRankerService.REASON_GROUP_OPTIMIZATION; |
| 26 | import static android.service.notification.NotificationRankerService.REASON_GROUP_SUMMARY_CANCELED; |
| 27 | import static android.service.notification.NotificationRankerService.REASON_LISTENER_CANCEL; |
| 28 | import static android.service.notification.NotificationRankerService.REASON_LISTENER_CANCEL_ALL; |
| 29 | import static android.service.notification.NotificationRankerService.REASON_PACKAGE_BANNED; |
| 30 | import static android.service.notification.NotificationRankerService.REASON_PACKAGE_CHANGED; |
| 31 | import static android.service.notification.NotificationRankerService.REASON_PACKAGE_SUSPENDED; |
| 32 | import static android.service.notification.NotificationRankerService.REASON_PROFILE_TURNED_OFF; |
| 33 | import static android.service.notification.NotificationRankerService.REASON_USER_STOPPED; |
Jason Monk | 6350674 | 2015-12-16 12:06:51 -0500 | [diff] [blame] | 34 | import static android.service.notification.NotificationListenerService.HINT_HOST_DISABLE_EFFECTS; |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 35 | import static android.service.notification.NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF; |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 36 | import static android.service.notification.NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_ON; |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 37 | import static android.service.notification.NotificationListenerService.TRIM_FULL; |
| 38 | import static android.service.notification.NotificationListenerService.TRIM_LIGHT; |
Julia Reynolds | f0f629f | 2016-02-25 09:34:04 -0500 | [diff] [blame] | 39 | import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_DEFAULT; |
Chris Wren | 4a4b49d | 2016-02-09 11:25:08 -0500 | [diff] [blame] | 40 | import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_NONE; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 41 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
| 42 | import static org.xmlpull.v1.XmlPullParser.END_TAG; |
| 43 | import static org.xmlpull.v1.XmlPullParser.START_TAG; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 44 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 45 | import android.Manifest; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 46 | import android.annotation.Nullable; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 47 | import android.app.ActivityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.app.ActivityManagerNative; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 49 | import android.app.AppGlobals; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 50 | import android.app.AppOpsManager; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 51 | import android.app.AutomaticZenRule; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.app.IActivityManager; |
| 53 | import android.app.INotificationManager; |
| 54 | import android.app.ITransientNotification; |
| 55 | import android.app.Notification; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 56 | import android.app.NotificationManager; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 57 | import android.app.NotificationManager.Policy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.app.PendingIntent; |
| 59 | import android.app.StatusBarManager; |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 60 | import android.app.backup.BackupManager; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 61 | import android.app.usage.UsageEvents; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 62 | import android.app.usage.UsageStatsManagerInternal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.content.BroadcastReceiver; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 64 | import android.content.ComponentName; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 65 | import android.content.ContentResolver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.content.Context; |
| 67 | import android.content.Intent; |
| 68 | import android.content.IntentFilter; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 69 | import android.content.pm.ApplicationInfo; |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 70 | import android.content.pm.IPackageManager; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 71 | import android.content.pm.PackageInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | import android.content.pm.PackageManager; |
| 73 | import android.content.pm.PackageManager.NameNotFoundException; |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 74 | import android.content.pm.ParceledListSlice; |
Chris Wren | 66189fc | 2015-06-25 14:04:33 -0400 | [diff] [blame] | 75 | import android.content.pm.UserInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import android.content.res.Resources; |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 77 | import android.database.ContentObserver; |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 78 | import android.media.AudioAttributes; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 79 | import android.media.AudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 80 | import android.media.AudioManagerInternal; |
Jean-Michel Trivi | ceb79bc | 2014-09-05 11:09:14 -0700 | [diff] [blame] | 81 | import android.media.AudioSystem; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 82 | import android.media.IRingtonePlayer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.os.Binder; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 85 | import android.os.Bundle; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 86 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | import android.os.Handler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 88 | import android.os.HandlerThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | import android.os.IBinder; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 90 | import android.os.IInterface; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 91 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import android.os.Message; |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 93 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 94 | import android.os.RemoteException; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 95 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 96 | import android.os.UserHandle; |
Chris Wren | 66189fc | 2015-06-25 14:04:33 -0400 | [diff] [blame] | 97 | import android.os.UserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | import android.os.Vibrator; |
| 99 | import android.provider.Settings; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 100 | import android.service.notification.Condition; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 101 | import android.service.notification.IConditionProvider; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 102 | import android.service.notification.INotificationListener; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 103 | import android.service.notification.IStatusBarNotificationHolder; |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 104 | import android.service.notification.NotificationRankerService; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 105 | import android.service.notification.NotificationListenerService; |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 106 | import android.service.notification.NotificationRankingUpdate; |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 107 | import android.service.notification.StatusBarNotification; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 108 | import android.service.notification.ZenModeConfig; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 109 | import android.telephony.PhoneStateListener; |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 110 | import android.telephony.TelephonyManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 111 | import android.text.TextUtils; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 112 | import android.util.ArrayMap; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 113 | import android.util.ArraySet; |
Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 114 | import android.util.AtomicFile; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | import android.util.Log; |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 116 | import android.util.Slog; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 117 | import android.util.Xml; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 118 | import android.view.accessibility.AccessibilityEvent; |
| 119 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | import android.widget.Toast; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 121 | |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 122 | import com.android.internal.R; |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 123 | import com.android.internal.annotations.VisibleForTesting; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 124 | import com.android.internal.statusbar.NotificationVisibility; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 125 | import com.android.internal.util.FastXmlSerializer; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 126 | import com.android.internal.util.Preconditions; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 127 | import com.android.server.EventLogTags; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 128 | import com.android.server.LocalServices; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 129 | import com.android.server.SystemService; |
| 130 | import com.android.server.lights.Light; |
| 131 | import com.android.server.lights.LightsManager; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 132 | import com.android.server.notification.ManagedServices.ManagedServiceInfo; |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 133 | import com.android.server.statusbar.StatusBarManagerInternal; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 134 | import com.android.server.vr.VrManagerInternal; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 135 | import com.android.server.notification.ManagedServices.UserProfiles; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 136 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 137 | import libcore.io.IoUtils; |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 138 | |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 139 | import org.json.JSONArray; |
| 140 | import org.json.JSONException; |
| 141 | import org.json.JSONObject; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 142 | import org.xmlpull.v1.XmlPullParser; |
| 143 | import org.xmlpull.v1.XmlPullParserException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 144 | import org.xmlpull.v1.XmlSerializer; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 145 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 146 | import java.io.ByteArrayInputStream; |
| 147 | import java.io.ByteArrayOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 148 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | import java.io.FileDescriptor; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 150 | import java.io.FileInputStream; |
| 151 | import java.io.FileNotFoundException; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 152 | import java.io.FileOutputStream; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 153 | import java.io.IOException; |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 154 | import java.io.InputStream; |
| 155 | import java.io.OutputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 157 | import java.nio.charset.StandardCharsets; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 158 | import java.util.ArrayDeque; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | import java.util.ArrayList; |
Julia Reynolds | 1c9bd42 | 2016-03-15 09:25:56 -0400 | [diff] [blame] | 160 | import java.util.Arrays; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 161 | import java.util.HashSet; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 162 | import java.util.Iterator; |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 163 | import java.util.List; |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 164 | import java.util.Map; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 165 | import java.util.Map.Entry; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 166 | import java.util.Objects; |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 167 | import java.util.Set; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 168 | import java.util.concurrent.TimeUnit; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 169 | |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 170 | /** {@hide} */ |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 171 | public class NotificationManagerService extends SystemService { |
| 172 | static final String TAG = "NotificationService"; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 173 | static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Selim Cinek | 4041249 | 2015-12-08 18:03:22 -0800 | [diff] [blame] | 174 | public static final boolean ENABLE_CHILD_NOTIFICATIONS |
| 175 | = SystemProperties.getBoolean("debug.child_notifs", true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 177 | static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 178 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | // message codes |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 180 | static final int MESSAGE_TIMEOUT = 2; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 181 | static final int MESSAGE_SAVE_POLICY_FILE = 3; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 182 | static final int MESSAGE_SEND_RANKING_UPDATE = 4; |
| 183 | static final int MESSAGE_LISTENER_HINTS_CHANGED = 5; |
| 184 | static final int MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED = 6; |
| 185 | |
| 186 | // ranking thread messages |
| 187 | private static final int MESSAGE_RECONSIDER_RANKING = 1000; |
| 188 | private static final int MESSAGE_RANKING_SORT = 1001; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 190 | static final int LONG_DELAY = 3500; // 3.5 seconds |
| 191 | static final int SHORT_DELAY = 2000; // 2 seconds |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 192 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 193 | static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 194 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 195 | 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] | 196 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 197 | static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION; |
Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 198 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 199 | static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true; |
| 200 | static final boolean ENABLE_BLOCKED_TOASTS = true; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 201 | |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 202 | // When #matchesCallFilter is called from the ringer, wait at most |
| 203 | // 3s to resolve the contacts. This timeout is required since |
| 204 | // ContactsProvider might take a long time to start up. |
| 205 | // |
| 206 | // Return STARRED_CONTACT when the timeout is hit in order to avoid |
| 207 | // missed calls in ZEN mode "Important". |
| 208 | static final int MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS = 3000; |
| 209 | static final float MATCHES_CALL_FILTER_TIMEOUT_AFFINITY = |
| 210 | ValidateNotificationPeople.STARRED_CONTACT; |
| 211 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 212 | /** notification_enqueue status value for a newly enqueued notification. */ |
| 213 | private static final int EVENTLOG_ENQUEUE_STATUS_NEW = 0; |
| 214 | |
| 215 | /** notification_enqueue status value for an existing notification. */ |
| 216 | private static final int EVENTLOG_ENQUEUE_STATUS_UPDATE = 1; |
| 217 | |
| 218 | /** notification_enqueue status value for an ignored notification. */ |
| 219 | private static final int EVENTLOG_ENQUEUE_STATUS_IGNORED = 2; |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 220 | private String mRankerServicePackageName; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 221 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 222 | private IActivityManager mAm; |
| 223 | AudioManager mAudioManager; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 224 | AudioManagerInternal mAudioManagerInternal; |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 225 | @Nullable StatusBarManagerInternal mStatusBar; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 226 | Vibrator mVibrator; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 227 | private VrManagerInternal mVrManagerInternal; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 228 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | final IBinder mForegroundToken = new Binder(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 230 | private Handler mHandler; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 231 | private final HandlerThread mRankingThread = new HandlerThread("ranker", |
| 232 | Process.THREAD_PRIORITY_BACKGROUND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 234 | private Light mNotificationLight; |
| 235 | Light mAttentionLight; |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 236 | private int mDefaultNotificationColor; |
| 237 | private int mDefaultNotificationLedOn; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 238 | |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 239 | private int mDefaultNotificationLedOff; |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 240 | private long[] mDefaultVibrationPattern; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 241 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 242 | private long[] mFallbackVibrationPattern; |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 243 | private boolean mUseAttentionLight; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 244 | boolean mSystemReady; |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 245 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 246 | private boolean mDisableNotificationEffects; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 247 | private int mCallState; |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 248 | private String mSoundNotificationKey; |
| 249 | private String mVibrateNotificationKey; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 251 | private final ArraySet<ManagedServiceInfo> mListenersDisablingEffects = new ArraySet<>(); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 252 | private ComponentName mEffectsSuppressor; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 253 | private int mListenerHints; // right now, all hints are global |
John Spurlock | 8310410 | 2015-02-12 23:25:12 -0500 | [diff] [blame] | 254 | private int mInterruptionFilter = NotificationListenerService.INTERRUPTION_FILTER_UNKNOWN; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 255 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 256 | // for enabling and disabling notification pulse behavior |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 257 | private boolean mScreenOn = true; |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 258 | private boolean mInCall = false; |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 259 | private boolean mNotificationPulseEnabled; |
| 260 | |
Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 261 | // used as a mutex for access to all active notifications & listeners |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 262 | final ArrayList<NotificationRecord> mNotificationList = |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 263 | new ArrayList<NotificationRecord>(); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 264 | final ArrayMap<String, NotificationRecord> mNotificationsByKey = |
| 265 | new ArrayMap<String, NotificationRecord>(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 266 | final ArrayList<ToastRecord> mToastQueue = new ArrayList<ToastRecord>(); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 267 | final ArrayMap<String, NotificationRecord> mSummaryByGroupKey = new ArrayMap<>(); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 268 | final PolicyAccess mPolicyAccess = new PolicyAccess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 270 | // The last key in this list owns the hardware. |
| 271 | ArrayList<String> mLights = new ArrayList<>(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 272 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 273 | private AppOpsManager mAppOps; |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 274 | private UsageStatsManagerInternal mAppUsageStats; |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 275 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 276 | private Archive mArchive; |
| 277 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 278 | // Persistent storage for notification policy |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 279 | private AtomicFile mPolicyFile; |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 280 | |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 281 | private static final int DB_VERSION = 1; |
| 282 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 283 | private static final String TAG_NOTIFICATION_POLICY = "notification-policy"; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 284 | private static final String ATTR_VERSION = "version"; |
| 285 | |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 286 | // Obsolete: converted if present, but not resaved to disk. |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 287 | private static final String TAG_BLOCKED_PKGS = "blocked-packages"; |
| 288 | private static final String TAG_PACKAGE = "package"; |
| 289 | private static final String ATTR_NAME = "name"; |
| 290 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 291 | private RankingHelper mRankingHelper; |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 292 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 293 | private final UserProfiles mUserProfiles = new UserProfiles(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 294 | private NotificationListeners mListeners; |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 295 | private NotificationRankers mRankerServices; |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 296 | private ConditionProviders mConditionProviders; |
Christoph Studer | 1c3f81f | 2014-04-16 15:05:56 +0200 | [diff] [blame] | 297 | private NotificationUsageStats mUsageStats; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 298 | |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 299 | private static final int MY_UID = Process.myUid(); |
| 300 | private static final int MY_PID = Process.myPid(); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 301 | private RankingHandler mRankingHandler; |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 302 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 303 | private static class Archive { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 304 | final int mBufferSize; |
| 305 | final ArrayDeque<StatusBarNotification> mBuffer; |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 306 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 307 | public Archive(int size) { |
| 308 | mBufferSize = size; |
| 309 | mBuffer = new ArrayDeque<StatusBarNotification>(mBufferSize); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 310 | } |
Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 311 | |
Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 312 | public String toString() { |
| 313 | final StringBuilder sb = new StringBuilder(); |
| 314 | final int N = mBuffer.size(); |
| 315 | sb.append("Archive ("); |
| 316 | sb.append(N); |
| 317 | sb.append(" notification"); |
| 318 | sb.append((N==1)?")":"s)"); |
| 319 | return sb.toString(); |
| 320 | } |
| 321 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 322 | public void record(StatusBarNotification nr) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 323 | if (mBuffer.size() == mBufferSize) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 324 | mBuffer.removeFirst(); |
| 325 | } |
Daniel Sandler | 26b81d5 | 2013-05-20 20:56:43 -0400 | [diff] [blame] | 326 | |
| 327 | // We don't want to store the heavy bits of the notification in the archive, |
| 328 | // but other clients in the system process might be using the object, so we |
| 329 | // store a (lightened) copy. |
| 330 | mBuffer.addLast(nr.cloneLight()); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 331 | } |
| 332 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 333 | public Iterator<StatusBarNotification> descendingIterator() { |
| 334 | return mBuffer.descendingIterator(); |
| 335 | } |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 336 | |
| 337 | public StatusBarNotification[] getArray(int count) { |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 338 | if (count == 0) count = mBufferSize; |
Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 339 | final StatusBarNotification[] a |
| 340 | = new StatusBarNotification[Math.min(count, mBuffer.size())]; |
| 341 | Iterator<StatusBarNotification> iter = descendingIterator(); |
| 342 | int i=0; |
| 343 | while (iter.hasNext() && i < count) { |
| 344 | a[i++] = iter.next(); |
| 345 | } |
| 346 | return a; |
| 347 | } |
| 348 | |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 349 | } |
| 350 | |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 351 | private void readPolicyXml(InputStream stream, boolean forRestore) |
| 352 | throws XmlPullParserException, NumberFormatException, IOException { |
| 353 | final XmlPullParser parser = Xml.newPullParser(); |
| 354 | parser.setInput(stream, StandardCharsets.UTF_8.name()); |
| 355 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 356 | while (parser.next() != END_DOCUMENT) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 357 | mZenModeHelper.readXml(parser, forRestore); |
| 358 | mRankingHelper.readXml(parser, forRestore); |
| 359 | } |
| 360 | } |
| 361 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 362 | private void loadPolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 363 | if (DBG) Slog.d(TAG, "loadPolicyFile"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 364 | synchronized(mPolicyFile) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 365 | |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 366 | FileInputStream infile = null; |
| 367 | try { |
| 368 | infile = mPolicyFile.openRead(); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 369 | readPolicyXml(infile, false /*forRestore*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 370 | } catch (FileNotFoundException e) { |
| 371 | // No data yet |
| 372 | } catch (IOException e) { |
| 373 | Log.wtf(TAG, "Unable to read notification policy", e); |
| 374 | } catch (NumberFormatException e) { |
| 375 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 376 | } catch (XmlPullParserException e) { |
| 377 | Log.wtf(TAG, "Unable to parse notification policy", e); |
| 378 | } finally { |
| 379 | IoUtils.closeQuietly(infile); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | public void savePolicyFile() { |
| 385 | mHandler.removeMessages(MESSAGE_SAVE_POLICY_FILE); |
| 386 | mHandler.sendEmptyMessage(MESSAGE_SAVE_POLICY_FILE); |
| 387 | } |
| 388 | |
| 389 | private void handleSavePolicyFile() { |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 390 | if (DBG) Slog.d(TAG, "handleSavePolicyFile"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 391 | synchronized (mPolicyFile) { |
| 392 | final FileOutputStream stream; |
| 393 | try { |
| 394 | stream = mPolicyFile.startWrite(); |
| 395 | } catch (IOException e) { |
| 396 | Slog.w(TAG, "Failed to save policy file", e); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | try { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 401 | writePolicyXml(stream, false /*forBackup*/); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 402 | mPolicyFile.finishWrite(stream); |
| 403 | } catch (IOException e) { |
| 404 | Slog.w(TAG, "Failed to save policy file, restoring backup", e); |
| 405 | mPolicyFile.failWrite(stream); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 406 | } |
| 407 | } |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 408 | BackupManager.dataChanged(getContext().getPackageName()); |
| 409 | } |
| 410 | |
| 411 | private void writePolicyXml(OutputStream stream, boolean forBackup) throws IOException { |
| 412 | final XmlSerializer out = new FastXmlSerializer(); |
| 413 | out.setOutput(stream, StandardCharsets.UTF_8.name()); |
| 414 | out.startDocument(null, true); |
| 415 | out.startTag(null, TAG_NOTIFICATION_POLICY); |
| 416 | out.attribute(null, ATTR_VERSION, Integer.toString(DB_VERSION)); |
| 417 | mZenModeHelper.writeXml(out, forBackup); |
| 418 | mRankingHelper.writeXml(out, forBackup); |
| 419 | out.endTag(null, TAG_NOTIFICATION_POLICY); |
| 420 | out.endDocument(); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 421 | } |
| 422 | |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 423 | /** Use this when you actually want to post a notification or toast. |
| 424 | * |
| 425 | * Unchecked. Not exposed via Binder, but can be called in the course of enqueue*(). |
| 426 | */ |
| 427 | private boolean noteNotificationOp(String pkg, int uid) { |
| 428 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg) |
| 429 | != AppOpsManager.MODE_ALLOWED) { |
| 430 | Slog.v(TAG, "notifications are disabled by AppOps for " + pkg); |
| 431 | return false; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 432 | } |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 433 | return true; |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 434 | } |
| 435 | |
Chris Wren | 66189fc | 2015-06-25 14:04:33 -0400 | [diff] [blame] | 436 | /** Use this to check if a package can post a notification or toast. */ |
| 437 | private boolean checkNotificationOp(String pkg, int uid) { |
| 438 | return mAppOps.checkOp(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg) |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 439 | == AppOpsManager.MODE_ALLOWED && !isPackageSuspendedForUser(pkg, uid); |
Chris Wren | 66189fc | 2015-06-25 14:04:33 -0400 | [diff] [blame] | 440 | } |
| 441 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | private static final class ToastRecord |
| 443 | { |
| 444 | final int pid; |
| 445 | final String pkg; |
| 446 | final ITransientNotification callback; |
| 447 | int duration; |
| 448 | |
| 449 | ToastRecord(int pid, String pkg, ITransientNotification callback, int duration) |
| 450 | { |
| 451 | this.pid = pid; |
| 452 | this.pkg = pkg; |
| 453 | this.callback = callback; |
| 454 | this.duration = duration; |
| 455 | } |
| 456 | |
| 457 | void update(int duration) { |
| 458 | this.duration = duration; |
| 459 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 460 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 461 | void dump(PrintWriter pw, String prefix, DumpFilter filter) { |
| 462 | if (filter != null && !filter.matches(pkg)) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | pw.println(prefix + this); |
| 464 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 465 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | @Override |
| 467 | public final String toString() |
| 468 | { |
| 469 | return "ToastRecord{" |
| 470 | + Integer.toHexString(System.identityHashCode(this)) |
| 471 | + " pkg=" + pkg |
| 472 | + " callback=" + callback |
| 473 | + " duration=" + duration; |
| 474 | } |
| 475 | } |
| 476 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 477 | private final NotificationDelegate mNotificationDelegate = new NotificationDelegate() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 479 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | public void onSetDisabled(int status) { |
| 481 | synchronized (mNotificationList) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 482 | mDisableNotificationEffects = |
| 483 | (status & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 484 | if (disableNotificationEffects(null) != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | // cancel whatever's going on |
| 486 | long identity = Binder.clearCallingIdentity(); |
| 487 | try { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 488 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 489 | if (player != null) { |
| 490 | player.stopAsync(); |
| 491 | } |
| 492 | } catch (RemoteException e) { |
| 493 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | Binder.restoreCallingIdentity(identity); |
| 495 | } |
| 496 | |
| 497 | identity = Binder.clearCallingIdentity(); |
| 498 | try { |
| 499 | mVibrator.cancel(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 500 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | Binder.restoreCallingIdentity(identity); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 507 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 508 | public void onClearAll(int callingUid, int callingPid, int userId) { |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 509 | synchronized (mNotificationList) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 510 | cancelAllLocked(callingUid, callingPid, userId, REASON_DELEGATE_CANCEL_ALL, null, |
| 511 | /*includeCurrentProfiles*/ true); |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 512 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | } |
| 514 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 515 | @Override |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 516 | public void onNotificationClick(int callingUid, int callingPid, String key) { |
| 517 | synchronized (mNotificationList) { |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 518 | NotificationRecord r = mNotificationsByKey.get(key); |
| 519 | if (r == null) { |
| 520 | Log.w(TAG, "No notification with key: " + key); |
| 521 | return; |
| 522 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 523 | final long now = System.currentTimeMillis(); |
| 524 | EventLogTags.writeNotificationClicked(key, |
| 525 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
| 526 | |
Christoph Studer | 03b87a2 | 2014-04-30 17:33:27 +0200 | [diff] [blame] | 527 | StatusBarNotification sbn = r.sbn; |
| 528 | cancelNotification(callingUid, callingPid, sbn.getPackageName(), sbn.getTag(), |
| 529 | sbn.getId(), Notification.FLAG_AUTO_CANCEL, |
| 530 | Notification.FLAG_FOREGROUND_SERVICE, false, r.getUserId(), |
| 531 | REASON_DELEGATE_CLICK, null); |
| 532 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | } |
| 534 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 535 | @Override |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 536 | public void onNotificationActionClick(int callingUid, int callingPid, String key, |
| 537 | int actionIndex) { |
| 538 | synchronized (mNotificationList) { |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 539 | NotificationRecord r = mNotificationsByKey.get(key); |
| 540 | if (r == null) { |
| 541 | Log.w(TAG, "No notification with key: " + key); |
| 542 | return; |
| 543 | } |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 544 | final long now = System.currentTimeMillis(); |
| 545 | EventLogTags.writeNotificationActionClicked(key, actionIndex, |
| 546 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
Christoph Studer | 4da84cd | 2014-10-21 17:24:20 +0200 | [diff] [blame] | 547 | // TODO: Log action click via UsageStats. |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 552 | public void onNotificationClear(int callingUid, int callingPid, |
| 553 | String pkg, String tag, int id, int userId) { |
| 554 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 555 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 556 | true, userId, REASON_DELEGATE_CANCEL, null); |
Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 557 | } |
| 558 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 559 | @Override |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 560 | public void onPanelRevealed(boolean clearEffects, int items) { |
| 561 | EventLogTags.writeNotificationPanelRevealed(items); |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 562 | if (clearEffects) { |
| 563 | clearEffects(); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | @Override |
| 568 | public void onPanelHidden() { |
| 569 | EventLogTags.writeNotificationPanelHidden(); |
| 570 | } |
| 571 | |
| 572 | @Override |
| 573 | public void clearEffects() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | synchronized (mNotificationList) { |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 575 | if (DBG) Slog.d(TAG, "clearEffects"); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 576 | clearSoundLocked(); |
| 577 | clearVibrateLocked(); |
| 578 | clearLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | } |
| 580 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 581 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 582 | @Override |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 583 | 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] | 584 | int uid, int initialPid, String message, int userId) { |
Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 585 | Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id |
| 586 | + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")"); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 587 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, 0, false, userId, |
| 588 | REASON_DELEGATE_ERROR, null); |
Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 589 | long ident = Binder.clearCallingIdentity(); |
| 590 | try { |
| 591 | ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg, |
| 592 | "Bad notification posted from package " + pkg |
| 593 | + ": " + message); |
| 594 | } catch (RemoteException e) { |
| 595 | } |
| 596 | Binder.restoreCallingIdentity(ident); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 597 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 598 | |
| 599 | @Override |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 600 | public void onNotificationVisibilityChanged(NotificationVisibility[] newlyVisibleKeys, |
| 601 | NotificationVisibility[] noLongerVisibleKeys) { |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 602 | synchronized (mNotificationList) { |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 603 | for (NotificationVisibility nv : newlyVisibleKeys) { |
| 604 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 605 | if (r == null) continue; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 606 | r.setVisibility(true, nv.rank); |
| 607 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 608 | } |
| 609 | // Note that we might receive this event after notifications |
| 610 | // have already left the system, e.g. after dismissing from the |
| 611 | // shade. Hence not finding notifications in |
| 612 | // mNotificationsByKey is not an exceptional condition. |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 613 | for (NotificationVisibility nv : noLongerVisibleKeys) { |
| 614 | NotificationRecord r = mNotificationsByKey.get(nv.key); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 615 | if (r == null) continue; |
Chris Wren | d1dbc92 | 2015-06-19 17:51:16 -0400 | [diff] [blame] | 616 | r.setVisibility(false, nv.rank); |
| 617 | nv.recycle(); |
Christoph Studer | ffeb0c3 | 2014-05-07 22:23:56 +0200 | [diff] [blame] | 618 | } |
| 619 | } |
Christoph Studer | 92b389d | 2014-04-01 18:44:40 +0200 | [diff] [blame] | 620 | } |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 621 | |
| 622 | @Override |
| 623 | public void onNotificationExpansionChanged(String key, |
| 624 | boolean userAction, boolean expanded) { |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 625 | synchronized (mNotificationList) { |
| 626 | NotificationRecord r = mNotificationsByKey.get(key); |
| 627 | if (r != null) { |
| 628 | r.stats.onExpansionChanged(userAction, expanded); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 629 | final long now = System.currentTimeMillis(); |
| 630 | EventLogTags.writeNotificationExpansion(key, |
| 631 | userAction ? 1 : 0, expanded ? 1 : 0, |
| 632 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
Chris Wren | 78403d7 | 2014-07-28 10:23:24 +0100 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | }; |
| 637 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 638 | private void clearSoundLocked() { |
| 639 | mSoundNotificationKey = null; |
| 640 | long identity = Binder.clearCallingIdentity(); |
| 641 | try { |
| 642 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
| 643 | if (player != null) { |
| 644 | player.stopAsync(); |
| 645 | } |
| 646 | } catch (RemoteException e) { |
| 647 | } finally { |
| 648 | Binder.restoreCallingIdentity(identity); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | private void clearVibrateLocked() { |
| 653 | mVibrateNotificationKey = null; |
| 654 | long identity = Binder.clearCallingIdentity(); |
| 655 | try { |
| 656 | mVibrator.cancel(); |
| 657 | } finally { |
| 658 | Binder.restoreCallingIdentity(identity); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | private void clearLightsLocked() { |
| 663 | // light |
| 664 | mLights.clear(); |
| 665 | updateLightsLocked(); |
| 666 | } |
| 667 | |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 668 | private final BroadcastReceiver mPackageIntentReceiver = new BroadcastReceiver() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 669 | @Override |
| 670 | public void onReceive(Context context, Intent intent) { |
| 671 | String action = intent.getAction(); |
Dianne Hackborn | 29cd7f1 | 2015-01-08 10:37:05 -0800 | [diff] [blame] | 672 | if (action == null) { |
| 673 | return; |
| 674 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 676 | boolean queryRestart = false; |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 677 | boolean queryRemove = false; |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 678 | boolean packageChanged = false; |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 679 | boolean cancelNotifications = true; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 680 | int reason = REASON_PACKAGE_CHANGED; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 681 | |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 682 | if (action.equals(Intent.ACTION_PACKAGE_ADDED) |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 683 | || (queryRemove=action.equals(Intent.ACTION_PACKAGE_REMOVED)) |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 684 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 685 | || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED)) |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 686 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 687 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE) |
| 688 | || action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 689 | int changeUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 690 | UserHandle.USER_ALL); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 691 | String pkgList[] = null; |
Chris Wren | ae9bb57 | 2013-05-15 14:50:28 -0400 | [diff] [blame] | 692 | boolean queryReplace = queryRemove && |
| 693 | intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 694 | if (DBG) Slog.i(TAG, "action=" + action + " queryReplace=" + queryReplace); |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 695 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 696 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 697 | } else if (action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) { |
| 698 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| 699 | reason = REASON_PACKAGE_SUSPENDED; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 700 | } else if (queryRestart) { |
| 701 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 702 | } else { |
| 703 | Uri uri = intent.getData(); |
| 704 | if (uri == null) { |
| 705 | return; |
| 706 | } |
| 707 | String pkgName = uri.getSchemeSpecificPart(); |
| 708 | if (pkgName == null) { |
| 709 | return; |
| 710 | } |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 711 | if (packageChanged) { |
| 712 | // We cancel notifications for packages which have just been disabled |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 713 | try { |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 714 | final IPackageManager pm = AppGlobals.getPackageManager(); |
| 715 | final int enabled = pm.getApplicationEnabledSetting(pkgName, |
| 716 | changeUserId != UserHandle.USER_ALL ? changeUserId : |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 717 | UserHandle.USER_SYSTEM); |
Christopher Tate | 06e5fed | 2013-10-09 14:39:15 -0700 | [diff] [blame] | 718 | if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 719 | || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 720 | cancelNotifications = false; |
| 721 | } |
| 722 | } catch (IllegalArgumentException e) { |
| 723 | // Package doesn't exist; probably racing with uninstall. |
| 724 | // cancelNotifications is already true, so nothing to do here. |
| 725 | if (DBG) { |
| 726 | Slog.i(TAG, "Exception trying to look up app enabled setting", e); |
| 727 | } |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 728 | } catch (RemoteException e) { |
| 729 | // Failed to talk to PackageManagerService Should never happen! |
Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 730 | } |
| 731 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 732 | pkgList = new String[]{pkgName}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | } |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 734 | |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 735 | if (pkgList != null && (pkgList.length > 0)) { |
| 736 | for (String pkgName : pkgList) { |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 737 | if (cancelNotifications) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 738 | cancelAllNotificationsInt(MY_UID, MY_PID, pkgName, 0, 0, !queryRestart, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 739 | changeUserId, reason, null); |
John Spurlock | 79f7892 | 2013-05-16 09:10:05 -0400 | [diff] [blame] | 740 | } |
Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 741 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 742 | } |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 743 | mListeners.onPackagesChanged(queryReplace, pkgList); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 744 | mRankerServices.onPackagesChanged(queryReplace, pkgList); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 745 | mConditionProviders.onPackagesChanged(queryReplace, pkgList); |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 746 | mRankingHelper.onPackagesChanged(queryReplace, pkgList); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | }; |
| 750 | |
| 751 | private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 752 | @Override |
| 753 | public void onReceive(Context context, Intent intent) { |
| 754 | String action = intent.getAction(); |
| 755 | |
| 756 | if (action.equals(Intent.ACTION_SCREEN_ON)) { |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 757 | // Keep track of screen on/off state, but do not turn off the notification light |
| 758 | // until user passes through the lock screen or views the notification. |
| 759 | mScreenOn = true; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 760 | updateNotificationPulse(); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 761 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 762 | mScreenOn = false; |
Christoph Studer | 1f32c65 | 2014-11-26 15:32:20 +0100 | [diff] [blame] | 763 | updateNotificationPulse(); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 764 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
John Spurlock | 5d2eeb1 | 2014-01-16 10:46:36 -0500 | [diff] [blame] | 765 | mInCall = TelephonyManager.EXTRA_STATE_OFFHOOK |
| 766 | .equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE)); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 767 | updateNotificationPulse(); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 768 | } else if (action.equals(Intent.ACTION_USER_STOPPED)) { |
| 769 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 770 | if (userHandle >= 0) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 771 | cancelAllNotificationsInt(MY_UID, MY_PID, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 772 | REASON_USER_STOPPED, null); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 773 | } |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 774 | } else if (action.equals(Intent.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED)) { |
| 775 | boolean inQuietMode = intent.getBooleanExtra(Intent.EXTRA_QUIET_MODE, false); |
| 776 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 777 | if (inQuietMode && userHandle >= 0) { |
| 778 | cancelAllNotificationsInt(MY_UID, MY_PID, null, 0, 0, true, userHandle, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 779 | REASON_PROFILE_TURNED_OFF, null); |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 780 | } |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 781 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { |
| 782 | // turn off LED when user passes through lock screen |
| 783 | mNotificationLight.turnOff(); |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 784 | if (mStatusBar != null) { |
| 785 | mStatusBar.notificationLightOff(); |
| 786 | } |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 787 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 788 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 789 | // reload per-user settings |
| 790 | mSettingsObserver.update(null); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 791 | mUserProfiles.updateCache(context); |
Christoph Studer | b53dfd4 | 2014-09-12 14:45:59 +0200 | [diff] [blame] | 792 | // Refresh managed services |
John Spurlock | 1b8b22b | 2015-05-20 09:47:13 -0400 | [diff] [blame] | 793 | mConditionProviders.onUserSwitched(user); |
| 794 | mListeners.onUserSwitched(user); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 795 | mRankerServices.onUserSwitched(user); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 796 | mZenModeHelper.onUserSwitched(user); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 797 | } else if (action.equals(Intent.ACTION_USER_ADDED)) { |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 798 | mUserProfiles.updateCache(context); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 799 | } else if (action.equals(Intent.ACTION_USER_REMOVED)) { |
| 800 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 801 | mZenModeHelper.onUserRemoved(user); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 802 | } else if (action.equals(Intent.ACTION_USER_UNLOCKED)) { |
| 803 | final int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 804 | mConditionProviders.onUserUnlocked(user); |
| 805 | mListeners.onUserUnlocked(user); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 806 | mRankerServices.onUserUnlocked(user); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 807 | mZenModeHelper.onUserUnlocked(user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | }; |
| 811 | |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 812 | private final class SettingsObserver extends ContentObserver { |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 813 | private final Uri NOTIFICATION_LIGHT_PULSE_URI |
| 814 | = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); |
| 815 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 816 | SettingsObserver(Handler handler) { |
| 817 | super(handler); |
| 818 | } |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 819 | |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 820 | void observe() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 821 | ContentResolver resolver = getContext().getContentResolver(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 822 | resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, |
Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 823 | false, this, UserHandle.USER_ALL); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 824 | update(null); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 827 | @Override public void onChange(boolean selfChange, Uri uri) { |
| 828 | update(uri); |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 831 | public void update(Uri uri) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 832 | ContentResolver resolver = getContext().getContentResolver(); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 833 | if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { |
| 834 | boolean pulseEnabled = Settings.System.getInt(resolver, |
| 835 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0; |
| 836 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 837 | mNotificationPulseEnabled = pulseEnabled; |
| 838 | updateNotificationPulse(); |
| 839 | } |
| 840 | } |
Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 841 | } |
| 842 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 843 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 844 | private SettingsObserver mSettingsObserver; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 845 | private ZenModeHelper mZenModeHelper; |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 846 | |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 847 | private final Runnable mBuzzBeepBlinked = new Runnable() { |
| 848 | @Override |
| 849 | public void run() { |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 850 | if (mStatusBar != null) { |
| 851 | mStatusBar.buzzBeepBlinked(); |
| 852 | } |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 853 | } |
| 854 | }; |
| 855 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 856 | static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) { |
| 857 | int[] ar = r.getIntArray(resid); |
| 858 | if (ar == null) { |
| 859 | return def; |
| 860 | } |
| 861 | final int len = ar.length > maxlen ? maxlen : ar.length; |
| 862 | long[] out = new long[len]; |
| 863 | for (int i=0; i<len; i++) { |
| 864 | out[i] = ar[i]; |
| 865 | } |
| 866 | return out; |
| 867 | } |
| 868 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 869 | public NotificationManagerService(Context context) { |
| 870 | super(context); |
| 871 | } |
| 872 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 873 | @VisibleForTesting |
| 874 | void setAudioManager(AudioManager audioMananger) { |
| 875 | mAudioManager = audioMananger; |
| 876 | } |
| 877 | |
| 878 | @VisibleForTesting |
| 879 | void setVibrator(Vibrator vibrator) { |
| 880 | mVibrator = vibrator; |
| 881 | } |
| 882 | |
| 883 | @VisibleForTesting |
| 884 | void setSystemReady(boolean systemReady) { |
| 885 | mSystemReady = systemReady; |
| 886 | } |
| 887 | |
| 888 | @VisibleForTesting |
| 889 | void setHandler(Handler handler) { |
| 890 | mHandler = handler; |
| 891 | } |
| 892 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 893 | @Override |
| 894 | public void onStart() { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 895 | Resources resources = getContext().getResources(); |
| 896 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | mAm = ActivityManagerNative.getDefault(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 898 | mAppOps = (AppOpsManager) getContext().getSystemService(Context.APP_OPS_SERVICE); |
| 899 | mVibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 900 | mAppUsageStats = LocalServices.getService(UsageStatsManagerInternal.class); |
San Mehat | 3ee1317 | 2010-02-04 20:54:43 -0800 | [diff] [blame] | 901 | |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 902 | // This is the package that contains the AOSP framework update. |
| 903 | mRankerServicePackageName = getContext().getPackageManager() |
| 904 | .getServicesSystemSharedLibraryPackageName(); |
| 905 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 906 | mHandler = new WorkerHandler(); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 907 | mRankingThread.start(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 908 | String[] extractorNames; |
| 909 | try { |
| 910 | extractorNames = resources.getStringArray(R.array.config_notificationSignalExtractors); |
| 911 | } catch (Resources.NotFoundException e) { |
| 912 | extractorNames = new String[0]; |
| 913 | } |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 914 | mUsageStats = new NotificationUsageStats(getContext()); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 915 | mRankingHandler = new RankingHandlerWorker(mRankingThread.getLooper()); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 916 | mRankingHelper = new RankingHelper(getContext(), |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 917 | mRankingHandler, |
Chris Wren | 5eab2b7 | 2015-06-16 13:56:22 -0400 | [diff] [blame] | 918 | mUsageStats, |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 919 | extractorNames); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 920 | mConditionProviders = new ConditionProviders(getContext(), mHandler, mUserProfiles); |
| 921 | mZenModeHelper = new ZenModeHelper(getContext(), mHandler.getLooper(), mConditionProviders); |
John Spurlock | 1c923a3 | 2014-04-27 16:42:29 -0400 | [diff] [blame] | 922 | mZenModeHelper.addCallback(new ZenModeHelper.Callback() { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 923 | @Override |
| 924 | public void onConfigChanged() { |
| 925 | savePolicyFile(); |
| 926 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 927 | |
| 928 | @Override |
| 929 | void onZenModeChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 930 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED); |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 931 | getContext().sendBroadcastAsUser( |
Jason Monk | 6350674 | 2015-12-16 12:06:51 -0500 | [diff] [blame] | 932 | new Intent(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL) |
| 933 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT), |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 934 | UserHandle.ALL, android.Manifest.permission.MANAGE_NOTIFICATIONS); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 935 | synchronized(mNotificationList) { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 936 | updateInterruptionFilterLocked(); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 937 | } |
| 938 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 939 | |
| 940 | @Override |
| 941 | void onPolicyChanged() { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 942 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED); |
| 943 | } |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 944 | }); |
| 945 | final File systemDir = new File(Environment.getDataDirectory(), "system"); |
| 946 | mPolicyFile = new AtomicFile(new File(systemDir, "notification_policy.xml")); |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 947 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 948 | syncBlockDb(); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 949 | |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 950 | // This is a MangedServices object that keeps track of the listeners. |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 951 | mListeners = new NotificationListeners(); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 952 | |
| 953 | // This is a MangedServices object that keeps track of the ranker. |
| 954 | mRankerServices = new NotificationRankers(); |
| 955 | // Find the updatable ranker and register it. |
Julia Reynolds | 1c9bd42 | 2016-03-15 09:25:56 -0400 | [diff] [blame] | 956 | mRankerServices.registerRanker(); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 957 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 958 | mStatusBar = getLocalService(StatusBarManagerInternal.class); |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 959 | if (mStatusBar != null) { |
| 960 | mStatusBar.setNotificationDelegate(mNotificationDelegate); |
| 961 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 962 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 963 | final LightsManager lights = getLocalService(LightsManager.class); |
| 964 | mNotificationLight = lights.getLight(LightsManager.LIGHT_ID_NOTIFICATIONS); |
| 965 | mAttentionLight = lights.getLight(LightsManager.LIGHT_ID_ATTENTION); |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 966 | |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 967 | mDefaultNotificationColor = resources.getColor( |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 968 | R.color.config_defaultNotificationColor); |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 969 | mDefaultNotificationLedOn = resources.getInteger( |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 970 | R.integer.config_defaultNotificationLedOn); |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 971 | mDefaultNotificationLedOff = resources.getInteger( |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 972 | R.integer.config_defaultNotificationLedOff); |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 973 | |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 974 | mDefaultVibrationPattern = getLongArray(resources, |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 975 | R.array.config_defaultNotificationVibePattern, |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 976 | VIBRATE_PATTERN_MAXLEN, |
| 977 | DEFAULT_VIBRATE_PATTERN); |
| 978 | |
| 979 | mFallbackVibrationPattern = getLongArray(resources, |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 980 | R.array.config_notificationFallbackVibePattern, |
Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 981 | VIBRATE_PATTERN_MAXLEN, |
| 982 | DEFAULT_VIBRATE_PATTERN); |
| 983 | |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 984 | mUseAttentionLight = resources.getBoolean(R.bool.config_useAttentionLight); |
| 985 | |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 986 | // Don't start allowing notifications until the setup wizard has run once. |
| 987 | // After that, including subsequent boots, init with notifications turned on. |
| 988 | // This works on the first boot because the setup wizard will toggle this |
| 989 | // 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] | 990 | if (0 == Settings.Global.getInt(getContext().getContentResolver(), |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 991 | Settings.Global.DEVICE_PROVISIONED, 0)) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 992 | mDisableNotificationEffects = true; |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 993 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 994 | mZenModeHelper.initZenMode(); |
John Spurlock | f370177 | 2015-02-12 13:29:37 -0500 | [diff] [blame] | 995 | mInterruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 996 | |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 997 | mUserProfiles.updateCache(getContext()); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 998 | listenForCallState(); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 999 | |
Mike Lockwood | 35e16bf | 2010-11-30 19:53:36 -0500 | [diff] [blame] | 1000 | // register for various Intents |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | IntentFilter filter = new IntentFilter(); |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1002 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 1003 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1004 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1005 | filter.addAction(Intent.ACTION_USER_PRESENT); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1006 | filter.addAction(Intent.ACTION_USER_STOPPED); |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1007 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1008 | filter.addAction(Intent.ACTION_USER_ADDED); |
John Spurlock | 21258a3 | 2015-05-27 18:22:55 -0400 | [diff] [blame] | 1009 | filter.addAction(Intent.ACTION_USER_REMOVED); |
Julia Reynolds | a3dcaff | 2016-02-03 15:04:05 -0500 | [diff] [blame] | 1010 | filter.addAction(Intent.ACTION_USER_UNLOCKED); |
Rubin Xu | 7eadc1b | 2016-02-01 16:13:45 +0000 | [diff] [blame] | 1011 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1012 | getContext().registerReceiver(mIntentReceiver, filter); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1013 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1014 | IntentFilter pkgFilter = new IntentFilter(); |
Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1015 | pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1016 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
Daniel Sandler | aac0eb0 | 2011-08-06 22:51:56 -0400 | [diff] [blame] | 1017 | pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1018 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 1019 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 1020 | pkgFilter.addDataScheme("package"); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1021 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, pkgFilter, null, |
| 1022 | null); |
| 1023 | |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1024 | IntentFilter suspendedPkgFilter = new IntentFilter(); |
| 1025 | suspendedPkgFilter.addAction(Intent.ACTION_PACKAGES_SUSPENDED); |
| 1026 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, |
| 1027 | suspendedPkgFilter, null, null); |
| 1028 | |
Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1029 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Kenny Guy | 7005840 | 2014-10-28 20:45:06 +0000 | [diff] [blame] | 1030 | getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, sdFilter, null, |
| 1031 | null); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1032 | |
Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1033 | mSettingsObserver = new SettingsObserver(mHandler); |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 1034 | |
Griff Hazen | 9f637d1 | 2014-06-10 11:13:51 -0700 | [diff] [blame] | 1035 | mArchive = new Archive(resources.getInteger( |
| 1036 | R.integer.config_notificationServiceArchiveSize)); |
| 1037 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1038 | publishBinderService(Context.NOTIFICATION_SERVICE, mService); |
| 1039 | publishLocalService(NotificationManagerInternal.class, mInternalService); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | } |
| 1041 | |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1042 | private void sendRegisteredOnlyBroadcast(String action) { |
| 1043 | getContext().sendBroadcastAsUser(new Intent(action) |
| 1044 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), UserHandle.ALL, null); |
| 1045 | } |
| 1046 | |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1047 | /** |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1048 | * Make sure the XML config and the the AppOps system agree about blocks. |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1049 | */ |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1050 | private void syncBlockDb() { |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1051 | loadPolicyFile(); |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1052 | |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1053 | // sync bans from ranker into app opps |
| 1054 | Map<Integer, String> packageBans = mRankingHelper.getPackageBans(); |
| 1055 | for(Entry<Integer, String> ban : packageBans.entrySet()) { |
| 1056 | final int uid = ban.getKey(); |
| 1057 | final String packageName = ban.getValue(); |
| 1058 | setNotificationsEnabledForPackageImpl(packageName, uid, false); |
| 1059 | } |
| 1060 | |
| 1061 | // sync bans from app opps into ranker |
| 1062 | packageBans.clear(); |
| 1063 | for (UserInfo user : UserManager.get(getContext()).getUsers()) { |
| 1064 | final int userId = user.getUserHandle().getIdentifier(); |
| 1065 | final PackageManager packageManager = getContext().getPackageManager(); |
| 1066 | List<PackageInfo> packages = packageManager.getInstalledPackagesAsUser(0, userId); |
| 1067 | final int packageCount = packages.size(); |
| 1068 | for (int p = 0; p < packageCount; p++) { |
| 1069 | final String packageName = packages.get(p).packageName; |
| 1070 | try { |
| 1071 | final int uid = packageManager.getPackageUidAsUser(packageName, userId); |
| 1072 | if (!checkNotificationOp(packageName, uid)) { |
| 1073 | packageBans.put(uid, packageName); |
| 1074 | } |
| 1075 | } catch (NameNotFoundException e) { |
| 1076 | // forget you |
| 1077 | } |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1078 | } |
| 1079 | } |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1080 | for (Entry<Integer, String> ban : packageBans.entrySet()) { |
| 1081 | mRankingHelper.setImportance(ban.getValue(), ban.getKey(), IMPORTANCE_NONE); |
| 1082 | } |
| 1083 | |
| 1084 | savePolicyFile(); |
Daniel Sandler | 4a900ac | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1085 | } |
| 1086 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1087 | @Override |
| 1088 | public void onBootPhase(int phase) { |
| 1089 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 1090 | // no beeping until we're basically done booting |
| 1091 | mSystemReady = true; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1092 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1093 | // Grab our optional AudioService |
| 1094 | mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1095 | mAudioManagerInternal = getLocalService(AudioManagerInternal.class); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1096 | mVrManagerInternal = getLocalService(VrManagerInternal.class); |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1097 | mZenModeHelper.onSystemReady(); |
Adam Lesinski | a6db4ab | 2014-03-24 12:31:45 -0700 | [diff] [blame] | 1098 | } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 1099 | // This observer will force an update when observe is called, causing us to |
| 1100 | // bind to listener services. |
| 1101 | mSettingsObserver.observe(); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1102 | mListeners.onBootPhaseAppsCanStart(); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 1103 | mRankerServices.onBootPhaseAppsCanStart(); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1104 | mConditionProviders.onBootPhaseAppsCanStart(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | } |
| 1106 | } |
| 1107 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1108 | void setNotificationsEnabledForPackageImpl(String pkg, int uid, boolean enabled) { |
| 1109 | Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1110 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1111 | mAppOps.setMode(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg, |
| 1112 | enabled ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1113 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1114 | // Now, cancel any outstanding notifications that are part of a just-disabled app |
| 1115 | if (ENABLE_BLOCKED_NOTIFICATIONS && !enabled) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1116 | cancelAllNotificationsInt(MY_UID, MY_PID, pkg, 0, 0, true, UserHandle.getUserId(uid), |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1117 | REASON_PACKAGE_BANNED, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1121 | private void updateListenerHintsLocked() { |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1122 | final int hints = mListenersDisablingEffects.isEmpty() ? 0 : HINT_HOST_DISABLE_EFFECTS; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1123 | if (hints == mListenerHints) return; |
John Spurlock | a708299 | 2015-03-09 12:19:23 -0400 | [diff] [blame] | 1124 | ZenLog.traceListenerHintsChanged(mListenerHints, hints, mListenersDisablingEffects.size()); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1125 | mListenerHints = hints; |
| 1126 | scheduleListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 1127 | } |
| 1128 | |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1129 | private void updateEffectsSuppressorLocked() { |
| 1130 | final ComponentName suppressor = !mListenersDisablingEffects.isEmpty() |
| 1131 | ? mListenersDisablingEffects.valueAt(0).component : null; |
| 1132 | if (Objects.equals(suppressor, mEffectsSuppressor)) return; |
John Spurlock | a708299 | 2015-03-09 12:19:23 -0400 | [diff] [blame] | 1133 | ZenLog.traceEffectsSuppressorChanged(mEffectsSuppressor, suppressor); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1134 | mEffectsSuppressor = suppressor; |
John Spurlock | 8403b75 | 2014-12-10 12:47:01 -0500 | [diff] [blame] | 1135 | mZenModeHelper.setEffectsSuppressed(suppressor != null); |
John Spurlock | e7a835b | 2015-05-13 10:47:05 -0400 | [diff] [blame] | 1136 | sendRegisteredOnlyBroadcast(NotificationManager.ACTION_EFFECTS_SUPPRESSOR_CHANGED); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1137 | } |
| 1138 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1139 | private void updateInterruptionFilterLocked() { |
| 1140 | int interruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter(); |
| 1141 | if (interruptionFilter == mInterruptionFilter) return; |
| 1142 | mInterruptionFilter = interruptionFilter; |
| 1143 | scheduleInterruptionFilterChanged(interruptionFilter); |
| 1144 | } |
| 1145 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1146 | private final IBinder mService = new INotificationManager.Stub() { |
| 1147 | // Toasts |
| 1148 | // ============================================================================ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1149 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1150 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1151 | public void enqueueToast(String pkg, ITransientNotification callback, int duration) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1152 | { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1153 | if (DBG) { |
| 1154 | Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback |
| 1155 | + " duration=" + duration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1156 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1157 | |
| 1158 | if (pkg == null || callback == null) { |
| 1159 | Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback); |
| 1160 | return ; |
| 1161 | } |
| 1162 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1163 | final boolean isSystemToast = isCallerSystem() || ("android".equals(pkg)); |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 1164 | final boolean isPackageSuspended = |
| 1165 | isPackageSuspendedForUser(pkg, Binder.getCallingUid()); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1166 | |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1167 | if (ENABLE_BLOCKED_TOASTS && (!noteNotificationOp(pkg, Binder.getCallingUid()) |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 1168 | || isPackageSuspended)) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1169 | if (!isSystemToast) { |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1170 | Slog.e(TAG, "Suppressing toast from package " + pkg |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 1171 | + (isPackageSuspended |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 1172 | ? " due to package suspended by administrator." |
| 1173 | : " by user request.")); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1174 | return; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | synchronized (mToastQueue) { |
| 1179 | int callingPid = Binder.getCallingPid(); |
| 1180 | long callingId = Binder.clearCallingIdentity(); |
| 1181 | try { |
| 1182 | ToastRecord record; |
| 1183 | int index = indexOfToastLocked(pkg, callback); |
| 1184 | // If it's already in the queue, we update it in place, we don't |
| 1185 | // move it to the end of the queue. |
| 1186 | if (index >= 0) { |
| 1187 | record = mToastQueue.get(index); |
| 1188 | record.update(duration); |
| 1189 | } else { |
| 1190 | // Limit the number of toasts that any given package except the android |
| 1191 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| 1192 | if (!isSystemToast) { |
| 1193 | int count = 0; |
| 1194 | final int N = mToastQueue.size(); |
| 1195 | for (int i=0; i<N; i++) { |
| 1196 | final ToastRecord r = mToastQueue.get(i); |
| 1197 | if (r.pkg.equals(pkg)) { |
| 1198 | count++; |
| 1199 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 1200 | Slog.e(TAG, "Package has already posted " + count |
| 1201 | + " toasts. Not showing more. Package=" + pkg); |
| 1202 | return; |
| 1203 | } |
| 1204 | } |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | record = new ToastRecord(callingPid, pkg, callback, duration); |
| 1209 | mToastQueue.add(record); |
| 1210 | index = mToastQueue.size() - 1; |
| 1211 | keepProcessAliveLocked(callingPid); |
| 1212 | } |
| 1213 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 1214 | // new or just been updated. Call back and tell it to show itself. |
| 1215 | // If the callback fails, this will remove it from the list, so don't |
| 1216 | // assume that it's valid after this. |
| 1217 | if (index == 0) { |
| 1218 | showNextToastLocked(); |
| 1219 | } |
| 1220 | } finally { |
| 1221 | Binder.restoreCallingIdentity(callingId); |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | @Override |
| 1227 | public void cancelToast(String pkg, ITransientNotification callback) { |
| 1228 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| 1229 | |
| 1230 | if (pkg == null || callback == null) { |
| 1231 | Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback); |
| 1232 | return ; |
| 1233 | } |
| 1234 | |
| 1235 | synchronized (mToastQueue) { |
| 1236 | long callingId = Binder.clearCallingIdentity(); |
| 1237 | try { |
| 1238 | int index = indexOfToastLocked(pkg, callback); |
| 1239 | if (index >= 0) { |
| 1240 | cancelToastLocked(index); |
| 1241 | } else { |
| 1242 | Slog.w(TAG, "Toast already cancelled. pkg=" + pkg |
| 1243 | + " callback=" + callback); |
| 1244 | } |
| 1245 | } finally { |
| 1246 | Binder.restoreCallingIdentity(callingId); |
| 1247 | } |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 1252 | public void enqueueNotificationWithTag(String pkg, String opPkg, String tag, int id, |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1253 | Notification notification, int[] idOut, int userId) throws RemoteException { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 1254 | enqueueNotificationInternal(pkg, opPkg, Binder.getCallingUid(), |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1255 | Binder.getCallingPid(), tag, id, notification, idOut, userId); |
| 1256 | } |
| 1257 | |
| 1258 | @Override |
| 1259 | public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1260 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1261 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1262 | Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg); |
| 1263 | // Don't allow client applications to cancel foreground service notis. |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1264 | cancelNotification(Binder.getCallingUid(), Binder.getCallingPid(), pkg, tag, id, 0, |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1265 | Binder.getCallingUid() == Process.SYSTEM_UID |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 1266 | ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId, |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 1267 | REASON_APP_CANCEL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | @Override |
| 1271 | public void cancelAllNotifications(String pkg, int userId) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1272 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1273 | |
| 1274 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1275 | Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg); |
| 1276 | |
| 1277 | // Calling from user space, don't allow the canceling of actively |
| 1278 | // running foreground services. |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1279 | cancelAllNotificationsInt(Binder.getCallingUid(), Binder.getCallingPid(), |
| 1280 | pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1281 | REASON_APP_CANCEL_ALL, null); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | @Override |
| 1285 | public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1286 | checkCallerIsSystem(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1287 | |
| 1288 | setNotificationsEnabledForPackageImpl(pkg, uid, enabled); |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 1289 | mRankingHelper.setEnabled(pkg, uid, enabled); |
| 1290 | savePolicyFile(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | /** |
| 1294 | * Use this when you just want to know if notifications are OK for this package. |
| 1295 | */ |
| 1296 | @Override |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1297 | public boolean areNotificationsEnabled(String pkg) { |
| 1298 | return areNotificationsEnabledForPackage(pkg, Binder.getCallingUid()); |
| 1299 | } |
| 1300 | |
| 1301 | /** |
| 1302 | * Use this when you just want to know if notifications are OK for this package. |
| 1303 | */ |
| 1304 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1305 | public boolean areNotificationsEnabledForPackage(String pkg, int uid) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1306 | checkCallerIsSystemOrSameApp(pkg); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1307 | return (mAppOps.checkOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg) |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 1308 | == AppOpsManager.MODE_ALLOWED) && !isPackageSuspendedForUser(pkg, uid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1311 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1312 | public void setPriority(String pkg, int uid, int priority) { |
Julia Reynolds | dd3e86b | 2016-02-02 10:24:30 -0500 | [diff] [blame] | 1313 | checkCallerIsSystem(); |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1314 | mRankingHelper.setPriority(pkg, uid, priority); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1315 | savePolicyFile(); |
| 1316 | } |
| 1317 | |
| 1318 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1319 | public int getPriority(String pkg, int uid) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1320 | checkCallerIsSystem(); |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1321 | return mRankingHelper.getPriority(pkg, uid); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 1322 | } |
| 1323 | |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 1324 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1325 | public void setVisibilityOverride(String pkg, int uid, int visibility) { |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 1326 | checkCallerIsSystem(); |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1327 | mRankingHelper.setVisibilityOverride(pkg, uid, visibility); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 1328 | savePolicyFile(); |
| 1329 | } |
| 1330 | |
| 1331 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1332 | public int getVisibilityOverride(String pkg, int uid) { |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 1333 | checkCallerIsSystem(); |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1334 | return mRankingHelper.getVisibilityOverride(pkg, uid); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 1335 | } |
| 1336 | |
Julia Reynolds | 5d25ee7 | 2015-11-20 15:38:20 -0500 | [diff] [blame] | 1337 | @Override |
Julia Reynolds | 0edb50c | 2016-02-26 14:08:25 -0500 | [diff] [blame] | 1338 | public void setImportance(String pkg, int uid, int importance) { |
Julia Reynolds | ead00aa | 2015-12-07 08:23:48 -0500 | [diff] [blame] | 1339 | enforceSystemOrSystemUI("Caller not system or systemui"); |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1340 | setNotificationsEnabledForPackageImpl(pkg, uid, |
| 1341 | importance != NotificationListenerService.Ranking.IMPORTANCE_NONE); |
| 1342 | mRankingHelper.setImportance(pkg, uid, importance); |
Julia Reynolds | 5d25ee7 | 2015-11-20 15:38:20 -0500 | [diff] [blame] | 1343 | savePolicyFile(); |
| 1344 | } |
| 1345 | |
| 1346 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1347 | public int getPackageImportance(String pkg) { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1348 | checkCallerIsSystemOrSameApp(pkg); |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1349 | return mRankingHelper.getImportance(pkg, Binder.getCallingUid()); |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | @Override |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1353 | public int getImportance(String pkg, int uid) { |
Julia Reynolds | cac8862 | 2016-03-03 09:28:19 -0500 | [diff] [blame] | 1354 | enforceSystemOrSystemUI("Caller not system or systemui"); |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 1355 | return mRankingHelper.getImportance(pkg, uid); |
Julia Reynolds | be8fdee | 2015-12-18 09:04:34 -0500 | [diff] [blame] | 1356 | } |
| 1357 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1358 | /** |
| 1359 | * System-only API for getting a list of current (i.e. not cleared) notifications. |
| 1360 | * |
| 1361 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 1362 | * @returns A list of all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1363 | */ |
| 1364 | @Override |
| 1365 | public StatusBarNotification[] getActiveNotifications(String callingPkg) { |
| 1366 | // enforce() will ensure the calling uid has the correct permission |
| 1367 | getContext().enforceCallingOrSelfPermission( |
| 1368 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 1369 | "NotificationManagerService.getActiveNotifications"); |
| 1370 | |
| 1371 | StatusBarNotification[] tmp = null; |
| 1372 | int uid = Binder.getCallingUid(); |
| 1373 | |
| 1374 | // noteOp will check to make sure the callingPkg matches the uid |
| 1375 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 1376 | == AppOpsManager.MODE_ALLOWED) { |
| 1377 | synchronized (mNotificationList) { |
| 1378 | tmp = new StatusBarNotification[mNotificationList.size()]; |
| 1379 | final int N = mNotificationList.size(); |
| 1380 | for (int i=0; i<N; i++) { |
| 1381 | tmp[i] = mNotificationList.get(i).sbn; |
| 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | return tmp; |
| 1386 | } |
| 1387 | |
| 1388 | /** |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1389 | * Public API for getting a list of current notifications for the calling package/uid. |
| 1390 | * |
| 1391 | * @returns A list of all the package's notifications, in natural order. |
| 1392 | */ |
| 1393 | @Override |
| 1394 | public ParceledListSlice<StatusBarNotification> getAppActiveNotifications(String pkg, |
| 1395 | int incomingUserId) { |
| 1396 | checkCallerIsSystemOrSameApp(pkg); |
| 1397 | int userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1398 | Binder.getCallingUid(), incomingUserId, true, false, |
| 1399 | "getAppActiveNotifications", pkg); |
| 1400 | |
Erik Wolsheimer | 2242b4d | 2015-11-24 13:22:04 -0800 | [diff] [blame] | 1401 | final ArrayList<StatusBarNotification> list |
| 1402 | = new ArrayList<StatusBarNotification>(mNotificationList.size()); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1403 | |
| 1404 | synchronized (mNotificationList) { |
Erik Wolsheimer | 2242b4d | 2015-11-24 13:22:04 -0800 | [diff] [blame] | 1405 | final int N = mNotificationList.size(); |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 1406 | for (int i = 0; i < N; i++) { |
| 1407 | final StatusBarNotification sbn = mNotificationList.get(i).sbn; |
| 1408 | if (sbn.getPackageName().equals(pkg) && sbn.getUserId() == userId) { |
| 1409 | // We could pass back a cloneLight() but clients might get confused and |
| 1410 | // try to send this thing back to notify() again, which would not work |
| 1411 | // very well. |
| 1412 | final StatusBarNotification sbnOut = new StatusBarNotification( |
| 1413 | sbn.getPackageName(), |
| 1414 | sbn.getOpPkg(), |
| 1415 | sbn.getId(), sbn.getTag(), sbn.getUid(), sbn.getInitialPid(), |
| 1416 | 0, // hide score from apps |
| 1417 | sbn.getNotification().clone(), |
| 1418 | sbn.getUser(), sbn.getPostTime()); |
| 1419 | list.add(sbnOut); |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | return new ParceledListSlice<StatusBarNotification>(list); |
| 1425 | } |
| 1426 | |
| 1427 | /** |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1428 | * System-only API for getting a list of recent (cleared, no longer shown) notifications. |
| 1429 | * |
| 1430 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
| 1431 | */ |
| 1432 | @Override |
| 1433 | public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) { |
| 1434 | // enforce() will ensure the calling uid has the correct permission |
| 1435 | getContext().enforceCallingOrSelfPermission( |
| 1436 | android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 1437 | "NotificationManagerService.getHistoricalNotifications"); |
| 1438 | |
| 1439 | StatusBarNotification[] tmp = null; |
| 1440 | int uid = Binder.getCallingUid(); |
| 1441 | |
| 1442 | // noteOp will check to make sure the callingPkg matches the uid |
| 1443 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 1444 | == AppOpsManager.MODE_ALLOWED) { |
| 1445 | synchronized (mArchive) { |
| 1446 | tmp = mArchive.getArray(count); |
| 1447 | } |
| 1448 | } |
| 1449 | return tmp; |
| 1450 | } |
| 1451 | |
| 1452 | /** |
| 1453 | * Register a listener binder directly with the notification manager. |
| 1454 | * |
| 1455 | * Only works with system callers. Apps should extend |
| 1456 | * {@link android.service.notification.NotificationListenerService}. |
| 1457 | */ |
| 1458 | @Override |
| 1459 | public void registerListener(final INotificationListener listener, |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1460 | final ComponentName component, final int userid) { |
Christoph Studer | 3e144d3 | 2014-05-22 16:48:40 +0200 | [diff] [blame] | 1461 | enforceSystemOrSystemUI("INotificationManager.registerListener"); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 1462 | mListeners.registerService(listener, component, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | /** |
| 1466 | * Remove a listener binder directly |
| 1467 | */ |
| 1468 | @Override |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 1469 | public void unregisterListener(INotificationListener token, int userid) { |
Chris Wren | b7c8109 | 2016-03-10 11:41:10 -0500 | [diff] [blame] | 1470 | mListeners.unregisterService(token, userid); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | /** |
| 1474 | * Allow an INotificationListener to simulate a "clear all" operation. |
| 1475 | * |
| 1476 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications} |
| 1477 | * |
| 1478 | * @param token The binder for the listener, to check that the caller is allowed |
| 1479 | */ |
| 1480 | @Override |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 1481 | public void cancelNotificationsFromListener(INotificationListener token, String[] keys) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1482 | final int callingUid = Binder.getCallingUid(); |
| 1483 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1484 | long identity = Binder.clearCallingIdentity(); |
| 1485 | try { |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 1486 | synchronized (mNotificationList) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1487 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 1488 | if (keys != null) { |
| 1489 | final int N = keys.length; |
| 1490 | for (int i = 0; i < N; i++) { |
| 1491 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 1492 | if (r == null) continue; |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1493 | final int userId = r.sbn.getUserId(); |
| 1494 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 1495 | !mUserProfiles.isCurrentProfile(userId)) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1496 | throw new SecurityException("Disallowed call from listener: " |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1497 | + info.service); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1498 | } |
Griff Hazen | 335e1f0 | 2014-09-11 14:49:31 -0700 | [diff] [blame] | 1499 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 1500 | r.sbn.getPackageName(), r.sbn.getTag(), r.sbn.getId(), |
| 1501 | userId); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 1502 | } |
| 1503 | } else { |
| 1504 | cancelAllLocked(callingUid, callingPid, info.userid, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1505 | REASON_LISTENER_CANCEL_ALL, info, info.supportsProfiles()); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 1506 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 1507 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1508 | } finally { |
| 1509 | Binder.restoreCallingIdentity(identity); |
| 1510 | } |
| 1511 | } |
| 1512 | |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 1513 | /** |
| 1514 | * Handle request from an approved listener to re-enable itself. |
| 1515 | * |
| 1516 | * @param component The componenet to be re-enabled, caller must match package. |
| 1517 | */ |
| 1518 | @Override |
| 1519 | public void requestBindListener(ComponentName component) { |
| 1520 | checkCallerIsSystemOrSameApp(component.getPackageName()); |
| 1521 | long identity = Binder.clearCallingIdentity(); |
| 1522 | try { |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 1523 | ManagedServices manager = mRankerServices.isComponentEnabledForCurrentProfiles(component) |
| 1524 | ? mRankerServices |
Chris Wren | ab41eec | 2016-01-04 18:01:27 -0500 | [diff] [blame] | 1525 | : mListeners; |
| 1526 | manager.setComponentState(component, true); |
| 1527 | } finally { |
| 1528 | Binder.restoreCallingIdentity(identity); |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | @Override |
| 1533 | public void requestUnbindListener(INotificationListener token) { |
| 1534 | long identity = Binder.clearCallingIdentity(); |
| 1535 | try { |
| 1536 | // allow bound services to disable themselves |
| 1537 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 1538 | info.getOwner().setComponentState(info.component, false); |
| 1539 | } finally { |
| 1540 | Binder.restoreCallingIdentity(identity); |
| 1541 | } |
| 1542 | } |
| 1543 | |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 1544 | @Override |
| 1545 | public void setNotificationsShownFromListener(INotificationListener token, String[] keys) { |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 1546 | long identity = Binder.clearCallingIdentity(); |
| 1547 | try { |
| 1548 | synchronized (mNotificationList) { |
| 1549 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 1550 | if (keys != null) { |
| 1551 | final int N = keys.length; |
| 1552 | for (int i = 0; i < N; i++) { |
| 1553 | NotificationRecord r = mNotificationsByKey.get(keys[i]); |
| 1554 | if (r == null) continue; |
| 1555 | final int userId = r.sbn.getUserId(); |
| 1556 | if (userId != info.userid && userId != UserHandle.USER_ALL && |
| 1557 | !mUserProfiles.isCurrentProfile(userId)) { |
| 1558 | throw new SecurityException("Disallowed call from listener: " |
| 1559 | + info.service); |
| 1560 | } |
| 1561 | if (!r.isSeen()) { |
| 1562 | if (DBG) Slog.d(TAG, "Marking notification as seen " + keys[i]); |
| 1563 | mAppUsageStats.reportEvent(r.sbn.getPackageName(), |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 1564 | userId == UserHandle.USER_ALL ? UserHandle.USER_SYSTEM |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 1565 | : userId, |
Adam Lesinski | c8e8729 | 2015-06-10 15:33:45 -0700 | [diff] [blame] | 1566 | UsageEvents.Event.USER_INTERACTION); |
Amith Yamasani | f47e51e | 2015-04-17 10:02:15 -0700 | [diff] [blame] | 1567 | r.setSeen(); |
| 1568 | } |
| 1569 | } |
| 1570 | } |
| 1571 | } |
| 1572 | } finally { |
| 1573 | Binder.restoreCallingIdentity(identity); |
| 1574 | } |
| 1575 | } |
| 1576 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1577 | private void cancelNotificationFromListenerLocked(ManagedServiceInfo info, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1578 | int callingUid, int callingPid, String pkg, String tag, int id, int userId) { |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 1579 | cancelNotification(callingUid, callingPid, pkg, tag, id, 0, |
| 1580 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
| 1581 | true, |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1582 | userId, REASON_LISTENER_CANCEL, info); |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 1583 | } |
| 1584 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1585 | /** |
| 1586 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 1587 | * |
| 1588 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 1589 | * |
| 1590 | * @param token The binder for the listener, to check that the caller is allowed |
| 1591 | */ |
| 1592 | @Override |
| 1593 | public void cancelNotificationFromListener(INotificationListener token, String pkg, |
| 1594 | String tag, int id) { |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 1595 | final int callingUid = Binder.getCallingUid(); |
| 1596 | final int callingPid = Binder.getCallingPid(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1597 | long identity = Binder.clearCallingIdentity(); |
| 1598 | try { |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 1599 | synchronized (mNotificationList) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1600 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 1601 | if (info.supportsProfiles()) { |
| 1602 | Log.e(TAG, "Ignoring deprecated cancelNotification(pkg, tag, id) " |
| 1603 | + "from " + info.component |
| 1604 | + " use cancelNotification(key) instead."); |
| 1605 | } else { |
| 1606 | cancelNotificationFromListenerLocked(info, callingUid, callingPid, |
| 1607 | pkg, tag, id, info.userid); |
| 1608 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 1609 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1610 | } finally { |
| 1611 | Binder.restoreCallingIdentity(identity); |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | /** |
| 1616 | * Allow an INotificationListener to request the list of outstanding notifications seen by |
| 1617 | * the current user. Useful when starting up, after which point the listener callbacks |
| 1618 | * should be used. |
| 1619 | * |
| 1620 | * @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] | 1621 | * @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] | 1622 | * @returns The return value will contain the notifications specified in keys, in that |
| 1623 | * order, or if keys is null, all the notifications, in natural order. |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1624 | */ |
| 1625 | @Override |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 1626 | public ParceledListSlice<StatusBarNotification> getActiveNotificationsFromListener( |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 1627 | INotificationListener token, String[] keys, int trim) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1628 | synchronized (mNotificationList) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1629 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 1630 | final boolean getKeys = keys != null; |
| 1631 | final int N = getKeys ? keys.length : mNotificationList.size(); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 1632 | final ArrayList<StatusBarNotification> list |
| 1633 | = new ArrayList<StatusBarNotification>(N); |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 1634 | for (int i=0; i<N; i++) { |
Dan Sandler | ea75fdd | 2014-08-12 12:29:19 -0400 | [diff] [blame] | 1635 | final NotificationRecord r = getKeys |
| 1636 | ? mNotificationsByKey.get(keys[i]) |
| 1637 | : mNotificationList.get(i); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 1638 | if (r == null) continue; |
| 1639 | StatusBarNotification sbn = r.sbn; |
| 1640 | if (!isVisibleToListener(sbn, info)) continue; |
| 1641 | StatusBarNotification sbnToSend = |
| 1642 | (trim == TRIM_FULL) ? sbn : sbn.cloneLight(); |
| 1643 | list.add(sbnToSend); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1644 | } |
Christoph Studer | cee44ba | 2014-05-20 18:36:43 +0200 | [diff] [blame] | 1645 | return new ParceledListSlice<StatusBarNotification>(list); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1646 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1650 | public void requestHintsFromListener(INotificationListener token, int hints) { |
| 1651 | final long identity = Binder.clearCallingIdentity(); |
| 1652 | try { |
| 1653 | synchronized (mNotificationList) { |
| 1654 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 1655 | final boolean disableEffects = (hints & HINT_HOST_DISABLE_EFFECTS) != 0; |
| 1656 | if (disableEffects) { |
| 1657 | mListenersDisablingEffects.add(info); |
| 1658 | } else { |
| 1659 | mListenersDisablingEffects.remove(info); |
| 1660 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1661 | updateListenerHintsLocked(); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1662 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 1663 | } |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1664 | } finally { |
| 1665 | Binder.restoreCallingIdentity(identity); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | @Override |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1670 | public int getHintsFromListener(INotificationListener token) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 1671 | synchronized (mNotificationList) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 1672 | return mListenerHints; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | @Override |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1677 | public void requestInterruptionFilterFromListener(INotificationListener token, |
| 1678 | int interruptionFilter) throws RemoteException { |
| 1679 | final long identity = Binder.clearCallingIdentity(); |
| 1680 | try { |
| 1681 | synchronized (mNotificationList) { |
John Spurlock | 661f2cf | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1682 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 1683 | mZenModeHelper.requestFromListener(info.component, interruptionFilter); |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 1684 | updateInterruptionFilterLocked(); |
| 1685 | } |
| 1686 | } finally { |
| 1687 | Binder.restoreCallingIdentity(identity); |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | @Override |
| 1692 | public int getInterruptionFilterFromListener(INotificationListener token) |
| 1693 | throws RemoteException { |
| 1694 | synchronized (mNotificationLight) { |
| 1695 | return mInterruptionFilter; |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | @Override |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 1700 | public void setOnNotificationPostedTrimFromListener(INotificationListener token, int trim) |
| 1701 | throws RemoteException { |
| 1702 | synchronized (mNotificationList) { |
| 1703 | final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token); |
| 1704 | if (info == null) return; |
| 1705 | mListeners.setOnNotificationPostedTrimLocked(info, trim); |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1710 | public int getZenMode() { |
| 1711 | return mZenModeHelper.getZenMode(); |
| 1712 | } |
| 1713 | |
| 1714 | @Override |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1715 | public ZenModeConfig getZenModeConfig() { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1716 | enforceSystemOrSystemUIOrVolume("INotificationManager.getZenModeConfig"); |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 1717 | return mZenModeHelper.getConfig(); |
| 1718 | } |
| 1719 | |
| 1720 | @Override |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1721 | public void setZenMode(int mode, Uri conditionId, String reason) throws RemoteException { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1722 | enforceSystemOrSystemUIOrVolume("INotificationManager.setZenMode"); |
| 1723 | final long identity = Binder.clearCallingIdentity(); |
| 1724 | try { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1725 | mZenModeHelper.setManualZenMode(mode, conditionId, reason); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1726 | } finally { |
| 1727 | Binder.restoreCallingIdentity(identity); |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 1732 | public List<ZenModeConfig.ZenRule> getZenRules() throws RemoteException { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 1733 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRules"); |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 1734 | return mZenModeHelper.getZenRules(); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 1738 | public AutomaticZenRule getAutomaticZenRule(String id) throws RemoteException { |
| 1739 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 1740 | enforcePolicyAccess(Binder.getCallingUid(), "getAutomaticZenRule"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 1741 | return mZenModeHelper.getAutomaticZenRule(id); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 1745 | public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 1746 | throws RemoteException { |
| 1747 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 1748 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 1749 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 1750 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 1751 | enforcePolicyAccess(Binder.getCallingUid(), "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 1752 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 1753 | return mZenModeHelper.addAutomaticZenRule(automaticZenRule, |
| 1754 | "addAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | @Override |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 1758 | public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 1759 | throws RemoteException { |
| 1760 | Preconditions.checkNotNull(automaticZenRule, "automaticZenRule is null"); |
| 1761 | Preconditions.checkNotNull(automaticZenRule.getName(), "Name is null"); |
| 1762 | Preconditions.checkNotNull(automaticZenRule.getOwner(), "Owner is null"); |
| 1763 | Preconditions.checkNotNull(automaticZenRule.getConditionId(), "ConditionId is null"); |
| 1764 | enforcePolicyAccess(Binder.getCallingUid(), "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 1765 | |
Julia Reynolds | 361e82d3 | 2016-02-26 18:19:49 -0500 | [diff] [blame] | 1766 | return mZenModeHelper.updateAutomaticZenRule(id, automaticZenRule, |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 1767 | "updateAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | @Override |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 1771 | public boolean removeAutomaticZenRule(String id) throws RemoteException { |
| 1772 | Preconditions.checkNotNull(id, "Id is null"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 1773 | // Verify that they can modify zen rules. |
| 1774 | enforcePolicyAccess(Binder.getCallingUid(), "removeAutomaticZenRule"); |
| 1775 | |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 1776 | return mZenModeHelper.removeAutomaticZenRule(id, "removeAutomaticZenRule"); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | @Override |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 1780 | public boolean removeAutomaticZenRules(String packageName) throws RemoteException { |
| 1781 | Preconditions.checkNotNull(packageName, "Package name is null"); |
| 1782 | enforceSystemOrSystemUI("removeAutomaticZenRules"); |
| 1783 | |
| 1784 | return mZenModeHelper.removeAutomaticZenRules(packageName, "removeAutomaticZenRules"); |
| 1785 | } |
| 1786 | |
| 1787 | @Override |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 1788 | public int getRuleInstanceCount(ComponentName owner) throws RemoteException { |
| 1789 | Preconditions.checkNotNull(owner, "Owner is null"); |
| 1790 | enforceSystemOrSystemUI("getRuleInstanceCount"); |
| 1791 | |
| 1792 | return mZenModeHelper.getCurrentInstanceCount(owner); |
| 1793 | } |
| 1794 | |
| 1795 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1796 | public void setInterruptionFilter(String pkg, int filter) throws RemoteException { |
| 1797 | enforcePolicyAccess(pkg, "setInterruptionFilter"); |
| 1798 | final int zen = NotificationManager.zenModeFromInterruptionFilter(filter, -1); |
| 1799 | if (zen == -1) throw new IllegalArgumentException("Invalid filter: " + filter); |
| 1800 | final long identity = Binder.clearCallingIdentity(); |
| 1801 | try { |
| 1802 | mZenModeHelper.setManualZenMode(zen, null, "setInterruptionFilter"); |
| 1803 | } finally { |
| 1804 | Binder.restoreCallingIdentity(identity); |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | @Override |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 1809 | public void notifyConditions(final String pkg, IConditionProvider provider, |
| 1810 | final Condition[] conditions) { |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 1811 | final ManagedServiceInfo info = mConditionProviders.checkServiceToken(provider); |
| 1812 | checkCallerIsSystemOrSameApp(pkg); |
John Spurlock | a7d92b1 | 2015-05-13 14:48:02 -0400 | [diff] [blame] | 1813 | mHandler.post(new Runnable() { |
| 1814 | @Override |
| 1815 | public void run() { |
| 1816 | mConditionProviders.notifyConditions(pkg, info, conditions); |
| 1817 | } |
| 1818 | }); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 1819 | } |
| 1820 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1821 | private void enforceSystemOrSystemUIOrVolume(String message) { |
| 1822 | if (mAudioManagerInternal != null) { |
| 1823 | final int vcuid = mAudioManagerInternal.getVolumeControllerUid(); |
| 1824 | if (vcuid > 0 && Binder.getCallingUid() == vcuid) { |
| 1825 | return; |
| 1826 | } |
| 1827 | } |
| 1828 | enforceSystemOrSystemUI(message); |
| 1829 | } |
| 1830 | |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 1831 | private void enforceSystemOrSystemUI(String message) { |
| 1832 | if (isCallerSystem()) return; |
| 1833 | getContext().enforceCallingPermission(android.Manifest.permission.STATUS_BAR_SERVICE, |
| 1834 | message); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1835 | } |
| 1836 | |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1837 | private void enforceSystemOrSystemUIOrSamePackage(String pkg, String message) { |
| 1838 | try { |
| 1839 | checkCallerIsSystemOrSameApp(pkg); |
| 1840 | } catch (SecurityException e) { |
| 1841 | getContext().enforceCallingPermission( |
| 1842 | android.Manifest.permission.STATUS_BAR_SERVICE, |
| 1843 | message); |
| 1844 | } |
| 1845 | } |
| 1846 | |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 1847 | private void enforcePolicyAccess(int uid, String method) { |
| 1848 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 1849 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 1850 | return; |
| 1851 | } |
| 1852 | boolean accessAllowed = false; |
| 1853 | String[] packages = getContext().getPackageManager().getPackagesForUid(uid); |
| 1854 | final int packageCount = packages.length; |
| 1855 | for (int i = 0; i < packageCount; i++) { |
| 1856 | if (checkPolicyAccess(packages[i])) { |
| 1857 | accessAllowed = true; |
| 1858 | } |
| 1859 | } |
| 1860 | if (!accessAllowed) { |
| 1861 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 1862 | throw new SecurityException("Notification policy access denied"); |
| 1863 | } |
| 1864 | } |
| 1865 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1866 | private void enforcePolicyAccess(String pkg, String method) { |
Julia Reynolds | 6ee2617 | 2015-09-28 11:34:48 -0400 | [diff] [blame] | 1867 | if (PackageManager.PERMISSION_GRANTED == getContext().checkCallingPermission( |
| 1868 | android.Manifest.permission.MANAGE_NOTIFICATIONS)) { |
| 1869 | return; |
| 1870 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1871 | if (!checkPolicyAccess(pkg)) { |
| 1872 | Slog.w(TAG, "Notification policy access denied calling " + method); |
| 1873 | throw new SecurityException("Notification policy access denied"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1874 | } |
| 1875 | } |
| 1876 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1877 | private boolean checkPackagePolicyAccess(String pkg) { |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 1878 | return mPolicyAccess.isPackageGranted(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | private boolean checkPolicyAccess(String pkg) { |
Julia Reynolds | 0867b3a | 2016-03-30 17:29:54 -0400 | [diff] [blame] | 1882 | try { |
| 1883 | int uid = getContext().getPackageManager().getPackageUidAsUser( |
| 1884 | pkg, UserHandle.getCallingUserId()); |
| 1885 | if (PackageManager.PERMISSION_GRANTED == ActivityManager.checkComponentPermission( |
| 1886 | android.Manifest.permission.MANAGE_NOTIFICATIONS, uid, |
| 1887 | -1, true)) { |
| 1888 | return true; |
| 1889 | } |
| 1890 | } catch (NameNotFoundException e) { |
| 1891 | return false; |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 1892 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1893 | return checkPackagePolicyAccess(pkg) || mListeners.isComponentEnabledForPackage(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1894 | } |
| 1895 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 1896 | @Override |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1897 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1898 | if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1899 | != PackageManager.PERMISSION_GRANTED) { |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 1900 | pw.println("Permission Denial: can't dump NotificationManager from pid=" |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1901 | + Binder.getCallingPid() |
| 1902 | + ", uid=" + Binder.getCallingUid()); |
| 1903 | return; |
| 1904 | } |
| 1905 | |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 1906 | final DumpFilter filter = DumpFilter.parseFromArguments(args); |
| 1907 | if (filter != null && filter.stats) { |
| 1908 | dumpJson(pw, filter); |
| 1909 | } else { |
| 1910 | dumpImpl(pw, filter); |
| 1911 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1912 | } |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1913 | |
| 1914 | @Override |
| 1915 | public ComponentName getEffectsSuppressor() { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1916 | enforceSystemOrSystemUIOrVolume("INotificationManager.getEffectsSuppressor"); |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 1917 | return mEffectsSuppressor; |
| 1918 | } |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 1919 | |
| 1920 | @Override |
| 1921 | public boolean matchesCallFilter(Bundle extras) { |
| 1922 | enforceSystemOrSystemUI("INotificationManager.matchesCallFilter"); |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 1923 | return mZenModeHelper.matchesCallFilter( |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 1924 | Binder.getCallingUserHandle(), |
Christoph Studer | 12aeda8 | 2014-09-23 19:08:56 +0200 | [diff] [blame] | 1925 | extras, |
| 1926 | mRankingHelper.findExtractor(ValidateNotificationPeople.class), |
| 1927 | MATCHES_CALL_FILTER_CONTACTS_TIMEOUT_MS, |
| 1928 | MATCHES_CALL_FILTER_TIMEOUT_AFFINITY); |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 1929 | } |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 1930 | |
| 1931 | @Override |
| 1932 | public boolean isSystemConditionProviderEnabled(String path) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 1933 | enforceSystemOrSystemUIOrVolume("INotificationManager.isSystemConditionProviderEnabled"); |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 1934 | return mConditionProviders.isSystemProviderEnabled(path); |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 1935 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 1936 | |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 1937 | // Backup/restore interface |
| 1938 | @Override |
| 1939 | public byte[] getBackupPayload(int user) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1940 | if (DBG) Slog.d(TAG, "getBackupPayload u=" + user); |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 1941 | //TODO: http://b/22388012 |
| 1942 | if (user != UserHandle.USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1943 | Slog.w(TAG, "getBackupPayload: cannot backup policy for user " + user); |
| 1944 | return null; |
| 1945 | } |
| 1946 | final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 1947 | try { |
| 1948 | writePolicyXml(baos, true /*forBackup*/); |
| 1949 | return baos.toByteArray(); |
| 1950 | } catch (IOException e) { |
| 1951 | Slog.w(TAG, "getBackupPayload: error writing payload for user " + user, e); |
| 1952 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 1953 | return null; |
| 1954 | } |
| 1955 | |
| 1956 | @Override |
| 1957 | public void applyRestore(byte[] payload, int user) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1958 | if (DBG) Slog.d(TAG, "applyRestore u=" + user + " payload=" |
| 1959 | + (payload != null ? new String(payload, StandardCharsets.UTF_8) : null)); |
| 1960 | if (payload == null) { |
| 1961 | Slog.w(TAG, "applyRestore: no payload to restore for user " + user); |
| 1962 | return; |
| 1963 | } |
Xiaohui Chen | ddbe4ca | 2015-08-13 16:20:56 -0700 | [diff] [blame] | 1964 | //TODO: http://b/22388012 |
| 1965 | if (user != UserHandle.USER_SYSTEM) { |
John Spurlock | 35ef0a6 | 2015-05-28 11:24:10 -0400 | [diff] [blame] | 1966 | Slog.w(TAG, "applyRestore: cannot restore policy for user " + user); |
| 1967 | return; |
| 1968 | } |
| 1969 | final ByteArrayInputStream bais = new ByteArrayInputStream(payload); |
| 1970 | try { |
| 1971 | readPolicyXml(bais, true /*forRestore*/); |
| 1972 | savePolicyFile(); |
| 1973 | } catch (NumberFormatException | XmlPullParserException | IOException e) { |
| 1974 | Slog.w(TAG, "applyRestore: error reading payload", e); |
| 1975 | } |
Christopher Tate | f9767d6 | 2015-04-08 14:35:43 -0700 | [diff] [blame] | 1976 | } |
| 1977 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1978 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1979 | public boolean isNotificationPolicyAccessGranted(String pkg) { |
| 1980 | return checkPolicyAccess(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | @Override |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1984 | public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {; |
| 1985 | enforceSystemOrSystemUIOrSamePackage(pkg, |
| 1986 | "request policy access status for another package"); |
Julia Reynolds | a2d0102 | 2016-03-18 15:03:43 -0400 | [diff] [blame] | 1987 | return checkPolicyAccess(pkg); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | @Override |
| 1991 | public String[] getPackagesRequestingNotificationPolicyAccess() |
| 1992 | throws RemoteException { |
| 1993 | enforceSystemOrSystemUI("request policy access packages"); |
| 1994 | final long identity = Binder.clearCallingIdentity(); |
| 1995 | try { |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 1996 | return mPolicyAccess.getRequestingPackages(); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 1997 | } finally { |
| 1998 | Binder.restoreCallingIdentity(identity); |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | @Override |
| 2003 | public void setNotificationPolicyAccessGranted(String pkg, boolean granted) |
| 2004 | throws RemoteException { |
| 2005 | enforceSystemOrSystemUI("grant notification policy access"); |
| 2006 | final long identity = Binder.clearCallingIdentity(); |
| 2007 | try { |
| 2008 | synchronized (mNotificationList) { |
| 2009 | mPolicyAccess.put(pkg, granted); |
| 2010 | } |
| 2011 | } finally { |
| 2012 | Binder.restoreCallingIdentity(identity); |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | @Override |
| 2017 | public Policy getNotificationPolicy(String pkg) { |
| 2018 | enforcePolicyAccess(pkg, "getNotificationPolicy"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2019 | final long identity = Binder.clearCallingIdentity(); |
| 2020 | try { |
| 2021 | return mZenModeHelper.getNotificationPolicy(); |
| 2022 | } finally { |
| 2023 | Binder.restoreCallingIdentity(identity); |
| 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | @Override |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2028 | public void setNotificationPolicy(String pkg, Policy policy) { |
| 2029 | enforcePolicyAccess(pkg, "setNotificationPolicy"); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2030 | final long identity = Binder.clearCallingIdentity(); |
| 2031 | try { |
| 2032 | mZenModeHelper.setNotificationPolicy(policy); |
| 2033 | } finally { |
| 2034 | Binder.restoreCallingIdentity(identity); |
| 2035 | } |
| 2036 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2037 | |
| 2038 | @Override |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 2039 | public void setImportanceFromRankerService(INotificationListener token, String key, |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 2040 | int importance, CharSequence explanation) throws RemoteException { |
Chris Wren | 4a4b49d | 2016-02-09 11:25:08 -0500 | [diff] [blame] | 2041 | if (importance == IMPORTANCE_NONE) { |
| 2042 | throw new IllegalArgumentException("blocking not allowed: key=" + key); |
| 2043 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2044 | final long identity = Binder.clearCallingIdentity(); |
| 2045 | try { |
| 2046 | synchronized (mNotificationList) { |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 2047 | mRankerServices.checkServiceTokenLocked(token); |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2048 | NotificationRecord n = mNotificationsByKey.get(key); |
| 2049 | n.setImportance(importance, explanation); |
| 2050 | mRankingHandler.requestSort(); |
| 2051 | } |
| 2052 | } finally { |
| 2053 | Binder.restoreCallingIdentity(identity); |
| 2054 | } |
| 2055 | } |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 2056 | }; |
John Spurlock | a429429 | 2014-03-24 18:02:32 -0400 | [diff] [blame] | 2057 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 2058 | private String disableNotificationEffects(NotificationRecord record) { |
| 2059 | if (mDisableNotificationEffects) { |
| 2060 | return "booleanState"; |
| 2061 | } |
| 2062 | if ((mListenerHints & HINT_HOST_DISABLE_EFFECTS) != 0) { |
| 2063 | return "listenerHints"; |
| 2064 | } |
| 2065 | if (mCallState != TelephonyManager.CALL_STATE_IDLE && !mZenModeHelper.isCall(record)) { |
| 2066 | return "callState"; |
| 2067 | } |
| 2068 | return null; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2069 | }; |
| 2070 | |
| 2071 | private void dumpJson(PrintWriter pw, DumpFilter filter) { |
| 2072 | JSONObject dump = new JSONObject(); |
| 2073 | try { |
| 2074 | dump.put("service", "Notification Manager"); |
Chris Wren | acf424a | 2016-03-15 12:48:55 -0400 | [diff] [blame] | 2075 | dump.put("bans", mRankingHelper.dumpBansJson(filter)); |
| 2076 | dump.put("ranking", mRankingHelper.dumpJson(filter)); |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 2077 | dump.put("stats", mUsageStats.dumpJson(filter)); |
| 2078 | } catch (JSONException e) { |
| 2079 | e.printStackTrace(); |
| 2080 | } |
| 2081 | pw.println(dump); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2082 | } |
| 2083 | |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2084 | void dumpImpl(PrintWriter pw, DumpFilter filter) { |
| 2085 | pw.print("Current Notification Manager state"); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 2086 | if (filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2087 | pw.print(" (filtered to "); pw.print(filter); pw.print(")"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2088 | } |
| 2089 | pw.println(':'); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2090 | int N; |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 2091 | final boolean zenOnly = filter.filtered && filter.zen; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2092 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2093 | if (!zenOnly) { |
| 2094 | synchronized (mToastQueue) { |
| 2095 | N = mToastQueue.size(); |
| 2096 | if (N > 0) { |
| 2097 | pw.println(" Toast Queue:"); |
| 2098 | for (int i=0; i<N; i++) { |
| 2099 | mToastQueue.get(i).dump(pw, " ", filter); |
| 2100 | } |
| 2101 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2102 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2103 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | synchronized (mNotificationList) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2107 | if (!zenOnly) { |
| 2108 | N = mNotificationList.size(); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2109 | if (N > 0) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2110 | pw.println(" Notification List:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2111 | for (int i=0; i<N; i++) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2112 | final NotificationRecord nr = mNotificationList.get(i); |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 2113 | if (filter.filtered && !filter.matches(nr.sbn)) continue; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 2114 | nr.dump(pw, " ", getContext(), filter.redact); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2115 | } |
| 2116 | pw.println(" "); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2117 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2118 | |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 2119 | if (!filter.filtered) { |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2120 | N = mLights.size(); |
| 2121 | if (N > 0) { |
| 2122 | pw.println(" Lights List:"); |
| 2123 | for (int i=0; i<N; i++) { |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 2124 | if (i == N - 1) { |
| 2125 | pw.print(" > "); |
| 2126 | } else { |
| 2127 | pw.print(" "); |
| 2128 | } |
| 2129 | pw.println(mLights.get(i)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2130 | } |
| 2131 | pw.println(" "); |
| 2132 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 2133 | pw.println(" mUseAttentionLight=" + mUseAttentionLight); |
| 2134 | pw.println(" mNotificationPulseEnabled=" + mNotificationPulseEnabled); |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 2135 | pw.println(" mSoundNotificationKey=" + mSoundNotificationKey); |
| 2136 | pw.println(" mVibrateNotificationKey=" + mVibrateNotificationKey); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2137 | pw.println(" mDisableNotificationEffects=" + mDisableNotificationEffects); |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 2138 | pw.println(" mCallState=" + callStateToString(mCallState)); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2139 | pw.println(" mSystemReady=" + mSystemReady); |
| 2140 | } |
| 2141 | pw.println(" mArchive=" + mArchive.toString()); |
| 2142 | Iterator<StatusBarNotification> iter = mArchive.descendingIterator(); |
| 2143 | int i=0; |
| 2144 | while (iter.hasNext()) { |
| 2145 | final StatusBarNotification sbn = iter.next(); |
| 2146 | if (filter != null && !filter.matches(sbn)) continue; |
| 2147 | pw.println(" " + sbn); |
| 2148 | if (++i >= 5) { |
| 2149 | if (iter.hasNext()) pw.println(" ..."); |
| 2150 | break; |
| 2151 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2152 | } |
| 2153 | } |
| 2154 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2155 | if (!zenOnly) { |
| 2156 | pw.println("\n Usage Stats:"); |
| 2157 | mUsageStats.dump(pw, " ", filter); |
| 2158 | } |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 2159 | |
Julia Reynolds | e6b53e6 | 2015-07-31 09:25:10 -0400 | [diff] [blame] | 2160 | if (!filter.filtered || zenOnly) { |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2161 | pw.println("\n Zen Mode:"); |
John Spurlock | f370177 | 2015-02-12 13:29:37 -0500 | [diff] [blame] | 2162 | pw.print(" mInterruptionFilter="); pw.println(mInterruptionFilter); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2163 | mZenModeHelper.dump(pw, " "); |
John Spurlock | 6ae82a7 | 2014-07-16 16:23:01 -0400 | [diff] [blame] | 2164 | |
| 2165 | pw.println("\n Zen Log:"); |
| 2166 | ZenLog.dump(pw, " "); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2167 | } |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2168 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2169 | if (!zenOnly) { |
| 2170 | pw.println("\n Ranking Config:"); |
| 2171 | mRankingHelper.dump(pw, " ", filter); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 2172 | |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2173 | pw.println("\n Notification listeners:"); |
| 2174 | mListeners.dump(pw, filter); |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2175 | pw.print(" mListenerHints: "); pw.println(mListenerHints); |
| 2176 | pw.print(" mListenersDisablingEffects: ("); |
| 2177 | N = mListenersDisablingEffects.size(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2178 | for (int i = 0; i < N; i++) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2179 | final ManagedServiceInfo listener = mListenersDisablingEffects.valueAt(i); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2180 | if (i > 0) pw.print(','); |
| 2181 | pw.print(listener.component); |
| 2182 | } |
| 2183 | pw.println(')'); |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 2184 | pw.println("\n mRankerServicePackageName: " + mRankerServicePackageName); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 2185 | pw.println("\n Notification ranker services:"); |
| 2186 | mRankerServices.dump(pw, filter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 2187 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 2188 | pw.println("\n Policy access:"); |
| 2189 | pw.print(" mPolicyAccess: "); pw.println(mPolicyAccess); |
John Spurlock | e77bb36 | 2014-04-26 10:24:59 -0400 | [diff] [blame] | 2190 | |
| 2191 | pw.println("\n Condition providers:"); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 2192 | mConditionProviders.dump(pw, filter); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 2193 | |
| 2194 | pw.println("\n Group summaries:"); |
| 2195 | for (Entry<String, NotificationRecord> entry : mSummaryByGroupKey.entrySet()) { |
| 2196 | NotificationRecord r = entry.getValue(); |
| 2197 | pw.println(" " + entry.getKey() + " -> " + r.getKey()); |
| 2198 | if (mNotificationsByKey.get(r.getKey()) != r) { |
| 2199 | pw.println("!!!!!!LEAK: Record not found in mNotificationsByKey."); |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 2200 | r.dump(pw, " ", getContext(), filter.redact); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 2201 | } |
| 2202 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2203 | } |
| 2204 | } |
| 2205 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2206 | /** |
| 2207 | * The private API only accessible to the system process. |
| 2208 | */ |
| 2209 | private final NotificationManagerInternal mInternalService = new NotificationManagerInternal() { |
| 2210 | @Override |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 2211 | public void enqueueNotification(String pkg, String opPkg, int callingUid, int callingPid, |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2212 | String tag, int id, Notification notification, int[] idReceived, int userId) { |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 2213 | enqueueNotificationInternal(pkg, opPkg, callingUid, callingPid, tag, id, notification, |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2214 | idReceived, userId); |
| 2215 | } |
Christoph Studer | 365e4c3 | 2014-09-18 20:35:36 +0200 | [diff] [blame] | 2216 | |
| 2217 | @Override |
| 2218 | public void removeForegroundServiceFlagFromNotification(String pkg, int notificationId, |
| 2219 | int userId) { |
| 2220 | checkCallerIsSystem(); |
| 2221 | synchronized (mNotificationList) { |
| 2222 | int i = indexOfNotificationLocked(pkg, null, notificationId, userId); |
| 2223 | if (i < 0) { |
| 2224 | Log.d(TAG, "stripForegroundServiceFlag: Could not find notification with " |
| 2225 | + "pkg=" + pkg + " / id=" + notificationId + " / userId=" + userId); |
| 2226 | return; |
| 2227 | } |
| 2228 | NotificationRecord r = mNotificationList.get(i); |
| 2229 | StatusBarNotification sbn = r.sbn; |
| 2230 | // NoMan adds flags FLAG_NO_CLEAR and FLAG_ONGOING_EVENT when it sees |
| 2231 | // FLAG_FOREGROUND_SERVICE. Hence it's not enough to remove FLAG_FOREGROUND_SERVICE, |
| 2232 | // we have to revert to the flags we received initially *and* force remove |
| 2233 | // FLAG_FOREGROUND_SERVICE. |
| 2234 | sbn.getNotification().flags = |
| 2235 | (r.mOriginalFlags & ~Notification.FLAG_FOREGROUND_SERVICE); |
| 2236 | mRankingHelper.sort(mNotificationList); |
| 2237 | mListeners.notifyPostedLocked(sbn, sbn /* oldSbn */); |
| 2238 | } |
| 2239 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2240 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2241 | |
Christoph Studer | 8fd7f1e | 2014-04-11 17:35:05 -0400 | [diff] [blame] | 2242 | void enqueueNotificationInternal(final String pkg, final String opPkg, final int callingUid, |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 2243 | final int callingPid, final String tag, final int id, final Notification notification, |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2244 | int[] idOut, int incomingUserId) { |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 2245 | if (DBG) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2246 | Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id |
| 2247 | + " notification=" + notification); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 2248 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 2249 | checkCallerIsSystemOrSameApp(pkg); |
| 2250 | final boolean isSystemNotification = isUidSystem(callingUid) || ("android".equals(pkg)); |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 2251 | final boolean isNotificationFromListener = mListeners.isListenerPackage(pkg); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2252 | |
Scott Greenwald | 9b05c61 | 2013-06-25 23:44:05 -0400 | [diff] [blame] | 2253 | final int userId = ActivityManager.handleIncomingUser(callingPid, |
| 2254 | callingUid, incomingUserId, true, false, "enqueueNotification", pkg); |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 2255 | final UserHandle user = new UserHandle(userId); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2256 | |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 2257 | // Limit the number of notifications that any given package except the android |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 2258 | // package or a registered listener can enqueue. Prevents DOS attacks and deals with leaks. |
| 2259 | if (!isSystemNotification && !isNotificationFromListener) { |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 2260 | synchronized (mNotificationList) { |
| 2261 | int count = 0; |
| 2262 | final int N = mNotificationList.size(); |
| 2263 | for (int i=0; i<N; i++) { |
| 2264 | final NotificationRecord r = mNotificationList.get(i); |
Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 2265 | if (r.sbn.getPackageName().equals(pkg) && r.sbn.getUserId() == userId) { |
Vladimir Marko | 2526f33 | 2013-09-11 11:13:55 +0100 | [diff] [blame] | 2266 | if (r.sbn.getId() == id && TextUtils.equals(r.sbn.getTag(), tag)) { |
| 2267 | break; // Allow updating existing notification |
| 2268 | } |
Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 2269 | count++; |
| 2270 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 2271 | Slog.e(TAG, "Package has already posted " + count |
| 2272 | + " notifications. Not showing more. package=" + pkg); |
| 2273 | return; |
| 2274 | } |
| 2275 | } |
| 2276 | } |
| 2277 | } |
| 2278 | } |
| 2279 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2280 | if (pkg == null || notification == null) { |
| 2281 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 2282 | + " id=" + id + " notification=" + notification); |
| 2283 | } |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 2284 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 2285 | // Sanitize inputs |
| 2286 | notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, |
| 2287 | Notification.PRIORITY_MAX); |
Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 2288 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 2289 | // setup local book-keeping |
| 2290 | final StatusBarNotification n = new StatusBarNotification( |
| 2291 | pkg, opPkg, id, tag, callingUid, callingPid, 0, notification, |
| 2292 | user); |
| 2293 | final NotificationRecord r = new NotificationRecord(getContext(), n); |
| 2294 | mHandler.post(new EnqueueNotificationRunnable(userId, r)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2295 | |
| 2296 | idOut[0] = id; |
| 2297 | } |
| 2298 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 2299 | private class EnqueueNotificationRunnable implements Runnable { |
| 2300 | private final NotificationRecord r; |
| 2301 | private final int userId; |
| 2302 | |
| 2303 | EnqueueNotificationRunnable(int userId, NotificationRecord r) { |
| 2304 | this.userId = userId; |
| 2305 | this.r = r; |
| 2306 | }; |
| 2307 | |
| 2308 | @Override |
| 2309 | public void run() { |
| 2310 | |
| 2311 | synchronized (mNotificationList) { |
| 2312 | final StatusBarNotification n = r.sbn; |
Chris Wren | 1ac52a9 | 2016-02-24 14:54:52 -0500 | [diff] [blame] | 2313 | if (DBG) Slog.d(TAG, "EnqueueNotificationRunnable.run for: " + n.getKey()); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 2314 | NotificationRecord old = mNotificationsByKey.get(n.getKey()); |
| 2315 | if (old != null) { |
| 2316 | // Retain ranking information from previous record |
| 2317 | r.copyRankingInformation(old); |
| 2318 | } |
| 2319 | |
| 2320 | final int callingUid = n.getUid(); |
| 2321 | final int callingPid = n.getInitialPid(); |
| 2322 | final Notification notification = n.getNotification(); |
| 2323 | final String pkg = n.getPackageName(); |
| 2324 | final int id = n.getId(); |
| 2325 | final String tag = n.getTag(); |
| 2326 | final boolean isSystemNotification = isUidSystem(callingUid) || |
| 2327 | ("android".equals(pkg)); |
| 2328 | |
| 2329 | // Handle grouped notifications and bail out early if we |
| 2330 | // can to avoid extracting signals. |
| 2331 | handleGroupedNotificationLocked(r, old, callingUid, callingPid); |
| 2332 | boolean ignoreNotification = |
| 2333 | removeUnusedGroupedNotificationLocked(r, old, callingUid, callingPid); |
Chris Wren | 1ac52a9 | 2016-02-24 14:54:52 -0500 | [diff] [blame] | 2334 | if (DBG) Slog.d(TAG, "ignoreNotification is " + ignoreNotification); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 2335 | |
| 2336 | // This conditional is a dirty hack to limit the logging done on |
| 2337 | // behalf of the download manager without affecting other apps. |
| 2338 | if (!pkg.equals("com.android.providers.downloads") |
| 2339 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| 2340 | int enqueueStatus = EVENTLOG_ENQUEUE_STATUS_NEW; |
| 2341 | if (ignoreNotification) { |
| 2342 | enqueueStatus = EVENTLOG_ENQUEUE_STATUS_IGNORED; |
| 2343 | } else if (old != null) { |
| 2344 | enqueueStatus = EVENTLOG_ENQUEUE_STATUS_UPDATE; |
| 2345 | } |
| 2346 | EventLogTags.writeNotificationEnqueue(callingUid, callingPid, |
| 2347 | pkg, id, tag, userId, notification.toString(), |
| 2348 | enqueueStatus); |
| 2349 | } |
| 2350 | |
| 2351 | if (ignoreNotification) { |
| 2352 | return; |
| 2353 | } |
| 2354 | |
| 2355 | mRankingHelper.extractSignals(r); |
| 2356 | |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 2357 | final boolean isPackageSuspended = isPackageSuspendedForUser(pkg, callingUid); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 2358 | |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 2359 | // blocked apps |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 2360 | if (r.getImportance() == NotificationListenerService.Ranking.IMPORTANCE_NONE |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 2361 | || !noteNotificationOp(pkg, callingUid) || isPackageSuspended) { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 2362 | if (!isSystemNotification) { |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 2363 | if (isPackageSuspended) { |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 2364 | Slog.e(TAG, "Suppressing notification from package due to package " |
| 2365 | + "suspended by administrator."); |
| 2366 | mUsageStats.registerSuspendedByAdmin(r); |
| 2367 | } else { |
| 2368 | Slog.e(TAG, "Suppressing notification from package by user request."); |
| 2369 | mUsageStats.registerBlocked(r); |
| 2370 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 2371 | return; |
| 2372 | } |
| 2373 | } |
| 2374 | |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 2375 | // tell the ranker service about the notification |
| 2376 | if (mRankerServices.isEnabled()) { |
| 2377 | mRankerServices.onNotificationEnqueued(r); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 2378 | // TODO delay the code below here for 100ms or until there is an answer |
| 2379 | } |
| 2380 | |
| 2381 | |
| 2382 | int index = indexOfNotificationLocked(n.getKey()); |
| 2383 | if (index < 0) { |
| 2384 | mNotificationList.add(r); |
| 2385 | mUsageStats.registerPostedByApp(r); |
| 2386 | } else { |
| 2387 | old = mNotificationList.get(index); |
| 2388 | mNotificationList.set(index, r); |
| 2389 | mUsageStats.registerUpdatedByApp(r, old); |
| 2390 | // Make sure we don't lose the foreground service state. |
| 2391 | notification.flags |= |
| 2392 | old.getNotification().flags & Notification.FLAG_FOREGROUND_SERVICE; |
| 2393 | r.isUpdate = true; |
| 2394 | } |
| 2395 | |
| 2396 | mNotificationsByKey.put(n.getKey(), r); |
| 2397 | |
| 2398 | // Ensure if this is a foreground service that the proper additional |
| 2399 | // flags are set. |
| 2400 | if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 2401 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 2402 | | Notification.FLAG_NO_CLEAR; |
| 2403 | } |
| 2404 | |
| 2405 | applyZenModeLocked(r); |
| 2406 | mRankingHelper.sort(mNotificationList); |
| 2407 | |
| 2408 | if (notification.getSmallIcon() != null) { |
| 2409 | StatusBarNotification oldSbn = (old != null) ? old.sbn : null; |
| 2410 | mListeners.notifyPostedLocked(n, oldSbn); |
| 2411 | } else { |
| 2412 | Slog.e(TAG, "Not posting notification without small icon: " + notification); |
| 2413 | if (old != null && !old.isCanceled) { |
| 2414 | mListeners.notifyRemovedLocked(n); |
| 2415 | } |
| 2416 | // ATTENTION: in a future release we will bail out here |
| 2417 | // so that we do not play sounds, show lights, etc. for invalid |
| 2418 | // notifications |
| 2419 | Slog.e(TAG, "WARNING: In a future release this will crash the app: " |
| 2420 | + n.getPackageName()); |
| 2421 | } |
| 2422 | |
| 2423 | buzzBeepBlinkLocked(r); |
| 2424 | } |
| 2425 | } |
| 2426 | } |
| 2427 | |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 2428 | /** |
| 2429 | * Ensures that grouped notification receive their special treatment. |
| 2430 | * |
| 2431 | * <p>Cancels group children if the new notification causes a group to lose |
| 2432 | * its summary.</p> |
| 2433 | * |
| 2434 | * <p>Updates mSummaryByGroupKey.</p> |
| 2435 | */ |
| 2436 | private void handleGroupedNotificationLocked(NotificationRecord r, NotificationRecord old, |
| 2437 | int callingUid, int callingPid) { |
| 2438 | StatusBarNotification sbn = r.sbn; |
| 2439 | Notification n = sbn.getNotification(); |
| 2440 | String group = sbn.getGroupKey(); |
| 2441 | boolean isSummary = n.isGroupSummary(); |
| 2442 | |
| 2443 | Notification oldN = old != null ? old.sbn.getNotification() : null; |
| 2444 | String oldGroup = old != null ? old.sbn.getGroupKey() : null; |
| 2445 | boolean oldIsSummary = old != null && oldN.isGroupSummary(); |
| 2446 | |
| 2447 | if (oldIsSummary) { |
| 2448 | NotificationRecord removedSummary = mSummaryByGroupKey.remove(oldGroup); |
| 2449 | if (removedSummary != old) { |
| 2450 | String removedKey = |
| 2451 | removedSummary != null ? removedSummary.getKey() : "<null>"; |
| 2452 | Slog.w(TAG, "Removed summary didn't match old notification: old=" + old.getKey() + |
| 2453 | ", removed=" + removedKey); |
| 2454 | } |
| 2455 | } |
| 2456 | if (isSummary) { |
| 2457 | mSummaryByGroupKey.put(group, r); |
| 2458 | } |
| 2459 | |
| 2460 | // Clear out group children of the old notification if the update |
| 2461 | // causes the group summary to go away. This happens when the old |
| 2462 | // notification was a summary and the new one isn't, or when the old |
| 2463 | // notification was a summary and its group key changed. |
| 2464 | if (oldIsSummary && (!isSummary || !oldGroup.equals(group))) { |
| 2465 | cancelGroupChildrenLocked(old, callingUid, callingPid, null, |
| 2466 | REASON_GROUP_SUMMARY_CANCELED); |
| 2467 | } |
| 2468 | } |
| 2469 | |
| 2470 | /** |
| 2471 | * Performs group notification optimizations if SysUI is the only active |
| 2472 | * notification listener and returns whether the given notification should |
| 2473 | * be ignored. |
| 2474 | * |
| 2475 | * <p>Returns true if the given notification is a child of a group with a |
| 2476 | * summary, which means that SysUI will never show it, and hence the new |
Christoph Studer | 4a9849b | 2015-01-06 18:55:08 +0100 | [diff] [blame] | 2477 | * notification can be safely ignored. Also cancels any previous instance |
| 2478 | * of the ignored notification.</p> |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 2479 | * |
| 2480 | * <p>For summaries, cancels all children of that group, as SysUI will |
| 2481 | * never show them anymore.</p> |
| 2482 | * |
| 2483 | * @return true if the given notification can be ignored as an optimization |
| 2484 | */ |
| 2485 | private boolean removeUnusedGroupedNotificationLocked(NotificationRecord r, |
Christoph Studer | 4a9849b | 2015-01-06 18:55:08 +0100 | [diff] [blame] | 2486 | NotificationRecord old, int callingUid, int callingPid) { |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 2487 | if (!ENABLE_CHILD_NOTIFICATIONS) { |
| 2488 | // No optimizations are possible if listeners want groups. |
| 2489 | if (mListeners.notificationGroupsDesired()) { |
| 2490 | return false; |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 2491 | } |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 2492 | |
| 2493 | StatusBarNotification sbn = r.sbn; |
| 2494 | String group = sbn.getGroupKey(); |
| 2495 | boolean isSummary = sbn.getNotification().isGroupSummary(); |
| 2496 | boolean isChild = sbn.getNotification().isGroupChild(); |
| 2497 | |
| 2498 | NotificationRecord summary = mSummaryByGroupKey.get(group); |
| 2499 | if (isChild && summary != null) { |
| 2500 | // Child with an active summary -> ignore |
Christoph Studer | 4a9849b | 2015-01-06 18:55:08 +0100 | [diff] [blame] | 2501 | if (DBG) { |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 2502 | Slog.d(TAG, "Ignoring group child " + sbn.getKey() + " due to existing summary " |
| 2503 | + summary.getKey()); |
Christoph Studer | 4a9849b | 2015-01-06 18:55:08 +0100 | [diff] [blame] | 2504 | } |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 2505 | // Make sure we don't leave an old version of the notification around. |
| 2506 | if (old != null) { |
| 2507 | if (DBG) { |
| 2508 | Slog.d(TAG, "Canceling old version of ignored group child " + sbn.getKey()); |
| 2509 | } |
| 2510 | cancelNotificationLocked(old, false, REASON_GROUP_OPTIMIZATION); |
| 2511 | } |
| 2512 | return true; |
| 2513 | } else if (isSummary) { |
| 2514 | // Summary -> cancel children |
| 2515 | cancelGroupChildrenLocked(r, callingUid, callingPid, null, |
| 2516 | REASON_GROUP_OPTIMIZATION); |
Christoph Studer | 4a9849b | 2015-01-06 18:55:08 +0100 | [diff] [blame] | 2517 | } |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 2518 | } |
| 2519 | return false; |
| 2520 | } |
| 2521 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 2522 | @VisibleForTesting |
| 2523 | void buzzBeepBlinkLocked(NotificationRecord record) { |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 2524 | boolean buzz = false; |
| 2525 | boolean beep = false; |
| 2526 | boolean blink = false; |
| 2527 | |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2528 | final Notification notification = record.sbn.getNotification(); |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 2529 | final String key = record.getKey(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2530 | |
| 2531 | // Should this notification make noise, vibe, or use the LED? |
Julia Reynolds | f0f629f | 2016-02-25 09:34:04 -0500 | [diff] [blame] | 2532 | final boolean aboveThreshold = record.getImportance() >= IMPORTANCE_DEFAULT; |
Chris Wren | ce00a23 | 2014-11-21 16:25:19 -0500 | [diff] [blame] | 2533 | final boolean canInterrupt = aboveThreshold && !record.isIntercepted(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2534 | if (DBG || record.isIntercepted()) |
| 2535 | Slog.v(TAG, |
| 2536 | "pkg=" + record.sbn.getPackageName() + " canInterrupt=" + canInterrupt + |
| 2537 | " intercept=" + record.isIntercepted() |
| 2538 | ); |
| 2539 | |
| 2540 | final int currentUser; |
| 2541 | final long token = Binder.clearCallingIdentity(); |
| 2542 | try { |
| 2543 | currentUser = ActivityManager.getCurrentUser(); |
| 2544 | } finally { |
| 2545 | Binder.restoreCallingIdentity(token); |
| 2546 | } |
| 2547 | |
| 2548 | // If we're not supposed to beep, vibrate, etc. then don't. |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 2549 | final String disableEffects = disableNotificationEffects(record); |
| 2550 | if (disableEffects != null) { |
| 2551 | ZenLog.traceDisableEffects(record, disableEffects); |
| 2552 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 2553 | |
| 2554 | // Remember if this notification already owns the notification channels. |
| 2555 | boolean wasBeep = key != null && key.equals(mSoundNotificationKey); |
| 2556 | boolean wasBuzz = key != null && key.equals(mVibrateNotificationKey); |
| 2557 | |
| 2558 | // These are set inside the conditional if the notification is allowed to make noise. |
| 2559 | boolean hasValidVibrate = false; |
| 2560 | boolean hasValidSound = false; |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 2561 | if (disableEffects == null |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2562 | && (record.getUserId() == UserHandle.USER_ALL || |
| 2563 | record.getUserId() == currentUser || |
| 2564 | mUserProfiles.isCurrentProfile(record.getUserId())) |
| 2565 | && canInterrupt |
| 2566 | && mSystemReady |
| 2567 | && mAudioManager != null) { |
| 2568 | if (DBG) Slog.v(TAG, "Interrupting!"); |
| 2569 | |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2570 | // should we use the default notification sound? (indicated either by |
| 2571 | // DEFAULT_SOUND or because notification.sound is pointing at |
| 2572 | // Settings.System.NOTIFICATION_SOUND) |
| 2573 | final boolean useDefaultSound = |
| 2574 | (notification.defaults & Notification.DEFAULT_SOUND) != 0 || |
| 2575 | Settings.System.DEFAULT_NOTIFICATION_URI |
| 2576 | .equals(notification.sound); |
| 2577 | |
| 2578 | Uri soundUri = null; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2579 | if (useDefaultSound) { |
| 2580 | soundUri = Settings.System.DEFAULT_NOTIFICATION_URI; |
| 2581 | |
| 2582 | // check to see if the default notification sound is silent |
| 2583 | ContentResolver resolver = getContext().getContentResolver(); |
| 2584 | hasValidSound = Settings.System.getString(resolver, |
| 2585 | Settings.System.NOTIFICATION_SOUND) != null; |
| 2586 | } else if (notification.sound != null) { |
| 2587 | soundUri = notification.sound; |
| 2588 | hasValidSound = (soundUri != null); |
| 2589 | } |
| 2590 | |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2591 | // Does the notification want to specify its own vibration? |
| 2592 | final boolean hasCustomVibrate = notification.vibrate != null; |
| 2593 | |
| 2594 | // new in 4.2: if there was supposed to be a sound and we're in vibrate |
| 2595 | // mode, and no other vibration is specified, we fall back to vibration |
| 2596 | final boolean convertSoundToVibration = |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 2597 | !hasCustomVibrate |
| 2598 | && hasValidSound |
| 2599 | && (mAudioManager.getRingerModeInternal() == AudioManager.RINGER_MODE_VIBRATE); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2600 | |
| 2601 | // The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback. |
| 2602 | final boolean useDefaultVibrate = |
| 2603 | (notification.defaults & Notification.DEFAULT_VIBRATE) != 0; |
| 2604 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 2605 | hasValidVibrate = useDefaultVibrate || convertSoundToVibration || |
| 2606 | hasCustomVibrate; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2607 | |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 2608 | // We can alert, and we're allowed to alert, but if the developer asked us to only do |
| 2609 | // it once, and we already have, then don't. |
| 2610 | if (!(record.isUpdate |
| 2611 | && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0)) { |
| 2612 | |
| 2613 | sendAccessibilityEvent(notification, record.sbn.getPackageName()); |
| 2614 | |
| 2615 | if (hasValidSound) { |
| 2616 | boolean looping = |
| 2617 | (notification.flags & Notification.FLAG_INSISTENT) != 0; |
| 2618 | AudioAttributes audioAttributes = audioAttributesForNotification(notification); |
| 2619 | mSoundNotificationKey = key; |
| 2620 | // do not play notifications if stream volume is 0 (typically because |
| 2621 | // ringer mode is silent) or if there is a user of exclusive audio focus |
| 2622 | if ((mAudioManager.getStreamVolume( |
| 2623 | AudioAttributes.toLegacyStreamType(audioAttributes)) != 0) |
| 2624 | && !mAudioManager.isAudioFocusExclusive()) { |
| 2625 | final long identity = Binder.clearCallingIdentity(); |
| 2626 | try { |
| 2627 | final IRingtonePlayer player = |
| 2628 | mAudioManager.getRingtonePlayer(); |
| 2629 | if (player != null) { |
| 2630 | if (DBG) Slog.v(TAG, "Playing sound " + soundUri |
| 2631 | + " with attributes " + audioAttributes); |
| 2632 | player.playAsync(soundUri, record.sbn.getUser(), looping, |
| 2633 | audioAttributes); |
| 2634 | beep = true; |
| 2635 | } |
| 2636 | } catch (RemoteException e) { |
| 2637 | } finally { |
| 2638 | Binder.restoreCallingIdentity(identity); |
| 2639 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2640 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | if (hasValidVibrate && !(mAudioManager.getRingerModeInternal() |
| 2644 | == AudioManager.RINGER_MODE_SILENT)) { |
| 2645 | mVibrateNotificationKey = key; |
| 2646 | |
| 2647 | if (useDefaultVibrate || convertSoundToVibration) { |
| 2648 | // Escalate privileges so we can use the vibrator even if the |
| 2649 | // notifying app does not have the VIBRATE permission. |
| 2650 | long identity = Binder.clearCallingIdentity(); |
| 2651 | try { |
| 2652 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 2653 | useDefaultVibrate ? mDefaultVibrationPattern |
| 2654 | : mFallbackVibrationPattern, |
| 2655 | ((notification.flags & Notification.FLAG_INSISTENT) != 0) |
| 2656 | ? 0: -1, audioAttributesForNotification(notification)); |
| 2657 | buzz = true; |
| 2658 | } finally { |
| 2659 | Binder.restoreCallingIdentity(identity); |
| 2660 | } |
| 2661 | } else if (notification.vibrate.length > 1) { |
| 2662 | // If you want your own vibration pattern, you need the VIBRATE |
| 2663 | // permission |
| 2664 | mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), |
| 2665 | notification.vibrate, |
| 2666 | ((notification.flags & Notification.FLAG_INSISTENT) != 0) |
| 2667 | ? 0: -1, audioAttributesForNotification(notification)); |
| 2668 | buzz = true; |
| 2669 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2670 | } |
| 2671 | } |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 2672 | |
| 2673 | } |
| 2674 | // If a notification is updated to remove the actively playing sound or vibrate, |
| 2675 | // cancel that feedback now |
| 2676 | if (wasBeep && !hasValidSound) { |
| 2677 | clearSoundLocked(); |
| 2678 | } |
| 2679 | if (wasBuzz && !hasValidVibrate) { |
| 2680 | clearVibrateLocked(); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | // light |
| 2684 | // release the light |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 2685 | boolean wasShowLights = mLights.remove(key); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 2686 | if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0 && aboveThreshold |
| 2687 | && ((record.getSuppressedVisualEffects() |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 2688 | & NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF) == 0)) { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 2689 | mLights.add(key); |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2690 | updateLightsLocked(); |
Chris Wren | 5116a82 | 2014-06-04 15:59:50 -0400 | [diff] [blame] | 2691 | if (mUseAttentionLight) { |
| 2692 | mAttentionLight.pulse(); |
| 2693 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 2694 | blink = true; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2695 | } else if (wasShowLights) { |
| 2696 | updateLightsLocked(); |
| 2697 | } |
Chris Wren | 82ba59d | 2015-06-05 11:23:44 -0400 | [diff] [blame] | 2698 | if (buzz || beep || blink) { |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 2699 | if (((record.getSuppressedVisualEffects() |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 2700 | & NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF) != 0)) { |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 2701 | if (DBG) Slog.v(TAG, "Suppressed SystemUI from triggering screen on"); |
| 2702 | } else { |
Chris Wren | 93bb8b8 | 2016-03-29 14:35:05 -0400 | [diff] [blame] | 2703 | EventLogTags.writeNotificationAlert(key, |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 2704 | buzz ? 1 : 0, beep ? 1 : 0, blink ? 1 : 0); |
| 2705 | mHandler.post(mBuzzBeepBlinked); |
| 2706 | } |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 2707 | } |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2708 | } |
| 2709 | |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 2710 | private static AudioAttributes audioAttributesForNotification(Notification n) { |
Marco Nelissen | 1c06630 | 2014-11-18 10:48:12 -0800 | [diff] [blame] | 2711 | if (n.audioAttributes != null |
| 2712 | && !Notification.AUDIO_ATTRIBUTES_DEFAULT.equals(n.audioAttributes)) { |
| 2713 | // the audio attributes are set and different from the default, use them |
John Spurlock | bfa5dc4 | 2014-07-28 23:30:45 -0400 | [diff] [blame] | 2714 | return n.audioAttributes; |
Jean-Michel Trivi | ceb79bc | 2014-09-05 11:09:14 -0700 | [diff] [blame] | 2715 | } else if (n.audioStreamType >= 0 && n.audioStreamType < AudioSystem.getNumStreamTypes()) { |
| 2716 | // the stream type is valid, use it |
| 2717 | return new AudioAttributes.Builder() |
| 2718 | .setInternalLegacyStreamType(n.audioStreamType) |
| 2719 | .build(); |
| 2720 | } else if (n.audioStreamType == AudioSystem.STREAM_DEFAULT) { |
| 2721 | return Notification.AUDIO_ATTRIBUTES_DEFAULT; |
| 2722 | } else { |
| 2723 | Log.w(TAG, String.format("Invalid stream type: %d", n.audioStreamType)); |
| 2724 | return Notification.AUDIO_ATTRIBUTES_DEFAULT; |
John Spurlock | bfa5dc4 | 2014-07-28 23:30:45 -0400 | [diff] [blame] | 2725 | } |
John Spurlock | 7b41467 | 2014-07-18 13:02:39 -0400 | [diff] [blame] | 2726 | } |
| 2727 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2728 | void showNextToastLocked() { |
| 2729 | ToastRecord record = mToastQueue.get(0); |
| 2730 | while (record != null) { |
| 2731 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| 2732 | try { |
| 2733 | record.callback.show(); |
| 2734 | scheduleTimeoutLocked(record); |
| 2735 | return; |
| 2736 | } catch (RemoteException e) { |
| 2737 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| 2738 | + " in package " + record.pkg); |
| 2739 | // remove it from the list and let the process die |
| 2740 | int index = mToastQueue.indexOf(record); |
| 2741 | if (index >= 0) { |
| 2742 | mToastQueue.remove(index); |
| 2743 | } |
| 2744 | keepProcessAliveLocked(record.pid); |
| 2745 | if (mToastQueue.size() > 0) { |
| 2746 | record = mToastQueue.get(0); |
| 2747 | } else { |
| 2748 | record = null; |
| 2749 | } |
| 2750 | } |
| 2751 | } |
| 2752 | } |
| 2753 | |
| 2754 | void cancelToastLocked(int index) { |
| 2755 | ToastRecord record = mToastQueue.get(index); |
| 2756 | try { |
| 2757 | record.callback.hide(); |
| 2758 | } catch (RemoteException e) { |
| 2759 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| 2760 | + " in package " + record.pkg); |
| 2761 | // don't worry about this, we're about to remove it from |
| 2762 | // the list anyway |
| 2763 | } |
| 2764 | mToastQueue.remove(index); |
| 2765 | keepProcessAliveLocked(record.pid); |
| 2766 | if (mToastQueue.size() > 0) { |
| 2767 | // Show the next one. If the callback fails, this will remove |
| 2768 | // it from the list, so don't assume that the list hasn't changed |
| 2769 | // after this point. |
| 2770 | showNextToastLocked(); |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | private void scheduleTimeoutLocked(ToastRecord r) |
| 2775 | { |
| 2776 | mHandler.removeCallbacksAndMessages(r); |
| 2777 | Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r); |
| 2778 | long delay = r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY; |
| 2779 | mHandler.sendMessageDelayed(m, delay); |
| 2780 | } |
| 2781 | |
| 2782 | private void handleTimeout(ToastRecord record) |
| 2783 | { |
| 2784 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| 2785 | synchronized (mToastQueue) { |
| 2786 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 2787 | if (index >= 0) { |
| 2788 | cancelToastLocked(index); |
| 2789 | } |
| 2790 | } |
| 2791 | } |
| 2792 | |
| 2793 | // lock on mToastQueue |
| 2794 | int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 2795 | { |
| 2796 | IBinder cbak = callback.asBinder(); |
| 2797 | ArrayList<ToastRecord> list = mToastQueue; |
| 2798 | int len = list.size(); |
| 2799 | for (int i=0; i<len; i++) { |
| 2800 | ToastRecord r = list.get(i); |
| 2801 | if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) { |
| 2802 | return i; |
| 2803 | } |
| 2804 | } |
| 2805 | return -1; |
| 2806 | } |
| 2807 | |
| 2808 | // lock on mToastQueue |
| 2809 | void keepProcessAliveLocked(int pid) |
| 2810 | { |
| 2811 | int toastCount = 0; // toasts from this pid |
| 2812 | ArrayList<ToastRecord> list = mToastQueue; |
| 2813 | int N = list.size(); |
| 2814 | for (int i=0; i<N; i++) { |
| 2815 | ToastRecord r = list.get(i); |
| 2816 | if (r.pid == pid) { |
| 2817 | toastCount++; |
| 2818 | } |
| 2819 | } |
| 2820 | try { |
| 2821 | mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0); |
| 2822 | } catch (RemoteException e) { |
| 2823 | // Shouldn't happen. |
| 2824 | } |
| 2825 | } |
| 2826 | |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2827 | private void handleRankingReconsideration(Message message) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 2828 | if (!(message.obj instanceof RankingReconsideration)) return; |
| 2829 | RankingReconsideration recon = (RankingReconsideration) message.obj; |
| 2830 | recon.run(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 2831 | boolean changed; |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 2832 | synchronized (mNotificationList) { |
| 2833 | final NotificationRecord record = mNotificationsByKey.get(recon.getKey()); |
| 2834 | if (record == null) { |
| 2835 | return; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2836 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 2837 | int indexBefore = findNotificationRecordIndexLocked(record); |
| 2838 | boolean interceptBefore = record.isIntercepted(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 2839 | int visibilityBefore = record.getPackageVisibilityOverride(); |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 2840 | recon.applyChangesLocked(record); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 2841 | applyZenModeLocked(record); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 2842 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 2843 | int indexAfter = findNotificationRecordIndexLocked(record); |
| 2844 | boolean interceptAfter = record.isIntercepted(); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 2845 | int visibilityAfter = record.getPackageVisibilityOverride(); |
| 2846 | changed = indexBefore != indexAfter || interceptBefore != interceptAfter |
| 2847 | || visibilityBefore != visibilityAfter; |
Chris Wren | a344656 | 2014-06-03 18:11:47 -0400 | [diff] [blame] | 2848 | if (interceptBefore && !interceptAfter) { |
| 2849 | buzzBeepBlinkLocked(record); |
| 2850 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2851 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 2852 | if (changed) { |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 2853 | scheduleSendRankingUpdate(); |
| 2854 | } |
| 2855 | } |
| 2856 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2857 | private void handleRankingSort() { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 2858 | synchronized (mNotificationList) { |
| 2859 | final int N = mNotificationList.size(); |
| 2860 | ArrayList<String> orderBefore = new ArrayList<String>(N); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 2861 | int[] visibilities = new int[N]; |
Julia Reynolds | 6976669 | 2016-02-01 15:35:08 -0500 | [diff] [blame] | 2862 | int [] importances = new int[N]; |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 2863 | for (int i = 0; i < N; i++) { |
| 2864 | final NotificationRecord r = mNotificationList.get(i); |
| 2865 | orderBefore.add(r.getKey()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 2866 | visibilities[i] = r.getPackageVisibilityOverride(); |
Julia Reynolds | 6976669 | 2016-02-01 15:35:08 -0500 | [diff] [blame] | 2867 | importances[i] = r.getImportance(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 2868 | mRankingHelper.extractSignals(r); |
| 2869 | } |
Chris Wren | 19a02b0 | 2015-12-22 10:34:22 -0500 | [diff] [blame] | 2870 | mRankingHelper.sort(mNotificationList); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 2871 | for (int i = 0; i < N; i++) { |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 2872 | final NotificationRecord r = mNotificationList.get(i); |
| 2873 | if (!orderBefore.get(i).equals(r.getKey()) |
Julia Reynolds | 6976669 | 2016-02-01 15:35:08 -0500 | [diff] [blame] | 2874 | || visibilities[i] != r.getPackageVisibilityOverride() |
| 2875 | || importances[i] != r.getImportance()) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 2876 | scheduleSendRankingUpdate(); |
| 2877 | return; |
| 2878 | } |
| 2879 | } |
| 2880 | } |
| 2881 | } |
| 2882 | |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 2883 | // let zen mode evaluate this record |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 2884 | private void applyZenModeLocked(NotificationRecord record) { |
Christoph Studer | d5092bc | 2014-07-03 17:47:58 +0200 | [diff] [blame] | 2885 | record.setIntercepted(mZenModeHelper.shouldIntercept(record)); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 2886 | if (record.isIntercepted()) { |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 2887 | int suppressed = (mZenModeHelper.shouldSuppressWhenScreenOff() |
| 2888 | ? SUPPRESSED_EFFECT_SCREEN_OFF : 0) |
| 2889 | | (mZenModeHelper.shouldSuppressWhenScreenOn() |
| 2890 | ? SUPPRESSED_EFFECT_SCREEN_ON : 0); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 2891 | record.setSuppressedVisualEffects(suppressed); |
| 2892 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 2893 | } |
| 2894 | |
Chris Wren | 470c1ac | 2014-05-21 15:28:10 -0400 | [diff] [blame] | 2895 | // lock on mNotificationList |
| 2896 | private int findNotificationRecordIndexLocked(NotificationRecord target) { |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 2897 | return mRankingHelper.indexOf(mNotificationList, target); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2898 | } |
| 2899 | |
| 2900 | private void scheduleSendRankingUpdate() { |
| 2901 | mHandler.removeMessages(MESSAGE_SEND_RANKING_UPDATE); |
| 2902 | Message m = Message.obtain(mHandler, MESSAGE_SEND_RANKING_UPDATE); |
| 2903 | mHandler.sendMessage(m); |
| 2904 | } |
| 2905 | |
| 2906 | private void handleSendRankingUpdate() { |
| 2907 | synchronized (mNotificationList) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 2908 | mListeners.notifyRankingUpdateLocked(); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2909 | } |
| 2910 | } |
| 2911 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2912 | private void scheduleListenerHintsChanged(int state) { |
| 2913 | mHandler.removeMessages(MESSAGE_LISTENER_HINTS_CHANGED); |
| 2914 | mHandler.obtainMessage(MESSAGE_LISTENER_HINTS_CHANGED, state, 0).sendToTarget(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2915 | } |
| 2916 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2917 | private void scheduleInterruptionFilterChanged(int listenerInterruptionFilter) { |
| 2918 | mHandler.removeMessages(MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED); |
| 2919 | mHandler.obtainMessage( |
| 2920 | MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED, |
| 2921 | listenerInterruptionFilter, |
| 2922 | 0).sendToTarget(); |
| 2923 | } |
| 2924 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2925 | private void handleListenerHintsChanged(int hints) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2926 | synchronized (mNotificationList) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2927 | mListeners.notifyListenerHintsChangedLocked(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2928 | } |
| 2929 | } |
| 2930 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2931 | private void handleListenerInterruptionFilterChanged(int interruptionFilter) { |
| 2932 | synchronized (mNotificationList) { |
| 2933 | mListeners.notifyInterruptionFilterChanged(interruptionFilter); |
| 2934 | } |
| 2935 | } |
| 2936 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2937 | private final class WorkerHandler extends Handler |
| 2938 | { |
| 2939 | @Override |
| 2940 | public void handleMessage(Message msg) |
| 2941 | { |
| 2942 | switch (msg.what) |
| 2943 | { |
| 2944 | case MESSAGE_TIMEOUT: |
| 2945 | handleTimeout((ToastRecord)msg.obj); |
| 2946 | break; |
John Spurlock | 056c519 | 2014-04-20 21:52:01 -0400 | [diff] [blame] | 2947 | case MESSAGE_SAVE_POLICY_FILE: |
| 2948 | handleSavePolicyFile(); |
| 2949 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2950 | case MESSAGE_SEND_RANKING_UPDATE: |
| 2951 | handleSendRankingUpdate(); |
| 2952 | break; |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 2953 | case MESSAGE_LISTENER_HINTS_CHANGED: |
| 2954 | handleListenerHintsChanged(msg.arg1); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 2955 | break; |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 2956 | case MESSAGE_LISTENER_NOTIFICATION_FILTER_CHANGED: |
| 2957 | handleListenerInterruptionFilterChanged(msg.arg1); |
| 2958 | break; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2959 | } |
| 2960 | } |
| 2961 | |
| 2962 | } |
| 2963 | |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2964 | private final class RankingHandlerWorker extends Handler implements RankingHandler |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2965 | { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2966 | public RankingHandlerWorker(Looper looper) { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 2967 | super(looper); |
| 2968 | } |
| 2969 | |
| 2970 | @Override |
| 2971 | public void handleMessage(Message msg) { |
| 2972 | switch (msg.what) { |
| 2973 | case MESSAGE_RECONSIDER_RANKING: |
| 2974 | handleRankingReconsideration(msg); |
| 2975 | break; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2976 | case MESSAGE_RANKING_SORT: |
| 2977 | handleRankingSort(); |
Chris Wren | 54bbef4 | 2014-07-09 18:37:56 -0400 | [diff] [blame] | 2978 | break; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2979 | } |
| 2980 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 2981 | |
| 2982 | public void requestSort() { |
| 2983 | removeMessages(MESSAGE_RANKING_SORT); |
| 2984 | sendEmptyMessage(MESSAGE_RANKING_SORT); |
| 2985 | } |
| 2986 | |
| 2987 | public void requestReconsideration(RankingReconsideration recon) { |
| 2988 | Message m = Message.obtain(this, |
| 2989 | NotificationManagerService.MESSAGE_RECONSIDER_RANKING, recon); |
| 2990 | long delay = recon.getDelay(TimeUnit.MILLISECONDS); |
| 2991 | sendMessageDelayed(m, delay); |
| 2992 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2993 | } |
| 2994 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 2995 | // Notifications |
| 2996 | // ============================================================================ |
| 2997 | static int clamp(int x, int low, int high) { |
| 2998 | return (x < low) ? low : ((x > high) ? high : x); |
| 2999 | } |
| 3000 | |
| 3001 | void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
| 3002 | AccessibilityManager manager = AccessibilityManager.getInstance(getContext()); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3003 | if (!manager.isEnabled()) { |
| 3004 | return; |
| 3005 | } |
| 3006 | |
| 3007 | AccessibilityEvent event = |
| 3008 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 3009 | event.setPackageName(packageName); |
| 3010 | event.setClassName(Notification.class.getName()); |
| 3011 | event.setParcelableData(notification); |
| 3012 | CharSequence tickerText = notification.tickerText; |
| 3013 | if (!TextUtils.isEmpty(tickerText)) { |
| 3014 | event.getText().add(tickerText); |
| 3015 | } |
| 3016 | |
| 3017 | manager.sendAccessibilityEvent(event); |
| 3018 | } |
| 3019 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 3020 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 3021 | // tell the app |
| 3022 | if (sendDelete) { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 3023 | if (r.getNotification().deleteIntent != null) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 3024 | try { |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 3025 | r.getNotification().deleteIntent.send(); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 3026 | } catch (PendingIntent.CanceledException ex) { |
| 3027 | // do nothing - there's no relevant way to recover, and |
| 3028 | // no reason to let this propagate |
Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 3029 | Slog.w(TAG, "canceled PendingIntent for " + r.sbn.getPackageName(), ex); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 3030 | } |
| 3031 | } |
| 3032 | } |
| 3033 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3034 | // status bar |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 3035 | if (r.getNotification().getSmallIcon() != null) { |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 3036 | r.isCanceled = true; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 3037 | mListeners.notifyRemovedLocked(r.sbn); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3038 | } |
| 3039 | |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3040 | final String canceledKey = r.getKey(); |
| 3041 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3042 | // sound |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3043 | if (canceledKey.equals(mSoundNotificationKey)) { |
| 3044 | mSoundNotificationKey = null; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 3045 | final long identity = Binder.clearCallingIdentity(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3046 | try { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3047 | final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 3048 | if (player != null) { |
| 3049 | player.stopAsync(); |
| 3050 | } |
| 3051 | } catch (RemoteException e) { |
| 3052 | } finally { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3053 | Binder.restoreCallingIdentity(identity); |
| 3054 | } |
| 3055 | } |
| 3056 | |
| 3057 | // vibrate |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3058 | if (canceledKey.equals(mVibrateNotificationKey)) { |
| 3059 | mVibrateNotificationKey = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3060 | long identity = Binder.clearCallingIdentity(); |
| 3061 | try { |
| 3062 | mVibrator.cancel(); |
| 3063 | } |
| 3064 | finally { |
| 3065 | Binder.restoreCallingIdentity(identity); |
| 3066 | } |
| 3067 | } |
| 3068 | |
| 3069 | // light |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3070 | mLights.remove(canceledKey); |
Daniel Sandler | 23d7c70 | 2013-03-07 16:32:06 -0500 | [diff] [blame] | 3071 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 3072 | // Record usage stats |
| 3073 | switch (reason) { |
| 3074 | case REASON_DELEGATE_CANCEL: |
| 3075 | case REASON_DELEGATE_CANCEL_ALL: |
| 3076 | case REASON_LISTENER_CANCEL: |
| 3077 | case REASON_LISTENER_CANCEL_ALL: |
| 3078 | mUsageStats.registerDismissedByUser(r); |
| 3079 | break; |
Chris Wren | 9fa689f | 2015-11-20 16:44:53 -0500 | [diff] [blame] | 3080 | case REASON_APP_CANCEL: |
| 3081 | case REASON_APP_CANCEL_ALL: |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 3082 | mUsageStats.registerRemovedByApp(r); |
| 3083 | break; |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 3084 | } |
| 3085 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 3086 | mNotificationsByKey.remove(r.sbn.getKey()); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3087 | String groupKey = r.getGroupKey(); |
| 3088 | NotificationRecord groupSummary = mSummaryByGroupKey.get(groupKey); |
| 3089 | if (groupSummary != null && groupSummary.getKey().equals(r.getKey())) { |
| 3090 | mSummaryByGroupKey.remove(groupKey); |
| 3091 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 3092 | |
Daniel Sandler | 23d7c70 | 2013-03-07 16:32:06 -0500 | [diff] [blame] | 3093 | // Save it for users of getHistoricalNotifications() |
| 3094 | mArchive.record(r.sbn); |
Christoph Studer | 81e5b5f | 2014-10-22 17:19:56 +0200 | [diff] [blame] | 3095 | |
Chris Wren | 6650e57 | 2015-05-15 17:19:25 -0400 | [diff] [blame] | 3096 | final long now = System.currentTimeMillis(); |
Chris Wren | e6ddb8a | 2015-05-27 15:21:00 -0400 | [diff] [blame] | 3097 | EventLogTags.writeNotificationCanceled(canceledKey, reason, |
| 3098 | r.getLifespanMs(now), r.getFreshnessMs(now), r.getExposureMs(now)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | /** |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 3102 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3103 | * and none of the {@code mustNotHaveFlags}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3104 | */ |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 3105 | void cancelNotification(final int callingUid, final int callingPid, |
| 3106 | final String pkg, final String tag, final int id, |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 3107 | final int mustHaveFlags, final int mustNotHaveFlags, final boolean sendDelete, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3108 | final int userId, final int reason, final ManagedServiceInfo listener) { |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 3109 | // In enqueueNotificationInternal notifications are added by scheduling the |
| 3110 | // work on the worker handler. Hence, we also schedule the cancel on this |
| 3111 | // handler to avoid a scenario where an add notification call followed by a |
| 3112 | // remove notification call ends up in not removing the notification. |
| 3113 | mHandler.post(new Runnable() { |
| 3114 | @Override |
| 3115 | public void run() { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3116 | String listenerName = listener == null ? null : listener.component.toShortString(); |
Chris Wren | bddb5bc | 2015-03-04 08:47:46 -0800 | [diff] [blame] | 3117 | if (DBG) EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, id, tag, |
| 3118 | userId, mustHaveFlags, mustNotHaveFlags, reason, listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3119 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 3120 | synchronized (mNotificationList) { |
| 3121 | int index = indexOfNotificationLocked(pkg, tag, id, userId); |
| 3122 | if (index >= 0) { |
| 3123 | NotificationRecord r = mNotificationList.get(index); |
Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 3124 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 3125 | // Ideally we'd do this in the caller of this method. However, that would |
| 3126 | // require the caller to also find the notification. |
| 3127 | if (reason == REASON_DELEGATE_CLICK) { |
| 3128 | mUsageStats.registerClickedByUser(r); |
| 3129 | } |
| 3130 | |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 3131 | if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) { |
| 3132 | return; |
| 3133 | } |
| 3134 | if ((r.getNotification().flags & mustNotHaveFlags) != 0) { |
| 3135 | return; |
| 3136 | } |
| 3137 | |
| 3138 | mNotificationList.remove(index); |
| 3139 | |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 3140 | cancelNotificationLocked(r, sendDelete, reason); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3141 | cancelGroupChildrenLocked(r, callingUid, callingPid, listenerName, |
| 3142 | REASON_GROUP_SUMMARY_CANCELED); |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 3143 | updateLightsLocked(); |
| 3144 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3145 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3146 | } |
Svetoslav Ganov | 835835e | 2013-08-04 20:17:52 -0700 | [diff] [blame] | 3147 | }); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | /** |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 3151 | * Determine whether the userId applies to the notification in question, either because |
| 3152 | * they match exactly, or one of them is USER_ALL (which is treated as a wildcard). |
| 3153 | */ |
| 3154 | private boolean notificationMatchesUserId(NotificationRecord r, int userId) { |
| 3155 | return |
| 3156 | // looking for USER_ALL notifications? match everything |
| 3157 | userId == UserHandle.USER_ALL |
| 3158 | // a notification sent to USER_ALL matches any query |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 3159 | || r.getUserId() == UserHandle.USER_ALL |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 3160 | // an exact user match |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 3161 | || r.getUserId() == userId; |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 3162 | } |
| 3163 | |
| 3164 | /** |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3165 | * Determine whether the userId applies to the notification in question, either because |
| 3166 | * 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] | 3167 | * because it matches one of the users profiles. |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3168 | */ |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 3169 | private boolean notificationMatchesCurrentProfiles(NotificationRecord r, int userId) { |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3170 | return notificationMatchesUserId(r, userId) |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 3171 | || mUserProfiles.isCurrentProfile(r.getUserId()); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | /** |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 3175 | * 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] | 3176 | * {@code mustHaveFlags}. |
| 3177 | */ |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 3178 | boolean cancelAllNotificationsInt(int callingUid, int callingPid, String pkg, int mustHaveFlags, |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 3179 | int mustNotHaveFlags, boolean doit, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3180 | ManagedServiceInfo listener) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3181 | String listenerName = listener == null ? null : listener.component.toShortString(); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 3182 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
| 3183 | pkg, userId, mustHaveFlags, mustNotHaveFlags, reason, |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3184 | listenerName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3185 | |
| 3186 | synchronized (mNotificationList) { |
| 3187 | final int N = mNotificationList.size(); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3188 | ArrayList<NotificationRecord> canceledNotifications = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3189 | for (int i = N-1; i >= 0; --i) { |
| 3190 | NotificationRecord r = mNotificationList.get(i); |
Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 3191 | if (!notificationMatchesUserId(r, userId)) { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3192 | continue; |
| 3193 | } |
Amith Yamasani | 5ec00e9 | 2012-11-07 16:58:30 -0800 | [diff] [blame] | 3194 | // Don't remove notifications to all, if there's no package name specified |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 3195 | if (r.getUserId() == UserHandle.USER_ALL && pkg == null) { |
Amith Yamasani | 5ec00e9 | 2012-11-07 16:58:30 -0800 | [diff] [blame] | 3196 | continue; |
| 3197 | } |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 3198 | if ((r.getFlags() & mustHaveFlags) != mustHaveFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3199 | continue; |
| 3200 | } |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 3201 | if ((r.getFlags() & mustNotHaveFlags) != 0) { |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 3202 | continue; |
| 3203 | } |
Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 3204 | if (pkg != null && !r.sbn.getPackageName().equals(pkg)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3205 | continue; |
| 3206 | } |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3207 | if (canceledNotifications == null) { |
| 3208 | canceledNotifications = new ArrayList<>(); |
| 3209 | } |
| 3210 | canceledNotifications.add(r); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 3211 | if (!doit) { |
| 3212 | return true; |
| 3213 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3214 | mNotificationList.remove(i); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 3215 | cancelNotificationLocked(r, false, reason); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3216 | } |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3217 | if (doit && canceledNotifications != null) { |
| 3218 | final int M = canceledNotifications.size(); |
| 3219 | for (int i = 0; i < M; i++) { |
| 3220 | cancelGroupChildrenLocked(canceledNotifications.get(i), callingUid, callingPid, |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3221 | listenerName, REASON_GROUP_SUMMARY_CANCELED); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3222 | } |
| 3223 | } |
| 3224 | if (canceledNotifications != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3225 | updateLightsLocked(); |
| 3226 | } |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3227 | return canceledNotifications != null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3228 | } |
| 3229 | } |
| 3230 | |
Adam Lesinski | 350159c | 2014-03-27 11:15:11 -0700 | [diff] [blame] | 3231 | void cancelAllLocked(int callingUid, int callingPid, int userId, int reason, |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3232 | ManagedServiceInfo listener, boolean includeCurrentProfiles) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3233 | String listenerName = listener == null ? null : listener.component.toShortString(); |
John Spurlock | e6a7d93 | 2014-03-13 12:29:00 -0400 | [diff] [blame] | 3234 | EventLogTags.writeNotificationCancelAll(callingUid, callingPid, |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3235 | null, userId, 0, 0, reason, listenerName); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 3236 | |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3237 | ArrayList<NotificationRecord> canceledNotifications = null; |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 3238 | final int N = mNotificationList.size(); |
| 3239 | for (int i=N-1; i>=0; i--) { |
| 3240 | NotificationRecord r = mNotificationList.get(i); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3241 | if (includeCurrentProfiles) { |
| 3242 | if (!notificationMatchesCurrentProfiles(r, userId)) { |
| 3243 | continue; |
| 3244 | } |
| 3245 | } else { |
| 3246 | if (!notificationMatchesUserId(r, userId)) { |
| 3247 | continue; |
| 3248 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3249 | } |
| 3250 | |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 3251 | if ((r.getFlags() & (Notification.FLAG_ONGOING_EVENT |
| 3252 | | Notification.FLAG_NO_CLEAR)) == 0) { |
| 3253 | mNotificationList.remove(i); |
Christoph Studer | 546bec8 | 2014-03-14 12:17:12 +0100 | [diff] [blame] | 3254 | cancelNotificationLocked(r, true, reason); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3255 | // Make a note so we can cancel children later. |
| 3256 | if (canceledNotifications == null) { |
| 3257 | canceledNotifications = new ArrayList<>(); |
| 3258 | } |
| 3259 | canceledNotifications.add(r); |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 3260 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3261 | } |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3262 | int M = canceledNotifications != null ? canceledNotifications.size() : 0; |
| 3263 | for (int i = 0; i < M; i++) { |
| 3264 | cancelGroupChildrenLocked(canceledNotifications.get(i), callingUid, callingPid, |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3265 | listenerName, REASON_GROUP_SUMMARY_CANCELED); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3266 | } |
Adam Lesinski | e824026 | 2014-03-26 16:01:00 -0700 | [diff] [blame] | 3267 | updateLightsLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3268 | } |
| 3269 | |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3270 | // Warning: The caller is responsible for invoking updateLightsLocked(). |
| 3271 | private void cancelGroupChildrenLocked(NotificationRecord r, int callingUid, int callingPid, |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3272 | String listenerName, int reason) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3273 | Notification n = r.getNotification(); |
Christoph Studer | 3f31f5d | 2014-07-31 16:55:32 +0200 | [diff] [blame] | 3274 | if (!n.isGroupSummary()) { |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3275 | return; |
| 3276 | } |
| 3277 | |
| 3278 | String pkg = r.sbn.getPackageName(); |
| 3279 | int userId = r.getUserId(); |
| 3280 | |
| 3281 | if (pkg == null) { |
| 3282 | if (DBG) Log.e(TAG, "No package for group summary: " + r.getKey()); |
| 3283 | return; |
| 3284 | } |
| 3285 | |
| 3286 | final int N = mNotificationList.size(); |
| 3287 | for (int i = N - 1; i >= 0; i--) { |
| 3288 | NotificationRecord childR = mNotificationList.get(i); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3289 | StatusBarNotification childSbn = childR.sbn; |
Christoph Studer | c44caa9 | 2014-08-22 19:16:00 +0200 | [diff] [blame] | 3290 | if (childR.getNotification().isGroupChild() && |
| 3291 | childR.getGroupKey().equals(r.getGroupKey())) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3292 | EventLogTags.writeNotificationCancel(callingUid, callingPid, pkg, childSbn.getId(), |
| 3293 | childSbn.getTag(), userId, 0, 0, reason, listenerName); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3294 | mNotificationList.remove(i); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3295 | cancelNotificationLocked(childR, false, reason); |
Christoph Studer | e4ef156b | 2014-07-04 18:41:57 +0200 | [diff] [blame] | 3296 | } |
| 3297 | } |
| 3298 | } |
| 3299 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3300 | // lock on mNotificationList |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3301 | void updateLightsLocked() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3302 | { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3303 | // handle notification lights |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3304 | NotificationRecord ledNotification = null; |
| 3305 | while (ledNotification == null && !mLights.isEmpty()) { |
| 3306 | final String owner = mLights.get(mLights.size() - 1); |
| 3307 | ledNotification = mNotificationsByKey.get(owner); |
| 3308 | if (ledNotification == null) { |
| 3309 | Slog.wtfStack(TAG, "LED Notification does not exist: " + owner); |
| 3310 | mLights.remove(owner); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3311 | } |
| 3312 | } |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 3313 | |
Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 3314 | // 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] | 3315 | if (ledNotification == null || mInCall || mScreenOn) { |
Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 3316 | mNotificationLight.turnOff(); |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 3317 | if (mStatusBar != null) { |
| 3318 | mStatusBar.notificationLightOff(); |
| 3319 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3320 | } else { |
Chris Wren | 6054e61 | 2014-11-25 17:16:46 -0500 | [diff] [blame] | 3321 | final Notification ledno = ledNotification.sbn.getNotification(); |
Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 3322 | int ledARGB = ledno.ledARGB; |
| 3323 | int ledOnMS = ledno.ledOnMS; |
| 3324 | int ledOffMS = ledno.ledOffMS; |
| 3325 | if ((ledno.defaults & Notification.DEFAULT_LIGHTS) != 0) { |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 3326 | ledARGB = mDefaultNotificationColor; |
| 3327 | ledOnMS = mDefaultNotificationLedOn; |
| 3328 | ledOffMS = mDefaultNotificationLedOff; |
| 3329 | } |
| 3330 | if (mNotificationPulseEnabled) { |
| 3331 | // pulse repeatedly |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3332 | mNotificationLight.setFlashing(ledARGB, Light.LIGHT_FLASH_TIMED, |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 3333 | ledOnMS, ledOffMS); |
Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 3334 | } |
Wei Liu | 97e5666 | 2016-03-04 10:52:33 -0800 | [diff] [blame] | 3335 | if (mStatusBar != null) { |
| 3336 | // let SystemUI make an independent decision |
| 3337 | mStatusBar.notificationLightPulse(ledARGB, ledOnMS, ledOffMS); |
| 3338 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 3339 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | // lock on mNotificationList |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 3343 | int indexOfNotificationLocked(String pkg, String tag, int id, int userId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3344 | { |
| 3345 | ArrayList<NotificationRecord> list = mNotificationList; |
| 3346 | final int len = list.size(); |
| 3347 | for (int i=0; i<len; i++) { |
| 3348 | NotificationRecord r = list.get(i); |
Vladimir Marko | 2526f33 | 2013-09-11 11:13:55 +0100 | [diff] [blame] | 3349 | if (notificationMatchesUserId(r, userId) && r.sbn.getId() == id && |
| 3350 | TextUtils.equals(r.sbn.getTag(), tag) && r.sbn.getPackageName().equals(pkg)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3351 | return i; |
| 3352 | } |
| 3353 | } |
| 3354 | return -1; |
| 3355 | } |
| 3356 | |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 3357 | // lock on mNotificationList |
| 3358 | int indexOfNotificationLocked(String key) { |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 3359 | final int N = mNotificationList.size(); |
| 3360 | for (int i = 0; i < N; i++) { |
| 3361 | if (key.equals(mNotificationList.get(i).getKey())) { |
| 3362 | return i; |
| 3363 | } |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 3364 | } |
Christoph Studer | c511555 | 2014-06-12 20:22:31 +0200 | [diff] [blame] | 3365 | return -1; |
Christoph Studer | 71f18fd | 2014-05-20 17:02:04 +0200 | [diff] [blame] | 3366 | } |
| 3367 | |
Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 3368 | private void updateNotificationPulse() { |
| 3369 | synchronized (mNotificationList) { |
| 3370 | updateLightsLocked(); |
| 3371 | } |
| 3372 | } |
John Spurlock | e677d71 | 2014-02-13 12:52:19 -0500 | [diff] [blame] | 3373 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3374 | private static boolean isUidSystem(int uid) { |
| 3375 | final int appid = UserHandle.getAppId(uid); |
| 3376 | return (appid == Process.SYSTEM_UID || appid == Process.PHONE_UID || uid == 0); |
| 3377 | } |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 3378 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3379 | private static boolean isCallerSystem() { |
| 3380 | return isUidSystem(Binder.getCallingUid()); |
| 3381 | } |
| 3382 | |
| 3383 | private static void checkCallerIsSystem() { |
| 3384 | if (isCallerSystem()) { |
| 3385 | return; |
| 3386 | } |
| 3387 | throw new SecurityException("Disallowed call for uid " + Binder.getCallingUid()); |
| 3388 | } |
| 3389 | |
| 3390 | private static void checkCallerIsSystemOrSameApp(String pkg) { |
| 3391 | if (isCallerSystem()) { |
| 3392 | return; |
| 3393 | } |
| 3394 | final int uid = Binder.getCallingUid(); |
| 3395 | try { |
| 3396 | ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo( |
| 3397 | pkg, 0, UserHandle.getCallingUserId()); |
Dan Sandler | 09afc2e | 2014-07-18 14:29:20 -0400 | [diff] [blame] | 3398 | if (ai == null) { |
| 3399 | throw new SecurityException("Unknown package " + pkg); |
| 3400 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3401 | if (!UserHandle.isSameApp(ai.uid, uid)) { |
| 3402 | throw new SecurityException("Calling uid " + uid + " gave package" |
| 3403 | + pkg + " which is owned by uid " + ai.uid); |
| 3404 | } |
| 3405 | } catch (RemoteException re) { |
| 3406 | throw new SecurityException("Unknown package " + pkg + "\n" + re); |
| 3407 | } |
| 3408 | } |
| 3409 | |
John Spurlock | 32fe4c6 | 2014-10-02 12:16:02 -0400 | [diff] [blame] | 3410 | private static String callStateToString(int state) { |
| 3411 | switch (state) { |
| 3412 | case TelephonyManager.CALL_STATE_IDLE: return "CALL_STATE_IDLE"; |
| 3413 | case TelephonyManager.CALL_STATE_RINGING: return "CALL_STATE_RINGING"; |
| 3414 | case TelephonyManager.CALL_STATE_OFFHOOK: return "CALL_STATE_OFFHOOK"; |
| 3415 | default: return "CALL_STATE_UNKNOWN_" + state; |
| 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | private void listenForCallState() { |
| 3420 | TelephonyManager.from(getContext()).listen(new PhoneStateListener() { |
| 3421 | @Override |
| 3422 | public void onCallStateChanged(int state, String incomingNumber) { |
| 3423 | if (mCallState == state) return; |
| 3424 | if (DBG) Slog.d(TAG, "Call state changed: " + callStateToString(state)); |
| 3425 | mCallState = state; |
| 3426 | } |
| 3427 | }, PhoneStateListener.LISTEN_CALL_STATE); |
| 3428 | } |
| 3429 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3430 | /** |
| 3431 | * Generates a NotificationRankingUpdate from 'sbns', considering only |
| 3432 | * notifications visible to the given listener. |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 3433 | * |
| 3434 | * <p>Caller must hold a lock on mNotificationList.</p> |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3435 | */ |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 3436 | private NotificationRankingUpdate makeRankingUpdateLocked(ManagedServiceInfo info) { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 3437 | final int N = mNotificationList.size(); |
| 3438 | ArrayList<String> keys = new ArrayList<String>(N); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 3439 | ArrayList<String> interceptedKeys = new ArrayList<String>(N); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 3440 | ArrayList<Integer> importance = new ArrayList<>(N); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 3441 | Bundle visibilityOverrides = new Bundle(); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 3442 | Bundle suppressedVisualEffects = new Bundle(); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 3443 | Bundle explanation = new Bundle(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 3444 | for (int i = 0; i < N; i++) { |
| 3445 | NotificationRecord record = mNotificationList.get(i); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 3446 | if (!isVisibleToListener(record.sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3447 | continue; |
| 3448 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 3449 | final String key = record.sbn.getKey(); |
| 3450 | keys.add(key); |
| 3451 | importance.add(record.getImportance()); |
| 3452 | if (record.getImportanceExplanation() != null) { |
| 3453 | explanation.putCharSequence(key, record.getImportanceExplanation()); |
| 3454 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 3455 | if (record.isIntercepted()) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 3456 | interceptedKeys.add(key); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 3457 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3458 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 3459 | suppressedVisualEffects.putInt(key, record.getSuppressedVisualEffects()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 3460 | if (record.getPackageVisibilityOverride() |
| 3461 | != NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE) { |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 3462 | visibilityOverrides.putInt(key, record.getPackageVisibilityOverride()); |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 3463 | } |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3464 | } |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 3465 | final int M = keys.size(); |
| 3466 | String[] keysAr = keys.toArray(new String[M]); |
Christoph Studer | 1d599da | 2014-06-12 15:25:59 +0200 | [diff] [blame] | 3467 | String[] interceptedKeysAr = interceptedKeys.toArray(new String[interceptedKeys.size()]); |
Chris Wren | bdf3376 | 2015-12-04 15:50:51 -0500 | [diff] [blame] | 3468 | int[] importanceAr = new int[M]; |
| 3469 | for (int i = 0; i < M; i++) { |
| 3470 | importanceAr[i] = importance.get(i); |
| 3471 | } |
Chris Wren | 3ad4e3a | 2014-09-02 17:23:51 -0400 | [diff] [blame] | 3472 | return new NotificationRankingUpdate(keysAr, interceptedKeysAr, visibilityOverrides, |
Julia Reynolds | 0421e6d | 2016-01-08 09:51:24 -0500 | [diff] [blame] | 3473 | suppressedVisualEffects, importanceAr, explanation); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3474 | } |
| 3475 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 3476 | private boolean isVisibleToListener(StatusBarNotification sbn, ManagedServiceInfo listener) { |
| 3477 | if (!listener.enabledAndUserMatches(sbn.getUserId())) { |
| 3478 | return false; |
| 3479 | } |
Justin Koh | 8d11a5a | 2014-08-04 18:29:49 -0700 | [diff] [blame] | 3480 | // TODO: remove this for older listeners. |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 3481 | return true; |
| 3482 | } |
| 3483 | |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 3484 | private boolean isPackageSuspendedForUser(String pkg, int uid) { |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 3485 | int userId = UserHandle.getUserId(uid); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 3486 | try { |
Andrei Stingaceanu | 355b232 | 2016-02-12 16:43:51 +0000 | [diff] [blame] | 3487 | return AppGlobals.getPackageManager().isPackageSuspendedForUser(pkg, userId); |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 3488 | } catch (RemoteException re) { |
| 3489 | throw new SecurityException("Could not talk to package manager service"); |
Andrei Stingaceanu | efc4a34 | 2016-03-22 14:43:01 +0000 | [diff] [blame] | 3490 | } catch (IllegalArgumentException ex) { |
| 3491 | // Package not found. |
| 3492 | return false; |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 3493 | } |
Andrei Stingaceanu | 0122f651 | 2016-01-22 15:33:03 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3496 | private class TrimCache { |
| 3497 | StatusBarNotification heavy; |
| 3498 | StatusBarNotification sbnClone; |
| 3499 | StatusBarNotification sbnCloneLight; |
| 3500 | |
| 3501 | TrimCache(StatusBarNotification sbn) { |
| 3502 | heavy = sbn; |
| 3503 | } |
| 3504 | |
| 3505 | StatusBarNotification ForListener(ManagedServiceInfo info) { |
| 3506 | if (mListeners.getOnNotificationPostedTrim(info) == TRIM_LIGHT) { |
| 3507 | if (sbnCloneLight == null) { |
| 3508 | sbnCloneLight = heavy.cloneLight(); |
| 3509 | } |
| 3510 | return sbnCloneLight; |
| 3511 | } else { |
| 3512 | if (sbnClone == null) { |
| 3513 | sbnClone = heavy.clone(); |
| 3514 | } |
| 3515 | return sbnClone; |
| 3516 | } |
| 3517 | } |
| 3518 | } |
| 3519 | |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 3520 | public class NotificationRankers extends ManagedServices { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3521 | |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 3522 | public NotificationRankers() { |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3523 | super(getContext(), mHandler, mNotificationList, mUserProfiles); |
| 3524 | } |
| 3525 | |
| 3526 | @Override |
| 3527 | protected Config getConfig() { |
| 3528 | Config c = new Config(); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 3529 | c.caption = "notification ranker service"; |
| 3530 | c.serviceInterface = NotificationRankerService.SERVICE_INTERFACE; |
| 3531 | c.secureSettingName = null; |
| 3532 | c.bindPermission = Manifest.permission.BIND_NOTIFICATION_RANKER_SERVICE; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3533 | c.settingsAction = Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS; |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 3534 | c.clientLabel = R.string.notification_ranker_binding_label; |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3535 | return c; |
| 3536 | } |
| 3537 | |
| 3538 | @Override |
| 3539 | protected IInterface asInterface(IBinder binder) { |
| 3540 | return INotificationListener.Stub.asInterface(binder); |
| 3541 | } |
| 3542 | |
| 3543 | @Override |
| 3544 | protected boolean checkType(IInterface service) { |
| 3545 | return service instanceof INotificationListener; |
| 3546 | } |
| 3547 | |
| 3548 | @Override |
| 3549 | protected void onServiceAdded(ManagedServiceInfo info) { |
| 3550 | mListeners.registerGuestService(info); |
| 3551 | } |
| 3552 | |
| 3553 | @Override |
| 3554 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
| 3555 | mListeners.unregisterService(removed.service, removed.userid); |
| 3556 | } |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3557 | |
| 3558 | public void onNotificationEnqueued(final NotificationRecord r) { |
| 3559 | final StatusBarNotification sbn = r.sbn; |
| 3560 | TrimCache trimCache = new TrimCache(sbn); |
| 3561 | |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 3562 | // mServices is the list inside ManagedServices of all the rankers, |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3563 | // There should be only one, but it's a list, so while we enforce |
| 3564 | // singularity elsewhere, we keep it general here, to avoid surprises. |
Chris Wren | 0efdb88 | 2016-03-01 17:17:47 -0500 | [diff] [blame] | 3565 | for (final ManagedServiceInfo info : NotificationRankers.this.mServices) { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3566 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 3567 | if (!sbnVisible) { |
| 3568 | continue; |
| 3569 | } |
| 3570 | |
| 3571 | final int importance = r.getImportance(); |
| 3572 | final boolean fromUser = r.isImportanceFromUser(); |
| 3573 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 3574 | mHandler.post(new Runnable() { |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3575 | @Override |
| 3576 | public void run() { |
| 3577 | notifyEnqueued(info, sbnToPost, importance, fromUser); |
| 3578 | } |
| 3579 | }); |
| 3580 | } |
| 3581 | } |
| 3582 | |
| 3583 | private void notifyEnqueued(final ManagedServiceInfo info, |
| 3584 | final StatusBarNotification sbn, int importance, boolean fromUser) { |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 3585 | final INotificationListener ranker = (INotificationListener) info.service; |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3586 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
| 3587 | try { |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 3588 | ranker.onNotificationEnqueued(sbnHolder, importance, fromUser); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3589 | } catch (RemoteException ex) { |
Chris Wren | e0ba7eb | 2016-03-04 17:30:43 -0500 | [diff] [blame] | 3590 | Log.e(TAG, "unable to notify ranker (enqueued): " + ranker, ex); |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3591 | } |
| 3592 | } |
| 3593 | |
| 3594 | public boolean isEnabled() { |
| 3595 | return !mServices.isEmpty(); |
| 3596 | } |
Julia Reynolds | 1c9bd42 | 2016-03-15 09:25:56 -0400 | [diff] [blame] | 3597 | |
| 3598 | @Override |
| 3599 | public void onUserSwitched(int user) { |
| 3600 | for (ManagedServiceInfo info : mServices) { |
| 3601 | unregisterService(info.service, info.userid); |
| 3602 | } |
| 3603 | registerRanker(); |
| 3604 | } |
| 3605 | |
| 3606 | @Override |
| 3607 | public void onPackagesChanged(boolean queryReplace, String[] pkgList) { |
| 3608 | if (DEBUG) Slog.d(TAG, "onPackagesChanged queryReplace=" + queryReplace |
| 3609 | + " pkgList=" + (pkgList == null ? null : Arrays.asList(pkgList))); |
Chris Wren | 88d2f6d | 2016-03-17 15:47:09 -0400 | [diff] [blame] | 3610 | if (mRankerServicePackageName == null) { |
| 3611 | return; |
| 3612 | } |
Julia Reynolds | 1c9bd42 | 2016-03-15 09:25:56 -0400 | [diff] [blame] | 3613 | |
| 3614 | if (pkgList != null && (pkgList.length > 0)) { |
| 3615 | for (String pkgName : pkgList) { |
| 3616 | if (mRankerServicePackageName.equals(pkgName)) { |
| 3617 | registerRanker(); |
| 3618 | } |
| 3619 | } |
| 3620 | } |
| 3621 | } |
| 3622 | |
| 3623 | protected void registerRanker() { |
| 3624 | // Find the updatable ranker and register it. |
Chris Wren | 88d2f6d | 2016-03-17 15:47:09 -0400 | [diff] [blame] | 3625 | if (mRankerServicePackageName == null) { |
| 3626 | Slog.w(TAG, "could not start ranker service: no package specified!"); |
| 3627 | return; |
| 3628 | } |
Julia Reynolds | 1c9bd42 | 2016-03-15 09:25:56 -0400 | [diff] [blame] | 3629 | Set<ComponentName> rankerComponents = queryPackageForServices( |
| 3630 | mRankerServicePackageName, UserHandle.USER_SYSTEM, null); |
| 3631 | Iterator<ComponentName> iterator = rankerComponents.iterator(); |
| 3632 | if (iterator.hasNext()) { |
| 3633 | ComponentName rankerComponent = iterator.next(); |
| 3634 | if (iterator.hasNext()) { |
| 3635 | Slog.e(TAG, "found multiple ranker services:" + rankerComponents); |
| 3636 | } else { |
| 3637 | registerSystemService(rankerComponent, UserHandle.USER_SYSTEM); |
| 3638 | } |
| 3639 | } else { |
| 3640 | Slog.w(TAG, "could not start ranker service: none found"); |
| 3641 | } |
| 3642 | } |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3643 | } |
| 3644 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3645 | public class NotificationListeners extends ManagedServices { |
| 3646 | |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 3647 | private final ArraySet<ManagedServiceInfo> mLightTrimListeners = new ArraySet<>(); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3648 | private boolean mNotificationGroupsDesired; |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 3649 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3650 | public NotificationListeners() { |
| 3651 | super(getContext(), mHandler, mNotificationList, mUserProfiles); |
| 3652 | } |
| 3653 | |
| 3654 | @Override |
| 3655 | protected Config getConfig() { |
| 3656 | Config c = new Config(); |
| 3657 | c.caption = "notification listener"; |
| 3658 | c.serviceInterface = NotificationListenerService.SERVICE_INTERFACE; |
| 3659 | c.secureSettingName = Settings.Secure.ENABLED_NOTIFICATION_LISTENERS; |
| 3660 | c.bindPermission = android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE; |
| 3661 | c.settingsAction = Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS; |
| 3662 | c.clientLabel = R.string.notification_listener_binding_label; |
| 3663 | return c; |
| 3664 | } |
| 3665 | |
| 3666 | @Override |
| 3667 | protected IInterface asInterface(IBinder binder) { |
| 3668 | return INotificationListener.Stub.asInterface(binder); |
| 3669 | } |
| 3670 | |
| 3671 | @Override |
Chris Wren | 51017d0 | 2015-12-15 15:34:46 -0500 | [diff] [blame] | 3672 | protected boolean checkType(IInterface service) { |
| 3673 | return service instanceof INotificationListener; |
| 3674 | } |
| 3675 | |
| 3676 | @Override |
John Spurlock | 3b98b3f | 2014-05-01 09:08:48 -0400 | [diff] [blame] | 3677 | public void onServiceAdded(ManagedServiceInfo info) { |
| 3678 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 3679 | final NotificationRankingUpdate update; |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3680 | synchronized (mNotificationList) { |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3681 | updateNotificationGroupsDesiredLocked(); |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 3682 | update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3683 | } |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3684 | try { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 3685 | listener.onListenerConnected(update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3686 | } catch (RemoteException e) { |
| 3687 | // we tried |
| 3688 | } |
| 3689 | } |
| 3690 | |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3691 | @Override |
| 3692 | protected void onServiceRemovedLocked(ManagedServiceInfo removed) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3693 | if (mListenersDisablingEffects.remove(removed)) { |
| 3694 | updateListenerHintsLocked(); |
Christoph Studer | 0d6ef4b | 2014-12-02 15:00:48 +0100 | [diff] [blame] | 3695 | updateEffectsSuppressorLocked(); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3696 | } |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 3697 | mLightTrimListeners.remove(removed); |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3698 | updateNotificationGroupsDesiredLocked(); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | public void setOnNotificationPostedTrimLocked(ManagedServiceInfo info, int trim) { |
| 3702 | if (trim == TRIM_LIGHT) { |
| 3703 | mLightTrimListeners.add(info); |
| 3704 | } else { |
| 3705 | mLightTrimListeners.remove(info); |
| 3706 | } |
| 3707 | } |
| 3708 | |
| 3709 | public int getOnNotificationPostedTrim(ManagedServiceInfo info) { |
| 3710 | return mLightTrimListeners.contains(info) ? TRIM_LIGHT : TRIM_FULL; |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3711 | } |
| 3712 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3713 | /** |
| 3714 | * asynchronously notify all listeners about a new notification |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 3715 | * |
| 3716 | * <p> |
| 3717 | * Also takes care of removing a notification that has been visible to a listener before, |
| 3718 | * but isn't anymore. |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3719 | */ |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 3720 | public void notifyPostedLocked(StatusBarNotification sbn, StatusBarNotification oldSbn) { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 3721 | // Lazily initialized snapshots of the notification. |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3722 | TrimCache trimCache = new TrimCache(sbn); |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 3723 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3724 | for (final ManagedServiceInfo info : mServices) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 3725 | boolean sbnVisible = isVisibleToListener(sbn, info); |
| 3726 | boolean oldSbnVisible = oldSbn != null ? isVisibleToListener(oldSbn, info) : false; |
| 3727 | // This notification hasn't been and still isn't visible -> ignore. |
| 3728 | if (!oldSbnVisible && !sbnVisible) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3729 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 3730 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 3731 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 3732 | |
| 3733 | // This notification became invisible -> remove the old one. |
| 3734 | if (oldSbnVisible && !sbnVisible) { |
| 3735 | final StatusBarNotification oldSbnLightClone = oldSbn.cloneLight(); |
| 3736 | mHandler.post(new Runnable() { |
| 3737 | @Override |
| 3738 | public void run() { |
| 3739 | notifyRemoved(info, oldSbnLightClone, update); |
| 3740 | } |
| 3741 | }); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3742 | continue; |
| 3743 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 3744 | |
Chris Wren | 4763342 | 2016-01-22 09:56:59 -0500 | [diff] [blame] | 3745 | final StatusBarNotification sbnToPost = trimCache.ForListener(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3746 | mHandler.post(new Runnable() { |
| 3747 | @Override |
| 3748 | public void run() { |
Christoph Studer | b82bc78 | 2014-08-20 14:29:43 +0200 | [diff] [blame] | 3749 | notifyPosted(info, sbnToPost, update); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3750 | } |
| 3751 | }); |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3752 | } |
| 3753 | } |
Kenny Guy | 3a7c4a5 | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3754 | |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3755 | /** |
| 3756 | * asynchronously notify all listeners about a removed notification |
| 3757 | */ |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 3758 | public void notifyRemovedLocked(StatusBarNotification sbn) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3759 | // make a copy in case changes are made to the underlying Notification object |
| 3760 | // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the |
| 3761 | // notification |
| 3762 | final StatusBarNotification sbnLight = sbn.cloneLight(); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 3763 | for (final ManagedServiceInfo info : mServices) { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 3764 | if (!isVisibleToListener(sbn, info)) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3765 | continue; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 3766 | } |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 3767 | final NotificationRankingUpdate update = makeRankingUpdateLocked(info); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3768 | mHandler.post(new Runnable() { |
| 3769 | @Override |
| 3770 | public void run() { |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 3771 | notifyRemoved(info, sbnLight, update); |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3772 | } |
| 3773 | }); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 3774 | } |
| 3775 | } |
| 3776 | |
| 3777 | /** |
| 3778 | * asynchronously notify all listeners about a reordering of notifications |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 3779 | */ |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 3780 | public void notifyRankingUpdateLocked() { |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 3781 | for (final ManagedServiceInfo serviceInfo : mServices) { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3782 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 3783 | continue; |
| 3784 | } |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 3785 | final NotificationRankingUpdate update = makeRankingUpdateLocked(serviceInfo); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3786 | mHandler.post(new Runnable() { |
| 3787 | @Override |
| 3788 | public void run() { |
Chris Wren | 333a61c | 2014-05-28 16:40:57 -0400 | [diff] [blame] | 3789 | notifyRankingUpdate(serviceInfo, update); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3790 | } |
| 3791 | }); |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3792 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3793 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3794 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3795 | public void notifyListenerHintsChangedLocked(final int hints) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3796 | for (final ManagedServiceInfo serviceInfo : mServices) { |
| 3797 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 3798 | continue; |
| 3799 | } |
| 3800 | mHandler.post(new Runnable() { |
| 3801 | @Override |
| 3802 | public void run() { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3803 | notifyListenerHintsChanged(serviceInfo, hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3804 | } |
| 3805 | }); |
| 3806 | } |
| 3807 | } |
| 3808 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 3809 | public void notifyInterruptionFilterChanged(final int interruptionFilter) { |
| 3810 | for (final ManagedServiceInfo serviceInfo : mServices) { |
| 3811 | if (!serviceInfo.isEnabledForCurrentProfiles()) { |
| 3812 | continue; |
| 3813 | } |
| 3814 | mHandler.post(new Runnable() { |
| 3815 | @Override |
| 3816 | public void run() { |
| 3817 | notifyInterruptionFilterChanged(serviceInfo, interruptionFilter); |
| 3818 | } |
| 3819 | }); |
| 3820 | } |
| 3821 | } |
| 3822 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 3823 | private void notifyPosted(final ManagedServiceInfo info, |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3824 | final StatusBarNotification sbn, NotificationRankingUpdate rankingUpdate) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3825 | final INotificationListener listener = (INotificationListener)info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 3826 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3827 | try { |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 3828 | listener.onNotificationPosted(sbnHolder, rankingUpdate); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3829 | } catch (RemoteException ex) { |
| 3830 | Log.e(TAG, "unable to notify listener (posted): " + listener, ex); |
| 3831 | } |
| 3832 | } |
| 3833 | |
Christoph Studer | cef37cf | 2014-07-25 14:18:17 +0200 | [diff] [blame] | 3834 | private void notifyRemoved(ManagedServiceInfo info, StatusBarNotification sbn, |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3835 | NotificationRankingUpdate rankingUpdate) { |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3836 | if (!info.enabledAndUserMatches(sbn.getUserId())) { |
| 3837 | return; |
| 3838 | } |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3839 | final INotificationListener listener = (INotificationListener) info.service; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 3840 | StatusBarNotificationHolder sbnHolder = new StatusBarNotificationHolder(sbn); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3841 | try { |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 3842 | listener.onNotificationRemoved(sbnHolder, rankingUpdate); |
John Spurlock | 7340fc8 | 2014-04-24 18:50:12 -0400 | [diff] [blame] | 3843 | } catch (RemoteException ex) { |
| 3844 | Log.e(TAG, "unable to notify listener (removed): " + listener, ex); |
John Spurlock | b408e8e | 2014-04-23 21:12:45 -0400 | [diff] [blame] | 3845 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3846 | } |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 3847 | |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3848 | private void notifyRankingUpdate(ManagedServiceInfo info, |
| 3849 | NotificationRankingUpdate rankingUpdate) { |
| 3850 | final INotificationListener listener = (INotificationListener) info.service; |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 3851 | try { |
Christoph Studer | 05ad482 | 2014-05-16 14:16:03 +0200 | [diff] [blame] | 3852 | listener.onNotificationRankingUpdate(rankingUpdate); |
Chris Wren | f953664 | 2014-04-17 10:01:54 -0400 | [diff] [blame] | 3853 | } catch (RemoteException ex) { |
| 3854 | Log.e(TAG, "unable to notify listener (ranking update): " + listener, ex); |
| 3855 | } |
| 3856 | } |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3857 | |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3858 | private void notifyListenerHintsChanged(ManagedServiceInfo info, int hints) { |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3859 | final INotificationListener listener = (INotificationListener) info.service; |
| 3860 | try { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3861 | listener.onListenerHintsChanged(hints); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3862 | } catch (RemoteException ex) { |
John Spurlock | d8afe3c | 2014-08-01 14:04:07 -0400 | [diff] [blame] | 3863 | Log.e(TAG, "unable to notify listener (listener hints): " + listener, ex); |
John Spurlock | 1fa865f | 2014-07-21 14:56:39 -0400 | [diff] [blame] | 3864 | } |
| 3865 | } |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 3866 | |
Christoph Studer | 85a384b | 2014-08-27 20:16:15 +0200 | [diff] [blame] | 3867 | private void notifyInterruptionFilterChanged(ManagedServiceInfo info, |
| 3868 | int interruptionFilter) { |
| 3869 | final INotificationListener listener = (INotificationListener) info.service; |
| 3870 | try { |
| 3871 | listener.onInterruptionFilterChanged(interruptionFilter); |
| 3872 | } catch (RemoteException ex) { |
| 3873 | Log.e(TAG, "unable to notify listener (interruption filter): " + listener, ex); |
| 3874 | } |
| 3875 | } |
| 3876 | |
Justin Koh | 38156c5 | 2014-06-04 13:57:49 -0700 | [diff] [blame] | 3877 | private boolean isListenerPackage(String packageName) { |
| 3878 | if (packageName == null) { |
| 3879 | return false; |
| 3880 | } |
| 3881 | // TODO: clean up locking object later |
| 3882 | synchronized (mNotificationList) { |
| 3883 | for (final ManagedServiceInfo serviceInfo : mServices) { |
| 3884 | if (packageName.equals(serviceInfo.component.getPackageName())) { |
| 3885 | return true; |
| 3886 | } |
| 3887 | } |
| 3888 | } |
| 3889 | return false; |
| 3890 | } |
Christoph Studer | 265c105 | 2014-07-23 17:14:33 +0200 | [diff] [blame] | 3891 | |
| 3892 | /** |
| 3893 | * Returns whether any of the currently registered listeners wants to receive notification |
| 3894 | * groups. |
| 3895 | * |
| 3896 | * <p>Currently we assume groups are desired by non-SystemUI listeners.</p> |
| 3897 | */ |
| 3898 | public boolean notificationGroupsDesired() { |
| 3899 | return mNotificationGroupsDesired; |
| 3900 | } |
| 3901 | |
| 3902 | private void updateNotificationGroupsDesiredLocked() { |
| 3903 | mNotificationGroupsDesired = true; |
| 3904 | // No listeners, no groups. |
| 3905 | if (mServices.isEmpty()) { |
| 3906 | mNotificationGroupsDesired = false; |
| 3907 | return; |
| 3908 | } |
| 3909 | // One listener: Check whether it's SysUI. |
| 3910 | if (mServices.size() == 1 && |
| 3911 | mServices.get(0).component.getPackageName().equals("com.android.systemui")) { |
| 3912 | mNotificationGroupsDesired = false; |
| 3913 | return; |
| 3914 | } |
| 3915 | } |
Kenny Guy | a263e4e | 2014-03-03 18:24:03 +0000 | [diff] [blame] | 3916 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3917 | |
| 3918 | public static final class DumpFilter { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3919 | public boolean filtered = false; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3920 | public String pkgFilter; |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3921 | public boolean zen; |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3922 | public long since; |
| 3923 | public boolean stats; |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3924 | public boolean redact = true; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3925 | |
| 3926 | public static DumpFilter parseFromArguments(String[] args) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3927 | final DumpFilter filter = new DumpFilter(); |
| 3928 | for (int ai = 0; ai < args.length; ai++) { |
| 3929 | final String a = args[ai]; |
| 3930 | if ("--noredact".equals(a) || "--reveal".equals(a)) { |
| 3931 | filter.redact = false; |
| 3932 | } else if ("p".equals(a) || "pkg".equals(a) || "--package".equals(a)) { |
| 3933 | if (ai < args.length-1) { |
| 3934 | ai++; |
| 3935 | filter.pkgFilter = args[ai].trim().toLowerCase(); |
| 3936 | if (filter.pkgFilter.isEmpty()) { |
| 3937 | filter.pkgFilter = null; |
| 3938 | } else { |
| 3939 | filter.filtered = true; |
| 3940 | } |
| 3941 | } |
| 3942 | } else if ("--zen".equals(a) || "zen".equals(a)) { |
| 3943 | filter.filtered = true; |
| 3944 | filter.zen = true; |
| 3945 | } else if ("--stats".equals(a)) { |
| 3946 | filter.stats = true; |
| 3947 | if (ai < args.length-1) { |
| 3948 | ai++; |
| 3949 | filter.since = Long.valueOf(args[ai]); |
| 3950 | } else { |
| 3951 | filter.since = 0; |
| 3952 | } |
| 3953 | } |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3954 | } |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3955 | return filter; |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3956 | } |
| 3957 | |
| 3958 | public boolean matches(StatusBarNotification sbn) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3959 | if (!filtered) return true; |
| 3960 | return zen ? true : sbn != null |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3961 | && (matches(sbn.getPackageName()) || matches(sbn.getOpPkg())); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3962 | } |
| 3963 | |
| 3964 | public boolean matches(ComponentName component) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3965 | if (!filtered) return true; |
| 3966 | return zen ? true : component != null && matches(component.getPackageName()); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3967 | } |
| 3968 | |
| 3969 | public boolean matches(String pkg) { |
Dan Sandler | a177031 | 2015-07-10 13:59:29 -0400 | [diff] [blame] | 3970 | if (!filtered) return true; |
| 3971 | return zen ? true : pkg != null && pkg.toLowerCase().contains(pkgFilter); |
John Spurlock | 50806fc | 2014-07-15 10:22:02 -0400 | [diff] [blame] | 3972 | } |
| 3973 | |
| 3974 | @Override |
| 3975 | public String toString() { |
Chris Wren | e4b3880 | 2015-07-07 15:54:19 -0400 | [diff] [blame] | 3976 | return stats ? "stats" : zen ? "zen" : ('\'' + pkgFilter + '\''); |
John Spurlock | 25e2d24 | 2014-06-27 13:58:23 -0400 | [diff] [blame] | 3977 | } |
| 3978 | } |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 3979 | |
| 3980 | /** |
| 3981 | * Wrapper for a StatusBarNotification object that allows transfer across a oneway |
| 3982 | * binder without sending large amounts of data over a oneway transaction. |
| 3983 | */ |
| 3984 | private static final class StatusBarNotificationHolder |
| 3985 | extends IStatusBarNotificationHolder.Stub { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 3986 | private StatusBarNotification mValue; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 3987 | |
| 3988 | public StatusBarNotificationHolder(StatusBarNotification value) { |
| 3989 | mValue = value; |
| 3990 | } |
| 3991 | |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 3992 | /** 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] | 3993 | @Override |
| 3994 | public StatusBarNotification get() { |
Griff Hazen | e9aac5f | 2014-09-05 20:04:09 -0700 | [diff] [blame] | 3995 | StatusBarNotification value = mValue; |
| 3996 | mValue = null; |
| 3997 | return value; |
Griff Hazen | 84a00ea | 2014-09-02 17:10:47 -0700 | [diff] [blame] | 3998 | } |
| 3999 | } |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 4000 | |
| 4001 | private final class PolicyAccess { |
| 4002 | private static final String SEPARATOR = ":"; |
| 4003 | private final String[] PERM = { |
| 4004 | android.Manifest.permission.ACCESS_NOTIFICATION_POLICY |
| 4005 | }; |
| 4006 | |
| 4007 | public boolean isPackageGranted(String pkg) { |
| 4008 | return pkg != null && getGrantedPackages().contains(pkg); |
| 4009 | } |
| 4010 | |
| 4011 | public void put(String pkg, boolean granted) { |
| 4012 | if (pkg == null) return; |
| 4013 | final ArraySet<String> pkgs = getGrantedPackages(); |
| 4014 | boolean changed; |
| 4015 | if (granted) { |
| 4016 | changed = pkgs.add(pkg); |
| 4017 | } else { |
| 4018 | changed = pkgs.remove(pkg); |
| 4019 | } |
| 4020 | if (!changed) return; |
| 4021 | final String setting = TextUtils.join(SEPARATOR, pkgs); |
| 4022 | final int currentUser = ActivityManager.getCurrentUser(); |
| 4023 | Settings.Secure.putStringForUser(getContext().getContentResolver(), |
| 4024 | Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES, |
| 4025 | setting, |
| 4026 | currentUser); |
| 4027 | getContext().sendBroadcastAsUser(new Intent(NotificationManager |
| 4028 | .ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED) |
| 4029 | .setPackage(pkg) |
| 4030 | .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY), new UserHandle(currentUser), null); |
| 4031 | } |
| 4032 | |
| 4033 | public ArraySet<String> getGrantedPackages() { |
| 4034 | final ArraySet<String> pkgs = new ArraySet<>(); |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 4035 | |
| 4036 | long identity = Binder.clearCallingIdentity(); |
| 4037 | try { |
| 4038 | final String setting = Settings.Secure.getStringForUser( |
| 4039 | getContext().getContentResolver(), |
| 4040 | Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES, |
| 4041 | ActivityManager.getCurrentUser()); |
| 4042 | if (setting != null) { |
| 4043 | final String[] tokens = setting.split(SEPARATOR); |
| 4044 | for (int i = 0; i < tokens.length; i++) { |
| 4045 | String token = tokens[i]; |
| 4046 | if (token != null) { |
Andreas Gampe | 1ed71f3 | 2015-12-11 15:49:07 -0800 | [diff] [blame] | 4047 | token = token.trim(); |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 4048 | } |
| 4049 | if (TextUtils.isEmpty(token)) { |
| 4050 | continue; |
| 4051 | } |
| 4052 | pkgs.add(token); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 4053 | } |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 4054 | } |
Julia Reynolds | ea6c448 | 2015-08-13 09:01:33 -0400 | [diff] [blame] | 4055 | } finally { |
| 4056 | Binder.restoreCallingIdentity(identity); |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 4057 | } |
| 4058 | return pkgs; |
| 4059 | } |
| 4060 | |
| 4061 | public String[] getRequestingPackages() throws RemoteException { |
| 4062 | final ParceledListSlice list = AppGlobals.getPackageManager() |
| 4063 | .getPackagesHoldingPermissions(PERM, 0 /*flags*/, |
| 4064 | ActivityManager.getCurrentUser()); |
| 4065 | final List<PackageInfo> pkgs = list.getList(); |
| 4066 | if (pkgs == null || pkgs.isEmpty()) return new String[0]; |
| 4067 | final int N = pkgs.size(); |
| 4068 | final String[] rt = new String[N]; |
| 4069 | for (int i = 0; i < N; i++) { |
| 4070 | rt[i] = pkgs.get(i).packageName; |
| 4071 | } |
| 4072 | return rt; |
| 4073 | } |
| 4074 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4075 | } |