blob: f0257ffe276d4ea498d428c046f682a1cd20db26 [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;
Daniel Sandler5e62e3a2013-04-15 20:57:02 -040052import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.IBinder;
55import android.os.Message;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070056import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.os.RemoteException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070058import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070059import android.os.UserHandle;
Daniel Sandler4b749ef2013-03-18 21:53:04 -040060import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.Vibrator;
62import android.provider.Settings;
Daniel Sandler5feceeb2013-03-22 18:29:23 -070063import android.service.notification.INotificationListener;
64import android.service.notification.NotificationListenerService;
65import android.service.notification.StatusBarNotification;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050066import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.text.TextUtils;
Dianne Hackborn39606a02012-07-31 17:54:35 -070068import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.EventLog;
70import android.util.Log;
Andy Stadler110988c2010-12-03 14:29:16 -080071import android.util.Slog;
Daniel Sandler0da673f2012-04-11 12:33:16 -040072import android.util.Xml;
svetoslavganov75986cf2009-05-14 22:28:01 -070073import android.view.accessibility.AccessibilityEvent;
74import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.widget.Toast;
76
Jeff Sharkey098d5802012-04-26 17:30:34 -070077import org.xmlpull.v1.XmlPullParser;
78import org.xmlpull.v1.XmlPullParserException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070079
Daniel Sandler0da673f2012-04-11 12:33:16 -040080import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import java.io.FileDescriptor;
Daniel Sandler0da673f2012-04-11 12:33:16 -040082import java.io.FileInputStream;
83import java.io.FileNotFoundException;
Daniel Sandler0da673f2012-04-11 12:33:16 -040084import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import java.io.PrintWriter;
Daniel Sandlerf45564e2013-04-15 15:05:08 -040086import java.lang.reflect.Array;
Daniel Sandlerfde19b12013-01-17 00:21:05 -050087import java.util.ArrayDeque;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.util.ArrayList;
89import java.util.Arrays;
Daniel Sandler0da673f2012-04-11 12:33:16 -040090import java.util.HashSet;
Daniel Sandlerfde19b12013-01-17 00:21:05 -050091import java.util.Iterator;
92import java.util.NoSuchElementException;
Daniel Sandler0da673f2012-04-11 12:33:16 -040093
94import libcore.io.IoUtils;
95
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040097/** {@hide} */
98public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099{
100 private static final String TAG = "NotificationService";
101 private static final boolean DBG = false;
102
Joe Onoratobd73d012010-06-04 11:44:54 -0700103 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 // message codes
106 private static final int MESSAGE_TIMEOUT = 2;
107
108 private static final int LONG_DELAY = 3500; // 3.5 seconds
109 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800110
111 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
Daniel Sandleredbb3802012-11-13 20:49:47 -0800112 private static final int VIBRATE_PATTERN_MAXLEN = 8 * 2 + 1; // up to eight bumps
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
114 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400115 private static final boolean SCORE_ONGOING_HIGHER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
Daniel Sandler0da673f2012-04-11 12:33:16 -0400117 private static final int JUNK_SCORE = -1000;
118 private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10;
119 private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER;
120
Daniel Sandler526fa0e2012-12-04 14:51:50 -0500121 // Notifications with scores below this will not interrupt the user, either via LED or
122 // sound or vibration
123 private static final int SCORE_INTERRUPTION_THRESHOLD =
124 Notification.PRIORITY_LOW * NOTIFICATION_PRIORITY_MULTIPLIER;
125
Daniel Sandler0da673f2012-04-11 12:33:16 -0400126 private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true;
127 private static final boolean ENABLE_BLOCKED_TOASTS = true;
128
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700129 private static final String ENABLED_NOTIFICATION_LISTENERS_SEPARATOR = ":";
130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 final Context mContext;
132 final IActivityManager mAm;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400133 final UserManager mUserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 final IBinder mForegroundToken = new Binder();
135
136 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -0700137 private StatusBarManagerService mStatusBar;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500138 private LightsService.Light mNotificationLight;
139 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
Mike Lockwood670f9322010-01-20 12:13:36 -0500141 private int mDefaultNotificationColor;
142 private int mDefaultNotificationLedOn;
143 private int mDefaultNotificationLedOff;
144
Daniel Sandleredbb3802012-11-13 20:49:47 -0800145 private long[] mDefaultVibrationPattern;
146 private long[] mFallbackVibrationPattern;
147
Joe Onorato30275482009-07-08 17:09:14 -0700148 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400149 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150
Jeff Sharkey098d5802012-04-26 17:30:34 -0700151 private NotificationRecord mSoundNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 private NotificationRecord mVibrateNotification;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700153
154 private IAudioService mAudioService;
Jeff Brownc2346132012-04-13 01:55:38 -0700155 private Vibrator mVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500157 // for enabling and disabling notification pulse behavior
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400158 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500159 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500160 private boolean mNotificationPulseEnabled;
161
Daniel Sandler09a247e2013-02-14 10:24:17 -0500162 // used as a mutex for access to all active notifications & listeners
Fred Quintana6ecaff12009-09-25 14:23:13 -0700163 private final ArrayList<NotificationRecord> mNotificationList =
164 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
166 private ArrayList<ToastRecord> mToastQueue;
167
168 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700170
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500171 private final AppOpsManager mAppOps;
172
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700173 // contains connections to all connected listeners, including app services
174 // and system listeners
175 private ArrayList<NotificationListenerInfo> mListeners
176 = new ArrayList<NotificationListenerInfo>();
177 // things that will be put into mListeners as soon as they're ready
178 private ArrayList<String> mServicesBinding = new ArrayList<String>();
179 // lists the component names of all enabled (and therefore connected) listener
180 // app services for the current user only
181 private HashSet<ComponentName> mEnabledListenersForCurrentUser
182 = new HashSet<ComponentName>();
183 // Just the packages from mEnabledListenersForCurrentUser
184 private HashSet<String> mEnabledListenerPackageNames = new HashSet<String>();
Daniel Sandler09a247e2013-02-14 10:24:17 -0500185
Daniel Sandler0da673f2012-04-11 12:33:16 -0400186 // Notification control database. For now just contains disabled packages.
187 private AtomicFile mPolicyFile;
188 private HashSet<String> mBlockedPackages = new HashSet<String>();
189
190 private static final int DB_VERSION = 1;
191
192 private static final String TAG_BODY = "notification-policy";
193 private static final String ATTR_VERSION = "version";
194
195 private static final String TAG_BLOCKED_PKGS = "blocked-packages";
196 private static final String TAG_PACKAGE = "package";
197 private static final String ATTR_NAME = "name";
198
Daniel Sandler09a247e2013-02-14 10:24:17 -0500199 private class NotificationListenerInfo implements DeathRecipient {
200 INotificationListener listener;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700201 ComponentName component;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500202 int userid;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400203 boolean isSystem;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700204 ServiceConnection connection;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400205
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700206 public NotificationListenerInfo(INotificationListener listener, ComponentName component,
207 int userid, boolean isSystem) {
Daniel Sandler09a247e2013-02-14 10:24:17 -0500208 this.listener = listener;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700209 this.component = component;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500210 this.userid = userid;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400211 this.isSystem = isSystem;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700212 this.connection = null;
213 }
214
215 public NotificationListenerInfo(INotificationListener listener, ComponentName component,
216 int userid, ServiceConnection connection) {
217 this.listener = listener;
218 this.component = component;
219 this.userid = userid;
220 this.isSystem = false;
221 this.connection = connection;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500222 }
223
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400224 boolean enabledAndUserMatches(StatusBarNotification sbn) {
225 final int nid = sbn.getUserId();
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700226 if (!isEnabledForCurrentUser()) {
227 return false;
228 }
Daniel Sandler21ca44d2013-03-07 13:58:00 -0500229 if (this.userid == UserHandle.USER_ALL) return true;
Daniel Sandler21ca44d2013-03-07 13:58:00 -0500230 return (nid == UserHandle.USER_ALL || nid == this.userid);
231 }
232
Daniel Sandler09a247e2013-02-14 10:24:17 -0500233 public void notifyPostedIfUserMatch(StatusBarNotification sbn) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700234 if (!enabledAndUserMatches(sbn)) {
235 return;
236 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500237 try {
238 listener.onNotificationPosted(sbn);
239 } catch (RemoteException ex) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400240 Log.e(TAG, "unable to notify listener (posted): " + listener, ex);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500241 }
242 }
243
244 public void notifyRemovedIfUserMatch(StatusBarNotification sbn) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400245 if (!enabledAndUserMatches(sbn)) return;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500246 try {
247 listener.onNotificationRemoved(sbn);
248 } catch (RemoteException ex) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400249 Log.e(TAG, "unable to notify listener (removed): " + listener, ex);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500250 }
251 }
252
253 @Override
254 public void binderDied() {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700255 if (connection == null) {
256 // This is not a service; it won't be recreated. We can give up this connection.
257 unregisterListener(this.listener, this.userid);
258 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500259 }
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400260
261 /** convenience method for looking in mEnabledListenersForCurrentUser */
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700262 public boolean isEnabledForCurrentUser() {
263 if (this.isSystem) return true;
264 if (this.connection == null) return false;
265 return mEnabledListenersForCurrentUser.contains(this.component);
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400266 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500267 }
268
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500269 private static class Archive {
Daniel Sandler5e62e3a2013-04-15 20:57:02 -0400270 static final int BUFFER_SIZE = 250;
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500271 ArrayDeque<StatusBarNotification> mBuffer = new ArrayDeque<StatusBarNotification>(BUFFER_SIZE);
272
273 public Archive() {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500274 }
Jeff Sharkey0c1baf92013-04-03 13:08:52 -0700275
Daniel Sandler5e62e3a2013-04-15 20:57:02 -0400276 public String toString() {
277 final StringBuilder sb = new StringBuilder();
278 final int N = mBuffer.size();
279 sb.append("Archive (");
280 sb.append(N);
281 sb.append(" notification");
282 sb.append((N==1)?")":"s)");
283 return sb.toString();
284 }
285
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500286 public void record(StatusBarNotification nr) {
Jeff Sharkey0c1baf92013-04-03 13:08:52 -0700287 // Nuke heavy parts of notification before storing in archive
Daniel Sandler1a497d32013-04-18 14:52:45 -0400288 nr.notification.lightenPayload();
Jeff Sharkey0c1baf92013-04-03 13:08:52 -0700289
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500290 if (mBuffer.size() == BUFFER_SIZE) {
291 mBuffer.removeFirst();
292 }
293 mBuffer.addLast(nr);
294 }
295
Daniel Sandler5e62e3a2013-04-15 20:57:02 -0400296
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500297 public void clear() {
298 mBuffer.clear();
299 }
300
301 public Iterator<StatusBarNotification> descendingIterator() {
302 return mBuffer.descendingIterator();
303 }
304 public Iterator<StatusBarNotification> ascendingIterator() {
305 return mBuffer.iterator();
306 }
307 public Iterator<StatusBarNotification> filter(
308 final Iterator<StatusBarNotification> iter, final String pkg, final int userId) {
309 return new Iterator<StatusBarNotification>() {
310 StatusBarNotification mNext = findNext();
311
312 private StatusBarNotification findNext() {
313 while (iter.hasNext()) {
314 StatusBarNotification nr = iter.next();
315 if ((pkg == null || nr.pkg == pkg)
316 && (userId == UserHandle.USER_ALL || nr.getUserId() == userId)) {
317 return nr;
318 }
319 }
320 return null;
321 }
322
323 @Override
324 public boolean hasNext() {
325 return mNext == null;
326 }
327
328 @Override
329 public StatusBarNotification next() {
330 StatusBarNotification next = mNext;
331 if (next == null) {
332 throw new NoSuchElementException();
333 }
334 mNext = findNext();
335 return next;
336 }
337
338 @Override
339 public void remove() {
340 iter.remove();
341 }
342 };
343 }
Daniel Sandler78d0d252013-02-12 08:14:52 -0500344
345 public StatusBarNotification[] getArray(int count) {
346 if (count == 0) count = Archive.BUFFER_SIZE;
347 final StatusBarNotification[] a
348 = new StatusBarNotification[Math.min(count, mBuffer.size())];
349 Iterator<StatusBarNotification> iter = descendingIterator();
350 int i=0;
351 while (iter.hasNext() && i < count) {
352 a[i++] = iter.next();
353 }
354 return a;
355 }
356
357 public StatusBarNotification[] getArray(int count, String pkg, int userId) {
358 if (count == 0) count = Archive.BUFFER_SIZE;
359 final StatusBarNotification[] a
360 = new StatusBarNotification[Math.min(count, mBuffer.size())];
361 Iterator<StatusBarNotification> iter = filter(descendingIterator(), pkg, userId);
362 int i=0;
363 while (iter.hasNext() && i < count) {
364 a[i++] = iter.next();
365 }
366 return a;
367 }
368
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500369 }
370
371 Archive mArchive = new Archive();
372
Daniel Sandler0da673f2012-04-11 12:33:16 -0400373 private void loadBlockDb() {
374 synchronized(mBlockedPackages) {
375 if (mPolicyFile == null) {
376 File dir = new File("/data/system");
377 mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml"));
378
379 mBlockedPackages.clear();
380
381 FileInputStream infile = null;
382 try {
383 infile = mPolicyFile.openRead();
384 final XmlPullParser parser = Xml.newPullParser();
385 parser.setInput(infile, null);
386
387 int type;
388 String tag;
389 int version = DB_VERSION;
390 while ((type = parser.next()) != END_DOCUMENT) {
391 tag = parser.getName();
392 if (type == START_TAG) {
393 if (TAG_BODY.equals(tag)) {
394 version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
395 } else if (TAG_BLOCKED_PKGS.equals(tag)) {
396 while ((type = parser.next()) != END_DOCUMENT) {
397 tag = parser.getName();
398 if (TAG_PACKAGE.equals(tag)) {
399 mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME));
400 } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) {
401 break;
402 }
403 }
404 }
405 }
406 }
407 } catch (FileNotFoundException e) {
408 // No data yet
409 } catch (IOException e) {
410 Log.wtf(TAG, "Unable to read blocked notifications database", e);
411 } catch (NumberFormatException e) {
412 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
413 } catch (XmlPullParserException e) {
414 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
415 } finally {
416 IoUtils.closeQuietly(infile);
417 }
418 }
419 }
420 }
421
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500422 /**
423 * Use this when you just want to know if notifications are OK for this package.
424 */
425 public boolean areNotificationsEnabledForPackage(String pkg, int uid) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400426 checkCallerIsSystem();
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500427 return (mAppOps.checkOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
428 == AppOpsManager.MODE_ALLOWED);
Daniel Sandler0da673f2012-04-11 12:33:16 -0400429 }
430
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500431 /** Use this when you actually want to post a notification or toast.
432 *
433 * Unchecked. Not exposed via Binder, but can be called in the course of enqueue*().
434 */
435 private boolean noteNotificationOp(String pkg, int uid) {
436 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
437 != AppOpsManager.MODE_ALLOWED) {
438 Slog.v(TAG, "notifications are disabled by AppOps for " + pkg);
439 return false;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400440 }
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500441 return true;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400442 }
443
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500444 public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400445 checkCallerIsSystem();
Daniel Sandler32e698b2013-04-18 10:51:35 -0400446
447 Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);
448
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500449 mAppOps.setMode(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg,
450 enabled ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED);
Daniel Sandler32e698b2013-04-18 10:51:35 -0400451
452 // Now, cancel any outstanding notifications that are part of a just-disabled app
453 if (ENABLE_BLOCKED_NOTIFICATIONS && !enabled) {
454 cancelAllNotificationsInt(pkg, 0, 0, true, UserHandle.getUserId(uid));
455 }
Daniel Sandler0da673f2012-04-11 12:33:16 -0400456 }
457
458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 private static String idDebugString(Context baseContext, String packageName, int id) {
460 Context c = null;
461
462 if (packageName != null) {
463 try {
464 c = baseContext.createPackageContext(packageName, 0);
465 } catch (NameNotFoundException e) {
466 c = baseContext;
467 }
468 } else {
469 c = baseContext;
470 }
471
472 String pkg;
473 String type;
474 String name;
475
476 Resources r = c.getResources();
477 try {
478 return r.getResourceName(id);
479 } catch (Resources.NotFoundException e) {
480 return "<name unknown>";
481 }
482 }
483
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700484 /**
485 * System-only API for getting a list of current (i.e. not cleared) notifications.
486 *
487 * Requires ACCESS_NOTIFICATIONS which is signature|system.
488 */
489 @Override
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500490 public StatusBarNotification[] getActiveNotifications(String callingPkg) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400491 // enforce() will ensure the calling uid has the correct permission
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500492 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
Daniel Sandler78d0d252013-02-12 08:14:52 -0500493 "NotificationManagerService.getActiveNotifications");
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500494
495 StatusBarNotification[] tmp = null;
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500496 int uid = Binder.getCallingUid();
497
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400498 // noteOp will check to make sure the callingPkg matches the uid
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500499 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
500 == AppOpsManager.MODE_ALLOWED) {
501 synchronized (mNotificationList) {
502 tmp = new StatusBarNotification[mNotificationList.size()];
503 final int N = mNotificationList.size();
504 for (int i=0; i<N; i++) {
505 tmp[i] = mNotificationList.get(i).sbn;
506 }
507 }
508 }
509 return tmp;
510 }
511
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700512 /**
513 * System-only API for getting a list of recent (cleared, no longer shown) notifications.
514 *
515 * Requires ACCESS_NOTIFICATIONS which is signature|system.
516 */
517 @Override
Daniel Sandler78d0d252013-02-12 08:14:52 -0500518 public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400519 // enforce() will ensure the calling uid has the correct permission
Daniel Sandler78d0d252013-02-12 08:14:52 -0500520 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
521 "NotificationManagerService.getHistoricalNotifications");
522
523 StatusBarNotification[] tmp = null;
524 int uid = Binder.getCallingUid();
525
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400526 // noteOp will check to make sure the callingPkg matches the uid
Daniel Sandler78d0d252013-02-12 08:14:52 -0500527 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
528 == AppOpsManager.MODE_ALLOWED) {
529 synchronized (mArchive) {
530 tmp = mArchive.getArray(count);
531 }
532 }
533 return tmp;
534 }
535
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700536 /**
537 * Called whenever packages change, the user switches, or ENABLED_NOTIFICATION_LISTENERS
538 * is altered. (For example in response to USER_SWITCHED in our broadcast receiver)
539 */
540 void rebindListenerServices() {
541 String flat = Settings.Secure.getString(
542 mContext.getContentResolver(),
543 Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
544
545 NotificationListenerInfo[] toRemove = new NotificationListenerInfo[mListeners.size()];
546 final ArrayList<ComponentName> toAdd;
547 final int currentUser = ActivityManager.getCurrentUser();
548
549 synchronized (mNotificationList) {
550 // unbind and remove all existing listeners
551 toRemove = mListeners.toArray(toRemove);
552
553 toAdd = new ArrayList<ComponentName>();
554 final HashSet<ComponentName> newEnabled = new HashSet<ComponentName>();
555 final HashSet<String> newPackages = new HashSet<String>();
556
557 // decode the list of components
558 if (flat != null) {
559 String[] components = flat.split(ENABLED_NOTIFICATION_LISTENERS_SEPARATOR);
560 for (int i=0; i<components.length; i++) {
561 final ComponentName component
562 = ComponentName.unflattenFromString(components[i]);
563 if (component != null) {
564 newEnabled.add(component);
565 toAdd.add(component);
566 newPackages.add(component.getPackageName());
567 }
568 }
569
570 mEnabledListenersForCurrentUser = newEnabled;
571 mEnabledListenerPackageNames = newPackages;
572 }
573 }
574
575 for (NotificationListenerInfo info : toRemove) {
576 final ComponentName component = info.component;
577 final int oldUser = info.userid;
578 Slog.v(TAG, "disabling notification listener for user " + oldUser + ": " + component);
579 unregisterListenerService(component, info.userid);
580 }
581
582 final int N = toAdd.size();
583 for (int i=0; i<N; i++) {
584 final ComponentName component = toAdd.get(i);
585 Slog.v(TAG, "enabling notification listener for user " + currentUser + ": "
586 + component);
587 registerListenerService(component, currentUser);
588 }
Daniel 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) {
731 mContext.unbindService(info.connection);
732 }
733 }
734 }
735 }
736 }
737
738 /**
739 * asynchronously notify all listeners about a new notification
740 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500741 private void notifyPostedLocked(NotificationRecord n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400742 // make a copy in case changes are made to the underlying Notification object
743 final StatusBarNotification sbn = n.sbn.clone();
Daniel Sandler09a247e2013-02-14 10:24:17 -0500744 for (final NotificationListenerInfo info : mListeners) {
745 mHandler.post(new Runnable() {
746 @Override
747 public void run() {
748 info.notifyPostedIfUserMatch(sbn);
749 }});
750 }
751 }
752
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700753 /**
754 * asynchronously notify all listeners about a removed notification
755 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500756 private void notifyRemovedLocked(NotificationRecord n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400757 // make a copy in case changes are made to the underlying Notification object
758 // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the notification
759 final StatusBarNotification sbn_light = n.sbn.cloneLight();
760
Daniel Sandler09a247e2013-02-14 10:24:17 -0500761 for (final NotificationListenerInfo info : mListeners) {
762 mHandler.post(new Runnable() {
763 @Override
764 public void run() {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400765 info.notifyRemovedIfUserMatch(sbn_light);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500766 }});
767 }
768 }
769
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700770 // -- APIs to support listeners clicking/clearing notifications --
771
772 private NotificationListenerInfo checkListenerToken(INotificationListener listener) {
773 final IBinder token = listener.asBinder();
774 final int N = mListeners.size();
775 for (int i=0; i<N; i++) {
776 final NotificationListenerInfo info = mListeners.get(i);
777 if (info.listener.asBinder() == token) return info;
778 }
779 throw new SecurityException("Disallowed call from unknown listener: " + listener);
780 }
781
782 /**
783 * Allow an INotificationListener to simulate a "clear all" operation.
784 *
785 * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications}
786 *
787 * @param token The binder for the listener, to check that the caller is allowed
788 */
789 public void clearAllNotificationsFromListener(INotificationListener token) {
790 NotificationListenerInfo info = checkListenerToken(token);
791 long identity = Binder.clearCallingIdentity();
792 try {
793 cancelAll(info.userid);
794 } finally {
795 Binder.restoreCallingIdentity(identity);
796 }
797 }
798
799 /**
800 * Allow an INotificationListener to simulate clearing (dismissing) a single notification.
801 *
802 * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear}
803 *
804 * @param token The binder for the listener, to check that the caller is allowed
805 */
806 public void clearNotificationFromListener(INotificationListener token, String pkg, String tag, int id) {
807 NotificationListenerInfo info = checkListenerToken(token);
808 long identity = Binder.clearCallingIdentity();
809 try {
810 cancelNotification(pkg, tag, id, 0,
811 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
812 true,
813 info.userid);
814 } finally {
815 Binder.restoreCallingIdentity(identity);
816 }
817 }
818
819 // -- end of listener APIs --
820
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500821 public static final class NotificationRecord
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500823 final StatusBarNotification sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 IBinder statusBarKey;
825
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500826 NotificationRecord(StatusBarNotification sbn)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500828 this.sbn = sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700830
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500831 public Notification getNotification() { return sbn.notification; }
832 public int getFlags() { return sbn.notification.flags; }
833 public int getUserId() { return sbn.getUserId(); }
834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 void dump(PrintWriter pw, String prefix, Context baseContext) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500836 final Notification notification = sbn.notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 pw.println(prefix + this);
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400838 pw.println(prefix + " uid=" + sbn.uid + " userId=" + sbn.getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400840 + " / " + idDebugString(baseContext, sbn.pkg, notification.icon));
841 pw.println(prefix + " pri=" + notification.priority + " score=" + sbn.score);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 pw.println(prefix + " contentIntent=" + notification.contentIntent);
843 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
844 pw.println(prefix + " tickerText=" + notification.tickerText);
845 pw.println(prefix + " contentView=" + notification.contentView);
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400846 pw.println(prefix + String.format(" defaults=0x%08x flags=0x%08x",
847 notification.defaults, notification.flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 pw.println(prefix + " sound=" + notification.sound);
849 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400850 pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d",
851 notification.ledARGB, notification.ledOnMS, notification.ledOffMS));
852 if (notification.actions != null && notification.actions.length > 0) {
853 pw.println(prefix + " actions={");
854 final int N = notification.actions.length;
855 for (int i=0; i<N; i++) {
856 final Notification.Action action = notification.actions[i];
857 pw.println(String.format("%s [%d] \"%s\" -> %s",
858 prefix,
859 i,
860 action.title,
861 action.actionIntent.toString()
862 ));
863 }
864 pw.println(prefix + " }");
865 }
866 if (notification.extras != null && notification.extras.size() > 0) {
867 pw.println(prefix + " extras={");
868 for (String key : notification.extras.keySet()) {
869 pw.print(prefix + " " + key + "=");
870 Object val = notification.extras.get(key);
871 if (val == null) {
872 pw.println("null");
873 } else {
874 pw.print(val.toString());
875 if (val instanceof Bitmap) {
876 pw.print(String.format(" (%dx%d)",
877 ((Bitmap) val).getWidth(),
878 ((Bitmap) val).getHeight()));
879 } else if (val.getClass().isArray()) {
880 pw.println(" {");
881 final int N = Array.getLength(val);
882 for (int i=0; i<N; i++) {
883 if (i > 0) pw.println(",");
884 pw.print(prefix + " " + Array.get(val, i));
885 }
886 pw.print("\n" + prefix + " }");
887 }
888 pw.println();
889 }
890 }
891 pw.println(prefix + " }");
892 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 @Override
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500896 public final String toString() {
897 return String.format(
898 "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s score=%d: %s)",
899 System.identityHashCode(this),
900 this.sbn.pkg, this.sbn.user, this.sbn.id, this.sbn.tag,
901 this.sbn.score, this.sbn.notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 }
903 }
904
905 private static final class ToastRecord
906 {
907 final int pid;
908 final String pkg;
909 final ITransientNotification callback;
910 int duration;
911
912 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
913 {
914 this.pid = pid;
915 this.pkg = pkg;
916 this.callback = callback;
917 this.duration = duration;
918 }
919
920 void update(int duration) {
921 this.duration = duration;
922 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 void dump(PrintWriter pw, String prefix) {
925 pw.println(prefix + this);
926 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 @Override
929 public final String toString()
930 {
931 return "ToastRecord{"
932 + Integer.toHexString(System.identityHashCode(this))
933 + " pkg=" + pkg
934 + " callback=" + callback
935 + " duration=" + duration;
936 }
937 }
938
Joe Onorato089de882010-04-12 08:18:45 -0700939 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
940 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941
942 public void onSetDisabled(int status) {
943 synchronized (mNotificationList) {
944 mDisabledNotifications = status;
945 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
946 // cancel whatever's going on
947 long identity = Binder.clearCallingIdentity();
948 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700949 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
950 if (player != null) {
951 player.stopAsync();
952 }
953 } catch (RemoteException e) {
954 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 Binder.restoreCallingIdentity(identity);
956 }
957
958 identity = Binder.clearCallingIdentity();
959 try {
960 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700961 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 Binder.restoreCallingIdentity(identity);
963 }
964 }
965 }
966 }
967
968 public void onClearAll() {
Dianne Hackborn41203752012-08-31 14:05:51 -0700969 // XXX to be totally correct, the caller should tell us which user
970 // this is for.
971 cancelAll(ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 }
973
Fred Quintana6ecaff12009-09-25 14:23:13 -0700974 public void onNotificationClick(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700975 // XXX to be totally correct, the caller should tell us which user
976 // this is for.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700977 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackborn41203752012-08-31 14:05:51 -0700978 Notification.FLAG_FOREGROUND_SERVICE, false,
979 ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 }
981
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400982 public void onNotificationClear(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700983 // XXX to be totally correct, the caller should tell us which user
984 // this is for.
Joe Onorato46439ce2010-11-19 13:56:21 -0800985 cancelNotification(pkg, tag, id, 0,
986 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
Dianne Hackborn41203752012-08-31 14:05:51 -0700987 true, ActivityManager.getCurrentUser());
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400988 }
989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 public void onPanelRevealed() {
991 synchronized (mNotificationList) {
992 // sound
993 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 long identity = Binder.clearCallingIdentity();
996 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700997 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
998 if (player != null) {
999 player.stopAsync();
1000 }
1001 } catch (RemoteException e) {
1002 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 Binder.restoreCallingIdentity(identity);
1004 }
1005
1006 // vibrate
1007 mVibrateNotification = null;
1008 identity = Binder.clearCallingIdentity();
1009 try {
1010 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -07001011 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 Binder.restoreCallingIdentity(identity);
1013 }
1014
1015 // light
1016 mLights.clear();
1017 mLedNotification = null;
1018 updateLightsLocked();
1019 }
1020 }
Joe Onorato005847b2010-06-04 16:08:02 -04001021
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001022 public void onNotificationError(String pkg, String tag, int id,
1023 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001024 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
1025 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Dianne Hackborn41203752012-08-31 14:05:51 -07001026 // XXX to be totally correct, the caller should tell us which user
1027 // this is for.
1028 cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid));
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001029 long ident = Binder.clearCallingIdentity();
1030 try {
1031 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
1032 "Bad notification posted from package " + pkg
1033 + ": " + message);
1034 } catch (RemoteException e) {
1035 }
1036 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -04001037 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 };
1039
1040 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
1041 @Override
1042 public void onReceive(Context context, Intent intent) {
1043 String action = intent.getAction();
1044
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001045 boolean queryRestart = false;
Daniel Sandler26ece572012-06-01 15:38:46 -04001046 boolean packageChanged = false;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001047
Mike Lockwood541c9942011-06-12 19:35:45 -04001048 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001049 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandler26ece572012-06-01 15:38:46 -04001050 || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED))
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001051 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001052 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001053 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001054 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001055 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001056 } else if (queryRestart) {
1057 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001058 } else {
1059 Uri uri = intent.getData();
1060 if (uri == null) {
1061 return;
1062 }
1063 String pkgName = uri.getSchemeSpecificPart();
1064 if (pkgName == null) {
1065 return;
1066 }
Daniel Sandler26ece572012-06-01 15:38:46 -04001067 if (packageChanged) {
1068 // We cancel notifications for packages which have just been disabled
1069 final int enabled = mContext.getPackageManager()
1070 .getApplicationEnabledSetting(pkgName);
1071 if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
1072 || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
1073 return;
1074 }
1075 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001076 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001078
1079 boolean anyListenersInvolved = false;
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001080 if (pkgList != null && (pkgList.length > 0)) {
1081 for (String pkgName : pkgList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001082 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart,
1083 UserHandle.USER_ALL);
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001084 if (mEnabledListenerPackageNames.contains(pkgName)) {
1085 anyListenersInvolved = true;
1086 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001089
1090 if (anyListenersInvolved) {
1091 // make sure we're still bound to any of our
1092 // listeners who may have just upgraded
1093 rebindListenerServices();
1094 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001095 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
1096 // Keep track of screen on/off state, but do not turn off the notification light
1097 // until user passes through the lock screen or views the notification.
1098 mScreenOn = true;
1099 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
1100 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001101 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001102 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
1103 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001104 updateNotificationPulse();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001105 } else if (action.equals(Intent.ACTION_USER_STOPPED)) {
1106 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
1107 if (userHandle >= 0) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001108 cancelAllNotificationsInt(null, 0, 0, true, userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001109 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001110 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
1111 // turn off LED when user passes through lock screen
1112 mNotificationLight.turnOff();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001113 } else if (action.equals(Intent.ACTION_USER_SWITCHED)) {
1114 // reload per-user settings
1115 mSettingsObserver.update(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 }
1117 }
1118 };
1119
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001120 class SettingsObserver extends ContentObserver {
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001121 private final Uri NOTIFICATION_LIGHT_PULSE_URI
1122 = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE);
1123
1124 private final Uri ENABLED_NOTIFICATION_LISTENERS_URI
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001125 = Settings.Secure.getUriFor(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001126
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001127 SettingsObserver(Handler handler) {
1128 super(handler);
1129 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001130
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001131 void observe() {
1132 ContentResolver resolver = mContext.getContentResolver();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001133 resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI,
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001134 false, this, UserHandle.USER_ALL);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001135 resolver.registerContentObserver(ENABLED_NOTIFICATION_LISTENERS_URI,
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001136 false, this, UserHandle.USER_ALL);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001137 update(null);
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001138 }
1139
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001140 @Override public void onChange(boolean selfChange, Uri uri) {
1141 update(uri);
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001142 }
1143
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001144 public void update(Uri uri) {
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001145 ContentResolver resolver = mContext.getContentResolver();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001146 if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) {
1147 boolean pulseEnabled = Settings.System.getInt(resolver,
1148 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
1149 if (mNotificationPulseEnabled != pulseEnabled) {
1150 mNotificationPulseEnabled = pulseEnabled;
1151 updateNotificationPulse();
1152 }
1153 }
1154 if (uri == null || ENABLED_NOTIFICATION_LISTENERS_URI.equals(uri)) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001155 rebindListenerServices();
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001156 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001157 }
1158 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001159
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001160 private SettingsObserver mSettingsObserver;
1161
Daniel Sandleredbb3802012-11-13 20:49:47 -08001162 static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) {
1163 int[] ar = r.getIntArray(resid);
1164 if (ar == null) {
1165 return def;
1166 }
1167 final int len = ar.length > maxlen ? maxlen : ar.length;
1168 long[] out = new long[len];
1169 for (int i=0; i<len; i++) {
1170 out[i] = ar[i];
1171 }
1172 return out;
1173 }
1174
Joe Onorato089de882010-04-12 08:18:45 -07001175 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -05001176 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 {
1178 super();
1179 mContext = context;
Jeff Brownc2346132012-04-13 01:55:38 -07001180 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 mAm = ActivityManagerNative.getDefault();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001182 mUserManager = (UserManager)context.getSystemService(Context.USER_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -08001185
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001186 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
1187
1188 importOldBlockDb();
Daniel Sandler0da673f2012-04-11 12:33:16 -04001189
Joe Onorato089de882010-04-12 08:18:45 -07001190 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 statusBar.setNotificationCallbacks(mNotificationCallbacks);
1192
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001193 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
1194 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
1195
Mike Lockwood670f9322010-01-20 12:13:36 -05001196 Resources resources = mContext.getResources();
1197 mDefaultNotificationColor = resources.getColor(
1198 com.android.internal.R.color.config_defaultNotificationColor);
1199 mDefaultNotificationLedOn = resources.getInteger(
1200 com.android.internal.R.integer.config_defaultNotificationLedOn);
1201 mDefaultNotificationLedOff = resources.getInteger(
1202 com.android.internal.R.integer.config_defaultNotificationLedOff);
1203
Daniel Sandleredbb3802012-11-13 20:49:47 -08001204 mDefaultVibrationPattern = getLongArray(resources,
1205 com.android.internal.R.array.config_defaultNotificationVibePattern,
1206 VIBRATE_PATTERN_MAXLEN,
1207 DEFAULT_VIBRATE_PATTERN);
1208
1209 mFallbackVibrationPattern = getLongArray(resources,
1210 com.android.internal.R.array.config_notificationFallbackVibePattern,
1211 VIBRATE_PATTERN_MAXLEN,
1212 DEFAULT_VIBRATE_PATTERN);
1213
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001214 // Don't start allowing notifications until the setup wizard has run once.
1215 // After that, including subsequent boots, init with notifications turned on.
1216 // This works on the first boot because the setup wizard will toggle this
1217 // flag at least once and we'll go back to 0 after that.
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001218 if (0 == Settings.Global.getInt(mContext.getContentResolver(),
1219 Settings.Global.DEVICE_PROVISIONED, 0)) {
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001220 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
1221 }
1222
Mike Lockwood35e16bf2010-11-30 19:53:36 -05001223 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001225 filter.addAction(Intent.ACTION_SCREEN_ON);
1226 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001227 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001228 filter.addAction(Intent.ACTION_USER_PRESENT);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001229 filter.addAction(Intent.ACTION_USER_STOPPED);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001230 filter.addAction(Intent.ACTION_USER_SWITCHED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001232 IntentFilter pkgFilter = new IntentFilter();
1233 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -04001234 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001235 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
1236 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
1237 pkgFilter.addDataScheme("package");
1238 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001239 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001240 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001241
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001242 mSettingsObserver = new SettingsObserver(mHandler);
1243 mSettingsObserver.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 }
1245
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001246 /**
1247 * Read the old XML-based app block database and import those blockages into the AppOps system.
1248 */
1249 private void importOldBlockDb() {
1250 loadBlockDb();
1251
1252 PackageManager pm = mContext.getPackageManager();
1253 for (String pkg : mBlockedPackages) {
1254 PackageInfo info = null;
1255 try {
1256 info = pm.getPackageInfo(pkg, 0);
1257 setNotificationsEnabledForPackage(pkg, info.applicationInfo.uid, false);
1258 } catch (NameNotFoundException e) {
1259 // forget you
1260 }
1261 }
1262 mBlockedPackages.clear();
1263 if (mPolicyFile != null) {
1264 mPolicyFile.delete();
1265 }
1266 }
1267
Joe Onorato30275482009-07-08 17:09:14 -07001268 void systemReady() {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001269 mAudioService = IAudioService.Stub.asInterface(
1270 ServiceManager.getService(Context.AUDIO_SERVICE));
1271
Joe Onorato30275482009-07-08 17:09:14 -07001272 // no beeping until we're basically done booting
1273 mSystemReady = true;
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001274
1275 // make sure our listener services are properly bound
1276 rebindListenerServices();
Joe Onorato30275482009-07-08 17:09:14 -07001277 }
1278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 // Toasts
1280 // ============================================================================
1281 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
1282 {
Daniel Sandlera7035902010-03-30 15:45:31 -04001283 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284
1285 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001286 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 return ;
1288 }
1289
Daniel Sandler0da673f2012-04-11 12:33:16 -04001290 final boolean isSystemToast = ("android".equals(pkg));
1291
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001292 if (ENABLE_BLOCKED_TOASTS && !noteNotificationOp(pkg, Binder.getCallingUid())) {
1293 if (!isSystemToast) {
1294 Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
1295 return;
1296 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001297 }
1298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 synchronized (mToastQueue) {
1300 int callingPid = Binder.getCallingPid();
1301 long callingId = Binder.clearCallingIdentity();
1302 try {
1303 ToastRecord record;
1304 int index = indexOfToastLocked(pkg, callback);
1305 // If it's already in the queue, we update it in place, we don't
1306 // move it to the end of the queue.
1307 if (index >= 0) {
1308 record = mToastQueue.get(index);
1309 record.update(duration);
1310 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -08001311 // Limit the number of toasts that any given package except the android
1312 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -04001313 if (!isSystemToast) {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -08001314 int count = 0;
1315 final int N = mToastQueue.size();
1316 for (int i=0; i<N; i++) {
1317 final ToastRecord r = mToastQueue.get(i);
1318 if (r.pkg.equals(pkg)) {
1319 count++;
1320 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
1321 Slog.e(TAG, "Package has already posted " + count
1322 + " toasts. Not showing more. Package=" + pkg);
1323 return;
1324 }
1325 }
1326 }
1327 }
1328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 record = new ToastRecord(callingPid, pkg, callback, duration);
1330 mToastQueue.add(record);
1331 index = mToastQueue.size() - 1;
1332 keepProcessAliveLocked(callingPid);
1333 }
1334 // If it's at index 0, it's the current toast. It doesn't matter if it's
1335 // new or just been updated. Call back and tell it to show itself.
1336 // If the callback fails, this will remove it from the list, so don't
1337 // assume that it's valid after this.
1338 if (index == 0) {
1339 showNextToastLocked();
1340 }
1341 } finally {
1342 Binder.restoreCallingIdentity(callingId);
1343 }
1344 }
1345 }
1346
1347 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001348 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349
1350 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001351 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 return ;
1353 }
1354
1355 synchronized (mToastQueue) {
1356 long callingId = Binder.clearCallingIdentity();
1357 try {
1358 int index = indexOfToastLocked(pkg, callback);
1359 if (index >= 0) {
1360 cancelToastLocked(index);
1361 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001362 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 }
1364 } finally {
1365 Binder.restoreCallingIdentity(callingId);
1366 }
1367 }
1368 }
1369
1370 private void showNextToastLocked() {
1371 ToastRecord record = mToastQueue.get(0);
1372 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001373 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 try {
1375 record.callback.show();
1376 scheduleTimeoutLocked(record, false);
1377 return;
1378 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001379 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 + " in package " + record.pkg);
1381 // remove it from the list and let the process die
1382 int index = mToastQueue.indexOf(record);
1383 if (index >= 0) {
1384 mToastQueue.remove(index);
1385 }
1386 keepProcessAliveLocked(record.pid);
1387 if (mToastQueue.size() > 0) {
1388 record = mToastQueue.get(0);
1389 } else {
1390 record = null;
1391 }
1392 }
1393 }
1394 }
1395
1396 private void cancelToastLocked(int index) {
1397 ToastRecord record = mToastQueue.get(index);
1398 try {
1399 record.callback.hide();
1400 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001401 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 + " in package " + record.pkg);
1403 // don't worry about this, we're about to remove it from
1404 // the list anyway
1405 }
1406 mToastQueue.remove(index);
1407 keepProcessAliveLocked(record.pid);
1408 if (mToastQueue.size() > 0) {
1409 // Show the next one. If the callback fails, this will remove
1410 // it from the list, so don't assume that the list hasn't changed
1411 // after this point.
1412 showNextToastLocked();
1413 }
1414 }
1415
1416 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
1417 {
1418 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
1419 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
1420 mHandler.removeCallbacksAndMessages(r);
1421 mHandler.sendMessageDelayed(m, delay);
1422 }
1423
1424 private void handleTimeout(ToastRecord record)
1425 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001426 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 synchronized (mToastQueue) {
1428 int index = indexOfToastLocked(record.pkg, record.callback);
1429 if (index >= 0) {
1430 cancelToastLocked(index);
1431 }
1432 }
1433 }
1434
1435 // lock on mToastQueue
1436 private int indexOfToastLocked(String pkg, ITransientNotification callback)
1437 {
1438 IBinder cbak = callback.asBinder();
1439 ArrayList<ToastRecord> list = mToastQueue;
1440 int len = list.size();
1441 for (int i=0; i<len; i++) {
1442 ToastRecord r = list.get(i);
1443 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
1444 return i;
1445 }
1446 }
1447 return -1;
1448 }
1449
1450 // lock on mToastQueue
1451 private void keepProcessAliveLocked(int pid)
1452 {
1453 int toastCount = 0; // toasts from this pid
1454 ArrayList<ToastRecord> list = mToastQueue;
1455 int N = list.size();
1456 for (int i=0; i<N; i++) {
1457 ToastRecord r = list.get(i);
1458 if (r.pid == pid) {
1459 toastCount++;
1460 }
1461 }
1462 try {
1463 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
1464 } catch (RemoteException e) {
1465 // Shouldn't happen.
1466 }
1467 }
1468
1469 private final class WorkerHandler extends Handler
1470 {
1471 @Override
1472 public void handleMessage(Message msg)
1473 {
1474 switch (msg.what)
1475 {
1476 case MESSAGE_TIMEOUT:
1477 handleTimeout((ToastRecord)msg.obj);
1478 break;
1479 }
1480 }
1481 }
1482
1483
1484 // Notifications
1485 // ============================================================================
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001486 public void enqueueNotificationWithTag(String pkg, String basePkg, String tag, int id,
1487 Notification notification, int[] idOut, int userId)
Fred Quintana6ecaff12009-09-25 14:23:13 -07001488 {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001489 enqueueNotificationInternal(pkg, basePkg, Binder.getCallingUid(), Binder.getCallingPid(),
Dianne Hackborn41203752012-08-31 14:05:51 -07001490 tag, id, notification, idOut, userId);
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001491 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001492
1493 private final static int clamp(int x, int low, int high) {
1494 return (x < low) ? low : ((x > high) ? high : x);
Daniel Sandlere40451a2011-02-03 14:51:35 -05001495 }
1496
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001497 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
1498 // uid/pid of another application)
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001499 public void enqueueNotificationInternal(String pkg, String basePkg, int callingUid,
1500 int callingPid, String tag, int id, Notification notification, int[] idOut, int userId)
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001501 {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001502 if (DBG) {
1503 Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
1504 }
1505 checkCallerIsSystemOrSameApp(pkg);
1506 final boolean isSystemNotification = ("android".equals(pkg));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001507
Dianne Hackborn41203752012-08-31 14:05:51 -07001508 userId = ActivityManager.handleIncomingUser(callingPid,
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001509 callingUid, userId, true, false, "enqueueNotification", pkg);
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -07001510 final UserHandle user = new UserHandle(userId);
Dianne Hackborn41203752012-08-31 14:05:51 -07001511
Joe Onoratobd73d012010-06-04 11:44:54 -07001512 // Limit the number of notifications that any given package except the android
1513 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -04001514 if (!isSystemNotification) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001515 synchronized (mNotificationList) {
1516 int count = 0;
1517 final int N = mNotificationList.size();
1518 for (int i=0; i<N; i++) {
1519 final NotificationRecord r = mNotificationList.get(i);
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001520 if (r.sbn.pkg.equals(pkg) && r.sbn.getUserId() == userId) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001521 count++;
1522 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
1523 Slog.e(TAG, "Package has already posted " + count
1524 + " notifications. Not showing more. package=" + pkg);
1525 return;
1526 }
1527 }
1528 }
1529 }
1530 }
1531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 // This conditional is a dirty hack to limit the logging done on
1533 // behalf of the download manager without affecting other apps.
1534 if (!pkg.equals("com.android.providers.downloads")
1535 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001536 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001537 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 }
1539
1540 if (pkg == null || notification == null) {
1541 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
1542 + " id=" + id + " notification=" + notification);
1543 }
1544 if (notification.icon != 0) {
1545 if (notification.contentView == null) {
1546 throw new IllegalArgumentException("contentView required: pkg=" + pkg
1547 + " id=" + id + " notification=" + notification);
1548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 }
1550
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001551 // === Scoring ===
Daniel Sandler0da673f2012-04-11 12:33:16 -04001552
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001553 // 0. Sanitize inputs
1554 notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
1555 // Migrate notification flags to scores
1556 if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
1557 if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
Daniel Sandler49a2ad12012-03-28 15:46:39 -04001558 } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001559 if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
1560 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001561
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001562 // 1. initial score: buckets of 10, around the app
Daniel Sandler0da673f2012-04-11 12:33:16 -04001563 int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001564
Daniel Sandler0da673f2012-04-11 12:33:16 -04001565 // 2. Consult external heuristics (TBD)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001566
Daniel Sandler0da673f2012-04-11 12:33:16 -04001567 // 3. Apply local rules
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001568
1569 // blocked apps
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001570 if (ENABLE_BLOCKED_NOTIFICATIONS && !noteNotificationOp(pkg, callingUid)) {
1571 if (!isSystemNotification) {
1572 score = JUNK_SCORE;
1573 Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
1574 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001575 }
1576
1577 if (DBG) {
1578 Slog.v(TAG, "Assigned score=" + score + " to " + notification);
1579 }
1580
1581 if (score < SCORE_DISPLAY_THRESHOLD) {
1582 // Notification will be blocked because the score is too low.
1583 return;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001584 }
1585
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001586 // Should this notification make noise, vibe, or use the LED?
1587 final boolean canInterrupt = (score >= SCORE_INTERRUPTION_THRESHOLD);
1588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 synchronized (mNotificationList) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001590 final StatusBarNotification n = new StatusBarNotification(
1591 pkg, id, tag, callingUid, callingPid, score, notification, user);
1592 NotificationRecord r = new NotificationRecord(n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 NotificationRecord old = null;
1594
Dianne Hackborn41203752012-08-31 14:05:51 -07001595 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 if (index < 0) {
1597 mNotificationList.add(r);
1598 } else {
1599 old = mNotificationList.remove(index);
1600 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001601 // Make sure we don't lose the foreground service state.
1602 if (old != null) {
1603 notification.flags |=
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001604 old.getNotification().flags&Notification.FLAG_FOREGROUND_SERVICE;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001607
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001608 // Ensure if this is a foreground service that the proper additional
1609 // flags are set.
1610 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
1611 notification.flags |= Notification.FLAG_ONGOING_EVENT
1612 | Notification.FLAG_NO_CLEAR;
1613 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001614
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001615 final int currentUser;
1616 final long token = Binder.clearCallingIdentity();
1617 try {
1618 currentUser = ActivityManager.getCurrentUser();
1619 } finally {
1620 Binder.restoreCallingIdentity(token);
1621 }
1622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 if (notification.icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 if (old != null && old.statusBarKey != null) {
1625 r.statusBarKey = old.statusBarKey;
1626 long identity = Binder.clearCallingIdentity();
1627 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001628 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 }
1630 finally {
1631 Binder.restoreCallingIdentity(identity);
1632 }
1633 } else {
1634 long identity = Binder.clearCallingIdentity();
1635 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001636 r.statusBarKey = mStatusBar.addNotification(n);
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001637 if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1638 && canInterrupt) {
Mike Lockwoodece18ef2012-02-13 20:42:19 -08001639 mAttentionLight.pulse();
1640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 }
1642 finally {
1643 Binder.restoreCallingIdentity(identity);
1644 }
1645 }
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001646 // Send accessibility events only for the current user.
1647 if (currentUser == userId) {
1648 sendAccessibilityEvent(notification, pkg);
1649 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001650
Daniel Sandler09a247e2013-02-14 10:24:17 -05001651 notifyPostedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -05001653 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 if (old != null && old.statusBarKey != null) {
1655 long identity = Binder.clearCallingIdentity();
1656 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001657 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
1659 finally {
1660 Binder.restoreCallingIdentity(identity);
1661 }
Daniel Sandler09a247e2013-02-14 10:24:17 -05001662
1663 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001665 return; // do not play sounds, show lights, etc. for invalid notifications
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 }
1667
1668 // If we're not supposed to beep, vibrate, etc. then don't.
1669 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
1670 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -07001671 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001672 && (r.getUserId() == UserHandle.USER_ALL ||
1673 (r.getUserId() == userId && r.getUserId() == currentUser))
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001674 && canInterrupt
Joe Onorato30275482009-07-08 17:09:14 -07001675 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -08001676
1677 final AudioManager audioManager = (AudioManager) mContext
1678 .getSystemService(Context.AUDIO_SERVICE);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 // sound
Daniel Sandler31475232013-04-17 00:17:22 -04001681
1682 // should we use the default notification sound? (indicated either by DEFAULT_SOUND
1683 // or because notification.sound is pointing at Settings.System.NOTIFICATION_SOUND)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 final boolean useDefaultSound =
Daniel Sandler31475232013-04-17 00:17:22 -04001685 (notification.defaults & Notification.DEFAULT_SOUND) != 0
1686 || Settings.System.DEFAULT_NOTIFICATION_URI.equals(notification.sound);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001687
1688 Uri soundUri = null;
1689 boolean hasValidSound = false;
1690
1691 if (useDefaultSound) {
1692 soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
1693
1694 // check to see if the default notification sound is silent
1695 ContentResolver resolver = mContext.getContentResolver();
1696 hasValidSound = Settings.System.getString(resolver,
1697 Settings.System.NOTIFICATION_SOUND) != null;
1698 } else if (notification.sound != null) {
1699 soundUri = notification.sound;
1700 hasValidSound = (soundUri != null);
1701 }
1702
1703 if (hasValidSound) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
1705 int audioStreamType;
1706 if (notification.audioStreamType >= 0) {
1707 audioStreamType = notification.audioStreamType;
1708 } else {
1709 audioStreamType = DEFAULT_STREAM_TYPE;
1710 }
1711 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -08001712 // do not play notifications if stream volume is 0
Jean-Michel Trivid6770542012-10-10 12:03:41 -07001713 // (typically because ringer mode is silent) or if speech recognition is active.
1714 if ((audioManager.getStreamVolume(audioStreamType) != 0)
1715 && !audioManager.isSpeechRecognitionActive()) {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001716 final long identity = Binder.clearCallingIdentity();
Eric Laurent524dc042009-11-27 05:07:55 -08001717 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001718 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1719 if (player != null) {
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001720 player.playAsync(soundUri, user, looping, audioStreamType);
Jeff Sharkey098d5802012-04-26 17:30:34 -07001721 }
1722 } catch (RemoteException e) {
1723 } finally {
Eric Laurent524dc042009-11-27 05:07:55 -08001724 Binder.restoreCallingIdentity(identity);
1725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 }
1727 }
1728
1729 // vibrate
Daniel Sandleredbb3802012-11-13 20:49:47 -08001730 // Does the notification want to specify its own vibration?
1731 final boolean hasCustomVibrate = notification.vibrate != null;
1732
David Agnew71789e12012-11-09 23:03:26 -05001733 // 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 -05001734 // and no other vibration is specified, we fall back to vibration
David Agnew71789e12012-11-09 23:03:26 -05001735 final boolean convertSoundToVibration =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001736 !hasCustomVibrate
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001737 && hasValidSound
David Agnew71789e12012-11-09 23:03:26 -05001738 && (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
1739
Daniel Sandler4a7a9b92012-11-20 12:59:41 -05001740 // The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 final boolean useDefaultVibrate =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001742 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
David Agnew71789e12012-11-09 23:03:26 -05001743
Daniel Sandleredbb3802012-11-13 20:49:47 -08001744 if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate)
Eric Laurentbffc3d12012-05-07 17:43:49 -07001745 && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 mVibrateNotification = r;
1747
Daniel Sandleredbb3802012-11-13 20:49:47 -08001748 if (useDefaultVibrate || convertSoundToVibration) {
1749 // Escalate privileges so we can use the vibrator even if the notifying app
1750 // does not have the VIBRATE permission.
1751 long identity = Binder.clearCallingIdentity();
1752 try {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001753 mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg,
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001754 useDefaultVibrate ? mDefaultVibrationPattern
1755 : mFallbackVibrationPattern,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001756 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1757 } finally {
1758 Binder.restoreCallingIdentity(identity);
1759 }
1760 } else if (notification.vibrate.length > 1) {
1761 // If you want your own vibration pattern, you need the VIBRATE permission
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001762 mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg, notification.vibrate,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001763 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1764 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 }
1766 }
1767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 // light
1769 // the most recent thing gets the light
1770 mLights.remove(old);
1771 if (mLedNotification == old) {
1772 mLedNotification = null;
1773 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001774 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001776 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1777 && canInterrupt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 mLights.add(r);
1779 updateLightsLocked();
1780 } else {
1781 if (old != null
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001782 && ((old.getFlags() & Notification.FLAG_SHOW_LIGHTS) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 updateLightsLocked();
1784 }
1785 }
1786 }
1787
1788 idOut[0] = id;
1789 }
1790
Joe Onorato30275482009-07-08 17:09:14 -07001791 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001792 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
1793 if (!manager.isEnabled()) {
1794 return;
1795 }
1796
1797 AccessibilityEvent event =
1798 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
1799 event.setPackageName(packageName);
1800 event.setClassName(Notification.class.getName());
1801 event.setParcelableData(notification);
1802 CharSequence tickerText = notification.tickerText;
1803 if (!TextUtils.isEmpty(tickerText)) {
1804 event.getText().add(tickerText);
1805 }
1806
1807 manager.sendAccessibilityEvent(event);
1808 }
1809
Joe Onorato46439ce2010-11-19 13:56:21 -08001810 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
1811 // tell the app
1812 if (sendDelete) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001813 if (r.getNotification().deleteIntent != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001814 try {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001815 r.getNotification().deleteIntent.send();
Joe Onorato46439ce2010-11-19 13:56:21 -08001816 } catch (PendingIntent.CanceledException ex) {
1817 // do nothing - there's no relevant way to recover, and
1818 // no reason to let this propagate
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001819 Slog.w(TAG, "canceled PendingIntent for " + r.sbn.pkg, ex);
Joe Onorato46439ce2010-11-19 13:56:21 -08001820 }
1821 }
1822 }
1823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 // status bar
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001825 if (r.getNotification().icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 long identity = Binder.clearCallingIdentity();
1827 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001828 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 }
1830 finally {
1831 Binder.restoreCallingIdentity(identity);
1832 }
1833 r.statusBarKey = null;
Daniel Sandler09a247e2013-02-14 10:24:17 -05001834 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 }
1836
1837 // sound
1838 if (mSoundNotification == r) {
1839 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001840 final long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001842 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1843 if (player != null) {
1844 player.stopAsync();
1845 }
1846 } catch (RemoteException e) {
1847 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 Binder.restoreCallingIdentity(identity);
1849 }
1850 }
1851
1852 // vibrate
1853 if (mVibrateNotification == r) {
1854 mVibrateNotification = null;
1855 long identity = Binder.clearCallingIdentity();
1856 try {
1857 mVibrator.cancel();
1858 }
1859 finally {
1860 Binder.restoreCallingIdentity(identity);
1861 }
1862 }
1863
1864 // light
1865 mLights.remove(r);
1866 if (mLedNotification == r) {
1867 mLedNotification = null;
1868 }
Daniel Sandler23d7c702013-03-07 16:32:06 -05001869
1870 // Save it for users of getHistoricalNotifications()
1871 mArchive.record(r.sbn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 }
1873
1874 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001875 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001876 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 */
Fred Quintana6ecaff12009-09-25 14:23:13 -07001878 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001879 int mustNotHaveFlags, boolean sendDelete, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001880 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001881 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882
1883 synchronized (mNotificationList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001884 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001886 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001887
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001888 if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 return;
1890 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001891 if ((r.getNotification().flags & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001892 return;
1893 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 mNotificationList.remove(index);
1896
Joe Onorato46439ce2010-11-19 13:56:21 -08001897 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 updateLightsLocked();
1899 }
1900 }
1901 }
1902
1903 /**
Daniel Sandler321e9c52012-10-12 10:59:26 -07001904 * Determine whether the userId applies to the notification in question, either because
1905 * they match exactly, or one of them is USER_ALL (which is treated as a wildcard).
1906 */
1907 private boolean notificationMatchesUserId(NotificationRecord r, int userId) {
1908 return
1909 // looking for USER_ALL notifications? match everything
1910 userId == UserHandle.USER_ALL
1911 // a notification sent to USER_ALL matches any query
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001912 || r.getUserId() == UserHandle.USER_ALL
Daniel Sandler321e9c52012-10-12 10:59:26 -07001913 // an exact user match
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001914 || r.getUserId() == userId;
Daniel Sandler321e9c52012-10-12 10:59:26 -07001915 }
1916
1917 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 * Cancels all notifications from a given package that have all of the
1919 * {@code mustHaveFlags}.
1920 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001921 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001922 int mustNotHaveFlags, boolean doit, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001923 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, userId,
1924 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925
1926 synchronized (mNotificationList) {
1927 final int N = mNotificationList.size();
1928 boolean canceledSomething = false;
1929 for (int i = N-1; i >= 0; --i) {
1930 NotificationRecord r = mNotificationList.get(i);
Daniel Sandler321e9c52012-10-12 10:59:26 -07001931 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001932 continue;
1933 }
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001934 // Don't remove notifications to all, if there's no package name specified
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001935 if (r.getUserId() == UserHandle.USER_ALL && pkg == null) {
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001936 continue;
1937 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001938 if ((r.getFlags() & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 continue;
1940 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001941 if ((r.getFlags() & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001942 continue;
1943 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001944 if (pkg != null && !r.sbn.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 continue;
1946 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001947 canceledSomething = true;
1948 if (!doit) {
1949 return true;
1950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001952 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 }
1954 if (canceledSomething) {
1955 updateLightsLocked();
1956 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001957 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 }
1959 }
1960
Dianne Hackborn41203752012-08-31 14:05:51 -07001961 public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001962 checkCallerIsSystemOrSameApp(pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001963 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001964 Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001965 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001966 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001967 Binder.getCallingUid() == Process.SYSTEM_UID
Dianne Hackborn41203752012-08-31 14:05:51 -07001968 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 }
1970
Dianne Hackborn41203752012-08-31 14:05:51 -07001971 public void cancelAllNotifications(String pkg, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001972 checkCallerIsSystemOrSameApp(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001973
Dianne Hackborn41203752012-08-31 14:05:51 -07001974 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001975 Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001976
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001977 // Calling from user space, don't allow the canceling of actively
1978 // running foreground services.
Dianne Hackborn41203752012-08-31 14:05:51 -07001979 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 }
1981
Daniel Sandler0da673f2012-04-11 12:33:16 -04001982 void checkCallerIsSystem() {
1983 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001984 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001985 return;
1986 }
1987 throw new SecurityException("Disallowed call for uid " + uid);
1988 }
1989
1990 void checkCallerIsSystemOrSameApp(String pkg) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001991 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001992 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001993 return;
1994 }
1995 try {
Amith Yamasanif203aee2012-08-29 18:41:53 -07001996 ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
1997 pkg, 0, UserHandle.getCallingUserId());
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001998 if (!UserHandle.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001999 throw new SecurityException("Calling uid " + uid + " gave package"
2000 + pkg + " which is owned by uid " + ai.uid);
2001 }
Amith Yamasanif203aee2012-08-29 18:41:53 -07002002 } catch (RemoteException re) {
2003 throw new SecurityException("Unknown package " + pkg + "\n" + re);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002004 }
2005 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002006
Dianne Hackborn41203752012-08-31 14:05:51 -07002007 void cancelAll(int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 synchronized (mNotificationList) {
2009 final int N = mNotificationList.size();
2010 for (int i=N-1; i>=0; i--) {
2011 NotificationRecord r = mNotificationList.get(i);
2012
Daniel Sandler321e9c52012-10-12 10:59:26 -07002013 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07002014 continue;
2015 }
2016
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002017 if ((r.getFlags() & (Notification.FLAG_ONGOING_EVENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08002020 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
2022 }
2023
2024 updateLightsLocked();
2025 }
2026 }
2027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 // lock on mNotificationList
2029 private void updateLightsLocked()
2030 {
The Android Open Source Project10592532009-03-18 17:39:46 -07002031 // handle notification lights
2032 if (mLedNotification == null) {
2033 // get next notification, if any
2034 int n = mLights.size();
2035 if (n > 0) {
2036 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 }
2038 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05002039
Mike Lockwood63b5ad92011-08-30 09:55:30 -04002040 // Don't flash while we are in a call or screen is on
2041 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002042 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07002043 } else {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002044 final Notification ledno = mLedNotification.sbn.notification;
2045 int ledARGB = ledno.ledARGB;
2046 int ledOnMS = ledno.ledOnMS;
2047 int ledOffMS = ledno.ledOffMS;
2048 if ((ledno.defaults & Notification.DEFAULT_LIGHTS) != 0) {
Mike Lockwood670f9322010-01-20 12:13:36 -05002049 ledARGB = mDefaultNotificationColor;
2050 ledOnMS = mDefaultNotificationLedOn;
2051 ledOffMS = mDefaultNotificationLedOff;
2052 }
2053 if (mNotificationPulseEnabled) {
2054 // pulse repeatedly
2055 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
2056 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05002057 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 }
2060
2061 // lock on mNotificationList
Dianne Hackborn41203752012-08-31 14:05:51 -07002062 private int indexOfNotificationLocked(String pkg, String tag, int id, int userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 {
2064 ArrayList<NotificationRecord> list = mNotificationList;
2065 final int len = list.size();
2066 for (int i=0; i<len; i++) {
2067 NotificationRecord r = list.get(i);
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002068 if (!notificationMatchesUserId(r, userId) || r.sbn.id != id) {
Dianne Hackborn41203752012-08-31 14:05:51 -07002069 continue;
2070 }
Fred Quintana6ecaff12009-09-25 14:23:13 -07002071 if (tag == null) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002072 if (r.sbn.tag != null) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07002073 continue;
2074 }
2075 } else {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002076 if (!tag.equals(r.sbn.tag)) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07002077 continue;
2078 }
2079 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002080 if (r.sbn.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 return i;
2082 }
2083 }
2084 return -1;
2085 }
2086
Mike Lockwoodc22404a2009-12-02 11:15:02 -05002087 private void updateNotificationPulse() {
2088 synchronized (mNotificationList) {
2089 updateLightsLocked();
2090 }
2091 }
2092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 // ======================================================================
2094 @Override
2095 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2096 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2097 != PackageManager.PERMISSION_GRANTED) {
2098 pw.println("Permission Denial: can't dump NotificationManager from from pid="
2099 + Binder.getCallingPid()
2100 + ", uid=" + Binder.getCallingUid());
2101 return;
2102 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 pw.println("Current Notification Manager state:");
2105
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002106 pw.println(" Listeners (" + mEnabledListenersForCurrentUser.size()
2107 + ") enabled for current user:");
2108 for (ComponentName cmpt : mEnabledListenersForCurrentUser) {
2109 pw.println(" " + cmpt);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002110 }
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002111
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002112 pw.println(" Live listeners (" + mListeners.size() + "):");
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002113 for (NotificationListenerInfo info : mListeners) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002114 pw.println(" " + info.component
2115 + " (user " + info.userid + "): " + info.listener
2116 + (info.isSystem?" SYSTEM":""));
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002117 }
2118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 int N;
2120
2121 synchronized (mToastQueue) {
2122 N = mToastQueue.size();
2123 if (N > 0) {
2124 pw.println(" Toast Queue:");
2125 for (int i=0; i<N; i++) {
2126 mToastQueue.get(i).dump(pw, " ");
2127 }
2128 pw.println(" ");
2129 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 }
2132
2133 synchronized (mNotificationList) {
2134 N = mNotificationList.size();
2135 if (N > 0) {
2136 pw.println(" Notification List:");
2137 for (int i=0; i<N; i++) {
2138 mNotificationList.get(i).dump(pw, " ", mContext);
2139 }
2140 pw.println(" ");
2141 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 N = mLights.size();
2144 if (N > 0) {
2145 pw.println(" Lights List:");
2146 for (int i=0; i<N; i++) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002147 pw.println(" " + mLights.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 }
2149 pw.println(" ");
2150 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 pw.println(" mSoundNotification=" + mSoundNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04002154 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
2155 pw.println(" mSystemReady=" + mSystemReady);
Daniel Sandler5e62e3a2013-04-15 20:57:02 -04002156 pw.println(" mArchive=" + mArchive.toString());
2157 Iterator<StatusBarNotification> iter = mArchive.descendingIterator();
2158 int i=0;
2159 while (iter.hasNext()) {
2160 pw.println(" " + iter.next());
2161 if (++i >= 5) {
2162 if (iter.hasNext()) pw.println(" ...");
2163 break;
2164 }
2165 }
2166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 }
2168 }
2169}