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