blob: 8d6c60446a8a20a99fd5b91c124ac837887ca6af [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Jeff Sharkey098d5802012-04-26 17:30:34 -070019import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
20import static org.xmlpull.v1.XmlPullParser.END_TAG;
21import static org.xmlpull.v1.XmlPullParser.START_TAG;
svetoslavganov75986cf2009-05-14 22:28:01 -070022
Dianne Hackborn41203752012-08-31 14:05:51 -070023import android.app.ActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.app.ActivityManagerNative;
Amith Yamasanif203aee2012-08-29 18:41:53 -070025import android.app.AppGlobals;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.app.IActivityManager;
27import android.app.INotificationManager;
28import android.app.ITransientNotification;
29import android.app.Notification;
30import android.app.PendingIntent;
31import android.app.StatusBarManager;
32import android.content.BroadcastReceiver;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070033import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.Context;
35import android.content.Intent;
36import android.content.IntentFilter;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070037import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.pm.PackageManager;
39import android.content.pm.PackageManager.NameNotFoundException;
40import android.content.res.Resources;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070041import android.database.ContentObserver;
svetoslavganov75986cf2009-05-14 22:28:01 -070042import android.media.AudioManager;
Jeff Sharkey098d5802012-04-26 17:30:34 -070043import android.media.IAudioService;
44import android.media.IRingtonePlayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.IBinder;
49import android.os.Message;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070050import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070051import android.os.RemoteException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070052import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070053import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Vibrator;
55import android.provider.Settings;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050056import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.text.TextUtils;
Dianne Hackborn39606a02012-07-31 17:54:35 -070058import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.util.EventLog;
60import android.util.Log;
Andy Stadler110988c2010-12-03 14:29:16 -080061import android.util.Slog;
Daniel Sandler0da673f2012-04-11 12:33:16 -040062import android.util.Xml;
svetoslavganov75986cf2009-05-14 22:28:01 -070063import android.view.accessibility.AccessibilityEvent;
64import android.view.accessibility.AccessibilityManager;
Jeff Sharkey6d515712012-09-20 16:06:08 -070065import android.widget.RemoteViews;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.widget.Toast;
67
Jeff Sharkey098d5802012-04-26 17:30:34 -070068import com.android.internal.statusbar.StatusBarNotification;
69import com.android.internal.util.FastXmlSerializer;
70
71import org.xmlpull.v1.XmlPullParser;
72import org.xmlpull.v1.XmlPullParserException;
73import org.xmlpull.v1.XmlSerializer;
74
Daniel Sandler0da673f2012-04-11 12:33:16 -040075import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import java.io.FileDescriptor;
Daniel Sandler0da673f2012-04-11 12:33:16 -040077import java.io.FileInputStream;
78import java.io.FileNotFoundException;
79import java.io.FileOutputStream;
80import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import java.io.PrintWriter;
82import java.util.ArrayList;
83import java.util.Arrays;
Daniel Sandler0da673f2012-04-11 12:33:16 -040084import java.util.HashSet;
85
86import libcore.io.IoUtils;
87
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040089/** {@hide} */
90public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091{
92 private static final String TAG = "NotificationService";
93 private static final boolean DBG = false;
94
Joe Onoratobd73d012010-06-04 11:44:54 -070095 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
96
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 // message codes
98 private static final int MESSAGE_TIMEOUT = 2;
99
100 private static final int LONG_DELAY = 3500; // 3.5 seconds
101 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800102
103 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
105 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400106 private static final boolean SCORE_ONGOING_HIGHER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
Daniel Sandler0da673f2012-04-11 12:33:16 -0400108 private static final int JUNK_SCORE = -1000;
109 private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10;
110 private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER;
111
112 private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true;
113 private static final boolean ENABLE_BLOCKED_TOASTS = true;
114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 final Context mContext;
116 final IActivityManager mAm;
117 final IBinder mForegroundToken = new Binder();
118
119 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -0700120 private StatusBarManagerService mStatusBar;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500121 private LightsService.Light mNotificationLight;
122 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
Mike Lockwood670f9322010-01-20 12:13:36 -0500124 private int mDefaultNotificationColor;
125 private int mDefaultNotificationLedOn;
126 private int mDefaultNotificationLedOff;
127
Joe Onorato30275482009-07-08 17:09:14 -0700128 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400129 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130
Jeff Sharkey098d5802012-04-26 17:30:34 -0700131 private NotificationRecord mSoundNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 private NotificationRecord mVibrateNotification;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700133
134 private IAudioService mAudioService;
Jeff Brownc2346132012-04-13 01:55:38 -0700135 private Vibrator mVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500137 // for enabling and disabling notification pulse behavior
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400138 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500139 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500140 private boolean mNotificationPulseEnabled;
141
Fred Quintana6ecaff12009-09-25 14:23:13 -0700142 private final ArrayList<NotificationRecord> mNotificationList =
143 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
145 private ArrayList<ToastRecord> mToastQueue;
146
147 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700149
Daniel Sandler0da673f2012-04-11 12:33:16 -0400150 // Notification control database. For now just contains disabled packages.
151 private AtomicFile mPolicyFile;
152 private HashSet<String> mBlockedPackages = new HashSet<String>();
153
154 private static final int DB_VERSION = 1;
155
156 private static final String TAG_BODY = "notification-policy";
157 private static final String ATTR_VERSION = "version";
158
159 private static final String TAG_BLOCKED_PKGS = "blocked-packages";
160 private static final String TAG_PACKAGE = "package";
161 private static final String ATTR_NAME = "name";
162
163 private void loadBlockDb() {
164 synchronized(mBlockedPackages) {
165 if (mPolicyFile == null) {
166 File dir = new File("/data/system");
167 mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml"));
168
169 mBlockedPackages.clear();
170
171 FileInputStream infile = null;
172 try {
173 infile = mPolicyFile.openRead();
174 final XmlPullParser parser = Xml.newPullParser();
175 parser.setInput(infile, null);
176
177 int type;
178 String tag;
179 int version = DB_VERSION;
180 while ((type = parser.next()) != END_DOCUMENT) {
181 tag = parser.getName();
182 if (type == START_TAG) {
183 if (TAG_BODY.equals(tag)) {
184 version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
185 } else if (TAG_BLOCKED_PKGS.equals(tag)) {
186 while ((type = parser.next()) != END_DOCUMENT) {
187 tag = parser.getName();
188 if (TAG_PACKAGE.equals(tag)) {
189 mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME));
190 } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) {
191 break;
192 }
193 }
194 }
195 }
196 }
197 } catch (FileNotFoundException e) {
198 // No data yet
199 } catch (IOException e) {
200 Log.wtf(TAG, "Unable to read blocked notifications database", e);
201 } catch (NumberFormatException e) {
202 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
203 } catch (XmlPullParserException e) {
204 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
205 } finally {
206 IoUtils.closeQuietly(infile);
207 }
208 }
209 }
210 }
211
212 private void writeBlockDb() {
213 synchronized(mBlockedPackages) {
214 FileOutputStream outfile = null;
215 try {
216 outfile = mPolicyFile.startWrite();
217
218 XmlSerializer out = new FastXmlSerializer();
219 out.setOutput(outfile, "utf-8");
220
221 out.startDocument(null, true);
222
223 out.startTag(null, TAG_BODY); {
224 out.attribute(null, ATTR_VERSION, String.valueOf(DB_VERSION));
225 out.startTag(null, TAG_BLOCKED_PKGS); {
226 // write all known network policies
227 for (String pkg : mBlockedPackages) {
228 out.startTag(null, TAG_PACKAGE); {
229 out.attribute(null, ATTR_NAME, pkg);
230 } out.endTag(null, TAG_PACKAGE);
231 }
232 } out.endTag(null, TAG_BLOCKED_PKGS);
233 } out.endTag(null, TAG_BODY);
234
235 out.endDocument();
236
237 mPolicyFile.finishWrite(outfile);
238 } catch (IOException e) {
239 if (outfile != null) {
240 mPolicyFile.failWrite(outfile);
241 }
242 }
243 }
244 }
245
246 public boolean areNotificationsEnabledForPackage(String pkg) {
247 checkCallerIsSystem();
248 return areNotificationsEnabledForPackageInt(pkg);
249 }
250
251 // Unchecked. Not exposed via Binder, but can be called in the course of enqueue*().
252 private boolean areNotificationsEnabledForPackageInt(String pkg) {
253 final boolean enabled = !mBlockedPackages.contains(pkg);
254 if (DBG) {
255 Slog.v(TAG, "notifications are " + (enabled?"en":"dis") + "abled for " + pkg);
256 }
257 return enabled;
258 }
259
260 public void setNotificationsEnabledForPackage(String pkg, boolean enabled) {
261 checkCallerIsSystem();
262 if (DBG) {
263 Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);
264 }
265 if (enabled) {
266 mBlockedPackages.remove(pkg);
267 } else {
268 mBlockedPackages.add(pkg);
269
270 // Now, cancel any outstanding notifications that are part of a just-disabled app
271 if (ENABLE_BLOCKED_NOTIFICATIONS) {
272 synchronized (mNotificationList) {
273 final int N = mNotificationList.size();
274 for (int i=0; i<N; i++) {
275 final NotificationRecord r = mNotificationList.get(i);
276 if (r.pkg.equals(pkg)) {
277 cancelNotificationLocked(r, false);
278 }
279 }
280 }
281 }
282 // Don't bother canceling toasts, they'll go away soon enough.
283 }
284 writeBlockDb();
285 }
286
287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 private static String idDebugString(Context baseContext, String packageName, int id) {
289 Context c = null;
290
291 if (packageName != null) {
292 try {
293 c = baseContext.createPackageContext(packageName, 0);
294 } catch (NameNotFoundException e) {
295 c = baseContext;
296 }
297 } else {
298 c = baseContext;
299 }
300
301 String pkg;
302 String type;
303 String name;
304
305 Resources r = c.getResources();
306 try {
307 return r.getResourceName(id);
308 } catch (Resources.NotFoundException e) {
309 return "<name unknown>";
310 }
311 }
312
313 private static final class NotificationRecord
314 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700315 final String pkg;
316 final String tag;
317 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700318 final int uid;
319 final int initialPid;
Dianne Hackborn41203752012-08-31 14:05:51 -0700320 final int userId;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700321 final Notification notification;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500322 final int score;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 IBinder statusBarKey;
324
Dianne Hackborn41203752012-08-31 14:05:51 -0700325 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid,
326 int userId, int score, Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 {
328 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700329 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700331 this.uid = uid;
332 this.initialPid = initialPid;
Dianne Hackborn41203752012-08-31 14:05:51 -0700333 this.userId = userId;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500334 this.score = score;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 this.notification = notification;
336 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 void dump(PrintWriter pw, String prefix, Context baseContext) {
339 pw.println(prefix + this);
340 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
341 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500342 pw.println(prefix + " pri=" + notification.priority);
343 pw.println(prefix + " score=" + this.score);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 pw.println(prefix + " contentIntent=" + notification.contentIntent);
345 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
346 pw.println(prefix + " tickerText=" + notification.tickerText);
347 pw.println(prefix + " contentView=" + notification.contentView);
Dianne Hackborn41203752012-08-31 14:05:51 -0700348 pw.println(prefix + " uid=" + uid + " userId=" + userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
350 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
351 pw.println(prefix + " sound=" + notification.sound);
352 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
353 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
354 + " ledOnMS=" + notification.ledOnMS
355 + " ledOffMS=" + notification.ledOffMS);
356 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 @Override
359 public final String toString()
360 {
361 return "NotificationRecord{"
362 + Integer.toHexString(System.identityHashCode(this))
363 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700364 + " id=" + Integer.toHexString(id)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500365 + " tag=" + tag
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500366 + " score=" + score
Daniel Sandlere40451a2011-02-03 14:51:35 -0500367 + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 }
369 }
370
371 private static final class ToastRecord
372 {
373 final int pid;
374 final String pkg;
375 final ITransientNotification callback;
376 int duration;
377
378 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
379 {
380 this.pid = pid;
381 this.pkg = pkg;
382 this.callback = callback;
383 this.duration = duration;
384 }
385
386 void update(int duration) {
387 this.duration = duration;
388 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 void dump(PrintWriter pw, String prefix) {
391 pw.println(prefix + this);
392 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 @Override
395 public final String toString()
396 {
397 return "ToastRecord{"
398 + Integer.toHexString(System.identityHashCode(this))
399 + " pkg=" + pkg
400 + " callback=" + callback
401 + " duration=" + duration;
402 }
403 }
404
Joe Onorato089de882010-04-12 08:18:45 -0700405 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
406 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407
408 public void onSetDisabled(int status) {
409 synchronized (mNotificationList) {
410 mDisabledNotifications = status;
411 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
412 // cancel whatever's going on
413 long identity = Binder.clearCallingIdentity();
414 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700415 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
416 if (player != null) {
417 player.stopAsync();
418 }
419 } catch (RemoteException e) {
420 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 Binder.restoreCallingIdentity(identity);
422 }
423
424 identity = Binder.clearCallingIdentity();
425 try {
426 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700427 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 Binder.restoreCallingIdentity(identity);
429 }
430 }
431 }
432 }
433
434 public void onClearAll() {
Dianne Hackborn41203752012-08-31 14:05:51 -0700435 // XXX to be totally correct, the caller should tell us which user
436 // this is for.
437 cancelAll(ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 }
439
Fred Quintana6ecaff12009-09-25 14:23:13 -0700440 public void onNotificationClick(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700441 // XXX to be totally correct, the caller should tell us which user
442 // this is for.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700443 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackborn41203752012-08-31 14:05:51 -0700444 Notification.FLAG_FOREGROUND_SERVICE, false,
445 ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 }
447
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400448 public void onNotificationClear(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700449 // XXX to be totally correct, the caller should tell us which user
450 // this is for.
Joe Onorato46439ce2010-11-19 13:56:21 -0800451 cancelNotification(pkg, tag, id, 0,
452 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
Dianne Hackborn41203752012-08-31 14:05:51 -0700453 true, ActivityManager.getCurrentUser());
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400454 }
455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 public void onPanelRevealed() {
457 synchronized (mNotificationList) {
458 // sound
459 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 long identity = Binder.clearCallingIdentity();
462 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700463 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
464 if (player != null) {
465 player.stopAsync();
466 }
467 } catch (RemoteException e) {
468 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 Binder.restoreCallingIdentity(identity);
470 }
471
472 // vibrate
473 mVibrateNotification = null;
474 identity = Binder.clearCallingIdentity();
475 try {
476 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700477 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 Binder.restoreCallingIdentity(identity);
479 }
480
481 // light
482 mLights.clear();
483 mLedNotification = null;
484 updateLightsLocked();
485 }
486 }
Joe Onorato005847b2010-06-04 16:08:02 -0400487
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700488 public void onNotificationError(String pkg, String tag, int id,
489 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400490 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
491 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Dianne Hackborn41203752012-08-31 14:05:51 -0700492 // XXX to be totally correct, the caller should tell us which user
493 // this is for.
494 cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid));
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700495 long ident = Binder.clearCallingIdentity();
496 try {
497 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
498 "Bad notification posted from package " + pkg
499 + ": " + message);
500 } catch (RemoteException e) {
501 }
502 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 };
505
506 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
507 @Override
508 public void onReceive(Context context, Intent intent) {
509 String action = intent.getAction();
510
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800511 boolean queryRestart = false;
Daniel Sandler26ece572012-06-01 15:38:46 -0400512 boolean packageChanged = false;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800513
Mike Lockwood541c9942011-06-12 19:35:45 -0400514 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800515 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandler26ece572012-06-01 15:38:46 -0400516 || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED))
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800517 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800518 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800519 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800520 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800521 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800522 } else if (queryRestart) {
523 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800524 } else {
525 Uri uri = intent.getData();
526 if (uri == null) {
527 return;
528 }
529 String pkgName = uri.getSchemeSpecificPart();
530 if (pkgName == null) {
531 return;
532 }
Daniel Sandler26ece572012-06-01 15:38:46 -0400533 if (packageChanged) {
534 // We cancel notifications for packages which have just been disabled
535 final int enabled = mContext.getPackageManager()
536 .getApplicationEnabledSetting(pkgName);
537 if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
538 || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
539 return;
540 }
541 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800542 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800544 if (pkgList != null && (pkgList.length > 0)) {
545 for (String pkgName : pkgList) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700546 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart,
547 UserHandle.USER_ALL);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400550 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
551 // Keep track of screen on/off state, but do not turn off the notification light
552 // until user passes through the lock screen or views the notification.
553 mScreenOn = true;
554 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
555 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500556 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400557 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
558 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500559 updateNotificationPulse();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700560 } else if (action.equals(Intent.ACTION_USER_STOPPED)) {
561 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
562 if (userHandle >= 0) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700563 cancelAllNotificationsInt(null, 0, 0, true, userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700564 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400565 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
566 // turn off LED when user passes through lock screen
567 mNotificationLight.turnOff();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 }
569 }
570 };
571
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700572 class SettingsObserver extends ContentObserver {
573 SettingsObserver(Handler handler) {
574 super(handler);
575 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800576
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700577 void observe() {
578 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500579 resolver.registerContentObserver(Settings.System.getUriFor(
580 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700581 update();
582 }
583
584 @Override public void onChange(boolean selfChange) {
585 update();
586 }
587
588 public void update() {
589 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500590 boolean pulseEnabled = Settings.System.getInt(resolver,
591 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
592 if (mNotificationPulseEnabled != pulseEnabled) {
593 mNotificationPulseEnabled = pulseEnabled;
594 updateNotificationPulse();
595 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700596 }
597 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500598
Joe Onorato089de882010-04-12 08:18:45 -0700599 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500600 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 {
602 super();
603 mContext = context;
Jeff Brownc2346132012-04-13 01:55:38 -0700604 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 mAm = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800608
Daniel Sandler0da673f2012-04-11 12:33:16 -0400609 loadBlockDb();
610
Joe Onorato089de882010-04-12 08:18:45 -0700611 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 statusBar.setNotificationCallbacks(mNotificationCallbacks);
613
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500614 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
615 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
616
Mike Lockwood670f9322010-01-20 12:13:36 -0500617 Resources resources = mContext.getResources();
618 mDefaultNotificationColor = resources.getColor(
619 com.android.internal.R.color.config_defaultNotificationColor);
620 mDefaultNotificationLedOn = resources.getInteger(
621 com.android.internal.R.integer.config_defaultNotificationLedOn);
622 mDefaultNotificationLedOff = resources.getInteger(
623 com.android.internal.R.integer.config_defaultNotificationLedOff);
624
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400625 // Don't start allowing notifications until the setup wizard has run once.
626 // After that, including subsequent boots, init with notifications turned on.
627 // This works on the first boot because the setup wizard will toggle this
628 // flag at least once and we'll go back to 0 after that.
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700629 if (0 == Settings.Global.getInt(mContext.getContentResolver(),
630 Settings.Global.DEVICE_PROVISIONED, 0)) {
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400631 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
632 }
633
Mike Lockwood35e16bf2010-11-30 19:53:36 -0500634 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500636 filter.addAction(Intent.ACTION_SCREEN_ON);
637 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500638 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400639 filter.addAction(Intent.ACTION_USER_PRESENT);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700640 filter.addAction(Intent.ACTION_USER_STOPPED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800642 IntentFilter pkgFilter = new IntentFilter();
643 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400644 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800645 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
646 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
647 pkgFilter.addDataScheme("package");
648 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800649 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800650 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800651
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500652 SettingsObserver observer = new SettingsObserver(mHandler);
653 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 }
655
Joe Onorato30275482009-07-08 17:09:14 -0700656 void systemReady() {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700657 mAudioService = IAudioService.Stub.asInterface(
658 ServiceManager.getService(Context.AUDIO_SERVICE));
659
Joe Onorato30275482009-07-08 17:09:14 -0700660 // no beeping until we're basically done booting
661 mSystemReady = true;
662 }
663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 // Toasts
665 // ============================================================================
666 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
667 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400668 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669
670 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800671 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 return ;
673 }
674
Daniel Sandler0da673f2012-04-11 12:33:16 -0400675 final boolean isSystemToast = ("android".equals(pkg));
676
677 if (ENABLE_BLOCKED_TOASTS && !isSystemToast && !areNotificationsEnabledForPackageInt(pkg)) {
678 Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
679 return;
680 }
681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 synchronized (mToastQueue) {
683 int callingPid = Binder.getCallingPid();
684 long callingId = Binder.clearCallingIdentity();
685 try {
686 ToastRecord record;
687 int index = indexOfToastLocked(pkg, callback);
688 // If it's already in the queue, we update it in place, we don't
689 // move it to the end of the queue.
690 if (index >= 0) {
691 record = mToastQueue.get(index);
692 record.update(duration);
693 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800694 // Limit the number of toasts that any given package except the android
695 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -0400696 if (!isSystemToast) {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800697 int count = 0;
698 final int N = mToastQueue.size();
699 for (int i=0; i<N; i++) {
700 final ToastRecord r = mToastQueue.get(i);
701 if (r.pkg.equals(pkg)) {
702 count++;
703 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
704 Slog.e(TAG, "Package has already posted " + count
705 + " toasts. Not showing more. Package=" + pkg);
706 return;
707 }
708 }
709 }
710 }
711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 record = new ToastRecord(callingPid, pkg, callback, duration);
713 mToastQueue.add(record);
714 index = mToastQueue.size() - 1;
715 keepProcessAliveLocked(callingPid);
716 }
717 // If it's at index 0, it's the current toast. It doesn't matter if it's
718 // new or just been updated. Call back and tell it to show itself.
719 // If the callback fails, this will remove it from the list, so don't
720 // assume that it's valid after this.
721 if (index == 0) {
722 showNextToastLocked();
723 }
724 } finally {
725 Binder.restoreCallingIdentity(callingId);
726 }
727 }
728 }
729
730 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800731 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732
733 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800734 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 return ;
736 }
737
738 synchronized (mToastQueue) {
739 long callingId = Binder.clearCallingIdentity();
740 try {
741 int index = indexOfToastLocked(pkg, callback);
742 if (index >= 0) {
743 cancelToastLocked(index);
744 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800745 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
747 } finally {
748 Binder.restoreCallingIdentity(callingId);
749 }
750 }
751 }
752
753 private void showNextToastLocked() {
754 ToastRecord record = mToastQueue.get(0);
755 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800756 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 try {
758 record.callback.show();
759 scheduleTimeoutLocked(record, false);
760 return;
761 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800762 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 + " in package " + record.pkg);
764 // remove it from the list and let the process die
765 int index = mToastQueue.indexOf(record);
766 if (index >= 0) {
767 mToastQueue.remove(index);
768 }
769 keepProcessAliveLocked(record.pid);
770 if (mToastQueue.size() > 0) {
771 record = mToastQueue.get(0);
772 } else {
773 record = null;
774 }
775 }
776 }
777 }
778
779 private void cancelToastLocked(int index) {
780 ToastRecord record = mToastQueue.get(index);
781 try {
782 record.callback.hide();
783 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800784 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 + " in package " + record.pkg);
786 // don't worry about this, we're about to remove it from
787 // the list anyway
788 }
789 mToastQueue.remove(index);
790 keepProcessAliveLocked(record.pid);
791 if (mToastQueue.size() > 0) {
792 // Show the next one. If the callback fails, this will remove
793 // it from the list, so don't assume that the list hasn't changed
794 // after this point.
795 showNextToastLocked();
796 }
797 }
798
799 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
800 {
801 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
802 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
803 mHandler.removeCallbacksAndMessages(r);
804 mHandler.sendMessageDelayed(m, delay);
805 }
806
807 private void handleTimeout(ToastRecord record)
808 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800809 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 synchronized (mToastQueue) {
811 int index = indexOfToastLocked(record.pkg, record.callback);
812 if (index >= 0) {
813 cancelToastLocked(index);
814 }
815 }
816 }
817
818 // lock on mToastQueue
819 private int indexOfToastLocked(String pkg, ITransientNotification callback)
820 {
821 IBinder cbak = callback.asBinder();
822 ArrayList<ToastRecord> list = mToastQueue;
823 int len = list.size();
824 for (int i=0; i<len; i++) {
825 ToastRecord r = list.get(i);
826 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
827 return i;
828 }
829 }
830 return -1;
831 }
832
833 // lock on mToastQueue
834 private void keepProcessAliveLocked(int pid)
835 {
836 int toastCount = 0; // toasts from this pid
837 ArrayList<ToastRecord> list = mToastQueue;
838 int N = list.size();
839 for (int i=0; i<N; i++) {
840 ToastRecord r = list.get(i);
841 if (r.pid == pid) {
842 toastCount++;
843 }
844 }
845 try {
846 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
847 } catch (RemoteException e) {
848 // Shouldn't happen.
849 }
850 }
851
852 private final class WorkerHandler extends Handler
853 {
854 @Override
855 public void handleMessage(Message msg)
856 {
857 switch (msg.what)
858 {
859 case MESSAGE_TIMEOUT:
860 handleTimeout((ToastRecord)msg.obj);
861 break;
862 }
863 }
864 }
865
866
867 // Notifications
868 // ============================================================================
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400869 public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification,
Dianne Hackborn41203752012-08-31 14:05:51 -0700870 int[] idOut, int userId)
Fred Quintana6ecaff12009-09-25 14:23:13 -0700871 {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400872 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
Dianne Hackborn41203752012-08-31 14:05:51 -0700873 tag, id, notification, idOut, userId);
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400874 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500875
876 private final static int clamp(int x, int low, int high) {
877 return (x < low) ? low : ((x > high) ? high : x);
Daniel Sandlere40451a2011-02-03 14:51:35 -0500878 }
879
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400880 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
881 // uid/pid of another application)
882 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
Dianne Hackborn41203752012-08-31 14:05:51 -0700883 String tag, int id, Notification notification, int[] idOut, int userId)
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400884 {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400885 if (DBG) {
886 Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
887 }
888 checkCallerIsSystemOrSameApp(pkg);
889 final boolean isSystemNotification = ("android".equals(pkg));
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800890
Dianne Hackborn41203752012-08-31 14:05:51 -0700891 userId = ActivityManager.handleIncomingUser(callingPid,
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700892 callingUid, userId, true, true, "enqueueNotification", pkg);
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700893 final UserHandle user = new UserHandle(userId);
Dianne Hackborn41203752012-08-31 14:05:51 -0700894
Joe Onoratobd73d012010-06-04 11:44:54 -0700895 // Limit the number of notifications that any given package except the android
896 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -0400897 if (!isSystemNotification) {
Joe Onoratobd73d012010-06-04 11:44:54 -0700898 synchronized (mNotificationList) {
899 int count = 0;
900 final int N = mNotificationList.size();
901 for (int i=0; i<N; i++) {
902 final NotificationRecord r = mNotificationList.get(i);
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700903 if (r.pkg.equals(pkg) && r.userId == userId) {
Joe Onoratobd73d012010-06-04 11:44:54 -0700904 count++;
905 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
906 Slog.e(TAG, "Package has already posted " + count
907 + " notifications. Not showing more. package=" + pkg);
908 return;
909 }
910 }
911 }
912 }
913 }
914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 // This conditional is a dirty hack to limit the logging done on
916 // behalf of the download manager without affecting other apps.
917 if (!pkg.equals("com.android.providers.downloads")
918 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -0500919 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag,
920 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 }
922
923 if (pkg == null || notification == null) {
924 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
925 + " id=" + id + " notification=" + notification);
926 }
927 if (notification.icon != 0) {
928 if (notification.contentView == null) {
929 throw new IllegalArgumentException("contentView required: pkg=" + pkg
930 + " id=" + id + " notification=" + notification);
931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 }
933
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500934 // === Scoring ===
Daniel Sandler0da673f2012-04-11 12:33:16 -0400935
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500936 // 0. Sanitize inputs
937 notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
938 // Migrate notification flags to scores
939 if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
940 if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400941 } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500942 if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
943 }
Daniel Sandler0da673f2012-04-11 12:33:16 -0400944
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500945 // 1. initial score: buckets of 10, around the app
Daniel Sandler0da673f2012-04-11 12:33:16 -0400946 int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500947
Daniel Sandler0da673f2012-04-11 12:33:16 -0400948 // 2. Consult external heuristics (TBD)
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500949
Daniel Sandler0da673f2012-04-11 12:33:16 -0400950 // 3. Apply local rules
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500951
952 // blocked apps
Daniel Sandler0da673f2012-04-11 12:33:16 -0400953 if (ENABLE_BLOCKED_NOTIFICATIONS && !isSystemNotification && !areNotificationsEnabledForPackageInt(pkg)) {
954 score = JUNK_SCORE;
955 Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
956 }
957
958 if (DBG) {
959 Slog.v(TAG, "Assigned score=" + score + " to " + notification);
960 }
961
962 if (score < SCORE_DISPLAY_THRESHOLD) {
963 // Notification will be blocked because the score is too low.
964 return;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500965 }
966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 synchronized (mNotificationList) {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500968 NotificationRecord r = new NotificationRecord(pkg, tag, id,
Dianne Hackborn41203752012-08-31 14:05:51 -0700969 callingUid, callingPid, userId,
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500970 score,
Daniel Sandlere40451a2011-02-03 14:51:35 -0500971 notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 NotificationRecord old = null;
973
Dianne Hackborn41203752012-08-31 14:05:51 -0700974 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 if (index < 0) {
976 mNotificationList.add(r);
977 } else {
978 old = mNotificationList.remove(index);
979 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700980 // Make sure we don't lose the foreground service state.
981 if (old != null) {
982 notification.flags |=
983 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800986
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700987 // Ensure if this is a foreground service that the proper additional
988 // flags are set.
989 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
990 notification.flags |= Notification.FLAG_ONGOING_EVENT
991 | Notification.FLAG_NO_CLEAR;
992 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 if (notification.icon != 0) {
Jeff Sharkey6d515712012-09-20 16:06:08 -0700995 final StatusBarNotification n = new StatusBarNotification(
996 pkg, id, tag, r.uid, r.initialPid, score, notification, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 if (old != null && old.statusBarKey != null) {
998 r.statusBarKey = old.statusBarKey;
999 long identity = Binder.clearCallingIdentity();
1000 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001001 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
1003 finally {
1004 Binder.restoreCallingIdentity(identity);
1005 }
1006 } else {
1007 long identity = Binder.clearCallingIdentity();
1008 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001009 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwoodece18ef2012-02-13 20:42:19 -08001010 if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
1011 mAttentionLight.pulse();
1012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 }
1014 finally {
1015 Binder.restoreCallingIdentity(identity);
1016 }
1017 }
Joe Onorato30275482009-07-08 17:09:14 -07001018 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -05001020 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 if (old != null && old.statusBarKey != null) {
1022 long identity = Binder.clearCallingIdentity();
1023 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001024 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 }
1026 finally {
1027 Binder.restoreCallingIdentity(identity);
1028 }
1029 }
1030 }
1031
1032 // If we're not supposed to beep, vibrate, etc. then don't.
1033 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
1034 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -07001035 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
Daniel Sandler24518e42012-09-04 13:23:01 -04001036 && (r.userId == UserHandle.USER_ALL || r.userId == userId)
Joe Onorato30275482009-07-08 17:09:14 -07001037 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -08001038
1039 final AudioManager audioManager = (AudioManager) mContext
1040 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 // sound
1042 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001043 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 if (useDefaultSound || notification.sound != null) {
1045 Uri uri;
1046 if (useDefaultSound) {
1047 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
1048 } else {
1049 uri = notification.sound;
1050 }
1051 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
1052 int audioStreamType;
1053 if (notification.audioStreamType >= 0) {
1054 audioStreamType = notification.audioStreamType;
1055 } else {
1056 audioStreamType = DEFAULT_STREAM_TYPE;
1057 }
1058 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -08001059 // do not play notifications if stream volume is 0
1060 // (typically because ringer mode is silent).
1061 if (audioManager.getStreamVolume(audioStreamType) != 0) {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001062 final long identity = Binder.clearCallingIdentity();
Eric Laurent524dc042009-11-27 05:07:55 -08001063 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001064 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1065 if (player != null) {
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -07001066 player.playAsync(uri, user, looping, audioStreamType);
Jeff Sharkey098d5802012-04-26 17:30:34 -07001067 }
1068 } catch (RemoteException e) {
1069 } finally {
Eric Laurent524dc042009-11-27 05:07:55 -08001070 Binder.restoreCallingIdentity(identity);
1071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
1073 }
1074
1075 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001077 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 if ((useDefaultVibrate || notification.vibrate != null)
Eric Laurentbffc3d12012-05-07 17:43:49 -07001079 && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 mVibrateNotification = r;
1081
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001082 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 : notification.vibrate,
1084 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1085 }
1086 }
1087
1088 // this option doesn't shut off the lights
1089
1090 // light
1091 // the most recent thing gets the light
1092 mLights.remove(old);
1093 if (mLedNotification == old) {
1094 mLedNotification = null;
1095 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001096 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
1098 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
1099 mLights.add(r);
1100 updateLightsLocked();
1101 } else {
1102 if (old != null
1103 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
1104 updateLightsLocked();
1105 }
1106 }
1107 }
1108
1109 idOut[0] = id;
1110 }
1111
Joe Onorato30275482009-07-08 17:09:14 -07001112 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001113 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
1114 if (!manager.isEnabled()) {
1115 return;
1116 }
1117
1118 AccessibilityEvent event =
1119 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
1120 event.setPackageName(packageName);
1121 event.setClassName(Notification.class.getName());
1122 event.setParcelableData(notification);
1123 CharSequence tickerText = notification.tickerText;
1124 if (!TextUtils.isEmpty(tickerText)) {
1125 event.getText().add(tickerText);
1126 }
1127
1128 manager.sendAccessibilityEvent(event);
1129 }
1130
Joe Onorato46439ce2010-11-19 13:56:21 -08001131 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
1132 // tell the app
1133 if (sendDelete) {
1134 if (r.notification.deleteIntent != null) {
1135 try {
1136 r.notification.deleteIntent.send();
1137 } catch (PendingIntent.CanceledException ex) {
1138 // do nothing - there's no relevant way to recover, and
1139 // no reason to let this propagate
1140 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
1141 }
1142 }
1143 }
1144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 // status bar
1146 if (r.notification.icon != 0) {
1147 long identity = Binder.clearCallingIdentity();
1148 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001149 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 }
1151 finally {
1152 Binder.restoreCallingIdentity(identity);
1153 }
1154 r.statusBarKey = null;
1155 }
1156
1157 // sound
1158 if (mSoundNotification == r) {
1159 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001160 final long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001162 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1163 if (player != null) {
1164 player.stopAsync();
1165 }
1166 } catch (RemoteException e) {
1167 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 Binder.restoreCallingIdentity(identity);
1169 }
1170 }
1171
1172 // vibrate
1173 if (mVibrateNotification == r) {
1174 mVibrateNotification = null;
1175 long identity = Binder.clearCallingIdentity();
1176 try {
1177 mVibrator.cancel();
1178 }
1179 finally {
1180 Binder.restoreCallingIdentity(identity);
1181 }
1182 }
1183
1184 // light
1185 mLights.remove(r);
1186 if (mLedNotification == r) {
1187 mLedNotification = null;
1188 }
1189 }
1190
1191 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001192 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001193 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 */
Fred Quintana6ecaff12009-09-25 14:23:13 -07001195 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001196 int mustNotHaveFlags, boolean sendDelete, int userId) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001197 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag,
1198 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199
1200 synchronized (mNotificationList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001201 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001203 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
1206 return;
1207 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001208 if ((r.notification.flags & mustNotHaveFlags) != 0) {
1209 return;
1210 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 mNotificationList.remove(index);
1213
Joe Onorato46439ce2010-11-19 13:56:21 -08001214 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 updateLightsLocked();
1216 }
1217 }
1218 }
1219
1220 /**
1221 * Cancels all notifications from a given package that have all of the
1222 * {@code mustHaveFlags}.
1223 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001224 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001225 int mustNotHaveFlags, boolean doit, int userId) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001226 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags,
1227 mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228
1229 synchronized (mNotificationList) {
1230 final int N = mNotificationList.size();
1231 boolean canceledSomething = false;
1232 for (int i = N-1; i >= 0; --i) {
1233 NotificationRecord r = mNotificationList.get(i);
Dianne Hackborn41203752012-08-31 14:05:51 -07001234 if (userId != UserHandle.USER_ALL && r.userId != userId) {
1235 continue;
1236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
1238 continue;
1239 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001240 if ((r.notification.flags & mustNotHaveFlags) != 0) {
1241 continue;
1242 }
John Spurlock078a4902012-10-04 12:00:53 -04001243 if (pkg != null && !r.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 continue;
1245 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001246 canceledSomething = true;
1247 if (!doit) {
1248 return true;
1249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001251 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 }
1253 if (canceledSomething) {
1254 updateLightsLocked();
1255 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001256 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 }
1258 }
1259
Dianne Hackborn41203752012-08-31 14:05:51 -07001260 public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001261 checkCallerIsSystemOrSameApp(pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001262 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -07001263 Binder.getCallingUid(), userId, true, true, "cancelNotificationWithTag", pkg);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001264 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001265 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001266 Binder.getCallingUid() == Process.SYSTEM_UID
Dianne Hackborn41203752012-08-31 14:05:51 -07001267 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 }
1269
Dianne Hackborn41203752012-08-31 14:05:51 -07001270 public void cancelAllNotifications(String pkg, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001271 checkCallerIsSystemOrSameApp(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001272
Dianne Hackborn41203752012-08-31 14:05:51 -07001273 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1274 Binder.getCallingUid(), userId, true, true, "cancelAllNotifications", pkg);
1275
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001276 // Calling from user space, don't allow the canceling of actively
1277 // running foreground services.
Dianne Hackborn41203752012-08-31 14:05:51 -07001278 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 }
1280
Daniel Sandler0da673f2012-04-11 12:33:16 -04001281 void checkCallerIsSystem() {
1282 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001283 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001284 return;
1285 }
1286 throw new SecurityException("Disallowed call for uid " + uid);
1287 }
1288
1289 void checkCallerIsSystemOrSameApp(String pkg) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001290 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001291 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001292 return;
1293 }
1294 try {
Amith Yamasanif203aee2012-08-29 18:41:53 -07001295 ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
1296 pkg, 0, UserHandle.getCallingUserId());
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001297 if (!UserHandle.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001298 throw new SecurityException("Calling uid " + uid + " gave package"
1299 + pkg + " which is owned by uid " + ai.uid);
1300 }
Amith Yamasanif203aee2012-08-29 18:41:53 -07001301 } catch (RemoteException re) {
1302 throw new SecurityException("Unknown package " + pkg + "\n" + re);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001303 }
1304 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001305
Dianne Hackborn41203752012-08-31 14:05:51 -07001306 void cancelAll(int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 synchronized (mNotificationList) {
1308 final int N = mNotificationList.size();
1309 for (int i=N-1; i>=0; i--) {
1310 NotificationRecord r = mNotificationList.get(i);
1311
Dianne Hackborn41203752012-08-31 14:05:51 -07001312 if (r.userId != userId) {
1313 continue;
1314 }
1315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1317 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001319 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 }
1321 }
1322
1323 updateLightsLocked();
1324 }
1325 }
1326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 // lock on mNotificationList
1328 private void updateLightsLocked()
1329 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001330 // handle notification lights
1331 if (mLedNotification == null) {
1332 // get next notification, if any
1333 int n = mLights.size();
1334 if (n > 0) {
1335 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 }
1337 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001338
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001339 // Don't flash while we are in a call or screen is on
1340 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001341 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001342 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001343 int ledARGB = mLedNotification.notification.ledARGB;
1344 int ledOnMS = mLedNotification.notification.ledOnMS;
1345 int ledOffMS = mLedNotification.notification.ledOffMS;
1346 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1347 ledARGB = mDefaultNotificationColor;
1348 ledOnMS = mDefaultNotificationLedOn;
1349 ledOffMS = mDefaultNotificationLedOff;
1350 }
1351 if (mNotificationPulseEnabled) {
1352 // pulse repeatedly
1353 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1354 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05001355 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 }
1358
1359 // lock on mNotificationList
Dianne Hackborn41203752012-08-31 14:05:51 -07001360 private int indexOfNotificationLocked(String pkg, String tag, int id, int userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 {
1362 ArrayList<NotificationRecord> list = mNotificationList;
1363 final int len = list.size();
1364 for (int i=0; i<len; i++) {
1365 NotificationRecord r = list.get(i);
Dianne Hackborn41203752012-08-31 14:05:51 -07001366 if (r.userId != userId || r.id != id) {
1367 continue;
1368 }
Fred Quintana6ecaff12009-09-25 14:23:13 -07001369 if (tag == null) {
1370 if (r.tag != null) {
1371 continue;
1372 }
1373 } else {
1374 if (!tag.equals(r.tag)) {
1375 continue;
1376 }
1377 }
Dianne Hackborn41203752012-08-31 14:05:51 -07001378 if (r.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 return i;
1380 }
1381 }
1382 return -1;
1383 }
1384
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001385 private void updateNotificationPulse() {
1386 synchronized (mNotificationList) {
1387 updateLightsLocked();
1388 }
1389 }
1390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 // ======================================================================
1392 @Override
1393 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1394 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1395 != PackageManager.PERMISSION_GRANTED) {
1396 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1397 + Binder.getCallingPid()
1398 + ", uid=" + Binder.getCallingUid());
1399 return;
1400 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 pw.println("Current Notification Manager state:");
1403
1404 int N;
1405
1406 synchronized (mToastQueue) {
1407 N = mToastQueue.size();
1408 if (N > 0) {
1409 pw.println(" Toast Queue:");
1410 for (int i=0; i<N; i++) {
1411 mToastQueue.get(i).dump(pw, " ");
1412 }
1413 pw.println(" ");
1414 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 }
1417
1418 synchronized (mNotificationList) {
1419 N = mNotificationList.size();
1420 if (N > 0) {
1421 pw.println(" Notification List:");
1422 for (int i=0; i<N; i++) {
1423 mNotificationList.get(i).dump(pw, " ", mContext);
1424 }
1425 pw.println(" ");
1426 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 N = mLights.size();
1429 if (N > 0) {
1430 pw.println(" Lights List:");
1431 for (int i=0; i<N; i++) {
1432 mLights.get(i).dump(pw, " ", mContext);
1433 }
1434 pw.println(" ");
1435 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 pw.println(" mSoundNotification=" + mSoundNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001439 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1440 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
1442 }
1443}