| 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 | |
| 17 | package com.android.server; |
| 18 | |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 19 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
| 20 | import static org.xmlpull.v1.XmlPullParser.END_TAG; |
| 21 | import static org.xmlpull.v1.XmlPullParser.START_TAG; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 22 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.app.ActivityManagerNative; |
| Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 25 | import android.app.AppGlobals; |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 26 | import android.app.AppOpsManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.app.IActivityManager; |
| 28 | import android.app.INotificationManager; |
| 29 | import android.app.ITransientNotification; |
| 30 | import android.app.Notification; |
| 31 | import android.app.PendingIntent; |
| 32 | import android.app.StatusBarManager; |
| 33 | import android.content.BroadcastReceiver; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 34 | import android.content.ComponentName; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 35 | import android.content.ContentResolver; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.content.Context; |
| 37 | import android.content.Intent; |
| 38 | import android.content.IntentFilter; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 39 | import android.content.ServiceConnection; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 40 | import android.content.pm.ApplicationInfo; |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 41 | import android.content.pm.PackageInfo; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.content.pm.PackageManager; |
| 43 | import android.content.pm.PackageManager.NameNotFoundException; |
| 44 | import android.content.res.Resources; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 45 | import android.database.ContentObserver; |
| Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 46 | import android.graphics.Bitmap; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 47 | import android.media.AudioManager; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 48 | import android.media.IAudioService; |
| 49 | import android.media.IRingtonePlayer; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.net.Uri; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.os.Binder; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.os.Handler; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.os.IBinder; |
| 54 | import android.os.Message; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 55 | import android.os.Process; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 56 | import android.os.RemoteException; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 57 | import android.os.ServiceManager; |
| Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 58 | import android.os.UserHandle; |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 59 | import android.os.UserManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.os.Vibrator; |
| 61 | import android.provider.Settings; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 62 | import android.service.notification.INotificationListener; |
| 63 | import android.service.notification.NotificationListenerService; |
| 64 | import android.service.notification.StatusBarNotification; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 65 | import android.telephony.TelephonyManager; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 66 | import android.text.TextUtils; |
| Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 67 | import android.util.AtomicFile; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import android.util.EventLog; |
| 69 | import android.util.Log; |
| Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 70 | import android.util.Slog; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 71 | import android.util.Xml; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 72 | import android.view.accessibility.AccessibilityEvent; |
| 73 | import android.view.accessibility.AccessibilityManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | import android.widget.Toast; |
| 75 | |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 76 | import org.xmlpull.v1.XmlPullParser; |
| 77 | import org.xmlpull.v1.XmlPullParserException; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 78 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 79 | import java.io.File; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import java.io.FileDescriptor; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 81 | import java.io.FileInputStream; |
| 82 | import java.io.FileNotFoundException; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 83 | import java.io.IOException; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import java.io.PrintWriter; |
| Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 85 | import java.lang.reflect.Array; |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 86 | import java.util.ArrayDeque; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | import java.util.ArrayList; |
| 88 | import java.util.Arrays; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 89 | import java.util.HashSet; |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 90 | import java.util.Iterator; |
| 91 | import java.util.NoSuchElementException; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 92 | |
| 93 | import libcore.io.IoUtils; |
| 94 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 96 | /** {@hide} */ |
| 97 | public class NotificationManagerService extends INotificationManager.Stub |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | { |
| 99 | private static final String TAG = "NotificationService"; |
| 100 | private static final boolean DBG = false; |
| 101 | |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 102 | private static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
| 103 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | // message codes |
| 105 | private static final int MESSAGE_TIMEOUT = 2; |
| 106 | |
| 107 | private static final int LONG_DELAY = 3500; // 3.5 seconds |
| 108 | private static final int SHORT_DELAY = 2000; // 2 seconds |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 109 | |
| 110 | private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 111 | private 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] | 112 | |
| 113 | private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION; |
| Daniel Sandler | 49a2ad1 | 2012-03-28 15:46:39 -0400 | [diff] [blame] | 114 | private static final boolean SCORE_ONGOING_HIGHER = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 116 | private static final int JUNK_SCORE = -1000; |
| 117 | private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10; |
| 118 | private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER; |
| 119 | |
| Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 120 | // Notifications with scores below this will not interrupt the user, either via LED or |
| 121 | // sound or vibration |
| 122 | private static final int SCORE_INTERRUPTION_THRESHOLD = |
| 123 | Notification.PRIORITY_LOW * NOTIFICATION_PRIORITY_MULTIPLIER; |
| 124 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 125 | private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true; |
| 126 | private static final boolean ENABLE_BLOCKED_TOASTS = true; |
| 127 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 128 | private static final String ENABLED_NOTIFICATION_LISTENERS_SEPARATOR = ":"; |
| 129 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | final Context mContext; |
| 131 | final IActivityManager mAm; |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 132 | final UserManager mUserManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | final IBinder mForegroundToken = new Binder(); |
| 134 | |
| 135 | private WorkerHandler mHandler; |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 136 | private StatusBarManagerService mStatusBar; |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 137 | private LightsService.Light mNotificationLight; |
| 138 | private LightsService.Light mAttentionLight; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 140 | private int mDefaultNotificationColor; |
| 141 | private int mDefaultNotificationLedOn; |
| 142 | private int mDefaultNotificationLedOff; |
| 143 | |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 144 | private long[] mDefaultVibrationPattern; |
| 145 | private long[] mFallbackVibrationPattern; |
| 146 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 147 | private boolean mSystemReady; |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 148 | private int mDisabledNotifications; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 150 | private NotificationRecord mSoundNotification; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | private NotificationRecord mVibrateNotification; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 152 | |
| 153 | private IAudioService mAudioService; |
| Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 154 | private Vibrator mVibrator; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 156 | // for enabling and disabling notification pulse behavior |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 157 | private boolean mScreenOn = true; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 158 | private boolean mInCall = false; |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 159 | private boolean mNotificationPulseEnabled; |
| 160 | |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 161 | // used as a mutex for access to all active notifications & listeners |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 162 | private final ArrayList<NotificationRecord> mNotificationList = |
| 163 | new ArrayList<NotificationRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | |
| 165 | private ArrayList<ToastRecord> mToastQueue; |
| 166 | |
| 167 | private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | private NotificationRecord mLedNotification; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 169 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 170 | private final AppOpsManager mAppOps; |
| 171 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 172 | // contains connections to all connected listeners, including app services |
| 173 | // and system listeners |
| 174 | private ArrayList<NotificationListenerInfo> mListeners |
| 175 | = new ArrayList<NotificationListenerInfo>(); |
| 176 | // things that will be put into mListeners as soon as they're ready |
| 177 | private ArrayList<String> mServicesBinding = new ArrayList<String>(); |
| 178 | // lists the component names of all enabled (and therefore connected) listener |
| 179 | // app services for the current user only |
| 180 | private HashSet<ComponentName> mEnabledListenersForCurrentUser |
| 181 | = new HashSet<ComponentName>(); |
| 182 | // Just the packages from mEnabledListenersForCurrentUser |
| 183 | private HashSet<String> mEnabledListenerPackageNames = new HashSet<String>(); |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 184 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 185 | // Notification control database. For now just contains disabled packages. |
| 186 | private AtomicFile mPolicyFile; |
| 187 | private HashSet<String> mBlockedPackages = new HashSet<String>(); |
| 188 | |
| 189 | private static final int DB_VERSION = 1; |
| 190 | |
| 191 | private static final String TAG_BODY = "notification-policy"; |
| 192 | private static final String ATTR_VERSION = "version"; |
| 193 | |
| 194 | private static final String TAG_BLOCKED_PKGS = "blocked-packages"; |
| 195 | private static final String TAG_PACKAGE = "package"; |
| 196 | private static final String ATTR_NAME = "name"; |
| 197 | |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 198 | private class NotificationListenerInfo implements DeathRecipient { |
| 199 | INotificationListener listener; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 200 | ComponentName component; |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 201 | int userid; |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 202 | boolean isSystem; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 203 | ServiceConnection connection; |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 204 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 205 | public NotificationListenerInfo(INotificationListener listener, ComponentName component, |
| 206 | int userid, boolean isSystem) { |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 207 | this.listener = listener; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 208 | this.component = component; |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 209 | this.userid = userid; |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 210 | this.isSystem = isSystem; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 211 | this.connection = null; |
| 212 | } |
| 213 | |
| 214 | public NotificationListenerInfo(INotificationListener listener, ComponentName component, |
| 215 | int userid, ServiceConnection connection) { |
| 216 | this.listener = listener; |
| 217 | this.component = component; |
| 218 | this.userid = userid; |
| 219 | this.isSystem = false; |
| 220 | this.connection = connection; |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 221 | } |
| 222 | |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 223 | boolean enabledAndUserMatches(StatusBarNotification sbn) { |
| 224 | final int nid = sbn.getUserId(); |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 225 | if (!isEnabledForCurrentUser()) { |
| 226 | return false; |
| 227 | } |
| Daniel Sandler | 21ca44d | 2013-03-07 13:58:00 -0500 | [diff] [blame] | 228 | if (this.userid == UserHandle.USER_ALL) return true; |
| Daniel Sandler | 21ca44d | 2013-03-07 13:58:00 -0500 | [diff] [blame] | 229 | return (nid == UserHandle.USER_ALL || nid == this.userid); |
| 230 | } |
| 231 | |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 232 | public void notifyPostedIfUserMatch(StatusBarNotification sbn) { |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 233 | if (!enabledAndUserMatches(sbn)) { |
| 234 | return; |
| 235 | } |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 236 | try { |
| 237 | listener.onNotificationPosted(sbn); |
| 238 | } catch (RemoteException ex) { |
| Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 239 | Log.e(TAG, "unable to notify listener (posted): " + listener, ex); |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | public void notifyRemovedIfUserMatch(StatusBarNotification sbn) { |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 244 | if (!enabledAndUserMatches(sbn)) return; |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 245 | try { |
| 246 | listener.onNotificationRemoved(sbn); |
| 247 | } catch (RemoteException ex) { |
| Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 248 | Log.e(TAG, "unable to notify listener (removed): " + listener, ex); |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public void binderDied() { |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 254 | if (connection == null) { |
| 255 | // This is not a service; it won't be recreated. We can give up this connection. |
| 256 | unregisterListener(this.listener, this.userid); |
| 257 | } |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 258 | } |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 259 | |
| 260 | /** convenience method for looking in mEnabledListenersForCurrentUser */ |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 261 | public boolean isEnabledForCurrentUser() { |
| 262 | if (this.isSystem) return true; |
| 263 | if (this.connection == null) return false; |
| 264 | return mEnabledListenersForCurrentUser.contains(this.component); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 265 | } |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 266 | } |
| 267 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 268 | private static class Archive { |
| Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 269 | static final int BUFFER_SIZE = 250; |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 270 | ArrayDeque<StatusBarNotification> mBuffer = new ArrayDeque<StatusBarNotification>(BUFFER_SIZE); |
| 271 | |
| 272 | public Archive() { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 273 | } |
| Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 274 | |
| Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 275 | public String toString() { |
| 276 | final StringBuilder sb = new StringBuilder(); |
| 277 | final int N = mBuffer.size(); |
| 278 | sb.append("Archive ("); |
| 279 | sb.append(N); |
| 280 | sb.append(" notification"); |
| 281 | sb.append((N==1)?")":"s)"); |
| 282 | return sb.toString(); |
| 283 | } |
| 284 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 285 | public void record(StatusBarNotification nr) { |
| Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 286 | // Nuke heavy parts of notification before storing in archive |
| Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 287 | nr.getNotification().lightenPayload(); |
| Jeff Sharkey | 0c1baf9 | 2013-04-03 13:08:52 -0700 | [diff] [blame] | 288 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 289 | if (mBuffer.size() == BUFFER_SIZE) { |
| 290 | mBuffer.removeFirst(); |
| 291 | } |
| 292 | mBuffer.addLast(nr); |
| 293 | } |
| 294 | |
| Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 295 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 296 | public void clear() { |
| 297 | mBuffer.clear(); |
| 298 | } |
| 299 | |
| 300 | public Iterator<StatusBarNotification> descendingIterator() { |
| 301 | return mBuffer.descendingIterator(); |
| 302 | } |
| 303 | public Iterator<StatusBarNotification> ascendingIterator() { |
| 304 | return mBuffer.iterator(); |
| 305 | } |
| 306 | public Iterator<StatusBarNotification> filter( |
| 307 | final Iterator<StatusBarNotification> iter, final String pkg, final int userId) { |
| 308 | return new Iterator<StatusBarNotification>() { |
| 309 | StatusBarNotification mNext = findNext(); |
| 310 | |
| 311 | private StatusBarNotification findNext() { |
| 312 | while (iter.hasNext()) { |
| 313 | StatusBarNotification nr = iter.next(); |
| Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 314 | if ((pkg == null || nr.getPackageName() == pkg) |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 315 | && (userId == UserHandle.USER_ALL || nr.getUserId() == userId)) { |
| 316 | return nr; |
| 317 | } |
| 318 | } |
| 319 | return null; |
| 320 | } |
| 321 | |
| 322 | @Override |
| 323 | public boolean hasNext() { |
| 324 | return mNext == null; |
| 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public StatusBarNotification next() { |
| 329 | StatusBarNotification next = mNext; |
| 330 | if (next == null) { |
| 331 | throw new NoSuchElementException(); |
| 332 | } |
| 333 | mNext = findNext(); |
| 334 | return next; |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | public void remove() { |
| 339 | iter.remove(); |
| 340 | } |
| 341 | }; |
| 342 | } |
| Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 343 | |
| 344 | public StatusBarNotification[] getArray(int count) { |
| 345 | if (count == 0) count = Archive.BUFFER_SIZE; |
| 346 | final StatusBarNotification[] a |
| 347 | = new StatusBarNotification[Math.min(count, mBuffer.size())]; |
| 348 | Iterator<StatusBarNotification> iter = descendingIterator(); |
| 349 | int i=0; |
| 350 | while (iter.hasNext() && i < count) { |
| 351 | a[i++] = iter.next(); |
| 352 | } |
| 353 | return a; |
| 354 | } |
| 355 | |
| 356 | public StatusBarNotification[] getArray(int count, String pkg, int userId) { |
| 357 | if (count == 0) count = Archive.BUFFER_SIZE; |
| 358 | final StatusBarNotification[] a |
| 359 | = new StatusBarNotification[Math.min(count, mBuffer.size())]; |
| 360 | Iterator<StatusBarNotification> iter = filter(descendingIterator(), pkg, userId); |
| 361 | int i=0; |
| 362 | while (iter.hasNext() && i < count) { |
| 363 | a[i++] = iter.next(); |
| 364 | } |
| 365 | return a; |
| 366 | } |
| 367 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | Archive mArchive = new Archive(); |
| 371 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 372 | private void loadBlockDb() { |
| 373 | synchronized(mBlockedPackages) { |
| 374 | if (mPolicyFile == null) { |
| 375 | File dir = new File("/data/system"); |
| 376 | mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml")); |
| 377 | |
| 378 | mBlockedPackages.clear(); |
| 379 | |
| 380 | FileInputStream infile = null; |
| 381 | try { |
| 382 | infile = mPolicyFile.openRead(); |
| 383 | final XmlPullParser parser = Xml.newPullParser(); |
| 384 | parser.setInput(infile, null); |
| 385 | |
| 386 | int type; |
| 387 | String tag; |
| 388 | int version = DB_VERSION; |
| 389 | while ((type = parser.next()) != END_DOCUMENT) { |
| 390 | tag = parser.getName(); |
| 391 | if (type == START_TAG) { |
| 392 | if (TAG_BODY.equals(tag)) { |
| 393 | version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION)); |
| 394 | } else if (TAG_BLOCKED_PKGS.equals(tag)) { |
| 395 | while ((type = parser.next()) != END_DOCUMENT) { |
| 396 | tag = parser.getName(); |
| 397 | if (TAG_PACKAGE.equals(tag)) { |
| 398 | mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME)); |
| 399 | } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) { |
| 400 | break; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | } catch (FileNotFoundException e) { |
| 407 | // No data yet |
| 408 | } catch (IOException e) { |
| 409 | Log.wtf(TAG, "Unable to read blocked notifications database", e); |
| 410 | } catch (NumberFormatException e) { |
| 411 | Log.wtf(TAG, "Unable to parse blocked notifications database", e); |
| 412 | } catch (XmlPullParserException e) { |
| 413 | Log.wtf(TAG, "Unable to parse blocked notifications database", e); |
| 414 | } finally { |
| 415 | IoUtils.closeQuietly(infile); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 421 | /** |
| 422 | * Use this when you just want to know if notifications are OK for this package. |
| 423 | */ |
| 424 | public boolean areNotificationsEnabledForPackage(String pkg, int uid) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 425 | checkCallerIsSystem(); |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 426 | return (mAppOps.checkOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg) |
| 427 | == AppOpsManager.MODE_ALLOWED); |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 428 | } |
| 429 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 430 | /** Use this when you actually want to post a notification or toast. |
| 431 | * |
| 432 | * Unchecked. Not exposed via Binder, but can be called in the course of enqueue*(). |
| 433 | */ |
| 434 | private boolean noteNotificationOp(String pkg, int uid) { |
| 435 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg) |
| 436 | != AppOpsManager.MODE_ALLOWED) { |
| 437 | Slog.v(TAG, "notifications are disabled by AppOps for " + pkg); |
| 438 | return false; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 439 | } |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 440 | return true; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 441 | } |
| 442 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 443 | public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 444 | checkCallerIsSystem(); |
| Daniel Sandler | 32e698b | 2013-04-18 10:51:35 -0400 | [diff] [blame] | 445 | |
| 446 | Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg); |
| 447 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 448 | mAppOps.setMode(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg, |
| 449 | enabled ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED); |
| Daniel Sandler | 32e698b | 2013-04-18 10:51:35 -0400 | [diff] [blame] | 450 | |
| 451 | // Now, cancel any outstanding notifications that are part of a just-disabled app |
| 452 | if (ENABLE_BLOCKED_NOTIFICATIONS && !enabled) { |
| 453 | cancelAllNotificationsInt(pkg, 0, 0, true, UserHandle.getUserId(uid)); |
| 454 | } |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | private static String idDebugString(Context baseContext, String packageName, int id) { |
| 459 | Context c = null; |
| 460 | |
| 461 | if (packageName != null) { |
| 462 | try { |
| 463 | c = baseContext.createPackageContext(packageName, 0); |
| 464 | } catch (NameNotFoundException e) { |
| 465 | c = baseContext; |
| 466 | } |
| 467 | } else { |
| 468 | c = baseContext; |
| 469 | } |
| 470 | |
| 471 | String pkg; |
| 472 | String type; |
| 473 | String name; |
| 474 | |
| 475 | Resources r = c.getResources(); |
| 476 | try { |
| 477 | return r.getResourceName(id); |
| 478 | } catch (Resources.NotFoundException e) { |
| 479 | return "<name unknown>"; |
| 480 | } |
| 481 | } |
| 482 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 483 | /** |
| 484 | * System-only API for getting a list of current (i.e. not cleared) notifications. |
| 485 | * |
| 486 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
| 487 | */ |
| 488 | @Override |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 489 | public StatusBarNotification[] getActiveNotifications(String callingPkg) { |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 490 | // enforce() will ensure the calling uid has the correct permission |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 491 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 492 | "NotificationManagerService.getActiveNotifications"); |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 493 | |
| 494 | StatusBarNotification[] tmp = null; |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 495 | int uid = Binder.getCallingUid(); |
| 496 | |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 497 | // noteOp will check to make sure the callingPkg matches the uid |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 498 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 499 | == AppOpsManager.MODE_ALLOWED) { |
| 500 | synchronized (mNotificationList) { |
| 501 | tmp = new StatusBarNotification[mNotificationList.size()]; |
| 502 | final int N = mNotificationList.size(); |
| 503 | for (int i=0; i<N; i++) { |
| 504 | tmp[i] = mNotificationList.get(i).sbn; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | return tmp; |
| 509 | } |
| 510 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 511 | /** |
| 512 | * System-only API for getting a list of recent (cleared, no longer shown) notifications. |
| 513 | * |
| 514 | * Requires ACCESS_NOTIFICATIONS which is signature|system. |
| 515 | */ |
| 516 | @Override |
| Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 517 | public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) { |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 518 | // enforce() will ensure the calling uid has the correct permission |
| Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 519 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS, |
| 520 | "NotificationManagerService.getHistoricalNotifications"); |
| 521 | |
| 522 | StatusBarNotification[] tmp = null; |
| 523 | int uid = Binder.getCallingUid(); |
| 524 | |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 525 | // noteOp will check to make sure the callingPkg matches the uid |
| Daniel Sandler | 78d0d25 | 2013-02-12 08:14:52 -0500 | [diff] [blame] | 526 | if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg) |
| 527 | == AppOpsManager.MODE_ALLOWED) { |
| 528 | synchronized (mArchive) { |
| 529 | tmp = mArchive.getArray(count); |
| 530 | } |
| 531 | } |
| 532 | return tmp; |
| 533 | } |
| 534 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 535 | /** |
| 536 | * Called whenever packages change, the user switches, or ENABLED_NOTIFICATION_LISTENERS |
| 537 | * is altered. (For example in response to USER_SWITCHED in our broadcast receiver) |
| 538 | */ |
| 539 | void rebindListenerServices() { |
| John Spurlock | 3ec4e70 | 2013-05-16 13:42:38 -0400 | [diff] [blame] | 540 | final int currentUser = ActivityManager.getCurrentUser(); |
| 541 | String flat = Settings.Secure.getStringForUser( |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 542 | mContext.getContentResolver(), |
| John Spurlock | 3ec4e70 | 2013-05-16 13:42:38 -0400 | [diff] [blame] | 543 | Settings.Secure.ENABLED_NOTIFICATION_LISTENERS, |
| 544 | currentUser); |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 545 | |
| 546 | NotificationListenerInfo[] toRemove = new NotificationListenerInfo[mListeners.size()]; |
| 547 | final ArrayList<ComponentName> toAdd; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 548 | |
| 549 | synchronized (mNotificationList) { |
| 550 | // unbind and remove all existing listeners |
| 551 | toRemove = mListeners.toArray(toRemove); |
| 552 | |
| 553 | toAdd = new ArrayList<ComponentName>(); |
| 554 | final HashSet<ComponentName> newEnabled = new HashSet<ComponentName>(); |
| 555 | final HashSet<String> newPackages = new HashSet<String>(); |
| 556 | |
| 557 | // decode the list of components |
| 558 | if (flat != null) { |
| 559 | String[] components = flat.split(ENABLED_NOTIFICATION_LISTENERS_SEPARATOR); |
| 560 | for (int i=0; i<components.length; i++) { |
| 561 | final ComponentName component |
| 562 | = ComponentName.unflattenFromString(components[i]); |
| 563 | if (component != null) { |
| 564 | newEnabled.add(component); |
| 565 | toAdd.add(component); |
| 566 | newPackages.add(component.getPackageName()); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | mEnabledListenersForCurrentUser = newEnabled; |
| 571 | mEnabledListenerPackageNames = newPackages; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | for (NotificationListenerInfo info : toRemove) { |
| 576 | final ComponentName component = info.component; |
| 577 | final int oldUser = info.userid; |
| 578 | Slog.v(TAG, "disabling notification listener for user " + oldUser + ": " + component); |
| 579 | unregisterListenerService(component, info.userid); |
| 580 | } |
| 581 | |
| 582 | final int N = toAdd.size(); |
| 583 | for (int i=0; i<N; i++) { |
| 584 | final ComponentName component = toAdd.get(i); |
| 585 | Slog.v(TAG, "enabling notification listener for user " + currentUser + ": " |
| 586 | + component); |
| 587 | registerListenerService(component, currentUser); |
| 588 | } |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 589 | } |
| 590 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 591 | /** |
| 592 | * Register a listener binder directly with the notification manager. |
| 593 | * |
| 594 | * Only works with system callers. Apps should extend |
| 595 | * {@link android.service.notification.NotificationListenerService}. |
| 596 | */ |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 597 | @Override |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 598 | public void registerListener(final INotificationListener listener, |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 599 | final ComponentName component, final int userid) { |
| 600 | checkCallerIsSystem(); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 601 | |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 602 | synchronized (mNotificationList) { |
| 603 | try { |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 604 | NotificationListenerInfo info |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 605 | = new NotificationListenerInfo(listener, component, userid, true); |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 606 | listener.asBinder().linkToDeath(info, 0); |
| 607 | mListeners.add(info); |
| 608 | } catch (RemoteException e) { |
| 609 | // already dead |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 614 | /** |
| 615 | * Version of registerListener that takes the name of a |
| 616 | * {@link android.service.notification.NotificationListenerService} to bind to. |
| 617 | * |
| 618 | * This is the mechanism by which third parties may subscribe to notifications. |
| 619 | */ |
| 620 | private void registerListenerService(final ComponentName name, final int userid) { |
| 621 | checkCallerIsSystem(); |
| 622 | |
| 623 | if (DBG) Slog.v(TAG, "registerListenerService: " + name + " u=" + userid); |
| 624 | |
| 625 | synchronized (mNotificationList) { |
| 626 | final String servicesBindingTag = name.toString() + "/" + userid; |
| 627 | if (mServicesBinding.contains(servicesBindingTag)) { |
| 628 | // stop registering this thing already! we're working on it |
| 629 | return; |
| 630 | } |
| 631 | mServicesBinding.add(servicesBindingTag); |
| 632 | |
| 633 | final int N = mListeners.size(); |
| 634 | for (int i=N-1; i>=0; i--) { |
| 635 | final NotificationListenerInfo info = mListeners.get(i); |
| 636 | if (name.equals(info.component) |
| 637 | && info.userid == userid) { |
| 638 | // cut old connections |
| 639 | if (DBG) Slog.v(TAG, " disconnecting old listener: " + info.listener); |
| 640 | mListeners.remove(i); |
| 641 | if (info.connection != null) { |
| 642 | mContext.unbindService(info.connection); |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | Intent intent = new Intent(NotificationListenerService.SERVICE_INTERFACE); |
| 648 | intent.setComponent(name); |
| 649 | |
| 650 | intent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 651 | com.android.internal.R.string.notification_listener_binding_label); |
| 652 | intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity( |
| 653 | mContext, 0, new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS), 0)); |
| 654 | |
| 655 | try { |
| 656 | if (DBG) Slog.v(TAG, "binding: " + intent); |
| 657 | if (!mContext.bindServiceAsUser(intent, |
| 658 | new ServiceConnection() { |
| 659 | INotificationListener mListener; |
| 660 | @Override |
| 661 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 662 | synchronized (mNotificationList) { |
| 663 | mServicesBinding.remove(servicesBindingTag); |
| 664 | try { |
| 665 | mListener = INotificationListener.Stub.asInterface(service); |
| 666 | NotificationListenerInfo info = new NotificationListenerInfo( |
| 667 | mListener, name, userid, this); |
| 668 | service.linkToDeath(info, 0); |
| 669 | mListeners.add(info); |
| 670 | } catch (RemoteException e) { |
| 671 | // already dead |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | @Override |
| 677 | public void onServiceDisconnected(ComponentName name) { |
| 678 | Slog.v(TAG, "notification listener connection lost: " + name); |
| 679 | } |
| 680 | }, |
| 681 | Context.BIND_AUTO_CREATE, |
| 682 | new UserHandle(userid))) |
| 683 | { |
| 684 | mServicesBinding.remove(servicesBindingTag); |
| 685 | Slog.w(TAG, "Unable to bind listener service: " + intent); |
| 686 | return; |
| 687 | } |
| 688 | } catch (SecurityException ex) { |
| 689 | Slog.e(TAG, "Unable to bind listener service: " + intent, ex); |
| 690 | return; |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * Remove a listener binder directly |
| 697 | */ |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 698 | @Override |
| 699 | public void unregisterListener(INotificationListener listener, int userid) { |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 700 | // no need to check permissions; if your listener binder is in the list, |
| 701 | // that's proof that you had permission to add it in the first place |
| 702 | |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 703 | synchronized (mNotificationList) { |
| 704 | final int N = mListeners.size(); |
| 705 | for (int i=N-1; i>=0; i--) { |
| 706 | final NotificationListenerInfo info = mListeners.get(i); |
| 707 | if (info.listener == listener && info.userid == userid) { |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 708 | mListeners.remove(i); |
| 709 | if (info.connection != null) { |
| 710 | mContext.unbindService(info.connection); |
| 711 | } |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 717 | /** |
| 718 | * Remove a listener service for the given user by ComponentName |
| 719 | */ |
| 720 | private void unregisterListenerService(ComponentName name, int userid) { |
| 721 | checkCallerIsSystem(); |
| 722 | |
| 723 | synchronized (mNotificationList) { |
| 724 | final int N = mListeners.size(); |
| 725 | for (int i=N-1; i>=0; i--) { |
| 726 | final NotificationListenerInfo info = mListeners.get(i); |
| 727 | if (name.equals(info.component) |
| 728 | && info.userid == userid) { |
| 729 | mListeners.remove(i); |
| 730 | if (info.connection != null) { |
| Daniel Sandler | c1b49bd | 2013-05-07 13:53:47 -0400 | [diff] [blame] | 731 | try { |
| 732 | mContext.unbindService(info.connection); |
| 733 | } catch (IllegalArgumentException ex) { |
| 734 | // something happened to the service: we think we have a connection |
| 735 | // but it's bogus. |
| 736 | Slog.e(TAG, "Listener " + name + " could not be unbound: " + ex); |
| 737 | } |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | /** |
| 745 | * asynchronously notify all listeners about a new notification |
| 746 | */ |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 747 | private void notifyPostedLocked(NotificationRecord n) { |
| Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 748 | // make a copy in case changes are made to the underlying Notification object |
| 749 | final StatusBarNotification sbn = n.sbn.clone(); |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 750 | for (final NotificationListenerInfo info : mListeners) { |
| 751 | mHandler.post(new Runnable() { |
| 752 | @Override |
| 753 | public void run() { |
| 754 | info.notifyPostedIfUserMatch(sbn); |
| 755 | }}); |
| 756 | } |
| 757 | } |
| 758 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 759 | /** |
| 760 | * asynchronously notify all listeners about a removed notification |
| 761 | */ |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 762 | private void notifyRemovedLocked(NotificationRecord n) { |
| Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 763 | // make a copy in case changes are made to the underlying Notification object |
| 764 | // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the notification |
| 765 | final StatusBarNotification sbn_light = n.sbn.cloneLight(); |
| 766 | |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 767 | for (final NotificationListenerInfo info : mListeners) { |
| 768 | mHandler.post(new Runnable() { |
| 769 | @Override |
| 770 | public void run() { |
| Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 771 | info.notifyRemovedIfUserMatch(sbn_light); |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 772 | }}); |
| 773 | } |
| 774 | } |
| 775 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 776 | // -- APIs to support listeners clicking/clearing notifications -- |
| 777 | |
| 778 | private NotificationListenerInfo checkListenerToken(INotificationListener listener) { |
| 779 | final IBinder token = listener.asBinder(); |
| 780 | final int N = mListeners.size(); |
| 781 | for (int i=0; i<N; i++) { |
| 782 | final NotificationListenerInfo info = mListeners.get(i); |
| 783 | if (info.listener.asBinder() == token) return info; |
| 784 | } |
| 785 | throw new SecurityException("Disallowed call from unknown listener: " + listener); |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * Allow an INotificationListener to simulate a "clear all" operation. |
| 790 | * |
| 791 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications} |
| 792 | * |
| 793 | * @param token The binder for the listener, to check that the caller is allowed |
| 794 | */ |
| Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 795 | public void cancelAllNotificationsFromListener(INotificationListener token) { |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 796 | NotificationListenerInfo info = checkListenerToken(token); |
| 797 | long identity = Binder.clearCallingIdentity(); |
| 798 | try { |
| 799 | cancelAll(info.userid); |
| 800 | } finally { |
| 801 | Binder.restoreCallingIdentity(identity); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * Allow an INotificationListener to simulate clearing (dismissing) a single notification. |
| 807 | * |
| 808 | * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear} |
| 809 | * |
| 810 | * @param token The binder for the listener, to check that the caller is allowed |
| 811 | */ |
| Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 812 | public void cancelNotificationFromListener(INotificationListener token, String pkg, String tag, int id) { |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 813 | NotificationListenerInfo info = checkListenerToken(token); |
| 814 | long identity = Binder.clearCallingIdentity(); |
| 815 | try { |
| 816 | cancelNotification(pkg, tag, id, 0, |
| 817 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
| 818 | true, |
| 819 | info.userid); |
| 820 | } finally { |
| 821 | Binder.restoreCallingIdentity(identity); |
| 822 | } |
| 823 | } |
| 824 | |
| Daniel Sandler | 25cf8ce | 2013-04-24 15:34:57 -0400 | [diff] [blame] | 825 | /** |
| 826 | * Allow an INotificationListener to request the list of outstanding notifications seen by |
| 827 | * the current user. Useful when starting up, after which point the listener callbacks should |
| 828 | * be used. |
| 829 | * |
| 830 | * @param token The binder for the listener, to check that the caller is allowed |
| 831 | */ |
| 832 | public StatusBarNotification[] getActiveNotificationsFromListener(INotificationListener token) { |
| 833 | NotificationListenerInfo info = checkListenerToken(token); |
| 834 | |
| 835 | StatusBarNotification[] result = new StatusBarNotification[0]; |
| 836 | ArrayList<StatusBarNotification> list = new ArrayList<StatusBarNotification>(); |
| 837 | synchronized (mNotificationList) { |
| 838 | final int N = mNotificationList.size(); |
| 839 | for (int i=0; i<N; i++) { |
| 840 | StatusBarNotification sbn = mNotificationList.get(i).sbn; |
| 841 | if (info.enabledAndUserMatches(sbn)) { |
| 842 | list.add(sbn); |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | return list.toArray(result); |
| 847 | } |
| 848 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 849 | // -- end of listener APIs -- |
| 850 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 851 | public static final class NotificationRecord |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 853 | final StatusBarNotification sbn; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | IBinder statusBarKey; |
| 855 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 856 | NotificationRecord(StatusBarNotification sbn) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 858 | this.sbn = sbn; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 859 | } |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 860 | |
| Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 861 | public Notification getNotification() { return sbn.getNotification(); } |
| 862 | public int getFlags() { return sbn.getNotification().flags; } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 863 | public int getUserId() { return sbn.getUserId(); } |
| 864 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 865 | void dump(PrintWriter pw, String prefix, Context baseContext) { |
| Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 866 | final Notification notification = sbn.getNotification(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | pw.println(prefix + this); |
| Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 868 | pw.println(prefix + " uid=" + sbn.getUid() + " userId=" + sbn.getUserId()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon) |
| Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 870 | + " / " + idDebugString(baseContext, sbn.getPackageName(), notification.icon)); |
| Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 871 | pw.println(prefix + " pri=" + notification.priority + " score=" + sbn.getScore()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 872 | pw.println(prefix + " contentIntent=" + notification.contentIntent); |
| 873 | pw.println(prefix + " deleteIntent=" + notification.deleteIntent); |
| 874 | pw.println(prefix + " tickerText=" + notification.tickerText); |
| 875 | pw.println(prefix + " contentView=" + notification.contentView); |
| Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 876 | pw.println(prefix + String.format(" defaults=0x%08x flags=0x%08x", |
| 877 | notification.defaults, notification.flags)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | pw.println(prefix + " sound=" + notification.sound); |
| 879 | pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate)); |
| Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 880 | pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d", |
| 881 | notification.ledARGB, notification.ledOnMS, notification.ledOffMS)); |
| 882 | if (notification.actions != null && notification.actions.length > 0) { |
| 883 | pw.println(prefix + " actions={"); |
| 884 | final int N = notification.actions.length; |
| 885 | for (int i=0; i<N; i++) { |
| 886 | final Notification.Action action = notification.actions[i]; |
| 887 | pw.println(String.format("%s [%d] \"%s\" -> %s", |
| 888 | prefix, |
| 889 | i, |
| 890 | action.title, |
| 891 | action.actionIntent.toString() |
| 892 | )); |
| 893 | } |
| 894 | pw.println(prefix + " }"); |
| 895 | } |
| 896 | if (notification.extras != null && notification.extras.size() > 0) { |
| 897 | pw.println(prefix + " extras={"); |
| 898 | for (String key : notification.extras.keySet()) { |
| 899 | pw.print(prefix + " " + key + "="); |
| 900 | Object val = notification.extras.get(key); |
| 901 | if (val == null) { |
| 902 | pw.println("null"); |
| 903 | } else { |
| 904 | pw.print(val.toString()); |
| 905 | if (val instanceof Bitmap) { |
| 906 | pw.print(String.format(" (%dx%d)", |
| 907 | ((Bitmap) val).getWidth(), |
| 908 | ((Bitmap) val).getHeight())); |
| 909 | } else if (val.getClass().isArray()) { |
| 910 | pw.println(" {"); |
| 911 | final int N = Array.getLength(val); |
| 912 | for (int i=0; i<N; i++) { |
| 913 | if (i > 0) pw.println(","); |
| 914 | pw.print(prefix + " " + Array.get(val, i)); |
| 915 | } |
| 916 | pw.print("\n" + prefix + " }"); |
| 917 | } |
| 918 | pw.println(); |
| 919 | } |
| 920 | } |
| 921 | pw.println(prefix + " }"); |
| 922 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 923 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 924 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | @Override |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 926 | public final String toString() { |
| 927 | return String.format( |
| 928 | "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s score=%d: %s)", |
| 929 | System.identityHashCode(this), |
| Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 930 | this.sbn.getPackageName(), this.sbn.getUser(), this.sbn.getId(), this.sbn.getTag(), |
| Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 931 | this.sbn.getScore(), this.sbn.getNotification()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
| 935 | private static final class ToastRecord |
| 936 | { |
| 937 | final int pid; |
| 938 | final String pkg; |
| 939 | final ITransientNotification callback; |
| 940 | int duration; |
| 941 | |
| 942 | ToastRecord(int pid, String pkg, ITransientNotification callback, int duration) |
| 943 | { |
| 944 | this.pid = pid; |
| 945 | this.pkg = pkg; |
| 946 | this.callback = callback; |
| 947 | this.duration = duration; |
| 948 | } |
| 949 | |
| 950 | void update(int duration) { |
| 951 | this.duration = duration; |
| 952 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 953 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | void dump(PrintWriter pw, String prefix) { |
| 955 | pw.println(prefix + this); |
| 956 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 957 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | @Override |
| 959 | public final String toString() |
| 960 | { |
| 961 | return "ToastRecord{" |
| 962 | + Integer.toHexString(System.identityHashCode(this)) |
| 963 | + " pkg=" + pkg |
| 964 | + " callback=" + callback |
| 965 | + " duration=" + duration; |
| 966 | } |
| 967 | } |
| 968 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 969 | private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks |
| 970 | = new StatusBarManagerService.NotificationCallbacks() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | |
| 972 | public void onSetDisabled(int status) { |
| 973 | synchronized (mNotificationList) { |
| 974 | mDisabledNotifications = status; |
| 975 | if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) { |
| 976 | // cancel whatever's going on |
| 977 | long identity = Binder.clearCallingIdentity(); |
| 978 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 979 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 980 | if (player != null) { |
| 981 | player.stopAsync(); |
| 982 | } |
| 983 | } catch (RemoteException e) { |
| 984 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | Binder.restoreCallingIdentity(identity); |
| 986 | } |
| 987 | |
| 988 | identity = Binder.clearCallingIdentity(); |
| 989 | try { |
| 990 | mVibrator.cancel(); |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 991 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | Binder.restoreCallingIdentity(identity); |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | public void onClearAll() { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 999 | // XXX to be totally correct, the caller should tell us which user |
| 1000 | // this is for. |
| 1001 | cancelAll(ActivityManager.getCurrentUser()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1002 | } |
| 1003 | |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1004 | public void onNotificationClick(String pkg, String tag, int id) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1005 | // XXX to be totally correct, the caller should tell us which user |
| 1006 | // this is for. |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1007 | cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1008 | Notification.FLAG_FOREGROUND_SERVICE, false, |
| 1009 | ActivityManager.getCurrentUser()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | } |
| 1011 | |
| Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 1012 | public void onNotificationClear(String pkg, String tag, int id) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1013 | // XXX to be totally correct, the caller should tell us which user |
| 1014 | // this is for. |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1015 | cancelNotification(pkg, tag, id, 0, |
| 1016 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1017 | true, ActivityManager.getCurrentUser()); |
| Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 1018 | } |
| 1019 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | public void onPanelRevealed() { |
| 1021 | synchronized (mNotificationList) { |
| 1022 | // sound |
| 1023 | mSoundNotification = null; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1024 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1025 | long identity = Binder.clearCallingIdentity(); |
| 1026 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1027 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 1028 | if (player != null) { |
| 1029 | player.stopAsync(); |
| 1030 | } |
| 1031 | } catch (RemoteException e) { |
| 1032 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1033 | Binder.restoreCallingIdentity(identity); |
| 1034 | } |
| 1035 | |
| 1036 | // vibrate |
| 1037 | mVibrateNotification = null; |
| 1038 | identity = Binder.clearCallingIdentity(); |
| 1039 | try { |
| 1040 | mVibrator.cancel(); |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1041 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | Binder.restoreCallingIdentity(identity); |
| 1043 | } |
| 1044 | |
| 1045 | // light |
| 1046 | mLights.clear(); |
| 1047 | mLedNotification = null; |
| 1048 | updateLightsLocked(); |
| 1049 | } |
| 1050 | } |
| Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 1051 | |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 1052 | public void onNotificationError(String pkg, String tag, int id, |
| 1053 | int uid, int initialPid, String message) { |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 1054 | Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id |
| 1055 | + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")"); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1056 | // XXX to be totally correct, the caller should tell us which user |
| 1057 | // this is for. |
| 1058 | cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid)); |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 1059 | long ident = Binder.clearCallingIdentity(); |
| 1060 | try { |
| 1061 | ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg, |
| 1062 | "Bad notification posted from package " + pkg |
| 1063 | + ": " + message); |
| 1064 | } catch (RemoteException e) { |
| 1065 | } |
| 1066 | Binder.restoreCallingIdentity(ident); |
| Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 1067 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | }; |
| 1069 | |
| 1070 | private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 1071 | @Override |
| 1072 | public void onReceive(Context context, Intent intent) { |
| 1073 | String action = intent.getAction(); |
| 1074 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1075 | boolean queryRestart = false; |
| Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1076 | boolean packageChanged = false; |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1077 | |
| Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1078 | if (action.equals(Intent.ACTION_PACKAGE_ADDED) |
| 1079 | || action.equals(Intent.ACTION_PACKAGE_REMOVED) |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1080 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
| Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1081 | || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED)) |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1082 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1083 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1084 | String pkgList[] = null; |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1085 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1086 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1087 | } else if (queryRestart) { |
| 1088 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1089 | } else { |
| 1090 | Uri uri = intent.getData(); |
| 1091 | if (uri == null) { |
| 1092 | return; |
| 1093 | } |
| 1094 | String pkgName = uri.getSchemeSpecificPart(); |
| 1095 | if (pkgName == null) { |
| 1096 | return; |
| 1097 | } |
| Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 1098 | if (packageChanged) { |
| 1099 | // We cancel notifications for packages which have just been disabled |
| 1100 | final int enabled = mContext.getPackageManager() |
| 1101 | .getApplicationEnabledSetting(pkgName); |
| 1102 | if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 1103 | || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 1104 | return; |
| 1105 | } |
| 1106 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1107 | pkgList = new String[]{pkgName}; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | } |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1109 | |
| 1110 | boolean anyListenersInvolved = false; |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1111 | if (pkgList != null && (pkgList.length > 0)) { |
| 1112 | for (String pkgName : pkgList) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1113 | cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart, |
| 1114 | UserHandle.USER_ALL); |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1115 | if (mEnabledListenerPackageNames.contains(pkgName)) { |
| 1116 | anyListenersInvolved = true; |
| 1117 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1118 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1119 | } |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1120 | |
| 1121 | if (anyListenersInvolved) { |
| 1122 | // make sure we're still bound to any of our |
| 1123 | // listeners who may have just upgraded |
| 1124 | rebindListenerServices(); |
| 1125 | } |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1126 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
| 1127 | // Keep track of screen on/off state, but do not turn off the notification light |
| 1128 | // until user passes through the lock screen or views the notification. |
| 1129 | mScreenOn = true; |
| 1130 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 1131 | mScreenOn = false; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1132 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1133 | mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals( |
| 1134 | TelephonyManager.EXTRA_STATE_OFFHOOK)); |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1135 | updateNotificationPulse(); |
| Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1136 | } else if (action.equals(Intent.ACTION_USER_STOPPED)) { |
| 1137 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 1138 | if (userHandle >= 0) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1139 | cancelAllNotificationsInt(null, 0, 0, true, userHandle); |
| Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1140 | } |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1141 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { |
| 1142 | // turn off LED when user passes through lock screen |
| 1143 | mNotificationLight.turnOff(); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1144 | } else if (action.equals(Intent.ACTION_USER_SWITCHED)) { |
| 1145 | // reload per-user settings |
| 1146 | mSettingsObserver.update(null); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1147 | } |
| 1148 | } |
| 1149 | }; |
| 1150 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1151 | class SettingsObserver extends ContentObserver { |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1152 | private final Uri NOTIFICATION_LIGHT_PULSE_URI |
| 1153 | = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE); |
| 1154 | |
| 1155 | private final Uri ENABLED_NOTIFICATION_LISTENERS_URI |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1156 | = Settings.Secure.getUriFor(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1157 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1158 | SettingsObserver(Handler handler) { |
| 1159 | super(handler); |
| 1160 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1161 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1162 | void observe() { |
| 1163 | ContentResolver resolver = mContext.getContentResolver(); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1164 | resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI, |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1165 | false, this, UserHandle.USER_ALL); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1166 | resolver.registerContentObserver(ENABLED_NOTIFICATION_LISTENERS_URI, |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1167 | false, this, UserHandle.USER_ALL); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1168 | update(null); |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1171 | @Override public void onChange(boolean selfChange, Uri uri) { |
| 1172 | update(uri); |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1175 | public void update(Uri uri) { |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1176 | ContentResolver resolver = mContext.getContentResolver(); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1177 | if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) { |
| 1178 | boolean pulseEnabled = Settings.System.getInt(resolver, |
| 1179 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0; |
| 1180 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 1181 | mNotificationPulseEnabled = pulseEnabled; |
| 1182 | updateNotificationPulse(); |
| 1183 | } |
| 1184 | } |
| 1185 | if (uri == null || ENABLED_NOTIFICATION_LISTENERS_URI.equals(uri)) { |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1186 | rebindListenerServices(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1187 | } |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1188 | } |
| 1189 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1190 | |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1191 | private SettingsObserver mSettingsObserver; |
| 1192 | |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1193 | static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) { |
| 1194 | int[] ar = r.getIntArray(resid); |
| 1195 | if (ar == null) { |
| 1196 | return def; |
| 1197 | } |
| 1198 | final int len = ar.length > maxlen ? maxlen : ar.length; |
| 1199 | long[] out = new long[len]; |
| 1200 | for (int i=0; i<len; i++) { |
| 1201 | out[i] = ar[i]; |
| 1202 | } |
| 1203 | return out; |
| 1204 | } |
| 1205 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 1206 | NotificationManagerService(Context context, StatusBarManagerService statusBar, |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 1207 | LightsService lights) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | { |
| 1209 | super(); |
| 1210 | mContext = context; |
| Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 1211 | mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1212 | mAm = ActivityManagerNative.getDefault(); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1213 | mUserManager = (UserManager)context.getSystemService(Context.USER_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1214 | mToastQueue = new ArrayList<ToastRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1215 | mHandler = new WorkerHandler(); |
| San Mehat | 3ee1317 | 2010-02-04 20:54:43 -0800 | [diff] [blame] | 1216 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1217 | mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE); |
| 1218 | |
| 1219 | importOldBlockDb(); |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1220 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 1221 | mStatusBar = statusBar; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1222 | statusBar.setNotificationCallbacks(mNotificationCallbacks); |
| 1223 | |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1224 | mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS); |
| 1225 | mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION); |
| 1226 | |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 1227 | Resources resources = mContext.getResources(); |
| 1228 | mDefaultNotificationColor = resources.getColor( |
| 1229 | com.android.internal.R.color.config_defaultNotificationColor); |
| 1230 | mDefaultNotificationLedOn = resources.getInteger( |
| 1231 | com.android.internal.R.integer.config_defaultNotificationLedOn); |
| 1232 | mDefaultNotificationLedOff = resources.getInteger( |
| 1233 | com.android.internal.R.integer.config_defaultNotificationLedOff); |
| 1234 | |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1235 | mDefaultVibrationPattern = getLongArray(resources, |
| 1236 | com.android.internal.R.array.config_defaultNotificationVibePattern, |
| 1237 | VIBRATE_PATTERN_MAXLEN, |
| 1238 | DEFAULT_VIBRATE_PATTERN); |
| 1239 | |
| 1240 | mFallbackVibrationPattern = getLongArray(resources, |
| 1241 | com.android.internal.R.array.config_notificationFallbackVibePattern, |
| 1242 | VIBRATE_PATTERN_MAXLEN, |
| 1243 | DEFAULT_VIBRATE_PATTERN); |
| 1244 | |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1245 | // Don't start allowing notifications until the setup wizard has run once. |
| 1246 | // After that, including subsequent boots, init with notifications turned on. |
| 1247 | // This works on the first boot because the setup wizard will toggle this |
| 1248 | // flag at least once and we'll go back to 0 after that. |
| Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1249 | if (0 == Settings.Global.getInt(mContext.getContentResolver(), |
| 1250 | Settings.Global.DEVICE_PROVISIONED, 0)) { |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1251 | mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS; |
| 1252 | } |
| 1253 | |
| Mike Lockwood | 35e16bf | 2010-11-30 19:53:36 -0500 | [diff] [blame] | 1254 | // register for various Intents |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | IntentFilter filter = new IntentFilter(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1256 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 1257 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1258 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1259 | filter.addAction(Intent.ACTION_USER_PRESENT); |
| Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1260 | filter.addAction(Intent.ACTION_USER_STOPPED); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1261 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1262 | mContext.registerReceiver(mIntentReceiver, filter); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1263 | IntentFilter pkgFilter = new IntentFilter(); |
| Chris Wren | 3da7302 | 2013-05-10 14:41:21 -0400 | [diff] [blame] | 1264 | pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1265 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| Daniel Sandler | aac0eb0 | 2011-08-06 22:51:56 -0400 | [diff] [blame] | 1266 | pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1267 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 1268 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 1269 | pkgFilter.addDataScheme("package"); |
| 1270 | mContext.registerReceiver(mIntentReceiver, pkgFilter); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 1271 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 1272 | mContext.registerReceiver(mIntentReceiver, sdFilter); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1273 | |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 1274 | mSettingsObserver = new SettingsObserver(mHandler); |
| 1275 | mSettingsObserver.observe(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1276 | } |
| 1277 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1278 | /** |
| 1279 | * Read the old XML-based app block database and import those blockages into the AppOps system. |
| 1280 | */ |
| 1281 | private void importOldBlockDb() { |
| 1282 | loadBlockDb(); |
| 1283 | |
| 1284 | PackageManager pm = mContext.getPackageManager(); |
| 1285 | for (String pkg : mBlockedPackages) { |
| 1286 | PackageInfo info = null; |
| 1287 | try { |
| 1288 | info = pm.getPackageInfo(pkg, 0); |
| 1289 | setNotificationsEnabledForPackage(pkg, info.applicationInfo.uid, false); |
| 1290 | } catch (NameNotFoundException e) { |
| 1291 | // forget you |
| 1292 | } |
| 1293 | } |
| 1294 | mBlockedPackages.clear(); |
| 1295 | if (mPolicyFile != null) { |
| 1296 | mPolicyFile.delete(); |
| 1297 | } |
| 1298 | } |
| 1299 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1300 | void systemReady() { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1301 | mAudioService = IAudioService.Stub.asInterface( |
| 1302 | ServiceManager.getService(Context.AUDIO_SERVICE)); |
| 1303 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1304 | // no beeping until we're basically done booting |
| 1305 | mSystemReady = true; |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 1306 | |
| 1307 | // make sure our listener services are properly bound |
| 1308 | rebindListenerServices(); |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1311 | // Toasts |
| 1312 | // ============================================================================ |
| 1313 | public void enqueueToast(String pkg, ITransientNotification callback, int duration) |
| 1314 | { |
| Daniel Sandler | a703590 | 2010-03-30 15:45:31 -0400 | [diff] [blame] | 1315 | if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1316 | |
| 1317 | if (pkg == null || callback == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1318 | Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1319 | return ; |
| 1320 | } |
| 1321 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1322 | final boolean isSystemToast = ("android".equals(pkg)); |
| 1323 | |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1324 | if (ENABLE_BLOCKED_TOASTS && !noteNotificationOp(pkg, Binder.getCallingUid())) { |
| 1325 | if (!isSystemToast) { |
| 1326 | Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request."); |
| 1327 | return; |
| 1328 | } |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1329 | } |
| 1330 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1331 | synchronized (mToastQueue) { |
| 1332 | int callingPid = Binder.getCallingPid(); |
| 1333 | long callingId = Binder.clearCallingIdentity(); |
| 1334 | try { |
| 1335 | ToastRecord record; |
| 1336 | int index = indexOfToastLocked(pkg, callback); |
| 1337 | // If it's already in the queue, we update it in place, we don't |
| 1338 | // move it to the end of the queue. |
| 1339 | if (index >= 0) { |
| 1340 | record = mToastQueue.get(index); |
| 1341 | record.update(duration); |
| 1342 | } else { |
| Vairavan Srinivasan | f9eb06c | 2011-01-21 18:08:36 -0800 | [diff] [blame] | 1343 | // Limit the number of toasts that any given package except the android |
| 1344 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1345 | if (!isSystemToast) { |
| Vairavan Srinivasan | f9eb06c | 2011-01-21 18:08:36 -0800 | [diff] [blame] | 1346 | int count = 0; |
| 1347 | final int N = mToastQueue.size(); |
| 1348 | for (int i=0; i<N; i++) { |
| 1349 | final ToastRecord r = mToastQueue.get(i); |
| 1350 | if (r.pkg.equals(pkg)) { |
| 1351 | count++; |
| 1352 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 1353 | Slog.e(TAG, "Package has already posted " + count |
| 1354 | + " toasts. Not showing more. Package=" + pkg); |
| 1355 | return; |
| 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | } |
| 1360 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1361 | record = new ToastRecord(callingPid, pkg, callback, duration); |
| 1362 | mToastQueue.add(record); |
| 1363 | index = mToastQueue.size() - 1; |
| 1364 | keepProcessAliveLocked(callingPid); |
| 1365 | } |
| 1366 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 1367 | // new or just been updated. Call back and tell it to show itself. |
| 1368 | // If the callback fails, this will remove it from the list, so don't |
| 1369 | // assume that it's valid after this. |
| 1370 | if (index == 0) { |
| 1371 | showNextToastLocked(); |
| 1372 | } |
| 1373 | } finally { |
| 1374 | Binder.restoreCallingIdentity(callingId); |
| 1375 | } |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | public void cancelToast(String pkg, ITransientNotification callback) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1380 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1381 | |
| 1382 | if (pkg == null || callback == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1383 | Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1384 | return ; |
| 1385 | } |
| 1386 | |
| 1387 | synchronized (mToastQueue) { |
| 1388 | long callingId = Binder.clearCallingIdentity(); |
| 1389 | try { |
| 1390 | int index = indexOfToastLocked(pkg, callback); |
| 1391 | if (index >= 0) { |
| 1392 | cancelToastLocked(index); |
| 1393 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1394 | Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1395 | } |
| 1396 | } finally { |
| 1397 | Binder.restoreCallingIdentity(callingId); |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | private void showNextToastLocked() { |
| 1403 | ToastRecord record = mToastQueue.get(0); |
| 1404 | while (record != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1405 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1406 | try { |
| 1407 | record.callback.show(); |
| 1408 | scheduleTimeoutLocked(record, false); |
| 1409 | return; |
| 1410 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1411 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1412 | + " in package " + record.pkg); |
| 1413 | // remove it from the list and let the process die |
| 1414 | int index = mToastQueue.indexOf(record); |
| 1415 | if (index >= 0) { |
| 1416 | mToastQueue.remove(index); |
| 1417 | } |
| 1418 | keepProcessAliveLocked(record.pid); |
| 1419 | if (mToastQueue.size() > 0) { |
| 1420 | record = mToastQueue.get(0); |
| 1421 | } else { |
| 1422 | record = null; |
| 1423 | } |
| 1424 | } |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | private void cancelToastLocked(int index) { |
| 1429 | ToastRecord record = mToastQueue.get(index); |
| 1430 | try { |
| 1431 | record.callback.hide(); |
| 1432 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1433 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1434 | + " in package " + record.pkg); |
| 1435 | // don't worry about this, we're about to remove it from |
| 1436 | // the list anyway |
| 1437 | } |
| 1438 | mToastQueue.remove(index); |
| 1439 | keepProcessAliveLocked(record.pid); |
| 1440 | if (mToastQueue.size() > 0) { |
| 1441 | // Show the next one. If the callback fails, this will remove |
| 1442 | // it from the list, so don't assume that the list hasn't changed |
| 1443 | // after this point. |
| 1444 | showNextToastLocked(); |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | private void scheduleTimeoutLocked(ToastRecord r, boolean immediate) |
| 1449 | { |
| 1450 | Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r); |
| 1451 | long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY); |
| 1452 | mHandler.removeCallbacksAndMessages(r); |
| 1453 | mHandler.sendMessageDelayed(m, delay); |
| 1454 | } |
| 1455 | |
| 1456 | private void handleTimeout(ToastRecord record) |
| 1457 | { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1458 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1459 | synchronized (mToastQueue) { |
| 1460 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 1461 | if (index >= 0) { |
| 1462 | cancelToastLocked(index); |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | // lock on mToastQueue |
| 1468 | private int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 1469 | { |
| 1470 | IBinder cbak = callback.asBinder(); |
| 1471 | ArrayList<ToastRecord> list = mToastQueue; |
| 1472 | int len = list.size(); |
| 1473 | for (int i=0; i<len; i++) { |
| 1474 | ToastRecord r = list.get(i); |
| 1475 | if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) { |
| 1476 | return i; |
| 1477 | } |
| 1478 | } |
| 1479 | return -1; |
| 1480 | } |
| 1481 | |
| 1482 | // lock on mToastQueue |
| 1483 | private void keepProcessAliveLocked(int pid) |
| 1484 | { |
| 1485 | int toastCount = 0; // toasts from this pid |
| 1486 | ArrayList<ToastRecord> list = mToastQueue; |
| 1487 | int N = list.size(); |
| 1488 | for (int i=0; i<N; i++) { |
| 1489 | ToastRecord r = list.get(i); |
| 1490 | if (r.pid == pid) { |
| 1491 | toastCount++; |
| 1492 | } |
| 1493 | } |
| 1494 | try { |
| 1495 | mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0); |
| 1496 | } catch (RemoteException e) { |
| 1497 | // Shouldn't happen. |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | private final class WorkerHandler extends Handler |
| 1502 | { |
| 1503 | @Override |
| 1504 | public void handleMessage(Message msg) |
| 1505 | { |
| 1506 | switch (msg.what) |
| 1507 | { |
| 1508 | case MESSAGE_TIMEOUT: |
| 1509 | handleTimeout((ToastRecord)msg.obj); |
| 1510 | break; |
| 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | |
| 1516 | // Notifications |
| 1517 | // ============================================================================ |
| Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1518 | public void enqueueNotificationWithTag(String pkg, String basePkg, String tag, int id, |
| 1519 | Notification notification, int[] idOut, int userId) |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1520 | { |
| Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1521 | enqueueNotificationInternal(pkg, basePkg, Binder.getCallingUid(), Binder.getCallingPid(), |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1522 | tag, id, notification, idOut, userId); |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 1523 | } |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1524 | |
| 1525 | private final static int clamp(int x, int low, int high) { |
| 1526 | return (x < low) ? low : ((x > high) ? high : x); |
| Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 1527 | } |
| 1528 | |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 1529 | // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the |
| 1530 | // uid/pid of another application) |
| Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1531 | public void enqueueNotificationInternal(String pkg, String basePkg, int callingUid, |
| 1532 | int callingPid, String tag, int id, Notification notification, int[] idOut, int userId) |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 1533 | { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1534 | if (DBG) { |
| 1535 | Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification); |
| 1536 | } |
| 1537 | checkCallerIsSystemOrSameApp(pkg); |
| 1538 | final boolean isSystemNotification = ("android".equals(pkg)); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1539 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1540 | userId = ActivityManager.handleIncomingUser(callingPid, |
| Amith Yamasani | 2c7ebea | 2012-10-30 15:28:27 -0700 | [diff] [blame] | 1541 | callingUid, userId, true, false, "enqueueNotification", pkg); |
| Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 1542 | final UserHandle user = new UserHandle(userId); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1543 | |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 1544 | // Limit the number of notifications that any given package except the android |
| 1545 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1546 | if (!isSystemNotification) { |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 1547 | synchronized (mNotificationList) { |
| 1548 | int count = 0; |
| 1549 | final int N = mNotificationList.size(); |
| 1550 | for (int i=0; i<N; i++) { |
| 1551 | final NotificationRecord r = mNotificationList.get(i); |
| Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 1552 | if (r.sbn.getPackageName().equals(pkg) && r.sbn.getUserId() == userId) { |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 1553 | count++; |
| 1554 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 1555 | Slog.e(TAG, "Package has already posted " + count |
| 1556 | + " notifications. Not showing more. package=" + pkg); |
| 1557 | return; |
| 1558 | } |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1564 | // This conditional is a dirty hack to limit the logging done on |
| 1565 | // behalf of the download manager without affecting other apps. |
| 1566 | if (!pkg.equals("com.android.providers.downloads") |
| 1567 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1568 | EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, userId, |
| Daniel Sandler | b64cb88 | 2011-11-29 23:48:29 -0500 | [diff] [blame] | 1569 | notification.toString()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | if (pkg == null || notification == null) { |
| 1573 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 1574 | + " id=" + id + " notification=" + notification); |
| 1575 | } |
| 1576 | if (notification.icon != 0) { |
| 1577 | if (notification.contentView == null) { |
| 1578 | throw new IllegalArgumentException("contentView required: pkg=" + pkg |
| 1579 | + " id=" + id + " notification=" + notification); |
| 1580 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1581 | } |
| 1582 | |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1583 | // === Scoring === |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1584 | |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1585 | // 0. Sanitize inputs |
| 1586 | notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX); |
| 1587 | // Migrate notification flags to scores |
| 1588 | if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) { |
| 1589 | if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX; |
| Daniel Sandler | 49a2ad1 | 2012-03-28 15:46:39 -0400 | [diff] [blame] | 1590 | } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) { |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1591 | if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH; |
| 1592 | } |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1593 | |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1594 | // 1. initial score: buckets of 10, around the app |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1595 | int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20] |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1596 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1597 | // 2. Consult external heuristics (TBD) |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1598 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1599 | // 3. Apply local rules |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1600 | |
| 1601 | // blocked apps |
| Daniel Sandler | 4a900acd | 2013-01-30 14:04:10 -0500 | [diff] [blame] | 1602 | if (ENABLE_BLOCKED_NOTIFICATIONS && !noteNotificationOp(pkg, callingUid)) { |
| 1603 | if (!isSystemNotification) { |
| 1604 | score = JUNK_SCORE; |
| 1605 | Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request."); |
| 1606 | } |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | if (DBG) { |
| 1610 | Slog.v(TAG, "Assigned score=" + score + " to " + notification); |
| 1611 | } |
| 1612 | |
| 1613 | if (score < SCORE_DISPLAY_THRESHOLD) { |
| 1614 | // Notification will be blocked because the score is too low. |
| 1615 | return; |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1616 | } |
| 1617 | |
| Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 1618 | // Should this notification make noise, vibe, or use the LED? |
| 1619 | final boolean canInterrupt = (score >= SCORE_INTERRUPTION_THRESHOLD); |
| 1620 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1621 | synchronized (mNotificationList) { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1622 | final StatusBarNotification n = new StatusBarNotification( |
| 1623 | pkg, id, tag, callingUid, callingPid, score, notification, user); |
| 1624 | NotificationRecord r = new NotificationRecord(n); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1625 | NotificationRecord old = null; |
| 1626 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1627 | int index = indexOfNotificationLocked(pkg, tag, id, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1628 | if (index < 0) { |
| 1629 | mNotificationList.add(r); |
| 1630 | } else { |
| 1631 | old = mNotificationList.remove(index); |
| 1632 | mNotificationList.add(index, r); |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1633 | // Make sure we don't lose the foreground service state. |
| 1634 | if (old != null) { |
| 1635 | notification.flags |= |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1636 | old.getNotification().flags&Notification.FLAG_FOREGROUND_SERVICE; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1637 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1638 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1639 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1640 | // Ensure if this is a foreground service that the proper additional |
| 1641 | // flags are set. |
| 1642 | if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 1643 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 1644 | | Notification.FLAG_NO_CLEAR; |
| 1645 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1646 | |
| Svetoslav Ganov | c31ed39 | 2012-10-10 14:58:28 -0700 | [diff] [blame] | 1647 | final int currentUser; |
| 1648 | final long token = Binder.clearCallingIdentity(); |
| 1649 | try { |
| 1650 | currentUser = ActivityManager.getCurrentUser(); |
| 1651 | } finally { |
| 1652 | Binder.restoreCallingIdentity(token); |
| 1653 | } |
| 1654 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1655 | if (notification.icon != 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1656 | if (old != null && old.statusBarKey != null) { |
| 1657 | r.statusBarKey = old.statusBarKey; |
| 1658 | long identity = Binder.clearCallingIdentity(); |
| 1659 | try { |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1660 | mStatusBar.updateNotification(r.statusBarKey, n); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1661 | } |
| 1662 | finally { |
| 1663 | Binder.restoreCallingIdentity(identity); |
| 1664 | } |
| 1665 | } else { |
| 1666 | long identity = Binder.clearCallingIdentity(); |
| 1667 | try { |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1668 | r.statusBarKey = mStatusBar.addNotification(n); |
| Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 1669 | if ((n.getNotification().flags & Notification.FLAG_SHOW_LIGHTS) != 0 |
| Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 1670 | && canInterrupt) { |
| Mike Lockwood | ece18ef | 2012-02-13 20:42:19 -0800 | [diff] [blame] | 1671 | mAttentionLight.pulse(); |
| 1672 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1673 | } |
| 1674 | finally { |
| 1675 | Binder.restoreCallingIdentity(identity); |
| 1676 | } |
| 1677 | } |
| Svetoslav Ganov | c31ed39 | 2012-10-10 14:58:28 -0700 | [diff] [blame] | 1678 | // Send accessibility events only for the current user. |
| 1679 | if (currentUser == userId) { |
| 1680 | sendAccessibilityEvent(notification, pkg); |
| 1681 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1682 | |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 1683 | notifyPostedLocked(r); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1684 | } else { |
| Daniel Sandler | ffcc976 | 2013-05-22 14:24:20 -0400 | [diff] [blame^] | 1685 | Slog.e(TAG, "Not posting notification with icon==0: " + notification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1686 | if (old != null && old.statusBarKey != null) { |
| 1687 | long identity = Binder.clearCallingIdentity(); |
| 1688 | try { |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 1689 | mStatusBar.removeNotification(old.statusBarKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1690 | } |
| 1691 | finally { |
| 1692 | Binder.restoreCallingIdentity(identity); |
| 1693 | } |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 1694 | |
| 1695 | notifyRemovedLocked(r); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1696 | } |
| Daniel Sandler | ffcc976 | 2013-05-22 14:24:20 -0400 | [diff] [blame^] | 1697 | // ATTENTION: in a future release we will bail out here |
| 1698 | // so that we do not play sounds, show lights, etc. for invalid notifications |
| 1699 | Slog.e(TAG, "WARNING: In a future release this will crash the app: " + n.getPackageName()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | // If we're not supposed to beep, vibrate, etc. then don't. |
| 1703 | if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0) |
| 1704 | && (!(old != null |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1705 | && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 )) |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1706 | && (r.getUserId() == UserHandle.USER_ALL || |
| 1707 | (r.getUserId() == userId && r.getUserId() == currentUser)) |
| Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 1708 | && canInterrupt |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1709 | && mSystemReady) { |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1710 | |
| 1711 | final AudioManager audioManager = (AudioManager) mContext |
| 1712 | .getSystemService(Context.AUDIO_SERVICE); |
| Daniel Sandler | d4d2de2 | 2012-11-14 11:25:46 -0800 | [diff] [blame] | 1713 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1714 | // sound |
| Daniel Sandler | 3147523 | 2013-04-17 00:17:22 -0400 | [diff] [blame] | 1715 | |
| 1716 | // should we use the default notification sound? (indicated either by DEFAULT_SOUND |
| 1717 | // or because notification.sound is pointing at Settings.System.NOTIFICATION_SOUND) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1718 | final boolean useDefaultSound = |
| Daniel Sandler | 3147523 | 2013-04-17 00:17:22 -0400 | [diff] [blame] | 1719 | (notification.defaults & Notification.DEFAULT_SOUND) != 0 |
| 1720 | || Settings.System.DEFAULT_NOTIFICATION_URI.equals(notification.sound); |
| Daniel Sandler | d4d2de2 | 2012-11-14 11:25:46 -0800 | [diff] [blame] | 1721 | |
| 1722 | Uri soundUri = null; |
| 1723 | boolean hasValidSound = false; |
| 1724 | |
| 1725 | if (useDefaultSound) { |
| 1726 | soundUri = Settings.System.DEFAULT_NOTIFICATION_URI; |
| 1727 | |
| 1728 | // check to see if the default notification sound is silent |
| 1729 | ContentResolver resolver = mContext.getContentResolver(); |
| 1730 | hasValidSound = Settings.System.getString(resolver, |
| 1731 | Settings.System.NOTIFICATION_SOUND) != null; |
| 1732 | } else if (notification.sound != null) { |
| 1733 | soundUri = notification.sound; |
| 1734 | hasValidSound = (soundUri != null); |
| 1735 | } |
| 1736 | |
| 1737 | if (hasValidSound) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1738 | boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0; |
| 1739 | int audioStreamType; |
| 1740 | if (notification.audioStreamType >= 0) { |
| 1741 | audioStreamType = notification.audioStreamType; |
| 1742 | } else { |
| 1743 | audioStreamType = DEFAULT_STREAM_TYPE; |
| 1744 | } |
| 1745 | mSoundNotification = r; |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1746 | // do not play notifications if stream volume is 0 |
| Jean-Michel Trivi | d677054 | 2012-10-10 12:03:41 -0700 | [diff] [blame] | 1747 | // (typically because ringer mode is silent) or if speech recognition is active. |
| 1748 | if ((audioManager.getStreamVolume(audioStreamType) != 0) |
| 1749 | && !audioManager.isSpeechRecognitionActive()) { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1750 | final long identity = Binder.clearCallingIdentity(); |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1751 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1752 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 1753 | if (player != null) { |
| Daniel Sandler | d4d2de2 | 2012-11-14 11:25:46 -0800 | [diff] [blame] | 1754 | player.playAsync(soundUri, user, looping, audioStreamType); |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1755 | } |
| 1756 | } catch (RemoteException e) { |
| 1757 | } finally { |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1758 | Binder.restoreCallingIdentity(identity); |
| 1759 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | // vibrate |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1764 | // Does the notification want to specify its own vibration? |
| 1765 | final boolean hasCustomVibrate = notification.vibrate != null; |
| 1766 | |
| David Agnew | 71789e1 | 2012-11-09 23:03:26 -0500 | [diff] [blame] | 1767 | // new in 4.2: if there was supposed to be a sound and we're in vibrate mode, |
| Daniel Sandler | 4a7a9b9 | 2012-11-20 12:59:41 -0500 | [diff] [blame] | 1768 | // and no other vibration is specified, we fall back to vibration |
| David Agnew | 71789e1 | 2012-11-09 23:03:26 -0500 | [diff] [blame] | 1769 | final boolean convertSoundToVibration = |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1770 | !hasCustomVibrate |
| Daniel Sandler | d4d2de2 | 2012-11-14 11:25:46 -0800 | [diff] [blame] | 1771 | && hasValidSound |
| David Agnew | 71789e1 | 2012-11-09 23:03:26 -0500 | [diff] [blame] | 1772 | && (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE); |
| 1773 | |
| Daniel Sandler | 4a7a9b9 | 2012-11-20 12:59:41 -0500 | [diff] [blame] | 1774 | // The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1775 | final boolean useDefaultVibrate = |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1776 | (notification.defaults & Notification.DEFAULT_VIBRATE) != 0; |
| David Agnew | 71789e1 | 2012-11-09 23:03:26 -0500 | [diff] [blame] | 1777 | |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1778 | if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate) |
| Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1779 | && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1780 | mVibrateNotification = r; |
| 1781 | |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1782 | if (useDefaultVibrate || convertSoundToVibration) { |
| 1783 | // Escalate privileges so we can use the vibrator even if the notifying app |
| 1784 | // does not have the VIBRATE permission. |
| 1785 | long identity = Binder.clearCallingIdentity(); |
| 1786 | try { |
| Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 1787 | mVibrator.vibrate(r.sbn.getUid(), r.sbn.getBasePkg(), |
| Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1788 | useDefaultVibrate ? mDefaultVibrationPattern |
| 1789 | : mFallbackVibrationPattern, |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1790 | ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1); |
| 1791 | } finally { |
| 1792 | Binder.restoreCallingIdentity(identity); |
| 1793 | } |
| 1794 | } else if (notification.vibrate.length > 1) { |
| 1795 | // If you want your own vibration pattern, you need the VIBRATE permission |
| Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 1796 | mVibrator.vibrate(r.sbn.getUid(), r.sbn.getBasePkg(), notification.vibrate, |
| Daniel Sandler | edbb380 | 2012-11-13 20:49:47 -0800 | [diff] [blame] | 1797 | ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1); |
| 1798 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1799 | } |
| 1800 | } |
| 1801 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1802 | // light |
| 1803 | // the most recent thing gets the light |
| 1804 | mLights.remove(old); |
| 1805 | if (mLedNotification == old) { |
| 1806 | mLedNotification = null; |
| 1807 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1808 | //Slog.i(TAG, "notification.lights=" |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1809 | // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0)); |
| Daniel Sandler | 526fa0e | 2012-12-04 14:51:50 -0500 | [diff] [blame] | 1810 | if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0 |
| 1811 | && canInterrupt) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1812 | mLights.add(r); |
| 1813 | updateLightsLocked(); |
| 1814 | } else { |
| 1815 | if (old != null |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1816 | && ((old.getFlags() & Notification.FLAG_SHOW_LIGHTS) != 0)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1817 | updateLightsLocked(); |
| 1818 | } |
| 1819 | } |
| 1820 | } |
| 1821 | |
| 1822 | idOut[0] = id; |
| 1823 | } |
| 1824 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1825 | private void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1826 | AccessibilityManager manager = AccessibilityManager.getInstance(mContext); |
| 1827 | if (!manager.isEnabled()) { |
| 1828 | return; |
| 1829 | } |
| 1830 | |
| 1831 | AccessibilityEvent event = |
| 1832 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 1833 | event.setPackageName(packageName); |
| 1834 | event.setClassName(Notification.class.getName()); |
| 1835 | event.setParcelableData(notification); |
| 1836 | CharSequence tickerText = notification.tickerText; |
| 1837 | if (!TextUtils.isEmpty(tickerText)) { |
| 1838 | event.getText().add(tickerText); |
| 1839 | } |
| 1840 | |
| 1841 | manager.sendAccessibilityEvent(event); |
| 1842 | } |
| 1843 | |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1844 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) { |
| 1845 | // tell the app |
| 1846 | if (sendDelete) { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1847 | if (r.getNotification().deleteIntent != null) { |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1848 | try { |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1849 | r.getNotification().deleteIntent.send(); |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1850 | } catch (PendingIntent.CanceledException ex) { |
| 1851 | // do nothing - there's no relevant way to recover, and |
| 1852 | // no reason to let this propagate |
| Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 1853 | Slog.w(TAG, "canceled PendingIntent for " + r.sbn.getPackageName(), ex); |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1854 | } |
| 1855 | } |
| 1856 | } |
| 1857 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1858 | // status bar |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1859 | if (r.getNotification().icon != 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1860 | long identity = Binder.clearCallingIdentity(); |
| 1861 | try { |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 1862 | mStatusBar.removeNotification(r.statusBarKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1863 | } |
| 1864 | finally { |
| 1865 | Binder.restoreCallingIdentity(identity); |
| 1866 | } |
| 1867 | r.statusBarKey = null; |
| Daniel Sandler | 09a247e | 2013-02-14 10:24:17 -0500 | [diff] [blame] | 1868 | notifyRemovedLocked(r); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | // sound |
| 1872 | if (mSoundNotification == r) { |
| 1873 | mSoundNotification = null; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1874 | final long identity = Binder.clearCallingIdentity(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1875 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1876 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 1877 | if (player != null) { |
| 1878 | player.stopAsync(); |
| 1879 | } |
| 1880 | } catch (RemoteException e) { |
| 1881 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1882 | Binder.restoreCallingIdentity(identity); |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | // vibrate |
| 1887 | if (mVibrateNotification == r) { |
| 1888 | mVibrateNotification = null; |
| 1889 | long identity = Binder.clearCallingIdentity(); |
| 1890 | try { |
| 1891 | mVibrator.cancel(); |
| 1892 | } |
| 1893 | finally { |
| 1894 | Binder.restoreCallingIdentity(identity); |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | // light |
| 1899 | mLights.remove(r); |
| 1900 | if (mLedNotification == r) { |
| 1901 | mLedNotification = null; |
| 1902 | } |
| Daniel Sandler | 23d7c70 | 2013-03-07 16:32:06 -0500 | [diff] [blame] | 1903 | |
| 1904 | // Save it for users of getHistoricalNotifications() |
| 1905 | mArchive.record(r.sbn); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | /** |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1909 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1910 | * and none of the {@code mustNotHaveFlags}. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1911 | */ |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1912 | private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1913 | int mustNotHaveFlags, boolean sendDelete, int userId) { |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1914 | EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, userId, |
| Daniel Sandler | b64cb88 | 2011-11-29 23:48:29 -0500 | [diff] [blame] | 1915 | mustHaveFlags, mustNotHaveFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1916 | |
| 1917 | synchronized (mNotificationList) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1918 | int index = indexOfNotificationLocked(pkg, tag, id, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1919 | if (index >= 0) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1920 | NotificationRecord r = mNotificationList.get(index); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1921 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1922 | if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1923 | return; |
| 1924 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1925 | if ((r.getNotification().flags & mustNotHaveFlags) != 0) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1926 | return; |
| 1927 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1928 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1929 | mNotificationList.remove(index); |
| 1930 | |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1931 | cancelNotificationLocked(r, sendDelete); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1932 | updateLightsLocked(); |
| 1933 | } |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | /** |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1938 | * Determine whether the userId applies to the notification in question, either because |
| 1939 | * they match exactly, or one of them is USER_ALL (which is treated as a wildcard). |
| 1940 | */ |
| 1941 | private boolean notificationMatchesUserId(NotificationRecord r, int userId) { |
| 1942 | return |
| 1943 | // looking for USER_ALL notifications? match everything |
| 1944 | userId == UserHandle.USER_ALL |
| 1945 | // a notification sent to USER_ALL matches any query |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1946 | || r.getUserId() == UserHandle.USER_ALL |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1947 | // an exact user match |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1948 | || r.getUserId() == userId; |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | /** |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1952 | * Cancels all notifications from a given package that have all of the |
| 1953 | * {@code mustHaveFlags}. |
| 1954 | */ |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1955 | boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1956 | int mustNotHaveFlags, boolean doit, int userId) { |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1957 | EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, userId, |
| 1958 | mustHaveFlags, mustNotHaveFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1959 | |
| 1960 | synchronized (mNotificationList) { |
| 1961 | final int N = mNotificationList.size(); |
| 1962 | boolean canceledSomething = false; |
| 1963 | for (int i = N-1; i >= 0; --i) { |
| 1964 | NotificationRecord r = mNotificationList.get(i); |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 1965 | if (!notificationMatchesUserId(r, userId)) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1966 | continue; |
| 1967 | } |
| Amith Yamasani | 5ec00e9 | 2012-11-07 16:58:30 -0800 | [diff] [blame] | 1968 | // 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] | 1969 | if (r.getUserId() == UserHandle.USER_ALL && pkg == null) { |
| Amith Yamasani | 5ec00e9 | 2012-11-07 16:58:30 -0800 | [diff] [blame] | 1970 | continue; |
| 1971 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1972 | if ((r.getFlags() & mustHaveFlags) != mustHaveFlags) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1973 | continue; |
| 1974 | } |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 1975 | if ((r.getFlags() & mustNotHaveFlags) != 0) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1976 | continue; |
| 1977 | } |
| Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 1978 | if (pkg != null && !r.sbn.getPackageName().equals(pkg)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1979 | continue; |
| 1980 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1981 | canceledSomething = true; |
| 1982 | if (!doit) { |
| 1983 | return true; |
| 1984 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1985 | mNotificationList.remove(i); |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1986 | cancelNotificationLocked(r, false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1987 | } |
| 1988 | if (canceledSomething) { |
| 1989 | updateLightsLocked(); |
| 1990 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1991 | return canceledSomething; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1992 | } |
| 1993 | } |
| 1994 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1995 | public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1996 | checkCallerIsSystemOrSameApp(pkg); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1997 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| Amith Yamasani | 2c7ebea | 2012-10-30 15:28:27 -0700 | [diff] [blame] | 1998 | Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg); |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1999 | // Don't allow client applications to cancel foreground service notis. |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 2000 | cancelNotification(pkg, tag, id, 0, |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 2001 | Binder.getCallingUid() == Process.SYSTEM_UID |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2002 | ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2003 | } |
| 2004 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2005 | public void cancelAllNotifications(String pkg, int userId) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 2006 | checkCallerIsSystemOrSameApp(pkg); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2007 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2008 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| Amith Yamasani | 2c7ebea | 2012-10-30 15:28:27 -0700 | [diff] [blame] | 2009 | Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2010 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 2011 | // Calling from user space, don't allow the canceling of actively |
| 2012 | // running foreground services. |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2013 | cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2014 | } |
| 2015 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 2016 | void checkCallerIsSystem() { |
| 2017 | int uid = Binder.getCallingUid(); |
| Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 2018 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 2019 | return; |
| 2020 | } |
| 2021 | throw new SecurityException("Disallowed call for uid " + uid); |
| 2022 | } |
| 2023 | |
| 2024 | void checkCallerIsSystemOrSameApp(String pkg) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 2025 | int uid = Binder.getCallingUid(); |
| Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 2026 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 2027 | return; |
| 2028 | } |
| 2029 | try { |
| Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 2030 | ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo( |
| 2031 | pkg, 0, UserHandle.getCallingUserId()); |
| Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2032 | if (!UserHandle.isSameApp(ai.uid, uid)) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 2033 | throw new SecurityException("Calling uid " + uid + " gave package" |
| 2034 | + pkg + " which is owned by uid " + ai.uid); |
| 2035 | } |
| Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 2036 | } catch (RemoteException re) { |
| 2037 | throw new SecurityException("Unknown package " + pkg + "\n" + re); |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 2038 | } |
| 2039 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2040 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2041 | void cancelAll(int userId) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2042 | synchronized (mNotificationList) { |
| 2043 | final int N = mNotificationList.size(); |
| 2044 | for (int i=N-1; i>=0; i--) { |
| 2045 | NotificationRecord r = mNotificationList.get(i); |
| 2046 | |
| Daniel Sandler | 321e9c5 | 2012-10-12 10:59:26 -0700 | [diff] [blame] | 2047 | if (!notificationMatchesUserId(r, userId)) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2048 | continue; |
| 2049 | } |
| 2050 | |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 2051 | if ((r.getFlags() & (Notification.FLAG_ONGOING_EVENT |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2052 | | Notification.FLAG_NO_CLEAR)) == 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2053 | mNotificationList.remove(i); |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2054 | cancelNotificationLocked(r, true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2055 | } |
| 2056 | } |
| 2057 | |
| 2058 | updateLightsLocked(); |
| 2059 | } |
| 2060 | } |
| 2061 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2062 | // lock on mNotificationList |
| 2063 | private void updateLightsLocked() |
| 2064 | { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2065 | // handle notification lights |
| 2066 | if (mLedNotification == null) { |
| 2067 | // get next notification, if any |
| 2068 | int n = mLights.size(); |
| 2069 | if (n > 0) { |
| 2070 | mLedNotification = mLights.get(n-1); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2071 | } |
| 2072 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 2073 | |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 2074 | // Don't flash while we are in a call or screen is on |
| 2075 | if (mLedNotification == null || mInCall || mScreenOn) { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 2076 | mNotificationLight.turnOff(); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2077 | } else { |
| Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 2078 | final Notification ledno = mLedNotification.sbn.getNotification(); |
| Daniel Sandler | fde19b1 | 2013-01-17 00:21:05 -0500 | [diff] [blame] | 2079 | int ledARGB = ledno.ledARGB; |
| 2080 | int ledOnMS = ledno.ledOnMS; |
| 2081 | int ledOffMS = ledno.ledOffMS; |
| 2082 | if ((ledno.defaults & Notification.DEFAULT_LIGHTS) != 0) { |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 2083 | ledARGB = mDefaultNotificationColor; |
| 2084 | ledOnMS = mDefaultNotificationLedOn; |
| 2085 | ledOffMS = mDefaultNotificationLedOff; |
| 2086 | } |
| 2087 | if (mNotificationPulseEnabled) { |
| 2088 | // pulse repeatedly |
| 2089 | mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED, |
| 2090 | ledOnMS, ledOffMS); |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 2091 | } |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2092 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2093 | } |
| 2094 | |
| 2095 | // lock on mNotificationList |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2096 | private 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] | 2097 | { |
| 2098 | ArrayList<NotificationRecord> list = mNotificationList; |
| 2099 | final int len = list.size(); |
| 2100 | for (int i=0; i<len; i++) { |
| 2101 | NotificationRecord r = list.get(i); |
| Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 2102 | if (!notificationMatchesUserId(r, userId) || r.sbn.getId() != id) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2103 | continue; |
| 2104 | } |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 2105 | if (tag == null) { |
| Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 2106 | if (r.sbn.getTag() != null) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 2107 | continue; |
| 2108 | } |
| 2109 | } else { |
| Daniel Sandler | e6f7f2e | 2013-04-25 15:44:16 -0400 | [diff] [blame] | 2110 | if (!tag.equals(r.sbn.getTag())) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 2111 | continue; |
| 2112 | } |
| 2113 | } |
| Daniel Sandler | 4f91efd | 2013-04-25 16:38:41 -0400 | [diff] [blame] | 2114 | if (r.sbn.getPackageName().equals(pkg)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2115 | return i; |
| 2116 | } |
| 2117 | } |
| 2118 | return -1; |
| 2119 | } |
| 2120 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 2121 | private void updateNotificationPulse() { |
| 2122 | synchronized (mNotificationList) { |
| 2123 | updateLightsLocked(); |
| 2124 | } |
| 2125 | } |
| 2126 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2127 | // ====================================================================== |
| 2128 | @Override |
| 2129 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 2130 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 2131 | != PackageManager.PERMISSION_GRANTED) { |
| 2132 | pw.println("Permission Denial: can't dump NotificationManager from from pid=" |
| 2133 | + Binder.getCallingPid() |
| 2134 | + ", uid=" + Binder.getCallingUid()); |
| 2135 | return; |
| 2136 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2137 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2138 | pw.println("Current Notification Manager state:"); |
| 2139 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 2140 | pw.println(" Listeners (" + mEnabledListenersForCurrentUser.size() |
| 2141 | + ") enabled for current user:"); |
| 2142 | for (ComponentName cmpt : mEnabledListenersForCurrentUser) { |
| 2143 | pw.println(" " + cmpt); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 2144 | } |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 2145 | |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 2146 | pw.println(" Live listeners (" + mListeners.size() + "):"); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 2147 | for (NotificationListenerInfo info : mListeners) { |
| Daniel Sandler | 5feceeb | 2013-03-22 18:29:23 -0700 | [diff] [blame] | 2148 | pw.println(" " + info.component |
| 2149 | + " (user " + info.userid + "): " + info.listener |
| 2150 | + (info.isSystem?" SYSTEM":"")); |
| Daniel Sandler | 4b749ef | 2013-03-18 21:53:04 -0400 | [diff] [blame] | 2151 | } |
| 2152 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2153 | int N; |
| 2154 | |
| 2155 | synchronized (mToastQueue) { |
| 2156 | N = mToastQueue.size(); |
| 2157 | if (N > 0) { |
| 2158 | pw.println(" Toast Queue:"); |
| 2159 | for (int i=0; i<N; i++) { |
| 2160 | mToastQueue.get(i).dump(pw, " "); |
| 2161 | } |
| 2162 | pw.println(" "); |
| 2163 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2164 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | synchronized (mNotificationList) { |
| 2168 | N = mNotificationList.size(); |
| 2169 | if (N > 0) { |
| 2170 | pw.println(" Notification List:"); |
| 2171 | for (int i=0; i<N; i++) { |
| 2172 | mNotificationList.get(i).dump(pw, " ", mContext); |
| 2173 | } |
| 2174 | pw.println(" "); |
| 2175 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2176 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2177 | N = mLights.size(); |
| 2178 | if (N > 0) { |
| 2179 | pw.println(" Lights List:"); |
| 2180 | for (int i=0; i<N; i++) { |
| Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 2181 | pw.println(" " + mLights.get(i)); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2182 | } |
| 2183 | pw.println(" "); |
| 2184 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 2185 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2186 | pw.println(" mSoundNotification=" + mSoundNotification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2187 | pw.println(" mVibrateNotification=" + mVibrateNotification); |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 2188 | pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications)); |
| 2189 | pw.println(" mSystemReady=" + mSystemReady); |
| Daniel Sandler | 5e62e3a | 2013-04-15 20:57:02 -0400 | [diff] [blame] | 2190 | pw.println(" mArchive=" + mArchive.toString()); |
| 2191 | Iterator<StatusBarNotification> iter = mArchive.descendingIterator(); |
| 2192 | int i=0; |
| 2193 | while (iter.hasNext()) { |
| 2194 | pw.println(" " + iter.next()); |
| 2195 | if (++i >= 5) { |
| 2196 | if (iter.hasNext()) pw.println(" ..."); |
| 2197 | break; |
| 2198 | } |
| 2199 | } |
| 2200 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2201 | } |
| 2202 | } |
| 2203 | } |