blob: 37d7ce7baac8850c88de4c232bd5310298b1a224 [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};
Daniel Sandleredbb3802012-11-13 20:49:47 -0800104 private static final int VIBRATE_PATTERN_MAXLEN = 8 * 2 + 1; // up to eight bumps
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
106 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400107 private static final boolean SCORE_ONGOING_HIGHER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
Daniel Sandler0da673f2012-04-11 12:33:16 -0400109 private static final int JUNK_SCORE = -1000;
110 private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10;
111 private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER;
112
Daniel Sandler526fa0e2012-12-04 14:51:50 -0500113 // Notifications with scores below this will not interrupt the user, either via LED or
114 // sound or vibration
115 private static final int SCORE_INTERRUPTION_THRESHOLD =
116 Notification.PRIORITY_LOW * NOTIFICATION_PRIORITY_MULTIPLIER;
117
Daniel Sandler0da673f2012-04-11 12:33:16 -0400118 private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true;
119 private static final boolean ENABLE_BLOCKED_TOASTS = true;
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 final Context mContext;
122 final IActivityManager mAm;
123 final IBinder mForegroundToken = new Binder();
124
125 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -0700126 private StatusBarManagerService mStatusBar;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500127 private LightsService.Light mNotificationLight;
128 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
Mike Lockwood670f9322010-01-20 12:13:36 -0500130 private int mDefaultNotificationColor;
131 private int mDefaultNotificationLedOn;
132 private int mDefaultNotificationLedOff;
133
Daniel Sandleredbb3802012-11-13 20:49:47 -0800134 private long[] mDefaultVibrationPattern;
135 private long[] mFallbackVibrationPattern;
136
Joe Onorato30275482009-07-08 17:09:14 -0700137 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400138 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
Jeff Sharkey098d5802012-04-26 17:30:34 -0700140 private NotificationRecord mSoundNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 private NotificationRecord mVibrateNotification;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700142
143 private IAudioService mAudioService;
Jeff Brownc2346132012-04-13 01:55:38 -0700144 private Vibrator mVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500146 // for enabling and disabling notification pulse behavior
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400147 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500148 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500149 private boolean mNotificationPulseEnabled;
150
Fred Quintana6ecaff12009-09-25 14:23:13 -0700151 private final ArrayList<NotificationRecord> mNotificationList =
152 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153
154 private ArrayList<ToastRecord> mToastQueue;
155
156 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700158
Daniel Sandler0da673f2012-04-11 12:33:16 -0400159 // Notification control database. For now just contains disabled packages.
160 private AtomicFile mPolicyFile;
161 private HashSet<String> mBlockedPackages = new HashSet<String>();
162
163 private static final int DB_VERSION = 1;
164
165 private static final String TAG_BODY = "notification-policy";
166 private static final String ATTR_VERSION = "version";
167
168 private static final String TAG_BLOCKED_PKGS = "blocked-packages";
169 private static final String TAG_PACKAGE = "package";
170 private static final String ATTR_NAME = "name";
171
172 private void loadBlockDb() {
173 synchronized(mBlockedPackages) {
174 if (mPolicyFile == null) {
175 File dir = new File("/data/system");
176 mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml"));
177
178 mBlockedPackages.clear();
179
180 FileInputStream infile = null;
181 try {
182 infile = mPolicyFile.openRead();
183 final XmlPullParser parser = Xml.newPullParser();
184 parser.setInput(infile, null);
185
186 int type;
187 String tag;
188 int version = DB_VERSION;
189 while ((type = parser.next()) != END_DOCUMENT) {
190 tag = parser.getName();
191 if (type == START_TAG) {
192 if (TAG_BODY.equals(tag)) {
193 version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
194 } else if (TAG_BLOCKED_PKGS.equals(tag)) {
195 while ((type = parser.next()) != END_DOCUMENT) {
196 tag = parser.getName();
197 if (TAG_PACKAGE.equals(tag)) {
198 mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME));
199 } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) {
200 break;
201 }
202 }
203 }
204 }
205 }
206 } catch (FileNotFoundException e) {
207 // No data yet
208 } catch (IOException e) {
209 Log.wtf(TAG, "Unable to read blocked notifications database", e);
210 } catch (NumberFormatException e) {
211 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
212 } catch (XmlPullParserException e) {
213 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
214 } finally {
215 IoUtils.closeQuietly(infile);
216 }
217 }
218 }
219 }
220
221 private void writeBlockDb() {
222 synchronized(mBlockedPackages) {
223 FileOutputStream outfile = null;
224 try {
225 outfile = mPolicyFile.startWrite();
226
227 XmlSerializer out = new FastXmlSerializer();
228 out.setOutput(outfile, "utf-8");
229
230 out.startDocument(null, true);
231
232 out.startTag(null, TAG_BODY); {
233 out.attribute(null, ATTR_VERSION, String.valueOf(DB_VERSION));
234 out.startTag(null, TAG_BLOCKED_PKGS); {
235 // write all known network policies
236 for (String pkg : mBlockedPackages) {
237 out.startTag(null, TAG_PACKAGE); {
238 out.attribute(null, ATTR_NAME, pkg);
239 } out.endTag(null, TAG_PACKAGE);
240 }
241 } out.endTag(null, TAG_BLOCKED_PKGS);
242 } out.endTag(null, TAG_BODY);
243
244 out.endDocument();
245
246 mPolicyFile.finishWrite(outfile);
247 } catch (IOException e) {
248 if (outfile != null) {
249 mPolicyFile.failWrite(outfile);
250 }
251 }
252 }
253 }
254
255 public boolean areNotificationsEnabledForPackage(String pkg) {
256 checkCallerIsSystem();
257 return areNotificationsEnabledForPackageInt(pkg);
258 }
259
260 // Unchecked. Not exposed via Binder, but can be called in the course of enqueue*().
261 private boolean areNotificationsEnabledForPackageInt(String pkg) {
262 final boolean enabled = !mBlockedPackages.contains(pkg);
263 if (DBG) {
264 Slog.v(TAG, "notifications are " + (enabled?"en":"dis") + "abled for " + pkg);
265 }
266 return enabled;
267 }
268
269 public void setNotificationsEnabledForPackage(String pkg, boolean enabled) {
270 checkCallerIsSystem();
271 if (DBG) {
272 Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);
273 }
274 if (enabled) {
275 mBlockedPackages.remove(pkg);
276 } else {
277 mBlockedPackages.add(pkg);
278
279 // Now, cancel any outstanding notifications that are part of a just-disabled app
280 if (ENABLE_BLOCKED_NOTIFICATIONS) {
281 synchronized (mNotificationList) {
282 final int N = mNotificationList.size();
283 for (int i=0; i<N; i++) {
284 final NotificationRecord r = mNotificationList.get(i);
285 if (r.pkg.equals(pkg)) {
286 cancelNotificationLocked(r, false);
287 }
288 }
289 }
290 }
291 // Don't bother canceling toasts, they'll go away soon enough.
292 }
293 writeBlockDb();
294 }
295
296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 private static String idDebugString(Context baseContext, String packageName, int id) {
298 Context c = null;
299
300 if (packageName != null) {
301 try {
302 c = baseContext.createPackageContext(packageName, 0);
303 } catch (NameNotFoundException e) {
304 c = baseContext;
305 }
306 } else {
307 c = baseContext;
308 }
309
310 String pkg;
311 String type;
312 String name;
313
314 Resources r = c.getResources();
315 try {
316 return r.getResourceName(id);
317 } catch (Resources.NotFoundException e) {
318 return "<name unknown>";
319 }
320 }
321
322 private static final class NotificationRecord
323 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700324 final String pkg;
325 final String tag;
326 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700327 final int uid;
328 final int initialPid;
Dianne Hackborn41203752012-08-31 14:05:51 -0700329 final int userId;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700330 final Notification notification;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500331 final int score;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 IBinder statusBarKey;
333
Dianne Hackborn41203752012-08-31 14:05:51 -0700334 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid,
335 int userId, int score, Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 {
337 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700338 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700340 this.uid = uid;
341 this.initialPid = initialPid;
Dianne Hackborn41203752012-08-31 14:05:51 -0700342 this.userId = userId;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500343 this.score = score;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 this.notification = notification;
345 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 void dump(PrintWriter pw, String prefix, Context baseContext) {
348 pw.println(prefix + this);
349 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
350 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500351 pw.println(prefix + " pri=" + notification.priority);
352 pw.println(prefix + " score=" + this.score);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 pw.println(prefix + " contentIntent=" + notification.contentIntent);
354 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
355 pw.println(prefix + " tickerText=" + notification.tickerText);
356 pw.println(prefix + " contentView=" + notification.contentView);
Dianne Hackborn41203752012-08-31 14:05:51 -0700357 pw.println(prefix + " uid=" + uid + " userId=" + userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
359 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
360 pw.println(prefix + " sound=" + notification.sound);
361 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
362 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
363 + " ledOnMS=" + notification.ledOnMS
364 + " ledOffMS=" + notification.ledOffMS);
365 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 @Override
368 public final String toString()
369 {
370 return "NotificationRecord{"
371 + Integer.toHexString(System.identityHashCode(this))
372 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700373 + " id=" + Integer.toHexString(id)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500374 + " tag=" + tag
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500375 + " score=" + score
Daniel Sandlere40451a2011-02-03 14:51:35 -0500376 + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 }
378 }
379
380 private static final class ToastRecord
381 {
382 final int pid;
383 final String pkg;
384 final ITransientNotification callback;
385 int duration;
386
387 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
388 {
389 this.pid = pid;
390 this.pkg = pkg;
391 this.callback = callback;
392 this.duration = duration;
393 }
394
395 void update(int duration) {
396 this.duration = duration;
397 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 void dump(PrintWriter pw, String prefix) {
400 pw.println(prefix + this);
401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 @Override
404 public final String toString()
405 {
406 return "ToastRecord{"
407 + Integer.toHexString(System.identityHashCode(this))
408 + " pkg=" + pkg
409 + " callback=" + callback
410 + " duration=" + duration;
411 }
412 }
413
Joe Onorato089de882010-04-12 08:18:45 -0700414 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
415 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416
417 public void onSetDisabled(int status) {
418 synchronized (mNotificationList) {
419 mDisabledNotifications = status;
420 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
421 // cancel whatever's going on
422 long identity = Binder.clearCallingIdentity();
423 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700424 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
425 if (player != null) {
426 player.stopAsync();
427 }
428 } catch (RemoteException e) {
429 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 Binder.restoreCallingIdentity(identity);
431 }
432
433 identity = Binder.clearCallingIdentity();
434 try {
435 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700436 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 Binder.restoreCallingIdentity(identity);
438 }
439 }
440 }
441 }
442
443 public void onClearAll() {
Dianne Hackborn41203752012-08-31 14:05:51 -0700444 // XXX to be totally correct, the caller should tell us which user
445 // this is for.
446 cancelAll(ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 }
448
Fred Quintana6ecaff12009-09-25 14:23:13 -0700449 public void onNotificationClick(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700450 // XXX to be totally correct, the caller should tell us which user
451 // this is for.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700452 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackborn41203752012-08-31 14:05:51 -0700453 Notification.FLAG_FOREGROUND_SERVICE, false,
454 ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 }
456
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400457 public void onNotificationClear(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700458 // XXX to be totally correct, the caller should tell us which user
459 // this is for.
Joe Onorato46439ce2010-11-19 13:56:21 -0800460 cancelNotification(pkg, tag, id, 0,
461 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
Dianne Hackborn41203752012-08-31 14:05:51 -0700462 true, ActivityManager.getCurrentUser());
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400463 }
464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 public void onPanelRevealed() {
466 synchronized (mNotificationList) {
467 // sound
468 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 long identity = Binder.clearCallingIdentity();
471 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700472 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
473 if (player != null) {
474 player.stopAsync();
475 }
476 } catch (RemoteException e) {
477 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 Binder.restoreCallingIdentity(identity);
479 }
480
481 // vibrate
482 mVibrateNotification = null;
483 identity = Binder.clearCallingIdentity();
484 try {
485 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700486 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 Binder.restoreCallingIdentity(identity);
488 }
489
490 // light
491 mLights.clear();
492 mLedNotification = null;
493 updateLightsLocked();
494 }
495 }
Joe Onorato005847b2010-06-04 16:08:02 -0400496
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700497 public void onNotificationError(String pkg, String tag, int id,
498 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400499 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
500 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Dianne Hackborn41203752012-08-31 14:05:51 -0700501 // XXX to be totally correct, the caller should tell us which user
502 // this is for.
503 cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid));
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700504 long ident = Binder.clearCallingIdentity();
505 try {
506 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
507 "Bad notification posted from package " + pkg
508 + ": " + message);
509 } catch (RemoteException e) {
510 }
511 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 };
514
515 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
516 @Override
517 public void onReceive(Context context, Intent intent) {
518 String action = intent.getAction();
519
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800520 boolean queryRestart = false;
Daniel Sandler26ece572012-06-01 15:38:46 -0400521 boolean packageChanged = false;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800522
Mike Lockwood541c9942011-06-12 19:35:45 -0400523 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800524 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandler26ece572012-06-01 15:38:46 -0400525 || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED))
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800526 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800527 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800528 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800529 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800530 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800531 } else if (queryRestart) {
532 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800533 } else {
534 Uri uri = intent.getData();
535 if (uri == null) {
536 return;
537 }
538 String pkgName = uri.getSchemeSpecificPart();
539 if (pkgName == null) {
540 return;
541 }
Daniel Sandler26ece572012-06-01 15:38:46 -0400542 if (packageChanged) {
543 // We cancel notifications for packages which have just been disabled
544 final int enabled = mContext.getPackageManager()
545 .getApplicationEnabledSetting(pkgName);
546 if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
547 || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
548 return;
549 }
550 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800551 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800553 if (pkgList != null && (pkgList.length > 0)) {
554 for (String pkgName : pkgList) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700555 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart,
556 UserHandle.USER_ALL);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400559 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
560 // Keep track of screen on/off state, but do not turn off the notification light
561 // until user passes through the lock screen or views the notification.
562 mScreenOn = true;
563 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
564 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500565 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400566 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
567 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500568 updateNotificationPulse();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700569 } else if (action.equals(Intent.ACTION_USER_STOPPED)) {
570 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
571 if (userHandle >= 0) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700572 cancelAllNotificationsInt(null, 0, 0, true, userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700573 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400574 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
575 // turn off LED when user passes through lock screen
576 mNotificationLight.turnOff();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 }
578 }
579 };
580
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700581 class SettingsObserver extends ContentObserver {
582 SettingsObserver(Handler handler) {
583 super(handler);
584 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800585
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700586 void observe() {
587 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500588 resolver.registerContentObserver(Settings.System.getUriFor(
589 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700590 update();
591 }
592
593 @Override public void onChange(boolean selfChange) {
594 update();
595 }
596
597 public void update() {
598 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500599 boolean pulseEnabled = Settings.System.getInt(resolver,
600 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
601 if (mNotificationPulseEnabled != pulseEnabled) {
602 mNotificationPulseEnabled = pulseEnabled;
603 updateNotificationPulse();
604 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700605 }
606 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500607
Daniel Sandleredbb3802012-11-13 20:49:47 -0800608 static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) {
609 int[] ar = r.getIntArray(resid);
610 if (ar == null) {
611 return def;
612 }
613 final int len = ar.length > maxlen ? maxlen : ar.length;
614 long[] out = new long[len];
615 for (int i=0; i<len; i++) {
616 out[i] = ar[i];
617 }
618 return out;
619 }
620
Joe Onorato089de882010-04-12 08:18:45 -0700621 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500622 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 {
624 super();
625 mContext = context;
Jeff Brownc2346132012-04-13 01:55:38 -0700626 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 mAm = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800630
Daniel Sandler0da673f2012-04-11 12:33:16 -0400631 loadBlockDb();
632
Joe Onorato089de882010-04-12 08:18:45 -0700633 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 statusBar.setNotificationCallbacks(mNotificationCallbacks);
635
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500636 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
637 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
638
Mike Lockwood670f9322010-01-20 12:13:36 -0500639 Resources resources = mContext.getResources();
640 mDefaultNotificationColor = resources.getColor(
641 com.android.internal.R.color.config_defaultNotificationColor);
642 mDefaultNotificationLedOn = resources.getInteger(
643 com.android.internal.R.integer.config_defaultNotificationLedOn);
644 mDefaultNotificationLedOff = resources.getInteger(
645 com.android.internal.R.integer.config_defaultNotificationLedOff);
646
Daniel Sandleredbb3802012-11-13 20:49:47 -0800647 mDefaultVibrationPattern = getLongArray(resources,
648 com.android.internal.R.array.config_defaultNotificationVibePattern,
649 VIBRATE_PATTERN_MAXLEN,
650 DEFAULT_VIBRATE_PATTERN);
651
652 mFallbackVibrationPattern = getLongArray(resources,
653 com.android.internal.R.array.config_notificationFallbackVibePattern,
654 VIBRATE_PATTERN_MAXLEN,
655 DEFAULT_VIBRATE_PATTERN);
656
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400657 // Don't start allowing notifications until the setup wizard has run once.
658 // After that, including subsequent boots, init with notifications turned on.
659 // This works on the first boot because the setup wizard will toggle this
660 // flag at least once and we'll go back to 0 after that.
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700661 if (0 == Settings.Global.getInt(mContext.getContentResolver(),
662 Settings.Global.DEVICE_PROVISIONED, 0)) {
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400663 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
664 }
665
Mike Lockwood35e16bf2010-11-30 19:53:36 -0500666 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500668 filter.addAction(Intent.ACTION_SCREEN_ON);
669 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500670 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400671 filter.addAction(Intent.ACTION_USER_PRESENT);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700672 filter.addAction(Intent.ACTION_USER_STOPPED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800674 IntentFilter pkgFilter = new IntentFilter();
675 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400676 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800677 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
678 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
679 pkgFilter.addDataScheme("package");
680 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800681 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800682 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800683
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500684 SettingsObserver observer = new SettingsObserver(mHandler);
685 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
687
Joe Onorato30275482009-07-08 17:09:14 -0700688 void systemReady() {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700689 mAudioService = IAudioService.Stub.asInterface(
690 ServiceManager.getService(Context.AUDIO_SERVICE));
691
Joe Onorato30275482009-07-08 17:09:14 -0700692 // no beeping until we're basically done booting
693 mSystemReady = true;
694 }
695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 // Toasts
697 // ============================================================================
698 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
699 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400700 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701
702 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800703 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 return ;
705 }
706
Daniel Sandler0da673f2012-04-11 12:33:16 -0400707 final boolean isSystemToast = ("android".equals(pkg));
708
709 if (ENABLE_BLOCKED_TOASTS && !isSystemToast && !areNotificationsEnabledForPackageInt(pkg)) {
710 Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
711 return;
712 }
713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 synchronized (mToastQueue) {
715 int callingPid = Binder.getCallingPid();
716 long callingId = Binder.clearCallingIdentity();
717 try {
718 ToastRecord record;
719 int index = indexOfToastLocked(pkg, callback);
720 // If it's already in the queue, we update it in place, we don't
721 // move it to the end of the queue.
722 if (index >= 0) {
723 record = mToastQueue.get(index);
724 record.update(duration);
725 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800726 // Limit the number of toasts that any given package except the android
727 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -0400728 if (!isSystemToast) {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800729 int count = 0;
730 final int N = mToastQueue.size();
731 for (int i=0; i<N; i++) {
732 final ToastRecord r = mToastQueue.get(i);
733 if (r.pkg.equals(pkg)) {
734 count++;
735 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
736 Slog.e(TAG, "Package has already posted " + count
737 + " toasts. Not showing more. Package=" + pkg);
738 return;
739 }
740 }
741 }
742 }
743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 record = new ToastRecord(callingPid, pkg, callback, duration);
745 mToastQueue.add(record);
746 index = mToastQueue.size() - 1;
747 keepProcessAliveLocked(callingPid);
748 }
749 // If it's at index 0, it's the current toast. It doesn't matter if it's
750 // new or just been updated. Call back and tell it to show itself.
751 // If the callback fails, this will remove it from the list, so don't
752 // assume that it's valid after this.
753 if (index == 0) {
754 showNextToastLocked();
755 }
756 } finally {
757 Binder.restoreCallingIdentity(callingId);
758 }
759 }
760 }
761
762 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800763 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764
765 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800766 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 return ;
768 }
769
770 synchronized (mToastQueue) {
771 long callingId = Binder.clearCallingIdentity();
772 try {
773 int index = indexOfToastLocked(pkg, callback);
774 if (index >= 0) {
775 cancelToastLocked(index);
776 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800777 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 }
779 } finally {
780 Binder.restoreCallingIdentity(callingId);
781 }
782 }
783 }
784
785 private void showNextToastLocked() {
786 ToastRecord record = mToastQueue.get(0);
787 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800788 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 try {
790 record.callback.show();
791 scheduleTimeoutLocked(record, false);
792 return;
793 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800794 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 + " in package " + record.pkg);
796 // remove it from the list and let the process die
797 int index = mToastQueue.indexOf(record);
798 if (index >= 0) {
799 mToastQueue.remove(index);
800 }
801 keepProcessAliveLocked(record.pid);
802 if (mToastQueue.size() > 0) {
803 record = mToastQueue.get(0);
804 } else {
805 record = null;
806 }
807 }
808 }
809 }
810
811 private void cancelToastLocked(int index) {
812 ToastRecord record = mToastQueue.get(index);
813 try {
814 record.callback.hide();
815 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800816 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 + " in package " + record.pkg);
818 // don't worry about this, we're about to remove it from
819 // the list anyway
820 }
821 mToastQueue.remove(index);
822 keepProcessAliveLocked(record.pid);
823 if (mToastQueue.size() > 0) {
824 // Show the next one. If the callback fails, this will remove
825 // it from the list, so don't assume that the list hasn't changed
826 // after this point.
827 showNextToastLocked();
828 }
829 }
830
831 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
832 {
833 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
834 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
835 mHandler.removeCallbacksAndMessages(r);
836 mHandler.sendMessageDelayed(m, delay);
837 }
838
839 private void handleTimeout(ToastRecord record)
840 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800841 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 synchronized (mToastQueue) {
843 int index = indexOfToastLocked(record.pkg, record.callback);
844 if (index >= 0) {
845 cancelToastLocked(index);
846 }
847 }
848 }
849
850 // lock on mToastQueue
851 private int indexOfToastLocked(String pkg, ITransientNotification callback)
852 {
853 IBinder cbak = callback.asBinder();
854 ArrayList<ToastRecord> list = mToastQueue;
855 int len = list.size();
856 for (int i=0; i<len; i++) {
857 ToastRecord r = list.get(i);
858 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
859 return i;
860 }
861 }
862 return -1;
863 }
864
865 // lock on mToastQueue
866 private void keepProcessAliveLocked(int pid)
867 {
868 int toastCount = 0; // toasts from this pid
869 ArrayList<ToastRecord> list = mToastQueue;
870 int N = list.size();
871 for (int i=0; i<N; i++) {
872 ToastRecord r = list.get(i);
873 if (r.pid == pid) {
874 toastCount++;
875 }
876 }
877 try {
878 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
879 } catch (RemoteException e) {
880 // Shouldn't happen.
881 }
882 }
883
884 private final class WorkerHandler extends Handler
885 {
886 @Override
887 public void handleMessage(Message msg)
888 {
889 switch (msg.what)
890 {
891 case MESSAGE_TIMEOUT:
892 handleTimeout((ToastRecord)msg.obj);
893 break;
894 }
895 }
896 }
897
898
899 // Notifications
900 // ============================================================================
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400901 public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification,
Dianne Hackborn41203752012-08-31 14:05:51 -0700902 int[] idOut, int userId)
Fred Quintana6ecaff12009-09-25 14:23:13 -0700903 {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400904 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
Dianne Hackborn41203752012-08-31 14:05:51 -0700905 tag, id, notification, idOut, userId);
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400906 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500907
908 private final static int clamp(int x, int low, int high) {
909 return (x < low) ? low : ((x > high) ? high : x);
Daniel Sandlere40451a2011-02-03 14:51:35 -0500910 }
911
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400912 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
913 // uid/pid of another application)
914 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
Dianne Hackborn41203752012-08-31 14:05:51 -0700915 String tag, int id, Notification notification, int[] idOut, int userId)
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400916 {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400917 if (DBG) {
918 Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
919 }
920 checkCallerIsSystemOrSameApp(pkg);
921 final boolean isSystemNotification = ("android".equals(pkg));
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800922
Dianne Hackborn41203752012-08-31 14:05:51 -0700923 userId = ActivityManager.handleIncomingUser(callingPid,
Amith Yamasani2c7ebea2012-10-30 15:28:27 -0700924 callingUid, userId, true, false, "enqueueNotification", pkg);
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700925 final UserHandle user = new UserHandle(userId);
Dianne Hackborn41203752012-08-31 14:05:51 -0700926
Joe Onoratobd73d012010-06-04 11:44:54 -0700927 // Limit the number of notifications that any given package except the android
928 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -0400929 if (!isSystemNotification) {
Joe Onoratobd73d012010-06-04 11:44:54 -0700930 synchronized (mNotificationList) {
931 int count = 0;
932 final int N = mNotificationList.size();
933 for (int i=0; i<N; i++) {
934 final NotificationRecord r = mNotificationList.get(i);
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700935 if (r.pkg.equals(pkg) && r.userId == userId) {
Joe Onoratobd73d012010-06-04 11:44:54 -0700936 count++;
937 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
938 Slog.e(TAG, "Package has already posted " + count
939 + " notifications. Not showing more. package=" + pkg);
940 return;
941 }
942 }
943 }
944 }
945 }
946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 // This conditional is a dirty hack to limit the logging done on
948 // behalf of the download manager without affecting other apps.
949 if (!pkg.equals("com.android.providers.downloads")
950 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandler321e9c52012-10-12 10:59:26 -0700951 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -0500952 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 }
954
955 if (pkg == null || notification == null) {
956 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
957 + " id=" + id + " notification=" + notification);
958 }
959 if (notification.icon != 0) {
960 if (notification.contentView == null) {
961 throw new IllegalArgumentException("contentView required: pkg=" + pkg
962 + " id=" + id + " notification=" + notification);
963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 }
965
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500966 // === Scoring ===
Daniel Sandler0da673f2012-04-11 12:33:16 -0400967
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500968 // 0. Sanitize inputs
969 notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
970 // Migrate notification flags to scores
971 if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
972 if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400973 } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500974 if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
975 }
Daniel Sandler0da673f2012-04-11 12:33:16 -0400976
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500977 // 1. initial score: buckets of 10, around the app
Daniel Sandler0da673f2012-04-11 12:33:16 -0400978 int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500979
Daniel Sandler0da673f2012-04-11 12:33:16 -0400980 // 2. Consult external heuristics (TBD)
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500981
Daniel Sandler0da673f2012-04-11 12:33:16 -0400982 // 3. Apply local rules
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500983
984 // blocked apps
Daniel Sandler0da673f2012-04-11 12:33:16 -0400985 if (ENABLE_BLOCKED_NOTIFICATIONS && !isSystemNotification && !areNotificationsEnabledForPackageInt(pkg)) {
986 score = JUNK_SCORE;
987 Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
988 }
989
990 if (DBG) {
991 Slog.v(TAG, "Assigned score=" + score + " to " + notification);
992 }
993
994 if (score < SCORE_DISPLAY_THRESHOLD) {
995 // Notification will be blocked because the score is too low.
996 return;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500997 }
998
Daniel Sandler526fa0e2012-12-04 14:51:50 -0500999 // Should this notification make noise, vibe, or use the LED?
1000 final boolean canInterrupt = (score >= SCORE_INTERRUPTION_THRESHOLD);
1001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 synchronized (mNotificationList) {
Daniel Sandlere40451a2011-02-03 14:51:35 -05001003 NotificationRecord r = new NotificationRecord(pkg, tag, id,
Dianne Hackborn41203752012-08-31 14:05:51 -07001004 callingUid, callingPid, userId,
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001005 score,
Daniel Sandlere40451a2011-02-03 14:51:35 -05001006 notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 NotificationRecord old = null;
1008
Dianne Hackborn41203752012-08-31 14:05:51 -07001009 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 if (index < 0) {
1011 mNotificationList.add(r);
1012 } else {
1013 old = mNotificationList.remove(index);
1014 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001015 // Make sure we don't lose the foreground service state.
1016 if (old != null) {
1017 notification.flags |=
1018 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
1019 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001021
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001022 // Ensure if this is a foreground service that the proper additional
1023 // flags are set.
1024 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
1025 notification.flags |= Notification.FLAG_ONGOING_EVENT
1026 | Notification.FLAG_NO_CLEAR;
1027 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001028
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001029 final int currentUser;
1030 final long token = Binder.clearCallingIdentity();
1031 try {
1032 currentUser = ActivityManager.getCurrentUser();
1033 } finally {
1034 Binder.restoreCallingIdentity(token);
1035 }
1036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 if (notification.icon != 0) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07001038 final StatusBarNotification n = new StatusBarNotification(
1039 pkg, id, tag, r.uid, r.initialPid, score, notification, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 if (old != null && old.statusBarKey != null) {
1041 r.statusBarKey = old.statusBarKey;
1042 long identity = Binder.clearCallingIdentity();
1043 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001044 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 }
1046 finally {
1047 Binder.restoreCallingIdentity(identity);
1048 }
1049 } else {
1050 long identity = Binder.clearCallingIdentity();
1051 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001052 r.statusBarKey = mStatusBar.addNotification(n);
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001053 if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1054 && canInterrupt) {
Mike Lockwoodece18ef2012-02-13 20:42:19 -08001055 mAttentionLight.pulse();
1056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 }
1058 finally {
1059 Binder.restoreCallingIdentity(identity);
1060 }
1061 }
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001062 // Send accessibility events only for the current user.
1063 if (currentUser == userId) {
1064 sendAccessibilityEvent(notification, pkg);
1065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -05001067 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 if (old != null && old.statusBarKey != null) {
1069 long identity = Binder.clearCallingIdentity();
1070 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001071 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
1073 finally {
1074 Binder.restoreCallingIdentity(identity);
1075 }
1076 }
1077 }
1078
1079 // If we're not supposed to beep, vibrate, etc. then don't.
1080 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
1081 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -07001082 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
Jeff Sharkey8637bd92012-10-05 15:25:17 -07001083 && (r.userId == UserHandle.USER_ALL ||
Jeff Sharkeyb78738f2012-10-05 16:03:01 -07001084 (r.userId == userId && r.userId == currentUser))
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001085 && canInterrupt
Joe Onorato30275482009-07-08 17:09:14 -07001086 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -08001087
1088 final AudioManager audioManager = (AudioManager) mContext
1089 .getSystemService(Context.AUDIO_SERVICE);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 // sound
1092 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001093 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001094
1095 Uri soundUri = null;
1096 boolean hasValidSound = false;
1097
1098 if (useDefaultSound) {
1099 soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
1100
1101 // check to see if the default notification sound is silent
1102 ContentResolver resolver = mContext.getContentResolver();
1103 hasValidSound = Settings.System.getString(resolver,
1104 Settings.System.NOTIFICATION_SOUND) != null;
1105 } else if (notification.sound != null) {
1106 soundUri = notification.sound;
1107 hasValidSound = (soundUri != null);
1108 }
1109
1110 if (hasValidSound) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
1112 int audioStreamType;
1113 if (notification.audioStreamType >= 0) {
1114 audioStreamType = notification.audioStreamType;
1115 } else {
1116 audioStreamType = DEFAULT_STREAM_TYPE;
1117 }
1118 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -08001119 // do not play notifications if stream volume is 0
Jean-Michel Trivid6770542012-10-10 12:03:41 -07001120 // (typically because ringer mode is silent) or if speech recognition is active.
1121 if ((audioManager.getStreamVolume(audioStreamType) != 0)
1122 && !audioManager.isSpeechRecognitionActive()) {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001123 final long identity = Binder.clearCallingIdentity();
Eric Laurent524dc042009-11-27 05:07:55 -08001124 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001125 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1126 if (player != null) {
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001127 player.playAsync(soundUri, user, looping, audioStreamType);
Jeff Sharkey098d5802012-04-26 17:30:34 -07001128 }
1129 } catch (RemoteException e) {
1130 } finally {
Eric Laurent524dc042009-11-27 05:07:55 -08001131 Binder.restoreCallingIdentity(identity);
1132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 }
1134 }
1135
1136 // vibrate
Daniel Sandleredbb3802012-11-13 20:49:47 -08001137 // Does the notification want to specify its own vibration?
1138 final boolean hasCustomVibrate = notification.vibrate != null;
1139
David Agnew71789e12012-11-09 23:03:26 -05001140 // new in 4.2: if there was supposed to be a sound and we're in vibrate mode,
Daniel Sandler4a7a9b92012-11-20 12:59:41 -05001141 // and no other vibration is specified, we fall back to vibration
David Agnew71789e12012-11-09 23:03:26 -05001142 final boolean convertSoundToVibration =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001143 !hasCustomVibrate
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001144 && hasValidSound
David Agnew71789e12012-11-09 23:03:26 -05001145 && (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
1146
Daniel Sandler4a7a9b92012-11-20 12:59:41 -05001147 // The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 final boolean useDefaultVibrate =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001149 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
David Agnew71789e12012-11-09 23:03:26 -05001150
Daniel Sandleredbb3802012-11-13 20:49:47 -08001151 if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate)
Eric Laurentbffc3d12012-05-07 17:43:49 -07001152 && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 mVibrateNotification = r;
1154
Daniel Sandleredbb3802012-11-13 20:49:47 -08001155 if (useDefaultVibrate || convertSoundToVibration) {
1156 // Escalate privileges so we can use the vibrator even if the notifying app
1157 // does not have the VIBRATE permission.
1158 long identity = Binder.clearCallingIdentity();
1159 try {
Daniel Sandler4a7a9b92012-11-20 12:59:41 -05001160 mVibrator.vibrate(useDefaultVibrate ? mDefaultVibrationPattern
1161 : mFallbackVibrationPattern,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001162 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1163 } finally {
1164 Binder.restoreCallingIdentity(identity);
1165 }
1166 } else if (notification.vibrate.length > 1) {
1167 // If you want your own vibration pattern, you need the VIBRATE permission
1168 mVibrator.vibrate(notification.vibrate,
1169 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
1172 }
1173
1174 // this option doesn't shut off the lights
1175
1176 // light
1177 // the most recent thing gets the light
1178 mLights.remove(old);
1179 if (mLedNotification == old) {
1180 mLedNotification = null;
1181 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001182 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001184 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1185 && canInterrupt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 mLights.add(r);
1187 updateLightsLocked();
1188 } else {
1189 if (old != null
1190 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
1191 updateLightsLocked();
1192 }
1193 }
1194 }
1195
1196 idOut[0] = id;
1197 }
1198
Joe Onorato30275482009-07-08 17:09:14 -07001199 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001200 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
1201 if (!manager.isEnabled()) {
1202 return;
1203 }
1204
1205 AccessibilityEvent event =
1206 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
1207 event.setPackageName(packageName);
1208 event.setClassName(Notification.class.getName());
1209 event.setParcelableData(notification);
1210 CharSequence tickerText = notification.tickerText;
1211 if (!TextUtils.isEmpty(tickerText)) {
1212 event.getText().add(tickerText);
1213 }
1214
1215 manager.sendAccessibilityEvent(event);
1216 }
1217
Joe Onorato46439ce2010-11-19 13:56:21 -08001218 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
1219 // tell the app
1220 if (sendDelete) {
1221 if (r.notification.deleteIntent != null) {
1222 try {
1223 r.notification.deleteIntent.send();
1224 } catch (PendingIntent.CanceledException ex) {
1225 // do nothing - there's no relevant way to recover, and
1226 // no reason to let this propagate
1227 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
1228 }
1229 }
1230 }
1231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 // status bar
1233 if (r.notification.icon != 0) {
1234 long identity = Binder.clearCallingIdentity();
1235 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001236 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 }
1238 finally {
1239 Binder.restoreCallingIdentity(identity);
1240 }
1241 r.statusBarKey = null;
1242 }
1243
1244 // sound
1245 if (mSoundNotification == r) {
1246 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001247 final long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001249 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1250 if (player != null) {
1251 player.stopAsync();
1252 }
1253 } catch (RemoteException e) {
1254 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 Binder.restoreCallingIdentity(identity);
1256 }
1257 }
1258
1259 // vibrate
1260 if (mVibrateNotification == r) {
1261 mVibrateNotification = null;
1262 long identity = Binder.clearCallingIdentity();
1263 try {
1264 mVibrator.cancel();
1265 }
1266 finally {
1267 Binder.restoreCallingIdentity(identity);
1268 }
1269 }
1270
1271 // light
1272 mLights.remove(r);
1273 if (mLedNotification == r) {
1274 mLedNotification = null;
1275 }
1276 }
1277
1278 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001279 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001280 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 */
Fred Quintana6ecaff12009-09-25 14:23:13 -07001282 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001283 int mustNotHaveFlags, boolean sendDelete, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001284 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001285 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286
1287 synchronized (mNotificationList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001288 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001290 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
1293 return;
1294 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001295 if ((r.notification.flags & mustNotHaveFlags) != 0) {
1296 return;
1297 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 mNotificationList.remove(index);
1300
Joe Onorato46439ce2010-11-19 13:56:21 -08001301 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 updateLightsLocked();
1303 }
1304 }
1305 }
1306
1307 /**
Daniel Sandler321e9c52012-10-12 10:59:26 -07001308 * Determine whether the userId applies to the notification in question, either because
1309 * they match exactly, or one of them is USER_ALL (which is treated as a wildcard).
1310 */
1311 private boolean notificationMatchesUserId(NotificationRecord r, int userId) {
1312 return
1313 // looking for USER_ALL notifications? match everything
1314 userId == UserHandle.USER_ALL
1315 // a notification sent to USER_ALL matches any query
1316 || r.userId == UserHandle.USER_ALL
1317 // an exact user match
1318 || r.userId == userId;
1319 }
1320
1321 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 * Cancels all notifications from a given package that have all of the
1323 * {@code mustHaveFlags}.
1324 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001325 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001326 int mustNotHaveFlags, boolean doit, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001327 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, userId,
1328 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329
1330 synchronized (mNotificationList) {
1331 final int N = mNotificationList.size();
1332 boolean canceledSomething = false;
1333 for (int i = N-1; i >= 0; --i) {
1334 NotificationRecord r = mNotificationList.get(i);
Daniel Sandler321e9c52012-10-12 10:59:26 -07001335 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001336 continue;
1337 }
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001338 // Don't remove notifications to all, if there's no package name specified
1339 if (r.userId == UserHandle.USER_ALL && pkg == null) {
1340 continue;
1341 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
1343 continue;
1344 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001345 if ((r.notification.flags & mustNotHaveFlags) != 0) {
1346 continue;
1347 }
John Spurlock078a4902012-10-04 12:00:53 -04001348 if (pkg != null && !r.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 continue;
1350 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001351 canceledSomething = true;
1352 if (!doit) {
1353 return true;
1354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001356 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 }
1358 if (canceledSomething) {
1359 updateLightsLocked();
1360 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001361 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 }
1363 }
1364
Dianne Hackborn41203752012-08-31 14:05:51 -07001365 public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001366 checkCallerIsSystemOrSameApp(pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001367 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001368 Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001369 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001370 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001371 Binder.getCallingUid() == Process.SYSTEM_UID
Dianne Hackborn41203752012-08-31 14:05:51 -07001372 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 }
1374
Dianne Hackborn41203752012-08-31 14:05:51 -07001375 public void cancelAllNotifications(String pkg, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001376 checkCallerIsSystemOrSameApp(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001377
Dianne Hackborn41203752012-08-31 14:05:51 -07001378 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001379 Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001380
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001381 // Calling from user space, don't allow the canceling of actively
1382 // running foreground services.
Dianne Hackborn41203752012-08-31 14:05:51 -07001383 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 }
1385
Daniel Sandler0da673f2012-04-11 12:33:16 -04001386 void checkCallerIsSystem() {
1387 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001388 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001389 return;
1390 }
1391 throw new SecurityException("Disallowed call for uid " + uid);
1392 }
1393
1394 void checkCallerIsSystemOrSameApp(String pkg) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001395 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001396 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001397 return;
1398 }
1399 try {
Amith Yamasanif203aee2012-08-29 18:41:53 -07001400 ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
1401 pkg, 0, UserHandle.getCallingUserId());
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001402 if (!UserHandle.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001403 throw new SecurityException("Calling uid " + uid + " gave package"
1404 + pkg + " which is owned by uid " + ai.uid);
1405 }
Amith Yamasanif203aee2012-08-29 18:41:53 -07001406 } catch (RemoteException re) {
1407 throw new SecurityException("Unknown package " + pkg + "\n" + re);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001408 }
1409 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001410
Dianne Hackborn41203752012-08-31 14:05:51 -07001411 void cancelAll(int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 synchronized (mNotificationList) {
1413 final int N = mNotificationList.size();
1414 for (int i=N-1; i>=0; i--) {
1415 NotificationRecord r = mNotificationList.get(i);
1416
Daniel Sandler321e9c52012-10-12 10:59:26 -07001417 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001418 continue;
1419 }
1420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1422 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001424 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 }
1426 }
1427
1428 updateLightsLocked();
1429 }
1430 }
1431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 // lock on mNotificationList
1433 private void updateLightsLocked()
1434 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001435 // handle notification lights
1436 if (mLedNotification == null) {
1437 // get next notification, if any
1438 int n = mLights.size();
1439 if (n > 0) {
1440 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
1442 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001443
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001444 // Don't flash while we are in a call or screen is on
1445 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001446 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001447 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001448 int ledARGB = mLedNotification.notification.ledARGB;
1449 int ledOnMS = mLedNotification.notification.ledOnMS;
1450 int ledOffMS = mLedNotification.notification.ledOffMS;
1451 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1452 ledARGB = mDefaultNotificationColor;
1453 ledOnMS = mDefaultNotificationLedOn;
1454 ledOffMS = mDefaultNotificationLedOff;
1455 }
1456 if (mNotificationPulseEnabled) {
1457 // pulse repeatedly
1458 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1459 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05001460 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 }
1463
1464 // lock on mNotificationList
Dianne Hackborn41203752012-08-31 14:05:51 -07001465 private int indexOfNotificationLocked(String pkg, String tag, int id, int userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 {
1467 ArrayList<NotificationRecord> list = mNotificationList;
1468 final int len = list.size();
1469 for (int i=0; i<len; i++) {
1470 NotificationRecord r = list.get(i);
Daniel Sandler321e9c52012-10-12 10:59:26 -07001471 if (!notificationMatchesUserId(r, userId) || r.id != id) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001472 continue;
1473 }
Fred Quintana6ecaff12009-09-25 14:23:13 -07001474 if (tag == null) {
1475 if (r.tag != null) {
1476 continue;
1477 }
1478 } else {
1479 if (!tag.equals(r.tag)) {
1480 continue;
1481 }
1482 }
Dianne Hackborn41203752012-08-31 14:05:51 -07001483 if (r.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 return i;
1485 }
1486 }
1487 return -1;
1488 }
1489
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001490 private void updateNotificationPulse() {
1491 synchronized (mNotificationList) {
1492 updateLightsLocked();
1493 }
1494 }
1495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 // ======================================================================
1497 @Override
1498 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1499 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1500 != PackageManager.PERMISSION_GRANTED) {
1501 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1502 + Binder.getCallingPid()
1503 + ", uid=" + Binder.getCallingUid());
1504 return;
1505 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 pw.println("Current Notification Manager state:");
1508
1509 int N;
1510
1511 synchronized (mToastQueue) {
1512 N = mToastQueue.size();
1513 if (N > 0) {
1514 pw.println(" Toast Queue:");
1515 for (int i=0; i<N; i++) {
1516 mToastQueue.get(i).dump(pw, " ");
1517 }
1518 pw.println(" ");
1519 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 }
1522
1523 synchronized (mNotificationList) {
1524 N = mNotificationList.size();
1525 if (N > 0) {
1526 pw.println(" Notification List:");
1527 for (int i=0; i<N; i++) {
1528 mNotificationList.get(i).dump(pw, " ", mContext);
1529 }
1530 pw.println(" ");
1531 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 N = mLights.size();
1534 if (N > 0) {
1535 pw.println(" Lights List:");
1536 for (int i=0; i<N; i++) {
1537 mLights.get(i).dump(pw, " ", mContext);
1538 }
1539 pw.println(" ");
1540 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 pw.println(" mSoundNotification=" + mSoundNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001544 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1545 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 }
1547 }
1548}