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