blob: f9b6eba68b60ad2a24981716ba715bd3c4c85b43 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package com.android.server;
18
Jeff Sharkey098d5802012-04-26 17:30:34 -070019import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
20import static org.xmlpull.v1.XmlPullParser.END_TAG;
21import static org.xmlpull.v1.XmlPullParser.START_TAG;
svetoslavganov75986cf2009-05-14 22:28:01 -070022
Dianne Hackborn41203752012-08-31 14:05:51 -070023import android.app.ActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.app.ActivityManagerNative;
Amith Yamasanif203aee2012-08-29 18:41:53 -070025import android.app.AppGlobals;
Daniel Sandler4a900acd2013-01-30 14:04:10 -050026import android.app.AppOpsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.app.IActivityManager;
28import android.app.INotificationManager;
29import android.app.ITransientNotification;
30import android.app.Notification;
31import android.app.PendingIntent;
32import android.app.StatusBarManager;
33import android.content.BroadcastReceiver;
Daniel Sandler5feceeb2013-03-22 18:29:23 -070034import android.content.ComponentName;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070035import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.Context;
37import android.content.Intent;
38import android.content.IntentFilter;
Daniel Sandler5feceeb2013-03-22 18:29:23 -070039import android.content.ServiceConnection;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070040import android.content.pm.ApplicationInfo;
Daniel Sandler4a900acd2013-01-30 14:04:10 -050041import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.content.pm.PackageManager;
43import android.content.pm.PackageManager.NameNotFoundException;
44import android.content.res.Resources;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070045import android.database.ContentObserver;
Daniel Sandlerf45564e2013-04-15 15:05:08 -040046import android.graphics.Bitmap;
svetoslavganov75986cf2009-05-14 22:28:01 -070047import android.media.AudioManager;
Jeff Sharkey098d5802012-04-26 17:30:34 -070048import android.media.IAudioService;
49import android.media.IRingtonePlayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.IBinder;
54import android.os.Message;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070055import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.os.RemoteException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070057import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070058import android.os.UserHandle;
Daniel Sandler4b749ef2013-03-18 21:53:04 -040059import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.Vibrator;
61import android.provider.Settings;
Daniel Sandler5feceeb2013-03-22 18:29:23 -070062import android.service.notification.INotificationListener;
63import android.service.notification.NotificationListenerService;
64import android.service.notification.StatusBarNotification;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050065import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.text.TextUtils;
Dianne Hackborn39606a02012-07-31 17:54:35 -070067import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.util.EventLog;
69import android.util.Log;
Andy Stadler110988c2010-12-03 14:29:16 -080070import android.util.Slog;
Daniel Sandler0da673f2012-04-11 12:33:16 -040071import android.util.Xml;
svetoslavganov75986cf2009-05-14 22:28:01 -070072import android.view.accessibility.AccessibilityEvent;
73import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.widget.Toast;
75
Jeff Sharkey098d5802012-04-26 17:30:34 -070076import org.xmlpull.v1.XmlPullParser;
77import org.xmlpull.v1.XmlPullParserException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070078
Daniel Sandler0da673f2012-04-11 12:33:16 -040079import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import java.io.FileDescriptor;
Daniel Sandler0da673f2012-04-11 12:33:16 -040081import java.io.FileInputStream;
82import java.io.FileNotFoundException;
Daniel Sandler0da673f2012-04-11 12:33:16 -040083import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.io.PrintWriter;
Daniel Sandlerf45564e2013-04-15 15:05:08 -040085import java.lang.reflect.Array;
Daniel Sandlerfde19b12013-01-17 00:21:05 -050086import java.util.ArrayDeque;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import java.util.ArrayList;
88import java.util.Arrays;
Daniel Sandler0da673f2012-04-11 12:33:16 -040089import java.util.HashSet;
Daniel Sandlerfde19b12013-01-17 00:21:05 -050090import java.util.Iterator;
91import java.util.NoSuchElementException;
Daniel Sandler0da673f2012-04-11 12:33:16 -040092
93import libcore.io.IoUtils;
94
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040096/** {@hide} */
97public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098{
99 private static final String TAG = "NotificationService";
100 private static final boolean DBG = false;
101
Joe Onoratobd73d012010-06-04 11:44:54 -0700102 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 // message codes
105 private static final int MESSAGE_TIMEOUT = 2;
106
107 private static final int LONG_DELAY = 3500; // 3.5 seconds
108 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800109
110 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
Daniel Sandleredbb3802012-11-13 20:49:47 -0800111 private static final int VIBRATE_PATTERN_MAXLEN = 8 * 2 + 1; // up to eight bumps
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
113 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400114 private static final boolean SCORE_ONGOING_HIGHER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
Daniel Sandler0da673f2012-04-11 12:33:16 -0400116 private static final int JUNK_SCORE = -1000;
117 private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10;
118 private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER;
119
Daniel Sandler526fa0e2012-12-04 14:51:50 -0500120 // Notifications with scores below this will not interrupt the user, either via LED or
121 // sound or vibration
122 private static final int SCORE_INTERRUPTION_THRESHOLD =
123 Notification.PRIORITY_LOW * NOTIFICATION_PRIORITY_MULTIPLIER;
124
Daniel Sandler0da673f2012-04-11 12:33:16 -0400125 private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true;
126 private static final boolean ENABLE_BLOCKED_TOASTS = true;
127
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700128 private static final String ENABLED_NOTIFICATION_LISTENERS_SEPARATOR = ":";
129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 final Context mContext;
131 final IActivityManager mAm;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400132 final UserManager mUserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 final IBinder mForegroundToken = new Binder();
134
135 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -0700136 private StatusBarManagerService mStatusBar;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500137 private LightsService.Light mNotificationLight;
138 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
Mike Lockwood670f9322010-01-20 12:13:36 -0500140 private int mDefaultNotificationColor;
141 private int mDefaultNotificationLedOn;
142 private int mDefaultNotificationLedOff;
143
Daniel Sandleredbb3802012-11-13 20:49:47 -0800144 private long[] mDefaultVibrationPattern;
145 private long[] mFallbackVibrationPattern;
146
Joe Onorato30275482009-07-08 17:09:14 -0700147 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400148 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Jeff Sharkey098d5802012-04-26 17:30:34 -0700150 private NotificationRecord mSoundNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 private NotificationRecord mVibrateNotification;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700152
153 private IAudioService mAudioService;
Jeff Brownc2346132012-04-13 01:55:38 -0700154 private Vibrator mVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500156 // for enabling and disabling notification pulse behavior
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400157 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500158 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500159 private boolean mNotificationPulseEnabled;
160
Daniel Sandler09a247e2013-02-14 10:24:17 -0500161 // used as a mutex for access to all active notifications & listeners
Fred Quintana6ecaff12009-09-25 14:23:13 -0700162 private final ArrayList<NotificationRecord> mNotificationList =
163 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
165 private ArrayList<ToastRecord> mToastQueue;
166
167 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700169
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500170 private final AppOpsManager mAppOps;
171
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700172 // contains connections to all connected listeners, including app services
173 // and system listeners
174 private ArrayList<NotificationListenerInfo> mListeners
175 = new ArrayList<NotificationListenerInfo>();
176 // things that will be put into mListeners as soon as they're ready
177 private ArrayList<String> mServicesBinding = new ArrayList<String>();
178 // lists the component names of all enabled (and therefore connected) listener
179 // app services for the current user only
180 private HashSet<ComponentName> mEnabledListenersForCurrentUser
181 = new HashSet<ComponentName>();
182 // Just the packages from mEnabledListenersForCurrentUser
183 private HashSet<String> mEnabledListenerPackageNames = new HashSet<String>();
Daniel Sandler09a247e2013-02-14 10:24:17 -0500184
Daniel Sandler0da673f2012-04-11 12:33:16 -0400185 // Notification control database. For now just contains disabled packages.
186 private AtomicFile mPolicyFile;
187 private HashSet<String> mBlockedPackages = new HashSet<String>();
188
189 private static final int DB_VERSION = 1;
190
191 private static final String TAG_BODY = "notification-policy";
192 private static final String ATTR_VERSION = "version";
193
194 private static final String TAG_BLOCKED_PKGS = "blocked-packages";
195 private static final String TAG_PACKAGE = "package";
196 private static final String ATTR_NAME = "name";
197
Daniel Sandler09a247e2013-02-14 10:24:17 -0500198 private class NotificationListenerInfo implements DeathRecipient {
199 INotificationListener listener;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700200 ComponentName component;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500201 int userid;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400202 boolean isSystem;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700203 ServiceConnection connection;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400204
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700205 public NotificationListenerInfo(INotificationListener listener, ComponentName component,
206 int userid, boolean isSystem) {
Daniel Sandler09a247e2013-02-14 10:24:17 -0500207 this.listener = listener;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700208 this.component = component;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500209 this.userid = userid;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400210 this.isSystem = isSystem;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700211 this.connection = null;
212 }
213
214 public NotificationListenerInfo(INotificationListener listener, ComponentName component,
215 int userid, ServiceConnection connection) {
216 this.listener = listener;
217 this.component = component;
218 this.userid = userid;
219 this.isSystem = false;
220 this.connection = connection;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500221 }
222
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400223 boolean enabledAndUserMatches(StatusBarNotification sbn) {
224 final int nid = sbn.getUserId();
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700225 if (!isEnabledForCurrentUser()) {
226 return false;
227 }
Daniel Sandler21ca44d2013-03-07 13:58:00 -0500228 if (this.userid == UserHandle.USER_ALL) return true;
Daniel Sandler21ca44d2013-03-07 13:58:00 -0500229 return (nid == UserHandle.USER_ALL || nid == this.userid);
230 }
231
Daniel Sandler09a247e2013-02-14 10:24:17 -0500232 public void notifyPostedIfUserMatch(StatusBarNotification sbn) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700233 if (!enabledAndUserMatches(sbn)) {
234 return;
235 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500236 try {
237 listener.onNotificationPosted(sbn);
238 } catch (RemoteException ex) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400239 Log.e(TAG, "unable to notify listener (posted): " + listener, ex);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500240 }
241 }
242
243 public void notifyRemovedIfUserMatch(StatusBarNotification sbn) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400244 if (!enabledAndUserMatches(sbn)) return;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500245 try {
246 listener.onNotificationRemoved(sbn);
247 } catch (RemoteException ex) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400248 Log.e(TAG, "unable to notify listener (removed): " + listener, ex);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500249 }
250 }
251
252 @Override
253 public void binderDied() {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700254 if (connection == null) {
255 // This is not a service; it won't be recreated. We can give up this connection.
256 unregisterListener(this.listener, this.userid);
257 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500258 }
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400259
260 /** convenience method for looking in mEnabledListenersForCurrentUser */
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700261 public boolean isEnabledForCurrentUser() {
262 if (this.isSystem) return true;
263 if (this.connection == null) return false;
264 return mEnabledListenersForCurrentUser.contains(this.component);
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400265 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500266 }
267
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500268 private static class Archive {
Daniel Sandler5e62e3a2013-04-15 20:57:02 -0400269 static final int BUFFER_SIZE = 250;
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500270 ArrayDeque<StatusBarNotification> mBuffer = new ArrayDeque<StatusBarNotification>(BUFFER_SIZE);
271
272 public Archive() {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500273 }
Jeff Sharkey0c1baf92013-04-03 13:08:52 -0700274
Daniel Sandler5e62e3a2013-04-15 20:57:02 -0400275 public String toString() {
276 final StringBuilder sb = new StringBuilder();
277 final int N = mBuffer.size();
278 sb.append("Archive (");
279 sb.append(N);
280 sb.append(" notification");
281 sb.append((N==1)?")":"s)");
282 return sb.toString();
283 }
284
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500285 public void record(StatusBarNotification nr) {
Jeff Sharkey0c1baf92013-04-03 13:08:52 -0700286 // Nuke heavy parts of notification before storing in archive
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400287 nr.getNotification().lightenPayload();
Jeff Sharkey0c1baf92013-04-03 13:08:52 -0700288
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500289 if (mBuffer.size() == BUFFER_SIZE) {
290 mBuffer.removeFirst();
291 }
292 mBuffer.addLast(nr);
293 }
294
Daniel Sandler5e62e3a2013-04-15 20:57:02 -0400295
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500296 public void clear() {
297 mBuffer.clear();
298 }
299
300 public Iterator<StatusBarNotification> descendingIterator() {
301 return mBuffer.descendingIterator();
302 }
303 public Iterator<StatusBarNotification> ascendingIterator() {
304 return mBuffer.iterator();
305 }
306 public Iterator<StatusBarNotification> filter(
307 final Iterator<StatusBarNotification> iter, final String pkg, final int userId) {
308 return new Iterator<StatusBarNotification>() {
309 StatusBarNotification mNext = findNext();
310
311 private StatusBarNotification findNext() {
312 while (iter.hasNext()) {
313 StatusBarNotification nr = iter.next();
Daniel Sandler4f91efd2013-04-25 16:38:41 -0400314 if ((pkg == null || nr.getPackageName() == pkg)
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500315 && (userId == UserHandle.USER_ALL || nr.getUserId() == userId)) {
316 return nr;
317 }
318 }
319 return null;
320 }
321
322 @Override
323 public boolean hasNext() {
324 return mNext == null;
325 }
326
327 @Override
328 public StatusBarNotification next() {
329 StatusBarNotification next = mNext;
330 if (next == null) {
331 throw new NoSuchElementException();
332 }
333 mNext = findNext();
334 return next;
335 }
336
337 @Override
338 public void remove() {
339 iter.remove();
340 }
341 };
342 }
Daniel Sandler78d0d252013-02-12 08:14:52 -0500343
344 public StatusBarNotification[] getArray(int count) {
345 if (count == 0) count = Archive.BUFFER_SIZE;
346 final StatusBarNotification[] a
347 = new StatusBarNotification[Math.min(count, mBuffer.size())];
348 Iterator<StatusBarNotification> iter = descendingIterator();
349 int i=0;
350 while (iter.hasNext() && i < count) {
351 a[i++] = iter.next();
352 }
353 return a;
354 }
355
356 public StatusBarNotification[] getArray(int count, String pkg, int userId) {
357 if (count == 0) count = Archive.BUFFER_SIZE;
358 final StatusBarNotification[] a
359 = new StatusBarNotification[Math.min(count, mBuffer.size())];
360 Iterator<StatusBarNotification> iter = filter(descendingIterator(), pkg, userId);
361 int i=0;
362 while (iter.hasNext() && i < count) {
363 a[i++] = iter.next();
364 }
365 return a;
366 }
367
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500368 }
369
370 Archive mArchive = new Archive();
371
Daniel Sandler0da673f2012-04-11 12:33:16 -0400372 private void loadBlockDb() {
373 synchronized(mBlockedPackages) {
374 if (mPolicyFile == null) {
375 File dir = new File("/data/system");
376 mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml"));
377
378 mBlockedPackages.clear();
379
380 FileInputStream infile = null;
381 try {
382 infile = mPolicyFile.openRead();
383 final XmlPullParser parser = Xml.newPullParser();
384 parser.setInput(infile, null);
385
386 int type;
387 String tag;
388 int version = DB_VERSION;
389 while ((type = parser.next()) != END_DOCUMENT) {
390 tag = parser.getName();
391 if (type == START_TAG) {
392 if (TAG_BODY.equals(tag)) {
393 version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
394 } else if (TAG_BLOCKED_PKGS.equals(tag)) {
395 while ((type = parser.next()) != END_DOCUMENT) {
396 tag = parser.getName();
397 if (TAG_PACKAGE.equals(tag)) {
398 mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME));
399 } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) {
400 break;
401 }
402 }
403 }
404 }
405 }
406 } catch (FileNotFoundException e) {
407 // No data yet
408 } catch (IOException e) {
409 Log.wtf(TAG, "Unable to read blocked notifications database", e);
410 } catch (NumberFormatException e) {
411 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
412 } catch (XmlPullParserException e) {
413 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
414 } finally {
415 IoUtils.closeQuietly(infile);
416 }
417 }
418 }
419 }
420
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500421 /**
422 * Use this when you just want to know if notifications are OK for this package.
423 */
424 public boolean areNotificationsEnabledForPackage(String pkg, int uid) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400425 checkCallerIsSystem();
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500426 return (mAppOps.checkOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
427 == AppOpsManager.MODE_ALLOWED);
Daniel Sandler0da673f2012-04-11 12:33:16 -0400428 }
429
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500430 /** Use this when you actually want to post a notification or toast.
431 *
432 * Unchecked. Not exposed via Binder, but can be called in the course of enqueue*().
433 */
434 private boolean noteNotificationOp(String pkg, int uid) {
435 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
436 != AppOpsManager.MODE_ALLOWED) {
437 Slog.v(TAG, "notifications are disabled by AppOps for " + pkg);
438 return false;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400439 }
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500440 return true;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400441 }
442
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500443 public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400444 checkCallerIsSystem();
Daniel Sandler32e698b2013-04-18 10:51:35 -0400445
446 Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);
447
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500448 mAppOps.setMode(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg,
449 enabled ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED);
Daniel Sandler32e698b2013-04-18 10:51:35 -0400450
451 // Now, cancel any outstanding notifications that are part of a just-disabled app
452 if (ENABLE_BLOCKED_NOTIFICATIONS && !enabled) {
453 cancelAllNotificationsInt(pkg, 0, 0, true, UserHandle.getUserId(uid));
454 }
Daniel Sandler0da673f2012-04-11 12:33:16 -0400455 }
456
457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 private static String idDebugString(Context baseContext, String packageName, int id) {
459 Context c = null;
460
461 if (packageName != null) {
462 try {
463 c = baseContext.createPackageContext(packageName, 0);
464 } catch (NameNotFoundException e) {
465 c = baseContext;
466 }
467 } else {
468 c = baseContext;
469 }
470
471 String pkg;
472 String type;
473 String name;
474
475 Resources r = c.getResources();
476 try {
477 return r.getResourceName(id);
478 } catch (Resources.NotFoundException e) {
479 return "<name unknown>";
480 }
481 }
482
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700483 /**
484 * System-only API for getting a list of current (i.e. not cleared) notifications.
485 *
486 * Requires ACCESS_NOTIFICATIONS which is signature|system.
487 */
488 @Override
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500489 public StatusBarNotification[] getActiveNotifications(String callingPkg) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400490 // enforce() will ensure the calling uid has the correct permission
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500491 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
Daniel Sandler78d0d252013-02-12 08:14:52 -0500492 "NotificationManagerService.getActiveNotifications");
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500493
494 StatusBarNotification[] tmp = null;
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500495 int uid = Binder.getCallingUid();
496
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400497 // noteOp will check to make sure the callingPkg matches the uid
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500498 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
499 == AppOpsManager.MODE_ALLOWED) {
500 synchronized (mNotificationList) {
501 tmp = new StatusBarNotification[mNotificationList.size()];
502 final int N = mNotificationList.size();
503 for (int i=0; i<N; i++) {
504 tmp[i] = mNotificationList.get(i).sbn;
505 }
506 }
507 }
508 return tmp;
509 }
510
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700511 /**
512 * System-only API for getting a list of recent (cleared, no longer shown) notifications.
513 *
514 * Requires ACCESS_NOTIFICATIONS which is signature|system.
515 */
516 @Override
Daniel Sandler78d0d252013-02-12 08:14:52 -0500517 public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400518 // enforce() will ensure the calling uid has the correct permission
Daniel Sandler78d0d252013-02-12 08:14:52 -0500519 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
520 "NotificationManagerService.getHistoricalNotifications");
521
522 StatusBarNotification[] tmp = null;
523 int uid = Binder.getCallingUid();
524
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400525 // noteOp will check to make sure the callingPkg matches the uid
Daniel Sandler78d0d252013-02-12 08:14:52 -0500526 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
527 == AppOpsManager.MODE_ALLOWED) {
528 synchronized (mArchive) {
529 tmp = mArchive.getArray(count);
530 }
531 }
532 return tmp;
533 }
534
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700535 /**
536 * Called whenever packages change, the user switches, or ENABLED_NOTIFICATION_LISTENERS
537 * is altered. (For example in response to USER_SWITCHED in our broadcast receiver)
538 */
539 void rebindListenerServices() {
John Spurlock3ec4e702013-05-16 13:42:38 -0400540 final int currentUser = ActivityManager.getCurrentUser();
541 String flat = Settings.Secure.getStringForUser(
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700542 mContext.getContentResolver(),
John Spurlock3ec4e702013-05-16 13:42:38 -0400543 Settings.Secure.ENABLED_NOTIFICATION_LISTENERS,
544 currentUser);
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700545
546 NotificationListenerInfo[] toRemove = new NotificationListenerInfo[mListeners.size()];
547 final ArrayList<ComponentName> toAdd;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700548
549 synchronized (mNotificationList) {
550 // unbind and remove all existing listeners
551 toRemove = mListeners.toArray(toRemove);
552
553 toAdd = new ArrayList<ComponentName>();
554 final HashSet<ComponentName> newEnabled = new HashSet<ComponentName>();
555 final HashSet<String> newPackages = new HashSet<String>();
556
557 // decode the list of components
558 if (flat != null) {
559 String[] components = flat.split(ENABLED_NOTIFICATION_LISTENERS_SEPARATOR);
560 for (int i=0; i<components.length; i++) {
561 final ComponentName component
562 = ComponentName.unflattenFromString(components[i]);
563 if (component != null) {
564 newEnabled.add(component);
565 toAdd.add(component);
566 newPackages.add(component.getPackageName());
567 }
568 }
569
570 mEnabledListenersForCurrentUser = newEnabled;
571 mEnabledListenerPackageNames = newPackages;
572 }
573 }
574
575 for (NotificationListenerInfo info : toRemove) {
576 final ComponentName component = info.component;
577 final int oldUser = info.userid;
578 Slog.v(TAG, "disabling notification listener for user " + oldUser + ": " + component);
579 unregisterListenerService(component, info.userid);
580 }
581
582 final int N = toAdd.size();
583 for (int i=0; i<N; i++) {
584 final ComponentName component = toAdd.get(i);
585 Slog.v(TAG, "enabling notification listener for user " + currentUser + ": "
586 + component);
587 registerListenerService(component, currentUser);
588 }
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400589 }
590
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700591 /**
592 * Register a listener binder directly with the notification manager.
593 *
594 * Only works with system callers. Apps should extend
595 * {@link android.service.notification.NotificationListenerService}.
596 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500597 @Override
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400598 public void registerListener(final INotificationListener listener,
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700599 final ComponentName component, final int userid) {
600 checkCallerIsSystem();
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400601
Daniel Sandler09a247e2013-02-14 10:24:17 -0500602 synchronized (mNotificationList) {
603 try {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400604 NotificationListenerInfo info
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700605 = new NotificationListenerInfo(listener, component, userid, true);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500606 listener.asBinder().linkToDeath(info, 0);
607 mListeners.add(info);
608 } catch (RemoteException e) {
609 // already dead
610 }
611 }
612 }
613
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700614 /**
615 * Version of registerListener that takes the name of a
616 * {@link android.service.notification.NotificationListenerService} to bind to.
617 *
618 * This is the mechanism by which third parties may subscribe to notifications.
619 */
620 private void registerListenerService(final ComponentName name, final int userid) {
621 checkCallerIsSystem();
622
623 if (DBG) Slog.v(TAG, "registerListenerService: " + name + " u=" + userid);
624
625 synchronized (mNotificationList) {
626 final String servicesBindingTag = name.toString() + "/" + userid;
627 if (mServicesBinding.contains(servicesBindingTag)) {
628 // stop registering this thing already! we're working on it
629 return;
630 }
631 mServicesBinding.add(servicesBindingTag);
632
633 final int N = mListeners.size();
634 for (int i=N-1; i>=0; i--) {
635 final NotificationListenerInfo info = mListeners.get(i);
636 if (name.equals(info.component)
637 && info.userid == userid) {
638 // cut old connections
639 if (DBG) Slog.v(TAG, " disconnecting old listener: " + info.listener);
640 mListeners.remove(i);
641 if (info.connection != null) {
642 mContext.unbindService(info.connection);
643 }
644 }
645 }
646
647 Intent intent = new Intent(NotificationListenerService.SERVICE_INTERFACE);
648 intent.setComponent(name);
649
650 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
651 com.android.internal.R.string.notification_listener_binding_label);
652 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
653 mContext, 0, new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS), 0));
654
655 try {
656 if (DBG) Slog.v(TAG, "binding: " + intent);
657 if (!mContext.bindServiceAsUser(intent,
658 new ServiceConnection() {
659 INotificationListener mListener;
660 @Override
661 public void onServiceConnected(ComponentName name, IBinder service) {
662 synchronized (mNotificationList) {
663 mServicesBinding.remove(servicesBindingTag);
664 try {
665 mListener = INotificationListener.Stub.asInterface(service);
666 NotificationListenerInfo info = new NotificationListenerInfo(
667 mListener, name, userid, this);
668 service.linkToDeath(info, 0);
669 mListeners.add(info);
670 } catch (RemoteException e) {
671 // already dead
672 }
673 }
674 }
675
676 @Override
677 public void onServiceDisconnected(ComponentName name) {
678 Slog.v(TAG, "notification listener connection lost: " + name);
679 }
680 },
681 Context.BIND_AUTO_CREATE,
682 new UserHandle(userid)))
683 {
684 mServicesBinding.remove(servicesBindingTag);
685 Slog.w(TAG, "Unable to bind listener service: " + intent);
686 return;
687 }
688 } catch (SecurityException ex) {
689 Slog.e(TAG, "Unable to bind listener service: " + intent, ex);
690 return;
691 }
692 }
693 }
694
695 /**
696 * Remove a listener binder directly
697 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500698 @Override
699 public void unregisterListener(INotificationListener listener, int userid) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400700 // no need to check permissions; if your listener binder is in the list,
701 // that's proof that you had permission to add it in the first place
702
Daniel Sandler09a247e2013-02-14 10:24:17 -0500703 synchronized (mNotificationList) {
704 final int N = mListeners.size();
705 for (int i=N-1; i>=0; i--) {
706 final NotificationListenerInfo info = mListeners.get(i);
707 if (info.listener == listener && info.userid == userid) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700708 mListeners.remove(i);
709 if (info.connection != null) {
710 mContext.unbindService(info.connection);
711 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500712 }
713 }
714 }
715 }
716
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700717 /**
718 * Remove a listener service for the given user by ComponentName
719 */
720 private void unregisterListenerService(ComponentName name, int userid) {
721 checkCallerIsSystem();
722
723 synchronized (mNotificationList) {
724 final int N = mListeners.size();
725 for (int i=N-1; i>=0; i--) {
726 final NotificationListenerInfo info = mListeners.get(i);
727 if (name.equals(info.component)
728 && info.userid == userid) {
729 mListeners.remove(i);
730 if (info.connection != null) {
Daniel Sandlerc1b49bd2013-05-07 13:53:47 -0400731 try {
732 mContext.unbindService(info.connection);
733 } catch (IllegalArgumentException ex) {
734 // something happened to the service: we think we have a connection
735 // but it's bogus.
736 Slog.e(TAG, "Listener " + name + " could not be unbound: " + ex);
737 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700738 }
739 }
740 }
741 }
742 }
743
744 /**
745 * asynchronously notify all listeners about a new notification
746 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500747 private void notifyPostedLocked(NotificationRecord n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400748 // make a copy in case changes are made to the underlying Notification object
749 final StatusBarNotification sbn = n.sbn.clone();
Daniel Sandler09a247e2013-02-14 10:24:17 -0500750 for (final NotificationListenerInfo info : mListeners) {
751 mHandler.post(new Runnable() {
752 @Override
753 public void run() {
754 info.notifyPostedIfUserMatch(sbn);
755 }});
756 }
757 }
758
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700759 /**
760 * asynchronously notify all listeners about a removed notification
761 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500762 private void notifyRemovedLocked(NotificationRecord n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400763 // make a copy in case changes are made to the underlying Notification object
764 // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the notification
765 final StatusBarNotification sbn_light = n.sbn.cloneLight();
766
Daniel Sandler09a247e2013-02-14 10:24:17 -0500767 for (final NotificationListenerInfo info : mListeners) {
768 mHandler.post(new Runnable() {
769 @Override
770 public void run() {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400771 info.notifyRemovedIfUserMatch(sbn_light);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500772 }});
773 }
774 }
775
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700776 // -- APIs to support listeners clicking/clearing notifications --
777
778 private NotificationListenerInfo checkListenerToken(INotificationListener listener) {
779 final IBinder token = listener.asBinder();
780 final int N = mListeners.size();
781 for (int i=0; i<N; i++) {
782 final NotificationListenerInfo info = mListeners.get(i);
783 if (info.listener.asBinder() == token) return info;
784 }
785 throw new SecurityException("Disallowed call from unknown listener: " + listener);
786 }
787
788 /**
789 * Allow an INotificationListener to simulate a "clear all" operation.
790 *
791 * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications}
792 *
793 * @param token The binder for the listener, to check that the caller is allowed
794 */
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400795 public void cancelAllNotificationsFromListener(INotificationListener token) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700796 NotificationListenerInfo info = checkListenerToken(token);
797 long identity = Binder.clearCallingIdentity();
798 try {
799 cancelAll(info.userid);
800 } finally {
801 Binder.restoreCallingIdentity(identity);
802 }
803 }
804
805 /**
806 * Allow an INotificationListener to simulate clearing (dismissing) a single notification.
807 *
808 * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear}
809 *
810 * @param token The binder for the listener, to check that the caller is allowed
811 */
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400812 public void cancelNotificationFromListener(INotificationListener token, String pkg, String tag, int id) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700813 NotificationListenerInfo info = checkListenerToken(token);
814 long identity = Binder.clearCallingIdentity();
815 try {
816 cancelNotification(pkg, tag, id, 0,
817 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
818 true,
819 info.userid);
820 } finally {
821 Binder.restoreCallingIdentity(identity);
822 }
823 }
824
Daniel Sandler25cf8ce2013-04-24 15:34:57 -0400825 /**
826 * Allow an INotificationListener to request the list of outstanding notifications seen by
827 * the current user. Useful when starting up, after which point the listener callbacks should
828 * be used.
829 *
830 * @param token The binder for the listener, to check that the caller is allowed
831 */
832 public StatusBarNotification[] getActiveNotificationsFromListener(INotificationListener token) {
833 NotificationListenerInfo info = checkListenerToken(token);
834
835 StatusBarNotification[] result = new StatusBarNotification[0];
836 ArrayList<StatusBarNotification> list = new ArrayList<StatusBarNotification>();
837 synchronized (mNotificationList) {
838 final int N = mNotificationList.size();
839 for (int i=0; i<N; i++) {
840 StatusBarNotification sbn = mNotificationList.get(i).sbn;
841 if (info.enabledAndUserMatches(sbn)) {
842 list.add(sbn);
843 }
844 }
845 }
846 return list.toArray(result);
847 }
848
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700849 // -- end of listener APIs --
850
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500851 public static final class NotificationRecord
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500853 final StatusBarNotification sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 IBinder statusBarKey;
855
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500856 NotificationRecord(StatusBarNotification sbn)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500858 this.sbn = sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700860
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400861 public Notification getNotification() { return sbn.getNotification(); }
862 public int getFlags() { return sbn.getNotification().flags; }
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500863 public int getUserId() { return sbn.getUserId(); }
864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 void dump(PrintWriter pw, String prefix, Context baseContext) {
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400866 final Notification notification = sbn.getNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 pw.println(prefix + this);
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400868 pw.println(prefix + " uid=" + sbn.getUid() + " userId=" + sbn.getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
Daniel Sandler4f91efd2013-04-25 16:38:41 -0400870 + " / " + idDebugString(baseContext, sbn.getPackageName(), notification.icon));
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400871 pw.println(prefix + " pri=" + notification.priority + " score=" + sbn.getScore());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 pw.println(prefix + " contentIntent=" + notification.contentIntent);
873 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
874 pw.println(prefix + " tickerText=" + notification.tickerText);
875 pw.println(prefix + " contentView=" + notification.contentView);
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400876 pw.println(prefix + String.format(" defaults=0x%08x flags=0x%08x",
877 notification.defaults, notification.flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 pw.println(prefix + " sound=" + notification.sound);
879 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400880 pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d",
881 notification.ledARGB, notification.ledOnMS, notification.ledOffMS));
882 if (notification.actions != null && notification.actions.length > 0) {
883 pw.println(prefix + " actions={");
884 final int N = notification.actions.length;
885 for (int i=0; i<N; i++) {
886 final Notification.Action action = notification.actions[i];
887 pw.println(String.format("%s [%d] \"%s\" -> %s",
888 prefix,
889 i,
890 action.title,
891 action.actionIntent.toString()
892 ));
893 }
894 pw.println(prefix + " }");
895 }
896 if (notification.extras != null && notification.extras.size() > 0) {
897 pw.println(prefix + " extras={");
898 for (String key : notification.extras.keySet()) {
899 pw.print(prefix + " " + key + "=");
900 Object val = notification.extras.get(key);
901 if (val == null) {
902 pw.println("null");
903 } else {
904 pw.print(val.toString());
905 if (val instanceof Bitmap) {
906 pw.print(String.format(" (%dx%d)",
907 ((Bitmap) val).getWidth(),
908 ((Bitmap) val).getHeight()));
909 } else if (val.getClass().isArray()) {
910 pw.println(" {");
911 final int N = Array.getLength(val);
912 for (int i=0; i<N; i++) {
913 if (i > 0) pw.println(",");
914 pw.print(prefix + " " + Array.get(val, i));
915 }
916 pw.print("\n" + prefix + " }");
917 }
918 pw.println();
919 }
920 }
921 pw.println(prefix + " }");
922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 @Override
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500926 public final String toString() {
927 return String.format(
928 "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s score=%d: %s)",
929 System.identityHashCode(this),
Daniel Sandler4f91efd2013-04-25 16:38:41 -0400930 this.sbn.getPackageName(), this.sbn.getUser(), this.sbn.getId(), this.sbn.getTag(),
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400931 this.sbn.getScore(), this.sbn.getNotification());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 }
933 }
934
935 private static final class ToastRecord
936 {
937 final int pid;
938 final String pkg;
939 final ITransientNotification callback;
940 int duration;
941
942 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
943 {
944 this.pid = pid;
945 this.pkg = pkg;
946 this.callback = callback;
947 this.duration = duration;
948 }
949
950 void update(int duration) {
951 this.duration = duration;
952 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 void dump(PrintWriter pw, String prefix) {
955 pw.println(prefix + this);
956 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 @Override
959 public final String toString()
960 {
961 return "ToastRecord{"
962 + Integer.toHexString(System.identityHashCode(this))
963 + " pkg=" + pkg
964 + " callback=" + callback
965 + " duration=" + duration;
966 }
967 }
968
Joe Onorato089de882010-04-12 08:18:45 -0700969 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
970 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971
972 public void onSetDisabled(int status) {
973 synchronized (mNotificationList) {
974 mDisabledNotifications = status;
975 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
976 // cancel whatever's going on
977 long identity = Binder.clearCallingIdentity();
978 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700979 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
980 if (player != null) {
981 player.stopAsync();
982 }
983 } catch (RemoteException e) {
984 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 Binder.restoreCallingIdentity(identity);
986 }
987
988 identity = Binder.clearCallingIdentity();
989 try {
990 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700991 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 Binder.restoreCallingIdentity(identity);
993 }
994 }
995 }
996 }
997
998 public void onClearAll() {
Dianne Hackborn41203752012-08-31 14:05:51 -0700999 // XXX to be totally correct, the caller should tell us which user
1000 // this is for.
1001 cancelAll(ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
1003
Fred Quintana6ecaff12009-09-25 14:23:13 -07001004 public void onNotificationClick(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001005 // XXX to be totally correct, the caller should tell us which user
1006 // this is for.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001007 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackborn41203752012-08-31 14:05:51 -07001008 Notification.FLAG_FOREGROUND_SERVICE, false,
1009 ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 }
1011
Daniel Sandler0f0b11c2010-08-04 15:54:58 -04001012 public void onNotificationClear(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001013 // XXX to be totally correct, the caller should tell us which user
1014 // this is for.
Joe Onorato46439ce2010-11-19 13:56:21 -08001015 cancelNotification(pkg, tag, id, 0,
1016 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
Dianne Hackborn41203752012-08-31 14:05:51 -07001017 true, ActivityManager.getCurrentUser());
Daniel Sandler0f0b11c2010-08-04 15:54:58 -04001018 }
1019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 public void onPanelRevealed() {
1021 synchronized (mNotificationList) {
1022 // sound
1023 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 long identity = Binder.clearCallingIdentity();
1026 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001027 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1028 if (player != null) {
1029 player.stopAsync();
1030 }
1031 } catch (RemoteException e) {
1032 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 Binder.restoreCallingIdentity(identity);
1034 }
1035
1036 // vibrate
1037 mVibrateNotification = null;
1038 identity = Binder.clearCallingIdentity();
1039 try {
1040 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -07001041 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 Binder.restoreCallingIdentity(identity);
1043 }
1044
1045 // light
1046 mLights.clear();
1047 mLedNotification = null;
1048 updateLightsLocked();
1049 }
1050 }
Joe Onorato005847b2010-06-04 16:08:02 -04001051
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001052 public void onNotificationError(String pkg, String tag, int id,
1053 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001054 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
1055 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Dianne Hackborn41203752012-08-31 14:05:51 -07001056 // XXX to be totally correct, the caller should tell us which user
1057 // this is for.
1058 cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid));
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001059 long ident = Binder.clearCallingIdentity();
1060 try {
1061 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
1062 "Bad notification posted from package " + pkg
1063 + ": " + message);
1064 } catch (RemoteException e) {
1065 }
1066 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -04001067 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 };
1069
1070 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
1071 @Override
1072 public void onReceive(Context context, Intent intent) {
1073 String action = intent.getAction();
1074
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001075 boolean queryRestart = false;
Daniel Sandler26ece572012-06-01 15:38:46 -04001076 boolean packageChanged = false;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001077
Chris Wren3da73022013-05-10 14:41:21 -04001078 if (action.equals(Intent.ACTION_PACKAGE_ADDED)
1079 || action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001080 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandler26ece572012-06-01 15:38:46 -04001081 || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED))
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001082 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001083 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001084 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001085 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001086 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001087 } else if (queryRestart) {
1088 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001089 } else {
1090 Uri uri = intent.getData();
1091 if (uri == null) {
1092 return;
1093 }
1094 String pkgName = uri.getSchemeSpecificPart();
1095 if (pkgName == null) {
1096 return;
1097 }
Daniel Sandler26ece572012-06-01 15:38:46 -04001098 if (packageChanged) {
1099 // We cancel notifications for packages which have just been disabled
1100 final int enabled = mContext.getPackageManager()
1101 .getApplicationEnabledSetting(pkgName);
1102 if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
1103 || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
1104 return;
1105 }
1106 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001107 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001109
1110 boolean anyListenersInvolved = false;
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001111 if (pkgList != null && (pkgList.length > 0)) {
1112 for (String pkgName : pkgList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001113 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart,
1114 UserHandle.USER_ALL);
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001115 if (mEnabledListenerPackageNames.contains(pkgName)) {
1116 anyListenersInvolved = true;
1117 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001120
1121 if (anyListenersInvolved) {
1122 // make sure we're still bound to any of our
1123 // listeners who may have just upgraded
1124 rebindListenerServices();
1125 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001126 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
1127 // Keep track of screen on/off state, but do not turn off the notification light
1128 // until user passes through the lock screen or views the notification.
1129 mScreenOn = true;
1130 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
1131 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001132 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001133 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
1134 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001135 updateNotificationPulse();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001136 } else if (action.equals(Intent.ACTION_USER_STOPPED)) {
1137 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
1138 if (userHandle >= 0) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001139 cancelAllNotificationsInt(null, 0, 0, true, userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001140 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001141 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
1142 // turn off LED when user passes through lock screen
1143 mNotificationLight.turnOff();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001144 } else if (action.equals(Intent.ACTION_USER_SWITCHED)) {
1145 // reload per-user settings
1146 mSettingsObserver.update(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 }
1148 }
1149 };
1150
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001151 class SettingsObserver extends ContentObserver {
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001152 private final Uri NOTIFICATION_LIGHT_PULSE_URI
1153 = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE);
1154
1155 private final Uri ENABLED_NOTIFICATION_LISTENERS_URI
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001156 = Settings.Secure.getUriFor(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001157
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001158 SettingsObserver(Handler handler) {
1159 super(handler);
1160 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001161
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001162 void observe() {
1163 ContentResolver resolver = mContext.getContentResolver();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001164 resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI,
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001165 false, this, UserHandle.USER_ALL);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001166 resolver.registerContentObserver(ENABLED_NOTIFICATION_LISTENERS_URI,
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001167 false, this, UserHandle.USER_ALL);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001168 update(null);
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001169 }
1170
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001171 @Override public void onChange(boolean selfChange, Uri uri) {
1172 update(uri);
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001173 }
1174
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001175 public void update(Uri uri) {
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001176 ContentResolver resolver = mContext.getContentResolver();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001177 if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) {
1178 boolean pulseEnabled = Settings.System.getInt(resolver,
1179 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
1180 if (mNotificationPulseEnabled != pulseEnabled) {
1181 mNotificationPulseEnabled = pulseEnabled;
1182 updateNotificationPulse();
1183 }
1184 }
1185 if (uri == null || ENABLED_NOTIFICATION_LISTENERS_URI.equals(uri)) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001186 rebindListenerServices();
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001187 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001188 }
1189 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001190
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001191 private SettingsObserver mSettingsObserver;
1192
Daniel Sandleredbb3802012-11-13 20:49:47 -08001193 static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) {
1194 int[] ar = r.getIntArray(resid);
1195 if (ar == null) {
1196 return def;
1197 }
1198 final int len = ar.length > maxlen ? maxlen : ar.length;
1199 long[] out = new long[len];
1200 for (int i=0; i<len; i++) {
1201 out[i] = ar[i];
1202 }
1203 return out;
1204 }
1205
Joe Onorato089de882010-04-12 08:18:45 -07001206 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -05001207 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 {
1209 super();
1210 mContext = context;
Jeff Brownc2346132012-04-13 01:55:38 -07001211 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 mAm = ActivityManagerNative.getDefault();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001213 mUserManager = (UserManager)context.getSystemService(Context.USER_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -08001216
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001217 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
1218
1219 importOldBlockDb();
Daniel Sandler0da673f2012-04-11 12:33:16 -04001220
Joe Onorato089de882010-04-12 08:18:45 -07001221 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 statusBar.setNotificationCallbacks(mNotificationCallbacks);
1223
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001224 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
1225 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
1226
Mike Lockwood670f9322010-01-20 12:13:36 -05001227 Resources resources = mContext.getResources();
1228 mDefaultNotificationColor = resources.getColor(
1229 com.android.internal.R.color.config_defaultNotificationColor);
1230 mDefaultNotificationLedOn = resources.getInteger(
1231 com.android.internal.R.integer.config_defaultNotificationLedOn);
1232 mDefaultNotificationLedOff = resources.getInteger(
1233 com.android.internal.R.integer.config_defaultNotificationLedOff);
1234
Daniel Sandleredbb3802012-11-13 20:49:47 -08001235 mDefaultVibrationPattern = getLongArray(resources,
1236 com.android.internal.R.array.config_defaultNotificationVibePattern,
1237 VIBRATE_PATTERN_MAXLEN,
1238 DEFAULT_VIBRATE_PATTERN);
1239
1240 mFallbackVibrationPattern = getLongArray(resources,
1241 com.android.internal.R.array.config_notificationFallbackVibePattern,
1242 VIBRATE_PATTERN_MAXLEN,
1243 DEFAULT_VIBRATE_PATTERN);
1244
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001245 // Don't start allowing notifications until the setup wizard has run once.
1246 // After that, including subsequent boots, init with notifications turned on.
1247 // This works on the first boot because the setup wizard will toggle this
1248 // flag at least once and we'll go back to 0 after that.
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001249 if (0 == Settings.Global.getInt(mContext.getContentResolver(),
1250 Settings.Global.DEVICE_PROVISIONED, 0)) {
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001251 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
1252 }
1253
Mike Lockwood35e16bf2010-11-30 19:53:36 -05001254 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001256 filter.addAction(Intent.ACTION_SCREEN_ON);
1257 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001258 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001259 filter.addAction(Intent.ACTION_USER_PRESENT);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001260 filter.addAction(Intent.ACTION_USER_STOPPED);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001261 filter.addAction(Intent.ACTION_USER_SWITCHED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001263 IntentFilter pkgFilter = new IntentFilter();
Chris Wren3da73022013-05-10 14:41:21 -04001264 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001265 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -04001266 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001267 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
1268 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
1269 pkgFilter.addDataScheme("package");
1270 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001271 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001272 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001273
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001274 mSettingsObserver = new SettingsObserver(mHandler);
1275 mSettingsObserver.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
1277
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001278 /**
1279 * Read the old XML-based app block database and import those blockages into the AppOps system.
1280 */
1281 private void importOldBlockDb() {
1282 loadBlockDb();
1283
1284 PackageManager pm = mContext.getPackageManager();
1285 for (String pkg : mBlockedPackages) {
1286 PackageInfo info = null;
1287 try {
1288 info = pm.getPackageInfo(pkg, 0);
1289 setNotificationsEnabledForPackage(pkg, info.applicationInfo.uid, false);
1290 } catch (NameNotFoundException e) {
1291 // forget you
1292 }
1293 }
1294 mBlockedPackages.clear();
1295 if (mPolicyFile != null) {
1296 mPolicyFile.delete();
1297 }
1298 }
1299
Joe Onorato30275482009-07-08 17:09:14 -07001300 void systemReady() {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001301 mAudioService = IAudioService.Stub.asInterface(
1302 ServiceManager.getService(Context.AUDIO_SERVICE));
1303
Joe Onorato30275482009-07-08 17:09:14 -07001304 // no beeping until we're basically done booting
1305 mSystemReady = true;
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001306
1307 // make sure our listener services are properly bound
1308 rebindListenerServices();
Joe Onorato30275482009-07-08 17:09:14 -07001309 }
1310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 // Toasts
1312 // ============================================================================
1313 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
1314 {
Daniel Sandlera7035902010-03-30 15:45:31 -04001315 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316
1317 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001318 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 return ;
1320 }
1321
Daniel Sandler0da673f2012-04-11 12:33:16 -04001322 final boolean isSystemToast = ("android".equals(pkg));
1323
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001324 if (ENABLE_BLOCKED_TOASTS && !noteNotificationOp(pkg, Binder.getCallingUid())) {
1325 if (!isSystemToast) {
1326 Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
1327 return;
1328 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001329 }
1330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 synchronized (mToastQueue) {
1332 int callingPid = Binder.getCallingPid();
1333 long callingId = Binder.clearCallingIdentity();
1334 try {
1335 ToastRecord record;
1336 int index = indexOfToastLocked(pkg, callback);
1337 // If it's already in the queue, we update it in place, we don't
1338 // move it to the end of the queue.
1339 if (index >= 0) {
1340 record = mToastQueue.get(index);
1341 record.update(duration);
1342 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -08001343 // Limit the number of toasts that any given package except the android
1344 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -04001345 if (!isSystemToast) {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -08001346 int count = 0;
1347 final int N = mToastQueue.size();
1348 for (int i=0; i<N; i++) {
1349 final ToastRecord r = mToastQueue.get(i);
1350 if (r.pkg.equals(pkg)) {
1351 count++;
1352 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
1353 Slog.e(TAG, "Package has already posted " + count
1354 + " toasts. Not showing more. Package=" + pkg);
1355 return;
1356 }
1357 }
1358 }
1359 }
1360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 record = new ToastRecord(callingPid, pkg, callback, duration);
1362 mToastQueue.add(record);
1363 index = mToastQueue.size() - 1;
1364 keepProcessAliveLocked(callingPid);
1365 }
1366 // If it's at index 0, it's the current toast. It doesn't matter if it's
1367 // new or just been updated. Call back and tell it to show itself.
1368 // If the callback fails, this will remove it from the list, so don't
1369 // assume that it's valid after this.
1370 if (index == 0) {
1371 showNextToastLocked();
1372 }
1373 } finally {
1374 Binder.restoreCallingIdentity(callingId);
1375 }
1376 }
1377 }
1378
1379 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001380 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381
1382 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001383 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 return ;
1385 }
1386
1387 synchronized (mToastQueue) {
1388 long callingId = Binder.clearCallingIdentity();
1389 try {
1390 int index = indexOfToastLocked(pkg, callback);
1391 if (index >= 0) {
1392 cancelToastLocked(index);
1393 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001394 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 }
1396 } finally {
1397 Binder.restoreCallingIdentity(callingId);
1398 }
1399 }
1400 }
1401
1402 private void showNextToastLocked() {
1403 ToastRecord record = mToastQueue.get(0);
1404 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001405 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 try {
1407 record.callback.show();
1408 scheduleTimeoutLocked(record, false);
1409 return;
1410 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001411 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 + " in package " + record.pkg);
1413 // remove it from the list and let the process die
1414 int index = mToastQueue.indexOf(record);
1415 if (index >= 0) {
1416 mToastQueue.remove(index);
1417 }
1418 keepProcessAliveLocked(record.pid);
1419 if (mToastQueue.size() > 0) {
1420 record = mToastQueue.get(0);
1421 } else {
1422 record = null;
1423 }
1424 }
1425 }
1426 }
1427
1428 private void cancelToastLocked(int index) {
1429 ToastRecord record = mToastQueue.get(index);
1430 try {
1431 record.callback.hide();
1432 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001433 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 + " in package " + record.pkg);
1435 // don't worry about this, we're about to remove it from
1436 // the list anyway
1437 }
1438 mToastQueue.remove(index);
1439 keepProcessAliveLocked(record.pid);
1440 if (mToastQueue.size() > 0) {
1441 // Show the next one. If the callback fails, this will remove
1442 // it from the list, so don't assume that the list hasn't changed
1443 // after this point.
1444 showNextToastLocked();
1445 }
1446 }
1447
1448 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
1449 {
1450 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
1451 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
1452 mHandler.removeCallbacksAndMessages(r);
1453 mHandler.sendMessageDelayed(m, delay);
1454 }
1455
1456 private void handleTimeout(ToastRecord record)
1457 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001458 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 synchronized (mToastQueue) {
1460 int index = indexOfToastLocked(record.pkg, record.callback);
1461 if (index >= 0) {
1462 cancelToastLocked(index);
1463 }
1464 }
1465 }
1466
1467 // lock on mToastQueue
1468 private int indexOfToastLocked(String pkg, ITransientNotification callback)
1469 {
1470 IBinder cbak = callback.asBinder();
1471 ArrayList<ToastRecord> list = mToastQueue;
1472 int len = list.size();
1473 for (int i=0; i<len; i++) {
1474 ToastRecord r = list.get(i);
1475 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
1476 return i;
1477 }
1478 }
1479 return -1;
1480 }
1481
1482 // lock on mToastQueue
1483 private void keepProcessAliveLocked(int pid)
1484 {
1485 int toastCount = 0; // toasts from this pid
1486 ArrayList<ToastRecord> list = mToastQueue;
1487 int N = list.size();
1488 for (int i=0; i<N; i++) {
1489 ToastRecord r = list.get(i);
1490 if (r.pid == pid) {
1491 toastCount++;
1492 }
1493 }
1494 try {
1495 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
1496 } catch (RemoteException e) {
1497 // Shouldn't happen.
1498 }
1499 }
1500
1501 private final class WorkerHandler extends Handler
1502 {
1503 @Override
1504 public void handleMessage(Message msg)
1505 {
1506 switch (msg.what)
1507 {
1508 case MESSAGE_TIMEOUT:
1509 handleTimeout((ToastRecord)msg.obj);
1510 break;
1511 }
1512 }
1513 }
1514
1515
1516 // Notifications
1517 // ============================================================================
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001518 public void enqueueNotificationWithTag(String pkg, String basePkg, String tag, int id,
1519 Notification notification, int[] idOut, int userId)
Fred Quintana6ecaff12009-09-25 14:23:13 -07001520 {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001521 enqueueNotificationInternal(pkg, basePkg, Binder.getCallingUid(), Binder.getCallingPid(),
Dianne Hackborn41203752012-08-31 14:05:51 -07001522 tag, id, notification, idOut, userId);
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001523 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001524
1525 private final static int clamp(int x, int low, int high) {
1526 return (x < low) ? low : ((x > high) ? high : x);
Daniel Sandlere40451a2011-02-03 14:51:35 -05001527 }
1528
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001529 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
1530 // uid/pid of another application)
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001531 public void enqueueNotificationInternal(String pkg, String basePkg, int callingUid,
1532 int callingPid, String tag, int id, Notification notification, int[] idOut, int userId)
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001533 {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001534 if (DBG) {
1535 Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
1536 }
1537 checkCallerIsSystemOrSameApp(pkg);
1538 final boolean isSystemNotification = ("android".equals(pkg));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001539
Dianne Hackborn41203752012-08-31 14:05:51 -07001540 userId = ActivityManager.handleIncomingUser(callingPid,
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001541 callingUid, userId, true, false, "enqueueNotification", pkg);
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -07001542 final UserHandle user = new UserHandle(userId);
Dianne Hackborn41203752012-08-31 14:05:51 -07001543
Joe Onoratobd73d012010-06-04 11:44:54 -07001544 // Limit the number of notifications that any given package except the android
1545 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -04001546 if (!isSystemNotification) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001547 synchronized (mNotificationList) {
1548 int count = 0;
1549 final int N = mNotificationList.size();
1550 for (int i=0; i<N; i++) {
1551 final NotificationRecord r = mNotificationList.get(i);
Daniel Sandler4f91efd2013-04-25 16:38:41 -04001552 if (r.sbn.getPackageName().equals(pkg) && r.sbn.getUserId() == userId) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001553 count++;
1554 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
1555 Slog.e(TAG, "Package has already posted " + count
1556 + " notifications. Not showing more. package=" + pkg);
1557 return;
1558 }
1559 }
1560 }
1561 }
1562 }
1563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 // This conditional is a dirty hack to limit the logging done on
1565 // behalf of the download manager without affecting other apps.
1566 if (!pkg.equals("com.android.providers.downloads")
1567 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001568 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001569 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 }
1571
1572 if (pkg == null || notification == null) {
1573 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
1574 + " id=" + id + " notification=" + notification);
1575 }
1576 if (notification.icon != 0) {
1577 if (notification.contentView == null) {
1578 throw new IllegalArgumentException("contentView required: pkg=" + pkg
1579 + " id=" + id + " notification=" + notification);
1580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 }
1582
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001583 // === Scoring ===
Daniel Sandler0da673f2012-04-11 12:33:16 -04001584
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001585 // 0. Sanitize inputs
1586 notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
1587 // Migrate notification flags to scores
1588 if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
1589 if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
Daniel Sandler49a2ad12012-03-28 15:46:39 -04001590 } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001591 if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
1592 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001593
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001594 // 1. initial score: buckets of 10, around the app
Daniel Sandler0da673f2012-04-11 12:33:16 -04001595 int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001596
Daniel Sandler0da673f2012-04-11 12:33:16 -04001597 // 2. Consult external heuristics (TBD)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001598
Daniel Sandler0da673f2012-04-11 12:33:16 -04001599 // 3. Apply local rules
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001600
1601 // blocked apps
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001602 if (ENABLE_BLOCKED_NOTIFICATIONS && !noteNotificationOp(pkg, callingUid)) {
1603 if (!isSystemNotification) {
1604 score = JUNK_SCORE;
1605 Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
1606 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001607 }
1608
1609 if (DBG) {
1610 Slog.v(TAG, "Assigned score=" + score + " to " + notification);
1611 }
1612
1613 if (score < SCORE_DISPLAY_THRESHOLD) {
1614 // Notification will be blocked because the score is too low.
1615 return;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001616 }
1617
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001618 // Should this notification make noise, vibe, or use the LED?
1619 final boolean canInterrupt = (score >= SCORE_INTERRUPTION_THRESHOLD);
1620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 synchronized (mNotificationList) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001622 final StatusBarNotification n = new StatusBarNotification(
1623 pkg, id, tag, callingUid, callingPid, score, notification, user);
1624 NotificationRecord r = new NotificationRecord(n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 NotificationRecord old = null;
1626
Dianne Hackborn41203752012-08-31 14:05:51 -07001627 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 if (index < 0) {
1629 mNotificationList.add(r);
1630 } else {
1631 old = mNotificationList.remove(index);
1632 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001633 // Make sure we don't lose the foreground service state.
1634 if (old != null) {
1635 notification.flags |=
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001636 old.getNotification().flags&Notification.FLAG_FOREGROUND_SERVICE;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001639
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001640 // Ensure if this is a foreground service that the proper additional
1641 // flags are set.
1642 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
1643 notification.flags |= Notification.FLAG_ONGOING_EVENT
1644 | Notification.FLAG_NO_CLEAR;
1645 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001646
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001647 final int currentUser;
1648 final long token = Binder.clearCallingIdentity();
1649 try {
1650 currentUser = ActivityManager.getCurrentUser();
1651 } finally {
1652 Binder.restoreCallingIdentity(token);
1653 }
1654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 if (notification.icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 if (old != null && old.statusBarKey != null) {
1657 r.statusBarKey = old.statusBarKey;
1658 long identity = Binder.clearCallingIdentity();
1659 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001660 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 }
1662 finally {
1663 Binder.restoreCallingIdentity(identity);
1664 }
1665 } else {
1666 long identity = Binder.clearCallingIdentity();
1667 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001668 r.statusBarKey = mStatusBar.addNotification(n);
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04001669 if ((n.getNotification().flags & Notification.FLAG_SHOW_LIGHTS) != 0
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001670 && canInterrupt) {
Mike Lockwoodece18ef2012-02-13 20:42:19 -08001671 mAttentionLight.pulse();
1672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 }
1674 finally {
1675 Binder.restoreCallingIdentity(identity);
1676 }
1677 }
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001678 // Send accessibility events only for the current user.
1679 if (currentUser == userId) {
1680 sendAccessibilityEvent(notification, pkg);
1681 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001682
Daniel Sandler09a247e2013-02-14 10:24:17 -05001683 notifyPostedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -05001685 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 if (old != null && old.statusBarKey != null) {
1687 long identity = Binder.clearCallingIdentity();
1688 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001689 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 }
1691 finally {
1692 Binder.restoreCallingIdentity(identity);
1693 }
Daniel Sandler09a247e2013-02-14 10:24:17 -05001694
1695 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001697 return; // do not play sounds, show lights, etc. for invalid notifications
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 }
1699
1700 // If we're not supposed to beep, vibrate, etc. then don't.
1701 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
1702 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -07001703 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001704 && (r.getUserId() == UserHandle.USER_ALL ||
1705 (r.getUserId() == userId && r.getUserId() == currentUser))
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001706 && canInterrupt
Joe Onorato30275482009-07-08 17:09:14 -07001707 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -08001708
1709 final AudioManager audioManager = (AudioManager) mContext
1710 .getSystemService(Context.AUDIO_SERVICE);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 // sound
Daniel Sandler31475232013-04-17 00:17:22 -04001713
1714 // should we use the default notification sound? (indicated either by DEFAULT_SOUND
1715 // or because notification.sound is pointing at Settings.System.NOTIFICATION_SOUND)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 final boolean useDefaultSound =
Daniel Sandler31475232013-04-17 00:17:22 -04001717 (notification.defaults & Notification.DEFAULT_SOUND) != 0
1718 || Settings.System.DEFAULT_NOTIFICATION_URI.equals(notification.sound);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001719
1720 Uri soundUri = null;
1721 boolean hasValidSound = false;
1722
1723 if (useDefaultSound) {
1724 soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
1725
1726 // check to see if the default notification sound is silent
1727 ContentResolver resolver = mContext.getContentResolver();
1728 hasValidSound = Settings.System.getString(resolver,
1729 Settings.System.NOTIFICATION_SOUND) != null;
1730 } else if (notification.sound != null) {
1731 soundUri = notification.sound;
1732 hasValidSound = (soundUri != null);
1733 }
1734
1735 if (hasValidSound) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
1737 int audioStreamType;
1738 if (notification.audioStreamType >= 0) {
1739 audioStreamType = notification.audioStreamType;
1740 } else {
1741 audioStreamType = DEFAULT_STREAM_TYPE;
1742 }
1743 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -08001744 // do not play notifications if stream volume is 0
Jean-Michel Trivid6770542012-10-10 12:03:41 -07001745 // (typically because ringer mode is silent) or if speech recognition is active.
1746 if ((audioManager.getStreamVolume(audioStreamType) != 0)
1747 && !audioManager.isSpeechRecognitionActive()) {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001748 final long identity = Binder.clearCallingIdentity();
Eric Laurent524dc042009-11-27 05:07:55 -08001749 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001750 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1751 if (player != null) {
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001752 player.playAsync(soundUri, user, looping, audioStreamType);
Jeff Sharkey098d5802012-04-26 17:30:34 -07001753 }
1754 } catch (RemoteException e) {
1755 } finally {
Eric Laurent524dc042009-11-27 05:07:55 -08001756 Binder.restoreCallingIdentity(identity);
1757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 }
1759 }
1760
1761 // vibrate
Daniel Sandleredbb3802012-11-13 20:49:47 -08001762 // Does the notification want to specify its own vibration?
1763 final boolean hasCustomVibrate = notification.vibrate != null;
1764
David Agnew71789e12012-11-09 23:03:26 -05001765 // new in 4.2: if there was supposed to be a sound and we're in vibrate mode,
Daniel Sandler4a7a9b92012-11-20 12:59:41 -05001766 // and no other vibration is specified, we fall back to vibration
David Agnew71789e12012-11-09 23:03:26 -05001767 final boolean convertSoundToVibration =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001768 !hasCustomVibrate
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001769 && hasValidSound
David Agnew71789e12012-11-09 23:03:26 -05001770 && (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
1771
Daniel Sandler4a7a9b92012-11-20 12:59:41 -05001772 // The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 final boolean useDefaultVibrate =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001774 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
David Agnew71789e12012-11-09 23:03:26 -05001775
Daniel Sandleredbb3802012-11-13 20:49:47 -08001776 if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate)
Eric Laurentbffc3d12012-05-07 17:43:49 -07001777 && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 mVibrateNotification = r;
1779
Daniel Sandleredbb3802012-11-13 20:49:47 -08001780 if (useDefaultVibrate || convertSoundToVibration) {
1781 // Escalate privileges so we can use the vibrator even if the notifying app
1782 // does not have the VIBRATE permission.
1783 long identity = Binder.clearCallingIdentity();
1784 try {
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04001785 mVibrator.vibrate(r.sbn.getUid(), r.sbn.getBasePkg(),
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001786 useDefaultVibrate ? mDefaultVibrationPattern
1787 : mFallbackVibrationPattern,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001788 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1789 } finally {
1790 Binder.restoreCallingIdentity(identity);
1791 }
1792 } else if (notification.vibrate.length > 1) {
1793 // If you want your own vibration pattern, you need the VIBRATE permission
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04001794 mVibrator.vibrate(r.sbn.getUid(), r.sbn.getBasePkg(), notification.vibrate,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001795 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 }
1798 }
1799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 // light
1801 // the most recent thing gets the light
1802 mLights.remove(old);
1803 if (mLedNotification == old) {
1804 mLedNotification = null;
1805 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001806 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001808 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1809 && canInterrupt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 mLights.add(r);
1811 updateLightsLocked();
1812 } else {
1813 if (old != null
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001814 && ((old.getFlags() & Notification.FLAG_SHOW_LIGHTS) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 updateLightsLocked();
1816 }
1817 }
1818 }
1819
1820 idOut[0] = id;
1821 }
1822
Joe Onorato30275482009-07-08 17:09:14 -07001823 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001824 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
1825 if (!manager.isEnabled()) {
1826 return;
1827 }
1828
1829 AccessibilityEvent event =
1830 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
1831 event.setPackageName(packageName);
1832 event.setClassName(Notification.class.getName());
1833 event.setParcelableData(notification);
1834 CharSequence tickerText = notification.tickerText;
1835 if (!TextUtils.isEmpty(tickerText)) {
1836 event.getText().add(tickerText);
1837 }
1838
1839 manager.sendAccessibilityEvent(event);
1840 }
1841
Joe Onorato46439ce2010-11-19 13:56:21 -08001842 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
1843 // tell the app
1844 if (sendDelete) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001845 if (r.getNotification().deleteIntent != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001846 try {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001847 r.getNotification().deleteIntent.send();
Joe Onorato46439ce2010-11-19 13:56:21 -08001848 } catch (PendingIntent.CanceledException ex) {
1849 // do nothing - there's no relevant way to recover, and
1850 // no reason to let this propagate
Daniel Sandler4f91efd2013-04-25 16:38:41 -04001851 Slog.w(TAG, "canceled PendingIntent for " + r.sbn.getPackageName(), ex);
Joe Onorato46439ce2010-11-19 13:56:21 -08001852 }
1853 }
1854 }
1855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 // status bar
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001857 if (r.getNotification().icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 long identity = Binder.clearCallingIdentity();
1859 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001860 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 }
1862 finally {
1863 Binder.restoreCallingIdentity(identity);
1864 }
1865 r.statusBarKey = null;
Daniel Sandler09a247e2013-02-14 10:24:17 -05001866 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 }
1868
1869 // sound
1870 if (mSoundNotification == r) {
1871 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001872 final long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001874 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1875 if (player != null) {
1876 player.stopAsync();
1877 }
1878 } catch (RemoteException e) {
1879 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 Binder.restoreCallingIdentity(identity);
1881 }
1882 }
1883
1884 // vibrate
1885 if (mVibrateNotification == r) {
1886 mVibrateNotification = null;
1887 long identity = Binder.clearCallingIdentity();
1888 try {
1889 mVibrator.cancel();
1890 }
1891 finally {
1892 Binder.restoreCallingIdentity(identity);
1893 }
1894 }
1895
1896 // light
1897 mLights.remove(r);
1898 if (mLedNotification == r) {
1899 mLedNotification = null;
1900 }
Daniel Sandler23d7c702013-03-07 16:32:06 -05001901
1902 // Save it for users of getHistoricalNotifications()
1903 mArchive.record(r.sbn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 }
1905
1906 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001907 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001908 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 */
Fred Quintana6ecaff12009-09-25 14:23:13 -07001910 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001911 int mustNotHaveFlags, boolean sendDelete, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001912 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001913 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914
1915 synchronized (mNotificationList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001916 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001918 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001919
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001920 if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 return;
1922 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001923 if ((r.getNotification().flags & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001924 return;
1925 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 mNotificationList.remove(index);
1928
Joe Onorato46439ce2010-11-19 13:56:21 -08001929 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 updateLightsLocked();
1931 }
1932 }
1933 }
1934
1935 /**
Daniel Sandler321e9c52012-10-12 10:59:26 -07001936 * Determine whether the userId applies to the notification in question, either because
1937 * they match exactly, or one of them is USER_ALL (which is treated as a wildcard).
1938 */
1939 private boolean notificationMatchesUserId(NotificationRecord r, int userId) {
1940 return
1941 // looking for USER_ALL notifications? match everything
1942 userId == UserHandle.USER_ALL
1943 // a notification sent to USER_ALL matches any query
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001944 || r.getUserId() == UserHandle.USER_ALL
Daniel Sandler321e9c52012-10-12 10:59:26 -07001945 // an exact user match
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001946 || r.getUserId() == userId;
Daniel Sandler321e9c52012-10-12 10:59:26 -07001947 }
1948
1949 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 * Cancels all notifications from a given package that have all of the
1951 * {@code mustHaveFlags}.
1952 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001953 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001954 int mustNotHaveFlags, boolean doit, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001955 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, userId,
1956 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957
1958 synchronized (mNotificationList) {
1959 final int N = mNotificationList.size();
1960 boolean canceledSomething = false;
1961 for (int i = N-1; i >= 0; --i) {
1962 NotificationRecord r = mNotificationList.get(i);
Daniel Sandler321e9c52012-10-12 10:59:26 -07001963 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001964 continue;
1965 }
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001966 // Don't remove notifications to all, if there's no package name specified
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001967 if (r.getUserId() == UserHandle.USER_ALL && pkg == null) {
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001968 continue;
1969 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001970 if ((r.getFlags() & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 continue;
1972 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001973 if ((r.getFlags() & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001974 continue;
1975 }
Daniel Sandler4f91efd2013-04-25 16:38:41 -04001976 if (pkg != null && !r.sbn.getPackageName().equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 continue;
1978 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001979 canceledSomething = true;
1980 if (!doit) {
1981 return true;
1982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001984 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 }
1986 if (canceledSomething) {
1987 updateLightsLocked();
1988 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001989 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 }
1991 }
1992
Dianne Hackborn41203752012-08-31 14:05:51 -07001993 public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001994 checkCallerIsSystemOrSameApp(pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001995 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001996 Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001997 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001998 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001999 Binder.getCallingUid() == Process.SYSTEM_UID
Dianne Hackborn41203752012-08-31 14:05:51 -07002000 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 }
2002
Dianne Hackborn41203752012-08-31 14:05:51 -07002003 public void cancelAllNotifications(String pkg, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04002004 checkCallerIsSystemOrSameApp(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002005
Dianne Hackborn41203752012-08-31 14:05:51 -07002006 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07002007 Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07002008
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002009 // Calling from user space, don't allow the canceling of actively
2010 // running foreground services.
Dianne Hackborn41203752012-08-31 14:05:51 -07002011 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 }
2013
Daniel Sandler0da673f2012-04-11 12:33:16 -04002014 void checkCallerIsSystem() {
2015 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07002016 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04002017 return;
2018 }
2019 throw new SecurityException("Disallowed call for uid " + uid);
2020 }
2021
2022 void checkCallerIsSystemOrSameApp(String pkg) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002023 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07002024 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002025 return;
2026 }
2027 try {
Amith Yamasanif203aee2012-08-29 18:41:53 -07002028 ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
2029 pkg, 0, UserHandle.getCallingUserId());
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002030 if (!UserHandle.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002031 throw new SecurityException("Calling uid " + uid + " gave package"
2032 + pkg + " which is owned by uid " + ai.uid);
2033 }
Amith Yamasanif203aee2012-08-29 18:41:53 -07002034 } catch (RemoteException re) {
2035 throw new SecurityException("Unknown package " + pkg + "\n" + re);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002036 }
2037 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002038
Dianne Hackborn41203752012-08-31 14:05:51 -07002039 void cancelAll(int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 synchronized (mNotificationList) {
2041 final int N = mNotificationList.size();
2042 for (int i=N-1; i>=0; i--) {
2043 NotificationRecord r = mNotificationList.get(i);
2044
Daniel Sandler321e9c52012-10-12 10:59:26 -07002045 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07002046 continue;
2047 }
2048
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002049 if ((r.getFlags() & (Notification.FLAG_ONGOING_EVENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08002052 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 }
2054 }
2055
2056 updateLightsLocked();
2057 }
2058 }
2059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 // lock on mNotificationList
2061 private void updateLightsLocked()
2062 {
The Android Open Source Project10592532009-03-18 17:39:46 -07002063 // handle notification lights
2064 if (mLedNotification == null) {
2065 // get next notification, if any
2066 int n = mLights.size();
2067 if (n > 0) {
2068 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 }
2070 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05002071
Mike Lockwood63b5ad92011-08-30 09:55:30 -04002072 // Don't flash while we are in a call or screen is on
2073 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002074 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07002075 } else {
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04002076 final Notification ledno = mLedNotification.sbn.getNotification();
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002077 int ledARGB = ledno.ledARGB;
2078 int ledOnMS = ledno.ledOnMS;
2079 int ledOffMS = ledno.ledOffMS;
2080 if ((ledno.defaults & Notification.DEFAULT_LIGHTS) != 0) {
Mike Lockwood670f9322010-01-20 12:13:36 -05002081 ledARGB = mDefaultNotificationColor;
2082 ledOnMS = mDefaultNotificationLedOn;
2083 ledOffMS = mDefaultNotificationLedOff;
2084 }
2085 if (mNotificationPulseEnabled) {
2086 // pulse repeatedly
2087 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
2088 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05002089 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 }
2092
2093 // lock on mNotificationList
Dianne Hackborn41203752012-08-31 14:05:51 -07002094 private int indexOfNotificationLocked(String pkg, String tag, int id, int userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 {
2096 ArrayList<NotificationRecord> list = mNotificationList;
2097 final int len = list.size();
2098 for (int i=0; i<len; i++) {
2099 NotificationRecord r = list.get(i);
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04002100 if (!notificationMatchesUserId(r, userId) || r.sbn.getId() != id) {
Dianne Hackborn41203752012-08-31 14:05:51 -07002101 continue;
2102 }
Fred Quintana6ecaff12009-09-25 14:23:13 -07002103 if (tag == null) {
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04002104 if (r.sbn.getTag() != null) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07002105 continue;
2106 }
2107 } else {
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04002108 if (!tag.equals(r.sbn.getTag())) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07002109 continue;
2110 }
2111 }
Daniel Sandler4f91efd2013-04-25 16:38:41 -04002112 if (r.sbn.getPackageName().equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 return i;
2114 }
2115 }
2116 return -1;
2117 }
2118
Mike Lockwoodc22404a2009-12-02 11:15:02 -05002119 private void updateNotificationPulse() {
2120 synchronized (mNotificationList) {
2121 updateLightsLocked();
2122 }
2123 }
2124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 // ======================================================================
2126 @Override
2127 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2128 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2129 != PackageManager.PERMISSION_GRANTED) {
2130 pw.println("Permission Denial: can't dump NotificationManager from from pid="
2131 + Binder.getCallingPid()
2132 + ", uid=" + Binder.getCallingUid());
2133 return;
2134 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 pw.println("Current Notification Manager state:");
2137
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002138 pw.println(" Listeners (" + mEnabledListenersForCurrentUser.size()
2139 + ") enabled for current user:");
2140 for (ComponentName cmpt : mEnabledListenersForCurrentUser) {
2141 pw.println(" " + cmpt);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002142 }
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002143
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002144 pw.println(" Live listeners (" + mListeners.size() + "):");
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002145 for (NotificationListenerInfo info : mListeners) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002146 pw.println(" " + info.component
2147 + " (user " + info.userid + "): " + info.listener
2148 + (info.isSystem?" SYSTEM":""));
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002149 }
2150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 int N;
2152
2153 synchronized (mToastQueue) {
2154 N = mToastQueue.size();
2155 if (N > 0) {
2156 pw.println(" Toast Queue:");
2157 for (int i=0; i<N; i++) {
2158 mToastQueue.get(i).dump(pw, " ");
2159 }
2160 pw.println(" ");
2161 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 }
2164
2165 synchronized (mNotificationList) {
2166 N = mNotificationList.size();
2167 if (N > 0) {
2168 pw.println(" Notification List:");
2169 for (int i=0; i<N; i++) {
2170 mNotificationList.get(i).dump(pw, " ", mContext);
2171 }
2172 pw.println(" ");
2173 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 N = mLights.size();
2176 if (N > 0) {
2177 pw.println(" Lights List:");
2178 for (int i=0; i<N; i++) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002179 pw.println(" " + mLights.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 }
2181 pw.println(" ");
2182 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 pw.println(" mSoundNotification=" + mSoundNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04002186 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
2187 pw.println(" mSystemReady=" + mSystemReady);
Daniel Sandler5e62e3a2013-04-15 20:57:02 -04002188 pw.println(" mArchive=" + mArchive.toString());
2189 Iterator<StatusBarNotification> iter = mArchive.descendingIterator();
2190 int i=0;
2191 while (iter.hasNext()) {
2192 pw.println(" " + iter.next());
2193 if (++i >= 5) {
2194 if (iter.hasNext()) pw.println(" ...");
2195 break;
2196 }
2197 }
2198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 }
2200 }
2201}