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