blob: 6cac22736ffd20452a7a7260b1028cb7e54cc9b8 [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
Daniel Sandler25cf8ce2013-04-24 15:34:57 -0400819 /**
820 * Allow an INotificationListener to request the list of outstanding notifications seen by
821 * the current user. Useful when starting up, after which point the listener callbacks should
822 * be used.
823 *
824 * @param token The binder for the listener, to check that the caller is allowed
825 */
826 public StatusBarNotification[] getActiveNotificationsFromListener(INotificationListener token) {
827 NotificationListenerInfo info = checkListenerToken(token);
828
829 StatusBarNotification[] result = new StatusBarNotification[0];
830 ArrayList<StatusBarNotification> list = new ArrayList<StatusBarNotification>();
831 synchronized (mNotificationList) {
832 final int N = mNotificationList.size();
833 for (int i=0; i<N; i++) {
834 StatusBarNotification sbn = mNotificationList.get(i).sbn;
835 if (info.enabledAndUserMatches(sbn)) {
836 list.add(sbn);
837 }
838 }
839 }
840 return list.toArray(result);
841 }
842
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700843 // -- end of listener APIs --
844
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500845 public static final class NotificationRecord
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500847 final StatusBarNotification sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 IBinder statusBarKey;
849
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500850 NotificationRecord(StatusBarNotification sbn)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500852 this.sbn = sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700854
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500855 public Notification getNotification() { return sbn.notification; }
856 public int getFlags() { return sbn.notification.flags; }
857 public int getUserId() { return sbn.getUserId(); }
858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 void dump(PrintWriter pw, String prefix, Context baseContext) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500860 final Notification notification = sbn.notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 pw.println(prefix + this);
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400862 pw.println(prefix + " uid=" + sbn.uid + " userId=" + sbn.getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400864 + " / " + idDebugString(baseContext, sbn.pkg, notification.icon));
865 pw.println(prefix + " pri=" + notification.priority + " score=" + sbn.score);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 pw.println(prefix + " contentIntent=" + notification.contentIntent);
867 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
868 pw.println(prefix + " tickerText=" + notification.tickerText);
869 pw.println(prefix + " contentView=" + notification.contentView);
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400870 pw.println(prefix + String.format(" defaults=0x%08x flags=0x%08x",
871 notification.defaults, notification.flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 pw.println(prefix + " sound=" + notification.sound);
873 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400874 pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d",
875 notification.ledARGB, notification.ledOnMS, notification.ledOffMS));
876 if (notification.actions != null && notification.actions.length > 0) {
877 pw.println(prefix + " actions={");
878 final int N = notification.actions.length;
879 for (int i=0; i<N; i++) {
880 final Notification.Action action = notification.actions[i];
881 pw.println(String.format("%s [%d] \"%s\" -> %s",
882 prefix,
883 i,
884 action.title,
885 action.actionIntent.toString()
886 ));
887 }
888 pw.println(prefix + " }");
889 }
890 if (notification.extras != null && notification.extras.size() > 0) {
891 pw.println(prefix + " extras={");
892 for (String key : notification.extras.keySet()) {
893 pw.print(prefix + " " + key + "=");
894 Object val = notification.extras.get(key);
895 if (val == null) {
896 pw.println("null");
897 } else {
898 pw.print(val.toString());
899 if (val instanceof Bitmap) {
900 pw.print(String.format(" (%dx%d)",
901 ((Bitmap) val).getWidth(),
902 ((Bitmap) val).getHeight()));
903 } else if (val.getClass().isArray()) {
904 pw.println(" {");
905 final int N = Array.getLength(val);
906 for (int i=0; i<N; i++) {
907 if (i > 0) pw.println(",");
908 pw.print(prefix + " " + Array.get(val, i));
909 }
910 pw.print("\n" + prefix + " }");
911 }
912 pw.println();
913 }
914 }
915 pw.println(prefix + " }");
916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 @Override
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500920 public final String toString() {
921 return String.format(
922 "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s score=%d: %s)",
923 System.identityHashCode(this),
924 this.sbn.pkg, this.sbn.user, this.sbn.id, this.sbn.tag,
925 this.sbn.score, this.sbn.notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 }
927 }
928
929 private static final class ToastRecord
930 {
931 final int pid;
932 final String pkg;
933 final ITransientNotification callback;
934 int duration;
935
936 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
937 {
938 this.pid = pid;
939 this.pkg = pkg;
940 this.callback = callback;
941 this.duration = duration;
942 }
943
944 void update(int duration) {
945 this.duration = duration;
946 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 void dump(PrintWriter pw, String prefix) {
949 pw.println(prefix + this);
950 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 @Override
953 public final String toString()
954 {
955 return "ToastRecord{"
956 + Integer.toHexString(System.identityHashCode(this))
957 + " pkg=" + pkg
958 + " callback=" + callback
959 + " duration=" + duration;
960 }
961 }
962
Joe Onorato089de882010-04-12 08:18:45 -0700963 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
964 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965
966 public void onSetDisabled(int status) {
967 synchronized (mNotificationList) {
968 mDisabledNotifications = status;
969 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
970 // cancel whatever's going on
971 long identity = Binder.clearCallingIdentity();
972 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700973 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
974 if (player != null) {
975 player.stopAsync();
976 }
977 } catch (RemoteException e) {
978 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 Binder.restoreCallingIdentity(identity);
980 }
981
982 identity = Binder.clearCallingIdentity();
983 try {
984 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700985 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 Binder.restoreCallingIdentity(identity);
987 }
988 }
989 }
990 }
991
992 public void onClearAll() {
Dianne Hackborn41203752012-08-31 14:05:51 -0700993 // XXX to be totally correct, the caller should tell us which user
994 // this is for.
995 cancelAll(ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 }
997
Fred Quintana6ecaff12009-09-25 14:23:13 -0700998 public void onNotificationClick(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700999 // XXX to be totally correct, the caller should tell us which user
1000 // this is for.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001001 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackborn41203752012-08-31 14:05:51 -07001002 Notification.FLAG_FOREGROUND_SERVICE, false,
1003 ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 }
1005
Daniel Sandler0f0b11c2010-08-04 15:54:58 -04001006 public void onNotificationClear(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001007 // XXX to be totally correct, the caller should tell us which user
1008 // this is for.
Joe Onorato46439ce2010-11-19 13:56:21 -08001009 cancelNotification(pkg, tag, id, 0,
1010 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
Dianne Hackborn41203752012-08-31 14:05:51 -07001011 true, ActivityManager.getCurrentUser());
Daniel Sandler0f0b11c2010-08-04 15:54:58 -04001012 }
1013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 public void onPanelRevealed() {
1015 synchronized (mNotificationList) {
1016 // sound
1017 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 long identity = Binder.clearCallingIdentity();
1020 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001021 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1022 if (player != null) {
1023 player.stopAsync();
1024 }
1025 } catch (RemoteException e) {
1026 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 Binder.restoreCallingIdentity(identity);
1028 }
1029
1030 // vibrate
1031 mVibrateNotification = null;
1032 identity = Binder.clearCallingIdentity();
1033 try {
1034 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -07001035 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 Binder.restoreCallingIdentity(identity);
1037 }
1038
1039 // light
1040 mLights.clear();
1041 mLedNotification = null;
1042 updateLightsLocked();
1043 }
1044 }
Joe Onorato005847b2010-06-04 16:08:02 -04001045
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001046 public void onNotificationError(String pkg, String tag, int id,
1047 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001048 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
1049 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Dianne Hackborn41203752012-08-31 14:05:51 -07001050 // XXX to be totally correct, the caller should tell us which user
1051 // this is for.
1052 cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid));
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001053 long ident = Binder.clearCallingIdentity();
1054 try {
1055 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
1056 "Bad notification posted from package " + pkg
1057 + ": " + message);
1058 } catch (RemoteException e) {
1059 }
1060 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -04001061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 };
1063
1064 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
1065 @Override
1066 public void onReceive(Context context, Intent intent) {
1067 String action = intent.getAction();
1068
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001069 boolean queryRestart = false;
Daniel Sandler26ece572012-06-01 15:38:46 -04001070 boolean packageChanged = false;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001071
Mike Lockwood541c9942011-06-12 19:35:45 -04001072 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001073 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandler26ece572012-06-01 15:38:46 -04001074 || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED))
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001075 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001076 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001077 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001078 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001079 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001080 } else if (queryRestart) {
1081 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001082 } else {
1083 Uri uri = intent.getData();
1084 if (uri == null) {
1085 return;
1086 }
1087 String pkgName = uri.getSchemeSpecificPart();
1088 if (pkgName == null) {
1089 return;
1090 }
Daniel Sandler26ece572012-06-01 15:38:46 -04001091 if (packageChanged) {
1092 // We cancel notifications for packages which have just been disabled
1093 final int enabled = mContext.getPackageManager()
1094 .getApplicationEnabledSetting(pkgName);
1095 if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
1096 || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
1097 return;
1098 }
1099 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001100 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001102
1103 boolean anyListenersInvolved = false;
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001104 if (pkgList != null && (pkgList.length > 0)) {
1105 for (String pkgName : pkgList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001106 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart,
1107 UserHandle.USER_ALL);
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001108 if (mEnabledListenerPackageNames.contains(pkgName)) {
1109 anyListenersInvolved = true;
1110 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001113
1114 if (anyListenersInvolved) {
1115 // make sure we're still bound to any of our
1116 // listeners who may have just upgraded
1117 rebindListenerServices();
1118 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001119 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
1120 // Keep track of screen on/off state, but do not turn off the notification light
1121 // until user passes through the lock screen or views the notification.
1122 mScreenOn = true;
1123 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
1124 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001125 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001126 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
1127 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001128 updateNotificationPulse();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001129 } else if (action.equals(Intent.ACTION_USER_STOPPED)) {
1130 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
1131 if (userHandle >= 0) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001132 cancelAllNotificationsInt(null, 0, 0, true, userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001133 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001134 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
1135 // turn off LED when user passes through lock screen
1136 mNotificationLight.turnOff();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001137 } else if (action.equals(Intent.ACTION_USER_SWITCHED)) {
1138 // reload per-user settings
1139 mSettingsObserver.update(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 }
1141 }
1142 };
1143
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001144 class SettingsObserver extends ContentObserver {
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001145 private final Uri NOTIFICATION_LIGHT_PULSE_URI
1146 = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE);
1147
1148 private final Uri ENABLED_NOTIFICATION_LISTENERS_URI
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001149 = Settings.Secure.getUriFor(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001150
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001151 SettingsObserver(Handler handler) {
1152 super(handler);
1153 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001154
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001155 void observe() {
1156 ContentResolver resolver = mContext.getContentResolver();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001157 resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI,
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001158 false, this, UserHandle.USER_ALL);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001159 resolver.registerContentObserver(ENABLED_NOTIFICATION_LISTENERS_URI,
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001160 false, this, UserHandle.USER_ALL);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001161 update(null);
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001162 }
1163
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001164 @Override public void onChange(boolean selfChange, Uri uri) {
1165 update(uri);
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001166 }
1167
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001168 public void update(Uri uri) {
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001169 ContentResolver resolver = mContext.getContentResolver();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001170 if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) {
1171 boolean pulseEnabled = Settings.System.getInt(resolver,
1172 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
1173 if (mNotificationPulseEnabled != pulseEnabled) {
1174 mNotificationPulseEnabled = pulseEnabled;
1175 updateNotificationPulse();
1176 }
1177 }
1178 if (uri == null || ENABLED_NOTIFICATION_LISTENERS_URI.equals(uri)) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001179 rebindListenerServices();
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001180 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001181 }
1182 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001183
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001184 private SettingsObserver mSettingsObserver;
1185
Daniel Sandleredbb3802012-11-13 20:49:47 -08001186 static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) {
1187 int[] ar = r.getIntArray(resid);
1188 if (ar == null) {
1189 return def;
1190 }
1191 final int len = ar.length > maxlen ? maxlen : ar.length;
1192 long[] out = new long[len];
1193 for (int i=0; i<len; i++) {
1194 out[i] = ar[i];
1195 }
1196 return out;
1197 }
1198
Joe Onorato089de882010-04-12 08:18:45 -07001199 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -05001200 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 {
1202 super();
1203 mContext = context;
Jeff Brownc2346132012-04-13 01:55:38 -07001204 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 mAm = ActivityManagerNative.getDefault();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001206 mUserManager = (UserManager)context.getSystemService(Context.USER_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -08001209
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001210 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
1211
1212 importOldBlockDb();
Daniel Sandler0da673f2012-04-11 12:33:16 -04001213
Joe Onorato089de882010-04-12 08:18:45 -07001214 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 statusBar.setNotificationCallbacks(mNotificationCallbacks);
1216
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001217 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
1218 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
1219
Mike Lockwood670f9322010-01-20 12:13:36 -05001220 Resources resources = mContext.getResources();
1221 mDefaultNotificationColor = resources.getColor(
1222 com.android.internal.R.color.config_defaultNotificationColor);
1223 mDefaultNotificationLedOn = resources.getInteger(
1224 com.android.internal.R.integer.config_defaultNotificationLedOn);
1225 mDefaultNotificationLedOff = resources.getInteger(
1226 com.android.internal.R.integer.config_defaultNotificationLedOff);
1227
Daniel Sandleredbb3802012-11-13 20:49:47 -08001228 mDefaultVibrationPattern = getLongArray(resources,
1229 com.android.internal.R.array.config_defaultNotificationVibePattern,
1230 VIBRATE_PATTERN_MAXLEN,
1231 DEFAULT_VIBRATE_PATTERN);
1232
1233 mFallbackVibrationPattern = getLongArray(resources,
1234 com.android.internal.R.array.config_notificationFallbackVibePattern,
1235 VIBRATE_PATTERN_MAXLEN,
1236 DEFAULT_VIBRATE_PATTERN);
1237
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001238 // Don't start allowing notifications until the setup wizard has run once.
1239 // After that, including subsequent boots, init with notifications turned on.
1240 // This works on the first boot because the setup wizard will toggle this
1241 // flag at least once and we'll go back to 0 after that.
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001242 if (0 == Settings.Global.getInt(mContext.getContentResolver(),
1243 Settings.Global.DEVICE_PROVISIONED, 0)) {
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001244 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
1245 }
1246
Mike Lockwood35e16bf2010-11-30 19:53:36 -05001247 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001249 filter.addAction(Intent.ACTION_SCREEN_ON);
1250 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001251 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001252 filter.addAction(Intent.ACTION_USER_PRESENT);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001253 filter.addAction(Intent.ACTION_USER_STOPPED);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001254 filter.addAction(Intent.ACTION_USER_SWITCHED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001256 IntentFilter pkgFilter = new IntentFilter();
1257 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -04001258 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001259 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
1260 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
1261 pkgFilter.addDataScheme("package");
1262 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001263 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001264 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001265
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001266 mSettingsObserver = new SettingsObserver(mHandler);
1267 mSettingsObserver.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 }
1269
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001270 /**
1271 * Read the old XML-based app block database and import those blockages into the AppOps system.
1272 */
1273 private void importOldBlockDb() {
1274 loadBlockDb();
1275
1276 PackageManager pm = mContext.getPackageManager();
1277 for (String pkg : mBlockedPackages) {
1278 PackageInfo info = null;
1279 try {
1280 info = pm.getPackageInfo(pkg, 0);
1281 setNotificationsEnabledForPackage(pkg, info.applicationInfo.uid, false);
1282 } catch (NameNotFoundException e) {
1283 // forget you
1284 }
1285 }
1286 mBlockedPackages.clear();
1287 if (mPolicyFile != null) {
1288 mPolicyFile.delete();
1289 }
1290 }
1291
Joe Onorato30275482009-07-08 17:09:14 -07001292 void systemReady() {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001293 mAudioService = IAudioService.Stub.asInterface(
1294 ServiceManager.getService(Context.AUDIO_SERVICE));
1295
Joe Onorato30275482009-07-08 17:09:14 -07001296 // no beeping until we're basically done booting
1297 mSystemReady = true;
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001298
1299 // make sure our listener services are properly bound
1300 rebindListenerServices();
Joe Onorato30275482009-07-08 17:09:14 -07001301 }
1302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 // Toasts
1304 // ============================================================================
1305 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
1306 {
Daniel Sandlera7035902010-03-30 15:45:31 -04001307 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308
1309 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001310 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 return ;
1312 }
1313
Daniel Sandler0da673f2012-04-11 12:33:16 -04001314 final boolean isSystemToast = ("android".equals(pkg));
1315
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001316 if (ENABLE_BLOCKED_TOASTS && !noteNotificationOp(pkg, Binder.getCallingUid())) {
1317 if (!isSystemToast) {
1318 Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
1319 return;
1320 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001321 }
1322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 synchronized (mToastQueue) {
1324 int callingPid = Binder.getCallingPid();
1325 long callingId = Binder.clearCallingIdentity();
1326 try {
1327 ToastRecord record;
1328 int index = indexOfToastLocked(pkg, callback);
1329 // If it's already in the queue, we update it in place, we don't
1330 // move it to the end of the queue.
1331 if (index >= 0) {
1332 record = mToastQueue.get(index);
1333 record.update(duration);
1334 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -08001335 // Limit the number of toasts that any given package except the android
1336 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -04001337 if (!isSystemToast) {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -08001338 int count = 0;
1339 final int N = mToastQueue.size();
1340 for (int i=0; i<N; i++) {
1341 final ToastRecord r = mToastQueue.get(i);
1342 if (r.pkg.equals(pkg)) {
1343 count++;
1344 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
1345 Slog.e(TAG, "Package has already posted " + count
1346 + " toasts. Not showing more. Package=" + pkg);
1347 return;
1348 }
1349 }
1350 }
1351 }
1352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 record = new ToastRecord(callingPid, pkg, callback, duration);
1354 mToastQueue.add(record);
1355 index = mToastQueue.size() - 1;
1356 keepProcessAliveLocked(callingPid);
1357 }
1358 // If it's at index 0, it's the current toast. It doesn't matter if it's
1359 // new or just been updated. Call back and tell it to show itself.
1360 // If the callback fails, this will remove it from the list, so don't
1361 // assume that it's valid after this.
1362 if (index == 0) {
1363 showNextToastLocked();
1364 }
1365 } finally {
1366 Binder.restoreCallingIdentity(callingId);
1367 }
1368 }
1369 }
1370
1371 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001372 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373
1374 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001375 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 return ;
1377 }
1378
1379 synchronized (mToastQueue) {
1380 long callingId = Binder.clearCallingIdentity();
1381 try {
1382 int index = indexOfToastLocked(pkg, callback);
1383 if (index >= 0) {
1384 cancelToastLocked(index);
1385 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001386 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 }
1388 } finally {
1389 Binder.restoreCallingIdentity(callingId);
1390 }
1391 }
1392 }
1393
1394 private void showNextToastLocked() {
1395 ToastRecord record = mToastQueue.get(0);
1396 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001397 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 try {
1399 record.callback.show();
1400 scheduleTimeoutLocked(record, false);
1401 return;
1402 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001403 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 + " in package " + record.pkg);
1405 // remove it from the list and let the process die
1406 int index = mToastQueue.indexOf(record);
1407 if (index >= 0) {
1408 mToastQueue.remove(index);
1409 }
1410 keepProcessAliveLocked(record.pid);
1411 if (mToastQueue.size() > 0) {
1412 record = mToastQueue.get(0);
1413 } else {
1414 record = null;
1415 }
1416 }
1417 }
1418 }
1419
1420 private void cancelToastLocked(int index) {
1421 ToastRecord record = mToastQueue.get(index);
1422 try {
1423 record.callback.hide();
1424 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001425 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 + " in package " + record.pkg);
1427 // don't worry about this, we're about to remove it from
1428 // the list anyway
1429 }
1430 mToastQueue.remove(index);
1431 keepProcessAliveLocked(record.pid);
1432 if (mToastQueue.size() > 0) {
1433 // Show the next one. If the callback fails, this will remove
1434 // it from the list, so don't assume that the list hasn't changed
1435 // after this point.
1436 showNextToastLocked();
1437 }
1438 }
1439
1440 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
1441 {
1442 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
1443 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
1444 mHandler.removeCallbacksAndMessages(r);
1445 mHandler.sendMessageDelayed(m, delay);
1446 }
1447
1448 private void handleTimeout(ToastRecord record)
1449 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001450 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 synchronized (mToastQueue) {
1452 int index = indexOfToastLocked(record.pkg, record.callback);
1453 if (index >= 0) {
1454 cancelToastLocked(index);
1455 }
1456 }
1457 }
1458
1459 // lock on mToastQueue
1460 private int indexOfToastLocked(String pkg, ITransientNotification callback)
1461 {
1462 IBinder cbak = callback.asBinder();
1463 ArrayList<ToastRecord> list = mToastQueue;
1464 int len = list.size();
1465 for (int i=0; i<len; i++) {
1466 ToastRecord r = list.get(i);
1467 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
1468 return i;
1469 }
1470 }
1471 return -1;
1472 }
1473
1474 // lock on mToastQueue
1475 private void keepProcessAliveLocked(int pid)
1476 {
1477 int toastCount = 0; // toasts from this pid
1478 ArrayList<ToastRecord> list = mToastQueue;
1479 int N = list.size();
1480 for (int i=0; i<N; i++) {
1481 ToastRecord r = list.get(i);
1482 if (r.pid == pid) {
1483 toastCount++;
1484 }
1485 }
1486 try {
1487 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
1488 } catch (RemoteException e) {
1489 // Shouldn't happen.
1490 }
1491 }
1492
1493 private final class WorkerHandler extends Handler
1494 {
1495 @Override
1496 public void handleMessage(Message msg)
1497 {
1498 switch (msg.what)
1499 {
1500 case MESSAGE_TIMEOUT:
1501 handleTimeout((ToastRecord)msg.obj);
1502 break;
1503 }
1504 }
1505 }
1506
1507
1508 // Notifications
1509 // ============================================================================
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001510 public void enqueueNotificationWithTag(String pkg, String basePkg, String tag, int id,
1511 Notification notification, int[] idOut, int userId)
Fred Quintana6ecaff12009-09-25 14:23:13 -07001512 {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001513 enqueueNotificationInternal(pkg, basePkg, Binder.getCallingUid(), Binder.getCallingPid(),
Dianne Hackborn41203752012-08-31 14:05:51 -07001514 tag, id, notification, idOut, userId);
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001515 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001516
1517 private final static int clamp(int x, int low, int high) {
1518 return (x < low) ? low : ((x > high) ? high : x);
Daniel Sandlere40451a2011-02-03 14:51:35 -05001519 }
1520
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001521 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
1522 // uid/pid of another application)
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001523 public void enqueueNotificationInternal(String pkg, String basePkg, int callingUid,
1524 int callingPid, String tag, int id, Notification notification, int[] idOut, int userId)
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001525 {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001526 if (DBG) {
1527 Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
1528 }
1529 checkCallerIsSystemOrSameApp(pkg);
1530 final boolean isSystemNotification = ("android".equals(pkg));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001531
Dianne Hackborn41203752012-08-31 14:05:51 -07001532 userId = ActivityManager.handleIncomingUser(callingPid,
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001533 callingUid, userId, true, false, "enqueueNotification", pkg);
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -07001534 final UserHandle user = new UserHandle(userId);
Dianne Hackborn41203752012-08-31 14:05:51 -07001535
Joe Onoratobd73d012010-06-04 11:44:54 -07001536 // Limit the number of notifications that any given package except the android
1537 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -04001538 if (!isSystemNotification) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001539 synchronized (mNotificationList) {
1540 int count = 0;
1541 final int N = mNotificationList.size();
1542 for (int i=0; i<N; i++) {
1543 final NotificationRecord r = mNotificationList.get(i);
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001544 if (r.sbn.pkg.equals(pkg) && r.sbn.getUserId() == userId) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001545 count++;
1546 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
1547 Slog.e(TAG, "Package has already posted " + count
1548 + " notifications. Not showing more. package=" + pkg);
1549 return;
1550 }
1551 }
1552 }
1553 }
1554 }
1555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 // This conditional is a dirty hack to limit the logging done on
1557 // behalf of the download manager without affecting other apps.
1558 if (!pkg.equals("com.android.providers.downloads")
1559 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001560 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001561 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 }
1563
1564 if (pkg == null || notification == null) {
1565 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
1566 + " id=" + id + " notification=" + notification);
1567 }
1568 if (notification.icon != 0) {
1569 if (notification.contentView == null) {
1570 throw new IllegalArgumentException("contentView required: pkg=" + pkg
1571 + " id=" + id + " notification=" + notification);
1572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
1574
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001575 // === Scoring ===
Daniel Sandler0da673f2012-04-11 12:33:16 -04001576
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001577 // 0. Sanitize inputs
1578 notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
1579 // Migrate notification flags to scores
1580 if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
1581 if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
Daniel Sandler49a2ad12012-03-28 15:46:39 -04001582 } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001583 if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
1584 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001585
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001586 // 1. initial score: buckets of 10, around the app
Daniel Sandler0da673f2012-04-11 12:33:16 -04001587 int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001588
Daniel Sandler0da673f2012-04-11 12:33:16 -04001589 // 2. Consult external heuristics (TBD)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001590
Daniel Sandler0da673f2012-04-11 12:33:16 -04001591 // 3. Apply local rules
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001592
1593 // blocked apps
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001594 if (ENABLE_BLOCKED_NOTIFICATIONS && !noteNotificationOp(pkg, callingUid)) {
1595 if (!isSystemNotification) {
1596 score = JUNK_SCORE;
1597 Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
1598 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001599 }
1600
1601 if (DBG) {
1602 Slog.v(TAG, "Assigned score=" + score + " to " + notification);
1603 }
1604
1605 if (score < SCORE_DISPLAY_THRESHOLD) {
1606 // Notification will be blocked because the score is too low.
1607 return;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001608 }
1609
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001610 // Should this notification make noise, vibe, or use the LED?
1611 final boolean canInterrupt = (score >= SCORE_INTERRUPTION_THRESHOLD);
1612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 synchronized (mNotificationList) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001614 final StatusBarNotification n = new StatusBarNotification(
1615 pkg, id, tag, callingUid, callingPid, score, notification, user);
1616 NotificationRecord r = new NotificationRecord(n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 NotificationRecord old = null;
1618
Dianne Hackborn41203752012-08-31 14:05:51 -07001619 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 if (index < 0) {
1621 mNotificationList.add(r);
1622 } else {
1623 old = mNotificationList.remove(index);
1624 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001625 // Make sure we don't lose the foreground service state.
1626 if (old != null) {
1627 notification.flags |=
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001628 old.getNotification().flags&Notification.FLAG_FOREGROUND_SERVICE;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001631
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001632 // Ensure if this is a foreground service that the proper additional
1633 // flags are set.
1634 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
1635 notification.flags |= Notification.FLAG_ONGOING_EVENT
1636 | Notification.FLAG_NO_CLEAR;
1637 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001638
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001639 final int currentUser;
1640 final long token = Binder.clearCallingIdentity();
1641 try {
1642 currentUser = ActivityManager.getCurrentUser();
1643 } finally {
1644 Binder.restoreCallingIdentity(token);
1645 }
1646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 if (notification.icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 if (old != null && old.statusBarKey != null) {
1649 r.statusBarKey = old.statusBarKey;
1650 long identity = Binder.clearCallingIdentity();
1651 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001652 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 }
1654 finally {
1655 Binder.restoreCallingIdentity(identity);
1656 }
1657 } else {
1658 long identity = Binder.clearCallingIdentity();
1659 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001660 r.statusBarKey = mStatusBar.addNotification(n);
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001661 if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1662 && canInterrupt) {
Mike Lockwoodece18ef2012-02-13 20:42:19 -08001663 mAttentionLight.pulse();
1664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 }
1666 finally {
1667 Binder.restoreCallingIdentity(identity);
1668 }
1669 }
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001670 // Send accessibility events only for the current user.
1671 if (currentUser == userId) {
1672 sendAccessibilityEvent(notification, pkg);
1673 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001674
Daniel Sandler09a247e2013-02-14 10:24:17 -05001675 notifyPostedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -05001677 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 if (old != null && old.statusBarKey != null) {
1679 long identity = Binder.clearCallingIdentity();
1680 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001681 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 }
1683 finally {
1684 Binder.restoreCallingIdentity(identity);
1685 }
Daniel Sandler09a247e2013-02-14 10:24:17 -05001686
1687 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001689 return; // do not play sounds, show lights, etc. for invalid notifications
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 }
1691
1692 // If we're not supposed to beep, vibrate, etc. then don't.
1693 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
1694 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -07001695 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001696 && (r.getUserId() == UserHandle.USER_ALL ||
1697 (r.getUserId() == userId && r.getUserId() == currentUser))
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001698 && canInterrupt
Joe Onorato30275482009-07-08 17:09:14 -07001699 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -08001700
1701 final AudioManager audioManager = (AudioManager) mContext
1702 .getSystemService(Context.AUDIO_SERVICE);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 // sound
Daniel Sandler31475232013-04-17 00:17:22 -04001705
1706 // should we use the default notification sound? (indicated either by DEFAULT_SOUND
1707 // or because notification.sound is pointing at Settings.System.NOTIFICATION_SOUND)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 final boolean useDefaultSound =
Daniel Sandler31475232013-04-17 00:17:22 -04001709 (notification.defaults & Notification.DEFAULT_SOUND) != 0
1710 || Settings.System.DEFAULT_NOTIFICATION_URI.equals(notification.sound);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001711
1712 Uri soundUri = null;
1713 boolean hasValidSound = false;
1714
1715 if (useDefaultSound) {
1716 soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
1717
1718 // check to see if the default notification sound is silent
1719 ContentResolver resolver = mContext.getContentResolver();
1720 hasValidSound = Settings.System.getString(resolver,
1721 Settings.System.NOTIFICATION_SOUND) != null;
1722 } else if (notification.sound != null) {
1723 soundUri = notification.sound;
1724 hasValidSound = (soundUri != null);
1725 }
1726
1727 if (hasValidSound) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
1729 int audioStreamType;
1730 if (notification.audioStreamType >= 0) {
1731 audioStreamType = notification.audioStreamType;
1732 } else {
1733 audioStreamType = DEFAULT_STREAM_TYPE;
1734 }
1735 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -08001736 // do not play notifications if stream volume is 0
Jean-Michel Trivid6770542012-10-10 12:03:41 -07001737 // (typically because ringer mode is silent) or if speech recognition is active.
1738 if ((audioManager.getStreamVolume(audioStreamType) != 0)
1739 && !audioManager.isSpeechRecognitionActive()) {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001740 final long identity = Binder.clearCallingIdentity();
Eric Laurent524dc042009-11-27 05:07:55 -08001741 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001742 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1743 if (player != null) {
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001744 player.playAsync(soundUri, user, looping, audioStreamType);
Jeff Sharkey098d5802012-04-26 17:30:34 -07001745 }
1746 } catch (RemoteException e) {
1747 } finally {
Eric Laurent524dc042009-11-27 05:07:55 -08001748 Binder.restoreCallingIdentity(identity);
1749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 }
1751 }
1752
1753 // vibrate
Daniel Sandleredbb3802012-11-13 20:49:47 -08001754 // Does the notification want to specify its own vibration?
1755 final boolean hasCustomVibrate = notification.vibrate != null;
1756
David Agnew71789e12012-11-09 23:03:26 -05001757 // 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 -05001758 // and no other vibration is specified, we fall back to vibration
David Agnew71789e12012-11-09 23:03:26 -05001759 final boolean convertSoundToVibration =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001760 !hasCustomVibrate
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001761 && hasValidSound
David Agnew71789e12012-11-09 23:03:26 -05001762 && (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
1763
Daniel Sandler4a7a9b92012-11-20 12:59:41 -05001764 // The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 final boolean useDefaultVibrate =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001766 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
David Agnew71789e12012-11-09 23:03:26 -05001767
Daniel Sandleredbb3802012-11-13 20:49:47 -08001768 if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate)
Eric Laurentbffc3d12012-05-07 17:43:49 -07001769 && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 mVibrateNotification = r;
1771
Daniel Sandleredbb3802012-11-13 20:49:47 -08001772 if (useDefaultVibrate || convertSoundToVibration) {
1773 // Escalate privileges so we can use the vibrator even if the notifying app
1774 // does not have the VIBRATE permission.
1775 long identity = Binder.clearCallingIdentity();
1776 try {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001777 mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg,
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001778 useDefaultVibrate ? mDefaultVibrationPattern
1779 : mFallbackVibrationPattern,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001780 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1781 } finally {
1782 Binder.restoreCallingIdentity(identity);
1783 }
1784 } else if (notification.vibrate.length > 1) {
1785 // If you want your own vibration pattern, you need the VIBRATE permission
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001786 mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg, notification.vibrate,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001787 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
1790 }
1791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 // light
1793 // the most recent thing gets the light
1794 mLights.remove(old);
1795 if (mLedNotification == old) {
1796 mLedNotification = null;
1797 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001798 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001800 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1801 && canInterrupt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 mLights.add(r);
1803 updateLightsLocked();
1804 } else {
1805 if (old != null
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001806 && ((old.getFlags() & Notification.FLAG_SHOW_LIGHTS) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 updateLightsLocked();
1808 }
1809 }
1810 }
1811
1812 idOut[0] = id;
1813 }
1814
Joe Onorato30275482009-07-08 17:09:14 -07001815 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001816 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
1817 if (!manager.isEnabled()) {
1818 return;
1819 }
1820
1821 AccessibilityEvent event =
1822 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
1823 event.setPackageName(packageName);
1824 event.setClassName(Notification.class.getName());
1825 event.setParcelableData(notification);
1826 CharSequence tickerText = notification.tickerText;
1827 if (!TextUtils.isEmpty(tickerText)) {
1828 event.getText().add(tickerText);
1829 }
1830
1831 manager.sendAccessibilityEvent(event);
1832 }
1833
Joe Onorato46439ce2010-11-19 13:56:21 -08001834 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
1835 // tell the app
1836 if (sendDelete) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001837 if (r.getNotification().deleteIntent != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001838 try {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001839 r.getNotification().deleteIntent.send();
Joe Onorato46439ce2010-11-19 13:56:21 -08001840 } catch (PendingIntent.CanceledException ex) {
1841 // do nothing - there's no relevant way to recover, and
1842 // no reason to let this propagate
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001843 Slog.w(TAG, "canceled PendingIntent for " + r.sbn.pkg, ex);
Joe Onorato46439ce2010-11-19 13:56:21 -08001844 }
1845 }
1846 }
1847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 // status bar
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001849 if (r.getNotification().icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 long identity = Binder.clearCallingIdentity();
1851 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001852 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 }
1854 finally {
1855 Binder.restoreCallingIdentity(identity);
1856 }
1857 r.statusBarKey = null;
Daniel Sandler09a247e2013-02-14 10:24:17 -05001858 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 }
1860
1861 // sound
1862 if (mSoundNotification == r) {
1863 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001864 final long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001866 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1867 if (player != null) {
1868 player.stopAsync();
1869 }
1870 } catch (RemoteException e) {
1871 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 Binder.restoreCallingIdentity(identity);
1873 }
1874 }
1875
1876 // vibrate
1877 if (mVibrateNotification == r) {
1878 mVibrateNotification = null;
1879 long identity = Binder.clearCallingIdentity();
1880 try {
1881 mVibrator.cancel();
1882 }
1883 finally {
1884 Binder.restoreCallingIdentity(identity);
1885 }
1886 }
1887
1888 // light
1889 mLights.remove(r);
1890 if (mLedNotification == r) {
1891 mLedNotification = null;
1892 }
Daniel Sandler23d7c702013-03-07 16:32:06 -05001893
1894 // Save it for users of getHistoricalNotifications()
1895 mArchive.record(r.sbn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 }
1897
1898 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001899 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001900 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 */
Fred Quintana6ecaff12009-09-25 14:23:13 -07001902 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001903 int mustNotHaveFlags, boolean sendDelete, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001904 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001905 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906
1907 synchronized (mNotificationList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001908 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001910 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001911
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001912 if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 return;
1914 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001915 if ((r.getNotification().flags & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001916 return;
1917 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 mNotificationList.remove(index);
1920
Joe Onorato46439ce2010-11-19 13:56:21 -08001921 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 updateLightsLocked();
1923 }
1924 }
1925 }
1926
1927 /**
Daniel Sandler321e9c52012-10-12 10:59:26 -07001928 * Determine whether the userId applies to the notification in question, either because
1929 * they match exactly, or one of them is USER_ALL (which is treated as a wildcard).
1930 */
1931 private boolean notificationMatchesUserId(NotificationRecord r, int userId) {
1932 return
1933 // looking for USER_ALL notifications? match everything
1934 userId == UserHandle.USER_ALL
1935 // a notification sent to USER_ALL matches any query
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001936 || r.getUserId() == UserHandle.USER_ALL
Daniel Sandler321e9c52012-10-12 10:59:26 -07001937 // an exact user match
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001938 || r.getUserId() == userId;
Daniel Sandler321e9c52012-10-12 10:59:26 -07001939 }
1940
1941 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 * Cancels all notifications from a given package that have all of the
1943 * {@code mustHaveFlags}.
1944 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001945 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001946 int mustNotHaveFlags, boolean doit, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001947 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, userId,
1948 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949
1950 synchronized (mNotificationList) {
1951 final int N = mNotificationList.size();
1952 boolean canceledSomething = false;
1953 for (int i = N-1; i >= 0; --i) {
1954 NotificationRecord r = mNotificationList.get(i);
Daniel Sandler321e9c52012-10-12 10:59:26 -07001955 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001956 continue;
1957 }
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001958 // Don't remove notifications to all, if there's no package name specified
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001959 if (r.getUserId() == UserHandle.USER_ALL && pkg == null) {
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001960 continue;
1961 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001962 if ((r.getFlags() & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 continue;
1964 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001965 if ((r.getFlags() & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001966 continue;
1967 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001968 if (pkg != null && !r.sbn.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969 continue;
1970 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001971 canceledSomething = true;
1972 if (!doit) {
1973 return true;
1974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001976 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 }
1978 if (canceledSomething) {
1979 updateLightsLocked();
1980 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001981 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 }
1983 }
1984
Dianne Hackborn41203752012-08-31 14:05:51 -07001985 public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001986 checkCallerIsSystemOrSameApp(pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001987 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001988 Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001989 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001990 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001991 Binder.getCallingUid() == Process.SYSTEM_UID
Dianne Hackborn41203752012-08-31 14:05:51 -07001992 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 }
1994
Dianne Hackborn41203752012-08-31 14:05:51 -07001995 public void cancelAllNotifications(String pkg, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001996 checkCallerIsSystemOrSameApp(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001997
Dianne Hackborn41203752012-08-31 14:05:51 -07001998 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001999 Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07002000
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002001 // Calling from user space, don't allow the canceling of actively
2002 // running foreground services.
Dianne Hackborn41203752012-08-31 14:05:51 -07002003 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 }
2005
Daniel Sandler0da673f2012-04-11 12:33:16 -04002006 void checkCallerIsSystem() {
2007 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07002008 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04002009 return;
2010 }
2011 throw new SecurityException("Disallowed call for uid " + uid);
2012 }
2013
2014 void checkCallerIsSystemOrSameApp(String pkg) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002015 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07002016 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002017 return;
2018 }
2019 try {
Amith Yamasanif203aee2012-08-29 18:41:53 -07002020 ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
2021 pkg, 0, UserHandle.getCallingUserId());
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002022 if (!UserHandle.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002023 throw new SecurityException("Calling uid " + uid + " gave package"
2024 + pkg + " which is owned by uid " + ai.uid);
2025 }
Amith Yamasanif203aee2012-08-29 18:41:53 -07002026 } catch (RemoteException re) {
2027 throw new SecurityException("Unknown package " + pkg + "\n" + re);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002028 }
2029 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002030
Dianne Hackborn41203752012-08-31 14:05:51 -07002031 void cancelAll(int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 synchronized (mNotificationList) {
2033 final int N = mNotificationList.size();
2034 for (int i=N-1; i>=0; i--) {
2035 NotificationRecord r = mNotificationList.get(i);
2036
Daniel Sandler321e9c52012-10-12 10:59:26 -07002037 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07002038 continue;
2039 }
2040
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002041 if ((r.getFlags() & (Notification.FLAG_ONGOING_EVENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08002044 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 }
2046 }
2047
2048 updateLightsLocked();
2049 }
2050 }
2051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 // lock on mNotificationList
2053 private void updateLightsLocked()
2054 {
The Android Open Source Project10592532009-03-18 17:39:46 -07002055 // handle notification lights
2056 if (mLedNotification == null) {
2057 // get next notification, if any
2058 int n = mLights.size();
2059 if (n > 0) {
2060 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 }
2062 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05002063
Mike Lockwood63b5ad92011-08-30 09:55:30 -04002064 // Don't flash while we are in a call or screen is on
2065 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002066 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07002067 } else {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002068 final Notification ledno = mLedNotification.sbn.notification;
2069 int ledARGB = ledno.ledARGB;
2070 int ledOnMS = ledno.ledOnMS;
2071 int ledOffMS = ledno.ledOffMS;
2072 if ((ledno.defaults & Notification.DEFAULT_LIGHTS) != 0) {
Mike Lockwood670f9322010-01-20 12:13:36 -05002073 ledARGB = mDefaultNotificationColor;
2074 ledOnMS = mDefaultNotificationLedOn;
2075 ledOffMS = mDefaultNotificationLedOff;
2076 }
2077 if (mNotificationPulseEnabled) {
2078 // pulse repeatedly
2079 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
2080 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05002081 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 }
2084
2085 // lock on mNotificationList
Dianne Hackborn41203752012-08-31 14:05:51 -07002086 private int indexOfNotificationLocked(String pkg, String tag, int id, int userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 {
2088 ArrayList<NotificationRecord> list = mNotificationList;
2089 final int len = list.size();
2090 for (int i=0; i<len; i++) {
2091 NotificationRecord r = list.get(i);
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002092 if (!notificationMatchesUserId(r, userId) || r.sbn.id != id) {
Dianne Hackborn41203752012-08-31 14:05:51 -07002093 continue;
2094 }
Fred Quintana6ecaff12009-09-25 14:23:13 -07002095 if (tag == null) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002096 if (r.sbn.tag != null) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07002097 continue;
2098 }
2099 } else {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002100 if (!tag.equals(r.sbn.tag)) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07002101 continue;
2102 }
2103 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002104 if (r.sbn.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 return i;
2106 }
2107 }
2108 return -1;
2109 }
2110
Mike Lockwoodc22404a2009-12-02 11:15:02 -05002111 private void updateNotificationPulse() {
2112 synchronized (mNotificationList) {
2113 updateLightsLocked();
2114 }
2115 }
2116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 // ======================================================================
2118 @Override
2119 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2120 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2121 != PackageManager.PERMISSION_GRANTED) {
2122 pw.println("Permission Denial: can't dump NotificationManager from from pid="
2123 + Binder.getCallingPid()
2124 + ", uid=" + Binder.getCallingUid());
2125 return;
2126 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 pw.println("Current Notification Manager state:");
2129
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002130 pw.println(" Listeners (" + mEnabledListenersForCurrentUser.size()
2131 + ") enabled for current user:");
2132 for (ComponentName cmpt : mEnabledListenersForCurrentUser) {
2133 pw.println(" " + cmpt);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002134 }
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002135
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002136 pw.println(" Live listeners (" + mListeners.size() + "):");
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002137 for (NotificationListenerInfo info : mListeners) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002138 pw.println(" " + info.component
2139 + " (user " + info.userid + "): " + info.listener
2140 + (info.isSystem?" SYSTEM":""));
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002141 }
2142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 int N;
2144
2145 synchronized (mToastQueue) {
2146 N = mToastQueue.size();
2147 if (N > 0) {
2148 pw.println(" Toast Queue:");
2149 for (int i=0; i<N; i++) {
2150 mToastQueue.get(i).dump(pw, " ");
2151 }
2152 pw.println(" ");
2153 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 }
2156
2157 synchronized (mNotificationList) {
2158 N = mNotificationList.size();
2159 if (N > 0) {
2160 pw.println(" Notification List:");
2161 for (int i=0; i<N; i++) {
2162 mNotificationList.get(i).dump(pw, " ", mContext);
2163 }
2164 pw.println(" ");
2165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 N = mLights.size();
2168 if (N > 0) {
2169 pw.println(" Lights List:");
2170 for (int i=0; i<N; i++) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002171 pw.println(" " + mLights.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 }
2173 pw.println(" ");
2174 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 pw.println(" mSoundNotification=" + mSoundNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04002178 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
2179 pw.println(" mSystemReady=" + mSystemReady);
Daniel Sandler5e62e3a2013-04-15 20:57:02 -04002180 pw.println(" mArchive=" + mArchive.toString());
2181 Iterator<StatusBarNotification> iter = mArchive.descendingIterator();
2182 int i=0;
2183 while (iter.hasNext()) {
2184 pw.println(" " + iter.next());
2185 if (++i >= 5) {
2186 if (iter.hasNext()) pw.println(" ...");
2187 break;
2188 }
2189 }
2190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 }
2192 }
2193}