blob: da9232bd6564c251ffacde12e73e0e6c663df3a7 [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;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050056import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080059import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.util.Log;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.view.accessibility.AccessibilityEvent;
62import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.widget.Toast;
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import java.io.FileDescriptor;
66import java.io.PrintWriter;
67import java.util.ArrayList;
68import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
70class NotificationManagerService extends INotificationManager.Stub
71{
72 private static final String TAG = "NotificationService";
73 private static final boolean DBG = false;
74
75 // message codes
76 private static final int MESSAGE_TIMEOUT = 2;
77
78 private static final int LONG_DELAY = 3500; // 3.5 seconds
79 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -080080
81 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
84
85 final Context mContext;
86 final IActivityManager mAm;
87 final IBinder mForegroundToken = new Binder();
88
89 private WorkerHandler mHandler;
90 private StatusBarService mStatusBarService;
Mike Lockwood3a322132009-11-24 00:30:52 -050091 private LightsService mLightsService;
Mike Lockwood3cb67a32009-11-27 14:25:58 -050092 private LightsService.Light mBatteryLight;
93 private LightsService.Light mNotificationLight;
94 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Mike Lockwood670f9322010-01-20 12:13:36 -050096 private int mDefaultNotificationColor;
97 private int mDefaultNotificationLedOn;
98 private int mDefaultNotificationLedOff;
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 private NotificationRecord mSoundNotification;
101 private AsyncPlayer mSound;
Joe Onorato30275482009-07-08 17:09:14 -0700102 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400103 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
105 private NotificationRecord mVibrateNotification;
106 private Vibrator mVibrator = new Vibrator();
107
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500108 // for enabling and disabling notification pulse behavior
109 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500110 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500111 private boolean mNotificationPulseEnabled;
112
113 // for adb connected notifications
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400114 private boolean mUsbConnected;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700115 private boolean mAdbEnabled = false;
116 private boolean mAdbNotificationShown = false;
117 private Notification mAdbNotification;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800118
Fred Quintana6ecaff12009-09-25 14:23:13 -0700119 private final ArrayList<NotificationRecord> mNotificationList =
120 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121
122 private ArrayList<ToastRecord> mToastQueue;
123
124 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
125
126 private boolean mBatteryCharging;
127 private boolean mBatteryLow;
128 private boolean mBatteryFull;
129 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700130
The Android Open Source Project10592532009-03-18 17:39:46 -0700131 private static final int BATTERY_LOW_ARGB = 0xFFFF0000; // Charging Low - red solid on
132 private static final int BATTERY_MEDIUM_ARGB = 0xFFFFFF00; // Charging - orange solid on
133 private static final int BATTERY_FULL_ARGB = 0xFF00FF00; // Charging Full - green solid on
134 private static final int BATTERY_BLINK_ON = 125;
135 private static final int BATTERY_BLINK_OFF = 2875;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 private static String idDebugString(Context baseContext, String packageName, int id) {
138 Context c = null;
139
140 if (packageName != null) {
141 try {
142 c = baseContext.createPackageContext(packageName, 0);
143 } catch (NameNotFoundException e) {
144 c = baseContext;
145 }
146 } else {
147 c = baseContext;
148 }
149
150 String pkg;
151 String type;
152 String name;
153
154 Resources r = c.getResources();
155 try {
156 return r.getResourceName(id);
157 } catch (Resources.NotFoundException e) {
158 return "<name unknown>";
159 }
160 }
161
162 private static final class NotificationRecord
163 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700164 final String pkg;
165 final String tag;
166 final int id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 ITransientNotification callback;
168 int duration;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700169 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 IBinder statusBarKey;
171
Fred Quintana6ecaff12009-09-25 14:23:13 -0700172 NotificationRecord(String pkg, String tag, int id, Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 {
174 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700175 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 this.id = id;
177 this.notification = notification;
178 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 void dump(PrintWriter pw, String prefix, Context baseContext) {
181 pw.println(prefix + this);
182 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
183 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
184 pw.println(prefix + " contentIntent=" + notification.contentIntent);
185 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
186 pw.println(prefix + " tickerText=" + notification.tickerText);
187 pw.println(prefix + " contentView=" + notification.contentView);
188 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
189 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
190 pw.println(prefix + " sound=" + notification.sound);
191 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
192 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
193 + " ledOnMS=" + notification.ledOnMS
194 + " ledOffMS=" + notification.ledOffMS);
195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 @Override
198 public final String toString()
199 {
200 return "NotificationRecord{"
201 + Integer.toHexString(System.identityHashCode(this))
202 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700203 + " id=" + Integer.toHexString(id)
204 + " tag=" + tag + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 }
206 }
207
208 private static final class ToastRecord
209 {
210 final int pid;
211 final String pkg;
212 final ITransientNotification callback;
213 int duration;
214
215 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
216 {
217 this.pid = pid;
218 this.pkg = pkg;
219 this.callback = callback;
220 this.duration = duration;
221 }
222
223 void update(int duration) {
224 this.duration = duration;
225 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 void dump(PrintWriter pw, String prefix) {
228 pw.println(prefix + this);
229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 @Override
232 public final String toString()
233 {
234 return "ToastRecord{"
235 + Integer.toHexString(System.identityHashCode(this))
236 + " pkg=" + pkg
237 + " callback=" + callback
238 + " duration=" + duration;
239 }
240 }
241
242 private StatusBarService.NotificationCallbacks mNotificationCallbacks
243 = new StatusBarService.NotificationCallbacks() {
244
245 public void onSetDisabled(int status) {
246 synchronized (mNotificationList) {
247 mDisabledNotifications = status;
248 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
249 // cancel whatever's going on
250 long identity = Binder.clearCallingIdentity();
251 try {
252 mSound.stop();
253 }
254 finally {
255 Binder.restoreCallingIdentity(identity);
256 }
257
258 identity = Binder.clearCallingIdentity();
259 try {
260 mVibrator.cancel();
261 }
262 finally {
263 Binder.restoreCallingIdentity(identity);
264 }
265 }
266 }
267 }
268
269 public void onClearAll() {
270 cancelAll();
271 }
272
Fred Quintana6ecaff12009-09-25 14:23:13 -0700273 public void onNotificationClick(String pkg, String tag, int id) {
274 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700275 Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 }
277
278 public void onPanelRevealed() {
279 synchronized (mNotificationList) {
280 // sound
281 mSoundNotification = null;
282 long identity = Binder.clearCallingIdentity();
283 try {
284 mSound.stop();
285 }
286 finally {
287 Binder.restoreCallingIdentity(identity);
288 }
289
290 // vibrate
291 mVibrateNotification = null;
292 identity = Binder.clearCallingIdentity();
293 try {
294 mVibrator.cancel();
295 }
296 finally {
297 Binder.restoreCallingIdentity(identity);
298 }
299
300 // light
301 mLights.clear();
302 mLedNotification = null;
303 updateLightsLocked();
304 }
305 }
306 };
307
308 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
309 @Override
310 public void onReceive(Context context, Intent intent) {
311 String action = intent.getAction();
312
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800313 boolean queryRestart = false;
314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
316 boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0);
317 int level = intent.getIntExtra("level", -1);
318 boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD);
319 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
320 boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90);
321
322 if (batteryCharging != mBatteryCharging ||
323 batteryLow != mBatteryLow ||
324 batteryFull != mBatteryFull) {
325 mBatteryCharging = batteryCharging;
326 mBatteryLow = batteryLow;
327 mBatteryFull = batteryFull;
328 updateLights();
329 }
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400330 } else if (action.equals(Intent.ACTION_UMS_CONNECTED)) {
331 mUsbConnected = true;
332 updateAdbNotification();
333 } else if (action.equals(Intent.ACTION_UMS_DISCONNECTED)) {
334 mUsbConnected = false;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700335 updateAdbNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800337 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800338 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800339 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800340 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800341 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800342 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800343 } else if (queryRestart) {
344 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800345 } else {
346 Uri uri = intent.getData();
347 if (uri == null) {
348 return;
349 }
350 String pkgName = uri.getSchemeSpecificPart();
351 if (pkgName == null) {
352 return;
353 }
354 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800356 if (pkgList != null && (pkgList.length > 0)) {
357 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800358 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500361 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
362 mScreenOn = true;
363 updateNotificationPulse();
364 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
365 mScreenOn = false;
366 updateNotificationPulse();
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500367 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
368 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK));
369 updateNotificationPulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 }
371 }
372 };
373
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700374 class SettingsObserver extends ContentObserver {
375 SettingsObserver(Handler handler) {
376 super(handler);
377 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800378
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700379 void observe() {
380 ContentResolver resolver = mContext.getContentResolver();
381 resolver.registerContentObserver(Settings.Secure.getUriFor(
382 Settings.Secure.ADB_ENABLED), false, this);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500383 resolver.registerContentObserver(Settings.System.getUriFor(
384 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700385 update();
386 }
387
388 @Override public void onChange(boolean selfChange) {
389 update();
390 }
391
392 public void update() {
393 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500394 boolean adbEnabled = Settings.Secure.getInt(resolver,
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700395 Settings.Secure.ADB_ENABLED, 0) != 0;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500396 if (mAdbEnabled != adbEnabled) {
397 mAdbEnabled = adbEnabled;
398 updateAdbNotification();
399 }
400 boolean pulseEnabled = Settings.System.getInt(resolver,
401 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
402 if (mNotificationPulseEnabled != pulseEnabled) {
403 mNotificationPulseEnabled = pulseEnabled;
404 updateNotificationPulse();
405 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700406 }
407 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500408
The Android Open Source Project10592532009-03-18 17:39:46 -0700409 NotificationManagerService(Context context, StatusBarService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500410 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 {
412 super();
413 mContext = context;
Mike Lockwood3a322132009-11-24 00:30:52 -0500414 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 mAm = ActivityManagerNative.getDefault();
416 mSound = new AsyncPlayer(TAG);
417 mSound.setUsesWakeLock(context);
418 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 mStatusBarService = statusBar;
422 statusBar.setNotificationCallbacks(mNotificationCallbacks);
423
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500424 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
425 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
426 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
427
Mike Lockwood670f9322010-01-20 12:13:36 -0500428 Resources resources = mContext.getResources();
429 mDefaultNotificationColor = resources.getColor(
430 com.android.internal.R.color.config_defaultNotificationColor);
431 mDefaultNotificationLedOn = resources.getInteger(
432 com.android.internal.R.integer.config_defaultNotificationLedOn);
433 mDefaultNotificationLedOff = resources.getInteger(
434 com.android.internal.R.integer.config_defaultNotificationLedOff);
435
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400436 // Don't start allowing notifications until the setup wizard has run once.
437 // After that, including subsequent boots, init with notifications turned on.
438 // This works on the first boot because the setup wizard will toggle this
439 // flag at least once and we'll go back to 0 after that.
440 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
441 Settings.Secure.DEVICE_PROVISIONED, 0)) {
442 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
443 }
444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 // register for battery changed notifications
446 IntentFilter filter = new IntentFilter();
447 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400448 filter.addAction(Intent.ACTION_UMS_CONNECTED);
449 filter.addAction(Intent.ACTION_UMS_DISCONNECTED);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500450 filter.addAction(Intent.ACTION_SCREEN_ON);
451 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500452 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800454 IntentFilter pkgFilter = new IntentFilter();
455 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
456 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
457 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
458 pkgFilter.addDataScheme("package");
459 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800460 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800461 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800462
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500463 SettingsObserver observer = new SettingsObserver(mHandler);
464 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
466
Joe Onorato30275482009-07-08 17:09:14 -0700467 void systemReady() {
468 // no beeping until we're basically done booting
469 mSystemReady = true;
470 }
471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 // Toasts
473 // ============================================================================
474 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
475 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800476 Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477
478 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800479 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 return ;
481 }
482
483 synchronized (mToastQueue) {
484 int callingPid = Binder.getCallingPid();
485 long callingId = Binder.clearCallingIdentity();
486 try {
487 ToastRecord record;
488 int index = indexOfToastLocked(pkg, callback);
489 // If it's already in the queue, we update it in place, we don't
490 // move it to the end of the queue.
491 if (index >= 0) {
492 record = mToastQueue.get(index);
493 record.update(duration);
494 } else {
495 record = new ToastRecord(callingPid, pkg, callback, duration);
496 mToastQueue.add(record);
497 index = mToastQueue.size() - 1;
498 keepProcessAliveLocked(callingPid);
499 }
500 // If it's at index 0, it's the current toast. It doesn't matter if it's
501 // new or just been updated. Call back and tell it to show itself.
502 // If the callback fails, this will remove it from the list, so don't
503 // assume that it's valid after this.
504 if (index == 0) {
505 showNextToastLocked();
506 }
507 } finally {
508 Binder.restoreCallingIdentity(callingId);
509 }
510 }
511 }
512
513 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800514 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515
516 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800517 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 return ;
519 }
520
521 synchronized (mToastQueue) {
522 long callingId = Binder.clearCallingIdentity();
523 try {
524 int index = indexOfToastLocked(pkg, callback);
525 if (index >= 0) {
526 cancelToastLocked(index);
527 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800528 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 }
530 } finally {
531 Binder.restoreCallingIdentity(callingId);
532 }
533 }
534 }
535
536 private void showNextToastLocked() {
537 ToastRecord record = mToastQueue.get(0);
538 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800539 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 try {
541 record.callback.show();
542 scheduleTimeoutLocked(record, false);
543 return;
544 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800545 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 + " in package " + record.pkg);
547 // remove it from the list and let the process die
548 int index = mToastQueue.indexOf(record);
549 if (index >= 0) {
550 mToastQueue.remove(index);
551 }
552 keepProcessAliveLocked(record.pid);
553 if (mToastQueue.size() > 0) {
554 record = mToastQueue.get(0);
555 } else {
556 record = null;
557 }
558 }
559 }
560 }
561
562 private void cancelToastLocked(int index) {
563 ToastRecord record = mToastQueue.get(index);
564 try {
565 record.callback.hide();
566 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800567 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 + " in package " + record.pkg);
569 // don't worry about this, we're about to remove it from
570 // the list anyway
571 }
572 mToastQueue.remove(index);
573 keepProcessAliveLocked(record.pid);
574 if (mToastQueue.size() > 0) {
575 // Show the next one. If the callback fails, this will remove
576 // it from the list, so don't assume that the list hasn't changed
577 // after this point.
578 showNextToastLocked();
579 }
580 }
581
582 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
583 {
584 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
585 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
586 mHandler.removeCallbacksAndMessages(r);
587 mHandler.sendMessageDelayed(m, delay);
588 }
589
590 private void handleTimeout(ToastRecord record)
591 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800592 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 synchronized (mToastQueue) {
594 int index = indexOfToastLocked(record.pkg, record.callback);
595 if (index >= 0) {
596 cancelToastLocked(index);
597 }
598 }
599 }
600
601 // lock on mToastQueue
602 private int indexOfToastLocked(String pkg, ITransientNotification callback)
603 {
604 IBinder cbak = callback.asBinder();
605 ArrayList<ToastRecord> list = mToastQueue;
606 int len = list.size();
607 for (int i=0; i<len; i++) {
608 ToastRecord r = list.get(i);
609 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
610 return i;
611 }
612 }
613 return -1;
614 }
615
616 // lock on mToastQueue
617 private void keepProcessAliveLocked(int pid)
618 {
619 int toastCount = 0; // toasts from this pid
620 ArrayList<ToastRecord> list = mToastQueue;
621 int N = list.size();
622 for (int i=0; i<N; i++) {
623 ToastRecord r = list.get(i);
624 if (r.pid == pid) {
625 toastCount++;
626 }
627 }
628 try {
629 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
630 } catch (RemoteException e) {
631 // Shouldn't happen.
632 }
633 }
634
635 private final class WorkerHandler extends Handler
636 {
637 @Override
638 public void handleMessage(Message msg)
639 {
640 switch (msg.what)
641 {
642 case MESSAGE_TIMEOUT:
643 handleTimeout((ToastRecord)msg.obj);
644 break;
645 }
646 }
647 }
648
649
650 // Notifications
651 // ============================================================================
652 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
653 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700654 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
655 }
656
657 public void enqueueNotificationWithTag(String pkg, String tag, int id,
658 Notification notification, int[] idOut)
659 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700660 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 // This conditional is a dirty hack to limit the logging done on
663 // behalf of the download manager without affecting other apps.
664 if (!pkg.equals("com.android.providers.downloads")
665 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800666 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 }
668
669 if (pkg == null || notification == null) {
670 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
671 + " id=" + id + " notification=" + notification);
672 }
673 if (notification.icon != 0) {
674 if (notification.contentView == null) {
675 throw new IllegalArgumentException("contentView required: pkg=" + pkg
676 + " id=" + id + " notification=" + notification);
677 }
678 if (notification.contentIntent == null) {
679 throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
680 + " id=" + id + " notification=" + notification);
681 }
682 }
683
684 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700685 NotificationRecord r = new NotificationRecord(pkg, tag, id, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 NotificationRecord old = null;
687
Fred Quintana6ecaff12009-09-25 14:23:13 -0700688 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 if (index < 0) {
690 mNotificationList.add(r);
691 } else {
692 old = mNotificationList.remove(index);
693 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700694 // Make sure we don't lose the foreground service state.
695 if (old != null) {
696 notification.flags |=
697 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800700
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700701 // Ensure if this is a foreground service that the proper additional
702 // flags are set.
703 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
704 notification.flags |= Notification.FLAG_ONGOING_EVENT
705 | Notification.FLAG_NO_CLEAR;
706 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 if (notification.icon != 0) {
709 IconData icon = IconData.makeIcon(null, pkg, notification.icon,
710 notification.iconLevel,
711 notification.number);
712 CharSequence truncatedTicker = notification.tickerText;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 // TODO: make this restriction do something smarter like never fill
715 // more than two screens. "Why would anyone need more than 80 characters." :-/
716 final int maxTickerLen = 80;
717 if (truncatedTicker != null && truncatedTicker.length() > maxTickerLen) {
718 truncatedTicker = truncatedTicker.subSequence(0, maxTickerLen);
719 }
720
721 NotificationData n = new NotificationData();
Fred Quintana6ecaff12009-09-25 14:23:13 -0700722 n.pkg = pkg;
723 n.tag = tag;
724 n.id = id;
725 n.when = notification.when;
726 n.tickerText = truncatedTicker;
727 n.ongoingEvent = (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0;
728 if (!n.ongoingEvent && (notification.flags & Notification.FLAG_NO_CLEAR) == 0) {
729 n.clearable = true;
730 }
731 n.contentView = notification.contentView;
732 n.contentIntent = notification.contentIntent;
733 n.deleteIntent = notification.deleteIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 if (old != null && old.statusBarKey != null) {
735 r.statusBarKey = old.statusBarKey;
736 long identity = Binder.clearCallingIdentity();
737 try {
738 mStatusBarService.updateIcon(r.statusBarKey, icon, n);
739 }
740 finally {
741 Binder.restoreCallingIdentity(identity);
742 }
743 } else {
744 long identity = Binder.clearCallingIdentity();
745 try {
746 r.statusBarKey = mStatusBarService.addIcon(icon, n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500747 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 }
749 finally {
750 Binder.restoreCallingIdentity(identity);
751 }
752 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700753
Joe Onorato30275482009-07-08 17:09:14 -0700754 sendAccessibilityEvent(notification, pkg);
svetoslavganov75986cf2009-05-14 22:28:01 -0700755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 } else {
757 if (old != null && old.statusBarKey != null) {
758 long identity = Binder.clearCallingIdentity();
759 try {
760 mStatusBarService.removeIcon(old.statusBarKey);
761 }
762 finally {
763 Binder.restoreCallingIdentity(identity);
764 }
765 }
766 }
767
768 // If we're not supposed to beep, vibrate, etc. then don't.
769 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
770 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700771 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
772 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800773
774 final AudioManager audioManager = (AudioManager) mContext
775 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 // sound
777 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800778 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 if (useDefaultSound || notification.sound != null) {
780 Uri uri;
781 if (useDefaultSound) {
782 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
783 } else {
784 uri = notification.sound;
785 }
786 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
787 int audioStreamType;
788 if (notification.audioStreamType >= 0) {
789 audioStreamType = notification.audioStreamType;
790 } else {
791 audioStreamType = DEFAULT_STREAM_TYPE;
792 }
793 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800794 // do not play notifications if stream volume is 0
795 // (typically because ringer mode is silent).
796 if (audioManager.getStreamVolume(audioStreamType) != 0) {
797 long identity = Binder.clearCallingIdentity();
798 try {
799 mSound.play(mContext, uri, looping, audioStreamType);
800 }
801 finally {
802 Binder.restoreCallingIdentity(identity);
803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
805 }
806
807 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800809 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 if ((useDefaultVibrate || notification.vibrate != null)
811 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
812 mVibrateNotification = r;
813
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800814 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 : notification.vibrate,
816 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
817 }
818 }
819
820 // this option doesn't shut off the lights
821
822 // light
823 // the most recent thing gets the light
824 mLights.remove(old);
825 if (mLedNotification == old) {
826 mLedNotification = null;
827 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800828 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
830 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
831 mLights.add(r);
832 updateLightsLocked();
833 } else {
834 if (old != null
835 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
836 updateLightsLocked();
837 }
838 }
839 }
840
841 idOut[0] = id;
842 }
843
Joe Onorato30275482009-07-08 17:09:14 -0700844 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700845 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
846 if (!manager.isEnabled()) {
847 return;
848 }
849
850 AccessibilityEvent event =
851 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
852 event.setPackageName(packageName);
853 event.setClassName(Notification.class.getName());
854 event.setParcelableData(notification);
855 CharSequence tickerText = notification.tickerText;
856 if (!TextUtils.isEmpty(tickerText)) {
857 event.getText().add(tickerText);
858 }
859
860 manager.sendAccessibilityEvent(event);
861 }
862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 private void cancelNotificationLocked(NotificationRecord r) {
864 // status bar
865 if (r.notification.icon != 0) {
866 long identity = Binder.clearCallingIdentity();
867 try {
868 mStatusBarService.removeIcon(r.statusBarKey);
869 }
870 finally {
871 Binder.restoreCallingIdentity(identity);
872 }
873 r.statusBarKey = null;
874 }
875
876 // sound
877 if (mSoundNotification == r) {
878 mSoundNotification = null;
879 long identity = Binder.clearCallingIdentity();
880 try {
881 mSound.stop();
882 }
883 finally {
884 Binder.restoreCallingIdentity(identity);
885 }
886 }
887
888 // vibrate
889 if (mVibrateNotification == r) {
890 mVibrateNotification = null;
891 long identity = Binder.clearCallingIdentity();
892 try {
893 mVibrator.cancel();
894 }
895 finally {
896 Binder.restoreCallingIdentity(identity);
897 }
898 }
899
900 // light
901 mLights.remove(r);
902 if (mLedNotification == r) {
903 mLedNotification = null;
904 }
905 }
906
907 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700908 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800909 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700911 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700912 int mustNotHaveFlags) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800913 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914
915 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700916 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700918 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
921 return;
922 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700923 if ((r.notification.flags & mustNotHaveFlags) != 0) {
924 return;
925 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 mNotificationList.remove(index);
928
929 cancelNotificationLocked(r);
930 updateLightsLocked();
931 }
932 }
933 }
934
935 /**
936 * Cancels all notifications from a given package that have all of the
937 * {@code mustHaveFlags}.
938 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800939 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
940 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800941 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942
943 synchronized (mNotificationList) {
944 final int N = mNotificationList.size();
945 boolean canceledSomething = false;
946 for (int i = N-1; i >= 0; --i) {
947 NotificationRecord r = mNotificationList.get(i);
948 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
949 continue;
950 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700951 if ((r.notification.flags & mustNotHaveFlags) != 0) {
952 continue;
953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 if (!r.pkg.equals(pkg)) {
955 continue;
956 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800957 canceledSomething = true;
958 if (!doit) {
959 return true;
960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 mNotificationList.remove(i);
962 cancelNotificationLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 }
964 if (canceledSomething) {
965 updateLightsLocked();
966 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800967 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 }
969 }
970
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800971
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700972 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700973 cancelNotificationWithTag(pkg, null /* tag */, id);
974 }
975
976 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700977 checkIncomingCall(pkg);
978 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700979 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700980 Binder.getCallingUid() == Process.SYSTEM_UID
981 ? 0 : Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 }
983
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700984 public void cancelAllNotifications(String pkg) {
985 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800986
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700987 // Calling from user space, don't allow the canceling of actively
988 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800989 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 }
991
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700992 void checkIncomingCall(String pkg) {
993 int uid = Binder.getCallingUid();
994 if (uid == Process.SYSTEM_UID || uid == 0) {
995 return;
996 }
997 try {
998 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
999 pkg, 0);
1000 if (ai.uid != uid) {
1001 throw new SecurityException("Calling uid " + uid + " gave package"
1002 + pkg + " which is owned by uid " + ai.uid);
1003 }
1004 } catch (PackageManager.NameNotFoundException e) {
1005 throw new SecurityException("Unknown package " + pkg);
1006 }
1007 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001008
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001009 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 synchronized (mNotificationList) {
1011 final int N = mNotificationList.size();
1012 for (int i=N-1; i>=0; i--) {
1013 NotificationRecord r = mNotificationList.get(i);
1014
1015 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1016 | Notification.FLAG_NO_CLEAR)) == 0) {
1017 if (r.notification.deleteIntent != null) {
1018 try {
1019 r.notification.deleteIntent.send();
1020 } catch (PendingIntent.CanceledException ex) {
1021 // do nothing - there's no relevant way to recover, and
1022 // no reason to let this propagate
Joe Onorato8a9b2202010-02-26 18:56:32 -08001023 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 }
1025 }
1026 mNotificationList.remove(i);
1027 cancelNotificationLocked(r);
1028 }
1029 }
1030
1031 updateLightsLocked();
1032 }
1033 }
1034
1035 private void updateLights() {
1036 synchronized (mNotificationList) {
1037 updateLightsLocked();
1038 }
1039 }
1040
1041 // lock on mNotificationList
1042 private void updateLightsLocked()
1043 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001044 // Battery low always shows, other states only show if charging.
1045 if (mBatteryLow) {
Mike Lockwood445f4302009-09-04 11:06:46 -04001046 if (mBatteryCharging) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001047 mBatteryLight.setColor(BATTERY_LOW_ARGB);
Mike Lockwood445f4302009-09-04 11:06:46 -04001048 } else {
1049 // Flash when battery is low and not charging
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001050 mBatteryLight.setFlashing(BATTERY_LOW_ARGB, LightsService.LIGHT_FLASH_TIMED,
1051 BATTERY_BLINK_ON, BATTERY_BLINK_OFF);
Mike Lockwood445f4302009-09-04 11:06:46 -04001052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 } else if (mBatteryCharging) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001054 if (mBatteryFull) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001055 mBatteryLight.setColor(BATTERY_FULL_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001057 mBatteryLight.setColor(BATTERY_MEDIUM_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 }
1059 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001060 mBatteryLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062
The Android Open Source Project10592532009-03-18 17:39:46 -07001063 // handle notification lights
1064 if (mLedNotification == null) {
1065 // get next notification, if any
1066 int n = mLights.size();
1067 if (n > 0) {
1068 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
1070 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001071
1072 // we only flash if screen is off and persistent pulsing is enabled
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001073 // and we are not currently in a call
1074 if (mLedNotification == null || mScreenOn || mInCall) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001075 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001076 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001077 int ledARGB = mLedNotification.notification.ledARGB;
1078 int ledOnMS = mLedNotification.notification.ledOnMS;
1079 int ledOffMS = mLedNotification.notification.ledOffMS;
1080 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1081 ledARGB = mDefaultNotificationColor;
1082 ledOnMS = mDefaultNotificationLedOn;
1083 ledOffMS = mDefaultNotificationLedOff;
1084 }
1085 if (mNotificationPulseEnabled) {
1086 // pulse repeatedly
1087 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1088 ledOnMS, ledOffMS);
1089 } else {
1090 // pulse only once
1091 mNotificationLight.pulse(ledARGB, ledOnMS);
1092 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
1095
1096 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001097 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 {
1099 ArrayList<NotificationRecord> list = mNotificationList;
1100 final int len = list.size();
1101 for (int i=0; i<len; i++) {
1102 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001103 if (tag == null) {
1104 if (r.tag != null) {
1105 continue;
1106 }
1107 } else {
1108 if (!tag.equals(r.tag)) {
1109 continue;
1110 }
1111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 if (r.id == id && r.pkg.equals(pkg)) {
1113 return i;
1114 }
1115 }
1116 return -1;
1117 }
1118
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001119 // This is here instead of StatusBarPolicy because it is an important
1120 // security feature that we don't want people customizing the platform
1121 // to accidentally lose.
1122 private void updateAdbNotification() {
Mike Lockwoodea8b7d52009-08-04 17:03:15 -04001123 if (mAdbEnabled && mUsbConnected) {
Mike Lockwooded760372009-07-09 07:07:27 -04001124 if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
1125 return;
1126 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001127 if (!mAdbNotificationShown) {
1128 NotificationManager notificationManager = (NotificationManager) mContext
1129 .getSystemService(Context.NOTIFICATION_SERVICE);
1130 if (notificationManager != null) {
1131 Resources r = mContext.getResources();
1132 CharSequence title = r.getText(
1133 com.android.internal.R.string.adb_active_notification_title);
1134 CharSequence message = r.getText(
1135 com.android.internal.R.string.adb_active_notification_message);
1136
1137 if (mAdbNotification == null) {
1138 mAdbNotification = new Notification();
1139 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_warning;
1140 mAdbNotification.when = 0;
1141 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
1142 mAdbNotification.tickerText = title;
1143 mAdbNotification.defaults |= Notification.DEFAULT_SOUND;
1144 }
1145
1146 Intent intent = new Intent(
1147 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
1148 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1149 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1150 // Note: we are hard-coding the component because this is
1151 // an important security UI that we don't want anyone
1152 // intercepting.
1153 intent.setComponent(new ComponentName("com.android.settings",
1154 "com.android.settings.DevelopmentSettings"));
1155 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
1156 intent, 0);
1157
1158 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001159
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001160 mAdbNotificationShown = true;
1161 notificationManager.notify(
1162 com.android.internal.R.string.adb_active_notification_title,
1163 mAdbNotification);
1164 }
1165 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001166
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001167 } else if (mAdbNotificationShown) {
1168 NotificationManager notificationManager = (NotificationManager) mContext
1169 .getSystemService(Context.NOTIFICATION_SERVICE);
1170 if (notificationManager != null) {
1171 mAdbNotificationShown = false;
1172 notificationManager.cancel(
1173 com.android.internal.R.string.adb_active_notification_title);
1174 }
1175 }
1176 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001177
1178 private void updateNotificationPulse() {
1179 synchronized (mNotificationList) {
1180 updateLightsLocked();
1181 }
1182 }
1183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 // ======================================================================
1185 @Override
1186 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1187 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1188 != PackageManager.PERMISSION_GRANTED) {
1189 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1190 + Binder.getCallingPid()
1191 + ", uid=" + Binder.getCallingUid());
1192 return;
1193 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 pw.println("Current Notification Manager state:");
1196
1197 int N;
1198
1199 synchronized (mToastQueue) {
1200 N = mToastQueue.size();
1201 if (N > 0) {
1202 pw.println(" Toast Queue:");
1203 for (int i=0; i<N; i++) {
1204 mToastQueue.get(i).dump(pw, " ");
1205 }
1206 pw.println(" ");
1207 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 }
1210
1211 synchronized (mNotificationList) {
1212 N = mNotificationList.size();
1213 if (N > 0) {
1214 pw.println(" Notification List:");
1215 for (int i=0; i<N; i++) {
1216 mNotificationList.get(i).dump(pw, " ", mContext);
1217 }
1218 pw.println(" ");
1219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 N = mLights.size();
1222 if (N > 0) {
1223 pw.println(" Lights List:");
1224 for (int i=0; i<N; i++) {
1225 mLights.get(i).dump(pw, " ", mContext);
1226 }
1227 pw.println(" ");
1228 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 pw.println(" mSoundNotification=" + mSoundNotification);
1231 pw.println(" mSound=" + mSound);
1232 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001233 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1234 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 }
1236 }
1237}