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