blob: 854138c7408544c5d9fb0b68b56cb5e1525154c3 [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
svetoslavganov75986cf2009-05-14 22:28:01 -070019import com.android.server.status.IconData;
20import com.android.server.status.NotificationData;
21import com.android.server.status.StatusBarService;
22
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;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070028import android.app.NotificationManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.app.PendingIntent;
30import android.app.StatusBarManager;
31import android.content.BroadcastReceiver;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070032import android.content.ComponentName;
33import android.content.ContentQueryMap;
34import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.Context;
36import android.content.Intent;
37import android.content.IntentFilter;
38import android.content.pm.PackageManager;
39import android.content.pm.PackageManager.NameNotFoundException;
40import android.content.res.Resources;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070041import android.database.ContentObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.media.AsyncPlayer;
svetoslavganov75986cf2009-05-14 22:28:01 -070043import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.net.Uri;
45import android.os.BatteryManager;
46import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.IBinder;
49import android.os.Message;
50import android.os.Power;
svetoslavganov75986cf2009-05-14 22:28:01 -070051import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Vibrator;
53import android.provider.Settings;
svetoslavganov75986cf2009-05-14 22:28:01 -070054import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.util.EventLog;
56import android.util.Log;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.view.accessibility.AccessibilityEvent;
58import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.widget.Toast;
60
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import java.io.FileDescriptor;
62import java.io.PrintWriter;
63import java.util.ArrayList;
64import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
66class NotificationManagerService extends INotificationManager.Stub
67{
68 private static final String TAG = "NotificationService";
69 private static final boolean DBG = false;
70
71 // message codes
72 private static final int MESSAGE_TIMEOUT = 2;
73
74 private static final int LONG_DELAY = 3500; // 3.5 seconds
75 private static final int SHORT_DELAY = 2000; // 2 seconds
76
77 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
78
79 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
80
81 final Context mContext;
82 final IActivityManager mAm;
83 final IBinder mForegroundToken = new Binder();
84
85 private WorkerHandler mHandler;
86 private StatusBarService mStatusBarService;
The Android Open Source Project10592532009-03-18 17:39:46 -070087 private HardwareService mHardware;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
89 private NotificationRecord mSoundNotification;
90 private AsyncPlayer mSound;
91 private int mDisabledNotifications;
92
93 private NotificationRecord mVibrateNotification;
94 private Vibrator mVibrator = new Vibrator();
95
Dianne Hackborn1dac2772009-06-26 18:16:48 -070096 // adb
97 private int mBatteryPlugged;
98 private boolean mAdbEnabled = false;
99 private boolean mAdbNotificationShown = false;
100 private Notification mAdbNotification;
101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 private ArrayList<NotificationRecord> mNotificationList;
103
104 private ArrayList<ToastRecord> mToastQueue;
105
106 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
107
108 private boolean mBatteryCharging;
109 private boolean mBatteryLow;
110 private boolean mBatteryFull;
111 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700112
The Android Open Source Project10592532009-03-18 17:39:46 -0700113 private static final int BATTERY_LOW_ARGB = 0xFFFF0000; // Charging Low - red solid on
114 private static final int BATTERY_MEDIUM_ARGB = 0xFFFFFF00; // Charging - orange solid on
115 private static final int BATTERY_FULL_ARGB = 0xFF00FF00; // Charging Full - green solid on
116 private static final int BATTERY_BLINK_ON = 125;
117 private static final int BATTERY_BLINK_OFF = 2875;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
119 // Tag IDs for EventLog.
120 private static final int EVENT_LOG_ENQUEUE = 2750;
121 private static final int EVENT_LOG_CANCEL = 2751;
122 private static final int EVENT_LOG_CANCEL_ALL = 2752;
123
124 private static String idDebugString(Context baseContext, String packageName, int id) {
125 Context c = null;
126
127 if (packageName != null) {
128 try {
129 c = baseContext.createPackageContext(packageName, 0);
130 } catch (NameNotFoundException e) {
131 c = baseContext;
132 }
133 } else {
134 c = baseContext;
135 }
136
137 String pkg;
138 String type;
139 String name;
140
141 Resources r = c.getResources();
142 try {
143 return r.getResourceName(id);
144 } catch (Resources.NotFoundException e) {
145 return "<name unknown>";
146 }
147 }
148
149 private static final class NotificationRecord
150 {
151 String pkg;
152 int id;
153 ITransientNotification callback;
154 int duration;
155 Notification notification;
156 IBinder statusBarKey;
157
158 NotificationRecord(String pkg, int id, Notification notification)
159 {
160 this.pkg = pkg;
161 this.id = id;
162 this.notification = notification;
163 }
164
165 void dump(PrintWriter pw, String prefix, Context baseContext) {
166 pw.println(prefix + this);
167 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
168 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
169 pw.println(prefix + " contentIntent=" + notification.contentIntent);
170 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
171 pw.println(prefix + " tickerText=" + notification.tickerText);
172 pw.println(prefix + " contentView=" + notification.contentView);
173 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
174 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
175 pw.println(prefix + " sound=" + notification.sound);
176 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
177 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
178 + " ledOnMS=" + notification.ledOnMS
179 + " ledOffMS=" + notification.ledOffMS);
180 }
181
182 @Override
183 public final String toString()
184 {
185 return "NotificationRecord{"
186 + Integer.toHexString(System.identityHashCode(this))
187 + " pkg=" + pkg
188 + " id=" + Integer.toHexString(id) + "}";
189 }
190 }
191
192 private static final class ToastRecord
193 {
194 final int pid;
195 final String pkg;
196 final ITransientNotification callback;
197 int duration;
198
199 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
200 {
201 this.pid = pid;
202 this.pkg = pkg;
203 this.callback = callback;
204 this.duration = duration;
205 }
206
207 void update(int duration) {
208 this.duration = duration;
209 }
210
211 void dump(PrintWriter pw, String prefix) {
212 pw.println(prefix + this);
213 }
214
215 @Override
216 public final String toString()
217 {
218 return "ToastRecord{"
219 + Integer.toHexString(System.identityHashCode(this))
220 + " pkg=" + pkg
221 + " callback=" + callback
222 + " duration=" + duration;
223 }
224 }
225
226 private StatusBarService.NotificationCallbacks mNotificationCallbacks
227 = new StatusBarService.NotificationCallbacks() {
228
229 public void onSetDisabled(int status) {
230 synchronized (mNotificationList) {
231 mDisabledNotifications = status;
232 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
233 // cancel whatever's going on
234 long identity = Binder.clearCallingIdentity();
235 try {
236 mSound.stop();
237 }
238 finally {
239 Binder.restoreCallingIdentity(identity);
240 }
241
242 identity = Binder.clearCallingIdentity();
243 try {
244 mVibrator.cancel();
245 }
246 finally {
247 Binder.restoreCallingIdentity(identity);
248 }
249 }
250 }
251 }
252
253 public void onClearAll() {
254 cancelAll();
255 }
256
257 public void onNotificationClick(String pkg, int id) {
258 cancelNotification(pkg, id, Notification.FLAG_AUTO_CANCEL);
259 }
260
261 public void onPanelRevealed() {
262 synchronized (mNotificationList) {
263 // sound
264 mSoundNotification = null;
265 long identity = Binder.clearCallingIdentity();
266 try {
267 mSound.stop();
268 }
269 finally {
270 Binder.restoreCallingIdentity(identity);
271 }
272
273 // vibrate
274 mVibrateNotification = null;
275 identity = Binder.clearCallingIdentity();
276 try {
277 mVibrator.cancel();
278 }
279 finally {
280 Binder.restoreCallingIdentity(identity);
281 }
282
283 // light
284 mLights.clear();
285 mLedNotification = null;
286 updateLightsLocked();
287 }
288 }
289 };
290
291 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
292 @Override
293 public void onReceive(Context context, Intent intent) {
294 String action = intent.getAction();
295
296 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
297 boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0);
298 int level = intent.getIntExtra("level", -1);
299 boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD);
300 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
301 boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90);
302
303 if (batteryCharging != mBatteryCharging ||
304 batteryLow != mBatteryLow ||
305 batteryFull != mBatteryFull) {
306 mBatteryCharging = batteryCharging;
307 mBatteryLow = batteryLow;
308 mBatteryFull = batteryFull;
309 updateLights();
310 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700311
312 mBatteryPlugged = intent.getIntExtra("plugged", 0);
313 updateAdbNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
315 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)) {
316 Uri uri = intent.getData();
317 if (uri == null) {
318 return;
319 }
320 String pkgName = uri.getSchemeSpecificPart();
321 if (pkgName == null) {
322 return;
323 }
324 cancelAllNotifications(pkgName);
325 }
326 }
327 };
328
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700329 class SettingsObserver extends ContentObserver {
330 SettingsObserver(Handler handler) {
331 super(handler);
332 }
333
334 void observe() {
335 ContentResolver resolver = mContext.getContentResolver();
336 resolver.registerContentObserver(Settings.Secure.getUriFor(
337 Settings.Secure.ADB_ENABLED), false, this);
338 update();
339 }
340
341 @Override public void onChange(boolean selfChange) {
342 update();
343 }
344
345 public void update() {
346 ContentResolver resolver = mContext.getContentResolver();
347 mAdbEnabled = Settings.Secure.getInt(resolver,
348 Settings.Secure.ADB_ENABLED, 0) != 0;
349 updateAdbNotification();
350 }
351 }
352 private final SettingsObserver mSettingsObserver;
353
The Android Open Source Project10592532009-03-18 17:39:46 -0700354 NotificationManagerService(Context context, StatusBarService statusBar,
355 HardwareService hardware)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 {
357 super();
358 mContext = context;
The Android Open Source Project10592532009-03-18 17:39:46 -0700359 mHardware = hardware;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 mAm = ActivityManagerNative.getDefault();
361 mSound = new AsyncPlayer(TAG);
362 mSound.setUsesWakeLock(context);
363 mToastQueue = new ArrayList<ToastRecord>();
364 mNotificationList = new ArrayList<NotificationRecord>();
365 mHandler = new WorkerHandler();
366 mStatusBarService = statusBar;
367 statusBar.setNotificationCallbacks(mNotificationCallbacks);
368
369 // register for battery changed notifications
370 IntentFilter filter = new IntentFilter();
371 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
372 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
373 filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
374 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700375
376 mSettingsObserver = new SettingsObserver(mHandler);
377 mSettingsObserver.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
379
380 // Toasts
381 // ============================================================================
382 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
383 {
384 Log.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
385
386 if (pkg == null || callback == null) {
387 Log.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
388 return ;
389 }
390
391 synchronized (mToastQueue) {
392 int callingPid = Binder.getCallingPid();
393 long callingId = Binder.clearCallingIdentity();
394 try {
395 ToastRecord record;
396 int index = indexOfToastLocked(pkg, callback);
397 // If it's already in the queue, we update it in place, we don't
398 // move it to the end of the queue.
399 if (index >= 0) {
400 record = mToastQueue.get(index);
401 record.update(duration);
402 } else {
403 record = new ToastRecord(callingPid, pkg, callback, duration);
404 mToastQueue.add(record);
405 index = mToastQueue.size() - 1;
406 keepProcessAliveLocked(callingPid);
407 }
408 // If it's at index 0, it's the current toast. It doesn't matter if it's
409 // new or just been updated. Call back and tell it to show itself.
410 // If the callback fails, this will remove it from the list, so don't
411 // assume that it's valid after this.
412 if (index == 0) {
413 showNextToastLocked();
414 }
415 } finally {
416 Binder.restoreCallingIdentity(callingId);
417 }
418 }
419 }
420
421 public void cancelToast(String pkg, ITransientNotification callback) {
422 Log.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
423
424 if (pkg == null || callback == null) {
425 Log.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
426 return ;
427 }
428
429 synchronized (mToastQueue) {
430 long callingId = Binder.clearCallingIdentity();
431 try {
432 int index = indexOfToastLocked(pkg, callback);
433 if (index >= 0) {
434 cancelToastLocked(index);
435 } else {
436 Log.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
437 }
438 } finally {
439 Binder.restoreCallingIdentity(callingId);
440 }
441 }
442 }
443
444 private void showNextToastLocked() {
445 ToastRecord record = mToastQueue.get(0);
446 while (record != null) {
447 if (DBG) Log.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
448 try {
449 record.callback.show();
450 scheduleTimeoutLocked(record, false);
451 return;
452 } catch (RemoteException e) {
453 Log.w(TAG, "Object died trying to show notification " + record.callback
454 + " in package " + record.pkg);
455 // remove it from the list and let the process die
456 int index = mToastQueue.indexOf(record);
457 if (index >= 0) {
458 mToastQueue.remove(index);
459 }
460 keepProcessAliveLocked(record.pid);
461 if (mToastQueue.size() > 0) {
462 record = mToastQueue.get(0);
463 } else {
464 record = null;
465 }
466 }
467 }
468 }
469
470 private void cancelToastLocked(int index) {
471 ToastRecord record = mToastQueue.get(index);
472 try {
473 record.callback.hide();
474 } catch (RemoteException e) {
475 Log.w(TAG, "Object died trying to hide notification " + record.callback
476 + " in package " + record.pkg);
477 // don't worry about this, we're about to remove it from
478 // the list anyway
479 }
480 mToastQueue.remove(index);
481 keepProcessAliveLocked(record.pid);
482 if (mToastQueue.size() > 0) {
483 // Show the next one. If the callback fails, this will remove
484 // it from the list, so don't assume that the list hasn't changed
485 // after this point.
486 showNextToastLocked();
487 }
488 }
489
490 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
491 {
492 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
493 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
494 mHandler.removeCallbacksAndMessages(r);
495 mHandler.sendMessageDelayed(m, delay);
496 }
497
498 private void handleTimeout(ToastRecord record)
499 {
500 if (DBG) Log.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
501 synchronized (mToastQueue) {
502 int index = indexOfToastLocked(record.pkg, record.callback);
503 if (index >= 0) {
504 cancelToastLocked(index);
505 }
506 }
507 }
508
509 // lock on mToastQueue
510 private int indexOfToastLocked(String pkg, ITransientNotification callback)
511 {
512 IBinder cbak = callback.asBinder();
513 ArrayList<ToastRecord> list = mToastQueue;
514 int len = list.size();
515 for (int i=0; i<len; i++) {
516 ToastRecord r = list.get(i);
517 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
518 return i;
519 }
520 }
521 return -1;
522 }
523
524 // lock on mToastQueue
525 private void keepProcessAliveLocked(int pid)
526 {
527 int toastCount = 0; // toasts from this pid
528 ArrayList<ToastRecord> list = mToastQueue;
529 int N = list.size();
530 for (int i=0; i<N; i++) {
531 ToastRecord r = list.get(i);
532 if (r.pid == pid) {
533 toastCount++;
534 }
535 }
536 try {
537 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
538 } catch (RemoteException e) {
539 // Shouldn't happen.
540 }
541 }
542
543 private final class WorkerHandler extends Handler
544 {
545 @Override
546 public void handleMessage(Message msg)
547 {
548 switch (msg.what)
549 {
550 case MESSAGE_TIMEOUT:
551 handleTimeout((ToastRecord)msg.obj);
552 break;
553 }
554 }
555 }
556
557
558 // Notifications
559 // ============================================================================
560 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
561 {
562 // This conditional is a dirty hack to limit the logging done on
563 // behalf of the download manager without affecting other apps.
564 if (!pkg.equals("com.android.providers.downloads")
565 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
566 EventLog.writeEvent(EVENT_LOG_ENQUEUE, pkg, id, notification.toString());
567 }
568
569 if (pkg == null || notification == null) {
570 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
571 + " id=" + id + " notification=" + notification);
572 }
573 if (notification.icon != 0) {
574 if (notification.contentView == null) {
575 throw new IllegalArgumentException("contentView required: pkg=" + pkg
576 + " id=" + id + " notification=" + notification);
577 }
578 if (notification.contentIntent == null) {
579 throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
580 + " id=" + id + " notification=" + notification);
581 }
582 }
583
584 synchronized (mNotificationList) {
585 NotificationRecord r = new NotificationRecord(pkg, id, notification);
586 NotificationRecord old = null;
587
588 int index = indexOfNotificationLocked(pkg, id);
589 if (index < 0) {
590 mNotificationList.add(r);
591 } else {
592 old = mNotificationList.remove(index);
593 mNotificationList.add(index, r);
594 }
595 if (notification.icon != 0) {
596 IconData icon = IconData.makeIcon(null, pkg, notification.icon,
597 notification.iconLevel,
598 notification.number);
599 CharSequence truncatedTicker = notification.tickerText;
600
601 // TODO: make this restriction do something smarter like never fill
602 // more than two screens. "Why would anyone need more than 80 characters." :-/
603 final int maxTickerLen = 80;
604 if (truncatedTicker != null && truncatedTicker.length() > maxTickerLen) {
605 truncatedTicker = truncatedTicker.subSequence(0, maxTickerLen);
606 }
607
608 NotificationData n = new NotificationData();
609 n.id = id;
610 n.pkg = pkg;
611 n.when = notification.when;
612 n.tickerText = truncatedTicker;
613 n.ongoingEvent = (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0;
614 if (!n.ongoingEvent && (notification.flags & Notification.FLAG_NO_CLEAR) == 0) {
615 n.clearable = true;
616 }
617 n.contentView = notification.contentView;
618 n.contentIntent = notification.contentIntent;
619 n.deleteIntent = notification.deleteIntent;
620 if (old != null && old.statusBarKey != null) {
621 r.statusBarKey = old.statusBarKey;
622 long identity = Binder.clearCallingIdentity();
623 try {
624 mStatusBarService.updateIcon(r.statusBarKey, icon, n);
625 }
626 finally {
627 Binder.restoreCallingIdentity(identity);
628 }
629 } else {
630 long identity = Binder.clearCallingIdentity();
631 try {
632 r.statusBarKey = mStatusBarService.addIcon(icon, n);
Joe Onoratoc1e84462009-03-24 19:29:20 -0700633 mHardware.pulseBreathingLight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 }
635 finally {
636 Binder.restoreCallingIdentity(identity);
637 }
638 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700639
640 sendAccessibilityEventTypeNotificationChangedDoCheck(notification, pkg);
641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 } else {
643 if (old != null && old.statusBarKey != null) {
644 long identity = Binder.clearCallingIdentity();
645 try {
646 mStatusBarService.removeIcon(old.statusBarKey);
647 }
648 finally {
649 Binder.restoreCallingIdentity(identity);
650 }
651 }
652 }
653
654 // If we're not supposed to beep, vibrate, etc. then don't.
655 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
656 && (!(old != null
657 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))) {
658 // sound
659 final boolean useDefaultSound =
660 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
661 if (useDefaultSound || notification.sound != null) {
662 Uri uri;
663 if (useDefaultSound) {
664 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
665 } else {
666 uri = notification.sound;
667 }
668 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
669 int audioStreamType;
670 if (notification.audioStreamType >= 0) {
671 audioStreamType = notification.audioStreamType;
672 } else {
673 audioStreamType = DEFAULT_STREAM_TYPE;
674 }
675 mSoundNotification = r;
676 long identity = Binder.clearCallingIdentity();
677 try {
678 mSound.play(mContext, uri, looping, audioStreamType);
679 }
680 finally {
681 Binder.restoreCallingIdentity(identity);
682 }
683 }
684
685 // vibrate
686 final AudioManager audioManager = (AudioManager) mContext
687 .getSystemService(Context.AUDIO_SERVICE);
688 final boolean useDefaultVibrate =
689 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
690 if ((useDefaultVibrate || notification.vibrate != null)
691 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
692 mVibrateNotification = r;
693
694 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
695 : notification.vibrate,
696 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
697 }
698 }
699
700 // this option doesn't shut off the lights
701
702 // light
703 // the most recent thing gets the light
704 mLights.remove(old);
705 if (mLedNotification == old) {
706 mLedNotification = null;
707 }
708 //Log.i(TAG, "notification.lights="
709 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
710 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
711 mLights.add(r);
712 updateLightsLocked();
713 } else {
714 if (old != null
715 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
716 updateLightsLocked();
717 }
718 }
719 }
720
721 idOut[0] = id;
722 }
723
svetoslavganov75986cf2009-05-14 22:28:01 -0700724 private void sendAccessibilityEventTypeNotificationChangedDoCheck(Notification notification,
725 CharSequence packageName) {
726 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
727 if (!manager.isEnabled()) {
728 return;
729 }
730
731 AccessibilityEvent event =
732 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
733 event.setPackageName(packageName);
734 event.setClassName(Notification.class.getName());
735 event.setParcelableData(notification);
736 CharSequence tickerText = notification.tickerText;
737 if (!TextUtils.isEmpty(tickerText)) {
738 event.getText().add(tickerText);
739 }
740
741 manager.sendAccessibilityEvent(event);
742 }
743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 private void cancelNotificationLocked(NotificationRecord r) {
745 // status bar
746 if (r.notification.icon != 0) {
747 long identity = Binder.clearCallingIdentity();
748 try {
749 mStatusBarService.removeIcon(r.statusBarKey);
750 }
751 finally {
752 Binder.restoreCallingIdentity(identity);
753 }
754 r.statusBarKey = null;
755 }
756
757 // sound
758 if (mSoundNotification == r) {
759 mSoundNotification = null;
760 long identity = Binder.clearCallingIdentity();
761 try {
762 mSound.stop();
763 }
764 finally {
765 Binder.restoreCallingIdentity(identity);
766 }
767 }
768
769 // vibrate
770 if (mVibrateNotification == r) {
771 mVibrateNotification = null;
772 long identity = Binder.clearCallingIdentity();
773 try {
774 mVibrator.cancel();
775 }
776 finally {
777 Binder.restoreCallingIdentity(identity);
778 }
779 }
780
781 // light
782 mLights.remove(r);
783 if (mLedNotification == r) {
784 mLedNotification = null;
785 }
786 }
787
788 /**
789 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}.
790 */
791 private void cancelNotification(String pkg, int id, int mustHaveFlags) {
792 EventLog.writeEvent(EVENT_LOG_CANCEL, pkg, id, mustHaveFlags);
793
794 synchronized (mNotificationList) {
795 NotificationRecord r = null;
796
797 int index = indexOfNotificationLocked(pkg, id);
798 if (index >= 0) {
799 r = mNotificationList.get(index);
800
801 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
802 return;
803 }
804
805 mNotificationList.remove(index);
806
807 cancelNotificationLocked(r);
808 updateLightsLocked();
809 }
810 }
811 }
812
813 /**
814 * Cancels all notifications from a given package that have all of the
815 * {@code mustHaveFlags}.
816 */
817 private void cancelAllNotificationsInt(String pkg, int mustHaveFlags) {
818 EventLog.writeEvent(EVENT_LOG_CANCEL_ALL, pkg, mustHaveFlags);
819
820 synchronized (mNotificationList) {
821 final int N = mNotificationList.size();
822 boolean canceledSomething = false;
823 for (int i = N-1; i >= 0; --i) {
824 NotificationRecord r = mNotificationList.get(i);
825 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
826 continue;
827 }
828 if (!r.pkg.equals(pkg)) {
829 continue;
830 }
831 mNotificationList.remove(i);
832 cancelNotificationLocked(r);
833 canceledSomething = true;
834 }
835 if (canceledSomething) {
836 updateLightsLocked();
837 }
838 }
839 }
840
841
842 public void cancelNotification(String pkg, int id)
843 {
844 cancelNotification(pkg, id, 0);
845 }
846
847 public void cancelAllNotifications(String pkg)
848 {
849 cancelAllNotificationsInt(pkg, 0);
850 }
851
852 public void cancelAll() {
853 synchronized (mNotificationList) {
854 final int N = mNotificationList.size();
855 for (int i=N-1; i>=0; i--) {
856 NotificationRecord r = mNotificationList.get(i);
857
858 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
859 | Notification.FLAG_NO_CLEAR)) == 0) {
860 if (r.notification.deleteIntent != null) {
861 try {
862 r.notification.deleteIntent.send();
863 } catch (PendingIntent.CanceledException ex) {
864 // do nothing - there's no relevant way to recover, and
865 // no reason to let this propagate
866 Log.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
867 }
868 }
869 mNotificationList.remove(i);
870 cancelNotificationLocked(r);
871 }
872 }
873
874 updateLightsLocked();
875 }
876 }
877
878 private void updateLights() {
879 synchronized (mNotificationList) {
880 updateLightsLocked();
881 }
882 }
883
884 // lock on mNotificationList
885 private void updateLightsLocked()
886 {
The Android Open Source Project10592532009-03-18 17:39:46 -0700887 // Battery low always shows, other states only show if charging.
888 if (mBatteryLow) {
889 mHardware.setLightFlashing_UNCHECKED(HardwareService.LIGHT_ID_BATTERY, BATTERY_LOW_ARGB,
890 HardwareService.LIGHT_FLASH_TIMED, BATTERY_BLINK_ON, BATTERY_BLINK_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 } else if (mBatteryCharging) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700892 if (mBatteryFull) {
893 mHardware.setLightColor_UNCHECKED(HardwareService.LIGHT_ID_BATTERY,
894 BATTERY_FULL_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -0700896 mHardware.setLightColor_UNCHECKED(HardwareService.LIGHT_ID_BATTERY,
897 BATTERY_MEDIUM_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 }
899 } else {
The Android Open Source Project10592532009-03-18 17:39:46 -0700900 mHardware.setLightOff_UNCHECKED(HardwareService.LIGHT_ID_BATTERY);
901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902
The Android Open Source Project10592532009-03-18 17:39:46 -0700903 // handle notification lights
904 if (mLedNotification == null) {
905 // get next notification, if any
906 int n = mLights.size();
907 if (n > 0) {
908 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 }
910 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700911 if (mLedNotification == null) {
912 mHardware.setLightOff_UNCHECKED(HardwareService.LIGHT_ID_NOTIFICATIONS);
913 } else {
914 mHardware.setLightFlashing_UNCHECKED(
915 HardwareService.LIGHT_ID_NOTIFICATIONS,
916 mLedNotification.notification.ledARGB,
917 HardwareService.LIGHT_FLASH_TIMED,
918 mLedNotification.notification.ledOnMS,
919 mLedNotification.notification.ledOffMS);
920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 }
922
923 // lock on mNotificationList
924 private int indexOfNotificationLocked(String pkg, int id)
925 {
926 ArrayList<NotificationRecord> list = mNotificationList;
927 final int len = list.size();
928 for (int i=0; i<len; i++) {
929 NotificationRecord r = list.get(i);
930 if (r.id == id && r.pkg.equals(pkg)) {
931 return i;
932 }
933 }
934 return -1;
935 }
936
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700937 // This is here instead of StatusBarPolicy because it is an important
938 // security feature that we don't want people customizing the platform
939 // to accidentally lose.
940 private void updateAdbNotification() {
941 if (mAdbEnabled && mBatteryPlugged == BatteryManager.BATTERY_PLUGGED_USB) {
942 if (!mAdbNotificationShown) {
943 NotificationManager notificationManager = (NotificationManager) mContext
944 .getSystemService(Context.NOTIFICATION_SERVICE);
945 if (notificationManager != null) {
946 Resources r = mContext.getResources();
947 CharSequence title = r.getText(
948 com.android.internal.R.string.adb_active_notification_title);
949 CharSequence message = r.getText(
950 com.android.internal.R.string.adb_active_notification_message);
951
952 if (mAdbNotification == null) {
953 mAdbNotification = new Notification();
954 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_warning;
955 mAdbNotification.when = 0;
956 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
957 mAdbNotification.tickerText = title;
958 mAdbNotification.defaults |= Notification.DEFAULT_SOUND;
959 }
960
961 Intent intent = new Intent(
962 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
963 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
964 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
965 // Note: we are hard-coding the component because this is
966 // an important security UI that we don't want anyone
967 // intercepting.
968 intent.setComponent(new ComponentName("com.android.settings",
969 "com.android.settings.DevelopmentSettings"));
970 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
971 intent, 0);
972
973 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
974
975 mAdbNotificationShown = true;
976 notificationManager.notify(
977 com.android.internal.R.string.adb_active_notification_title,
978 mAdbNotification);
979 }
980 }
981
982 } else if (mAdbNotificationShown) {
983 NotificationManager notificationManager = (NotificationManager) mContext
984 .getSystemService(Context.NOTIFICATION_SERVICE);
985 if (notificationManager != null) {
986 mAdbNotificationShown = false;
987 notificationManager.cancel(
988 com.android.internal.R.string.adb_active_notification_title);
989 }
990 }
991 }
992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 // ======================================================================
994 @Override
995 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
996 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
997 != PackageManager.PERMISSION_GRANTED) {
998 pw.println("Permission Denial: can't dump NotificationManager from from pid="
999 + Binder.getCallingPid()
1000 + ", uid=" + Binder.getCallingUid());
1001 return;
1002 }
1003
1004 pw.println("Current Notification Manager state:");
1005
1006 int N;
1007
1008 synchronized (mToastQueue) {
1009 N = mToastQueue.size();
1010 if (N > 0) {
1011 pw.println(" Toast Queue:");
1012 for (int i=0; i<N; i++) {
1013 mToastQueue.get(i).dump(pw, " ");
1014 }
1015 pw.println(" ");
1016 }
1017
1018 }
1019
1020 synchronized (mNotificationList) {
1021 N = mNotificationList.size();
1022 if (N > 0) {
1023 pw.println(" Notification List:");
1024 for (int i=0; i<N; i++) {
1025 mNotificationList.get(i).dump(pw, " ", mContext);
1026 }
1027 pw.println(" ");
1028 }
1029
1030 N = mLights.size();
1031 if (N > 0) {
1032 pw.println(" Lights List:");
1033 for (int i=0; i<N; i++) {
1034 mLights.get(i).dump(pw, " ", mContext);
1035 }
1036 pw.println(" ");
1037 }
1038
1039 pw.println(" mSoundNotification=" + mSoundNotification);
1040 pw.println(" mSound=" + mSound);
1041 pw.println(" mVibrateNotification=" + mVibrateNotification);
1042 }
1043 }
1044}