blob: 3c43352d25cab2ffa64485030ebfb980e232ffad [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;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070033import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.Context;
35import android.content.Intent;
36import android.content.IntentFilter;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070037import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import 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;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070051import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070052import android.os.RemoteException;
Mike Lockwooded760372009-07-09 07:07:27 -040053import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Vibrator;
55import android.provider.Settings;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.util.EventLog;
58import android.util.Log;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.view.accessibility.AccessibilityEvent;
60import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.widget.Toast;
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import java.io.FileDescriptor;
64import java.io.PrintWriter;
65import java.util.ArrayList;
66import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
68class NotificationManagerService extends INotificationManager.Stub
69{
70 private static final String TAG = "NotificationService";
71 private static final boolean DBG = false;
72
73 // message codes
74 private static final int MESSAGE_TIMEOUT = 2;
75
76 private static final int LONG_DELAY = 3500; // 3.5 seconds
77 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -080078
79 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
82
83 final Context mContext;
84 final IActivityManager mAm;
85 final IBinder mForegroundToken = new Binder();
86
87 private WorkerHandler mHandler;
88 private StatusBarService mStatusBarService;
Mike Lockwood3a322132009-11-24 00:30:52 -050089 private LightsService mLightsService;
Mike Lockwood3cb67a32009-11-27 14:25:58 -050090 private LightsService.Light mBatteryLight;
91 private LightsService.Light mNotificationLight;
92 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
Mike Lockwood670f9322010-01-20 12:13:36 -050094 private int mDefaultNotificationColor;
95 private int mDefaultNotificationLedOn;
96 private int mDefaultNotificationLedOff;
97
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 private NotificationRecord mSoundNotification;
99 private AsyncPlayer mSound;
Joe Onorato30275482009-07-08 17:09:14 -0700100 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400101 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
103 private NotificationRecord mVibrateNotification;
104 private Vibrator mVibrator = new Vibrator();
105
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500106 // for enabling and disabling notification pulse behavior
107 private boolean mScreenOn = true;
108 private boolean mNotificationPulseEnabled;
109
110 // for adb connected notifications
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400111 private boolean mUsbConnected;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700112 private boolean mAdbEnabled = false;
113 private boolean mAdbNotificationShown = false;
114 private Notification mAdbNotification;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800115
Fred Quintana6ecaff12009-09-25 14:23:13 -0700116 private final ArrayList<NotificationRecord> mNotificationList =
117 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
119 private ArrayList<ToastRecord> mToastQueue;
120
121 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
122
123 private boolean mBatteryCharging;
124 private boolean mBatteryLow;
125 private boolean mBatteryFull;
126 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700127
The Android Open Source Project10592532009-03-18 17:39:46 -0700128 private static final int BATTERY_LOW_ARGB = 0xFFFF0000; // Charging Low - red solid on
129 private static final int BATTERY_MEDIUM_ARGB = 0xFFFFFF00; // Charging - orange solid on
130 private static final int BATTERY_FULL_ARGB = 0xFF00FF00; // Charging Full - green solid on
131 private static final int BATTERY_BLINK_ON = 125;
132 private static final int BATTERY_BLINK_OFF = 2875;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 private static String idDebugString(Context baseContext, String packageName, int id) {
135 Context c = null;
136
137 if (packageName != null) {
138 try {
139 c = baseContext.createPackageContext(packageName, 0);
140 } catch (NameNotFoundException e) {
141 c = baseContext;
142 }
143 } else {
144 c = baseContext;
145 }
146
147 String pkg;
148 String type;
149 String name;
150
151 Resources r = c.getResources();
152 try {
153 return r.getResourceName(id);
154 } catch (Resources.NotFoundException e) {
155 return "<name unknown>";
156 }
157 }
158
159 private static final class NotificationRecord
160 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700161 final String pkg;
162 final String tag;
163 final int id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 ITransientNotification callback;
165 int duration;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700166 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 IBinder statusBarKey;
168
Fred Quintana6ecaff12009-09-25 14:23:13 -0700169 NotificationRecord(String pkg, String tag, int id, Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 {
171 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700172 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 this.id = id;
174 this.notification = notification;
175 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 void dump(PrintWriter pw, String prefix, Context baseContext) {
178 pw.println(prefix + this);
179 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
180 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
181 pw.println(prefix + " contentIntent=" + notification.contentIntent);
182 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
183 pw.println(prefix + " tickerText=" + notification.tickerText);
184 pw.println(prefix + " contentView=" + notification.contentView);
185 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
186 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
187 pw.println(prefix + " sound=" + notification.sound);
188 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
189 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
190 + " ledOnMS=" + notification.ledOnMS
191 + " ledOffMS=" + notification.ledOffMS);
192 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 @Override
195 public final String toString()
196 {
197 return "NotificationRecord{"
198 + Integer.toHexString(System.identityHashCode(this))
199 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700200 + " id=" + Integer.toHexString(id)
201 + " tag=" + tag + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 }
203 }
204
205 private static final class ToastRecord
206 {
207 final int pid;
208 final String pkg;
209 final ITransientNotification callback;
210 int duration;
211
212 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
213 {
214 this.pid = pid;
215 this.pkg = pkg;
216 this.callback = callback;
217 this.duration = duration;
218 }
219
220 void update(int duration) {
221 this.duration = duration;
222 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 void dump(PrintWriter pw, String prefix) {
225 pw.println(prefix + this);
226 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 @Override
229 public final String toString()
230 {
231 return "ToastRecord{"
232 + Integer.toHexString(System.identityHashCode(this))
233 + " pkg=" + pkg
234 + " callback=" + callback
235 + " duration=" + duration;
236 }
237 }
238
239 private StatusBarService.NotificationCallbacks mNotificationCallbacks
240 = new StatusBarService.NotificationCallbacks() {
241
242 public void onSetDisabled(int status) {
243 synchronized (mNotificationList) {
244 mDisabledNotifications = status;
245 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
246 // cancel whatever's going on
247 long identity = Binder.clearCallingIdentity();
248 try {
249 mSound.stop();
250 }
251 finally {
252 Binder.restoreCallingIdentity(identity);
253 }
254
255 identity = Binder.clearCallingIdentity();
256 try {
257 mVibrator.cancel();
258 }
259 finally {
260 Binder.restoreCallingIdentity(identity);
261 }
262 }
263 }
264 }
265
266 public void onClearAll() {
267 cancelAll();
268 }
269
Fred Quintana6ecaff12009-09-25 14:23:13 -0700270 public void onNotificationClick(String pkg, String tag, int id) {
271 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700272 Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 }
274
275 public void onPanelRevealed() {
276 synchronized (mNotificationList) {
277 // sound
278 mSoundNotification = null;
279 long identity = Binder.clearCallingIdentity();
280 try {
281 mSound.stop();
282 }
283 finally {
284 Binder.restoreCallingIdentity(identity);
285 }
286
287 // vibrate
288 mVibrateNotification = null;
289 identity = Binder.clearCallingIdentity();
290 try {
291 mVibrator.cancel();
292 }
293 finally {
294 Binder.restoreCallingIdentity(identity);
295 }
296
297 // light
298 mLights.clear();
299 mLedNotification = null;
300 updateLightsLocked();
301 }
302 }
303 };
304
305 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
306 @Override
307 public void onReceive(Context context, Intent intent) {
308 String action = intent.getAction();
309
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800310 boolean queryRestart = false;
311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
313 boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0);
314 int level = intent.getIntExtra("level", -1);
315 boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD);
316 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
317 boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90);
318
319 if (batteryCharging != mBatteryCharging ||
320 batteryLow != mBatteryLow ||
321 batteryFull != mBatteryFull) {
322 mBatteryCharging = batteryCharging;
323 mBatteryLow = batteryLow;
324 mBatteryFull = batteryFull;
325 updateLights();
326 }
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400327 } else if (action.equals(Intent.ACTION_UMS_CONNECTED)) {
328 mUsbConnected = true;
329 updateAdbNotification();
330 } else if (action.equals(Intent.ACTION_UMS_DISCONNECTED)) {
331 mUsbConnected = false;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700332 updateAdbNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800334 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800335 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800336 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800337 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800338 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800339 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800340 } else if (queryRestart) {
341 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800342 } else {
343 Uri uri = intent.getData();
344 if (uri == null) {
345 return;
346 }
347 String pkgName = uri.getSchemeSpecificPart();
348 if (pkgName == null) {
349 return;
350 }
351 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800353 if (pkgList != null && (pkgList.length > 0)) {
354 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800355 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500358 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
359 mScreenOn = true;
360 updateNotificationPulse();
361 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
362 mScreenOn = false;
363 updateNotificationPulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 }
365 }
366 };
367
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700368 class SettingsObserver extends ContentObserver {
369 SettingsObserver(Handler handler) {
370 super(handler);
371 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800372
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700373 void observe() {
374 ContentResolver resolver = mContext.getContentResolver();
375 resolver.registerContentObserver(Settings.Secure.getUriFor(
376 Settings.Secure.ADB_ENABLED), false, this);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500377 resolver.registerContentObserver(Settings.System.getUriFor(
378 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700379 update();
380 }
381
382 @Override public void onChange(boolean selfChange) {
383 update();
384 }
385
386 public void update() {
387 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500388 boolean adbEnabled = Settings.Secure.getInt(resolver,
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700389 Settings.Secure.ADB_ENABLED, 0) != 0;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500390 if (mAdbEnabled != adbEnabled) {
391 mAdbEnabled = adbEnabled;
392 updateAdbNotification();
393 }
394 boolean pulseEnabled = Settings.System.getInt(resolver,
395 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
396 if (mNotificationPulseEnabled != pulseEnabled) {
397 mNotificationPulseEnabled = pulseEnabled;
398 updateNotificationPulse();
399 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700400 }
401 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500402
The Android Open Source Project10592532009-03-18 17:39:46 -0700403 NotificationManagerService(Context context, StatusBarService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500404 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 {
406 super();
407 mContext = context;
Mike Lockwood3a322132009-11-24 00:30:52 -0500408 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 mAm = ActivityManagerNative.getDefault();
410 mSound = new AsyncPlayer(TAG);
411 mSound.setUsesWakeLock(context);
412 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 mStatusBarService = statusBar;
416 statusBar.setNotificationCallbacks(mNotificationCallbacks);
417
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500418 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
419 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
420 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
421
Mike Lockwood670f9322010-01-20 12:13:36 -0500422 Resources resources = mContext.getResources();
423 mDefaultNotificationColor = resources.getColor(
424 com.android.internal.R.color.config_defaultNotificationColor);
425 mDefaultNotificationLedOn = resources.getInteger(
426 com.android.internal.R.integer.config_defaultNotificationLedOn);
427 mDefaultNotificationLedOff = resources.getInteger(
428 com.android.internal.R.integer.config_defaultNotificationLedOff);
429
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400430 // Don't start allowing notifications until the setup wizard has run once.
431 // After that, including subsequent boots, init with notifications turned on.
432 // This works on the first boot because the setup wizard will toggle this
433 // flag at least once and we'll go back to 0 after that.
434 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
435 Settings.Secure.DEVICE_PROVISIONED, 0)) {
436 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
437 }
438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 // register for battery changed notifications
440 IntentFilter filter = new IntentFilter();
441 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400442 filter.addAction(Intent.ACTION_UMS_CONNECTED);
443 filter.addAction(Intent.ACTION_UMS_DISCONNECTED);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500444 filter.addAction(Intent.ACTION_SCREEN_ON);
445 filter.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800447 IntentFilter pkgFilter = new IntentFilter();
448 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
449 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
450 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
451 pkgFilter.addDataScheme("package");
452 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800453 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800454 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800455
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500456 SettingsObserver observer = new SettingsObserver(mHandler);
457 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 }
459
Joe Onorato30275482009-07-08 17:09:14 -0700460 void systemReady() {
461 // no beeping until we're basically done booting
462 mSystemReady = true;
463 }
464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 // Toasts
466 // ============================================================================
467 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
468 {
469 Log.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
470
471 if (pkg == null || callback == null) {
472 Log.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
473 return ;
474 }
475
476 synchronized (mToastQueue) {
477 int callingPid = Binder.getCallingPid();
478 long callingId = Binder.clearCallingIdentity();
479 try {
480 ToastRecord record;
481 int index = indexOfToastLocked(pkg, callback);
482 // If it's already in the queue, we update it in place, we don't
483 // move it to the end of the queue.
484 if (index >= 0) {
485 record = mToastQueue.get(index);
486 record.update(duration);
487 } else {
488 record = new ToastRecord(callingPid, pkg, callback, duration);
489 mToastQueue.add(record);
490 index = mToastQueue.size() - 1;
491 keepProcessAliveLocked(callingPid);
492 }
493 // If it's at index 0, it's the current toast. It doesn't matter if it's
494 // new or just been updated. Call back and tell it to show itself.
495 // If the callback fails, this will remove it from the list, so don't
496 // assume that it's valid after this.
497 if (index == 0) {
498 showNextToastLocked();
499 }
500 } finally {
501 Binder.restoreCallingIdentity(callingId);
502 }
503 }
504 }
505
506 public void cancelToast(String pkg, ITransientNotification callback) {
507 Log.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
508
509 if (pkg == null || callback == null) {
510 Log.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
511 return ;
512 }
513
514 synchronized (mToastQueue) {
515 long callingId = Binder.clearCallingIdentity();
516 try {
517 int index = indexOfToastLocked(pkg, callback);
518 if (index >= 0) {
519 cancelToastLocked(index);
520 } else {
521 Log.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
522 }
523 } finally {
524 Binder.restoreCallingIdentity(callingId);
525 }
526 }
527 }
528
529 private void showNextToastLocked() {
530 ToastRecord record = mToastQueue.get(0);
531 while (record != null) {
532 if (DBG) Log.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
533 try {
534 record.callback.show();
535 scheduleTimeoutLocked(record, false);
536 return;
537 } catch (RemoteException e) {
538 Log.w(TAG, "Object died trying to show notification " + record.callback
539 + " in package " + record.pkg);
540 // remove it from the list and let the process die
541 int index = mToastQueue.indexOf(record);
542 if (index >= 0) {
543 mToastQueue.remove(index);
544 }
545 keepProcessAliveLocked(record.pid);
546 if (mToastQueue.size() > 0) {
547 record = mToastQueue.get(0);
548 } else {
549 record = null;
550 }
551 }
552 }
553 }
554
555 private void cancelToastLocked(int index) {
556 ToastRecord record = mToastQueue.get(index);
557 try {
558 record.callback.hide();
559 } catch (RemoteException e) {
560 Log.w(TAG, "Object died trying to hide notification " + record.callback
561 + " in package " + record.pkg);
562 // don't worry about this, we're about to remove it from
563 // the list anyway
564 }
565 mToastQueue.remove(index);
566 keepProcessAliveLocked(record.pid);
567 if (mToastQueue.size() > 0) {
568 // Show the next one. If the callback fails, this will remove
569 // it from the list, so don't assume that the list hasn't changed
570 // after this point.
571 showNextToastLocked();
572 }
573 }
574
575 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
576 {
577 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
578 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
579 mHandler.removeCallbacksAndMessages(r);
580 mHandler.sendMessageDelayed(m, delay);
581 }
582
583 private void handleTimeout(ToastRecord record)
584 {
585 if (DBG) Log.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
586 synchronized (mToastQueue) {
587 int index = indexOfToastLocked(record.pkg, record.callback);
588 if (index >= 0) {
589 cancelToastLocked(index);
590 }
591 }
592 }
593
594 // lock on mToastQueue
595 private int indexOfToastLocked(String pkg, ITransientNotification callback)
596 {
597 IBinder cbak = callback.asBinder();
598 ArrayList<ToastRecord> list = mToastQueue;
599 int len = list.size();
600 for (int i=0; i<len; i++) {
601 ToastRecord r = list.get(i);
602 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
603 return i;
604 }
605 }
606 return -1;
607 }
608
609 // lock on mToastQueue
610 private void keepProcessAliveLocked(int pid)
611 {
612 int toastCount = 0; // toasts from this pid
613 ArrayList<ToastRecord> list = mToastQueue;
614 int N = list.size();
615 for (int i=0; i<N; i++) {
616 ToastRecord r = list.get(i);
617 if (r.pid == pid) {
618 toastCount++;
619 }
620 }
621 try {
622 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
623 } catch (RemoteException e) {
624 // Shouldn't happen.
625 }
626 }
627
628 private final class WorkerHandler extends Handler
629 {
630 @Override
631 public void handleMessage(Message msg)
632 {
633 switch (msg.what)
634 {
635 case MESSAGE_TIMEOUT:
636 handleTimeout((ToastRecord)msg.obj);
637 break;
638 }
639 }
640 }
641
642
643 // Notifications
644 // ============================================================================
645 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
646 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700647 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
648 }
649
650 public void enqueueNotificationWithTag(String pkg, String tag, int id,
651 Notification notification, int[] idOut)
652 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700653 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 // This conditional is a dirty hack to limit the logging done on
656 // behalf of the download manager without affecting other apps.
657 if (!pkg.equals("com.android.providers.downloads")
658 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800659 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 }
661
662 if (pkg == null || notification == null) {
663 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
664 + " id=" + id + " notification=" + notification);
665 }
666 if (notification.icon != 0) {
667 if (notification.contentView == null) {
668 throw new IllegalArgumentException("contentView required: pkg=" + pkg
669 + " id=" + id + " notification=" + notification);
670 }
671 if (notification.contentIntent == null) {
672 throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
673 + " id=" + id + " notification=" + notification);
674 }
675 }
676
677 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700678 NotificationRecord r = new NotificationRecord(pkg, tag, id, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 NotificationRecord old = null;
680
Fred Quintana6ecaff12009-09-25 14:23:13 -0700681 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 if (index < 0) {
683 mNotificationList.add(r);
684 } else {
685 old = mNotificationList.remove(index);
686 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700687 // Make sure we don't lose the foreground service state.
688 if (old != null) {
689 notification.flags |=
690 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800693
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700694 // Ensure if this is a foreground service that the proper additional
695 // flags are set.
696 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
697 notification.flags |= Notification.FLAG_ONGOING_EVENT
698 | Notification.FLAG_NO_CLEAR;
699 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 if (notification.icon != 0) {
702 IconData icon = IconData.makeIcon(null, pkg, notification.icon,
703 notification.iconLevel,
704 notification.number);
705 CharSequence truncatedTicker = notification.tickerText;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 // TODO: make this restriction do something smarter like never fill
708 // more than two screens. "Why would anyone need more than 80 characters." :-/
709 final int maxTickerLen = 80;
710 if (truncatedTicker != null && truncatedTicker.length() > maxTickerLen) {
711 truncatedTicker = truncatedTicker.subSequence(0, maxTickerLen);
712 }
713
714 NotificationData n = new NotificationData();
Fred Quintana6ecaff12009-09-25 14:23:13 -0700715 n.pkg = pkg;
716 n.tag = tag;
717 n.id = id;
718 n.when = notification.when;
719 n.tickerText = truncatedTicker;
720 n.ongoingEvent = (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0;
721 if (!n.ongoingEvent && (notification.flags & Notification.FLAG_NO_CLEAR) == 0) {
722 n.clearable = true;
723 }
724 n.contentView = notification.contentView;
725 n.contentIntent = notification.contentIntent;
726 n.deleteIntent = notification.deleteIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 if (old != null && old.statusBarKey != null) {
728 r.statusBarKey = old.statusBarKey;
729 long identity = Binder.clearCallingIdentity();
730 try {
731 mStatusBarService.updateIcon(r.statusBarKey, icon, n);
732 }
733 finally {
734 Binder.restoreCallingIdentity(identity);
735 }
736 } else {
737 long identity = Binder.clearCallingIdentity();
738 try {
739 r.statusBarKey = mStatusBarService.addIcon(icon, n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500740 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742 finally {
743 Binder.restoreCallingIdentity(identity);
744 }
745 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700746
Joe Onorato30275482009-07-08 17:09:14 -0700747 sendAccessibilityEvent(notification, pkg);
svetoslavganov75986cf2009-05-14 22:28:01 -0700748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 } else {
750 if (old != null && old.statusBarKey != null) {
751 long identity = Binder.clearCallingIdentity();
752 try {
753 mStatusBarService.removeIcon(old.statusBarKey);
754 }
755 finally {
756 Binder.restoreCallingIdentity(identity);
757 }
758 }
759 }
760
761 // If we're not supposed to beep, vibrate, etc. then don't.
762 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
763 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700764 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
765 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800766
767 final AudioManager audioManager = (AudioManager) mContext
768 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 // sound
770 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800771 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 if (useDefaultSound || notification.sound != null) {
773 Uri uri;
774 if (useDefaultSound) {
775 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
776 } else {
777 uri = notification.sound;
778 }
779 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
780 int audioStreamType;
781 if (notification.audioStreamType >= 0) {
782 audioStreamType = notification.audioStreamType;
783 } else {
784 audioStreamType = DEFAULT_STREAM_TYPE;
785 }
786 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800787 // do not play notifications if stream volume is 0
788 // (typically because ringer mode is silent).
789 if (audioManager.getStreamVolume(audioStreamType) != 0) {
790 long identity = Binder.clearCallingIdentity();
791 try {
792 mSound.play(mContext, uri, looping, audioStreamType);
793 }
794 finally {
795 Binder.restoreCallingIdentity(identity);
796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 }
798 }
799
800 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800802 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 if ((useDefaultVibrate || notification.vibrate != null)
804 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
805 mVibrateNotification = r;
806
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800807 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 : notification.vibrate,
809 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
810 }
811 }
812
813 // this option doesn't shut off the lights
814
815 // light
816 // the most recent thing gets the light
817 mLights.remove(old);
818 if (mLedNotification == old) {
819 mLedNotification = null;
820 }
821 //Log.i(TAG, "notification.lights="
822 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
823 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
824 mLights.add(r);
825 updateLightsLocked();
826 } else {
827 if (old != null
828 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
829 updateLightsLocked();
830 }
831 }
832 }
833
834 idOut[0] = id;
835 }
836
Joe Onorato30275482009-07-08 17:09:14 -0700837 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700838 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
839 if (!manager.isEnabled()) {
840 return;
841 }
842
843 AccessibilityEvent event =
844 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
845 event.setPackageName(packageName);
846 event.setClassName(Notification.class.getName());
847 event.setParcelableData(notification);
848 CharSequence tickerText = notification.tickerText;
849 if (!TextUtils.isEmpty(tickerText)) {
850 event.getText().add(tickerText);
851 }
852
853 manager.sendAccessibilityEvent(event);
854 }
855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 private void cancelNotificationLocked(NotificationRecord r) {
857 // status bar
858 if (r.notification.icon != 0) {
859 long identity = Binder.clearCallingIdentity();
860 try {
861 mStatusBarService.removeIcon(r.statusBarKey);
862 }
863 finally {
864 Binder.restoreCallingIdentity(identity);
865 }
866 r.statusBarKey = null;
867 }
868
869 // sound
870 if (mSoundNotification == r) {
871 mSoundNotification = null;
872 long identity = Binder.clearCallingIdentity();
873 try {
874 mSound.stop();
875 }
876 finally {
877 Binder.restoreCallingIdentity(identity);
878 }
879 }
880
881 // vibrate
882 if (mVibrateNotification == r) {
883 mVibrateNotification = null;
884 long identity = Binder.clearCallingIdentity();
885 try {
886 mVibrator.cancel();
887 }
888 finally {
889 Binder.restoreCallingIdentity(identity);
890 }
891 }
892
893 // light
894 mLights.remove(r);
895 if (mLedNotification == r) {
896 mLedNotification = null;
897 }
898 }
899
900 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700901 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800902 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700904 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700905 int mustNotHaveFlags) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800906 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907
908 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700909 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700911 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
914 return;
915 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700916 if ((r.notification.flags & mustNotHaveFlags) != 0) {
917 return;
918 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 mNotificationList.remove(index);
921
922 cancelNotificationLocked(r);
923 updateLightsLocked();
924 }
925 }
926 }
927
928 /**
929 * Cancels all notifications from a given package that have all of the
930 * {@code mustHaveFlags}.
931 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800932 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
933 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800934 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935
936 synchronized (mNotificationList) {
937 final int N = mNotificationList.size();
938 boolean canceledSomething = false;
939 for (int i = N-1; i >= 0; --i) {
940 NotificationRecord r = mNotificationList.get(i);
941 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
942 continue;
943 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700944 if ((r.notification.flags & mustNotHaveFlags) != 0) {
945 continue;
946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 if (!r.pkg.equals(pkg)) {
948 continue;
949 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800950 canceledSomething = true;
951 if (!doit) {
952 return true;
953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 mNotificationList.remove(i);
955 cancelNotificationLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 }
957 if (canceledSomething) {
958 updateLightsLocked();
959 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800960 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 }
962 }
963
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800964
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700965 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700966 cancelNotificationWithTag(pkg, null /* tag */, id);
967 }
968
969 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700970 checkIncomingCall(pkg);
971 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700972 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700973 Binder.getCallingUid() == Process.SYSTEM_UID
974 ? 0 : Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 }
976
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700977 public void cancelAllNotifications(String pkg) {
978 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800979
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700980 // Calling from user space, don't allow the canceling of actively
981 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800982 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
984
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700985 void checkIncomingCall(String pkg) {
986 int uid = Binder.getCallingUid();
987 if (uid == Process.SYSTEM_UID || uid == 0) {
988 return;
989 }
990 try {
991 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
992 pkg, 0);
993 if (ai.uid != uid) {
994 throw new SecurityException("Calling uid " + uid + " gave package"
995 + pkg + " which is owned by uid " + ai.uid);
996 }
997 } catch (PackageManager.NameNotFoundException e) {
998 throw new SecurityException("Unknown package " + pkg);
999 }
1000 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001001
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001002 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 synchronized (mNotificationList) {
1004 final int N = mNotificationList.size();
1005 for (int i=N-1; i>=0; i--) {
1006 NotificationRecord r = mNotificationList.get(i);
1007
1008 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1009 | Notification.FLAG_NO_CLEAR)) == 0) {
1010 if (r.notification.deleteIntent != null) {
1011 try {
1012 r.notification.deleteIntent.send();
1013 } catch (PendingIntent.CanceledException ex) {
1014 // do nothing - there's no relevant way to recover, and
1015 // no reason to let this propagate
1016 Log.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
1017 }
1018 }
1019 mNotificationList.remove(i);
1020 cancelNotificationLocked(r);
1021 }
1022 }
1023
1024 updateLightsLocked();
1025 }
1026 }
1027
1028 private void updateLights() {
1029 synchronized (mNotificationList) {
1030 updateLightsLocked();
1031 }
1032 }
1033
1034 // lock on mNotificationList
1035 private void updateLightsLocked()
1036 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001037 // Battery low always shows, other states only show if charging.
1038 if (mBatteryLow) {
Mike Lockwood445f4302009-09-04 11:06:46 -04001039 if (mBatteryCharging) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001040 mBatteryLight.setColor(BATTERY_LOW_ARGB);
Mike Lockwood445f4302009-09-04 11:06:46 -04001041 } else {
1042 // Flash when battery is low and not charging
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001043 mBatteryLight.setFlashing(BATTERY_LOW_ARGB, LightsService.LIGHT_FLASH_TIMED,
1044 BATTERY_BLINK_ON, BATTERY_BLINK_OFF);
Mike Lockwood445f4302009-09-04 11:06:46 -04001045 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 } else if (mBatteryCharging) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001047 if (mBatteryFull) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001048 mBatteryLight.setColor(BATTERY_FULL_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001050 mBatteryLight.setColor(BATTERY_MEDIUM_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 }
1052 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001053 mBatteryLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055
The Android Open Source Project10592532009-03-18 17:39:46 -07001056 // handle notification lights
1057 if (mLedNotification == null) {
1058 // get next notification, if any
1059 int n = mLights.size();
1060 if (n > 0) {
1061 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 }
1063 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001064
1065 // we only flash if screen is off and persistent pulsing is enabled
Mike Lockwood670f9322010-01-20 12:13:36 -05001066 if (mLedNotification == null || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001067 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001068 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001069 int ledARGB = mLedNotification.notification.ledARGB;
1070 int ledOnMS = mLedNotification.notification.ledOnMS;
1071 int ledOffMS = mLedNotification.notification.ledOffMS;
1072 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1073 ledARGB = mDefaultNotificationColor;
1074 ledOnMS = mDefaultNotificationLedOn;
1075 ledOffMS = mDefaultNotificationLedOff;
1076 }
1077 if (mNotificationPulseEnabled) {
1078 // pulse repeatedly
1079 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1080 ledOnMS, ledOffMS);
1081 } else {
1082 // pulse only once
1083 mNotificationLight.pulse(ledARGB, ledOnMS);
1084 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 }
1087
1088 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001089 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 {
1091 ArrayList<NotificationRecord> list = mNotificationList;
1092 final int len = list.size();
1093 for (int i=0; i<len; i++) {
1094 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001095 if (tag == null) {
1096 if (r.tag != null) {
1097 continue;
1098 }
1099 } else {
1100 if (!tag.equals(r.tag)) {
1101 continue;
1102 }
1103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 if (r.id == id && r.pkg.equals(pkg)) {
1105 return i;
1106 }
1107 }
1108 return -1;
1109 }
1110
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001111 // This is here instead of StatusBarPolicy because it is an important
1112 // security feature that we don't want people customizing the platform
1113 // to accidentally lose.
1114 private void updateAdbNotification() {
Mike Lockwoodea8b7d52009-08-04 17:03:15 -04001115 if (mAdbEnabled && mUsbConnected) {
Mike Lockwooded760372009-07-09 07:07:27 -04001116 if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
1117 return;
1118 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001119 if (!mAdbNotificationShown) {
1120 NotificationManager notificationManager = (NotificationManager) mContext
1121 .getSystemService(Context.NOTIFICATION_SERVICE);
1122 if (notificationManager != null) {
1123 Resources r = mContext.getResources();
1124 CharSequence title = r.getText(
1125 com.android.internal.R.string.adb_active_notification_title);
1126 CharSequence message = r.getText(
1127 com.android.internal.R.string.adb_active_notification_message);
1128
1129 if (mAdbNotification == null) {
1130 mAdbNotification = new Notification();
1131 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_warning;
1132 mAdbNotification.when = 0;
1133 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
1134 mAdbNotification.tickerText = title;
1135 mAdbNotification.defaults |= Notification.DEFAULT_SOUND;
1136 }
1137
1138 Intent intent = new Intent(
1139 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
1140 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1141 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1142 // Note: we are hard-coding the component because this is
1143 // an important security UI that we don't want anyone
1144 // intercepting.
1145 intent.setComponent(new ComponentName("com.android.settings",
1146 "com.android.settings.DevelopmentSettings"));
1147 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
1148 intent, 0);
1149
1150 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001151
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001152 mAdbNotificationShown = true;
1153 notificationManager.notify(
1154 com.android.internal.R.string.adb_active_notification_title,
1155 mAdbNotification);
1156 }
1157 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001158
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001159 } else if (mAdbNotificationShown) {
1160 NotificationManager notificationManager = (NotificationManager) mContext
1161 .getSystemService(Context.NOTIFICATION_SERVICE);
1162 if (notificationManager != null) {
1163 mAdbNotificationShown = false;
1164 notificationManager.cancel(
1165 com.android.internal.R.string.adb_active_notification_title);
1166 }
1167 }
1168 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001169
1170 private void updateNotificationPulse() {
1171 synchronized (mNotificationList) {
1172 updateLightsLocked();
1173 }
1174 }
1175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 // ======================================================================
1177 @Override
1178 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1179 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1180 != PackageManager.PERMISSION_GRANTED) {
1181 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1182 + Binder.getCallingPid()
1183 + ", uid=" + Binder.getCallingUid());
1184 return;
1185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 pw.println("Current Notification Manager state:");
1188
1189 int N;
1190
1191 synchronized (mToastQueue) {
1192 N = mToastQueue.size();
1193 if (N > 0) {
1194 pw.println(" Toast Queue:");
1195 for (int i=0; i<N; i++) {
1196 mToastQueue.get(i).dump(pw, " ");
1197 }
1198 pw.println(" ");
1199 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202
1203 synchronized (mNotificationList) {
1204 N = mNotificationList.size();
1205 if (N > 0) {
1206 pw.println(" Notification List:");
1207 for (int i=0; i<N; i++) {
1208 mNotificationList.get(i).dump(pw, " ", mContext);
1209 }
1210 pw.println(" ");
1211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 N = mLights.size();
1214 if (N > 0) {
1215 pw.println(" Lights List:");
1216 for (int i=0; i<N; i++) {
1217 mLights.get(i).dump(pw, " ", mContext);
1218 }
1219 pw.println(" ");
1220 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 pw.println(" mSoundNotification=" + mSoundNotification);
1223 pw.println(" mSound=" + mSound);
1224 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001225 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1226 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 }
1228 }
1229}