blob: 73d17ea7d22d08a331e89786fd3b7ad5367573a4 [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
The Android Open Source Project10592532009-03-18 17:39:46 -0700130 private static final int BATTERY_LOW_ARGB = 0xFFFF0000; // Charging Low - red solid on
131 private static final int BATTERY_MEDIUM_ARGB = 0xFFFFFF00; // Charging - orange solid on
132 private static final int BATTERY_FULL_ARGB = 0xFF00FF00; // Charging Full - green solid on
133 private static final int BATTERY_BLINK_ON = 125;
134 private static final int BATTERY_BLINK_OFF = 2875;
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
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400435 // Don't start allowing notifications until the setup wizard has run once.
436 // After that, including subsequent boots, init with notifications turned on.
437 // This works on the first boot because the setup wizard will toggle this
438 // flag at least once and we'll go back to 0 after that.
439 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
440 Settings.Secure.DEVICE_PROVISIONED, 0)) {
441 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
442 }
443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 // register for battery changed notifications
445 IntentFilter filter = new IntentFilter();
446 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400447 filter.addAction(Intent.ACTION_UMS_CONNECTED);
448 filter.addAction(Intent.ACTION_UMS_DISCONNECTED);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500449 filter.addAction(Intent.ACTION_SCREEN_ON);
450 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500451 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800453 IntentFilter pkgFilter = new IntentFilter();
454 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
455 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
456 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
457 pkgFilter.addDataScheme("package");
458 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800459 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800460 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800461
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500462 SettingsObserver observer = new SettingsObserver(mHandler);
463 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 }
465
Joe Onorato30275482009-07-08 17:09:14 -0700466 void systemReady() {
467 // no beeping until we're basically done booting
468 mSystemReady = true;
469 }
470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 // Toasts
472 // ============================================================================
473 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
474 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400475 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476
477 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800478 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 return ;
480 }
481
482 synchronized (mToastQueue) {
483 int callingPid = Binder.getCallingPid();
484 long callingId = Binder.clearCallingIdentity();
485 try {
486 ToastRecord record;
487 int index = indexOfToastLocked(pkg, callback);
488 // If it's already in the queue, we update it in place, we don't
489 // move it to the end of the queue.
490 if (index >= 0) {
491 record = mToastQueue.get(index);
492 record.update(duration);
493 } else {
494 record = new ToastRecord(callingPid, pkg, callback, duration);
495 mToastQueue.add(record);
496 index = mToastQueue.size() - 1;
497 keepProcessAliveLocked(callingPid);
498 }
499 // If it's at index 0, it's the current toast. It doesn't matter if it's
500 // new or just been updated. Call back and tell it to show itself.
501 // If the callback fails, this will remove it from the list, so don't
502 // assume that it's valid after this.
503 if (index == 0) {
504 showNextToastLocked();
505 }
506 } finally {
507 Binder.restoreCallingIdentity(callingId);
508 }
509 }
510 }
511
512 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800513 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514
515 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800516 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 return ;
518 }
519
520 synchronized (mToastQueue) {
521 long callingId = Binder.clearCallingIdentity();
522 try {
523 int index = indexOfToastLocked(pkg, callback);
524 if (index >= 0) {
525 cancelToastLocked(index);
526 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800527 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 }
529 } finally {
530 Binder.restoreCallingIdentity(callingId);
531 }
532 }
533 }
534
535 private void showNextToastLocked() {
536 ToastRecord record = mToastQueue.get(0);
537 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800538 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 try {
540 record.callback.show();
541 scheduleTimeoutLocked(record, false);
542 return;
543 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800544 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 + " in package " + record.pkg);
546 // remove it from the list and let the process die
547 int index = mToastQueue.indexOf(record);
548 if (index >= 0) {
549 mToastQueue.remove(index);
550 }
551 keepProcessAliveLocked(record.pid);
552 if (mToastQueue.size() > 0) {
553 record = mToastQueue.get(0);
554 } else {
555 record = null;
556 }
557 }
558 }
559 }
560
561 private void cancelToastLocked(int index) {
562 ToastRecord record = mToastQueue.get(index);
563 try {
564 record.callback.hide();
565 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800566 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 + " in package " + record.pkg);
568 // don't worry about this, we're about to remove it from
569 // the list anyway
570 }
571 mToastQueue.remove(index);
572 keepProcessAliveLocked(record.pid);
573 if (mToastQueue.size() > 0) {
574 // Show the next one. If the callback fails, this will remove
575 // it from the list, so don't assume that the list hasn't changed
576 // after this point.
577 showNextToastLocked();
578 }
579 }
580
581 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
582 {
583 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
584 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
585 mHandler.removeCallbacksAndMessages(r);
586 mHandler.sendMessageDelayed(m, delay);
587 }
588
589 private void handleTimeout(ToastRecord record)
590 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800591 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 synchronized (mToastQueue) {
593 int index = indexOfToastLocked(record.pkg, record.callback);
594 if (index >= 0) {
595 cancelToastLocked(index);
596 }
597 }
598 }
599
600 // lock on mToastQueue
601 private int indexOfToastLocked(String pkg, ITransientNotification callback)
602 {
603 IBinder cbak = callback.asBinder();
604 ArrayList<ToastRecord> list = mToastQueue;
605 int len = list.size();
606 for (int i=0; i<len; i++) {
607 ToastRecord r = list.get(i);
608 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
609 return i;
610 }
611 }
612 return -1;
613 }
614
615 // lock on mToastQueue
616 private void keepProcessAliveLocked(int pid)
617 {
618 int toastCount = 0; // toasts from this pid
619 ArrayList<ToastRecord> list = mToastQueue;
620 int N = list.size();
621 for (int i=0; i<N; i++) {
622 ToastRecord r = list.get(i);
623 if (r.pid == pid) {
624 toastCount++;
625 }
626 }
627 try {
628 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
629 } catch (RemoteException e) {
630 // Shouldn't happen.
631 }
632 }
633
634 private final class WorkerHandler extends Handler
635 {
636 @Override
637 public void handleMessage(Message msg)
638 {
639 switch (msg.what)
640 {
641 case MESSAGE_TIMEOUT:
642 handleTimeout((ToastRecord)msg.obj);
643 break;
644 }
645 }
646 }
647
648
649 // Notifications
650 // ============================================================================
651 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
652 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700653 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
654 }
655
656 public void enqueueNotificationWithTag(String pkg, String tag, int id,
657 Notification notification, int[] idOut)
658 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700659 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 // This conditional is a dirty hack to limit the logging done on
662 // behalf of the download manager without affecting other apps.
663 if (!pkg.equals("com.android.providers.downloads")
664 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800665 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
667
668 if (pkg == null || notification == null) {
669 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
670 + " id=" + id + " notification=" + notification);
671 }
672 if (notification.icon != 0) {
673 if (notification.contentView == null) {
674 throw new IllegalArgumentException("contentView required: pkg=" + pkg
675 + " id=" + id + " notification=" + notification);
676 }
677 if (notification.contentIntent == null) {
678 throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
679 + " id=" + id + " notification=" + notification);
680 }
681 }
682
683 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700684 NotificationRecord r = new NotificationRecord(pkg, tag, id, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 NotificationRecord old = null;
686
Fred Quintana6ecaff12009-09-25 14:23:13 -0700687 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 if (index < 0) {
689 mNotificationList.add(r);
690 } else {
691 old = mNotificationList.remove(index);
692 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700693 // Make sure we don't lose the foreground service state.
694 if (old != null) {
695 notification.flags |=
696 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800699
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700700 // Ensure if this is a foreground service that the proper additional
701 // flags are set.
702 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
703 notification.flags |= Notification.FLAG_ONGOING_EVENT
704 | Notification.FLAG_NO_CLEAR;
705 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 if (notification.icon != 0) {
708 IconData icon = IconData.makeIcon(null, pkg, notification.icon,
709 notification.iconLevel,
710 notification.number);
711 CharSequence truncatedTicker = notification.tickerText;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 // TODO: make this restriction do something smarter like never fill
714 // more than two screens. "Why would anyone need more than 80 characters." :-/
715 final int maxTickerLen = 80;
716 if (truncatedTicker != null && truncatedTicker.length() > maxTickerLen) {
717 truncatedTicker = truncatedTicker.subSequence(0, maxTickerLen);
718 }
719
720 NotificationData n = new NotificationData();
Fred Quintana6ecaff12009-09-25 14:23:13 -0700721 n.pkg = pkg;
722 n.tag = tag;
723 n.id = id;
724 n.when = notification.when;
725 n.tickerText = truncatedTicker;
726 n.ongoingEvent = (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0;
727 if (!n.ongoingEvent && (notification.flags & Notification.FLAG_NO_CLEAR) == 0) {
728 n.clearable = true;
729 }
730 n.contentView = notification.contentView;
731 n.contentIntent = notification.contentIntent;
732 n.deleteIntent = notification.deleteIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 if (old != null && old.statusBarKey != null) {
734 r.statusBarKey = old.statusBarKey;
735 long identity = Binder.clearCallingIdentity();
736 try {
737 mStatusBarService.updateIcon(r.statusBarKey, icon, n);
738 }
739 finally {
740 Binder.restoreCallingIdentity(identity);
741 }
742 } else {
743 long identity = Binder.clearCallingIdentity();
744 try {
745 r.statusBarKey = mStatusBarService.addIcon(icon, n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500746 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 }
748 finally {
749 Binder.restoreCallingIdentity(identity);
750 }
751 }
svetoslavganov75986cf2009-05-14 22:28:01 -0700752
Joe Onorato30275482009-07-08 17:09:14 -0700753 sendAccessibilityEvent(notification, pkg);
svetoslavganov75986cf2009-05-14 22:28:01 -0700754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 } else {
756 if (old != null && old.statusBarKey != null) {
757 long identity = Binder.clearCallingIdentity();
758 try {
759 mStatusBarService.removeIcon(old.statusBarKey);
760 }
761 finally {
762 Binder.restoreCallingIdentity(identity);
763 }
764 }
765 }
766
767 // If we're not supposed to beep, vibrate, etc. then don't.
768 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
769 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700770 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
771 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800772
773 final AudioManager audioManager = (AudioManager) mContext
774 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 // sound
776 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800777 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 if (useDefaultSound || notification.sound != null) {
779 Uri uri;
780 if (useDefaultSound) {
781 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
782 } else {
783 uri = notification.sound;
784 }
785 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
786 int audioStreamType;
787 if (notification.audioStreamType >= 0) {
788 audioStreamType = notification.audioStreamType;
789 } else {
790 audioStreamType = DEFAULT_STREAM_TYPE;
791 }
792 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800793 // do not play notifications if stream volume is 0
794 // (typically because ringer mode is silent).
795 if (audioManager.getStreamVolume(audioStreamType) != 0) {
796 long identity = Binder.clearCallingIdentity();
797 try {
798 mSound.play(mContext, uri, looping, audioStreamType);
799 }
800 finally {
801 Binder.restoreCallingIdentity(identity);
802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
804 }
805
806 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800808 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 if ((useDefaultVibrate || notification.vibrate != null)
810 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
811 mVibrateNotification = r;
812
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800813 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 : notification.vibrate,
815 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
816 }
817 }
818
819 // this option doesn't shut off the lights
820
821 // light
822 // the most recent thing gets the light
823 mLights.remove(old);
824 if (mLedNotification == old) {
825 mLedNotification = null;
826 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800827 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
829 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
830 mLights.add(r);
831 updateLightsLocked();
832 } else {
833 if (old != null
834 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
835 updateLightsLocked();
836 }
837 }
838 }
839
840 idOut[0] = id;
841 }
842
Joe Onorato30275482009-07-08 17:09:14 -0700843 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700844 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
845 if (!manager.isEnabled()) {
846 return;
847 }
848
849 AccessibilityEvent event =
850 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
851 event.setPackageName(packageName);
852 event.setClassName(Notification.class.getName());
853 event.setParcelableData(notification);
854 CharSequence tickerText = notification.tickerText;
855 if (!TextUtils.isEmpty(tickerText)) {
856 event.getText().add(tickerText);
857 }
858
859 manager.sendAccessibilityEvent(event);
860 }
861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 private void cancelNotificationLocked(NotificationRecord r) {
863 // status bar
864 if (r.notification.icon != 0) {
865 long identity = Binder.clearCallingIdentity();
866 try {
867 mStatusBarService.removeIcon(r.statusBarKey);
868 }
869 finally {
870 Binder.restoreCallingIdentity(identity);
871 }
872 r.statusBarKey = null;
873 }
874
875 // sound
876 if (mSoundNotification == r) {
877 mSoundNotification = null;
878 long identity = Binder.clearCallingIdentity();
879 try {
880 mSound.stop();
881 }
882 finally {
883 Binder.restoreCallingIdentity(identity);
884 }
885 }
886
887 // vibrate
888 if (mVibrateNotification == r) {
889 mVibrateNotification = null;
890 long identity = Binder.clearCallingIdentity();
891 try {
892 mVibrator.cancel();
893 }
894 finally {
895 Binder.restoreCallingIdentity(identity);
896 }
897 }
898
899 // light
900 mLights.remove(r);
901 if (mLedNotification == r) {
902 mLedNotification = null;
903 }
904 }
905
906 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700907 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800908 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700910 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700911 int mustNotHaveFlags) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800912 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913
914 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700915 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700917 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
920 return;
921 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700922 if ((r.notification.flags & mustNotHaveFlags) != 0) {
923 return;
924 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 mNotificationList.remove(index);
927
928 cancelNotificationLocked(r);
929 updateLightsLocked();
930 }
931 }
932 }
933
934 /**
935 * Cancels all notifications from a given package that have all of the
936 * {@code mustHaveFlags}.
937 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800938 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
939 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800940 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941
942 synchronized (mNotificationList) {
943 final int N = mNotificationList.size();
944 boolean canceledSomething = false;
945 for (int i = N-1; i >= 0; --i) {
946 NotificationRecord r = mNotificationList.get(i);
947 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
948 continue;
949 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700950 if ((r.notification.flags & mustNotHaveFlags) != 0) {
951 continue;
952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 if (!r.pkg.equals(pkg)) {
954 continue;
955 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800956 canceledSomething = true;
957 if (!doit) {
958 return true;
959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 mNotificationList.remove(i);
961 cancelNotificationLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
963 if (canceledSomething) {
964 updateLightsLocked();
965 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800966 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 }
968 }
969
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800970
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700971 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700972 cancelNotificationWithTag(pkg, null /* tag */, id);
973 }
974
975 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700976 checkIncomingCall(pkg);
977 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700978 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700979 Binder.getCallingUid() == Process.SYSTEM_UID
980 ? 0 : Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 }
982
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700983 public void cancelAllNotifications(String pkg) {
984 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800985
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700986 // Calling from user space, don't allow the canceling of actively
987 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800988 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 }
990
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700991 void checkIncomingCall(String pkg) {
992 int uid = Binder.getCallingUid();
993 if (uid == Process.SYSTEM_UID || uid == 0) {
994 return;
995 }
996 try {
997 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
998 pkg, 0);
999 if (ai.uid != uid) {
1000 throw new SecurityException("Calling uid " + uid + " gave package"
1001 + pkg + " which is owned by uid " + ai.uid);
1002 }
1003 } catch (PackageManager.NameNotFoundException e) {
1004 throw new SecurityException("Unknown package " + pkg);
1005 }
1006 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001007
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001008 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 synchronized (mNotificationList) {
1010 final int N = mNotificationList.size();
1011 for (int i=N-1; i>=0; i--) {
1012 NotificationRecord r = mNotificationList.get(i);
1013
1014 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1015 | Notification.FLAG_NO_CLEAR)) == 0) {
1016 if (r.notification.deleteIntent != null) {
1017 try {
1018 r.notification.deleteIntent.send();
1019 } catch (PendingIntent.CanceledException ex) {
1020 // do nothing - there's no relevant way to recover, and
1021 // no reason to let this propagate
Joe Onorato8a9b2202010-02-26 18:56:32 -08001022 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 }
1024 }
1025 mNotificationList.remove(i);
1026 cancelNotificationLocked(r);
1027 }
1028 }
1029
1030 updateLightsLocked();
1031 }
1032 }
1033
1034 private void updateLights() {
1035 synchronized (mNotificationList) {
1036 updateLightsLocked();
1037 }
1038 }
1039
1040 // lock on mNotificationList
1041 private void updateLightsLocked()
1042 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001043 // Battery low always shows, other states only show if charging.
1044 if (mBatteryLow) {
Mike Lockwood445f4302009-09-04 11:06:46 -04001045 if (mBatteryCharging) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001046 mBatteryLight.setColor(BATTERY_LOW_ARGB);
Mike Lockwood445f4302009-09-04 11:06:46 -04001047 } else {
1048 // Flash when battery is low and not charging
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001049 mBatteryLight.setFlashing(BATTERY_LOW_ARGB, LightsService.LIGHT_FLASH_TIMED,
1050 BATTERY_BLINK_ON, BATTERY_BLINK_OFF);
Mike Lockwood445f4302009-09-04 11:06:46 -04001051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 } else if (mBatteryCharging) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001053 if (mBatteryFull) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001054 mBatteryLight.setColor(BATTERY_FULL_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001056 mBatteryLight.setColor(BATTERY_MEDIUM_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 }
1058 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001059 mBatteryLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061
The Android Open Source Project10592532009-03-18 17:39:46 -07001062 // handle notification lights
1063 if (mLedNotification == null) {
1064 // get next notification, if any
1065 int n = mLights.size();
1066 if (n > 0) {
1067 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 }
1069 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001070
1071 // we only flash if screen is off and persistent pulsing is enabled
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001072 // and we are not currently in a call
1073 if (mLedNotification == null || mScreenOn || mInCall) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001074 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001075 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001076 int ledARGB = mLedNotification.notification.ledARGB;
1077 int ledOnMS = mLedNotification.notification.ledOnMS;
1078 int ledOffMS = mLedNotification.notification.ledOffMS;
1079 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1080 ledARGB = mDefaultNotificationColor;
1081 ledOnMS = mDefaultNotificationLedOn;
1082 ledOffMS = mDefaultNotificationLedOff;
1083 }
1084 if (mNotificationPulseEnabled) {
1085 // pulse repeatedly
1086 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1087 ledOnMS, ledOffMS);
1088 } else {
1089 // pulse only once
1090 mNotificationLight.pulse(ledARGB, ledOnMS);
1091 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001092 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 }
1094
1095 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001096 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 {
1098 ArrayList<NotificationRecord> list = mNotificationList;
1099 final int len = list.size();
1100 for (int i=0; i<len; i++) {
1101 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001102 if (tag == null) {
1103 if (r.tag != null) {
1104 continue;
1105 }
1106 } else {
1107 if (!tag.equals(r.tag)) {
1108 continue;
1109 }
1110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 if (r.id == id && r.pkg.equals(pkg)) {
1112 return i;
1113 }
1114 }
1115 return -1;
1116 }
1117
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001118 // This is here instead of StatusBarPolicy because it is an important
1119 // security feature that we don't want people customizing the platform
1120 // to accidentally lose.
1121 private void updateAdbNotification() {
Mike Lockwoodea8b7d52009-08-04 17:03:15 -04001122 if (mAdbEnabled && mUsbConnected) {
Mike Lockwooded760372009-07-09 07:07:27 -04001123 if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
1124 return;
1125 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001126 if (!mAdbNotificationShown) {
1127 NotificationManager notificationManager = (NotificationManager) mContext
1128 .getSystemService(Context.NOTIFICATION_SERVICE);
1129 if (notificationManager != null) {
1130 Resources r = mContext.getResources();
1131 CharSequence title = r.getText(
1132 com.android.internal.R.string.adb_active_notification_title);
1133 CharSequence message = r.getText(
1134 com.android.internal.R.string.adb_active_notification_message);
1135
1136 if (mAdbNotification == null) {
1137 mAdbNotification = new Notification();
Daniel Sandler39576c82010-03-25 16:02:33 -04001138 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001139 mAdbNotification.when = 0;
1140 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
1141 mAdbNotification.tickerText = title;
Daniel Sandler39576c82010-03-25 16:02:33 -04001142 mAdbNotification.defaults = 0; // please be quiet
1143 mAdbNotification.sound = null;
1144 mAdbNotification.vibrate = null;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001145 }
1146
1147 Intent intent = new Intent(
1148 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
1149 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1150 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1151 // Note: we are hard-coding the component because this is
1152 // an important security UI that we don't want anyone
1153 // intercepting.
1154 intent.setComponent(new ComponentName("com.android.settings",
1155 "com.android.settings.DevelopmentSettings"));
1156 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
1157 intent, 0);
1158
1159 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001160
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001161 mAdbNotificationShown = true;
1162 notificationManager.notify(
1163 com.android.internal.R.string.adb_active_notification_title,
1164 mAdbNotification);
1165 }
1166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001167
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001168 } else if (mAdbNotificationShown) {
1169 NotificationManager notificationManager = (NotificationManager) mContext
1170 .getSystemService(Context.NOTIFICATION_SERVICE);
1171 if (notificationManager != null) {
1172 mAdbNotificationShown = false;
1173 notificationManager.cancel(
1174 com.android.internal.R.string.adb_active_notification_title);
1175 }
1176 }
1177 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001178
1179 private void updateNotificationPulse() {
1180 synchronized (mNotificationList) {
1181 updateLightsLocked();
1182 }
1183 }
1184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 // ======================================================================
1186 @Override
1187 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1188 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1189 != PackageManager.PERMISSION_GRANTED) {
1190 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1191 + Binder.getCallingPid()
1192 + ", uid=" + Binder.getCallingUid());
1193 return;
1194 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 pw.println("Current Notification Manager state:");
1197
1198 int N;
1199
1200 synchronized (mToastQueue) {
1201 N = mToastQueue.size();
1202 if (N > 0) {
1203 pw.println(" Toast Queue:");
1204 for (int i=0; i<N; i++) {
1205 mToastQueue.get(i).dump(pw, " ");
1206 }
1207 pw.println(" ");
1208 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
1211
1212 synchronized (mNotificationList) {
1213 N = mNotificationList.size();
1214 if (N > 0) {
1215 pw.println(" Notification List:");
1216 for (int i=0; i<N; i++) {
1217 mNotificationList.get(i).dump(pw, " ", mContext);
1218 }
1219 pw.println(" ");
1220 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 N = mLights.size();
1223 if (N > 0) {
1224 pw.println(" Lights List:");
1225 for (int i=0; i<N; i++) {
1226 mLights.get(i).dump(pw, " ", mContext);
1227 }
1228 pw.println(" ");
1229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 pw.println(" mSoundNotification=" + mSoundNotification);
1232 pw.println(" mSound=" + mSound);
1233 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001234 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1235 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 }
1237 }
1238}