blob: 348accd5533ecde7ac58d1cdf55905da1e40b9d5 [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;
svetoslavganov75986cf2009-05-14 22:28:01 -070042import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.net.Uri;
44import android.os.BatteryManager;
45import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.IBinder;
48import android.os.Message;
49import android.os.Power;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070050import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070051import android.os.RemoteException;
Mike Lockwooded760372009-07-09 07:07:27 -040052import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Vibrator;
54import android.provider.Settings;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050055import android.telephony.TelephonyManager;
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;
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700100 private NotificationPlayer 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;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500109 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500110 private boolean mNotificationPulseEnabled;
111
112 // for adb connected notifications
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400113 private boolean mUsbConnected;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700114 private boolean mAdbEnabled = false;
115 private boolean mAdbNotificationShown = false;
116 private Notification mAdbNotification;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800117
Fred Quintana6ecaff12009-09-25 14:23:13 -0700118 private final ArrayList<NotificationRecord> mNotificationList =
119 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120
121 private ArrayList<ToastRecord> mToastQueue;
122
123 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
124
125 private boolean mBatteryCharging;
126 private boolean mBatteryLow;
127 private boolean mBatteryFull;
128 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700129
Kenneth Anderssond5d87b22010-08-03 13:30:18 +0200130 private static int mBatteryLowARGB;
131 private static int mBatteryMediumARGB;
132 private static int mBatteryFullARGB;
133 private static int mBatteryLedOn;
134 private static int mBatteryLedOff;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 private static String idDebugString(Context baseContext, String packageName, int id) {
137 Context c = null;
138
139 if (packageName != null) {
140 try {
141 c = baseContext.createPackageContext(packageName, 0);
142 } catch (NameNotFoundException e) {
143 c = baseContext;
144 }
145 } else {
146 c = baseContext;
147 }
148
149 String pkg;
150 String type;
151 String name;
152
153 Resources r = c.getResources();
154 try {
155 return r.getResourceName(id);
156 } catch (Resources.NotFoundException e) {
157 return "<name unknown>";
158 }
159 }
160
161 private static final class NotificationRecord
162 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700163 final String pkg;
164 final String tag;
165 final int id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 ITransientNotification callback;
167 int duration;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700168 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 IBinder statusBarKey;
170
Fred Quintana6ecaff12009-09-25 14:23:13 -0700171 NotificationRecord(String pkg, String tag, int id, Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 {
173 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700174 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 this.id = id;
176 this.notification = notification;
177 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 void dump(PrintWriter pw, String prefix, Context baseContext) {
180 pw.println(prefix + this);
181 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
182 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
183 pw.println(prefix + " contentIntent=" + notification.contentIntent);
184 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
185 pw.println(prefix + " tickerText=" + notification.tickerText);
186 pw.println(prefix + " contentView=" + notification.contentView);
187 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
188 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
189 pw.println(prefix + " sound=" + notification.sound);
190 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
191 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
192 + " ledOnMS=" + notification.ledOnMS
193 + " ledOffMS=" + notification.ledOffMS);
194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 @Override
197 public final String toString()
198 {
199 return "NotificationRecord{"
200 + Integer.toHexString(System.identityHashCode(this))
201 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700202 + " id=" + Integer.toHexString(id)
203 + " tag=" + tag + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 }
205 }
206
207 private static final class ToastRecord
208 {
209 final int pid;
210 final String pkg;
211 final ITransientNotification callback;
212 int duration;
213
214 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
215 {
216 this.pid = pid;
217 this.pkg = pkg;
218 this.callback = callback;
219 this.duration = duration;
220 }
221
222 void update(int duration) {
223 this.duration = duration;
224 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 void dump(PrintWriter pw, String prefix) {
227 pw.println(prefix + this);
228 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 @Override
231 public final String toString()
232 {
233 return "ToastRecord{"
234 + Integer.toHexString(System.identityHashCode(this))
235 + " pkg=" + pkg
236 + " callback=" + callback
237 + " duration=" + duration;
238 }
239 }
240
241 private StatusBarService.NotificationCallbacks mNotificationCallbacks
242 = new StatusBarService.NotificationCallbacks() {
243
244 public void onSetDisabled(int status) {
245 synchronized (mNotificationList) {
246 mDisabledNotifications = status;
247 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
248 // cancel whatever's going on
249 long identity = Binder.clearCallingIdentity();
250 try {
251 mSound.stop();
252 }
253 finally {
254 Binder.restoreCallingIdentity(identity);
255 }
256
257 identity = Binder.clearCallingIdentity();
258 try {
259 mVibrator.cancel();
260 }
261 finally {
262 Binder.restoreCallingIdentity(identity);
263 }
264 }
265 }
266 }
267
268 public void onClearAll() {
269 cancelAll();
270 }
271
Fred Quintana6ecaff12009-09-25 14:23:13 -0700272 public void onNotificationClick(String pkg, String tag, int id) {
273 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700274 Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 }
276
277 public void onPanelRevealed() {
278 synchronized (mNotificationList) {
279 // sound
280 mSoundNotification = null;
281 long identity = Binder.clearCallingIdentity();
282 try {
283 mSound.stop();
284 }
285 finally {
286 Binder.restoreCallingIdentity(identity);
287 }
288
289 // vibrate
290 mVibrateNotification = null;
291 identity = Binder.clearCallingIdentity();
292 try {
293 mVibrator.cancel();
294 }
295 finally {
296 Binder.restoreCallingIdentity(identity);
297 }
298
299 // light
300 mLights.clear();
301 mLedNotification = null;
302 updateLightsLocked();
303 }
304 }
305 };
306
307 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
308 @Override
309 public void onReceive(Context context, Intent intent) {
310 String action = intent.getAction();
311
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800312 boolean queryRestart = false;
313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
315 boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0);
316 int level = intent.getIntExtra("level", -1);
317 boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD);
318 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
319 boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90);
320
321 if (batteryCharging != mBatteryCharging ||
322 batteryLow != mBatteryLow ||
323 batteryFull != mBatteryFull) {
324 mBatteryCharging = batteryCharging;
325 mBatteryLow = batteryLow;
326 mBatteryFull = batteryFull;
327 updateLights();
328 }
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400329 } else if (action.equals(Intent.ACTION_UMS_CONNECTED)) {
330 mUsbConnected = true;
331 updateAdbNotification();
332 } else if (action.equals(Intent.ACTION_UMS_DISCONNECTED)) {
333 mUsbConnected = false;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700334 updateAdbNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800336 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800337 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800338 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800339 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800340 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800341 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800342 } else if (queryRestart) {
343 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800344 } else {
345 Uri uri = intent.getData();
346 if (uri == null) {
347 return;
348 }
349 String pkgName = uri.getSchemeSpecificPart();
350 if (pkgName == null) {
351 return;
352 }
353 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800355 if (pkgList != null && (pkgList.length > 0)) {
356 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800357 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800358 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500360 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
361 mScreenOn = true;
362 updateNotificationPulse();
363 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
364 mScreenOn = false;
365 updateNotificationPulse();
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500366 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
367 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK));
368 updateNotificationPulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 }
370 }
371 };
372
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700373 class SettingsObserver extends ContentObserver {
374 SettingsObserver(Handler handler) {
375 super(handler);
376 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800377
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700378 void observe() {
379 ContentResolver resolver = mContext.getContentResolver();
380 resolver.registerContentObserver(Settings.Secure.getUriFor(
381 Settings.Secure.ADB_ENABLED), false, this);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500382 resolver.registerContentObserver(Settings.System.getUriFor(
383 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700384 update();
385 }
386
387 @Override public void onChange(boolean selfChange) {
388 update();
389 }
390
391 public void update() {
392 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500393 boolean adbEnabled = Settings.Secure.getInt(resolver,
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700394 Settings.Secure.ADB_ENABLED, 0) != 0;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500395 if (mAdbEnabled != adbEnabled) {
396 mAdbEnabled = adbEnabled;
397 updateAdbNotification();
398 }
399 boolean pulseEnabled = Settings.System.getInt(resolver,
400 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
401 if (mNotificationPulseEnabled != pulseEnabled) {
402 mNotificationPulseEnabled = pulseEnabled;
403 updateNotificationPulse();
404 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700405 }
406 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500407
The Android Open Source Project10592532009-03-18 17:39:46 -0700408 NotificationManagerService(Context context, StatusBarService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500409 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 {
411 super();
412 mContext = context;
Mike Lockwood3a322132009-11-24 00:30:52 -0500413 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 mAm = ActivityManagerNative.getDefault();
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700415 mSound = new NotificationPlayer(TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 mSound.setUsesWakeLock(context);
417 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 mStatusBarService = statusBar;
421 statusBar.setNotificationCallbacks(mNotificationCallbacks);
422
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500423 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
424 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
425 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
426
Mike Lockwood670f9322010-01-20 12:13:36 -0500427 Resources resources = mContext.getResources();
428 mDefaultNotificationColor = resources.getColor(
429 com.android.internal.R.color.config_defaultNotificationColor);
430 mDefaultNotificationLedOn = resources.getInteger(
431 com.android.internal.R.integer.config_defaultNotificationLedOn);
432 mDefaultNotificationLedOff = resources.getInteger(
433 com.android.internal.R.integer.config_defaultNotificationLedOff);
434
Kenneth Anderssond5d87b22010-08-03 13:30:18 +0200435 mBatteryLowARGB = mContext.getResources().getInteger(
436 com.android.internal.R.integer.config_notificationsBatteryLowARGB);
437 mBatteryMediumARGB = mContext.getResources().getInteger(
438 com.android.internal.R.integer.config_notificationsBatteryMediumARGB);
439 mBatteryFullARGB = mContext.getResources().getInteger(
440 com.android.internal.R.integer.config_notificationsBatteryFullARGB);
441 mBatteryLedOn = mContext.getResources().getInteger(
442 com.android.internal.R.integer.config_notificationsBatteryLedOn);
443 mBatteryLedOff = mContext.getResources().getInteger(
444 com.android.internal.R.integer.config_notificationsBatteryLedOff);
445
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400446 // Don't start allowing notifications until the setup wizard has run once.
447 // After that, including subsequent boots, init with notifications turned on.
448 // This works on the first boot because the setup wizard will toggle this
449 // flag at least once and we'll go back to 0 after that.
450 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
451 Settings.Secure.DEVICE_PROVISIONED, 0)) {
452 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
453 }
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 // register for battery changed notifications
456 IntentFilter filter = new IntentFilter();
457 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400458 filter.addAction(Intent.ACTION_UMS_CONNECTED);
459 filter.addAction(Intent.ACTION_UMS_DISCONNECTED);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500460 filter.addAction(Intent.ACTION_SCREEN_ON);
461 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500462 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800464 IntentFilter pkgFilter = new IntentFilter();
465 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
466 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
467 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
468 pkgFilter.addDataScheme("package");
469 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800470 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800471 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800472
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500473 SettingsObserver observer = new SettingsObserver(mHandler);
474 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 }
476
Joe Onorato30275482009-07-08 17:09:14 -0700477 void systemReady() {
478 // no beeping until we're basically done booting
479 mSystemReady = true;
480 }
481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 // Toasts
483 // ============================================================================
484 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
485 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400486 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487
488 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800489 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 return ;
491 }
492
493 synchronized (mToastQueue) {
494 int callingPid = Binder.getCallingPid();
495 long callingId = Binder.clearCallingIdentity();
496 try {
497 ToastRecord record;
498 int index = indexOfToastLocked(pkg, callback);
499 // If it's already in the queue, we update it in place, we don't
500 // move it to the end of the queue.
501 if (index >= 0) {
502 record = mToastQueue.get(index);
503 record.update(duration);
504 } else {
505 record = new ToastRecord(callingPid, pkg, callback, duration);
506 mToastQueue.add(record);
507 index = mToastQueue.size() - 1;
508 keepProcessAliveLocked(callingPid);
509 }
510 // If it's at index 0, it's the current toast. It doesn't matter if it's
511 // new or just been updated. Call back and tell it to show itself.
512 // If the callback fails, this will remove it from the list, so don't
513 // assume that it's valid after this.
514 if (index == 0) {
515 showNextToastLocked();
516 }
517 } finally {
518 Binder.restoreCallingIdentity(callingId);
519 }
520 }
521 }
522
523 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800524 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525
526 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800527 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 return ;
529 }
530
531 synchronized (mToastQueue) {
532 long callingId = Binder.clearCallingIdentity();
533 try {
534 int index = indexOfToastLocked(pkg, callback);
535 if (index >= 0) {
536 cancelToastLocked(index);
537 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800538 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 }
540 } finally {
541 Binder.restoreCallingIdentity(callingId);
542 }
543 }
544 }
545
546 private void showNextToastLocked() {
547 ToastRecord record = mToastQueue.get(0);
548 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800549 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 try {
551 record.callback.show();
552 scheduleTimeoutLocked(record, false);
553 return;
554 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800555 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 + " in package " + record.pkg);
557 // remove it from the list and let the process die
558 int index = mToastQueue.indexOf(record);
559 if (index >= 0) {
560 mToastQueue.remove(index);
561 }
562 keepProcessAliveLocked(record.pid);
563 if (mToastQueue.size() > 0) {
564 record = mToastQueue.get(0);
565 } else {
566 record = null;
567 }
568 }
569 }
570 }
571
572 private void cancelToastLocked(int index) {
573 ToastRecord record = mToastQueue.get(index);
574 try {
575 record.callback.hide();
576 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800577 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 + " in package " + record.pkg);
579 // don't worry about this, we're about to remove it from
580 // the list anyway
581 }
582 mToastQueue.remove(index);
583 keepProcessAliveLocked(record.pid);
584 if (mToastQueue.size() > 0) {
585 // Show the next one. If the callback fails, this will remove
586 // it from the list, so don't assume that the list hasn't changed
587 // after this point.
588 showNextToastLocked();
589 }
590 }
591
592 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
593 {
594 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
595 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
596 mHandler.removeCallbacksAndMessages(r);
597 mHandler.sendMessageDelayed(m, delay);
598 }
599
600 private void handleTimeout(ToastRecord record)
601 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800602 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 synchronized (mToastQueue) {
604 int index = indexOfToastLocked(record.pkg, record.callback);
605 if (index >= 0) {
606 cancelToastLocked(index);
607 }
608 }
609 }
610
611 // lock on mToastQueue
612 private int indexOfToastLocked(String pkg, ITransientNotification callback)
613 {
614 IBinder cbak = callback.asBinder();
615 ArrayList<ToastRecord> list = mToastQueue;
616 int len = list.size();
617 for (int i=0; i<len; i++) {
618 ToastRecord r = list.get(i);
619 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
620 return i;
621 }
622 }
623 return -1;
624 }
625
626 // lock on mToastQueue
627 private void keepProcessAliveLocked(int pid)
628 {
629 int toastCount = 0; // toasts from this pid
630 ArrayList<ToastRecord> list = mToastQueue;
631 int N = list.size();
632 for (int i=0; i<N; i++) {
633 ToastRecord r = list.get(i);
634 if (r.pid == pid) {
635 toastCount++;
636 }
637 }
638 try {
639 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
640 } catch (RemoteException e) {
641 // Shouldn't happen.
642 }
643 }
644
645 private final class WorkerHandler extends Handler
646 {
647 @Override
648 public void handleMessage(Message msg)
649 {
650 switch (msg.what)
651 {
652 case MESSAGE_TIMEOUT:
653 handleTimeout((ToastRecord)msg.obj);
654 break;
655 }
656 }
657 }
658
659
660 // Notifications
661 // ============================================================================
662 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
663 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700664 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
665 }
666
667 public void enqueueNotificationWithTag(String pkg, String tag, int id,
668 Notification notification, int[] idOut)
669 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700670 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 // This conditional is a dirty hack to limit the logging done on
673 // behalf of the download manager without affecting other apps.
674 if (!pkg.equals("com.android.providers.downloads")
675 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800676 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 }
678
679 if (pkg == null || notification == null) {
680 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
681 + " id=" + id + " notification=" + notification);
682 }
683 if (notification.icon != 0) {
684 if (notification.contentView == null) {
685 throw new IllegalArgumentException("contentView required: pkg=" + pkg
686 + " id=" + id + " notification=" + notification);
687 }
688 if (notification.contentIntent == null) {
689 throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
690 + " id=" + id + " notification=" + notification);
691 }
692 }
693
694 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700695 NotificationRecord r = new NotificationRecord(pkg, tag, id, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 NotificationRecord old = null;
697
Fred Quintana6ecaff12009-09-25 14:23:13 -0700698 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 if (index < 0) {
700 mNotificationList.add(r);
701 } else {
702 old = mNotificationList.remove(index);
703 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700704 // Make sure we don't lose the foreground service state.
705 if (old != null) {
706 notification.flags |=
707 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800710
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700711 // Ensure if this is a foreground service that the proper additional
712 // flags are set.
713 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
714 notification.flags |= Notification.FLAG_ONGOING_EVENT
715 | Notification.FLAG_NO_CLEAR;
716 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 if (notification.icon != 0) {
719 IconData icon = IconData.makeIcon(null, pkg, notification.icon,
720 notification.iconLevel,
721 notification.number);
722 CharSequence truncatedTicker = notification.tickerText;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 // TODO: make this restriction do something smarter like never fill
725 // more than two screens. "Why would anyone need more than 80 characters." :-/
726 final int maxTickerLen = 80;
727 if (truncatedTicker != null && truncatedTicker.length() > maxTickerLen) {
728 truncatedTicker = truncatedTicker.subSequence(0, maxTickerLen);
729 }
730
731 NotificationData n = new NotificationData();
Fred Quintana6ecaff12009-09-25 14:23:13 -0700732 n.pkg = pkg;
733 n.tag = tag;
734 n.id = id;
735 n.when = notification.when;
736 n.tickerText = truncatedTicker;
737 n.ongoingEvent = (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0;
738 if (!n.ongoingEvent && (notification.flags & Notification.FLAG_NO_CLEAR) == 0) {
739 n.clearable = true;
740 }
741 n.contentView = notification.contentView;
742 n.contentIntent = notification.contentIntent;
743 n.deleteIntent = notification.deleteIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 if (old != null && old.statusBarKey != null) {
745 r.statusBarKey = old.statusBarKey;
746 long identity = Binder.clearCallingIdentity();
747 try {
748 mStatusBarService.updateIcon(r.statusBarKey, icon, n);
749 }
750 finally {
751 Binder.restoreCallingIdentity(identity);
752 }
753 } else {
754 long identity = Binder.clearCallingIdentity();
755 try {
756 r.statusBarKey = mStatusBarService.addIcon(icon, n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500757 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759 finally {
760 Binder.restoreCallingIdentity(identity);
761 }
762 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700763
Joe Onorato30275482009-07-08 17:09:14 -0700764 sendAccessibilityEvent(notification, pkg);
svetoslavganov75986cf2009-05-14 22:28:01 -0700765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 } else {
767 if (old != null && old.statusBarKey != null) {
768 long identity = Binder.clearCallingIdentity();
769 try {
770 mStatusBarService.removeIcon(old.statusBarKey);
771 }
772 finally {
773 Binder.restoreCallingIdentity(identity);
774 }
775 }
776 }
777
778 // If we're not supposed to beep, vibrate, etc. then don't.
779 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
780 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700781 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
782 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800783
784 final AudioManager audioManager = (AudioManager) mContext
785 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 // sound
787 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800788 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 if (useDefaultSound || notification.sound != null) {
790 Uri uri;
791 if (useDefaultSound) {
792 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
793 } else {
794 uri = notification.sound;
795 }
796 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
797 int audioStreamType;
798 if (notification.audioStreamType >= 0) {
799 audioStreamType = notification.audioStreamType;
800 } else {
801 audioStreamType = DEFAULT_STREAM_TYPE;
802 }
803 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800804 // do not play notifications if stream volume is 0
805 // (typically because ringer mode is silent).
806 if (audioManager.getStreamVolume(audioStreamType) != 0) {
807 long identity = Binder.clearCallingIdentity();
808 try {
809 mSound.play(mContext, uri, looping, audioStreamType);
810 }
811 finally {
812 Binder.restoreCallingIdentity(identity);
813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 }
815 }
816
817 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800819 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 if ((useDefaultVibrate || notification.vibrate != null)
821 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
822 mVibrateNotification = r;
823
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800824 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 : notification.vibrate,
826 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
827 }
828 }
829
830 // this option doesn't shut off the lights
831
832 // light
833 // the most recent thing gets the light
834 mLights.remove(old);
835 if (mLedNotification == old) {
836 mLedNotification = null;
837 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800838 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
840 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
841 mLights.add(r);
842 updateLightsLocked();
843 } else {
844 if (old != null
845 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
846 updateLightsLocked();
847 }
848 }
849 }
850
851 idOut[0] = id;
852 }
853
Joe Onorato30275482009-07-08 17:09:14 -0700854 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700855 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
856 if (!manager.isEnabled()) {
857 return;
858 }
859
860 AccessibilityEvent event =
861 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
862 event.setPackageName(packageName);
863 event.setClassName(Notification.class.getName());
864 event.setParcelableData(notification);
865 CharSequence tickerText = notification.tickerText;
866 if (!TextUtils.isEmpty(tickerText)) {
867 event.getText().add(tickerText);
868 }
869
870 manager.sendAccessibilityEvent(event);
871 }
872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 private void cancelNotificationLocked(NotificationRecord r) {
874 // status bar
875 if (r.notification.icon != 0) {
876 long identity = Binder.clearCallingIdentity();
877 try {
878 mStatusBarService.removeIcon(r.statusBarKey);
879 }
880 finally {
881 Binder.restoreCallingIdentity(identity);
882 }
883 r.statusBarKey = null;
884 }
885
886 // sound
887 if (mSoundNotification == r) {
888 mSoundNotification = null;
889 long identity = Binder.clearCallingIdentity();
890 try {
891 mSound.stop();
892 }
893 finally {
894 Binder.restoreCallingIdentity(identity);
895 }
896 }
897
898 // vibrate
899 if (mVibrateNotification == r) {
900 mVibrateNotification = null;
901 long identity = Binder.clearCallingIdentity();
902 try {
903 mVibrator.cancel();
904 }
905 finally {
906 Binder.restoreCallingIdentity(identity);
907 }
908 }
909
910 // light
911 mLights.remove(r);
912 if (mLedNotification == r) {
913 mLedNotification = null;
914 }
915 }
916
917 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700918 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800919 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700921 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700922 int mustNotHaveFlags) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800923 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924
925 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700926 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700928 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
931 return;
932 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700933 if ((r.notification.flags & mustNotHaveFlags) != 0) {
934 return;
935 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 mNotificationList.remove(index);
938
939 cancelNotificationLocked(r);
940 updateLightsLocked();
941 }
942 }
943 }
944
945 /**
946 * Cancels all notifications from a given package that have all of the
947 * {@code mustHaveFlags}.
948 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800949 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
950 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800951 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952
953 synchronized (mNotificationList) {
954 final int N = mNotificationList.size();
955 boolean canceledSomething = false;
956 for (int i = N-1; i >= 0; --i) {
957 NotificationRecord r = mNotificationList.get(i);
958 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
959 continue;
960 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700961 if ((r.notification.flags & mustNotHaveFlags) != 0) {
962 continue;
963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 if (!r.pkg.equals(pkg)) {
965 continue;
966 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800967 canceledSomething = true;
968 if (!doit) {
969 return true;
970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 mNotificationList.remove(i);
972 cancelNotificationLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 }
974 if (canceledSomething) {
975 updateLightsLocked();
976 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800977 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 }
979 }
980
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800981
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700982 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700983 cancelNotificationWithTag(pkg, null /* tag */, id);
984 }
985
986 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700987 checkIncomingCall(pkg);
988 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700989 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700990 Binder.getCallingUid() == Process.SYSTEM_UID
991 ? 0 : Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 }
993
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700994 public void cancelAllNotifications(String pkg) {
995 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800996
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700997 // Calling from user space, don't allow the canceling of actively
998 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800999 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 }
1001
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001002 void checkIncomingCall(String pkg) {
1003 int uid = Binder.getCallingUid();
1004 if (uid == Process.SYSTEM_UID || uid == 0) {
1005 return;
1006 }
1007 try {
1008 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
1009 pkg, 0);
1010 if (ai.uid != uid) {
1011 throw new SecurityException("Calling uid " + uid + " gave package"
1012 + pkg + " which is owned by uid " + ai.uid);
1013 }
1014 } catch (PackageManager.NameNotFoundException e) {
1015 throw new SecurityException("Unknown package " + pkg);
1016 }
1017 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001018
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001019 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 synchronized (mNotificationList) {
1021 final int N = mNotificationList.size();
1022 for (int i=N-1; i>=0; i--) {
1023 NotificationRecord r = mNotificationList.get(i);
1024
1025 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1026 | Notification.FLAG_NO_CLEAR)) == 0) {
1027 if (r.notification.deleteIntent != null) {
1028 try {
1029 r.notification.deleteIntent.send();
1030 } catch (PendingIntent.CanceledException ex) {
1031 // do nothing - there's no relevant way to recover, and
1032 // no reason to let this propagate
Joe Onorato8a9b2202010-02-26 18:56:32 -08001033 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 }
1035 }
1036 mNotificationList.remove(i);
1037 cancelNotificationLocked(r);
1038 }
1039 }
1040
1041 updateLightsLocked();
1042 }
1043 }
1044
1045 private void updateLights() {
1046 synchronized (mNotificationList) {
1047 updateLightsLocked();
1048 }
1049 }
1050
1051 // lock on mNotificationList
1052 private void updateLightsLocked()
1053 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001054 // Battery low always shows, other states only show if charging.
1055 if (mBatteryLow) {
Mike Lockwood445f4302009-09-04 11:06:46 -04001056 if (mBatteryCharging) {
Kenneth Anderssond5d87b22010-08-03 13:30:18 +02001057 mBatteryLight.setColor(mBatteryLowARGB);
Mike Lockwood445f4302009-09-04 11:06:46 -04001058 } else {
1059 // Flash when battery is low and not charging
Kenneth Anderssond5d87b22010-08-03 13:30:18 +02001060 mBatteryLight.setFlashing(mBatteryLowARGB, LightsService.LIGHT_FLASH_TIMED,
1061 mBatteryLedOn, mBatteryLedOff);
Mike Lockwood445f4302009-09-04 11:06:46 -04001062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 } else if (mBatteryCharging) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001064 if (mBatteryFull) {
Kenneth Anderssond5d87b22010-08-03 13:30:18 +02001065 mBatteryLight.setColor(mBatteryFullARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 } else {
Kenneth Anderssond5d87b22010-08-03 13:30:18 +02001067 mBatteryLight.setColor(mBatteryMediumARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 }
1069 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001070 mBatteryLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072
The Android Open Source Project10592532009-03-18 17:39:46 -07001073 // handle notification lights
1074 if (mLedNotification == null) {
1075 // get next notification, if any
1076 int n = mLights.size();
1077 if (n > 0) {
1078 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 }
1080 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001081
1082 // we only flash if screen is off and persistent pulsing is enabled
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001083 // and we are not currently in a call
1084 if (mLedNotification == null || mScreenOn || mInCall) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001085 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001086 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001087 int ledARGB = mLedNotification.notification.ledARGB;
1088 int ledOnMS = mLedNotification.notification.ledOnMS;
1089 int ledOffMS = mLedNotification.notification.ledOffMS;
1090 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1091 ledARGB = mDefaultNotificationColor;
1092 ledOnMS = mDefaultNotificationLedOn;
1093 ledOffMS = mDefaultNotificationLedOff;
1094 }
1095 if (mNotificationPulseEnabled) {
1096 // pulse repeatedly
1097 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1098 ledOnMS, ledOffMS);
1099 } else {
1100 // pulse only once
1101 mNotificationLight.pulse(ledARGB, ledOnMS);
1102 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 }
1105
1106 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001107 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 {
1109 ArrayList<NotificationRecord> list = mNotificationList;
1110 final int len = list.size();
1111 for (int i=0; i<len; i++) {
1112 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001113 if (tag == null) {
1114 if (r.tag != null) {
1115 continue;
1116 }
1117 } else {
1118 if (!tag.equals(r.tag)) {
1119 continue;
1120 }
1121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 if (r.id == id && r.pkg.equals(pkg)) {
1123 return i;
1124 }
1125 }
1126 return -1;
1127 }
1128
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001129 // This is here instead of StatusBarPolicy because it is an important
1130 // security feature that we don't want people customizing the platform
1131 // to accidentally lose.
1132 private void updateAdbNotification() {
Mike Lockwoodea8b7d52009-08-04 17:03:15 -04001133 if (mAdbEnabled && mUsbConnected) {
Mike Lockwooded760372009-07-09 07:07:27 -04001134 if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
1135 return;
1136 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001137 if (!mAdbNotificationShown) {
1138 NotificationManager notificationManager = (NotificationManager) mContext
1139 .getSystemService(Context.NOTIFICATION_SERVICE);
1140 if (notificationManager != null) {
1141 Resources r = mContext.getResources();
1142 CharSequence title = r.getText(
1143 com.android.internal.R.string.adb_active_notification_title);
1144 CharSequence message = r.getText(
1145 com.android.internal.R.string.adb_active_notification_message);
1146
1147 if (mAdbNotification == null) {
1148 mAdbNotification = new Notification();
Daniel Sandler39576c82010-03-25 16:02:33 -04001149 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001150 mAdbNotification.when = 0;
1151 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
1152 mAdbNotification.tickerText = title;
Daniel Sandler39576c82010-03-25 16:02:33 -04001153 mAdbNotification.defaults = 0; // please be quiet
1154 mAdbNotification.sound = null;
1155 mAdbNotification.vibrate = null;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001156 }
1157
1158 Intent intent = new Intent(
1159 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
1160 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1161 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1162 // Note: we are hard-coding the component because this is
1163 // an important security UI that we don't want anyone
1164 // intercepting.
1165 intent.setComponent(new ComponentName("com.android.settings",
1166 "com.android.settings.DevelopmentSettings"));
1167 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
1168 intent, 0);
1169
1170 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001171
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001172 mAdbNotificationShown = true;
1173 notificationManager.notify(
1174 com.android.internal.R.string.adb_active_notification_title,
1175 mAdbNotification);
1176 }
1177 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001178
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001179 } else if (mAdbNotificationShown) {
1180 NotificationManager notificationManager = (NotificationManager) mContext
1181 .getSystemService(Context.NOTIFICATION_SERVICE);
1182 if (notificationManager != null) {
1183 mAdbNotificationShown = false;
1184 notificationManager.cancel(
1185 com.android.internal.R.string.adb_active_notification_title);
1186 }
1187 }
1188 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001189
1190 private void updateNotificationPulse() {
1191 synchronized (mNotificationList) {
1192 updateLightsLocked();
1193 }
1194 }
1195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 // ======================================================================
1197 @Override
1198 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1199 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1200 != PackageManager.PERMISSION_GRANTED) {
1201 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1202 + Binder.getCallingPid()
1203 + ", uid=" + Binder.getCallingUid());
1204 return;
1205 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 pw.println("Current Notification Manager state:");
1208
1209 int N;
1210
1211 synchronized (mToastQueue) {
1212 N = mToastQueue.size();
1213 if (N > 0) {
1214 pw.println(" Toast Queue:");
1215 for (int i=0; i<N; i++) {
1216 mToastQueue.get(i).dump(pw, " ");
1217 }
1218 pw.println(" ");
1219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 }
1222
1223 synchronized (mNotificationList) {
1224 N = mNotificationList.size();
1225 if (N > 0) {
1226 pw.println(" Notification List:");
1227 for (int i=0; i<N; i++) {
1228 mNotificationList.get(i).dump(pw, " ", mContext);
1229 }
1230 pw.println(" ");
1231 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 N = mLights.size();
1234 if (N > 0) {
1235 pw.println(" Lights List:");
1236 for (int i=0; i<N; i++) {
1237 mLights.get(i).dump(pw, " ", mContext);
1238 }
1239 pw.println(" ");
1240 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 pw.println(" mSoundNotification=" + mSoundNotification);
1243 pw.println(" mSound=" + mSound);
1244 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001245 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1246 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 }
1248 }
1249}