blob: 1e394d6f9e4c66c1a537f7be7b520c53375be30d [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;
Daniel Sandler09a247e2013-02-14 10:24:17 -050029import android.app.INotificationListener;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.app.ITransientNotification;
31import android.app.Notification;
32import android.app.PendingIntent;
33import android.app.StatusBarManager;
34import android.content.BroadcastReceiver;
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;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070039import android.content.pm.ApplicationInfo;
Daniel Sandler4a900acd2013-01-30 14:04:10 -050040import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.pm.PackageManager;
42import android.content.pm.PackageManager.NameNotFoundException;
43import android.content.res.Resources;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070044import android.database.ContentObserver;
svetoslavganov75986cf2009-05-14 22:28:01 -070045import android.media.AudioManager;
Jeff Sharkey098d5802012-04-26 17:30:34 -070046import android.media.IAudioService;
47import android.media.IRingtonePlayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.IBinder;
52import android.os.Message;
Daniel Sandlerfde19b12013-01-17 00:21:05 -050053import android.os.Parcel;
54import android.os.Parcelable;
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Vibrator;
60import android.provider.Settings;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050061import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070062import android.text.TextUtils;
Dianne Hackborn39606a02012-07-31 17:54:35 -070063import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.util.EventLog;
65import android.util.Log;
Andy Stadler110988c2010-12-03 14:29:16 -080066import android.util.Slog;
Daniel Sandler0da673f2012-04-11 12:33:16 -040067import android.util.Xml;
svetoslavganov75986cf2009-05-14 22:28:01 -070068import android.view.accessibility.AccessibilityEvent;
69import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.Toast;
71
Jeff Sharkey098d5802012-04-26 17:30:34 -070072import com.android.internal.statusbar.StatusBarNotification;
Jeff Sharkey098d5802012-04-26 17:30:34 -070073
74import org.xmlpull.v1.XmlPullParser;
75import org.xmlpull.v1.XmlPullParserException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070076
Daniel Sandler0da673f2012-04-11 12:33:16 -040077import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import java.io.FileDescriptor;
Daniel Sandler0da673f2012-04-11 12:33:16 -040079import java.io.FileInputStream;
80import java.io.FileNotFoundException;
Daniel Sandler0da673f2012-04-11 12:33:16 -040081import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import java.io.PrintWriter;
Daniel Sandlerfde19b12013-01-17 00:21:05 -050083import java.util.ArrayDeque;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.util.ArrayList;
85import java.util.Arrays;
Daniel Sandler0da673f2012-04-11 12:33:16 -040086import java.util.HashSet;
Daniel Sandlerfde19b12013-01-17 00:21:05 -050087import java.util.Iterator;
88import java.util.NoSuchElementException;
Daniel Sandler0da673f2012-04-11 12:33:16 -040089
90import libcore.io.IoUtils;
91
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040093/** {@hide} */
94public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095{
96 private static final String TAG = "NotificationService";
97 private static final boolean DBG = false;
98
Joe Onoratobd73d012010-06-04 11:44:54 -070099 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 // message codes
102 private static final int MESSAGE_TIMEOUT = 2;
103
104 private static final int LONG_DELAY = 3500; // 3.5 seconds
105 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800106
107 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
Daniel Sandleredbb3802012-11-13 20:49:47 -0800108 private static final int VIBRATE_PATTERN_MAXLEN = 8 * 2 + 1; // up to eight bumps
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
110 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400111 private static final boolean SCORE_ONGOING_HIGHER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
Daniel Sandler0da673f2012-04-11 12:33:16 -0400113 private static final int JUNK_SCORE = -1000;
114 private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10;
115 private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER;
116
Daniel Sandler526fa0e2012-12-04 14:51:50 -0500117 // Notifications with scores below this will not interrupt the user, either via LED or
118 // sound or vibration
119 private static final int SCORE_INTERRUPTION_THRESHOLD =
120 Notification.PRIORITY_LOW * NOTIFICATION_PRIORITY_MULTIPLIER;
121
Daniel Sandler0da673f2012-04-11 12:33:16 -0400122 private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true;
123 private static final boolean ENABLE_BLOCKED_TOASTS = true;
124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 final Context mContext;
126 final IActivityManager mAm;
127 final IBinder mForegroundToken = new Binder();
128
129 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -0700130 private StatusBarManagerService mStatusBar;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500131 private LightsService.Light mNotificationLight;
132 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
Mike Lockwood670f9322010-01-20 12:13:36 -0500134 private int mDefaultNotificationColor;
135 private int mDefaultNotificationLedOn;
136 private int mDefaultNotificationLedOff;
137
Daniel Sandleredbb3802012-11-13 20:49:47 -0800138 private long[] mDefaultVibrationPattern;
139 private long[] mFallbackVibrationPattern;
140
Joe Onorato30275482009-07-08 17:09:14 -0700141 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400142 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
Jeff Sharkey098d5802012-04-26 17:30:34 -0700144 private NotificationRecord mSoundNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 private NotificationRecord mVibrateNotification;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700146
147 private IAudioService mAudioService;
Jeff Brownc2346132012-04-13 01:55:38 -0700148 private Vibrator mVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500150 // for enabling and disabling notification pulse behavior
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400151 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500152 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500153 private boolean mNotificationPulseEnabled;
154
Daniel Sandler09a247e2013-02-14 10:24:17 -0500155 // used as a mutex for access to all active notifications & listeners
Fred Quintana6ecaff12009-09-25 14:23:13 -0700156 private final ArrayList<NotificationRecord> mNotificationList =
157 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158
159 private ArrayList<ToastRecord> mToastQueue;
160
161 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700163
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500164 private final AppOpsManager mAppOps;
165
Daniel Sandler09a247e2013-02-14 10:24:17 -0500166 private ArrayList<NotificationListenerInfo> mListeners = new ArrayList<NotificationListenerInfo>();
167
Daniel Sandler0da673f2012-04-11 12:33:16 -0400168 // Notification control database. For now just contains disabled packages.
169 private AtomicFile mPolicyFile;
170 private HashSet<String> mBlockedPackages = new HashSet<String>();
171
172 private static final int DB_VERSION = 1;
173
174 private static final String TAG_BODY = "notification-policy";
175 private static final String ATTR_VERSION = "version";
176
177 private static final String TAG_BLOCKED_PKGS = "blocked-packages";
178 private static final String TAG_PACKAGE = "package";
179 private static final String ATTR_NAME = "name";
180
Daniel Sandler09a247e2013-02-14 10:24:17 -0500181 private class NotificationListenerInfo implements DeathRecipient {
182 INotificationListener listener;
183 int userid;
184 public NotificationListenerInfo(INotificationListener listener, int userid) {
185 this.listener = listener;
186 this.userid = userid;
187 }
188
Daniel Sandler21ca44d2013-03-07 13:58:00 -0500189 boolean userMatches(StatusBarNotification sbn) {
190 if (this.userid == UserHandle.USER_ALL) return true;
191 int nid = sbn.getUserId();
192 return (nid == UserHandle.USER_ALL || nid == this.userid);
193 }
194
Daniel Sandler09a247e2013-02-14 10:24:17 -0500195 public void notifyPostedIfUserMatch(StatusBarNotification sbn) {
Daniel Sandler21ca44d2013-03-07 13:58:00 -0500196 if (!userMatches(sbn)) return;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500197 try {
198 listener.onNotificationPosted(sbn);
199 } catch (RemoteException ex) {
200 // not there?
201 }
202 }
203
204 public void notifyRemovedIfUserMatch(StatusBarNotification sbn) {
Daniel Sandler21ca44d2013-03-07 13:58:00 -0500205 if (!userMatches(sbn)) return;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500206 try {
207 listener.onNotificationRemoved(sbn);
208 } catch (RemoteException ex) {
209 // not there?
210 }
211 }
212
213 @Override
214 public void binderDied() {
215 unregisterListener(this.listener, this.userid);
216 }
217 }
218
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500219 private static class Archive {
220 static final int BUFFER_SIZE = 1000;
221 ArrayDeque<StatusBarNotification> mBuffer = new ArrayDeque<StatusBarNotification>(BUFFER_SIZE);
222
223 public Archive() {
224
225 }
226 public void record(StatusBarNotification nr) {
227 if (mBuffer.size() == BUFFER_SIZE) {
228 mBuffer.removeFirst();
229 }
230 mBuffer.addLast(nr);
231 }
232
233 public void clear() {
234 mBuffer.clear();
235 }
236
237 public Iterator<StatusBarNotification> descendingIterator() {
238 return mBuffer.descendingIterator();
239 }
240 public Iterator<StatusBarNotification> ascendingIterator() {
241 return mBuffer.iterator();
242 }
243 public Iterator<StatusBarNotification> filter(
244 final Iterator<StatusBarNotification> iter, final String pkg, final int userId) {
245 return new Iterator<StatusBarNotification>() {
246 StatusBarNotification mNext = findNext();
247
248 private StatusBarNotification findNext() {
249 while (iter.hasNext()) {
250 StatusBarNotification nr = iter.next();
251 if ((pkg == null || nr.pkg == pkg)
252 && (userId == UserHandle.USER_ALL || nr.getUserId() == userId)) {
253 return nr;
254 }
255 }
256 return null;
257 }
258
259 @Override
260 public boolean hasNext() {
261 return mNext == null;
262 }
263
264 @Override
265 public StatusBarNotification next() {
266 StatusBarNotification next = mNext;
267 if (next == null) {
268 throw new NoSuchElementException();
269 }
270 mNext = findNext();
271 return next;
272 }
273
274 @Override
275 public void remove() {
276 iter.remove();
277 }
278 };
279 }
Daniel Sandler78d0d252013-02-12 08:14:52 -0500280
281 public StatusBarNotification[] getArray(int count) {
282 if (count == 0) count = Archive.BUFFER_SIZE;
283 final StatusBarNotification[] a
284 = new StatusBarNotification[Math.min(count, mBuffer.size())];
285 Iterator<StatusBarNotification> iter = descendingIterator();
286 int i=0;
287 while (iter.hasNext() && i < count) {
288 a[i++] = iter.next();
289 }
290 return a;
291 }
292
293 public StatusBarNotification[] getArray(int count, String pkg, int userId) {
294 if (count == 0) count = Archive.BUFFER_SIZE;
295 final StatusBarNotification[] a
296 = new StatusBarNotification[Math.min(count, mBuffer.size())];
297 Iterator<StatusBarNotification> iter = filter(descendingIterator(), pkg, userId);
298 int i=0;
299 while (iter.hasNext() && i < count) {
300 a[i++] = iter.next();
301 }
302 return a;
303 }
304
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500305 }
306
307 Archive mArchive = new Archive();
308
Daniel Sandler0da673f2012-04-11 12:33:16 -0400309 private void loadBlockDb() {
310 synchronized(mBlockedPackages) {
311 if (mPolicyFile == null) {
312 File dir = new File("/data/system");
313 mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml"));
314
315 mBlockedPackages.clear();
316
317 FileInputStream infile = null;
318 try {
319 infile = mPolicyFile.openRead();
320 final XmlPullParser parser = Xml.newPullParser();
321 parser.setInput(infile, null);
322
323 int type;
324 String tag;
325 int version = DB_VERSION;
326 while ((type = parser.next()) != END_DOCUMENT) {
327 tag = parser.getName();
328 if (type == START_TAG) {
329 if (TAG_BODY.equals(tag)) {
330 version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
331 } else if (TAG_BLOCKED_PKGS.equals(tag)) {
332 while ((type = parser.next()) != END_DOCUMENT) {
333 tag = parser.getName();
334 if (TAG_PACKAGE.equals(tag)) {
335 mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME));
336 } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) {
337 break;
338 }
339 }
340 }
341 }
342 }
343 } catch (FileNotFoundException e) {
344 // No data yet
345 } catch (IOException e) {
346 Log.wtf(TAG, "Unable to read blocked notifications database", e);
347 } catch (NumberFormatException e) {
348 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
349 } catch (XmlPullParserException e) {
350 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
351 } finally {
352 IoUtils.closeQuietly(infile);
353 }
354 }
355 }
356 }
357
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500358 /**
359 * Use this when you just want to know if notifications are OK for this package.
360 */
361 public boolean areNotificationsEnabledForPackage(String pkg, int uid) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400362 checkCallerIsSystem();
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500363 return (mAppOps.checkOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
364 == AppOpsManager.MODE_ALLOWED);
Daniel Sandler0da673f2012-04-11 12:33:16 -0400365 }
366
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500367 /** Use this when you actually want to post a notification or toast.
368 *
369 * Unchecked. Not exposed via Binder, but can be called in the course of enqueue*().
370 */
371 private boolean noteNotificationOp(String pkg, int uid) {
372 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
373 != AppOpsManager.MODE_ALLOWED) {
374 Slog.v(TAG, "notifications are disabled by AppOps for " + pkg);
375 return false;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400376 }
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500377 return true;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400378 }
379
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500380 public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400381 checkCallerIsSystem();
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500382 if (true||DBG) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400383 Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);
384 }
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500385 mAppOps.setMode(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg,
386 enabled ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED);
Daniel Sandler0da673f2012-04-11 12:33:16 -0400387 }
388
389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 private static String idDebugString(Context baseContext, String packageName, int id) {
391 Context c = null;
392
393 if (packageName != null) {
394 try {
395 c = baseContext.createPackageContext(packageName, 0);
396 } catch (NameNotFoundException e) {
397 c = baseContext;
398 }
399 } else {
400 c = baseContext;
401 }
402
403 String pkg;
404 String type;
405 String name;
406
407 Resources r = c.getResources();
408 try {
409 return r.getResourceName(id);
410 } catch (Resources.NotFoundException e) {
411 return "<name unknown>";
412 }
413 }
414
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500415 public StatusBarNotification[] getActiveNotifications(String callingPkg) {
416 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
Daniel Sandler78d0d252013-02-12 08:14:52 -0500417 "NotificationManagerService.getActiveNotifications");
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500418
419 StatusBarNotification[] tmp = null;
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500420 int uid = Binder.getCallingUid();
421
422 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
423 == AppOpsManager.MODE_ALLOWED) {
424 synchronized (mNotificationList) {
425 tmp = new StatusBarNotification[mNotificationList.size()];
426 final int N = mNotificationList.size();
427 for (int i=0; i<N; i++) {
428 tmp[i] = mNotificationList.get(i).sbn;
429 }
430 }
431 }
432 return tmp;
433 }
434
Daniel Sandler78d0d252013-02-12 08:14:52 -0500435 public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) {
436 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
437 "NotificationManagerService.getHistoricalNotifications");
438
439 StatusBarNotification[] tmp = null;
440 int uid = Binder.getCallingUid();
441
442 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
443 == AppOpsManager.MODE_ALLOWED) {
444 synchronized (mArchive) {
445 tmp = mArchive.getArray(count);
446 }
447 }
448 return tmp;
449 }
450
Daniel Sandler09a247e2013-02-14 10:24:17 -0500451 @Override
452 public void registerListener(final INotificationListener listener, final int userid) {
453 checkCallerIsSystem();
454 synchronized (mNotificationList) {
455 try {
456 NotificationListenerInfo info = new NotificationListenerInfo(listener, userid);
457 listener.asBinder().linkToDeath(info, 0);
458 mListeners.add(info);
459 } catch (RemoteException e) {
460 // already dead
461 }
462 }
463 }
464
465 @Override
466 public void unregisterListener(INotificationListener listener, int userid) {
467 checkCallerIsSystem();
468 synchronized (mNotificationList) {
469 final int N = mListeners.size();
470 for (int i=N-1; i>=0; i--) {
471 final NotificationListenerInfo info = mListeners.get(i);
472 if (info.listener == listener && info.userid == userid) {
473 mListeners.remove(listener);
474 }
475 }
476 }
477 }
478
479 private void notifyPostedLocked(NotificationRecord n) {
480 final StatusBarNotification sbn = n.sbn;
481 for (final NotificationListenerInfo info : mListeners) {
482 mHandler.post(new Runnable() {
483 @Override
484 public void run() {
485 info.notifyPostedIfUserMatch(sbn);
486 }});
487 }
488 }
489
490 private void notifyRemovedLocked(NotificationRecord n) {
491 final StatusBarNotification sbn = n.sbn;
492 for (final NotificationListenerInfo info : mListeners) {
493 mHandler.post(new Runnable() {
494 @Override
495 public void run() {
496 info.notifyRemovedIfUserMatch(sbn);
497 }});
498 }
499 }
500
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500501 public static final class NotificationRecord
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500503 final StatusBarNotification sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 IBinder statusBarKey;
505
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500506 NotificationRecord(StatusBarNotification sbn)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500508 this.sbn = sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700510
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500511 public Notification getNotification() { return sbn.notification; }
512 public int getFlags() { return sbn.notification.flags; }
513 public int getUserId() { return sbn.getUserId(); }
514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 void dump(PrintWriter pw, String prefix, Context baseContext) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500516 final Notification notification = sbn.notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 pw.println(prefix + this);
518 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500519 + " / " + idDebugString(baseContext, this.sbn.pkg, notification.icon));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500520 pw.println(prefix + " pri=" + notification.priority);
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500521 pw.println(prefix + " score=" + this.sbn.score);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 pw.println(prefix + " contentIntent=" + notification.contentIntent);
523 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
524 pw.println(prefix + " tickerText=" + notification.tickerText);
525 pw.println(prefix + " contentView=" + notification.contentView);
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500526 pw.println(prefix + " uid=" + this.sbn.uid + " userId=" + this.sbn.getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
528 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
529 pw.println(prefix + " sound=" + notification.sound);
530 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
531 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
532 + " ledOnMS=" + notification.ledOnMS
533 + " ledOffMS=" + notification.ledOffMS);
534 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 @Override
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500537 public final String toString() {
538 return String.format(
539 "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s score=%d: %s)",
540 System.identityHashCode(this),
541 this.sbn.pkg, this.sbn.user, this.sbn.id, this.sbn.tag,
542 this.sbn.score, this.sbn.notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
544 }
545
546 private static final class ToastRecord
547 {
548 final int pid;
549 final String pkg;
550 final ITransientNotification callback;
551 int duration;
552
553 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
554 {
555 this.pid = pid;
556 this.pkg = pkg;
557 this.callback = callback;
558 this.duration = duration;
559 }
560
561 void update(int duration) {
562 this.duration = duration;
563 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 void dump(PrintWriter pw, String prefix) {
566 pw.println(prefix + this);
567 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 @Override
570 public final String toString()
571 {
572 return "ToastRecord{"
573 + Integer.toHexString(System.identityHashCode(this))
574 + " pkg=" + pkg
575 + " callback=" + callback
576 + " duration=" + duration;
577 }
578 }
579
Joe Onorato089de882010-04-12 08:18:45 -0700580 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
581 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582
583 public void onSetDisabled(int status) {
584 synchronized (mNotificationList) {
585 mDisabledNotifications = status;
586 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
587 // cancel whatever's going on
588 long identity = Binder.clearCallingIdentity();
589 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700590 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
591 if (player != null) {
592 player.stopAsync();
593 }
594 } catch (RemoteException e) {
595 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 Binder.restoreCallingIdentity(identity);
597 }
598
599 identity = Binder.clearCallingIdentity();
600 try {
601 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700602 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 Binder.restoreCallingIdentity(identity);
604 }
605 }
606 }
607 }
608
609 public void onClearAll() {
Dianne Hackborn41203752012-08-31 14:05:51 -0700610 // XXX to be totally correct, the caller should tell us which user
611 // this is for.
612 cancelAll(ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 }
614
Fred Quintana6ecaff12009-09-25 14:23:13 -0700615 public void onNotificationClick(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700616 // XXX to be totally correct, the caller should tell us which user
617 // this is for.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700618 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackborn41203752012-08-31 14:05:51 -0700619 Notification.FLAG_FOREGROUND_SERVICE, false,
620 ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
622
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400623 public void onNotificationClear(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700624 // XXX to be totally correct, the caller should tell us which user
625 // this is for.
Joe Onorato46439ce2010-11-19 13:56:21 -0800626 cancelNotification(pkg, tag, id, 0,
627 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
Dianne Hackborn41203752012-08-31 14:05:51 -0700628 true, ActivityManager.getCurrentUser());
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400629 }
630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 public void onPanelRevealed() {
632 synchronized (mNotificationList) {
633 // sound
634 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 long identity = Binder.clearCallingIdentity();
637 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700638 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
639 if (player != null) {
640 player.stopAsync();
641 }
642 } catch (RemoteException e) {
643 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 Binder.restoreCallingIdentity(identity);
645 }
646
647 // vibrate
648 mVibrateNotification = null;
649 identity = Binder.clearCallingIdentity();
650 try {
651 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700652 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 Binder.restoreCallingIdentity(identity);
654 }
655
656 // light
657 mLights.clear();
658 mLedNotification = null;
659 updateLightsLocked();
660 }
661 }
Joe Onorato005847b2010-06-04 16:08:02 -0400662
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700663 public void onNotificationError(String pkg, String tag, int id,
664 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400665 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
666 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Dianne Hackborn41203752012-08-31 14:05:51 -0700667 // XXX to be totally correct, the caller should tell us which user
668 // this is for.
669 cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid));
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700670 long ident = Binder.clearCallingIdentity();
671 try {
672 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
673 "Bad notification posted from package " + pkg
674 + ": " + message);
675 } catch (RemoteException e) {
676 }
677 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400678 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 };
680
681 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
682 @Override
683 public void onReceive(Context context, Intent intent) {
684 String action = intent.getAction();
685
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800686 boolean queryRestart = false;
Daniel Sandler26ece572012-06-01 15:38:46 -0400687 boolean packageChanged = false;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800688
Mike Lockwood541c9942011-06-12 19:35:45 -0400689 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800690 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandler26ece572012-06-01 15:38:46 -0400691 || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED))
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800692 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800693 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800694 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800695 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800696 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800697 } else if (queryRestart) {
698 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800699 } else {
700 Uri uri = intent.getData();
701 if (uri == null) {
702 return;
703 }
704 String pkgName = uri.getSchemeSpecificPart();
705 if (pkgName == null) {
706 return;
707 }
Daniel Sandler26ece572012-06-01 15:38:46 -0400708 if (packageChanged) {
709 // We cancel notifications for packages which have just been disabled
710 final int enabled = mContext.getPackageManager()
711 .getApplicationEnabledSetting(pkgName);
712 if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
713 || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
714 return;
715 }
716 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800717 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800719 if (pkgList != null && (pkgList.length > 0)) {
720 for (String pkgName : pkgList) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700721 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart,
722 UserHandle.USER_ALL);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400725 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
726 // Keep track of screen on/off state, but do not turn off the notification light
727 // until user passes through the lock screen or views the notification.
728 mScreenOn = true;
729 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
730 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500731 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400732 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
733 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500734 updateNotificationPulse();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700735 } else if (action.equals(Intent.ACTION_USER_STOPPED)) {
736 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
737 if (userHandle >= 0) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700738 cancelAllNotificationsInt(null, 0, 0, true, userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700739 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400740 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
741 // turn off LED when user passes through lock screen
742 mNotificationLight.turnOff();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 }
744 }
745 };
746
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700747 class SettingsObserver extends ContentObserver {
748 SettingsObserver(Handler handler) {
749 super(handler);
750 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800751
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700752 void observe() {
753 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500754 resolver.registerContentObserver(Settings.System.getUriFor(
755 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700756 update();
757 }
758
759 @Override public void onChange(boolean selfChange) {
760 update();
761 }
762
763 public void update() {
764 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500765 boolean pulseEnabled = Settings.System.getInt(resolver,
766 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
767 if (mNotificationPulseEnabled != pulseEnabled) {
768 mNotificationPulseEnabled = pulseEnabled;
769 updateNotificationPulse();
770 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700771 }
772 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500773
Daniel Sandleredbb3802012-11-13 20:49:47 -0800774 static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) {
775 int[] ar = r.getIntArray(resid);
776 if (ar == null) {
777 return def;
778 }
779 final int len = ar.length > maxlen ? maxlen : ar.length;
780 long[] out = new long[len];
781 for (int i=0; i<len; i++) {
782 out[i] = ar[i];
783 }
784 return out;
785 }
786
Joe Onorato089de882010-04-12 08:18:45 -0700787 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500788 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 {
790 super();
791 mContext = context;
Jeff Brownc2346132012-04-13 01:55:38 -0700792 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 mAm = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800796
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500797 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
798
799 importOldBlockDb();
Daniel Sandler0da673f2012-04-11 12:33:16 -0400800
Joe Onorato089de882010-04-12 08:18:45 -0700801 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 statusBar.setNotificationCallbacks(mNotificationCallbacks);
803
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500804 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
805 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
806
Mike Lockwood670f9322010-01-20 12:13:36 -0500807 Resources resources = mContext.getResources();
808 mDefaultNotificationColor = resources.getColor(
809 com.android.internal.R.color.config_defaultNotificationColor);
810 mDefaultNotificationLedOn = resources.getInteger(
811 com.android.internal.R.integer.config_defaultNotificationLedOn);
812 mDefaultNotificationLedOff = resources.getInteger(
813 com.android.internal.R.integer.config_defaultNotificationLedOff);
814
Daniel Sandleredbb3802012-11-13 20:49:47 -0800815 mDefaultVibrationPattern = getLongArray(resources,
816 com.android.internal.R.array.config_defaultNotificationVibePattern,
817 VIBRATE_PATTERN_MAXLEN,
818 DEFAULT_VIBRATE_PATTERN);
819
820 mFallbackVibrationPattern = getLongArray(resources,
821 com.android.internal.R.array.config_notificationFallbackVibePattern,
822 VIBRATE_PATTERN_MAXLEN,
823 DEFAULT_VIBRATE_PATTERN);
824
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400825 // Don't start allowing notifications until the setup wizard has run once.
826 // After that, including subsequent boots, init with notifications turned on.
827 // This works on the first boot because the setup wizard will toggle this
828 // flag at least once and we'll go back to 0 after that.
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700829 if (0 == Settings.Global.getInt(mContext.getContentResolver(),
830 Settings.Global.DEVICE_PROVISIONED, 0)) {
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400831 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
832 }
833
Mike Lockwood35e16bf2010-11-30 19:53:36 -0500834 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500836 filter.addAction(Intent.ACTION_SCREEN_ON);
837 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500838 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400839 filter.addAction(Intent.ACTION_USER_PRESENT);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700840 filter.addAction(Intent.ACTION_USER_STOPPED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800842 IntentFilter pkgFilter = new IntentFilter();
843 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400844 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800845 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
846 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
847 pkgFilter.addDataScheme("package");
848 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800849 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800850 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800851
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500852 SettingsObserver observer = new SettingsObserver(mHandler);
853 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 }
855
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500856 /**
857 * Read the old XML-based app block database and import those blockages into the AppOps system.
858 */
859 private void importOldBlockDb() {
860 loadBlockDb();
861
862 PackageManager pm = mContext.getPackageManager();
863 for (String pkg : mBlockedPackages) {
864 PackageInfo info = null;
865 try {
866 info = pm.getPackageInfo(pkg, 0);
867 setNotificationsEnabledForPackage(pkg, info.applicationInfo.uid, false);
868 } catch (NameNotFoundException e) {
869 // forget you
870 }
871 }
872 mBlockedPackages.clear();
873 if (mPolicyFile != null) {
874 mPolicyFile.delete();
875 }
876 }
877
Joe Onorato30275482009-07-08 17:09:14 -0700878 void systemReady() {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700879 mAudioService = IAudioService.Stub.asInterface(
880 ServiceManager.getService(Context.AUDIO_SERVICE));
881
Joe Onorato30275482009-07-08 17:09:14 -0700882 // no beeping until we're basically done booting
883 mSystemReady = true;
884 }
885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 // Toasts
887 // ============================================================================
888 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
889 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400890 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891
892 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800893 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 return ;
895 }
896
Daniel Sandler0da673f2012-04-11 12:33:16 -0400897 final boolean isSystemToast = ("android".equals(pkg));
898
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500899 if (ENABLE_BLOCKED_TOASTS && !noteNotificationOp(pkg, Binder.getCallingUid())) {
900 if (!isSystemToast) {
901 Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
902 return;
903 }
Daniel Sandler0da673f2012-04-11 12:33:16 -0400904 }
905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 synchronized (mToastQueue) {
907 int callingPid = Binder.getCallingPid();
908 long callingId = Binder.clearCallingIdentity();
909 try {
910 ToastRecord record;
911 int index = indexOfToastLocked(pkg, callback);
912 // If it's already in the queue, we update it in place, we don't
913 // move it to the end of the queue.
914 if (index >= 0) {
915 record = mToastQueue.get(index);
916 record.update(duration);
917 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800918 // Limit the number of toasts that any given package except the android
919 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -0400920 if (!isSystemToast) {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800921 int count = 0;
922 final int N = mToastQueue.size();
923 for (int i=0; i<N; i++) {
924 final ToastRecord r = mToastQueue.get(i);
925 if (r.pkg.equals(pkg)) {
926 count++;
927 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
928 Slog.e(TAG, "Package has already posted " + count
929 + " toasts. Not showing more. Package=" + pkg);
930 return;
931 }
932 }
933 }
934 }
935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 record = new ToastRecord(callingPid, pkg, callback, duration);
937 mToastQueue.add(record);
938 index = mToastQueue.size() - 1;
939 keepProcessAliveLocked(callingPid);
940 }
941 // If it's at index 0, it's the current toast. It doesn't matter if it's
942 // new or just been updated. Call back and tell it to show itself.
943 // If the callback fails, this will remove it from the list, so don't
944 // assume that it's valid after this.
945 if (index == 0) {
946 showNextToastLocked();
947 }
948 } finally {
949 Binder.restoreCallingIdentity(callingId);
950 }
951 }
952 }
953
954 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800955 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956
957 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800958 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 return ;
960 }
961
962 synchronized (mToastQueue) {
963 long callingId = Binder.clearCallingIdentity();
964 try {
965 int index = indexOfToastLocked(pkg, callback);
966 if (index >= 0) {
967 cancelToastLocked(index);
968 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800969 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 }
971 } finally {
972 Binder.restoreCallingIdentity(callingId);
973 }
974 }
975 }
976
977 private void showNextToastLocked() {
978 ToastRecord record = mToastQueue.get(0);
979 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800980 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 try {
982 record.callback.show();
983 scheduleTimeoutLocked(record, false);
984 return;
985 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800986 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 + " in package " + record.pkg);
988 // remove it from the list and let the process die
989 int index = mToastQueue.indexOf(record);
990 if (index >= 0) {
991 mToastQueue.remove(index);
992 }
993 keepProcessAliveLocked(record.pid);
994 if (mToastQueue.size() > 0) {
995 record = mToastQueue.get(0);
996 } else {
997 record = null;
998 }
999 }
1000 }
1001 }
1002
1003 private void cancelToastLocked(int index) {
1004 ToastRecord record = mToastQueue.get(index);
1005 try {
1006 record.callback.hide();
1007 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001008 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 + " in package " + record.pkg);
1010 // don't worry about this, we're about to remove it from
1011 // the list anyway
1012 }
1013 mToastQueue.remove(index);
1014 keepProcessAliveLocked(record.pid);
1015 if (mToastQueue.size() > 0) {
1016 // Show the next one. If the callback fails, this will remove
1017 // it from the list, so don't assume that the list hasn't changed
1018 // after this point.
1019 showNextToastLocked();
1020 }
1021 }
1022
1023 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
1024 {
1025 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
1026 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
1027 mHandler.removeCallbacksAndMessages(r);
1028 mHandler.sendMessageDelayed(m, delay);
1029 }
1030
1031 private void handleTimeout(ToastRecord record)
1032 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001033 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 synchronized (mToastQueue) {
1035 int index = indexOfToastLocked(record.pkg, record.callback);
1036 if (index >= 0) {
1037 cancelToastLocked(index);
1038 }
1039 }
1040 }
1041
1042 // lock on mToastQueue
1043 private int indexOfToastLocked(String pkg, ITransientNotification callback)
1044 {
1045 IBinder cbak = callback.asBinder();
1046 ArrayList<ToastRecord> list = mToastQueue;
1047 int len = list.size();
1048 for (int i=0; i<len; i++) {
1049 ToastRecord r = list.get(i);
1050 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
1051 return i;
1052 }
1053 }
1054 return -1;
1055 }
1056
1057 // lock on mToastQueue
1058 private void keepProcessAliveLocked(int pid)
1059 {
1060 int toastCount = 0; // toasts from this pid
1061 ArrayList<ToastRecord> list = mToastQueue;
1062 int N = list.size();
1063 for (int i=0; i<N; i++) {
1064 ToastRecord r = list.get(i);
1065 if (r.pid == pid) {
1066 toastCount++;
1067 }
1068 }
1069 try {
1070 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
1071 } catch (RemoteException e) {
1072 // Shouldn't happen.
1073 }
1074 }
1075
1076 private final class WorkerHandler extends Handler
1077 {
1078 @Override
1079 public void handleMessage(Message msg)
1080 {
1081 switch (msg.what)
1082 {
1083 case MESSAGE_TIMEOUT:
1084 handleTimeout((ToastRecord)msg.obj);
1085 break;
1086 }
1087 }
1088 }
1089
1090
1091 // Notifications
1092 // ============================================================================
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001093 public void enqueueNotificationWithTag(String pkg, String basePkg, String tag, int id,
1094 Notification notification, int[] idOut, int userId)
Fred Quintana6ecaff12009-09-25 14:23:13 -07001095 {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001096 enqueueNotificationInternal(pkg, basePkg, Binder.getCallingUid(), Binder.getCallingPid(),
Dianne Hackborn41203752012-08-31 14:05:51 -07001097 tag, id, notification, idOut, userId);
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001098 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001099
1100 private final static int clamp(int x, int low, int high) {
1101 return (x < low) ? low : ((x > high) ? high : x);
Daniel Sandlere40451a2011-02-03 14:51:35 -05001102 }
1103
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001104 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
1105 // uid/pid of another application)
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001106 public void enqueueNotificationInternal(String pkg, String basePkg, int callingUid,
1107 int callingPid, String tag, int id, Notification notification, int[] idOut, int userId)
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001108 {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001109 if (DBG) {
1110 Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
1111 }
1112 checkCallerIsSystemOrSameApp(pkg);
1113 final boolean isSystemNotification = ("android".equals(pkg));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001114
Dianne Hackborn41203752012-08-31 14:05:51 -07001115 userId = ActivityManager.handleIncomingUser(callingPid,
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001116 callingUid, userId, true, false, "enqueueNotification", pkg);
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -07001117 final UserHandle user = new UserHandle(userId);
Dianne Hackborn41203752012-08-31 14:05:51 -07001118
Joe Onoratobd73d012010-06-04 11:44:54 -07001119 // Limit the number of notifications that any given package except the android
1120 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -04001121 if (!isSystemNotification) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001122 synchronized (mNotificationList) {
1123 int count = 0;
1124 final int N = mNotificationList.size();
1125 for (int i=0; i<N; i++) {
1126 final NotificationRecord r = mNotificationList.get(i);
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001127 if (r.sbn.pkg.equals(pkg) && r.sbn.getUserId() == userId) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001128 count++;
1129 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
1130 Slog.e(TAG, "Package has already posted " + count
1131 + " notifications. Not showing more. package=" + pkg);
1132 return;
1133 }
1134 }
1135 }
1136 }
1137 }
1138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 // This conditional is a dirty hack to limit the logging done on
1140 // behalf of the download manager without affecting other apps.
1141 if (!pkg.equals("com.android.providers.downloads")
1142 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001143 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001144 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
1146
1147 if (pkg == null || notification == null) {
1148 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
1149 + " id=" + id + " notification=" + notification);
1150 }
1151 if (notification.icon != 0) {
1152 if (notification.contentView == null) {
1153 throw new IllegalArgumentException("contentView required: pkg=" + pkg
1154 + " id=" + id + " notification=" + notification);
1155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
1157
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001158 // === Scoring ===
Daniel Sandler0da673f2012-04-11 12:33:16 -04001159
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001160 // 0. Sanitize inputs
1161 notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
1162 // Migrate notification flags to scores
1163 if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
1164 if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
Daniel Sandler49a2ad12012-03-28 15:46:39 -04001165 } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001166 if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
1167 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001168
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001169 // 1. initial score: buckets of 10, around the app
Daniel Sandler0da673f2012-04-11 12:33:16 -04001170 int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001171
Daniel Sandler0da673f2012-04-11 12:33:16 -04001172 // 2. Consult external heuristics (TBD)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001173
Daniel Sandler0da673f2012-04-11 12:33:16 -04001174 // 3. Apply local rules
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001175
1176 // blocked apps
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001177 if (ENABLE_BLOCKED_NOTIFICATIONS && !noteNotificationOp(pkg, callingUid)) {
1178 if (!isSystemNotification) {
1179 score = JUNK_SCORE;
1180 Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
1181 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001182 }
1183
1184 if (DBG) {
1185 Slog.v(TAG, "Assigned score=" + score + " to " + notification);
1186 }
1187
1188 if (score < SCORE_DISPLAY_THRESHOLD) {
1189 // Notification will be blocked because the score is too low.
1190 return;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001191 }
1192
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001193 // Should this notification make noise, vibe, or use the LED?
1194 final boolean canInterrupt = (score >= SCORE_INTERRUPTION_THRESHOLD);
1195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 synchronized (mNotificationList) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001197 final StatusBarNotification n = new StatusBarNotification(
1198 pkg, id, tag, callingUid, callingPid, score, notification, user);
1199 NotificationRecord r = new NotificationRecord(n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 NotificationRecord old = null;
1201
Dianne Hackborn41203752012-08-31 14:05:51 -07001202 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 if (index < 0) {
1204 mNotificationList.add(r);
1205 } else {
1206 old = mNotificationList.remove(index);
1207 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001208 // Make sure we don't lose the foreground service state.
1209 if (old != null) {
1210 notification.flags |=
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001211 old.getNotification().flags&Notification.FLAG_FOREGROUND_SERVICE;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001214
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001215 // Ensure if this is a foreground service that the proper additional
1216 // flags are set.
1217 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
1218 notification.flags |= Notification.FLAG_ONGOING_EVENT
1219 | Notification.FLAG_NO_CLEAR;
1220 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001221
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001222 final int currentUser;
1223 final long token = Binder.clearCallingIdentity();
1224 try {
1225 currentUser = ActivityManager.getCurrentUser();
1226 } finally {
1227 Binder.restoreCallingIdentity(token);
1228 }
1229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 if (notification.icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 if (old != null && old.statusBarKey != null) {
1232 r.statusBarKey = old.statusBarKey;
1233 long identity = Binder.clearCallingIdentity();
1234 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001235 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 }
1237 finally {
1238 Binder.restoreCallingIdentity(identity);
1239 }
1240 } else {
1241 long identity = Binder.clearCallingIdentity();
1242 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001243 r.statusBarKey = mStatusBar.addNotification(n);
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001244 if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1245 && canInterrupt) {
Mike Lockwoodece18ef2012-02-13 20:42:19 -08001246 mAttentionLight.pulse();
1247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 }
1249 finally {
1250 Binder.restoreCallingIdentity(identity);
1251 }
1252 }
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001253 // Send accessibility events only for the current user.
1254 if (currentUser == userId) {
1255 sendAccessibilityEvent(notification, pkg);
1256 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001257
1258 // finally, keep some of this information around for later use
1259 mArchive.record(n);
Daniel Sandler09a247e2013-02-14 10:24:17 -05001260
1261 notifyPostedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -05001263 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 if (old != null && old.statusBarKey != null) {
1265 long identity = Binder.clearCallingIdentity();
1266 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001267 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 }
1269 finally {
1270 Binder.restoreCallingIdentity(identity);
1271 }
Daniel Sandler09a247e2013-02-14 10:24:17 -05001272
1273 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001275 return; // do not play sounds, show lights, etc. for invalid notifications
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 }
1277
1278 // If we're not supposed to beep, vibrate, etc. then don't.
1279 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
1280 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -07001281 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001282 && (r.getUserId() == UserHandle.USER_ALL ||
1283 (r.getUserId() == userId && r.getUserId() == currentUser))
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001284 && canInterrupt
Joe Onorato30275482009-07-08 17:09:14 -07001285 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -08001286
1287 final AudioManager audioManager = (AudioManager) mContext
1288 .getSystemService(Context.AUDIO_SERVICE);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 // sound
1291 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001292 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001293
1294 Uri soundUri = null;
1295 boolean hasValidSound = false;
1296
1297 if (useDefaultSound) {
1298 soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
1299
1300 // check to see if the default notification sound is silent
1301 ContentResolver resolver = mContext.getContentResolver();
1302 hasValidSound = Settings.System.getString(resolver,
1303 Settings.System.NOTIFICATION_SOUND) != null;
1304 } else if (notification.sound != null) {
1305 soundUri = notification.sound;
1306 hasValidSound = (soundUri != null);
1307 }
1308
1309 if (hasValidSound) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
1311 int audioStreamType;
1312 if (notification.audioStreamType >= 0) {
1313 audioStreamType = notification.audioStreamType;
1314 } else {
1315 audioStreamType = DEFAULT_STREAM_TYPE;
1316 }
1317 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -08001318 // do not play notifications if stream volume is 0
Jean-Michel Trivid6770542012-10-10 12:03:41 -07001319 // (typically because ringer mode is silent) or if speech recognition is active.
1320 if ((audioManager.getStreamVolume(audioStreamType) != 0)
1321 && !audioManager.isSpeechRecognitionActive()) {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001322 final long identity = Binder.clearCallingIdentity();
Eric Laurent524dc042009-11-27 05:07:55 -08001323 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001324 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1325 if (player != null) {
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001326 player.playAsync(soundUri, user, looping, audioStreamType);
Jeff Sharkey098d5802012-04-26 17:30:34 -07001327 }
1328 } catch (RemoteException e) {
1329 } finally {
Eric Laurent524dc042009-11-27 05:07:55 -08001330 Binder.restoreCallingIdentity(identity);
1331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 }
1333 }
1334
1335 // vibrate
Daniel Sandleredbb3802012-11-13 20:49:47 -08001336 // Does the notification want to specify its own vibration?
1337 final boolean hasCustomVibrate = notification.vibrate != null;
1338
David Agnew71789e12012-11-09 23:03:26 -05001339 // 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 -05001340 // and no other vibration is specified, we fall back to vibration
David Agnew71789e12012-11-09 23:03:26 -05001341 final boolean convertSoundToVibration =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001342 !hasCustomVibrate
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001343 && hasValidSound
David Agnew71789e12012-11-09 23:03:26 -05001344 && (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
1345
Daniel Sandler4a7a9b92012-11-20 12:59:41 -05001346 // The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 final boolean useDefaultVibrate =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001348 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
David Agnew71789e12012-11-09 23:03:26 -05001349
Daniel Sandleredbb3802012-11-13 20:49:47 -08001350 if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate)
Eric Laurentbffc3d12012-05-07 17:43:49 -07001351 && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 mVibrateNotification = r;
1353
Daniel Sandleredbb3802012-11-13 20:49:47 -08001354 if (useDefaultVibrate || convertSoundToVibration) {
1355 // Escalate privileges so we can use the vibrator even if the notifying app
1356 // does not have the VIBRATE permission.
1357 long identity = Binder.clearCallingIdentity();
1358 try {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001359 mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg,
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001360 useDefaultVibrate ? mDefaultVibrationPattern
1361 : mFallbackVibrationPattern,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001362 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1363 } finally {
1364 Binder.restoreCallingIdentity(identity);
1365 }
1366 } else if (notification.vibrate.length > 1) {
1367 // If you want your own vibration pattern, you need the VIBRATE permission
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001368 mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg, notification.vibrate,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001369 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 }
1372 }
1373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 // light
1375 // the most recent thing gets the light
1376 mLights.remove(old);
1377 if (mLedNotification == old) {
1378 mLedNotification = null;
1379 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001380 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001382 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1383 && canInterrupt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 mLights.add(r);
1385 updateLightsLocked();
1386 } else {
1387 if (old != null
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001388 && ((old.getFlags() & Notification.FLAG_SHOW_LIGHTS) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 updateLightsLocked();
1390 }
1391 }
1392 }
1393
1394 idOut[0] = id;
1395 }
1396
Joe Onorato30275482009-07-08 17:09:14 -07001397 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001398 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
1399 if (!manager.isEnabled()) {
1400 return;
1401 }
1402
1403 AccessibilityEvent event =
1404 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
1405 event.setPackageName(packageName);
1406 event.setClassName(Notification.class.getName());
1407 event.setParcelableData(notification);
1408 CharSequence tickerText = notification.tickerText;
1409 if (!TextUtils.isEmpty(tickerText)) {
1410 event.getText().add(tickerText);
1411 }
1412
1413 manager.sendAccessibilityEvent(event);
1414 }
1415
Joe Onorato46439ce2010-11-19 13:56:21 -08001416 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
1417 // tell the app
1418 if (sendDelete) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001419 if (r.getNotification().deleteIntent != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001420 try {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001421 r.getNotification().deleteIntent.send();
Joe Onorato46439ce2010-11-19 13:56:21 -08001422 } catch (PendingIntent.CanceledException ex) {
1423 // do nothing - there's no relevant way to recover, and
1424 // no reason to let this propagate
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001425 Slog.w(TAG, "canceled PendingIntent for " + r.sbn.pkg, ex);
Joe Onorato46439ce2010-11-19 13:56:21 -08001426 }
1427 }
1428 }
1429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 // status bar
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001431 if (r.getNotification().icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 long identity = Binder.clearCallingIdentity();
1433 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001434 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 }
1436 finally {
1437 Binder.restoreCallingIdentity(identity);
1438 }
1439 r.statusBarKey = null;
Daniel Sandler09a247e2013-02-14 10:24:17 -05001440 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
1442
1443 // sound
1444 if (mSoundNotification == r) {
1445 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001446 final long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001448 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1449 if (player != null) {
1450 player.stopAsync();
1451 }
1452 } catch (RemoteException e) {
1453 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 Binder.restoreCallingIdentity(identity);
1455 }
1456 }
1457
1458 // vibrate
1459 if (mVibrateNotification == r) {
1460 mVibrateNotification = null;
1461 long identity = Binder.clearCallingIdentity();
1462 try {
1463 mVibrator.cancel();
1464 }
1465 finally {
1466 Binder.restoreCallingIdentity(identity);
1467 }
1468 }
1469
1470 // light
1471 mLights.remove(r);
1472 if (mLedNotification == r) {
1473 mLedNotification = null;
1474 }
1475 }
1476
1477 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001478 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001479 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 */
Fred Quintana6ecaff12009-09-25 14:23:13 -07001481 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001482 int mustNotHaveFlags, boolean sendDelete, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001483 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001484 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485
1486 synchronized (mNotificationList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001487 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001489 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001490
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001491 if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 return;
1493 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001494 if ((r.getNotification().flags & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001495 return;
1496 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 mNotificationList.remove(index);
1499
Joe Onorato46439ce2010-11-19 13:56:21 -08001500 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 updateLightsLocked();
1502 }
1503 }
1504 }
1505
1506 /**
Daniel Sandler321e9c52012-10-12 10:59:26 -07001507 * Determine whether the userId applies to the notification in question, either because
1508 * they match exactly, or one of them is USER_ALL (which is treated as a wildcard).
1509 */
1510 private boolean notificationMatchesUserId(NotificationRecord r, int userId) {
1511 return
1512 // looking for USER_ALL notifications? match everything
1513 userId == UserHandle.USER_ALL
1514 // a notification sent to USER_ALL matches any query
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001515 || r.getUserId() == UserHandle.USER_ALL
Daniel Sandler321e9c52012-10-12 10:59:26 -07001516 // an exact user match
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001517 || r.getUserId() == userId;
Daniel Sandler321e9c52012-10-12 10:59:26 -07001518 }
1519
1520 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 * Cancels all notifications from a given package that have all of the
1522 * {@code mustHaveFlags}.
1523 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001524 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001525 int mustNotHaveFlags, boolean doit, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001526 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, userId,
1527 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
1529 synchronized (mNotificationList) {
1530 final int N = mNotificationList.size();
1531 boolean canceledSomething = false;
1532 for (int i = N-1; i >= 0; --i) {
1533 NotificationRecord r = mNotificationList.get(i);
Daniel Sandler321e9c52012-10-12 10:59:26 -07001534 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001535 continue;
1536 }
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001537 // Don't remove notifications to all, if there's no package name specified
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001538 if (r.getUserId() == UserHandle.USER_ALL && pkg == null) {
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001539 continue;
1540 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001541 if ((r.getFlags() & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 continue;
1543 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001544 if ((r.getFlags() & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001545 continue;
1546 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001547 if (pkg != null && !r.sbn.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 continue;
1549 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001550 canceledSomething = true;
1551 if (!doit) {
1552 return true;
1553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001555 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 }
1557 if (canceledSomething) {
1558 updateLightsLocked();
1559 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001560 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 }
1562 }
1563
Dianne Hackborn41203752012-08-31 14:05:51 -07001564 public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001565 checkCallerIsSystemOrSameApp(pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001566 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001567 Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001568 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001569 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001570 Binder.getCallingUid() == Process.SYSTEM_UID
Dianne Hackborn41203752012-08-31 14:05:51 -07001571 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
1573
Dianne Hackborn41203752012-08-31 14:05:51 -07001574 public void cancelAllNotifications(String pkg, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001575 checkCallerIsSystemOrSameApp(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001576
Dianne Hackborn41203752012-08-31 14:05:51 -07001577 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001578 Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001579
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001580 // Calling from user space, don't allow the canceling of actively
1581 // running foreground services.
Dianne Hackborn41203752012-08-31 14:05:51 -07001582 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 }
1584
Daniel Sandler0da673f2012-04-11 12:33:16 -04001585 void checkCallerIsSystem() {
1586 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001587 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001588 return;
1589 }
1590 throw new SecurityException("Disallowed call for uid " + uid);
1591 }
1592
1593 void checkCallerIsSystemOrSameApp(String pkg) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001594 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001595 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001596 return;
1597 }
1598 try {
Amith Yamasanif203aee2012-08-29 18:41:53 -07001599 ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
1600 pkg, 0, UserHandle.getCallingUserId());
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001601 if (!UserHandle.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001602 throw new SecurityException("Calling uid " + uid + " gave package"
1603 + pkg + " which is owned by uid " + ai.uid);
1604 }
Amith Yamasanif203aee2012-08-29 18:41:53 -07001605 } catch (RemoteException re) {
1606 throw new SecurityException("Unknown package " + pkg + "\n" + re);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001607 }
1608 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001609
Dianne Hackborn41203752012-08-31 14:05:51 -07001610 void cancelAll(int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 synchronized (mNotificationList) {
1612 final int N = mNotificationList.size();
1613 for (int i=N-1; i>=0; i--) {
1614 NotificationRecord r = mNotificationList.get(i);
1615
Daniel Sandler321e9c52012-10-12 10:59:26 -07001616 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001617 continue;
1618 }
1619
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001620 if ((r.getFlags() & (Notification.FLAG_ONGOING_EVENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001623 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
1625 }
1626
1627 updateLightsLocked();
1628 }
1629 }
1630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 // lock on mNotificationList
1632 private void updateLightsLocked()
1633 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001634 // handle notification lights
1635 if (mLedNotification == null) {
1636 // get next notification, if any
1637 int n = mLights.size();
1638 if (n > 0) {
1639 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 }
1641 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001642
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001643 // Don't flash while we are in a call or screen is on
1644 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001645 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001646 } else {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001647 final Notification ledno = mLedNotification.sbn.notification;
1648 int ledARGB = ledno.ledARGB;
1649 int ledOnMS = ledno.ledOnMS;
1650 int ledOffMS = ledno.ledOffMS;
1651 if ((ledno.defaults & Notification.DEFAULT_LIGHTS) != 0) {
Mike Lockwood670f9322010-01-20 12:13:36 -05001652 ledARGB = mDefaultNotificationColor;
1653 ledOnMS = mDefaultNotificationLedOn;
1654 ledOffMS = mDefaultNotificationLedOff;
1655 }
1656 if (mNotificationPulseEnabled) {
1657 // pulse repeatedly
1658 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1659 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05001660 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 }
1663
1664 // lock on mNotificationList
Dianne Hackborn41203752012-08-31 14:05:51 -07001665 private int indexOfNotificationLocked(String pkg, String tag, int id, int userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 {
1667 ArrayList<NotificationRecord> list = mNotificationList;
1668 final int len = list.size();
1669 for (int i=0; i<len; i++) {
1670 NotificationRecord r = list.get(i);
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001671 if (!notificationMatchesUserId(r, userId) || r.sbn.id != id) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001672 continue;
1673 }
Fred Quintana6ecaff12009-09-25 14:23:13 -07001674 if (tag == null) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001675 if (r.sbn.tag != null) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001676 continue;
1677 }
1678 } else {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001679 if (!tag.equals(r.sbn.tag)) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001680 continue;
1681 }
1682 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001683 if (r.sbn.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 return i;
1685 }
1686 }
1687 return -1;
1688 }
1689
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001690 private void updateNotificationPulse() {
1691 synchronized (mNotificationList) {
1692 updateLightsLocked();
1693 }
1694 }
1695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 // ======================================================================
1697 @Override
1698 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1699 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1700 != PackageManager.PERMISSION_GRANTED) {
1701 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1702 + Binder.getCallingPid()
1703 + ", uid=" + Binder.getCallingUid());
1704 return;
1705 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 pw.println("Current Notification Manager state:");
1708
1709 int N;
1710
1711 synchronized (mToastQueue) {
1712 N = mToastQueue.size();
1713 if (N > 0) {
1714 pw.println(" Toast Queue:");
1715 for (int i=0; i<N; i++) {
1716 mToastQueue.get(i).dump(pw, " ");
1717 }
1718 pw.println(" ");
1719 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 }
1722
1723 synchronized (mNotificationList) {
1724 N = mNotificationList.size();
1725 if (N > 0) {
1726 pw.println(" Notification List:");
1727 for (int i=0; i<N; i++) {
1728 mNotificationList.get(i).dump(pw, " ", mContext);
1729 }
1730 pw.println(" ");
1731 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 N = mLights.size();
1734 if (N > 0) {
1735 pw.println(" Lights List:");
1736 for (int i=0; i<N; i++) {
1737 mLights.get(i).dump(pw, " ", mContext);
1738 }
1739 pw.println(" ");
1740 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 pw.println(" mSoundNotification=" + mSoundNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001744 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1745 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 }
1747 }
1748}