blob: 9b61ec480bd7f2489636e467dea87a2acd66ad9c [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.app.ActivityManagerNative;
24import android.app.IActivityManager;
25import android.app.INotificationManager;
26import android.app.ITransientNotification;
27import android.app.Notification;
28import android.app.PendingIntent;
29import android.app.StatusBarManager;
30import android.content.BroadcastReceiver;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070031import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.Context;
33import android.content.Intent;
34import android.content.IntentFilter;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070035import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.pm.PackageManager;
37import android.content.pm.PackageManager.NameNotFoundException;
38import android.content.res.Resources;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070039import android.database.ContentObserver;
svetoslavganov75986cf2009-05-14 22:28:01 -070040import android.media.AudioManager;
Jeff Sharkey098d5802012-04-26 17:30:34 -070041import android.media.IAudioService;
42import android.media.IRingtonePlayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.IBinder;
47import android.os.Message;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070048import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070049import android.os.RemoteException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070050import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070051import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Vibrator;
53import android.provider.Settings;
John Spurlock7be1a3d2012-08-13 16:45:12 -040054import android.service.dreams.IDreamManager;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050055import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.text.TextUtils;
Dianne Hackborn39606a02012-07-31 17:54:35 -070057import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.util.EventLog;
59import android.util.Log;
Andy Stadler110988c2010-12-03 14:29:16 -080060import android.util.Slog;
Daniel Sandler0da673f2012-04-11 12:33:16 -040061import android.util.Xml;
svetoslavganov75986cf2009-05-14 22:28:01 -070062import android.view.accessibility.AccessibilityEvent;
63import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.widget.Toast;
65
Jeff Sharkey098d5802012-04-26 17:30:34 -070066import com.android.internal.statusbar.StatusBarNotification;
67import com.android.internal.util.FastXmlSerializer;
68
69import org.xmlpull.v1.XmlPullParser;
70import org.xmlpull.v1.XmlPullParserException;
71import org.xmlpull.v1.XmlSerializer;
72
Daniel Sandler0da673f2012-04-11 12:33:16 -040073import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import java.io.FileDescriptor;
Daniel Sandler0da673f2012-04-11 12:33:16 -040075import java.io.FileInputStream;
76import java.io.FileNotFoundException;
77import java.io.FileOutputStream;
78import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import java.io.PrintWriter;
80import java.util.ArrayList;
81import java.util.Arrays;
Daniel Sandler0da673f2012-04-11 12:33:16 -040082import java.util.HashSet;
83
84import libcore.io.IoUtils;
85
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040087/** {@hide} */
88public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089{
90 private static final String TAG = "NotificationService";
91 private static final boolean DBG = false;
92
Joe Onoratobd73d012010-06-04 11:44:54 -070093 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
94
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 // message codes
96 private static final int MESSAGE_TIMEOUT = 2;
97
98 private static final int LONG_DELAY = 3500; // 3.5 seconds
99 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800100
101 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
103 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400104 private static final boolean SCORE_ONGOING_HIGHER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
Daniel Sandler0da673f2012-04-11 12:33:16 -0400106 private static final int JUNK_SCORE = -1000;
107 private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10;
108 private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER;
109
110 private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true;
111 private static final boolean ENABLE_BLOCKED_TOASTS = true;
112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 final Context mContext;
114 final IActivityManager mAm;
115 final IBinder mForegroundToken = new Binder();
116
117 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -0700118 private StatusBarManagerService mStatusBar;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500119 private LightsService.Light mNotificationLight;
120 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121
Mike Lockwood670f9322010-01-20 12:13:36 -0500122 private int mDefaultNotificationColor;
123 private int mDefaultNotificationLedOn;
124 private int mDefaultNotificationLedOff;
125
Joe Onorato30275482009-07-08 17:09:14 -0700126 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400127 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
Jeff Sharkey098d5802012-04-26 17:30:34 -0700129 private NotificationRecord mSoundNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 private NotificationRecord mVibrateNotification;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700131
132 private IAudioService mAudioService;
Jeff Brownc2346132012-04-13 01:55:38 -0700133 private Vibrator mVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500135 // for enabling and disabling notification pulse behavior
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400136 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500137 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500138 private boolean mNotificationPulseEnabled;
139
Fred Quintana6ecaff12009-09-25 14:23:13 -0700140 private final ArrayList<NotificationRecord> mNotificationList =
141 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
143 private ArrayList<ToastRecord> mToastQueue;
144
145 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700147
Daniel Sandler0da673f2012-04-11 12:33:16 -0400148 // Notification control database. For now just contains disabled packages.
149 private AtomicFile mPolicyFile;
150 private HashSet<String> mBlockedPackages = new HashSet<String>();
151
John Spurlock7be1a3d2012-08-13 16:45:12 -0400152 private IDreamManager mSandman;
153
Daniel Sandler0da673f2012-04-11 12:33:16 -0400154 private static final int DB_VERSION = 1;
155
156 private static final String TAG_BODY = "notification-policy";
157 private static final String ATTR_VERSION = "version";
158
159 private static final String TAG_BLOCKED_PKGS = "blocked-packages";
160 private static final String TAG_PACKAGE = "package";
161 private static final String ATTR_NAME = "name";
162
163 private void loadBlockDb() {
164 synchronized(mBlockedPackages) {
165 if (mPolicyFile == null) {
166 File dir = new File("/data/system");
167 mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml"));
168
169 mBlockedPackages.clear();
170
171 FileInputStream infile = null;
172 try {
173 infile = mPolicyFile.openRead();
174 final XmlPullParser parser = Xml.newPullParser();
175 parser.setInput(infile, null);
176
177 int type;
178 String tag;
179 int version = DB_VERSION;
180 while ((type = parser.next()) != END_DOCUMENT) {
181 tag = parser.getName();
182 if (type == START_TAG) {
183 if (TAG_BODY.equals(tag)) {
184 version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
185 } else if (TAG_BLOCKED_PKGS.equals(tag)) {
186 while ((type = parser.next()) != END_DOCUMENT) {
187 tag = parser.getName();
188 if (TAG_PACKAGE.equals(tag)) {
189 mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME));
190 } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) {
191 break;
192 }
193 }
194 }
195 }
196 }
197 } catch (FileNotFoundException e) {
198 // No data yet
199 } catch (IOException e) {
200 Log.wtf(TAG, "Unable to read blocked notifications database", e);
201 } catch (NumberFormatException e) {
202 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
203 } catch (XmlPullParserException e) {
204 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
205 } finally {
206 IoUtils.closeQuietly(infile);
207 }
208 }
209 }
210 }
211
212 private void writeBlockDb() {
213 synchronized(mBlockedPackages) {
214 FileOutputStream outfile = null;
215 try {
216 outfile = mPolicyFile.startWrite();
217
218 XmlSerializer out = new FastXmlSerializer();
219 out.setOutput(outfile, "utf-8");
220
221 out.startDocument(null, true);
222
223 out.startTag(null, TAG_BODY); {
224 out.attribute(null, ATTR_VERSION, String.valueOf(DB_VERSION));
225 out.startTag(null, TAG_BLOCKED_PKGS); {
226 // write all known network policies
227 for (String pkg : mBlockedPackages) {
228 out.startTag(null, TAG_PACKAGE); {
229 out.attribute(null, ATTR_NAME, pkg);
230 } out.endTag(null, TAG_PACKAGE);
231 }
232 } out.endTag(null, TAG_BLOCKED_PKGS);
233 } out.endTag(null, TAG_BODY);
234
235 out.endDocument();
236
237 mPolicyFile.finishWrite(outfile);
238 } catch (IOException e) {
239 if (outfile != null) {
240 mPolicyFile.failWrite(outfile);
241 }
242 }
243 }
244 }
245
246 public boolean areNotificationsEnabledForPackage(String pkg) {
247 checkCallerIsSystem();
248 return areNotificationsEnabledForPackageInt(pkg);
249 }
250
251 // Unchecked. Not exposed via Binder, but can be called in the course of enqueue*().
252 private boolean areNotificationsEnabledForPackageInt(String pkg) {
253 final boolean enabled = !mBlockedPackages.contains(pkg);
254 if (DBG) {
255 Slog.v(TAG, "notifications are " + (enabled?"en":"dis") + "abled for " + pkg);
256 }
257 return enabled;
258 }
259
260 public void setNotificationsEnabledForPackage(String pkg, boolean enabled) {
261 checkCallerIsSystem();
262 if (DBG) {
263 Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);
264 }
265 if (enabled) {
266 mBlockedPackages.remove(pkg);
267 } else {
268 mBlockedPackages.add(pkg);
269
270 // Now, cancel any outstanding notifications that are part of a just-disabled app
271 if (ENABLE_BLOCKED_NOTIFICATIONS) {
272 synchronized (mNotificationList) {
273 final int N = mNotificationList.size();
274 for (int i=0; i<N; i++) {
275 final NotificationRecord r = mNotificationList.get(i);
276 if (r.pkg.equals(pkg)) {
277 cancelNotificationLocked(r, false);
278 }
279 }
280 }
281 }
282 // Don't bother canceling toasts, they'll go away soon enough.
283 }
284 writeBlockDb();
285 }
286
287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 private static String idDebugString(Context baseContext, String packageName, int id) {
289 Context c = null;
290
291 if (packageName != null) {
292 try {
293 c = baseContext.createPackageContext(packageName, 0);
294 } catch (NameNotFoundException e) {
295 c = baseContext;
296 }
297 } else {
298 c = baseContext;
299 }
300
301 String pkg;
302 String type;
303 String name;
304
305 Resources r = c.getResources();
306 try {
307 return r.getResourceName(id);
308 } catch (Resources.NotFoundException e) {
309 return "<name unknown>";
310 }
311 }
312
313 private static final class NotificationRecord
314 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700315 final String pkg;
316 final String tag;
317 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700318 final int uid;
319 final int initialPid;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700320 final Notification notification;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500321 final int score;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 IBinder statusBarKey;
323
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500324 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid, int score, Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 {
326 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700327 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700329 this.uid = uid;
330 this.initialPid = initialPid;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500331 this.score = score;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 this.notification = notification;
333 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 void dump(PrintWriter pw, String prefix, Context baseContext) {
336 pw.println(prefix + this);
337 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
338 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500339 pw.println(prefix + " pri=" + notification.priority);
340 pw.println(prefix + " score=" + this.score);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 pw.println(prefix + " contentIntent=" + notification.contentIntent);
342 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
343 pw.println(prefix + " tickerText=" + notification.tickerText);
344 pw.println(prefix + " contentView=" + notification.contentView);
Amith Yamasani0dedffd2012-03-30 10:47:23 -0700345 pw.println(prefix + " uid=" + uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
347 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
348 pw.println(prefix + " sound=" + notification.sound);
349 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
350 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
351 + " ledOnMS=" + notification.ledOnMS
352 + " ledOffMS=" + notification.ledOffMS);
353 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 @Override
356 public final String toString()
357 {
358 return "NotificationRecord{"
359 + Integer.toHexString(System.identityHashCode(this))
360 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700361 + " id=" + Integer.toHexString(id)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500362 + " tag=" + tag
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500363 + " score=" + score
Daniel Sandlere40451a2011-02-03 14:51:35 -0500364 + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 }
366 }
367
368 private static final class ToastRecord
369 {
370 final int pid;
371 final String pkg;
372 final ITransientNotification callback;
373 int duration;
374
375 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
376 {
377 this.pid = pid;
378 this.pkg = pkg;
379 this.callback = callback;
380 this.duration = duration;
381 }
382
383 void update(int duration) {
384 this.duration = duration;
385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 void dump(PrintWriter pw, String prefix) {
388 pw.println(prefix + this);
389 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 @Override
392 public final String toString()
393 {
394 return "ToastRecord{"
395 + Integer.toHexString(System.identityHashCode(this))
396 + " pkg=" + pkg
397 + " callback=" + callback
398 + " duration=" + duration;
399 }
400 }
401
Joe Onorato089de882010-04-12 08:18:45 -0700402 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
403 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404
405 public void onSetDisabled(int status) {
406 synchronized (mNotificationList) {
407 mDisabledNotifications = status;
408 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
409 // cancel whatever's going on
410 long identity = Binder.clearCallingIdentity();
411 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700412 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
413 if (player != null) {
414 player.stopAsync();
415 }
416 } catch (RemoteException e) {
417 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 Binder.restoreCallingIdentity(identity);
419 }
420
421 identity = Binder.clearCallingIdentity();
422 try {
423 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700424 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 Binder.restoreCallingIdentity(identity);
426 }
427 }
428 }
429 }
430
431 public void onClearAll() {
432 cancelAll();
433 }
434
Fred Quintana6ecaff12009-09-25 14:23:13 -0700435 public void onNotificationClick(String pkg, String tag, int id) {
436 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
jhtop.kim2e448f72011-07-13 17:15:32 +0900437 Notification.FLAG_FOREGROUND_SERVICE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 }
439
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400440 public void onNotificationClear(String pkg, String tag, int id) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800441 cancelNotification(pkg, tag, id, 0,
442 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
443 true);
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400444 }
445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 public void onPanelRevealed() {
447 synchronized (mNotificationList) {
448 // sound
449 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 long identity = Binder.clearCallingIdentity();
452 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700453 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
454 if (player != null) {
455 player.stopAsync();
456 }
457 } catch (RemoteException e) {
458 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 Binder.restoreCallingIdentity(identity);
460 }
461
462 // vibrate
463 mVibrateNotification = null;
464 identity = Binder.clearCallingIdentity();
465 try {
466 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700467 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 Binder.restoreCallingIdentity(identity);
469 }
470
471 // light
472 mLights.clear();
473 mLedNotification = null;
474 updateLightsLocked();
475 }
476 }
Joe Onorato005847b2010-06-04 16:08:02 -0400477
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700478 public void onNotificationError(String pkg, String tag, int id,
479 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400480 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
481 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Joe Onorato46439ce2010-11-19 13:56:21 -0800482 cancelNotification(pkg, tag, id, 0, 0, false);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700483 long ident = Binder.clearCallingIdentity();
484 try {
485 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
486 "Bad notification posted from package " + pkg
487 + ": " + message);
488 } catch (RemoteException e) {
489 }
490 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 };
493
494 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
495 @Override
496 public void onReceive(Context context, Intent intent) {
497 String action = intent.getAction();
498
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800499 boolean queryRestart = false;
Daniel Sandler26ece572012-06-01 15:38:46 -0400500 boolean packageChanged = false;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800501
Mike Lockwood541c9942011-06-12 19:35:45 -0400502 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800503 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandler26ece572012-06-01 15:38:46 -0400504 || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED))
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800505 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800506 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800507 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800508 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800509 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800510 } else if (queryRestart) {
511 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800512 } else {
513 Uri uri = intent.getData();
514 if (uri == null) {
515 return;
516 }
517 String pkgName = uri.getSchemeSpecificPart();
518 if (pkgName == null) {
519 return;
520 }
Daniel Sandler26ece572012-06-01 15:38:46 -0400521 if (packageChanged) {
522 // We cancel notifications for packages which have just been disabled
523 final int enabled = mContext.getPackageManager()
524 .getApplicationEnabledSetting(pkgName);
525 if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
526 || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
527 return;
528 }
529 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800530 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800532 if (pkgList != null && (pkgList.length > 0)) {
533 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800534 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400537 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
538 // Keep track of screen on/off state, but do not turn off the notification light
539 // until user passes through the lock screen or views the notification.
540 mScreenOn = true;
541 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
542 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500543 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400544 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
545 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500546 updateNotificationPulse();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700547 } else if (action.equals(Intent.ACTION_USER_STOPPED)) {
548 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
549 if (userHandle >= 0) {
550 cancelAllNotificationsUser(userHandle);
551 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400552 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
553 // turn off LED when user passes through lock screen
554 mNotificationLight.turnOff();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
556 }
557 };
558
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700559 class SettingsObserver extends ContentObserver {
560 SettingsObserver(Handler handler) {
561 super(handler);
562 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800563
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700564 void observe() {
565 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500566 resolver.registerContentObserver(Settings.System.getUriFor(
567 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700568 update();
569 }
570
571 @Override public void onChange(boolean selfChange) {
572 update();
573 }
574
575 public void update() {
576 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500577 boolean pulseEnabled = Settings.System.getInt(resolver,
578 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
579 if (mNotificationPulseEnabled != pulseEnabled) {
580 mNotificationPulseEnabled = pulseEnabled;
581 updateNotificationPulse();
582 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700583 }
584 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500585
Joe Onorato089de882010-04-12 08:18:45 -0700586 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500587 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 {
589 super();
590 mContext = context;
Jeff Brownc2346132012-04-13 01:55:38 -0700591 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 mAm = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800595
Daniel Sandler0da673f2012-04-11 12:33:16 -0400596 loadBlockDb();
597
Joe Onorato089de882010-04-12 08:18:45 -0700598 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 statusBar.setNotificationCallbacks(mNotificationCallbacks);
600
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500601 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
602 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
603
Mike Lockwood670f9322010-01-20 12:13:36 -0500604 Resources resources = mContext.getResources();
605 mDefaultNotificationColor = resources.getColor(
606 com.android.internal.R.color.config_defaultNotificationColor);
607 mDefaultNotificationLedOn = resources.getInteger(
608 com.android.internal.R.integer.config_defaultNotificationLedOn);
609 mDefaultNotificationLedOff = resources.getInteger(
610 com.android.internal.R.integer.config_defaultNotificationLedOff);
611
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400612 // Don't start allowing notifications until the setup wizard has run once.
613 // After that, including subsequent boots, init with notifications turned on.
614 // This works on the first boot because the setup wizard will toggle this
615 // flag at least once and we'll go back to 0 after that.
616 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
617 Settings.Secure.DEVICE_PROVISIONED, 0)) {
618 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
619 }
620
Mike Lockwood35e16bf2010-11-30 19:53:36 -0500621 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500623 filter.addAction(Intent.ACTION_SCREEN_ON);
624 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500625 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400626 filter.addAction(Intent.ACTION_USER_PRESENT);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700627 filter.addAction(Intent.ACTION_USER_STOPPED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800629 IntentFilter pkgFilter = new IntentFilter();
630 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400631 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800632 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
633 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
634 pkgFilter.addDataScheme("package");
635 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800636 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800637 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800638
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500639 SettingsObserver observer = new SettingsObserver(mHandler);
640 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 }
642
Joe Onorato30275482009-07-08 17:09:14 -0700643 void systemReady() {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700644 mAudioService = IAudioService.Stub.asInterface(
645 ServiceManager.getService(Context.AUDIO_SERVICE));
John Spurlock7be1a3d2012-08-13 16:45:12 -0400646 mSandman = IDreamManager.Stub.asInterface(
647 ServiceManager.getService("dreams"));
Jeff Sharkey098d5802012-04-26 17:30:34 -0700648
Joe Onorato30275482009-07-08 17:09:14 -0700649 // no beeping until we're basically done booting
650 mSystemReady = true;
651 }
652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 // Toasts
654 // ============================================================================
655 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
656 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400657 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658
659 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800660 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 return ;
662 }
663
Daniel Sandler0da673f2012-04-11 12:33:16 -0400664 final boolean isSystemToast = ("android".equals(pkg));
665
666 if (ENABLE_BLOCKED_TOASTS && !isSystemToast && !areNotificationsEnabledForPackageInt(pkg)) {
667 Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
668 return;
669 }
670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 synchronized (mToastQueue) {
672 int callingPid = Binder.getCallingPid();
673 long callingId = Binder.clearCallingIdentity();
674 try {
675 ToastRecord record;
676 int index = indexOfToastLocked(pkg, callback);
677 // If it's already in the queue, we update it in place, we don't
678 // move it to the end of the queue.
679 if (index >= 0) {
680 record = mToastQueue.get(index);
681 record.update(duration);
682 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800683 // Limit the number of toasts that any given package except the android
684 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -0400685 if (!isSystemToast) {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800686 int count = 0;
687 final int N = mToastQueue.size();
688 for (int i=0; i<N; i++) {
689 final ToastRecord r = mToastQueue.get(i);
690 if (r.pkg.equals(pkg)) {
691 count++;
692 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
693 Slog.e(TAG, "Package has already posted " + count
694 + " toasts. Not showing more. Package=" + pkg);
695 return;
696 }
697 }
698 }
699 }
700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 record = new ToastRecord(callingPid, pkg, callback, duration);
702 mToastQueue.add(record);
703 index = mToastQueue.size() - 1;
704 keepProcessAliveLocked(callingPid);
705 }
706 // If it's at index 0, it's the current toast. It doesn't matter if it's
707 // new or just been updated. Call back and tell it to show itself.
708 // If the callback fails, this will remove it from the list, so don't
709 // assume that it's valid after this.
710 if (index == 0) {
711 showNextToastLocked();
712 }
713 } finally {
714 Binder.restoreCallingIdentity(callingId);
715 }
716 }
717 }
718
719 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800720 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721
722 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800723 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 return ;
725 }
726
727 synchronized (mToastQueue) {
728 long callingId = Binder.clearCallingIdentity();
729 try {
730 int index = indexOfToastLocked(pkg, callback);
731 if (index >= 0) {
732 cancelToastLocked(index);
733 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800734 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
736 } finally {
737 Binder.restoreCallingIdentity(callingId);
738 }
739 }
740 }
741
742 private void showNextToastLocked() {
743 ToastRecord record = mToastQueue.get(0);
744 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800745 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 try {
747 record.callback.show();
748 scheduleTimeoutLocked(record, false);
749 return;
750 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800751 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 + " in package " + record.pkg);
753 // remove it from the list and let the process die
754 int index = mToastQueue.indexOf(record);
755 if (index >= 0) {
756 mToastQueue.remove(index);
757 }
758 keepProcessAliveLocked(record.pid);
759 if (mToastQueue.size() > 0) {
760 record = mToastQueue.get(0);
761 } else {
762 record = null;
763 }
764 }
765 }
766 }
767
768 private void cancelToastLocked(int index) {
769 ToastRecord record = mToastQueue.get(index);
770 try {
771 record.callback.hide();
772 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800773 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 + " in package " + record.pkg);
775 // don't worry about this, we're about to remove it from
776 // the list anyway
777 }
778 mToastQueue.remove(index);
779 keepProcessAliveLocked(record.pid);
780 if (mToastQueue.size() > 0) {
781 // Show the next one. If the callback fails, this will remove
782 // it from the list, so don't assume that the list hasn't changed
783 // after this point.
784 showNextToastLocked();
785 }
786 }
787
788 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
789 {
790 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
791 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
792 mHandler.removeCallbacksAndMessages(r);
793 mHandler.sendMessageDelayed(m, delay);
794 }
795
796 private void handleTimeout(ToastRecord record)
797 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800798 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 synchronized (mToastQueue) {
800 int index = indexOfToastLocked(record.pkg, record.callback);
801 if (index >= 0) {
802 cancelToastLocked(index);
803 }
804 }
805 }
806
807 // lock on mToastQueue
808 private int indexOfToastLocked(String pkg, ITransientNotification callback)
809 {
810 IBinder cbak = callback.asBinder();
811 ArrayList<ToastRecord> list = mToastQueue;
812 int len = list.size();
813 for (int i=0; i<len; i++) {
814 ToastRecord r = list.get(i);
815 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
816 return i;
817 }
818 }
819 return -1;
820 }
821
822 // lock on mToastQueue
823 private void keepProcessAliveLocked(int pid)
824 {
825 int toastCount = 0; // toasts from this pid
826 ArrayList<ToastRecord> list = mToastQueue;
827 int N = list.size();
828 for (int i=0; i<N; i++) {
829 ToastRecord r = list.get(i);
830 if (r.pid == pid) {
831 toastCount++;
832 }
833 }
834 try {
835 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
836 } catch (RemoteException e) {
837 // Shouldn't happen.
838 }
839 }
840
841 private final class WorkerHandler extends Handler
842 {
843 @Override
844 public void handleMessage(Message msg)
845 {
846 switch (msg.what)
847 {
848 case MESSAGE_TIMEOUT:
849 handleTimeout((ToastRecord)msg.obj);
850 break;
851 }
852 }
853 }
854
855
856 // Notifications
857 // ============================================================================
Andy Stadler110988c2010-12-03 14:29:16 -0800858 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
860 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700861 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
862 }
863
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400864 public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification,
865 int[] idOut)
Fred Quintana6ecaff12009-09-25 14:23:13 -0700866 {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400867 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
868 tag, id, notification, idOut);
869 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500870
871 private final static int clamp(int x, int low, int high) {
872 return (x < low) ? low : ((x > high) ? high : x);
Daniel Sandlere40451a2011-02-03 14:51:35 -0500873 }
874
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500875
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400876 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
877 // uid/pid of another application)
878 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
879 String tag, int id, Notification notification, int[] idOut)
880 {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400881 if (DBG) {
882 Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
883 }
884 checkCallerIsSystemOrSameApp(pkg);
885 final boolean isSystemNotification = ("android".equals(pkg));
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800886
Joe Onoratobd73d012010-06-04 11:44:54 -0700887 // Limit the number of notifications that any given package except the android
888 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -0400889 if (!isSystemNotification) {
Joe Onoratobd73d012010-06-04 11:44:54 -0700890 synchronized (mNotificationList) {
891 int count = 0;
892 final int N = mNotificationList.size();
893 for (int i=0; i<N; i++) {
894 final NotificationRecord r = mNotificationList.get(i);
895 if (r.pkg.equals(pkg)) {
896 count++;
897 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
898 Slog.e(TAG, "Package has already posted " + count
899 + " notifications. Not showing more. package=" + pkg);
900 return;
901 }
902 }
903 }
904 }
905 }
906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 // This conditional is a dirty hack to limit the logging done on
908 // behalf of the download manager without affecting other apps.
909 if (!pkg.equals("com.android.providers.downloads")
910 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -0500911 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag,
912 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914
915 if (pkg == null || notification == null) {
916 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
917 + " id=" + id + " notification=" + notification);
918 }
919 if (notification.icon != 0) {
920 if (notification.contentView == null) {
921 throw new IllegalArgumentException("contentView required: pkg=" + pkg
922 + " id=" + id + " notification=" + notification);
923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 }
925
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500926 // === Scoring ===
Daniel Sandler0da673f2012-04-11 12:33:16 -0400927
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500928 // 0. Sanitize inputs
929 notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
930 // Migrate notification flags to scores
931 if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
932 if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400933 } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500934 if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
935 }
Daniel Sandler0da673f2012-04-11 12:33:16 -0400936
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500937 // 1. initial score: buckets of 10, around the app
Daniel Sandler0da673f2012-04-11 12:33:16 -0400938 int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500939
Daniel Sandler0da673f2012-04-11 12:33:16 -0400940 // 2. Consult external heuristics (TBD)
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500941
Daniel Sandler0da673f2012-04-11 12:33:16 -0400942 // 3. Apply local rules
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500943
944 // blocked apps
Daniel Sandler0da673f2012-04-11 12:33:16 -0400945 if (ENABLE_BLOCKED_NOTIFICATIONS && !isSystemNotification && !areNotificationsEnabledForPackageInt(pkg)) {
946 score = JUNK_SCORE;
947 Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
948 }
949
950 if (DBG) {
951 Slog.v(TAG, "Assigned score=" + score + " to " + notification);
952 }
953
954 if (score < SCORE_DISPLAY_THRESHOLD) {
955 // Notification will be blocked because the score is too low.
956 return;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500957 }
958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 synchronized (mNotificationList) {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500960 NotificationRecord r = new NotificationRecord(pkg, tag, id,
961 callingUid, callingPid,
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500962 score,
Daniel Sandlere40451a2011-02-03 14:51:35 -0500963 notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 NotificationRecord old = null;
965
Fred Quintana6ecaff12009-09-25 14:23:13 -0700966 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 if (index < 0) {
968 mNotificationList.add(r);
969 } else {
970 old = mNotificationList.remove(index);
971 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700972 // Make sure we don't lose the foreground service state.
973 if (old != null) {
974 notification.flags |=
975 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800978
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700979 // Ensure if this is a foreground service that the proper additional
980 // flags are set.
981 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
982 notification.flags |= Notification.FLAG_ONGOING_EVENT
983 | Notification.FLAG_NO_CLEAR;
984 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800985
John Spurlock7be1a3d2012-08-13 16:45:12 -0400986 // Stop screensaver if the notification has a full-screen intent.
987 // (like an incoming phone call)
988 if (notification.fullScreenIntent != null && mSandman != null) {
989 try {
990 mSandman.awaken();
991 } catch (RemoteException e) {
992 // noop
993 }
994 }
995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 if (notification.icon != 0) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700997 StatusBarNotification n = new StatusBarNotification(pkg, id, tag,
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500998 r.uid, r.initialPid, score, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 if (old != null && old.statusBarKey != null) {
1000 r.statusBarKey = old.statusBarKey;
1001 long identity = Binder.clearCallingIdentity();
1002 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001003 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 }
1005 finally {
1006 Binder.restoreCallingIdentity(identity);
1007 }
1008 } else {
1009 long identity = Binder.clearCallingIdentity();
1010 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001011 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwoodece18ef2012-02-13 20:42:19 -08001012 if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
1013 mAttentionLight.pulse();
1014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 }
1016 finally {
1017 Binder.restoreCallingIdentity(identity);
1018 }
1019 }
Joe Onorato30275482009-07-08 17:09:14 -07001020 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -05001022 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 if (old != null && old.statusBarKey != null) {
1024 long identity = Binder.clearCallingIdentity();
1025 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001026 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 }
1028 finally {
1029 Binder.restoreCallingIdentity(identity);
1030 }
1031 }
1032 }
1033
1034 // If we're not supposed to beep, vibrate, etc. then don't.
1035 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
1036 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -07001037 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
1038 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -08001039
1040 final AudioManager audioManager = (AudioManager) mContext
1041 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 // sound
1043 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001044 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 if (useDefaultSound || notification.sound != null) {
1046 Uri uri;
1047 if (useDefaultSound) {
1048 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
1049 } else {
1050 uri = notification.sound;
1051 }
1052 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
1053 int audioStreamType;
1054 if (notification.audioStreamType >= 0) {
1055 audioStreamType = notification.audioStreamType;
1056 } else {
1057 audioStreamType = DEFAULT_STREAM_TYPE;
1058 }
1059 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -08001060 // do not play notifications if stream volume is 0
1061 // (typically because ringer mode is silent).
1062 if (audioManager.getStreamVolume(audioStreamType) != 0) {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001063 final long identity = Binder.clearCallingIdentity();
Eric Laurent524dc042009-11-27 05:07:55 -08001064 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001065 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1066 if (player != null) {
1067 player.playAsync(uri, looping, audioStreamType);
1068 }
1069 } catch (RemoteException e) {
1070 } finally {
Eric Laurent524dc042009-11-27 05:07:55 -08001071 Binder.restoreCallingIdentity(identity);
1072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 }
1074 }
1075
1076 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001078 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 if ((useDefaultVibrate || notification.vibrate != null)
Eric Laurentbffc3d12012-05-07 17:43:49 -07001080 && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 mVibrateNotification = r;
1082
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001083 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 : notification.vibrate,
1085 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1086 }
1087 }
1088
1089 // this option doesn't shut off the lights
1090
1091 // light
1092 // the most recent thing gets the light
1093 mLights.remove(old);
1094 if (mLedNotification == old) {
1095 mLedNotification = null;
1096 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001097 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
1099 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
1100 mLights.add(r);
1101 updateLightsLocked();
1102 } else {
1103 if (old != null
1104 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
1105 updateLightsLocked();
1106 }
1107 }
1108 }
1109
1110 idOut[0] = id;
1111 }
1112
Joe Onorato30275482009-07-08 17:09:14 -07001113 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001114 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
1115 if (!manager.isEnabled()) {
1116 return;
1117 }
1118
1119 AccessibilityEvent event =
1120 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
1121 event.setPackageName(packageName);
1122 event.setClassName(Notification.class.getName());
1123 event.setParcelableData(notification);
1124 CharSequence tickerText = notification.tickerText;
1125 if (!TextUtils.isEmpty(tickerText)) {
1126 event.getText().add(tickerText);
1127 }
1128
1129 manager.sendAccessibilityEvent(event);
1130 }
1131
Joe Onorato46439ce2010-11-19 13:56:21 -08001132 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
1133 // tell the app
1134 if (sendDelete) {
1135 if (r.notification.deleteIntent != null) {
1136 try {
1137 r.notification.deleteIntent.send();
1138 } catch (PendingIntent.CanceledException ex) {
1139 // do nothing - there's no relevant way to recover, and
1140 // no reason to let this propagate
1141 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
1142 }
1143 }
1144 }
1145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 // status bar
1147 if (r.notification.icon != 0) {
1148 long identity = Binder.clearCallingIdentity();
1149 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001150 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 }
1152 finally {
1153 Binder.restoreCallingIdentity(identity);
1154 }
1155 r.statusBarKey = null;
1156 }
1157
1158 // sound
1159 if (mSoundNotification == r) {
1160 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001161 final long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001163 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1164 if (player != null) {
1165 player.stopAsync();
1166 }
1167 } catch (RemoteException e) {
1168 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 Binder.restoreCallingIdentity(identity);
1170 }
1171 }
1172
1173 // vibrate
1174 if (mVibrateNotification == r) {
1175 mVibrateNotification = null;
1176 long identity = Binder.clearCallingIdentity();
1177 try {
1178 mVibrator.cancel();
1179 }
1180 finally {
1181 Binder.restoreCallingIdentity(identity);
1182 }
1183 }
1184
1185 // light
1186 mLights.remove(r);
1187 if (mLedNotification == r) {
1188 mLedNotification = null;
1189 }
1190 }
1191
1192 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001193 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001194 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 */
Fred Quintana6ecaff12009-09-25 14:23:13 -07001196 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Joe Onorato46439ce2010-11-19 13:56:21 -08001197 int mustNotHaveFlags, boolean sendDelete) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001198 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag,
1199 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200
1201 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001202 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001204 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
1207 return;
1208 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001209 if ((r.notification.flags & mustNotHaveFlags) != 0) {
1210 return;
1211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 mNotificationList.remove(index);
1214
Joe Onorato46439ce2010-11-19 13:56:21 -08001215 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 updateLightsLocked();
1217 }
1218 }
1219 }
1220
1221 /**
1222 * Cancels all notifications from a given package that have all of the
1223 * {@code mustHaveFlags}.
1224 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001225 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
1226 int mustNotHaveFlags, boolean doit) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001227 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags,
1228 mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229
1230 synchronized (mNotificationList) {
1231 final int N = mNotificationList.size();
1232 boolean canceledSomething = false;
1233 for (int i = N-1; i >= 0; --i) {
1234 NotificationRecord r = mNotificationList.get(i);
1235 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
1236 continue;
1237 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001238 if ((r.notification.flags & mustNotHaveFlags) != 0) {
1239 continue;
1240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 if (!r.pkg.equals(pkg)) {
1242 continue;
1243 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001244 canceledSomething = true;
1245 if (!doit) {
1246 return true;
1247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001249 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 }
1251 if (canceledSomething) {
1252 updateLightsLocked();
1253 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001254 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 }
1256 }
1257
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001258 /**
1259 * Cancels all notifications from a given user.
1260 */
1261 boolean cancelAllNotificationsUser(int userHandle) {
1262 synchronized (mNotificationList) {
1263 final int N = mNotificationList.size();
1264 boolean canceledSomething = false;
1265 for (int i = N-1; i >= 0; --i) {
1266 NotificationRecord r = mNotificationList.get(i);
1267 if (UserHandle.getUserId(r.uid) != userHandle) {
1268 continue;
1269 }
1270 canceledSomething = true;
1271 mNotificationList.remove(i);
1272 cancelNotificationLocked(r, false);
1273 }
1274 if (canceledSomething) {
1275 updateLightsLocked();
1276 }
1277 return canceledSomething;
1278 }
1279 }
1280
Andy Stadler110988c2010-12-03 14:29:16 -08001281 @Deprecated
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001282 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001283 cancelNotificationWithTag(pkg, null /* tag */, id);
1284 }
1285
1286 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001287 checkCallerIsSystemOrSameApp(pkg);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001288 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001289 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001290 Binder.getCallingUid() == Process.SYSTEM_UID
Joe Onorato46439ce2010-11-19 13:56:21 -08001291 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 }
1293
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001294 public void cancelAllNotifications(String pkg) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001295 checkCallerIsSystemOrSameApp(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001296
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001297 // Calling from user space, don't allow the canceling of actively
1298 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001299 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 }
1301
Daniel Sandler0da673f2012-04-11 12:33:16 -04001302 void checkCallerIsSystem() {
1303 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001304 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001305 return;
1306 }
1307 throw new SecurityException("Disallowed call for uid " + uid);
1308 }
1309
1310 void checkCallerIsSystemOrSameApp(String pkg) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001311 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001312 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001313 return;
1314 }
1315 try {
1316 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
1317 pkg, 0);
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001318 if (!UserHandle.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001319 throw new SecurityException("Calling uid " + uid + " gave package"
1320 + pkg + " which is owned by uid " + ai.uid);
1321 }
1322 } catch (PackageManager.NameNotFoundException e) {
1323 throw new SecurityException("Unknown package " + pkg);
1324 }
1325 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001326
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001327 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 synchronized (mNotificationList) {
1329 final int N = mNotificationList.size();
1330 for (int i=N-1; i>=0; i--) {
1331 NotificationRecord r = mNotificationList.get(i);
1332
1333 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1334 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001336 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
1338 }
1339
1340 updateLightsLocked();
1341 }
1342 }
1343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 // lock on mNotificationList
1345 private void updateLightsLocked()
1346 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001347 // handle notification lights
1348 if (mLedNotification == null) {
1349 // get next notification, if any
1350 int n = mLights.size();
1351 if (n > 0) {
1352 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 }
1354 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001355
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001356 // Don't flash while we are in a call or screen is on
1357 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001358 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001359 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001360 int ledARGB = mLedNotification.notification.ledARGB;
1361 int ledOnMS = mLedNotification.notification.ledOnMS;
1362 int ledOffMS = mLedNotification.notification.ledOffMS;
1363 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1364 ledARGB = mDefaultNotificationColor;
1365 ledOnMS = mDefaultNotificationLedOn;
1366 ledOffMS = mDefaultNotificationLedOff;
1367 }
1368 if (mNotificationPulseEnabled) {
1369 // pulse repeatedly
1370 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1371 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05001372 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 }
1375
1376 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001377 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 {
1379 ArrayList<NotificationRecord> list = mNotificationList;
1380 final int len = list.size();
1381 for (int i=0; i<len; i++) {
1382 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001383 if (tag == null) {
1384 if (r.tag != null) {
1385 continue;
1386 }
1387 } else {
1388 if (!tag.equals(r.tag)) {
1389 continue;
1390 }
1391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 if (r.id == id && r.pkg.equals(pkg)) {
1393 return i;
1394 }
1395 }
1396 return -1;
1397 }
1398
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001399 private void updateNotificationPulse() {
1400 synchronized (mNotificationList) {
1401 updateLightsLocked();
1402 }
1403 }
1404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 // ======================================================================
1406 @Override
1407 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1408 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1409 != PackageManager.PERMISSION_GRANTED) {
1410 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1411 + Binder.getCallingPid()
1412 + ", uid=" + Binder.getCallingUid());
1413 return;
1414 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 pw.println("Current Notification Manager state:");
1417
1418 int N;
1419
1420 synchronized (mToastQueue) {
1421 N = mToastQueue.size();
1422 if (N > 0) {
1423 pw.println(" Toast Queue:");
1424 for (int i=0; i<N; i++) {
1425 mToastQueue.get(i).dump(pw, " ");
1426 }
1427 pw.println(" ");
1428 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 }
1431
1432 synchronized (mNotificationList) {
1433 N = mNotificationList.size();
1434 if (N > 0) {
1435 pw.println(" Notification List:");
1436 for (int i=0; i<N; i++) {
1437 mNotificationList.get(i).dump(pw, " ", mContext);
1438 }
1439 pw.println(" ");
1440 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 N = mLights.size();
1443 if (N > 0) {
1444 pw.println(" Lights List:");
1445 for (int i=0; i<N; i++) {
1446 mLights.get(i).dump(pw, " ", mContext);
1447 }
1448 pw.println(" ");
1449 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 pw.println(" mSoundNotification=" + mSoundNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001453 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1454 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 }
1456 }
1457}