| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 19 | import com.android.internal.statusbar.StatusBarNotification; |
| Joe Onorato | 7a0f36b | 2010-06-07 10:24:36 -0700 | [diff] [blame] | 20 | import com.android.server.StatusBarManagerService; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 21 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.app.ActivityManagerNative; |
| 23 | import android.app.IActivityManager; |
| 24 | import android.app.INotificationManager; |
| 25 | import android.app.ITransientNotification; |
| 26 | import android.app.Notification; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 27 | import android.app.NotificationManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.app.PendingIntent; |
| 29 | import android.app.StatusBarManager; |
| 30 | import android.content.BroadcastReceiver; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 31 | import android.content.ComponentName; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 32 | import android.content.ContentResolver; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.content.Context; |
| 34 | import android.content.Intent; |
| 35 | import android.content.IntentFilter; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 36 | import android.content.pm.ApplicationInfo; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.content.pm.PackageManager; |
| 38 | import android.content.pm.PackageManager.NameNotFoundException; |
| 39 | import android.content.res.Resources; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 40 | import android.database.ContentObserver; |
| Mike Lockwood | ff2544c | 2010-06-28 09:17:50 -0400 | [diff] [blame] | 41 | import android.hardware.Usb; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 42 | import android.media.AudioManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.net.Uri; |
| 44 | import android.os.BatteryManager; |
| Mike Lockwood | ff2544c | 2010-06-28 09:17:50 -0400 | [diff] [blame] | 45 | import android.os.Bundle; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.os.Binder; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.os.Handler; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.os.IBinder; |
| 49 | import android.os.Message; |
| 50 | import android.os.Power; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 51 | import android.os.Process; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 52 | import android.os.RemoteException; |
| Mike Lockwood | ed76037 | 2009-07-09 07:07:27 -0400 | [diff] [blame] | 53 | import android.os.SystemProperties; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.os.Vibrator; |
| 55 | import android.provider.Settings; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 56 | import android.telephony.TelephonyManager; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 57 | import android.text.TextUtils; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.util.EventLog; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 59 | import android.util.Slog; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.util.Log; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 61 | import android.view.accessibility.AccessibilityEvent; |
| 62 | import android.view.accessibility.AccessibilityManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.widget.Toast; |
| 64 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import java.io.FileDescriptor; |
| 66 | import java.io.PrintWriter; |
| 67 | import java.util.ArrayList; |
| 68 | import java.util.Arrays; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
| 70 | class NotificationManagerService extends INotificationManager.Stub |
| 71 | { |
| 72 | private static final String TAG = "NotificationService"; |
| 73 | private static final boolean DBG = false; |
| 74 | |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 75 | private static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
| 76 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | // message codes |
| 78 | private static final int MESSAGE_TIMEOUT = 2; |
| 79 | |
| 80 | private static final int LONG_DELAY = 3500; // 3.5 seconds |
| 81 | private static final int SHORT_DELAY = 2000; // 2 seconds |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 82 | |
| 83 | private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
| 85 | private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION; |
| 86 | |
| 87 | final Context mContext; |
| 88 | final IActivityManager mAm; |
| 89 | final IBinder mForegroundToken = new Binder(); |
| 90 | |
| 91 | private WorkerHandler mHandler; |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 92 | private StatusBarManagerService mStatusBar; |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 93 | private LightsService mLightsService; |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 94 | private LightsService.Light mBatteryLight; |
| 95 | private LightsService.Light mNotificationLight; |
| 96 | private LightsService.Light mAttentionLight; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 98 | private int mDefaultNotificationColor; |
| 99 | private int mDefaultNotificationLedOn; |
| 100 | private int mDefaultNotificationLedOff; |
| 101 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | private NotificationRecord mSoundNotification; |
| Jean-Michel Trivi | 211957f | 2010-03-26 18:19:33 -0700 | [diff] [blame] | 103 | private NotificationPlayer mSound; |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 104 | private boolean mSystemReady; |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 105 | private int mDisabledNotifications; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | |
| 107 | private NotificationRecord mVibrateNotification; |
| 108 | private Vibrator mVibrator = new Vibrator(); |
| 109 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 110 | // for enabling and disabling notification pulse behavior |
| 111 | private boolean mScreenOn = true; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 112 | private boolean mInCall = false; |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 113 | private boolean mNotificationPulseEnabled; |
| 114 | |
| 115 | // for adb connected notifications |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 116 | private boolean mAdbNotificationShown = false; |
| 117 | private Notification mAdbNotification; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 118 | |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 119 | private final ArrayList<NotificationRecord> mNotificationList = |
| 120 | new ArrayList<NotificationRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | |
| 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; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 130 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 131 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | 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 Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 164 | final String pkg; |
| 165 | final String tag; |
| 166 | final int id; |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 167 | final int uid; |
| 168 | final int initialPid; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | ITransientNotification callback; |
| 170 | int duration; |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 171 | final Notification notification; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | IBinder statusBarKey; |
| 173 | |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 174 | NotificationRecord(String pkg, String tag, int id, int uid, int initialPid, |
| 175 | Notification notification) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | { |
| 177 | this.pkg = pkg; |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 178 | this.tag = tag; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | this.id = id; |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 180 | this.uid = uid; |
| 181 | this.initialPid = initialPid; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | this.notification = notification; |
| 183 | } |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 184 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | void dump(PrintWriter pw, String prefix, Context baseContext) { |
| 186 | pw.println(prefix + this); |
| 187 | pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon) |
| 188 | + " / " + idDebugString(baseContext, this.pkg, notification.icon)); |
| 189 | pw.println(prefix + " contentIntent=" + notification.contentIntent); |
| 190 | pw.println(prefix + " deleteIntent=" + notification.deleteIntent); |
| 191 | pw.println(prefix + " tickerText=" + notification.tickerText); |
| 192 | pw.println(prefix + " contentView=" + notification.contentView); |
| 193 | pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults)); |
| 194 | pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags)); |
| 195 | pw.println(prefix + " sound=" + notification.sound); |
| 196 | pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate)); |
| 197 | pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB) |
| 198 | + " ledOnMS=" + notification.ledOnMS |
| 199 | + " ledOffMS=" + notification.ledOffMS); |
| 200 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 201 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | @Override |
| 203 | public final String toString() |
| 204 | { |
| 205 | return "NotificationRecord{" |
| 206 | + Integer.toHexString(System.identityHashCode(this)) |
| 207 | + " pkg=" + pkg |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 208 | + " id=" + Integer.toHexString(id) |
| 209 | + " tag=" + tag + "}"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | private static final class ToastRecord |
| 214 | { |
| 215 | final int pid; |
| 216 | final String pkg; |
| 217 | final ITransientNotification callback; |
| 218 | int duration; |
| 219 | |
| 220 | ToastRecord(int pid, String pkg, ITransientNotification callback, int duration) |
| 221 | { |
| 222 | this.pid = pid; |
| 223 | this.pkg = pkg; |
| 224 | this.callback = callback; |
| 225 | this.duration = duration; |
| 226 | } |
| 227 | |
| 228 | void update(int duration) { |
| 229 | this.duration = duration; |
| 230 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 231 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | void dump(PrintWriter pw, String prefix) { |
| 233 | pw.println(prefix + this); |
| 234 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 235 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | @Override |
| 237 | public final String toString() |
| 238 | { |
| 239 | return "ToastRecord{" |
| 240 | + Integer.toHexString(System.identityHashCode(this)) |
| 241 | + " pkg=" + pkg |
| 242 | + " callback=" + callback |
| 243 | + " duration=" + duration; |
| 244 | } |
| 245 | } |
| 246 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 247 | private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks |
| 248 | = new StatusBarManagerService.NotificationCallbacks() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | |
| 250 | public void onSetDisabled(int status) { |
| 251 | synchronized (mNotificationList) { |
| 252 | mDisabledNotifications = status; |
| 253 | if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) { |
| 254 | // cancel whatever's going on |
| 255 | long identity = Binder.clearCallingIdentity(); |
| 256 | try { |
| 257 | mSound.stop(); |
| 258 | } |
| 259 | finally { |
| 260 | Binder.restoreCallingIdentity(identity); |
| 261 | } |
| 262 | |
| 263 | identity = Binder.clearCallingIdentity(); |
| 264 | try { |
| 265 | mVibrator.cancel(); |
| 266 | } |
| 267 | finally { |
| 268 | Binder.restoreCallingIdentity(identity); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | public void onClearAll() { |
| 275 | cancelAll(); |
| 276 | } |
| 277 | |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 278 | public void onNotificationClick(String pkg, String tag, int id) { |
| 279 | cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL, |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 280 | Notification.FLAG_FOREGROUND_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | public void onPanelRevealed() { |
| 284 | synchronized (mNotificationList) { |
| 285 | // sound |
| 286 | mSoundNotification = null; |
| 287 | long identity = Binder.clearCallingIdentity(); |
| 288 | try { |
| 289 | mSound.stop(); |
| 290 | } |
| 291 | finally { |
| 292 | Binder.restoreCallingIdentity(identity); |
| 293 | } |
| 294 | |
| 295 | // vibrate |
| 296 | mVibrateNotification = null; |
| 297 | identity = Binder.clearCallingIdentity(); |
| 298 | try { |
| 299 | mVibrator.cancel(); |
| 300 | } |
| 301 | finally { |
| 302 | Binder.restoreCallingIdentity(identity); |
| 303 | } |
| 304 | |
| 305 | // light |
| 306 | mLights.clear(); |
| 307 | mLedNotification = null; |
| 308 | updateLightsLocked(); |
| 309 | } |
| 310 | } |
| Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 311 | |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 312 | public void onNotificationError(String pkg, String tag, int id, |
| 313 | int uid, int initialPid, String message) { |
| Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 314 | Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id); |
| 315 | cancelNotification(pkg, tag, id, 0, 0); |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 316 | long ident = Binder.clearCallingIdentity(); |
| 317 | try { |
| 318 | ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg, |
| 319 | "Bad notification posted from package " + pkg |
| 320 | + ": " + message); |
| 321 | } catch (RemoteException e) { |
| 322 | } |
| 323 | Binder.restoreCallingIdentity(ident); |
| Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 324 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 328 | @Override |
| 329 | public void onReceive(Context context, Intent intent) { |
| 330 | String action = intent.getAction(); |
| 331 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 332 | boolean queryRestart = false; |
| 333 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { |
| 335 | boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0); |
| 336 | int level = intent.getIntExtra("level", -1); |
| 337 | boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD); |
| 338 | int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN); |
| 339 | boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90); |
| 340 | |
| 341 | if (batteryCharging != mBatteryCharging || |
| 342 | batteryLow != mBatteryLow || |
| 343 | batteryFull != mBatteryFull) { |
| 344 | mBatteryCharging = batteryCharging; |
| 345 | mBatteryLow = batteryLow; |
| 346 | mBatteryFull = batteryFull; |
| 347 | updateLights(); |
| 348 | } |
| Mike Lockwood | ff2544c | 2010-06-28 09:17:50 -0400 | [diff] [blame] | 349 | } else if (action.equals(Usb.ACTION_USB_STATE)) { |
| 350 | Bundle extras = intent.getExtras(); |
| 351 | boolean usbConnected = extras.getBoolean(Usb.USB_CONNECTED); |
| 352 | boolean adbEnabled = (Usb.USB_FUNCTION_ENABLED.equals( |
| 353 | extras.getString(Usb.USB_FUNCTION_ADB))); |
| 354 | updateAdbNotification(usbConnected && adbEnabled); |
| 355 | } else if (action.equals(Usb.ACTION_USB_DISCONNECTED)) { |
| 356 | updateAdbNotification(false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED) |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 358 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 359 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 360 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 361 | String pkgList[] = null; |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 362 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 363 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 364 | } else if (queryRestart) { |
| 365 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 366 | } else { |
| 367 | Uri uri = intent.getData(); |
| 368 | if (uri == null) { |
| 369 | return; |
| 370 | } |
| 371 | String pkgName = uri.getSchemeSpecificPart(); |
| 372 | if (pkgName == null) { |
| 373 | return; |
| 374 | } |
| 375 | pkgList = new String[]{pkgName}; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 377 | if (pkgList != null && (pkgList.length > 0)) { |
| 378 | for (String pkgName : pkgList) { |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 379 | cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 380 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 381 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 382 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
| 383 | mScreenOn = true; |
| 384 | updateNotificationPulse(); |
| 385 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 386 | mScreenOn = false; |
| 387 | updateNotificationPulse(); |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 388 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
| 389 | mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)); |
| 390 | updateNotificationPulse(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | }; |
| 394 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 395 | class SettingsObserver extends ContentObserver { |
| 396 | SettingsObserver(Handler handler) { |
| 397 | super(handler); |
| 398 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 399 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 400 | void observe() { |
| 401 | ContentResolver resolver = mContext.getContentResolver(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 402 | resolver.registerContentObserver(Settings.System.getUriFor( |
| 403 | Settings.System.NOTIFICATION_LIGHT_PULSE), false, this); |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 404 | update(); |
| 405 | } |
| 406 | |
| 407 | @Override public void onChange(boolean selfChange) { |
| 408 | update(); |
| 409 | } |
| 410 | |
| 411 | public void update() { |
| 412 | ContentResolver resolver = mContext.getContentResolver(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 413 | boolean pulseEnabled = Settings.System.getInt(resolver, |
| 414 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0; |
| 415 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 416 | mNotificationPulseEnabled = pulseEnabled; |
| 417 | updateNotificationPulse(); |
| 418 | } |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 421 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 422 | NotificationManagerService(Context context, StatusBarManagerService statusBar, |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 423 | LightsService lights) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | { |
| 425 | super(); |
| 426 | mContext = context; |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 427 | mLightsService = lights; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | mAm = ActivityManagerNative.getDefault(); |
| Jean-Michel Trivi | 211957f | 2010-03-26 18:19:33 -0700 | [diff] [blame] | 429 | mSound = new NotificationPlayer(TAG); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | mSound.setUsesWakeLock(context); |
| 431 | mToastQueue = new ArrayList<ToastRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | mHandler = new WorkerHandler(); |
| San Mehat | 3ee1317 | 2010-02-04 20:54:43 -0800 | [diff] [blame] | 433 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 434 | mStatusBar = statusBar; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | statusBar.setNotificationCallbacks(mNotificationCallbacks); |
| 436 | |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 437 | mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY); |
| 438 | mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS); |
| 439 | mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION); |
| 440 | |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 441 | Resources resources = mContext.getResources(); |
| 442 | mDefaultNotificationColor = resources.getColor( |
| 443 | com.android.internal.R.color.config_defaultNotificationColor); |
| 444 | mDefaultNotificationLedOn = resources.getInteger( |
| 445 | com.android.internal.R.integer.config_defaultNotificationLedOn); |
| 446 | mDefaultNotificationLedOff = resources.getInteger( |
| 447 | com.android.internal.R.integer.config_defaultNotificationLedOff); |
| 448 | |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 449 | // Don't start allowing notifications until the setup wizard has run once. |
| 450 | // After that, including subsequent boots, init with notifications turned on. |
| 451 | // This works on the first boot because the setup wizard will toggle this |
| 452 | // flag at least once and we'll go back to 0 after that. |
| 453 | if (0 == Settings.Secure.getInt(mContext.getContentResolver(), |
| 454 | Settings.Secure.DEVICE_PROVISIONED, 0)) { |
| 455 | mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS; |
| 456 | } |
| 457 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | // register for battery changed notifications |
| 459 | IntentFilter filter = new IntentFilter(); |
| 460 | filter.addAction(Intent.ACTION_BATTERY_CHANGED); |
| Mike Lockwood | ff2544c | 2010-06-28 09:17:50 -0400 | [diff] [blame] | 461 | filter.addAction(Usb.ACTION_USB_STATE); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 462 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 463 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 464 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | mContext.registerReceiver(mIntentReceiver, filter); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 466 | IntentFilter pkgFilter = new IntentFilter(); |
| 467 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 468 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 469 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 470 | pkgFilter.addDataScheme("package"); |
| 471 | mContext.registerReceiver(mIntentReceiver, pkgFilter); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 472 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 473 | mContext.registerReceiver(mIntentReceiver, sdFilter); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 474 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 475 | SettingsObserver observer = new SettingsObserver(mHandler); |
| 476 | observer.observe(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | } |
| 478 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 479 | void systemReady() { |
| 480 | // no beeping until we're basically done booting |
| 481 | mSystemReady = true; |
| 482 | } |
| 483 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | // Toasts |
| 485 | // ============================================================================ |
| 486 | public void enqueueToast(String pkg, ITransientNotification callback, int duration) |
| 487 | { |
| Daniel Sandler | a703590 | 2010-03-30 15:45:31 -0400 | [diff] [blame] | 488 | if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | |
| 490 | if (pkg == null || callback == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 491 | Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | return ; |
| 493 | } |
| 494 | |
| 495 | synchronized (mToastQueue) { |
| 496 | int callingPid = Binder.getCallingPid(); |
| 497 | long callingId = Binder.clearCallingIdentity(); |
| 498 | try { |
| 499 | ToastRecord record; |
| 500 | int index = indexOfToastLocked(pkg, callback); |
| 501 | // If it's already in the queue, we update it in place, we don't |
| 502 | // move it to the end of the queue. |
| 503 | if (index >= 0) { |
| 504 | record = mToastQueue.get(index); |
| 505 | record.update(duration); |
| 506 | } else { |
| 507 | record = new ToastRecord(callingPid, pkg, callback, duration); |
| 508 | mToastQueue.add(record); |
| 509 | index = mToastQueue.size() - 1; |
| 510 | keepProcessAliveLocked(callingPid); |
| 511 | } |
| 512 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 513 | // new or just been updated. Call back and tell it to show itself. |
| 514 | // If the callback fails, this will remove it from the list, so don't |
| 515 | // assume that it's valid after this. |
| 516 | if (index == 0) { |
| 517 | showNextToastLocked(); |
| 518 | } |
| 519 | } finally { |
| 520 | Binder.restoreCallingIdentity(callingId); |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | public void cancelToast(String pkg, ITransientNotification callback) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 526 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | |
| 528 | if (pkg == null || callback == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 529 | Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | return ; |
| 531 | } |
| 532 | |
| 533 | synchronized (mToastQueue) { |
| 534 | long callingId = Binder.clearCallingIdentity(); |
| 535 | try { |
| 536 | int index = indexOfToastLocked(pkg, callback); |
| 537 | if (index >= 0) { |
| 538 | cancelToastLocked(index); |
| 539 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 540 | Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | } |
| 542 | } finally { |
| 543 | Binder.restoreCallingIdentity(callingId); |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | private void showNextToastLocked() { |
| 549 | ToastRecord record = mToastQueue.get(0); |
| 550 | while (record != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 551 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | try { |
| 553 | record.callback.show(); |
| 554 | scheduleTimeoutLocked(record, false); |
| 555 | return; |
| 556 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 557 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | + " in package " + record.pkg); |
| 559 | // remove it from the list and let the process die |
| 560 | int index = mToastQueue.indexOf(record); |
| 561 | if (index >= 0) { |
| 562 | mToastQueue.remove(index); |
| 563 | } |
| 564 | keepProcessAliveLocked(record.pid); |
| 565 | if (mToastQueue.size() > 0) { |
| 566 | record = mToastQueue.get(0); |
| 567 | } else { |
| 568 | record = null; |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | private void cancelToastLocked(int index) { |
| 575 | ToastRecord record = mToastQueue.get(index); |
| 576 | try { |
| 577 | record.callback.hide(); |
| 578 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 579 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | + " in package " + record.pkg); |
| 581 | // don't worry about this, we're about to remove it from |
| 582 | // the list anyway |
| 583 | } |
| 584 | mToastQueue.remove(index); |
| 585 | keepProcessAliveLocked(record.pid); |
| 586 | if (mToastQueue.size() > 0) { |
| 587 | // Show the next one. If the callback fails, this will remove |
| 588 | // it from the list, so don't assume that the list hasn't changed |
| 589 | // after this point. |
| 590 | showNextToastLocked(); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | private void scheduleTimeoutLocked(ToastRecord r, boolean immediate) |
| 595 | { |
| 596 | Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r); |
| 597 | long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY); |
| 598 | mHandler.removeCallbacksAndMessages(r); |
| 599 | mHandler.sendMessageDelayed(m, delay); |
| 600 | } |
| 601 | |
| 602 | private void handleTimeout(ToastRecord record) |
| 603 | { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 604 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | synchronized (mToastQueue) { |
| 606 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 607 | if (index >= 0) { |
| 608 | cancelToastLocked(index); |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | // lock on mToastQueue |
| 614 | private int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 615 | { |
| 616 | IBinder cbak = callback.asBinder(); |
| 617 | ArrayList<ToastRecord> list = mToastQueue; |
| 618 | int len = list.size(); |
| 619 | for (int i=0; i<len; i++) { |
| 620 | ToastRecord r = list.get(i); |
| 621 | if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) { |
| 622 | return i; |
| 623 | } |
| 624 | } |
| 625 | return -1; |
| 626 | } |
| 627 | |
| 628 | // lock on mToastQueue |
| 629 | private void keepProcessAliveLocked(int pid) |
| 630 | { |
| 631 | int toastCount = 0; // toasts from this pid |
| 632 | ArrayList<ToastRecord> list = mToastQueue; |
| 633 | int N = list.size(); |
| 634 | for (int i=0; i<N; i++) { |
| 635 | ToastRecord r = list.get(i); |
| 636 | if (r.pid == pid) { |
| 637 | toastCount++; |
| 638 | } |
| 639 | } |
| 640 | try { |
| 641 | mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0); |
| 642 | } catch (RemoteException e) { |
| 643 | // Shouldn't happen. |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | private final class WorkerHandler extends Handler |
| 648 | { |
| 649 | @Override |
| 650 | public void handleMessage(Message msg) |
| 651 | { |
| 652 | switch (msg.what) |
| 653 | { |
| 654 | case MESSAGE_TIMEOUT: |
| 655 | handleTimeout((ToastRecord)msg.obj); |
| 656 | break; |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | |
| 662 | // Notifications |
| 663 | // ============================================================================ |
| 664 | public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut) |
| 665 | { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 666 | enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut); |
| 667 | } |
| 668 | |
| 669 | public void enqueueNotificationWithTag(String pkg, String tag, int id, |
| 670 | Notification notification, int[] idOut) |
| 671 | { |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 672 | final int callingUid = Binder.getCallingUid(); |
| 673 | final int callingPid = Binder.getCallingPid(); |
| 674 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 675 | checkIncomingCall(pkg); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 676 | |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 677 | // Limit the number of notifications that any given package except the android |
| 678 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| 679 | if (!"android".equals(pkg)) { |
| 680 | synchronized (mNotificationList) { |
| 681 | int count = 0; |
| 682 | final int N = mNotificationList.size(); |
| 683 | for (int i=0; i<N; i++) { |
| 684 | final NotificationRecord r = mNotificationList.get(i); |
| 685 | if (r.pkg.equals(pkg)) { |
| 686 | count++; |
| 687 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 688 | Slog.e(TAG, "Package has already posted " + count |
| 689 | + " notifications. Not showing more. package=" + pkg); |
| 690 | return; |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | // This conditional is a dirty hack to limit the logging done on |
| 698 | // behalf of the download manager without affecting other apps. |
| 699 | if (!pkg.equals("com.android.providers.downloads") |
| 700 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 701 | EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | if (pkg == null || notification == null) { |
| 705 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 706 | + " id=" + id + " notification=" + notification); |
| 707 | } |
| 708 | if (notification.icon != 0) { |
| 709 | if (notification.contentView == null) { |
| 710 | throw new IllegalArgumentException("contentView required: pkg=" + pkg |
| 711 | + " id=" + id + " notification=" + notification); |
| 712 | } |
| 713 | if (notification.contentIntent == null) { |
| 714 | throw new IllegalArgumentException("contentIntent required: pkg=" + pkg |
| 715 | + " id=" + id + " notification=" + notification); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | synchronized (mNotificationList) { |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 720 | NotificationRecord r = new NotificationRecord(pkg, tag, id, |
| 721 | callingUid, callingPid, notification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | NotificationRecord old = null; |
| 723 | |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 724 | int index = indexOfNotificationLocked(pkg, tag, id); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 725 | if (index < 0) { |
| 726 | mNotificationList.add(r); |
| 727 | } else { |
| 728 | old = mNotificationList.remove(index); |
| 729 | mNotificationList.add(index, r); |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 730 | // Make sure we don't lose the foreground service state. |
| 731 | if (old != null) { |
| 732 | notification.flags |= |
| 733 | old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE; |
| 734 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 736 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 737 | // Ensure if this is a foreground service that the proper additional |
| 738 | // flags are set. |
| 739 | if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 740 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 741 | | Notification.FLAG_NO_CLEAR; |
| 742 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 743 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 744 | if (notification.icon != 0) { |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 745 | StatusBarNotification n = new StatusBarNotification(pkg, id, tag, |
| 746 | r.uid, r.initialPid, notification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 747 | if (old != null && old.statusBarKey != null) { |
| 748 | r.statusBarKey = old.statusBarKey; |
| 749 | long identity = Binder.clearCallingIdentity(); |
| 750 | try { |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 751 | mStatusBar.updateNotification(r.statusBarKey, n); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 752 | } |
| 753 | finally { |
| 754 | Binder.restoreCallingIdentity(identity); |
| 755 | } |
| 756 | } else { |
| 757 | long identity = Binder.clearCallingIdentity(); |
| 758 | try { |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 759 | r.statusBarKey = mStatusBar.addNotification(n); |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 760 | mAttentionLight.pulse(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 761 | } |
| 762 | finally { |
| 763 | Binder.restoreCallingIdentity(identity); |
| 764 | } |
| 765 | } |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 766 | sendAccessibilityEvent(notification, pkg); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 767 | } else { |
| 768 | if (old != null && old.statusBarKey != null) { |
| 769 | long identity = Binder.clearCallingIdentity(); |
| 770 | try { |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 771 | mStatusBar.removeNotification(old.statusBarKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | } |
| 773 | finally { |
| 774 | Binder.restoreCallingIdentity(identity); |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | // If we're not supposed to beep, vibrate, etc. then don't. |
| 780 | if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0) |
| 781 | && (!(old != null |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 782 | && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 )) |
| 783 | && mSystemReady) { |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 784 | |
| 785 | final AudioManager audioManager = (AudioManager) mContext |
| 786 | .getSystemService(Context.AUDIO_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 787 | // sound |
| 788 | final boolean useDefaultSound = |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 789 | (notification.defaults & Notification.DEFAULT_SOUND) != 0; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 790 | if (useDefaultSound || notification.sound != null) { |
| 791 | Uri uri; |
| 792 | if (useDefaultSound) { |
| 793 | uri = Settings.System.DEFAULT_NOTIFICATION_URI; |
| 794 | } else { |
| 795 | uri = notification.sound; |
| 796 | } |
| 797 | boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0; |
| 798 | int audioStreamType; |
| 799 | if (notification.audioStreamType >= 0) { |
| 800 | audioStreamType = notification.audioStreamType; |
| 801 | } else { |
| 802 | audioStreamType = DEFAULT_STREAM_TYPE; |
| 803 | } |
| 804 | mSoundNotification = r; |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 805 | // do not play notifications if stream volume is 0 |
| 806 | // (typically because ringer mode is silent). |
| 807 | if (audioManager.getStreamVolume(audioStreamType) != 0) { |
| 808 | long identity = Binder.clearCallingIdentity(); |
| 809 | try { |
| 810 | mSound.play(mContext, uri, looping, audioStreamType); |
| 811 | } |
| 812 | finally { |
| 813 | Binder.restoreCallingIdentity(identity); |
| 814 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
| 818 | // vibrate |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | final boolean useDefaultVibrate = |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 820 | (notification.defaults & Notification.DEFAULT_VIBRATE) != 0; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | if ((useDefaultVibrate || notification.vibrate != null) |
| 822 | && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) { |
| 823 | mVibrateNotification = r; |
| 824 | |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 825 | mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | : notification.vibrate, |
| 827 | ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | // this option doesn't shut off the lights |
| 832 | |
| 833 | // light |
| 834 | // the most recent thing gets the light |
| 835 | mLights.remove(old); |
| 836 | if (mLedNotification == old) { |
| 837 | mLedNotification = null; |
| 838 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 839 | //Slog.i(TAG, "notification.lights=" |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 840 | // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0)); |
| 841 | if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) { |
| 842 | mLights.add(r); |
| 843 | updateLightsLocked(); |
| 844 | } else { |
| 845 | if (old != null |
| 846 | && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) { |
| 847 | updateLightsLocked(); |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | idOut[0] = id; |
| 853 | } |
| 854 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 855 | private void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 856 | AccessibilityManager manager = AccessibilityManager.getInstance(mContext); |
| 857 | if (!manager.isEnabled()) { |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | AccessibilityEvent event = |
| 862 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 863 | event.setPackageName(packageName); |
| 864 | event.setClassName(Notification.class.getName()); |
| 865 | event.setParcelableData(notification); |
| 866 | CharSequence tickerText = notification.tickerText; |
| 867 | if (!TextUtils.isEmpty(tickerText)) { |
| 868 | event.getText().add(tickerText); |
| 869 | } |
| 870 | |
| 871 | manager.sendAccessibilityEvent(event); |
| 872 | } |
| 873 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | private void cancelNotificationLocked(NotificationRecord r) { |
| 875 | // status bar |
| 876 | if (r.notification.icon != 0) { |
| 877 | long identity = Binder.clearCallingIdentity(); |
| 878 | try { |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 879 | mStatusBar.removeNotification(r.statusBarKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | } |
| 881 | finally { |
| 882 | Binder.restoreCallingIdentity(identity); |
| 883 | } |
| 884 | r.statusBarKey = null; |
| 885 | } |
| 886 | |
| 887 | // sound |
| 888 | if (mSoundNotification == r) { |
| 889 | mSoundNotification = null; |
| 890 | long identity = Binder.clearCallingIdentity(); |
| 891 | try { |
| 892 | mSound.stop(); |
| 893 | } |
| 894 | finally { |
| 895 | Binder.restoreCallingIdentity(identity); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | // vibrate |
| 900 | if (mVibrateNotification == r) { |
| 901 | mVibrateNotification = null; |
| 902 | long identity = Binder.clearCallingIdentity(); |
| 903 | try { |
| 904 | mVibrator.cancel(); |
| 905 | } |
| 906 | finally { |
| 907 | Binder.restoreCallingIdentity(identity); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | // light |
| 912 | mLights.remove(r); |
| 913 | if (mLedNotification == r) { |
| 914 | mLedNotification = null; |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | /** |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 919 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 920 | * and none of the {@code mustNotHaveFlags}. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | */ |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 922 | private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags, |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 923 | int mustNotHaveFlags) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 924 | EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | |
| 926 | synchronized (mNotificationList) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 927 | int index = indexOfNotificationLocked(pkg, tag, id); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | if (index >= 0) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 929 | NotificationRecord r = mNotificationList.get(index); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 930 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 931 | if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) { |
| 932 | return; |
| 933 | } |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 934 | if ((r.notification.flags & mustNotHaveFlags) != 0) { |
| 935 | return; |
| 936 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 937 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | mNotificationList.remove(index); |
| 939 | |
| 940 | cancelNotificationLocked(r); |
| 941 | updateLightsLocked(); |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | /** |
| 947 | * Cancels all notifications from a given package that have all of the |
| 948 | * {@code mustHaveFlags}. |
| 949 | */ |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 950 | boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags, |
| 951 | int mustNotHaveFlags, boolean doit) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 952 | EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 953 | |
| 954 | synchronized (mNotificationList) { |
| 955 | final int N = mNotificationList.size(); |
| 956 | boolean canceledSomething = false; |
| 957 | for (int i = N-1; i >= 0; --i) { |
| 958 | NotificationRecord r = mNotificationList.get(i); |
| 959 | if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) { |
| 960 | continue; |
| 961 | } |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 962 | if ((r.notification.flags & mustNotHaveFlags) != 0) { |
| 963 | continue; |
| 964 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 965 | if (!r.pkg.equals(pkg)) { |
| 966 | continue; |
| 967 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 968 | canceledSomething = true; |
| 969 | if (!doit) { |
| 970 | return true; |
| 971 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 972 | mNotificationList.remove(i); |
| 973 | cancelNotificationLocked(r); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 974 | } |
| 975 | if (canceledSomething) { |
| 976 | updateLightsLocked(); |
| 977 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 978 | return canceledSomething; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 982 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 983 | public void cancelNotification(String pkg, int id) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 984 | cancelNotificationWithTag(pkg, null /* tag */, id); |
| 985 | } |
| 986 | |
| 987 | public void cancelNotificationWithTag(String pkg, String tag, int id) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 988 | checkIncomingCall(pkg); |
| 989 | // Don't allow client applications to cancel foreground service notis. |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 990 | cancelNotification(pkg, tag, id, 0, |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 991 | Binder.getCallingUid() == Process.SYSTEM_UID |
| 992 | ? 0 : Notification.FLAG_FOREGROUND_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | } |
| 994 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 995 | public void cancelAllNotifications(String pkg) { |
| 996 | checkIncomingCall(pkg); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 997 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 998 | // Calling from user space, don't allow the canceling of actively |
| 999 | // running foreground services. |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1000 | cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | } |
| 1002 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1003 | void checkIncomingCall(String pkg) { |
| 1004 | int uid = Binder.getCallingUid(); |
| 1005 | if (uid == Process.SYSTEM_UID || uid == 0) { |
| 1006 | return; |
| 1007 | } |
| 1008 | try { |
| 1009 | ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo( |
| 1010 | pkg, 0); |
| 1011 | if (ai.uid != uid) { |
| 1012 | throw new SecurityException("Calling uid " + uid + " gave package" |
| 1013 | + pkg + " which is owned by uid " + ai.uid); |
| 1014 | } |
| 1015 | } catch (PackageManager.NameNotFoundException e) { |
| 1016 | throw new SecurityException("Unknown package " + pkg); |
| 1017 | } |
| 1018 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1019 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1020 | void cancelAll() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1021 | synchronized (mNotificationList) { |
| 1022 | final int N = mNotificationList.size(); |
| 1023 | for (int i=N-1; i>=0; i--) { |
| 1024 | NotificationRecord r = mNotificationList.get(i); |
| 1025 | |
| 1026 | if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT |
| 1027 | | Notification.FLAG_NO_CLEAR)) == 0) { |
| 1028 | if (r.notification.deleteIntent != null) { |
| 1029 | try { |
| 1030 | r.notification.deleteIntent.send(); |
| 1031 | } catch (PendingIntent.CanceledException ex) { |
| 1032 | // do nothing - there's no relevant way to recover, and |
| 1033 | // no reason to let this propagate |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1034 | Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | mNotificationList.remove(i); |
| 1038 | cancelNotificationLocked(r); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | updateLightsLocked(); |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | private void updateLights() { |
| 1047 | synchronized (mNotificationList) { |
| 1048 | updateLightsLocked(); |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | // lock on mNotificationList |
| 1053 | private void updateLightsLocked() |
| 1054 | { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1055 | // Battery low always shows, other states only show if charging. |
| 1056 | if (mBatteryLow) { |
| Mike Lockwood | 445f430 | 2009-09-04 11:06:46 -0400 | [diff] [blame] | 1057 | if (mBatteryCharging) { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1058 | mBatteryLight.setColor(BATTERY_LOW_ARGB); |
| Mike Lockwood | 445f430 | 2009-09-04 11:06:46 -0400 | [diff] [blame] | 1059 | } else { |
| 1060 | // Flash when battery is low and not charging |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1061 | mBatteryLight.setFlashing(BATTERY_LOW_ARGB, LightsService.LIGHT_FLASH_TIMED, |
| 1062 | BATTERY_BLINK_ON, BATTERY_BLINK_OFF); |
| Mike Lockwood | 445f430 | 2009-09-04 11:06:46 -0400 | [diff] [blame] | 1063 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | } else if (mBatteryCharging) { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1065 | if (mBatteryFull) { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1066 | mBatteryLight.setColor(BATTERY_FULL_ARGB); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | } else { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1068 | mBatteryLight.setColor(BATTERY_MEDIUM_ARGB); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1069 | } |
| 1070 | } else { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1071 | mBatteryLight.turnOff(); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1072 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1074 | // handle notification lights |
| 1075 | if (mLedNotification == null) { |
| 1076 | // get next notification, if any |
| 1077 | int n = mLights.size(); |
| 1078 | if (n > 0) { |
| 1079 | mLedNotification = mLights.get(n-1); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | } |
| 1081 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1082 | |
| 1083 | // we only flash if screen is off and persistent pulsing is enabled |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1084 | // and we are not currently in a call |
| 1085 | if (mLedNotification == null || mScreenOn || mInCall) { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1086 | mNotificationLight.turnOff(); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1087 | } else { |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 1088 | int ledARGB = mLedNotification.notification.ledARGB; |
| 1089 | int ledOnMS = mLedNotification.notification.ledOnMS; |
| 1090 | int ledOffMS = mLedNotification.notification.ledOffMS; |
| 1091 | if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) { |
| 1092 | ledARGB = mDefaultNotificationColor; |
| 1093 | ledOnMS = mDefaultNotificationLedOn; |
| 1094 | ledOffMS = mDefaultNotificationLedOff; |
| 1095 | } |
| 1096 | if (mNotificationPulseEnabled) { |
| 1097 | // pulse repeatedly |
| 1098 | mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED, |
| 1099 | ledOnMS, ledOffMS); |
| 1100 | } else { |
| 1101 | // pulse only once |
| 1102 | mNotificationLight.pulse(ledARGB, ledOnMS); |
| 1103 | } |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1104 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | // lock on mNotificationList |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1108 | private int indexOfNotificationLocked(String pkg, String tag, int id) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1109 | { |
| 1110 | ArrayList<NotificationRecord> list = mNotificationList; |
| 1111 | final int len = list.size(); |
| 1112 | for (int i=0; i<len; i++) { |
| 1113 | NotificationRecord r = list.get(i); |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1114 | if (tag == null) { |
| 1115 | if (r.tag != null) { |
| 1116 | continue; |
| 1117 | } |
| 1118 | } else { |
| 1119 | if (!tag.equals(r.tag)) { |
| 1120 | continue; |
| 1121 | } |
| 1122 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1123 | if (r.id == id && r.pkg.equals(pkg)) { |
| 1124 | return i; |
| 1125 | } |
| 1126 | } |
| 1127 | return -1; |
| 1128 | } |
| 1129 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1130 | // This is here instead of StatusBarPolicy because it is an important |
| 1131 | // security feature that we don't want people customizing the platform |
| 1132 | // to accidentally lose. |
| Mike Lockwood | ff2544c | 2010-06-28 09:17:50 -0400 | [diff] [blame] | 1133 | private void updateAdbNotification(boolean adbEnabled) { |
| 1134 | if (adbEnabled) { |
| Mike Lockwood | ed76037 | 2009-07-09 07:07:27 -0400 | [diff] [blame] | 1135 | if ("0".equals(SystemProperties.get("persist.adb.notify"))) { |
| 1136 | return; |
| 1137 | } |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1138 | if (!mAdbNotificationShown) { |
| 1139 | NotificationManager notificationManager = (NotificationManager) mContext |
| 1140 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 1141 | if (notificationManager != null) { |
| 1142 | Resources r = mContext.getResources(); |
| 1143 | CharSequence title = r.getText( |
| 1144 | com.android.internal.R.string.adb_active_notification_title); |
| 1145 | CharSequence message = r.getText( |
| 1146 | com.android.internal.R.string.adb_active_notification_message); |
| 1147 | |
| 1148 | if (mAdbNotification == null) { |
| 1149 | mAdbNotification = new Notification(); |
| Daniel Sandler | 39576c8 | 2010-03-25 16:02:33 -0400 | [diff] [blame] | 1150 | mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1151 | mAdbNotification.when = 0; |
| 1152 | mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1153 | mAdbNotification.tickerText = title; |
| Daniel Sandler | 39576c8 | 2010-03-25 16:02:33 -0400 | [diff] [blame] | 1154 | mAdbNotification.defaults = 0; // please be quiet |
| 1155 | mAdbNotification.sound = null; |
| 1156 | mAdbNotification.vibrate = null; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | Intent intent = new Intent( |
| 1160 | Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS); |
| 1161 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | |
| 1162 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 1163 | // Note: we are hard-coding the component because this is |
| 1164 | // an important security UI that we don't want anyone |
| 1165 | // intercepting. |
| 1166 | intent.setComponent(new ComponentName("com.android.settings", |
| 1167 | "com.android.settings.DevelopmentSettings")); |
| 1168 | PendingIntent pi = PendingIntent.getActivity(mContext, 0, |
| 1169 | intent, 0); |
| 1170 | |
| 1171 | mAdbNotification.setLatestEventInfo(mContext, title, message, pi); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1172 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1173 | mAdbNotificationShown = true; |
| 1174 | notificationManager.notify( |
| 1175 | com.android.internal.R.string.adb_active_notification_title, |
| 1176 | mAdbNotification); |
| 1177 | } |
| 1178 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1179 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1180 | } else if (mAdbNotificationShown) { |
| 1181 | NotificationManager notificationManager = (NotificationManager) mContext |
| 1182 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 1183 | if (notificationManager != null) { |
| 1184 | mAdbNotificationShown = false; |
| 1185 | notificationManager.cancel( |
| 1186 | com.android.internal.R.string.adb_active_notification_title); |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1190 | |
| 1191 | private void updateNotificationPulse() { |
| 1192 | synchronized (mNotificationList) { |
| 1193 | updateLightsLocked(); |
| 1194 | } |
| 1195 | } |
| 1196 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1197 | // ====================================================================== |
| 1198 | @Override |
| 1199 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1200 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1201 | != PackageManager.PERMISSION_GRANTED) { |
| 1202 | pw.println("Permission Denial: can't dump NotificationManager from from pid=" |
| 1203 | + Binder.getCallingPid() |
| 1204 | + ", uid=" + Binder.getCallingUid()); |
| 1205 | return; |
| 1206 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1207 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | pw.println("Current Notification Manager state:"); |
| 1209 | |
| 1210 | int N; |
| 1211 | |
| 1212 | synchronized (mToastQueue) { |
| 1213 | N = mToastQueue.size(); |
| 1214 | if (N > 0) { |
| 1215 | pw.println(" Toast Queue:"); |
| 1216 | for (int i=0; i<N; i++) { |
| 1217 | mToastQueue.get(i).dump(pw, " "); |
| 1218 | } |
| 1219 | pw.println(" "); |
| 1220 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1221 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | synchronized (mNotificationList) { |
| 1225 | N = mNotificationList.size(); |
| 1226 | if (N > 0) { |
| 1227 | pw.println(" Notification List:"); |
| 1228 | for (int i=0; i<N; i++) { |
| 1229 | mNotificationList.get(i).dump(pw, " ", mContext); |
| 1230 | } |
| 1231 | pw.println(" "); |
| 1232 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1233 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1234 | N = mLights.size(); |
| 1235 | if (N > 0) { |
| 1236 | pw.println(" Lights List:"); |
| 1237 | for (int i=0; i<N; i++) { |
| 1238 | mLights.get(i).dump(pw, " ", mContext); |
| 1239 | } |
| 1240 | pw.println(" "); |
| 1241 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1242 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1243 | pw.println(" mSoundNotification=" + mSoundNotification); |
| 1244 | pw.println(" mSound=" + mSound); |
| 1245 | pw.println(" mVibrateNotification=" + mVibrateNotification); |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1246 | pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications)); |
| 1247 | pw.println(" mSystemReady=" + mSystemReady); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1248 | } |
| 1249 | } |
| 1250 | } |