blob: 3657133676348d6ef8e801d943d8cbdcb093de26 [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
310 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
311 boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0);
312 int level = intent.getIntExtra("level", -1);
313 boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD);
314 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
315 boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90);
316
317 if (batteryCharging != mBatteryCharging ||
318 batteryLow != mBatteryLow ||
319 batteryFull != mBatteryFull) {
320 mBatteryCharging = batteryCharging;
321 mBatteryLow = batteryLow;
322 mBatteryFull = batteryFull;
323 updateLights();
324 }
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400325 } else if (action.equals(Intent.ACTION_UMS_CONNECTED)) {
326 mUsbConnected = true;
327 updateAdbNotification();
328 } else if (action.equals(Intent.ACTION_UMS_DISCONNECTED)) {
329 mUsbConnected = false;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700330 updateAdbNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800332 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800333 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800334 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800335 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800336 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
337 } else {
338 Uri uri = intent.getData();
339 if (uri == null) {
340 return;
341 }
342 String pkgName = uri.getSchemeSpecificPart();
343 if (pkgName == null) {
344 return;
345 }
346 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800348 if (pkgList != null && (pkgList.length > 0)) {
349 for (String pkgName : pkgList) {
350 cancelAllNotificationsInt(pkgName, 0, 0);
351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500353 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
354 mScreenOn = true;
355 updateNotificationPulse();
356 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
357 mScreenOn = false;
358 updateNotificationPulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 }
360 }
361 };
362
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700363 class SettingsObserver extends ContentObserver {
364 SettingsObserver(Handler handler) {
365 super(handler);
366 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800367
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700368 void observe() {
369 ContentResolver resolver = mContext.getContentResolver();
370 resolver.registerContentObserver(Settings.Secure.getUriFor(
371 Settings.Secure.ADB_ENABLED), false, this);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500372 resolver.registerContentObserver(Settings.System.getUriFor(
373 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700374 update();
375 }
376
377 @Override public void onChange(boolean selfChange) {
378 update();
379 }
380
381 public void update() {
382 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500383 boolean adbEnabled = Settings.Secure.getInt(resolver,
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700384 Settings.Secure.ADB_ENABLED, 0) != 0;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500385 if (mAdbEnabled != adbEnabled) {
386 mAdbEnabled = adbEnabled;
387 updateAdbNotification();
388 }
389 boolean pulseEnabled = Settings.System.getInt(resolver,
390 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
391 if (mNotificationPulseEnabled != pulseEnabled) {
392 mNotificationPulseEnabled = pulseEnabled;
393 updateNotificationPulse();
394 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700395 }
396 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500397
The Android Open Source Project10592532009-03-18 17:39:46 -0700398 NotificationManagerService(Context context, StatusBarService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500399 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 {
401 super();
402 mContext = context;
Mike Lockwood3a322132009-11-24 00:30:52 -0500403 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 mAm = ActivityManagerNative.getDefault();
405 mSound = new AsyncPlayer(TAG);
406 mSound.setUsesWakeLock(context);
407 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 mStatusBarService = statusBar;
411 statusBar.setNotificationCallbacks(mNotificationCallbacks);
412
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500413 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
414 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
415 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
416
Mike Lockwood670f9322010-01-20 12:13:36 -0500417 Resources resources = mContext.getResources();
418 mDefaultNotificationColor = resources.getColor(
419 com.android.internal.R.color.config_defaultNotificationColor);
420 mDefaultNotificationLedOn = resources.getInteger(
421 com.android.internal.R.integer.config_defaultNotificationLedOn);
422 mDefaultNotificationLedOff = resources.getInteger(
423 com.android.internal.R.integer.config_defaultNotificationLedOff);
424
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400425 // Don't start allowing notifications until the setup wizard has run once.
426 // After that, including subsequent boots, init with notifications turned on.
427 // This works on the first boot because the setup wizard will toggle this
428 // flag at least once and we'll go back to 0 after that.
429 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
430 Settings.Secure.DEVICE_PROVISIONED, 0)) {
431 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
432 }
433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 // register for battery changed notifications
435 IntentFilter filter = new IntentFilter();
436 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400437 filter.addAction(Intent.ACTION_UMS_CONNECTED);
438 filter.addAction(Intent.ACTION_UMS_DISCONNECTED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
440 filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500441 filter.addAction(Intent.ACTION_SCREEN_ON);
442 filter.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 mContext.registerReceiver(mIntentReceiver, filter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800444 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800445 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800446
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500447 SettingsObserver observer = new SettingsObserver(mHandler);
448 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450
Joe Onorato30275482009-07-08 17:09:14 -0700451 void systemReady() {
452 // no beeping until we're basically done booting
453 mSystemReady = true;
454 }
455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 // Toasts
457 // ============================================================================
458 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
459 {
460 Log.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
461
462 if (pkg == null || callback == null) {
463 Log.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
464 return ;
465 }
466
467 synchronized (mToastQueue) {
468 int callingPid = Binder.getCallingPid();
469 long callingId = Binder.clearCallingIdentity();
470 try {
471 ToastRecord record;
472 int index = indexOfToastLocked(pkg, callback);
473 // If it's already in the queue, we update it in place, we don't
474 // move it to the end of the queue.
475 if (index >= 0) {
476 record = mToastQueue.get(index);
477 record.update(duration);
478 } else {
479 record = new ToastRecord(callingPid, pkg, callback, duration);
480 mToastQueue.add(record);
481 index = mToastQueue.size() - 1;
482 keepProcessAliveLocked(callingPid);
483 }
484 // If it's at index 0, it's the current toast. It doesn't matter if it's
485 // new or just been updated. Call back and tell it to show itself.
486 // If the callback fails, this will remove it from the list, so don't
487 // assume that it's valid after this.
488 if (index == 0) {
489 showNextToastLocked();
490 }
491 } finally {
492 Binder.restoreCallingIdentity(callingId);
493 }
494 }
495 }
496
497 public void cancelToast(String pkg, ITransientNotification callback) {
498 Log.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
499
500 if (pkg == null || callback == null) {
501 Log.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
502 return ;
503 }
504
505 synchronized (mToastQueue) {
506 long callingId = Binder.clearCallingIdentity();
507 try {
508 int index = indexOfToastLocked(pkg, callback);
509 if (index >= 0) {
510 cancelToastLocked(index);
511 } else {
512 Log.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
513 }
514 } finally {
515 Binder.restoreCallingIdentity(callingId);
516 }
517 }
518 }
519
520 private void showNextToastLocked() {
521 ToastRecord record = mToastQueue.get(0);
522 while (record != null) {
523 if (DBG) Log.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
524 try {
525 record.callback.show();
526 scheduleTimeoutLocked(record, false);
527 return;
528 } catch (RemoteException e) {
529 Log.w(TAG, "Object died trying to show notification " + record.callback
530 + " in package " + record.pkg);
531 // remove it from the list and let the process die
532 int index = mToastQueue.indexOf(record);
533 if (index >= 0) {
534 mToastQueue.remove(index);
535 }
536 keepProcessAliveLocked(record.pid);
537 if (mToastQueue.size() > 0) {
538 record = mToastQueue.get(0);
539 } else {
540 record = null;
541 }
542 }
543 }
544 }
545
546 private void cancelToastLocked(int index) {
547 ToastRecord record = mToastQueue.get(index);
548 try {
549 record.callback.hide();
550 } catch (RemoteException e) {
551 Log.w(TAG, "Object died trying to hide notification " + record.callback
552 + " in package " + record.pkg);
553 // don't worry about this, we're about to remove it from
554 // the list anyway
555 }
556 mToastQueue.remove(index);
557 keepProcessAliveLocked(record.pid);
558 if (mToastQueue.size() > 0) {
559 // Show the next one. If the callback fails, this will remove
560 // it from the list, so don't assume that the list hasn't changed
561 // after this point.
562 showNextToastLocked();
563 }
564 }
565
566 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
567 {
568 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
569 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
570 mHandler.removeCallbacksAndMessages(r);
571 mHandler.sendMessageDelayed(m, delay);
572 }
573
574 private void handleTimeout(ToastRecord record)
575 {
576 if (DBG) Log.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
577 synchronized (mToastQueue) {
578 int index = indexOfToastLocked(record.pkg, record.callback);
579 if (index >= 0) {
580 cancelToastLocked(index);
581 }
582 }
583 }
584
585 // lock on mToastQueue
586 private int indexOfToastLocked(String pkg, ITransientNotification callback)
587 {
588 IBinder cbak = callback.asBinder();
589 ArrayList<ToastRecord> list = mToastQueue;
590 int len = list.size();
591 for (int i=0; i<len; i++) {
592 ToastRecord r = list.get(i);
593 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
594 return i;
595 }
596 }
597 return -1;
598 }
599
600 // lock on mToastQueue
601 private void keepProcessAliveLocked(int pid)
602 {
603 int toastCount = 0; // toasts from this pid
604 ArrayList<ToastRecord> list = mToastQueue;
605 int N = list.size();
606 for (int i=0; i<N; i++) {
607 ToastRecord r = list.get(i);
608 if (r.pid == pid) {
609 toastCount++;
610 }
611 }
612 try {
613 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
614 } catch (RemoteException e) {
615 // Shouldn't happen.
616 }
617 }
618
619 private final class WorkerHandler extends Handler
620 {
621 @Override
622 public void handleMessage(Message msg)
623 {
624 switch (msg.what)
625 {
626 case MESSAGE_TIMEOUT:
627 handleTimeout((ToastRecord)msg.obj);
628 break;
629 }
630 }
631 }
632
633
634 // Notifications
635 // ============================================================================
636 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
637 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700638 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
639 }
640
641 public void enqueueNotificationWithTag(String pkg, String tag, int id,
642 Notification notification, int[] idOut)
643 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700644 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 // This conditional is a dirty hack to limit the logging done on
647 // behalf of the download manager without affecting other apps.
648 if (!pkg.equals("com.android.providers.downloads")
649 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800650 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 }
652
653 if (pkg == null || notification == null) {
654 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
655 + " id=" + id + " notification=" + notification);
656 }
657 if (notification.icon != 0) {
658 if (notification.contentView == null) {
659 throw new IllegalArgumentException("contentView required: pkg=" + pkg
660 + " id=" + id + " notification=" + notification);
661 }
662 if (notification.contentIntent == null) {
663 throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
664 + " id=" + id + " notification=" + notification);
665 }
666 }
667
668 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700669 NotificationRecord r = new NotificationRecord(pkg, tag, id, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 NotificationRecord old = null;
671
Fred Quintana6ecaff12009-09-25 14:23:13 -0700672 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 if (index < 0) {
674 mNotificationList.add(r);
675 } else {
676 old = mNotificationList.remove(index);
677 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700678 // Make sure we don't lose the foreground service state.
679 if (old != null) {
680 notification.flags |=
681 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800684
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700685 // Ensure if this is a foreground service that the proper additional
686 // flags are set.
687 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
688 notification.flags |= Notification.FLAG_ONGOING_EVENT
689 | Notification.FLAG_NO_CLEAR;
690 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 if (notification.icon != 0) {
693 IconData icon = IconData.makeIcon(null, pkg, notification.icon,
694 notification.iconLevel,
695 notification.number);
696 CharSequence truncatedTicker = notification.tickerText;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 // TODO: make this restriction do something smarter like never fill
699 // more than two screens. "Why would anyone need more than 80 characters." :-/
700 final int maxTickerLen = 80;
701 if (truncatedTicker != null && truncatedTicker.length() > maxTickerLen) {
702 truncatedTicker = truncatedTicker.subSequence(0, maxTickerLen);
703 }
704
705 NotificationData n = new NotificationData();
Fred Quintana6ecaff12009-09-25 14:23:13 -0700706 n.pkg = pkg;
707 n.tag = tag;
708 n.id = id;
709 n.when = notification.when;
710 n.tickerText = truncatedTicker;
711 n.ongoingEvent = (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0;
712 if (!n.ongoingEvent && (notification.flags & Notification.FLAG_NO_CLEAR) == 0) {
713 n.clearable = true;
714 }
715 n.contentView = notification.contentView;
716 n.contentIntent = notification.contentIntent;
717 n.deleteIntent = notification.deleteIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 if (old != null && old.statusBarKey != null) {
719 r.statusBarKey = old.statusBarKey;
720 long identity = Binder.clearCallingIdentity();
721 try {
722 mStatusBarService.updateIcon(r.statusBarKey, icon, n);
723 }
724 finally {
725 Binder.restoreCallingIdentity(identity);
726 }
727 } else {
728 long identity = Binder.clearCallingIdentity();
729 try {
730 r.statusBarKey = mStatusBarService.addIcon(icon, n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500731 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 }
733 finally {
734 Binder.restoreCallingIdentity(identity);
735 }
736 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700737
Joe Onorato30275482009-07-08 17:09:14 -0700738 sendAccessibilityEvent(notification, pkg);
svetoslavganov75986cf2009-05-14 22:28:01 -0700739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 } else {
741 if (old != null && old.statusBarKey != null) {
742 long identity = Binder.clearCallingIdentity();
743 try {
744 mStatusBarService.removeIcon(old.statusBarKey);
745 }
746 finally {
747 Binder.restoreCallingIdentity(identity);
748 }
749 }
750 }
751
752 // If we're not supposed to beep, vibrate, etc. then don't.
753 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
754 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700755 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
756 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800757
758 final AudioManager audioManager = (AudioManager) mContext
759 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 // sound
761 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800762 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 if (useDefaultSound || notification.sound != null) {
764 Uri uri;
765 if (useDefaultSound) {
766 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
767 } else {
768 uri = notification.sound;
769 }
770 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
771 int audioStreamType;
772 if (notification.audioStreamType >= 0) {
773 audioStreamType = notification.audioStreamType;
774 } else {
775 audioStreamType = DEFAULT_STREAM_TYPE;
776 }
777 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800778 // do not play notifications if stream volume is 0
779 // (typically because ringer mode is silent).
780 if (audioManager.getStreamVolume(audioStreamType) != 0) {
781 long identity = Binder.clearCallingIdentity();
782 try {
783 mSound.play(mContext, uri, looping, audioStreamType);
784 }
785 finally {
786 Binder.restoreCallingIdentity(identity);
787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 }
789 }
790
791 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800793 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 if ((useDefaultVibrate || notification.vibrate != null)
795 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
796 mVibrateNotification = r;
797
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800798 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 : notification.vibrate,
800 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
801 }
802 }
803
804 // this option doesn't shut off the lights
805
806 // light
807 // the most recent thing gets the light
808 mLights.remove(old);
809 if (mLedNotification == old) {
810 mLedNotification = null;
811 }
812 //Log.i(TAG, "notification.lights="
813 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
814 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
815 mLights.add(r);
816 updateLightsLocked();
817 } else {
818 if (old != null
819 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
820 updateLightsLocked();
821 }
822 }
823 }
824
825 idOut[0] = id;
826 }
827
Joe Onorato30275482009-07-08 17:09:14 -0700828 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700829 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
830 if (!manager.isEnabled()) {
831 return;
832 }
833
834 AccessibilityEvent event =
835 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
836 event.setPackageName(packageName);
837 event.setClassName(Notification.class.getName());
838 event.setParcelableData(notification);
839 CharSequence tickerText = notification.tickerText;
840 if (!TextUtils.isEmpty(tickerText)) {
841 event.getText().add(tickerText);
842 }
843
844 manager.sendAccessibilityEvent(event);
845 }
846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 private void cancelNotificationLocked(NotificationRecord r) {
848 // status bar
849 if (r.notification.icon != 0) {
850 long identity = Binder.clearCallingIdentity();
851 try {
852 mStatusBarService.removeIcon(r.statusBarKey);
853 }
854 finally {
855 Binder.restoreCallingIdentity(identity);
856 }
857 r.statusBarKey = null;
858 }
859
860 // sound
861 if (mSoundNotification == r) {
862 mSoundNotification = null;
863 long identity = Binder.clearCallingIdentity();
864 try {
865 mSound.stop();
866 }
867 finally {
868 Binder.restoreCallingIdentity(identity);
869 }
870 }
871
872 // vibrate
873 if (mVibrateNotification == r) {
874 mVibrateNotification = null;
875 long identity = Binder.clearCallingIdentity();
876 try {
877 mVibrator.cancel();
878 }
879 finally {
880 Binder.restoreCallingIdentity(identity);
881 }
882 }
883
884 // light
885 mLights.remove(r);
886 if (mLedNotification == r) {
887 mLedNotification = null;
888 }
889 }
890
891 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700892 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800893 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700895 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700896 int mustNotHaveFlags) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800897 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898
899 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700900 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700902 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
905 return;
906 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700907 if ((r.notification.flags & mustNotHaveFlags) != 0) {
908 return;
909 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 mNotificationList.remove(index);
912
913 cancelNotificationLocked(r);
914 updateLightsLocked();
915 }
916 }
917 }
918
919 /**
920 * Cancels all notifications from a given package that have all of the
921 * {@code mustHaveFlags}.
922 */
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700923 void cancelAllNotificationsInt(String pkg, int mustHaveFlags,
924 int mustNotHaveFlags) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800925 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926
927 synchronized (mNotificationList) {
928 final int N = mNotificationList.size();
929 boolean canceledSomething = false;
930 for (int i = N-1; i >= 0; --i) {
931 NotificationRecord r = mNotificationList.get(i);
932 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
933 continue;
934 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700935 if ((r.notification.flags & mustNotHaveFlags) != 0) {
936 continue;
937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 if (!r.pkg.equals(pkg)) {
939 continue;
940 }
941 mNotificationList.remove(i);
942 cancelNotificationLocked(r);
943 canceledSomething = true;
944 }
945 if (canceledSomething) {
946 updateLightsLocked();
947 }
948 }
949 }
950
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800951
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700952 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700953 cancelNotificationWithTag(pkg, null /* tag */, id);
954 }
955
956 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700957 checkIncomingCall(pkg);
958 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700959 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700960 Binder.getCallingUid() == Process.SYSTEM_UID
961 ? 0 : Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
963
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700964 public void cancelAllNotifications(String pkg) {
965 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800966
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700967 // Calling from user space, don't allow the canceling of actively
968 // running foreground services.
969 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 }
971
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700972 void checkIncomingCall(String pkg) {
973 int uid = Binder.getCallingUid();
974 if (uid == Process.SYSTEM_UID || uid == 0) {
975 return;
976 }
977 try {
978 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
979 pkg, 0);
980 if (ai.uid != uid) {
981 throw new SecurityException("Calling uid " + uid + " gave package"
982 + pkg + " which is owned by uid " + ai.uid);
983 }
984 } catch (PackageManager.NameNotFoundException e) {
985 throw new SecurityException("Unknown package " + pkg);
986 }
987 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800988
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700989 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 synchronized (mNotificationList) {
991 final int N = mNotificationList.size();
992 for (int i=N-1; i>=0; i--) {
993 NotificationRecord r = mNotificationList.get(i);
994
995 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
996 | Notification.FLAG_NO_CLEAR)) == 0) {
997 if (r.notification.deleteIntent != null) {
998 try {
999 r.notification.deleteIntent.send();
1000 } catch (PendingIntent.CanceledException ex) {
1001 // do nothing - there's no relevant way to recover, and
1002 // no reason to let this propagate
1003 Log.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
1004 }
1005 }
1006 mNotificationList.remove(i);
1007 cancelNotificationLocked(r);
1008 }
1009 }
1010
1011 updateLightsLocked();
1012 }
1013 }
1014
1015 private void updateLights() {
1016 synchronized (mNotificationList) {
1017 updateLightsLocked();
1018 }
1019 }
1020
1021 // lock on mNotificationList
1022 private void updateLightsLocked()
1023 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001024 // Battery low always shows, other states only show if charging.
1025 if (mBatteryLow) {
Mike Lockwood445f4302009-09-04 11:06:46 -04001026 if (mBatteryCharging) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001027 mBatteryLight.setColor(BATTERY_LOW_ARGB);
Mike Lockwood445f4302009-09-04 11:06:46 -04001028 } else {
1029 // Flash when battery is low and not charging
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001030 mBatteryLight.setFlashing(BATTERY_LOW_ARGB, LightsService.LIGHT_FLASH_TIMED,
1031 BATTERY_BLINK_ON, BATTERY_BLINK_OFF);
Mike Lockwood445f4302009-09-04 11:06:46 -04001032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 } else if (mBatteryCharging) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001034 if (mBatteryFull) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001035 mBatteryLight.setColor(BATTERY_FULL_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001037 mBatteryLight.setColor(BATTERY_MEDIUM_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 }
1039 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001040 mBatteryLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001041 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042
The Android Open Source Project10592532009-03-18 17:39:46 -07001043 // handle notification lights
1044 if (mLedNotification == null) {
1045 // get next notification, if any
1046 int n = mLights.size();
1047 if (n > 0) {
1048 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 }
1050 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001051
1052 // we only flash if screen is off and persistent pulsing is enabled
Mike Lockwood670f9322010-01-20 12:13:36 -05001053 if (mLedNotification == null || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001054 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001055 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001056 int ledARGB = mLedNotification.notification.ledARGB;
1057 int ledOnMS = mLedNotification.notification.ledOnMS;
1058 int ledOffMS = mLedNotification.notification.ledOffMS;
1059 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1060 ledARGB = mDefaultNotificationColor;
1061 ledOnMS = mDefaultNotificationLedOn;
1062 ledOffMS = mDefaultNotificationLedOff;
1063 }
1064 if (mNotificationPulseEnabled) {
1065 // pulse repeatedly
1066 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1067 ledOnMS, ledOffMS);
1068 } else {
1069 // pulse only once
1070 mNotificationLight.pulse(ledARGB, ledOnMS);
1071 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 }
1074
1075 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001076 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 {
1078 ArrayList<NotificationRecord> list = mNotificationList;
1079 final int len = list.size();
1080 for (int i=0; i<len; i++) {
1081 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001082 if (tag == null) {
1083 if (r.tag != null) {
1084 continue;
1085 }
1086 } else {
1087 if (!tag.equals(r.tag)) {
1088 continue;
1089 }
1090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 if (r.id == id && r.pkg.equals(pkg)) {
1092 return i;
1093 }
1094 }
1095 return -1;
1096 }
1097
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001098 // This is here instead of StatusBarPolicy because it is an important
1099 // security feature that we don't want people customizing the platform
1100 // to accidentally lose.
1101 private void updateAdbNotification() {
Mike Lockwoodea8b7d52009-08-04 17:03:15 -04001102 if (mAdbEnabled && mUsbConnected) {
Mike Lockwooded760372009-07-09 07:07:27 -04001103 if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
1104 return;
1105 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001106 if (!mAdbNotificationShown) {
1107 NotificationManager notificationManager = (NotificationManager) mContext
1108 .getSystemService(Context.NOTIFICATION_SERVICE);
1109 if (notificationManager != null) {
1110 Resources r = mContext.getResources();
1111 CharSequence title = r.getText(
1112 com.android.internal.R.string.adb_active_notification_title);
1113 CharSequence message = r.getText(
1114 com.android.internal.R.string.adb_active_notification_message);
1115
1116 if (mAdbNotification == null) {
1117 mAdbNotification = new Notification();
1118 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_warning;
1119 mAdbNotification.when = 0;
1120 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
1121 mAdbNotification.tickerText = title;
1122 mAdbNotification.defaults |= Notification.DEFAULT_SOUND;
1123 }
1124
1125 Intent intent = new Intent(
1126 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
1127 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1128 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1129 // Note: we are hard-coding the component because this is
1130 // an important security UI that we don't want anyone
1131 // intercepting.
1132 intent.setComponent(new ComponentName("com.android.settings",
1133 "com.android.settings.DevelopmentSettings"));
1134 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
1135 intent, 0);
1136
1137 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001138
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001139 mAdbNotificationShown = true;
1140 notificationManager.notify(
1141 com.android.internal.R.string.adb_active_notification_title,
1142 mAdbNotification);
1143 }
1144 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001145
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001146 } else if (mAdbNotificationShown) {
1147 NotificationManager notificationManager = (NotificationManager) mContext
1148 .getSystemService(Context.NOTIFICATION_SERVICE);
1149 if (notificationManager != null) {
1150 mAdbNotificationShown = false;
1151 notificationManager.cancel(
1152 com.android.internal.R.string.adb_active_notification_title);
1153 }
1154 }
1155 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001156
1157 private void updateNotificationPulse() {
1158 synchronized (mNotificationList) {
1159 updateLightsLocked();
1160 }
1161 }
1162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 // ======================================================================
1164 @Override
1165 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1166 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1167 != PackageManager.PERMISSION_GRANTED) {
1168 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1169 + Binder.getCallingPid()
1170 + ", uid=" + Binder.getCallingUid());
1171 return;
1172 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 pw.println("Current Notification Manager state:");
1175
1176 int N;
1177
1178 synchronized (mToastQueue) {
1179 N = mToastQueue.size();
1180 if (N > 0) {
1181 pw.println(" Toast Queue:");
1182 for (int i=0; i<N; i++) {
1183 mToastQueue.get(i).dump(pw, " ");
1184 }
1185 pw.println(" ");
1186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 }
1189
1190 synchronized (mNotificationList) {
1191 N = mNotificationList.size();
1192 if (N > 0) {
1193 pw.println(" Notification List:");
1194 for (int i=0; i<N; i++) {
1195 mNotificationList.get(i).dump(pw, " ", mContext);
1196 }
1197 pw.println(" ");
1198 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 N = mLights.size();
1201 if (N > 0) {
1202 pw.println(" Lights List:");
1203 for (int i=0; i<N; i++) {
1204 mLights.get(i).dump(pw, " ", mContext);
1205 }
1206 pw.println(" ");
1207 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 pw.println(" mSoundNotification=" + mSoundNotification);
1210 pw.println(" mSound=" + mSound);
1211 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001212 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1213 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 }
1215 }
1216}