blob: 78d8c49dfd026a84a3fd9714c3b98ff5926ecb87 [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;
Joe Onorato8a9b2202010-02-26 18:56:32 -080058import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.util.Log;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.view.accessibility.AccessibilityEvent;
61import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.widget.Toast;
63
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import java.io.FileDescriptor;
65import java.io.PrintWriter;
66import java.util.ArrayList;
67import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068
69class NotificationManagerService extends INotificationManager.Stub
70{
71 private static final String TAG = "NotificationService";
72 private static final boolean DBG = false;
73
74 // message codes
75 private static final int MESSAGE_TIMEOUT = 2;
76
77 private static final int LONG_DELAY = 3500; // 3.5 seconds
78 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -080079
80 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
82 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
83
84 final Context mContext;
85 final IActivityManager mAm;
86 final IBinder mForegroundToken = new Binder();
87
88 private WorkerHandler mHandler;
89 private StatusBarService mStatusBarService;
Mike Lockwood3a322132009-11-24 00:30:52 -050090 private LightsService mLightsService;
Mike Lockwood3cb67a32009-11-27 14:25:58 -050091 private LightsService.Light mBatteryLight;
92 private LightsService.Light mNotificationLight;
93 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
Mike Lockwood670f9322010-01-20 12:13:36 -050095 private int mDefaultNotificationColor;
96 private int mDefaultNotificationLedOn;
97 private int mDefaultNotificationLedOff;
98
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 private NotificationRecord mSoundNotification;
100 private AsyncPlayer mSound;
Joe Onorato30275482009-07-08 17:09:14 -0700101 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400102 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
104 private NotificationRecord mVibrateNotification;
105 private Vibrator mVibrator = new Vibrator();
106
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500107 // for enabling and disabling notification pulse behavior
108 private boolean mScreenOn = true;
109 private boolean mNotificationPulseEnabled;
110
111 // for adb connected notifications
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400112 private boolean mUsbConnected;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700113 private boolean mAdbEnabled = false;
114 private boolean mAdbNotificationShown = false;
115 private Notification mAdbNotification;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800116
Fred Quintana6ecaff12009-09-25 14:23:13 -0700117 private final ArrayList<NotificationRecord> mNotificationList =
118 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
120 private ArrayList<ToastRecord> mToastQueue;
121
122 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
123
124 private boolean mBatteryCharging;
125 private boolean mBatteryLow;
126 private boolean mBatteryFull;
127 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700128
The Android Open Source Project10592532009-03-18 17:39:46 -0700129 private static final int BATTERY_LOW_ARGB = 0xFFFF0000; // Charging Low - red solid on
130 private static final int BATTERY_MEDIUM_ARGB = 0xFFFFFF00; // Charging - orange solid on
131 private static final int BATTERY_FULL_ARGB = 0xFF00FF00; // Charging Full - green solid on
132 private static final int BATTERY_BLINK_ON = 125;
133 private static final int BATTERY_BLINK_OFF = 2875;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 private static String idDebugString(Context baseContext, String packageName, int id) {
136 Context c = null;
137
138 if (packageName != null) {
139 try {
140 c = baseContext.createPackageContext(packageName, 0);
141 } catch (NameNotFoundException e) {
142 c = baseContext;
143 }
144 } else {
145 c = baseContext;
146 }
147
148 String pkg;
149 String type;
150 String name;
151
152 Resources r = c.getResources();
153 try {
154 return r.getResourceName(id);
155 } catch (Resources.NotFoundException e) {
156 return "<name unknown>";
157 }
158 }
159
160 private static final class NotificationRecord
161 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700162 final String pkg;
163 final String tag;
164 final int id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 ITransientNotification callback;
166 int duration;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700167 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 IBinder statusBarKey;
169
Fred Quintana6ecaff12009-09-25 14:23:13 -0700170 NotificationRecord(String pkg, String tag, int id, Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 {
172 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700173 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 this.id = id;
175 this.notification = notification;
176 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 void dump(PrintWriter pw, String prefix, Context baseContext) {
179 pw.println(prefix + this);
180 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
181 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
182 pw.println(prefix + " contentIntent=" + notification.contentIntent);
183 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
184 pw.println(prefix + " tickerText=" + notification.tickerText);
185 pw.println(prefix + " contentView=" + notification.contentView);
186 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
187 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
188 pw.println(prefix + " sound=" + notification.sound);
189 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
190 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
191 + " ledOnMS=" + notification.ledOnMS
192 + " ledOffMS=" + notification.ledOffMS);
193 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 @Override
196 public final String toString()
197 {
198 return "NotificationRecord{"
199 + Integer.toHexString(System.identityHashCode(this))
200 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700201 + " id=" + Integer.toHexString(id)
202 + " tag=" + tag + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 }
204 }
205
206 private static final class ToastRecord
207 {
208 final int pid;
209 final String pkg;
210 final ITransientNotification callback;
211 int duration;
212
213 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
214 {
215 this.pid = pid;
216 this.pkg = pkg;
217 this.callback = callback;
218 this.duration = duration;
219 }
220
221 void update(int duration) {
222 this.duration = duration;
223 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 void dump(PrintWriter pw, String prefix) {
226 pw.println(prefix + this);
227 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 @Override
230 public final String toString()
231 {
232 return "ToastRecord{"
233 + Integer.toHexString(System.identityHashCode(this))
234 + " pkg=" + pkg
235 + " callback=" + callback
236 + " duration=" + duration;
237 }
238 }
239
240 private StatusBarService.NotificationCallbacks mNotificationCallbacks
241 = new StatusBarService.NotificationCallbacks() {
242
243 public void onSetDisabled(int status) {
244 synchronized (mNotificationList) {
245 mDisabledNotifications = status;
246 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
247 // cancel whatever's going on
248 long identity = Binder.clearCallingIdentity();
249 try {
250 mSound.stop();
251 }
252 finally {
253 Binder.restoreCallingIdentity(identity);
254 }
255
256 identity = Binder.clearCallingIdentity();
257 try {
258 mVibrator.cancel();
259 }
260 finally {
261 Binder.restoreCallingIdentity(identity);
262 }
263 }
264 }
265 }
266
267 public void onClearAll() {
268 cancelAll();
269 }
270
Fred Quintana6ecaff12009-09-25 14:23:13 -0700271 public void onNotificationClick(String pkg, String tag, int id) {
272 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700273 Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 }
275
276 public void onPanelRevealed() {
277 synchronized (mNotificationList) {
278 // sound
279 mSoundNotification = null;
280 long identity = Binder.clearCallingIdentity();
281 try {
282 mSound.stop();
283 }
284 finally {
285 Binder.restoreCallingIdentity(identity);
286 }
287
288 // vibrate
289 mVibrateNotification = null;
290 identity = Binder.clearCallingIdentity();
291 try {
292 mVibrator.cancel();
293 }
294 finally {
295 Binder.restoreCallingIdentity(identity);
296 }
297
298 // light
299 mLights.clear();
300 mLedNotification = null;
301 updateLightsLocked();
302 }
303 }
304 };
305
306 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
307 @Override
308 public void onReceive(Context context, Intent intent) {
309 String action = intent.getAction();
310
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800311 boolean queryRestart = false;
312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
314 boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0);
315 int level = intent.getIntExtra("level", -1);
316 boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD);
317 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
318 boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90);
319
320 if (batteryCharging != mBatteryCharging ||
321 batteryLow != mBatteryLow ||
322 batteryFull != mBatteryFull) {
323 mBatteryCharging = batteryCharging;
324 mBatteryLow = batteryLow;
325 mBatteryFull = batteryFull;
326 updateLights();
327 }
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400328 } else if (action.equals(Intent.ACTION_UMS_CONNECTED)) {
329 mUsbConnected = true;
330 updateAdbNotification();
331 } else if (action.equals(Intent.ACTION_UMS_DISCONNECTED)) {
332 mUsbConnected = false;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700333 updateAdbNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800335 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800336 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800337 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800338 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800339 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800340 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800341 } else if (queryRestart) {
342 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800343 } else {
344 Uri uri = intent.getData();
345 if (uri == null) {
346 return;
347 }
348 String pkgName = uri.getSchemeSpecificPart();
349 if (pkgName == null) {
350 return;
351 }
352 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800354 if (pkgList != null && (pkgList.length > 0)) {
355 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800356 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500359 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
360 mScreenOn = true;
361 updateNotificationPulse();
362 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
363 mScreenOn = false;
364 updateNotificationPulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 }
366 }
367 };
368
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700369 class SettingsObserver extends ContentObserver {
370 SettingsObserver(Handler handler) {
371 super(handler);
372 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800373
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700374 void observe() {
375 ContentResolver resolver = mContext.getContentResolver();
376 resolver.registerContentObserver(Settings.Secure.getUriFor(
377 Settings.Secure.ADB_ENABLED), false, this);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500378 resolver.registerContentObserver(Settings.System.getUriFor(
379 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700380 update();
381 }
382
383 @Override public void onChange(boolean selfChange) {
384 update();
385 }
386
387 public void update() {
388 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500389 boolean adbEnabled = Settings.Secure.getInt(resolver,
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700390 Settings.Secure.ADB_ENABLED, 0) != 0;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500391 if (mAdbEnabled != adbEnabled) {
392 mAdbEnabled = adbEnabled;
393 updateAdbNotification();
394 }
395 boolean pulseEnabled = Settings.System.getInt(resolver,
396 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
397 if (mNotificationPulseEnabled != pulseEnabled) {
398 mNotificationPulseEnabled = pulseEnabled;
399 updateNotificationPulse();
400 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700401 }
402 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500403
The Android Open Source Project10592532009-03-18 17:39:46 -0700404 NotificationManagerService(Context context, StatusBarService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500405 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 {
407 super();
408 mContext = context;
Mike Lockwood3a322132009-11-24 00:30:52 -0500409 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 mAm = ActivityManagerNative.getDefault();
411 mSound = new AsyncPlayer(TAG);
412 mSound.setUsesWakeLock(context);
413 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 mStatusBarService = statusBar;
417 statusBar.setNotificationCallbacks(mNotificationCallbacks);
418
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500419 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
420 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
421 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
422
Mike Lockwood670f9322010-01-20 12:13:36 -0500423 Resources resources = mContext.getResources();
424 mDefaultNotificationColor = resources.getColor(
425 com.android.internal.R.color.config_defaultNotificationColor);
426 mDefaultNotificationLedOn = resources.getInteger(
427 com.android.internal.R.integer.config_defaultNotificationLedOn);
428 mDefaultNotificationLedOff = resources.getInteger(
429 com.android.internal.R.integer.config_defaultNotificationLedOff);
430
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400431 // Don't start allowing notifications until the setup wizard has run once.
432 // After that, including subsequent boots, init with notifications turned on.
433 // This works on the first boot because the setup wizard will toggle this
434 // flag at least once and we'll go back to 0 after that.
435 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
436 Settings.Secure.DEVICE_PROVISIONED, 0)) {
437 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
438 }
439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 // register for battery changed notifications
441 IntentFilter filter = new IntentFilter();
442 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400443 filter.addAction(Intent.ACTION_UMS_CONNECTED);
444 filter.addAction(Intent.ACTION_UMS_DISCONNECTED);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500445 filter.addAction(Intent.ACTION_SCREEN_ON);
446 filter.addAction(Intent.ACTION_SCREEN_OFF);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800448 IntentFilter pkgFilter = new IntentFilter();
449 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
450 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
451 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
452 pkgFilter.addDataScheme("package");
453 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800454 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800455 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800456
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500457 SettingsObserver observer = new SettingsObserver(mHandler);
458 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 }
460
Joe Onorato30275482009-07-08 17:09:14 -0700461 void systemReady() {
462 // no beeping until we're basically done booting
463 mSystemReady = true;
464 }
465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 // Toasts
467 // ============================================================================
468 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
469 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800470 Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471
472 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800473 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 return ;
475 }
476
477 synchronized (mToastQueue) {
478 int callingPid = Binder.getCallingPid();
479 long callingId = Binder.clearCallingIdentity();
480 try {
481 ToastRecord record;
482 int index = indexOfToastLocked(pkg, callback);
483 // If it's already in the queue, we update it in place, we don't
484 // move it to the end of the queue.
485 if (index >= 0) {
486 record = mToastQueue.get(index);
487 record.update(duration);
488 } else {
489 record = new ToastRecord(callingPid, pkg, callback, duration);
490 mToastQueue.add(record);
491 index = mToastQueue.size() - 1;
492 keepProcessAliveLocked(callingPid);
493 }
494 // If it's at index 0, it's the current toast. It doesn't matter if it's
495 // new or just been updated. Call back and tell it to show itself.
496 // If the callback fails, this will remove it from the list, so don't
497 // assume that it's valid after this.
498 if (index == 0) {
499 showNextToastLocked();
500 }
501 } finally {
502 Binder.restoreCallingIdentity(callingId);
503 }
504 }
505 }
506
507 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800508 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509
510 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800511 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 return ;
513 }
514
515 synchronized (mToastQueue) {
516 long callingId = Binder.clearCallingIdentity();
517 try {
518 int index = indexOfToastLocked(pkg, callback);
519 if (index >= 0) {
520 cancelToastLocked(index);
521 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800522 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 }
524 } finally {
525 Binder.restoreCallingIdentity(callingId);
526 }
527 }
528 }
529
530 private void showNextToastLocked() {
531 ToastRecord record = mToastQueue.get(0);
532 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800533 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 try {
535 record.callback.show();
536 scheduleTimeoutLocked(record, false);
537 return;
538 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800539 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 + " in package " + record.pkg);
541 // remove it from the list and let the process die
542 int index = mToastQueue.indexOf(record);
543 if (index >= 0) {
544 mToastQueue.remove(index);
545 }
546 keepProcessAliveLocked(record.pid);
547 if (mToastQueue.size() > 0) {
548 record = mToastQueue.get(0);
549 } else {
550 record = null;
551 }
552 }
553 }
554 }
555
556 private void cancelToastLocked(int index) {
557 ToastRecord record = mToastQueue.get(index);
558 try {
559 record.callback.hide();
560 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800561 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 + " in package " + record.pkg);
563 // don't worry about this, we're about to remove it from
564 // the list anyway
565 }
566 mToastQueue.remove(index);
567 keepProcessAliveLocked(record.pid);
568 if (mToastQueue.size() > 0) {
569 // Show the next one. If the callback fails, this will remove
570 // it from the list, so don't assume that the list hasn't changed
571 // after this point.
572 showNextToastLocked();
573 }
574 }
575
576 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
577 {
578 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
579 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
580 mHandler.removeCallbacksAndMessages(r);
581 mHandler.sendMessageDelayed(m, delay);
582 }
583
584 private void handleTimeout(ToastRecord record)
585 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800586 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 synchronized (mToastQueue) {
588 int index = indexOfToastLocked(record.pkg, record.callback);
589 if (index >= 0) {
590 cancelToastLocked(index);
591 }
592 }
593 }
594
595 // lock on mToastQueue
596 private int indexOfToastLocked(String pkg, ITransientNotification callback)
597 {
598 IBinder cbak = callback.asBinder();
599 ArrayList<ToastRecord> list = mToastQueue;
600 int len = list.size();
601 for (int i=0; i<len; i++) {
602 ToastRecord r = list.get(i);
603 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
604 return i;
605 }
606 }
607 return -1;
608 }
609
610 // lock on mToastQueue
611 private void keepProcessAliveLocked(int pid)
612 {
613 int toastCount = 0; // toasts from this pid
614 ArrayList<ToastRecord> list = mToastQueue;
615 int N = list.size();
616 for (int i=0; i<N; i++) {
617 ToastRecord r = list.get(i);
618 if (r.pid == pid) {
619 toastCount++;
620 }
621 }
622 try {
623 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
624 } catch (RemoteException e) {
625 // Shouldn't happen.
626 }
627 }
628
629 private final class WorkerHandler extends Handler
630 {
631 @Override
632 public void handleMessage(Message msg)
633 {
634 switch (msg.what)
635 {
636 case MESSAGE_TIMEOUT:
637 handleTimeout((ToastRecord)msg.obj);
638 break;
639 }
640 }
641 }
642
643
644 // Notifications
645 // ============================================================================
646 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
647 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700648 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
649 }
650
651 public void enqueueNotificationWithTag(String pkg, String tag, int id,
652 Notification notification, int[] idOut)
653 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700654 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 // This conditional is a dirty hack to limit the logging done on
657 // behalf of the download manager without affecting other apps.
658 if (!pkg.equals("com.android.providers.downloads")
659 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800660 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 }
662
663 if (pkg == null || notification == null) {
664 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
665 + " id=" + id + " notification=" + notification);
666 }
667 if (notification.icon != 0) {
668 if (notification.contentView == null) {
669 throw new IllegalArgumentException("contentView required: pkg=" + pkg
670 + " id=" + id + " notification=" + notification);
671 }
672 if (notification.contentIntent == null) {
673 throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
674 + " id=" + id + " notification=" + notification);
675 }
676 }
677
678 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700679 NotificationRecord r = new NotificationRecord(pkg, tag, id, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 NotificationRecord old = null;
681
Fred Quintana6ecaff12009-09-25 14:23:13 -0700682 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 if (index < 0) {
684 mNotificationList.add(r);
685 } else {
686 old = mNotificationList.remove(index);
687 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700688 // Make sure we don't lose the foreground service state.
689 if (old != null) {
690 notification.flags |=
691 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800694
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700695 // Ensure if this is a foreground service that the proper additional
696 // flags are set.
697 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
698 notification.flags |= Notification.FLAG_ONGOING_EVENT
699 | Notification.FLAG_NO_CLEAR;
700 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 if (notification.icon != 0) {
703 IconData icon = IconData.makeIcon(null, pkg, notification.icon,
704 notification.iconLevel,
705 notification.number);
706 CharSequence truncatedTicker = notification.tickerText;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 // TODO: make this restriction do something smarter like never fill
709 // more than two screens. "Why would anyone need more than 80 characters." :-/
710 final int maxTickerLen = 80;
711 if (truncatedTicker != null && truncatedTicker.length() > maxTickerLen) {
712 truncatedTicker = truncatedTicker.subSequence(0, maxTickerLen);
713 }
714
715 NotificationData n = new NotificationData();
Fred Quintana6ecaff12009-09-25 14:23:13 -0700716 n.pkg = pkg;
717 n.tag = tag;
718 n.id = id;
719 n.when = notification.when;
720 n.tickerText = truncatedTicker;
721 n.ongoingEvent = (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0;
722 if (!n.ongoingEvent && (notification.flags & Notification.FLAG_NO_CLEAR) == 0) {
723 n.clearable = true;
724 }
725 n.contentView = notification.contentView;
726 n.contentIntent = notification.contentIntent;
727 n.deleteIntent = notification.deleteIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 if (old != null && old.statusBarKey != null) {
729 r.statusBarKey = old.statusBarKey;
730 long identity = Binder.clearCallingIdentity();
731 try {
732 mStatusBarService.updateIcon(r.statusBarKey, icon, n);
733 }
734 finally {
735 Binder.restoreCallingIdentity(identity);
736 }
737 } else {
738 long identity = Binder.clearCallingIdentity();
739 try {
740 r.statusBarKey = mStatusBarService.addIcon(icon, n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500741 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 }
743 finally {
744 Binder.restoreCallingIdentity(identity);
745 }
746 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700747
Joe Onorato30275482009-07-08 17:09:14 -0700748 sendAccessibilityEvent(notification, pkg);
svetoslavganov75986cf2009-05-14 22:28:01 -0700749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 } else {
751 if (old != null && old.statusBarKey != null) {
752 long identity = Binder.clearCallingIdentity();
753 try {
754 mStatusBarService.removeIcon(old.statusBarKey);
755 }
756 finally {
757 Binder.restoreCallingIdentity(identity);
758 }
759 }
760 }
761
762 // If we're not supposed to beep, vibrate, etc. then don't.
763 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
764 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700765 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
766 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800767
768 final AudioManager audioManager = (AudioManager) mContext
769 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 // sound
771 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800772 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 if (useDefaultSound || notification.sound != null) {
774 Uri uri;
775 if (useDefaultSound) {
776 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
777 } else {
778 uri = notification.sound;
779 }
780 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
781 int audioStreamType;
782 if (notification.audioStreamType >= 0) {
783 audioStreamType = notification.audioStreamType;
784 } else {
785 audioStreamType = DEFAULT_STREAM_TYPE;
786 }
787 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800788 // do not play notifications if stream volume is 0
789 // (typically because ringer mode is silent).
790 if (audioManager.getStreamVolume(audioStreamType) != 0) {
791 long identity = Binder.clearCallingIdentity();
792 try {
793 mSound.play(mContext, uri, looping, audioStreamType);
794 }
795 finally {
796 Binder.restoreCallingIdentity(identity);
797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
799 }
800
801 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800803 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 if ((useDefaultVibrate || notification.vibrate != null)
805 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
806 mVibrateNotification = r;
807
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800808 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 : notification.vibrate,
810 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
811 }
812 }
813
814 // this option doesn't shut off the lights
815
816 // light
817 // the most recent thing gets the light
818 mLights.remove(old);
819 if (mLedNotification == old) {
820 mLedNotification = null;
821 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800822 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
824 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
825 mLights.add(r);
826 updateLightsLocked();
827 } else {
828 if (old != null
829 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
830 updateLightsLocked();
831 }
832 }
833 }
834
835 idOut[0] = id;
836 }
837
Joe Onorato30275482009-07-08 17:09:14 -0700838 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700839 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
840 if (!manager.isEnabled()) {
841 return;
842 }
843
844 AccessibilityEvent event =
845 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
846 event.setPackageName(packageName);
847 event.setClassName(Notification.class.getName());
848 event.setParcelableData(notification);
849 CharSequence tickerText = notification.tickerText;
850 if (!TextUtils.isEmpty(tickerText)) {
851 event.getText().add(tickerText);
852 }
853
854 manager.sendAccessibilityEvent(event);
855 }
856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 private void cancelNotificationLocked(NotificationRecord r) {
858 // status bar
859 if (r.notification.icon != 0) {
860 long identity = Binder.clearCallingIdentity();
861 try {
862 mStatusBarService.removeIcon(r.statusBarKey);
863 }
864 finally {
865 Binder.restoreCallingIdentity(identity);
866 }
867 r.statusBarKey = null;
868 }
869
870 // sound
871 if (mSoundNotification == r) {
872 mSoundNotification = null;
873 long identity = Binder.clearCallingIdentity();
874 try {
875 mSound.stop();
876 }
877 finally {
878 Binder.restoreCallingIdentity(identity);
879 }
880 }
881
882 // vibrate
883 if (mVibrateNotification == r) {
884 mVibrateNotification = null;
885 long identity = Binder.clearCallingIdentity();
886 try {
887 mVibrator.cancel();
888 }
889 finally {
890 Binder.restoreCallingIdentity(identity);
891 }
892 }
893
894 // light
895 mLights.remove(r);
896 if (mLedNotification == r) {
897 mLedNotification = null;
898 }
899 }
900
901 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700902 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800903 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700905 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700906 int mustNotHaveFlags) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800907 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908
909 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700910 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700912 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
915 return;
916 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700917 if ((r.notification.flags & mustNotHaveFlags) != 0) {
918 return;
919 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 mNotificationList.remove(index);
922
923 cancelNotificationLocked(r);
924 updateLightsLocked();
925 }
926 }
927 }
928
929 /**
930 * Cancels all notifications from a given package that have all of the
931 * {@code mustHaveFlags}.
932 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800933 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
934 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800935 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936
937 synchronized (mNotificationList) {
938 final int N = mNotificationList.size();
939 boolean canceledSomething = false;
940 for (int i = N-1; i >= 0; --i) {
941 NotificationRecord r = mNotificationList.get(i);
942 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
943 continue;
944 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700945 if ((r.notification.flags & mustNotHaveFlags) != 0) {
946 continue;
947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 if (!r.pkg.equals(pkg)) {
949 continue;
950 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800951 canceledSomething = true;
952 if (!doit) {
953 return true;
954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 mNotificationList.remove(i);
956 cancelNotificationLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 }
958 if (canceledSomething) {
959 updateLightsLocked();
960 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800961 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
963 }
964
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800965
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700966 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700967 cancelNotificationWithTag(pkg, null /* tag */, id);
968 }
969
970 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700971 checkIncomingCall(pkg);
972 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700973 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700974 Binder.getCallingUid() == Process.SYSTEM_UID
975 ? 0 : Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 }
977
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700978 public void cancelAllNotifications(String pkg) {
979 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800980
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700981 // Calling from user space, don't allow the canceling of actively
982 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800983 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
985
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700986 void checkIncomingCall(String pkg) {
987 int uid = Binder.getCallingUid();
988 if (uid == Process.SYSTEM_UID || uid == 0) {
989 return;
990 }
991 try {
992 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
993 pkg, 0);
994 if (ai.uid != uid) {
995 throw new SecurityException("Calling uid " + uid + " gave package"
996 + pkg + " which is owned by uid " + ai.uid);
997 }
998 } catch (PackageManager.NameNotFoundException e) {
999 throw new SecurityException("Unknown package " + pkg);
1000 }
1001 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001002
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001003 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 synchronized (mNotificationList) {
1005 final int N = mNotificationList.size();
1006 for (int i=N-1; i>=0; i--) {
1007 NotificationRecord r = mNotificationList.get(i);
1008
1009 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1010 | Notification.FLAG_NO_CLEAR)) == 0) {
1011 if (r.notification.deleteIntent != null) {
1012 try {
1013 r.notification.deleteIntent.send();
1014 } catch (PendingIntent.CanceledException ex) {
1015 // do nothing - there's no relevant way to recover, and
1016 // no reason to let this propagate
Joe Onorato8a9b2202010-02-26 18:56:32 -08001017 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 }
1019 }
1020 mNotificationList.remove(i);
1021 cancelNotificationLocked(r);
1022 }
1023 }
1024
1025 updateLightsLocked();
1026 }
1027 }
1028
1029 private void updateLights() {
1030 synchronized (mNotificationList) {
1031 updateLightsLocked();
1032 }
1033 }
1034
1035 // lock on mNotificationList
1036 private void updateLightsLocked()
1037 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001038 // Battery low always shows, other states only show if charging.
1039 if (mBatteryLow) {
Mike Lockwood445f4302009-09-04 11:06:46 -04001040 if (mBatteryCharging) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001041 mBatteryLight.setColor(BATTERY_LOW_ARGB);
Mike Lockwood445f4302009-09-04 11:06:46 -04001042 } else {
1043 // Flash when battery is low and not charging
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001044 mBatteryLight.setFlashing(BATTERY_LOW_ARGB, LightsService.LIGHT_FLASH_TIMED,
1045 BATTERY_BLINK_ON, BATTERY_BLINK_OFF);
Mike Lockwood445f4302009-09-04 11:06:46 -04001046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 } else if (mBatteryCharging) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001048 if (mBatteryFull) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001049 mBatteryLight.setColor(BATTERY_FULL_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001051 mBatteryLight.setColor(BATTERY_MEDIUM_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 }
1053 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001054 mBatteryLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056
The Android Open Source Project10592532009-03-18 17:39:46 -07001057 // handle notification lights
1058 if (mLedNotification == null) {
1059 // get next notification, if any
1060 int n = mLights.size();
1061 if (n > 0) {
1062 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
1064 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001065
1066 // we only flash if screen is off and persistent pulsing is enabled
Mike Lockwood670f9322010-01-20 12:13:36 -05001067 if (mLedNotification == null || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001068 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001069 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001070 int ledARGB = mLedNotification.notification.ledARGB;
1071 int ledOnMS = mLedNotification.notification.ledOnMS;
1072 int ledOffMS = mLedNotification.notification.ledOffMS;
1073 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1074 ledARGB = mDefaultNotificationColor;
1075 ledOnMS = mDefaultNotificationLedOn;
1076 ledOffMS = mDefaultNotificationLedOff;
1077 }
1078 if (mNotificationPulseEnabled) {
1079 // pulse repeatedly
1080 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1081 ledOnMS, ledOffMS);
1082 } else {
1083 // pulse only once
1084 mNotificationLight.pulse(ledARGB, ledOnMS);
1085 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088
1089 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001090 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 {
1092 ArrayList<NotificationRecord> list = mNotificationList;
1093 final int len = list.size();
1094 for (int i=0; i<len; i++) {
1095 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001096 if (tag == null) {
1097 if (r.tag != null) {
1098 continue;
1099 }
1100 } else {
1101 if (!tag.equals(r.tag)) {
1102 continue;
1103 }
1104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 if (r.id == id && r.pkg.equals(pkg)) {
1106 return i;
1107 }
1108 }
1109 return -1;
1110 }
1111
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001112 // This is here instead of StatusBarPolicy because it is an important
1113 // security feature that we don't want people customizing the platform
1114 // to accidentally lose.
1115 private void updateAdbNotification() {
Mike Lockwoodea8b7d52009-08-04 17:03:15 -04001116 if (mAdbEnabled && mUsbConnected) {
Mike Lockwooded760372009-07-09 07:07:27 -04001117 if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
1118 return;
1119 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001120 if (!mAdbNotificationShown) {
1121 NotificationManager notificationManager = (NotificationManager) mContext
1122 .getSystemService(Context.NOTIFICATION_SERVICE);
1123 if (notificationManager != null) {
1124 Resources r = mContext.getResources();
1125 CharSequence title = r.getText(
1126 com.android.internal.R.string.adb_active_notification_title);
1127 CharSequence message = r.getText(
1128 com.android.internal.R.string.adb_active_notification_message);
1129
1130 if (mAdbNotification == null) {
1131 mAdbNotification = new Notification();
1132 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_warning;
1133 mAdbNotification.when = 0;
1134 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
1135 mAdbNotification.tickerText = title;
1136 mAdbNotification.defaults |= Notification.DEFAULT_SOUND;
1137 }
1138
1139 Intent intent = new Intent(
1140 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
1141 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1142 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1143 // Note: we are hard-coding the component because this is
1144 // an important security UI that we don't want anyone
1145 // intercepting.
1146 intent.setComponent(new ComponentName("com.android.settings",
1147 "com.android.settings.DevelopmentSettings"));
1148 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
1149 intent, 0);
1150
1151 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001152
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001153 mAdbNotificationShown = true;
1154 notificationManager.notify(
1155 com.android.internal.R.string.adb_active_notification_title,
1156 mAdbNotification);
1157 }
1158 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001159
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001160 } else if (mAdbNotificationShown) {
1161 NotificationManager notificationManager = (NotificationManager) mContext
1162 .getSystemService(Context.NOTIFICATION_SERVICE);
1163 if (notificationManager != null) {
1164 mAdbNotificationShown = false;
1165 notificationManager.cancel(
1166 com.android.internal.R.string.adb_active_notification_title);
1167 }
1168 }
1169 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001170
1171 private void updateNotificationPulse() {
1172 synchronized (mNotificationList) {
1173 updateLightsLocked();
1174 }
1175 }
1176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 // ======================================================================
1178 @Override
1179 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1180 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1181 != PackageManager.PERMISSION_GRANTED) {
1182 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1183 + Binder.getCallingPid()
1184 + ", uid=" + Binder.getCallingUid());
1185 return;
1186 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 pw.println("Current Notification Manager state:");
1189
1190 int N;
1191
1192 synchronized (mToastQueue) {
1193 N = mToastQueue.size();
1194 if (N > 0) {
1195 pw.println(" Toast Queue:");
1196 for (int i=0; i<N; i++) {
1197 mToastQueue.get(i).dump(pw, " ");
1198 }
1199 pw.println(" ");
1200 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 }
1203
1204 synchronized (mNotificationList) {
1205 N = mNotificationList.size();
1206 if (N > 0) {
1207 pw.println(" Notification List:");
1208 for (int i=0; i<N; i++) {
1209 mNotificationList.get(i).dump(pw, " ", mContext);
1210 }
1211 pw.println(" ");
1212 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 N = mLights.size();
1215 if (N > 0) {
1216 pw.println(" Lights List:");
1217 for (int i=0; i<N; i++) {
1218 mLights.get(i).dump(pw, " ", mContext);
1219 }
1220 pw.println(" ");
1221 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 pw.println(" mSoundNotification=" + mSoundNotification);
1224 pw.println(" mSound=" + mSound);
1225 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001226 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1227 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 }
1229 }
1230}