2009-03-03 19:31:44 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.server;
|
|
|
|
|
2012-04-26 17:30:34 -07:00
|
|
|
import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
|
|
|
|
import static org.xmlpull.v1.XmlPullParser.END_TAG;
|
|
|
|
import static org.xmlpull.v1.XmlPullParser.START_TAG;
|
2009-05-14 22:28:01 -07:00
|
|
|
|
2012-08-31 14:05:51 -07:00
|
|
|
import android.app.ActivityManager;
|
2009-03-03 19:31:44 -08:00
|
|
|
import android.app.ActivityManagerNative;
|
2012-08-29 18:41:53 -07:00
|
|
|
import android.app.AppGlobals;
|
2013-01-30 14:04:10 -05:00
|
|
|
import android.app.AppOpsManager;
|
2009-03-03 19:31:44 -08:00
|
|
|
import android.app.IActivityManager;
|
|
|
|
import android.app.INotificationManager;
|
|
|
|
import android.app.ITransientNotification;
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.app.StatusBarManager;
|
|
|
|
import android.content.BroadcastReceiver;
|
2009-06-26 18:16:48 -07:00
|
|
|
import android.content.ContentResolver;
|
2009-03-03 19:31:44 -08:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
2009-08-17 23:33:56 -07:00
|
|
|
import android.content.pm.ApplicationInfo;
|
2013-01-30 14:04:10 -05:00
|
|
|
import android.content.pm.PackageInfo;
|
2009-03-03 19:31:44 -08:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.content.pm.PackageManager.NameNotFoundException;
|
|
|
|
import android.content.res.Resources;
|
2009-06-26 18:16:48 -07:00
|
|
|
import android.database.ContentObserver;
|
2009-05-14 22:28:01 -07:00
|
|
|
import android.media.AudioManager;
|
2012-04-26 17:30:34 -07:00
|
|
|
import android.media.IAudioService;
|
|
|
|
import android.media.IRingtonePlayer;
|
2009-03-03 19:31:44 -08:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Binder;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.IBinder;
|
|
|
|
import android.os.Message;
|
2009-08-17 23:33:56 -07:00
|
|
|
import android.os.Process;
|
2009-05-14 22:28:01 -07:00
|
|
|
import android.os.RemoteException;
|
2012-04-26 17:30:34 -07:00
|
|
|
import android.os.ServiceManager;
|
2012-08-16 10:48:27 -07:00
|
|
|
import android.os.UserHandle;
|
2009-03-03 19:31:44 -08:00
|
|
|
import android.os.Vibrator;
|
|
|
|
import android.provider.Settings;
|
2010-03-11 13:38:06 -05:00
|
|
|
import android.telephony.TelephonyManager;
|
2009-05-14 22:28:01 -07:00
|
|
|
import android.text.TextUtils;
|
2012-07-31 17:54:35 -07:00
|
|
|
import android.util.AtomicFile;
|
2009-03-03 19:31:44 -08:00
|
|
|
import android.util.EventLog;
|
|
|
|
import android.util.Log;
|
2010-12-03 14:29:16 -08:00
|
|
|
import android.util.Slog;
|
2012-04-11 12:33:16 -04:00
|
|
|
import android.util.Xml;
|
2009-05-14 22:28:01 -07:00
|
|
|
import android.view.accessibility.AccessibilityEvent;
|
|
|
|
import android.view.accessibility.AccessibilityManager;
|
2009-03-03 19:31:44 -08:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2012-04-26 17:30:34 -07:00
|
|
|
import com.android.internal.statusbar.StatusBarNotification;
|
|
|
|
|
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
|
|
|
2012-04-11 12:33:16 -04:00
|
|
|
import java.io.File;
|
2009-03-03 19:31:44 -08:00
|
|
|
import java.io.FileDescriptor;
|
2012-04-11 12:33:16 -04:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
2009-03-03 19:31:44 -08:00
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
2012-04-11 12:33:16 -04:00
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
import libcore.io.IoUtils;
|
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
|
2010-08-03 15:29:31 -04:00
|
|
|
/** {@hide} */
|
|
|
|
public class NotificationManagerService extends INotificationManager.Stub
|
2009-03-03 19:31:44 -08:00
|
|
|
{
|
|
|
|
private static final String TAG = "NotificationService";
|
|
|
|
private static final boolean DBG = false;
|
|
|
|
|
2010-06-04 11:44:54 -07:00
|
|
|
private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
|
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
// message codes
|
|
|
|
private static final int MESSAGE_TIMEOUT = 2;
|
|
|
|
|
|
|
|
private static final int LONG_DELAY = 3500; // 3.5 seconds
|
|
|
|
private static final int SHORT_DELAY = 2000; // 2 seconds
|
2009-12-04 10:31:43 -08:00
|
|
|
|
|
|
|
private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
|
2012-11-13 20:49:47 -08:00
|
|
|
private static final int VIBRATE_PATTERN_MAXLEN = 8 * 2 + 1; // up to eight bumps
|
2009-03-03 19:31:44 -08:00
|
|
|
|
|
|
|
private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
|
2012-03-28 15:46:39 -04:00
|
|
|
private static final boolean SCORE_ONGOING_HIGHER = false;
|
2009-03-03 19:31:44 -08:00
|
|
|
|
2012-04-11 12:33:16 -04:00
|
|
|
private static final int JUNK_SCORE = -1000;
|
|
|
|
private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10;
|
|
|
|
private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER;
|
|
|
|
|
2012-12-04 14:51:50 -05:00
|
|
|
// Notifications with scores below this will not interrupt the user, either via LED or
|
|
|
|
// sound or vibration
|
|
|
|
private static final int SCORE_INTERRUPTION_THRESHOLD =
|
|
|
|
Notification.PRIORITY_LOW * NOTIFICATION_PRIORITY_MULTIPLIER;
|
|
|
|
|
2012-04-11 12:33:16 -04:00
|
|
|
private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true;
|
|
|
|
private static final boolean ENABLE_BLOCKED_TOASTS = true;
|
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
final Context mContext;
|
|
|
|
final IActivityManager mAm;
|
|
|
|
final IBinder mForegroundToken = new Binder();
|
|
|
|
|
|
|
|
private WorkerHandler mHandler;
|
2010-04-12 08:18:45 -07:00
|
|
|
private StatusBarManagerService mStatusBar;
|
2009-11-27 14:25:58 -05:00
|
|
|
private LightsService.Light mNotificationLight;
|
|
|
|
private LightsService.Light mAttentionLight;
|
2009-03-03 19:31:44 -08:00
|
|
|
|
2010-01-20 12:13:36 -05:00
|
|
|
private int mDefaultNotificationColor;
|
|
|
|
private int mDefaultNotificationLedOn;
|
|
|
|
private int mDefaultNotificationLedOff;
|
|
|
|
|
2012-11-13 20:49:47 -08:00
|
|
|
private long[] mDefaultVibrationPattern;
|
|
|
|
private long[] mFallbackVibrationPattern;
|
|
|
|
|
2009-07-08 17:09:14 -07:00
|
|
|
private boolean mSystemReady;
|
2009-07-23 12:29:19 -04:00
|
|
|
private int mDisabledNotifications;
|
2009-03-03 19:31:44 -08:00
|
|
|
|
2012-04-26 17:30:34 -07:00
|
|
|
private NotificationRecord mSoundNotification;
|
2009-03-03 19:31:44 -08:00
|
|
|
private NotificationRecord mVibrateNotification;
|
2012-04-26 17:30:34 -07:00
|
|
|
|
|
|
|
private IAudioService mAudioService;
|
2012-04-13 01:55:38 -07:00
|
|
|
private Vibrator mVibrator;
|
2009-03-03 19:31:44 -08:00
|
|
|
|
2009-12-02 11:15:02 -05:00
|
|
|
// for enabling and disabling notification pulse behavior
|
2011-08-30 09:55:30 -04:00
|
|
|
private boolean mScreenOn = true;
|
2010-03-11 13:38:06 -05:00
|
|
|
private boolean mInCall = false;
|
2009-12-02 11:15:02 -05:00
|
|
|
private boolean mNotificationPulseEnabled;
|
|
|
|
|
2009-09-25 14:23:13 -07:00
|
|
|
private final ArrayList<NotificationRecord> mNotificationList =
|
|
|
|
new ArrayList<NotificationRecord>();
|
2009-03-03 19:31:44 -08:00
|
|
|
|
|
|
|
private ArrayList<ToastRecord> mToastQueue;
|
|
|
|
|
|
|
|
private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
|
|
|
|
private NotificationRecord mLedNotification;
|
2009-05-14 22:28:01 -07:00
|
|
|
|
2013-01-30 14:04:10 -05:00
|
|
|
private final AppOpsManager mAppOps;
|
|
|
|
|
2012-04-11 12:33:16 -04:00
|
|
|
// Notification control database. For now just contains disabled packages.
|
|
|
|
private AtomicFile mPolicyFile;
|
|
|
|
private HashSet<String> mBlockedPackages = new HashSet<String>();
|
|
|
|
|
|
|
|
private static final int DB_VERSION = 1;
|
|
|
|
|
|
|
|
private static final String TAG_BODY = "notification-policy";
|
|
|
|
private static final String ATTR_VERSION = "version";
|
|
|
|
|
|
|
|
private static final String TAG_BLOCKED_PKGS = "blocked-packages";
|
|
|
|
private static final String TAG_PACKAGE = "package";
|
|
|
|
private static final String ATTR_NAME = "name";
|
|
|
|
|
|
|
|
private void loadBlockDb() {
|
|
|
|
synchronized(mBlockedPackages) {
|
|
|
|
if (mPolicyFile == null) {
|
|
|
|
File dir = new File("/data/system");
|
|
|
|
mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml"));
|
|
|
|
|
|
|
|
mBlockedPackages.clear();
|
|
|
|
|
|
|
|
FileInputStream infile = null;
|
|
|
|
try {
|
|
|
|
infile = mPolicyFile.openRead();
|
|
|
|
final XmlPullParser parser = Xml.newPullParser();
|
|
|
|
parser.setInput(infile, null);
|
|
|
|
|
|
|
|
int type;
|
|
|
|
String tag;
|
|
|
|
int version = DB_VERSION;
|
|
|
|
while ((type = parser.next()) != END_DOCUMENT) {
|
|
|
|
tag = parser.getName();
|
|
|
|
if (type == START_TAG) {
|
|
|
|
if (TAG_BODY.equals(tag)) {
|
|
|
|
version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
|
|
|
|
} else if (TAG_BLOCKED_PKGS.equals(tag)) {
|
|
|
|
while ((type = parser.next()) != END_DOCUMENT) {
|
|
|
|
tag = parser.getName();
|
|
|
|
if (TAG_PACKAGE.equals(tag)) {
|
|
|
|
mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME));
|
|
|
|
} else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
// No data yet
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.wtf(TAG, "Unable to read blocked notifications database", e);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
Log.wtf(TAG, "Unable to parse blocked notifications database", e);
|
|
|
|
} catch (XmlPullParserException e) {
|
|
|
|
Log.wtf(TAG, "Unable to parse blocked notifications database", e);
|
|
|
|
} finally {
|
|
|
|
IoUtils.closeQuietly(infile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 14:04:10 -05:00
|
|
|
/**
|
|
|
|
* Use this when you just want to know if notifications are OK for this package.
|
|
|
|
*/
|
|
|
|
public boolean areNotificationsEnabledForPackage(String pkg, int uid) {
|
2012-04-11 12:33:16 -04:00
|
|
|
checkCallerIsSystem();
|
2013-01-30 14:04:10 -05:00
|
|
|
return (mAppOps.checkOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
|
|
|
|
== AppOpsManager.MODE_ALLOWED);
|
2012-04-11 12:33:16 -04:00
|
|
|
}
|
|
|
|
|
2013-01-30 14:04:10 -05:00
|
|
|
/** Use this when you actually want to post a notification or toast.
|
|
|
|
*
|
|
|
|
* Unchecked. Not exposed via Binder, but can be called in the course of enqueue*().
|
|
|
|
*/
|
|
|
|
private boolean noteNotificationOp(String pkg, int uid) {
|
|
|
|
if (mAppOps.noteOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
|
|
|
|
!= AppOpsManager.MODE_ALLOWED) {
|
|
|
|
Slog.v(TAG, "notifications are disabled by AppOps for " + pkg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2012-04-11 12:33:16 -04:00
|
|
|
}
|
|
|
|
|
2013-01-30 14:04:10 -05:00
|
|
|
public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) {
|
2012-04-11 12:33:16 -04:00
|
|
|
checkCallerIsSystem();
|
2013-01-30 14:04:10 -05:00
|
|
|
if (true||DBG) {
|
2012-04-11 12:33:16 -04:00
|
|
|
Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);
|
|
|
|
}
|
2013-01-30 14:04:10 -05:00
|
|
|
mAppOps.setMode(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg,
|
|
|
|
enabled ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED);
|
2012-04-11 12:33:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
private static String idDebugString(Context baseContext, String packageName, int id) {
|
|
|
|
Context c = null;
|
|
|
|
|
|
|
|
if (packageName != null) {
|
|
|
|
try {
|
|
|
|
c = baseContext.createPackageContext(packageName, 0);
|
|
|
|
} catch (NameNotFoundException e) {
|
|
|
|
c = baseContext;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c = baseContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
String pkg;
|
|
|
|
String type;
|
|
|
|
String name;
|
|
|
|
|
|
|
|
Resources r = c.getResources();
|
|
|
|
try {
|
|
|
|
return r.getResourceName(id);
|
|
|
|
} catch (Resources.NotFoundException e) {
|
|
|
|
return "<name unknown>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final class NotificationRecord
|
|
|
|
{
|
2009-09-25 14:23:13 -07:00
|
|
|
final String pkg;
|
|
|
|
final String tag;
|
|
|
|
final int id;
|
2010-06-24 15:57:42 -07:00
|
|
|
final int uid;
|
|
|
|
final int initialPid;
|
2012-08-31 14:05:51 -07:00
|
|
|
final int userId;
|
2009-09-25 14:23:13 -07:00
|
|
|
final Notification notification;
|
2012-02-13 21:04:12 -05:00
|
|
|
final int score;
|
2009-03-03 19:31:44 -08:00
|
|
|
IBinder statusBarKey;
|
|
|
|
|
2012-08-31 14:05:51 -07:00
|
|
|
NotificationRecord(String pkg, String tag, int id, int uid, int initialPid,
|
|
|
|
int userId, int score, Notification notification)
|
2009-03-03 19:31:44 -08:00
|
|
|
{
|
|
|
|
this.pkg = pkg;
|
2009-09-25 14:23:13 -07:00
|
|
|
this.tag = tag;
|
2009-03-03 19:31:44 -08:00
|
|
|
this.id = id;
|
2010-06-24 15:57:42 -07:00
|
|
|
this.uid = uid;
|
|
|
|
this.initialPid = initialPid;
|
2012-08-31 14:05:51 -07:00
|
|
|
this.userId = userId;
|
2012-02-13 21:04:12 -05:00
|
|
|
this.score = score;
|
2009-03-03 19:31:44 -08:00
|
|
|
this.notification = notification;
|
|
|
|
}
|
2009-09-25 14:23:13 -07:00
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
void dump(PrintWriter pw, String prefix, Context baseContext) {
|
|
|
|
pw.println(prefix + this);
|
|
|
|
pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
|
|
|
|
+ " / " + idDebugString(baseContext, this.pkg, notification.icon));
|
2012-02-13 21:04:12 -05:00
|
|
|
pw.println(prefix + " pri=" + notification.priority);
|
|
|
|
pw.println(prefix + " score=" + this.score);
|
2009-03-03 19:31:44 -08:00
|
|
|
pw.println(prefix + " contentIntent=" + notification.contentIntent);
|
|
|
|
pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
|
|
|
|
pw.println(prefix + " tickerText=" + notification.tickerText);
|
|
|
|
pw.println(prefix + " contentView=" + notification.contentView);
|
2012-08-31 14:05:51 -07:00
|
|
|
pw.println(prefix + " uid=" + uid + " userId=" + userId);
|
2009-03-03 19:31:44 -08:00
|
|
|
pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
|
|
|
|
pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
|
|
|
|
pw.println(prefix + " sound=" + notification.sound);
|
|
|
|
pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
|
|
|
|
pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
|
|
|
|
+ " ledOnMS=" + notification.ledOnMS
|
|
|
|
+ " ledOffMS=" + notification.ledOffMS);
|
|
|
|
}
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
@Override
|
|
|
|
public final String toString()
|
|
|
|
{
|
|
|
|
return "NotificationRecord{"
|
|
|
|
+ Integer.toHexString(System.identityHashCode(this))
|
|
|
|
+ " pkg=" + pkg
|
2009-09-25 14:23:13 -07:00
|
|
|
+ " id=" + Integer.toHexString(id)
|
2011-02-03 14:51:35 -05:00
|
|
|
+ " tag=" + tag
|
2012-02-13 21:04:12 -05:00
|
|
|
+ " score=" + score
|
2011-02-03 14:51:35 -05:00
|
|
|
+ "}";
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final class ToastRecord
|
|
|
|
{
|
|
|
|
final int pid;
|
|
|
|
final String pkg;
|
|
|
|
final ITransientNotification callback;
|
|
|
|
int duration;
|
|
|
|
|
|
|
|
ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
|
|
|
|
{
|
|
|
|
this.pid = pid;
|
|
|
|
this.pkg = pkg;
|
|
|
|
this.callback = callback;
|
|
|
|
this.duration = duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
void update(int duration) {
|
|
|
|
this.duration = duration;
|
|
|
|
}
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
void dump(PrintWriter pw, String prefix) {
|
|
|
|
pw.println(prefix + this);
|
|
|
|
}
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
@Override
|
|
|
|
public final String toString()
|
|
|
|
{
|
|
|
|
return "ToastRecord{"
|
|
|
|
+ Integer.toHexString(System.identityHashCode(this))
|
|
|
|
+ " pkg=" + pkg
|
|
|
|
+ " callback=" + callback
|
|
|
|
+ " duration=" + duration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-12 08:18:45 -07:00
|
|
|
private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
|
|
|
|
= new StatusBarManagerService.NotificationCallbacks() {
|
2009-03-03 19:31:44 -08:00
|
|
|
|
|
|
|
public void onSetDisabled(int status) {
|
|
|
|
synchronized (mNotificationList) {
|
|
|
|
mDisabledNotifications = status;
|
|
|
|
if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
|
|
|
|
// cancel whatever's going on
|
|
|
|
long identity = Binder.clearCallingIdentity();
|
|
|
|
try {
|
2012-04-26 17:30:34 -07:00
|
|
|
final IRingtonePlayer player = mAudioService.getRingtonePlayer();
|
|
|
|
if (player != null) {
|
|
|
|
player.stopAsync();
|
|
|
|
}
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
} finally {
|
2009-03-03 19:31:44 -08:00
|
|
|
Binder.restoreCallingIdentity(identity);
|
|
|
|
}
|
|
|
|
|
|
|
|
identity = Binder.clearCallingIdentity();
|
|
|
|
try {
|
|
|
|
mVibrator.cancel();
|
2012-04-26 17:30:34 -07:00
|
|
|
} finally {
|
2009-03-03 19:31:44 -08:00
|
|
|
Binder.restoreCallingIdentity(identity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onClearAll() {
|
2012-08-31 14:05:51 -07:00
|
|
|
// XXX to be totally correct, the caller should tell us which user
|
|
|
|
// this is for.
|
|
|
|
cancelAll(ActivityManager.getCurrentUser());
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
|
2009-09-25 14:23:13 -07:00
|
|
|
public void onNotificationClick(String pkg, String tag, int id) {
|
2012-08-31 14:05:51 -07:00
|
|
|
// XXX to be totally correct, the caller should tell us which user
|
|
|
|
// this is for.
|
2009-09-25 14:23:13 -07:00
|
|
|
cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
|
2012-08-31 14:05:51 -07:00
|
|
|
Notification.FLAG_FOREGROUND_SERVICE, false,
|
|
|
|
ActivityManager.getCurrentUser());
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
|
2010-08-04 15:54:58 -04:00
|
|
|
public void onNotificationClear(String pkg, String tag, int id) {
|
2012-08-31 14:05:51 -07:00
|
|
|
// XXX to be totally correct, the caller should tell us which user
|
|
|
|
// this is for.
|
2010-11-19 13:56:21 -08:00
|
|
|
cancelNotification(pkg, tag, id, 0,
|
|
|
|
Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
|
2012-08-31 14:05:51 -07:00
|
|
|
true, ActivityManager.getCurrentUser());
|
2010-08-04 15:54:58 -04:00
|
|
|
}
|
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
public void onPanelRevealed() {
|
|
|
|
synchronized (mNotificationList) {
|
|
|
|
// sound
|
|
|
|
mSoundNotification = null;
|
2012-04-26 17:30:34 -07:00
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
long identity = Binder.clearCallingIdentity();
|
|
|
|
try {
|
2012-04-26 17:30:34 -07:00
|
|
|
final IRingtonePlayer player = mAudioService.getRingtonePlayer();
|
|
|
|
if (player != null) {
|
|
|
|
player.stopAsync();
|
|
|
|
}
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
} finally {
|
2009-03-03 19:31:44 -08:00
|
|
|
Binder.restoreCallingIdentity(identity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// vibrate
|
|
|
|
mVibrateNotification = null;
|
|
|
|
identity = Binder.clearCallingIdentity();
|
|
|
|
try {
|
|
|
|
mVibrator.cancel();
|
2012-04-26 17:30:34 -07:00
|
|
|
} finally {
|
2009-03-03 19:31:44 -08:00
|
|
|
Binder.restoreCallingIdentity(identity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// light
|
|
|
|
mLights.clear();
|
|
|
|
mLedNotification = null;
|
|
|
|
updateLightsLocked();
|
|
|
|
}
|
|
|
|
}
|
2010-06-04 16:08:02 -04:00
|
|
|
|
2010-06-24 15:57:42 -07:00
|
|
|
public void onNotificationError(String pkg, String tag, int id,
|
|
|
|
int uid, int initialPid, String message) {
|
2010-08-03 15:29:31 -04:00
|
|
|
Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
|
|
|
|
+ "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
|
2012-08-31 14:05:51 -07:00
|
|
|
// XXX to be totally correct, the caller should tell us which user
|
|
|
|
// this is for.
|
|
|
|
cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid));
|
2010-06-24 15:57:42 -07:00
|
|
|
long ident = Binder.clearCallingIdentity();
|
|
|
|
try {
|
|
|
|
ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
|
|
|
|
"Bad notification posted from package " + pkg
|
|
|
|
+ ": " + message);
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
}
|
|
|
|
Binder.restoreCallingIdentity(ident);
|
2010-06-04 16:08:02 -04:00
|
|
|
}
|
2009-03-03 19:31:44 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
|
2010-02-19 17:02:21 -08:00
|
|
|
boolean queryRestart = false;
|
2012-06-01 15:38:46 -04:00
|
|
|
boolean packageChanged = false;
|
2010-02-19 17:02:21 -08:00
|
|
|
|
2011-06-12 19:35:45 -04:00
|
|
|
if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
|
2010-01-28 09:57:30 -08:00
|
|
|
|| action.equals(Intent.ACTION_PACKAGE_RESTARTED)
|
2012-06-01 15:38:46 -04:00
|
|
|
|| (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED))
|
2010-02-19 17:02:21 -08:00
|
|
|
|| (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
|
2010-02-04 22:51:07 -08:00
|
|
|
|| action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
|
2010-01-28 09:57:30 -08:00
|
|
|
String pkgList[] = null;
|
2010-02-04 22:51:07 -08:00
|
|
|
if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
|
2010-01-28 09:57:30 -08:00
|
|
|
pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
|
2010-02-19 17:02:21 -08:00
|
|
|
} else if (queryRestart) {
|
|
|
|
pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
|
2010-01-28 09:57:30 -08:00
|
|
|
} else {
|
|
|
|
Uri uri = intent.getData();
|
|
|
|
if (uri == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String pkgName = uri.getSchemeSpecificPart();
|
|
|
|
if (pkgName == null) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-01 15:38:46 -04:00
|
|
|
if (packageChanged) {
|
|
|
|
// We cancel notifications for packages which have just been disabled
|
|
|
|
final int enabled = mContext.getPackageManager()
|
|
|
|
.getApplicationEnabledSetting(pkgName);
|
|
|
|
if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
|
|
|
|
|| enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-01-28 09:57:30 -08:00
|
|
|
pkgList = new String[]{pkgName};
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
2010-01-28 09:57:30 -08:00
|
|
|
if (pkgList != null && (pkgList.length > 0)) {
|
|
|
|
for (String pkgName : pkgList) {
|
2012-08-31 14:05:51 -07:00
|
|
|
cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart,
|
|
|
|
UserHandle.USER_ALL);
|
2010-01-28 09:57:30 -08:00
|
|
|
}
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
2011-08-30 09:55:30 -04:00
|
|
|
} else if (action.equals(Intent.ACTION_SCREEN_ON)) {
|
|
|
|
// Keep track of screen on/off state, but do not turn off the notification light
|
|
|
|
// until user passes through the lock screen or views the notification.
|
|
|
|
mScreenOn = true;
|
|
|
|
} else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
|
|
|
|
mScreenOn = false;
|
2010-03-11 13:38:06 -05:00
|
|
|
} else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
|
2011-08-30 09:55:30 -04:00
|
|
|
mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
|
|
|
|
TelephonyManager.EXTRA_STATE_OFFHOOK));
|
2010-03-11 13:38:06 -05:00
|
|
|
updateNotificationPulse();
|
2012-08-27 19:18:31 -07:00
|
|
|
} else if (action.equals(Intent.ACTION_USER_STOPPED)) {
|
|
|
|
int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
|
|
|
|
if (userHandle >= 0) {
|
2012-08-31 14:05:51 -07:00
|
|
|
cancelAllNotificationsInt(null, 0, 0, true, userHandle);
|
2012-08-27 19:18:31 -07:00
|
|
|
}
|
2011-08-30 09:55:30 -04:00
|
|
|
} else if (action.equals(Intent.ACTION_USER_PRESENT)) {
|
|
|
|
// turn off LED when user passes through lock screen
|
|
|
|
mNotificationLight.turnOff();
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-06-26 18:16:48 -07:00
|
|
|
class SettingsObserver extends ContentObserver {
|
|
|
|
SettingsObserver(Handler handler) {
|
|
|
|
super(handler);
|
|
|
|
}
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2009-06-26 18:16:48 -07:00
|
|
|
void observe() {
|
|
|
|
ContentResolver resolver = mContext.getContentResolver();
|
2009-12-02 11:15:02 -05:00
|
|
|
resolver.registerContentObserver(Settings.System.getUriFor(
|
|
|
|
Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
|
2009-06-26 18:16:48 -07:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override public void onChange(boolean selfChange) {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void update() {
|
|
|
|
ContentResolver resolver = mContext.getContentResolver();
|
2009-12-02 11:15:02 -05:00
|
|
|
boolean pulseEnabled = Settings.System.getInt(resolver,
|
|
|
|
Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
|
|
|
|
if (mNotificationPulseEnabled != pulseEnabled) {
|
|
|
|
mNotificationPulseEnabled = pulseEnabled;
|
|
|
|
updateNotificationPulse();
|
|
|
|
}
|
2009-06-26 18:16:48 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-02 11:15:02 -05:00
|
|
|
|
2012-11-13 20:49:47 -08:00
|
|
|
static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) {
|
|
|
|
int[] ar = r.getIntArray(resid);
|
|
|
|
if (ar == null) {
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
final int len = ar.length > maxlen ? maxlen : ar.length;
|
|
|
|
long[] out = new long[len];
|
|
|
|
for (int i=0; i<len; i++) {
|
|
|
|
out[i] = ar[i];
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2010-04-12 08:18:45 -07:00
|
|
|
NotificationManagerService(Context context, StatusBarManagerService statusBar,
|
2009-11-24 00:30:52 -05:00
|
|
|
LightsService lights)
|
2009-03-03 19:31:44 -08:00
|
|
|
{
|
|
|
|
super();
|
|
|
|
mContext = context;
|
2012-04-13 01:55:38 -07:00
|
|
|
mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
|
2009-03-03 19:31:44 -08:00
|
|
|
mAm = ActivityManagerNative.getDefault();
|
|
|
|
mToastQueue = new ArrayList<ToastRecord>();
|
|
|
|
mHandler = new WorkerHandler();
|
2010-02-04 20:54:43 -08:00
|
|
|
|
2013-01-30 14:04:10 -05:00
|
|
|
mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
|
|
|
|
|
|
|
|
importOldBlockDb();
|
2012-04-11 12:33:16 -04:00
|
|
|
|
2010-04-12 08:18:45 -07:00
|
|
|
mStatusBar = statusBar;
|
2009-03-03 19:31:44 -08:00
|
|
|
statusBar.setNotificationCallbacks(mNotificationCallbacks);
|
|
|
|
|
2009-11-27 14:25:58 -05:00
|
|
|
mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
|
|
|
|
mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
|
|
|
|
|
2010-01-20 12:13:36 -05:00
|
|
|
Resources resources = mContext.getResources();
|
|
|
|
mDefaultNotificationColor = resources.getColor(
|
|
|
|
com.android.internal.R.color.config_defaultNotificationColor);
|
|
|
|
mDefaultNotificationLedOn = resources.getInteger(
|
|
|
|
com.android.internal.R.integer.config_defaultNotificationLedOn);
|
|
|
|
mDefaultNotificationLedOff = resources.getInteger(
|
|
|
|
com.android.internal.R.integer.config_defaultNotificationLedOff);
|
|
|
|
|
2012-11-13 20:49:47 -08:00
|
|
|
mDefaultVibrationPattern = getLongArray(resources,
|
|
|
|
com.android.internal.R.array.config_defaultNotificationVibePattern,
|
|
|
|
VIBRATE_PATTERN_MAXLEN,
|
|
|
|
DEFAULT_VIBRATE_PATTERN);
|
|
|
|
|
|
|
|
mFallbackVibrationPattern = getLongArray(resources,
|
|
|
|
com.android.internal.R.array.config_notificationFallbackVibePattern,
|
|
|
|
VIBRATE_PATTERN_MAXLEN,
|
|
|
|
DEFAULT_VIBRATE_PATTERN);
|
|
|
|
|
2009-07-23 12:29:19 -04:00
|
|
|
// Don't start allowing notifications until the setup wizard has run once.
|
|
|
|
// After that, including subsequent boots, init with notifications turned on.
|
|
|
|
// This works on the first boot because the setup wizard will toggle this
|
|
|
|
// flag at least once and we'll go back to 0 after that.
|
2012-09-25 15:03:20 -07:00
|
|
|
if (0 == Settings.Global.getInt(mContext.getContentResolver(),
|
|
|
|
Settings.Global.DEVICE_PROVISIONED, 0)) {
|
2009-07-23 12:29:19 -04:00
|
|
|
mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
|
|
|
|
}
|
|
|
|
|
2010-11-30 19:53:36 -05:00
|
|
|
// register for various Intents
|
2009-03-03 19:31:44 -08:00
|
|
|
IntentFilter filter = new IntentFilter();
|
2009-12-02 11:15:02 -05:00
|
|
|
filter.addAction(Intent.ACTION_SCREEN_ON);
|
|
|
|
filter.addAction(Intent.ACTION_SCREEN_OFF);
|
2010-03-11 13:38:06 -05:00
|
|
|
filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
|
2011-08-30 09:55:30 -04:00
|
|
|
filter.addAction(Intent.ACTION_USER_PRESENT);
|
2012-08-27 19:18:31 -07:00
|
|
|
filter.addAction(Intent.ACTION_USER_STOPPED);
|
2009-03-03 19:31:44 -08:00
|
|
|
mContext.registerReceiver(mIntentReceiver, filter);
|
2010-02-19 17:02:21 -08:00
|
|
|
IntentFilter pkgFilter = new IntentFilter();
|
|
|
|
pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
|
2011-08-06 22:51:56 -04:00
|
|
|
pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
|
2010-02-19 17:02:21 -08:00
|
|
|
pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
|
|
|
|
pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
|
|
|
|
pkgFilter.addDataScheme("package");
|
|
|
|
mContext.registerReceiver(mIntentReceiver, pkgFilter);
|
2010-02-04 22:51:07 -08:00
|
|
|
IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
|
2010-01-28 09:57:30 -08:00
|
|
|
mContext.registerReceiver(mIntentReceiver, sdFilter);
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2009-12-02 11:15:02 -05:00
|
|
|
SettingsObserver observer = new SettingsObserver(mHandler);
|
|
|
|
observer.observe();
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
|
2013-01-30 14:04:10 -05:00
|
|
|
/**
|
|
|
|
* Read the old XML-based app block database and import those blockages into the AppOps system.
|
|
|
|
*/
|
|
|
|
private void importOldBlockDb() {
|
|
|
|
loadBlockDb();
|
|
|
|
|
|
|
|
PackageManager pm = mContext.getPackageManager();
|
|
|
|
for (String pkg : mBlockedPackages) {
|
|
|
|
PackageInfo info = null;
|
|
|
|
try {
|
|
|
|
info = pm.getPackageInfo(pkg, 0);
|
|
|
|
setNotificationsEnabledForPackage(pkg, info.applicationInfo.uid, false);
|
|
|
|
} catch (NameNotFoundException e) {
|
|
|
|
// forget you
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mBlockedPackages.clear();
|
|
|
|
if (mPolicyFile != null) {
|
|
|
|
mPolicyFile.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-08 17:09:14 -07:00
|
|
|
void systemReady() {
|
2012-04-26 17:30:34 -07:00
|
|
|
mAudioService = IAudioService.Stub.asInterface(
|
|
|
|
ServiceManager.getService(Context.AUDIO_SERVICE));
|
|
|
|
|
2009-07-08 17:09:14 -07:00
|
|
|
// no beeping until we're basically done booting
|
|
|
|
mSystemReady = true;
|
|
|
|
}
|
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
// Toasts
|
|
|
|
// ============================================================================
|
|
|
|
public void enqueueToast(String pkg, ITransientNotification callback, int duration)
|
|
|
|
{
|
2010-03-30 15:45:31 -04:00
|
|
|
if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
|
2009-03-03 19:31:44 -08:00
|
|
|
|
|
|
|
if (pkg == null || callback == null) {
|
2010-02-26 18:56:32 -08:00
|
|
|
Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
|
2009-03-03 19:31:44 -08:00
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2012-04-11 12:33:16 -04:00
|
|
|
final boolean isSystemToast = ("android".equals(pkg));
|
|
|
|
|
2013-01-30 14:04:10 -05:00
|
|
|
if (ENABLE_BLOCKED_TOASTS && !noteNotificationOp(pkg, Binder.getCallingUid())) {
|
|
|
|
if (!isSystemToast) {
|
|
|
|
Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
|
|
|
|
return;
|
|
|
|
}
|
2012-04-11 12:33:16 -04:00
|
|
|
}
|
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
synchronized (mToastQueue) {
|
|
|
|
int callingPid = Binder.getCallingPid();
|
|
|
|
long callingId = Binder.clearCallingIdentity();
|
|
|
|
try {
|
|
|
|
ToastRecord record;
|
|
|
|
int index = indexOfToastLocked(pkg, callback);
|
|
|
|
// If it's already in the queue, we update it in place, we don't
|
|
|
|
// move it to the end of the queue.
|
|
|
|
if (index >= 0) {
|
|
|
|
record = mToastQueue.get(index);
|
|
|
|
record.update(duration);
|
|
|
|
} else {
|
2011-01-21 18:08:36 -08:00
|
|
|
// Limit the number of toasts that any given package except the android
|
|
|
|
// package can enqueue. Prevents DOS attacks and deals with leaks.
|
2012-04-11 12:33:16 -04:00
|
|
|
if (!isSystemToast) {
|
2011-01-21 18:08:36 -08:00
|
|
|
int count = 0;
|
|
|
|
final int N = mToastQueue.size();
|
|
|
|
for (int i=0; i<N; i++) {
|
|
|
|
final ToastRecord r = mToastQueue.get(i);
|
|
|
|
if (r.pkg.equals(pkg)) {
|
|
|
|
count++;
|
|
|
|
if (count >= MAX_PACKAGE_NOTIFICATIONS) {
|
|
|
|
Slog.e(TAG, "Package has already posted " + count
|
|
|
|
+ " toasts. Not showing more. Package=" + pkg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
record = new ToastRecord(callingPid, pkg, callback, duration);
|
|
|
|
mToastQueue.add(record);
|
|
|
|
index = mToastQueue.size() - 1;
|
|
|
|
keepProcessAliveLocked(callingPid);
|
|
|
|
}
|
|
|
|
// If it's at index 0, it's the current toast. It doesn't matter if it's
|
|
|
|
// new or just been updated. Call back and tell it to show itself.
|
|
|
|
// If the callback fails, this will remove it from the list, so don't
|
|
|
|
// assume that it's valid after this.
|
|
|
|
if (index == 0) {
|
|
|
|
showNextToastLocked();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
Binder.restoreCallingIdentity(callingId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancelToast(String pkg, ITransientNotification callback) {
|
2010-02-26 18:56:32 -08:00
|
|
|
Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
|
2009-03-03 19:31:44 -08:00
|
|
|
|
|
|
|
if (pkg == null || callback == null) {
|
2010-02-26 18:56:32 -08:00
|
|
|
Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
|
2009-03-03 19:31:44 -08:00
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized (mToastQueue) {
|
|
|
|
long callingId = Binder.clearCallingIdentity();
|
|
|
|
try {
|
|
|
|
int index = indexOfToastLocked(pkg, callback);
|
|
|
|
if (index >= 0) {
|
|
|
|
cancelToastLocked(index);
|
|
|
|
} else {
|
2010-02-26 18:56:32 -08:00
|
|
|
Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
Binder.restoreCallingIdentity(callingId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showNextToastLocked() {
|
|
|
|
ToastRecord record = mToastQueue.get(0);
|
|
|
|
while (record != null) {
|
2010-02-26 18:56:32 -08:00
|
|
|
if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
|
2009-03-03 19:31:44 -08:00
|
|
|
try {
|
|
|
|
record.callback.show();
|
|
|
|
scheduleTimeoutLocked(record, false);
|
|
|
|
return;
|
|
|
|
} catch (RemoteException e) {
|
2010-02-26 18:56:32 -08:00
|
|
|
Slog.w(TAG, "Object died trying to show notification " + record.callback
|
2009-03-03 19:31:44 -08:00
|
|
|
+ " in package " + record.pkg);
|
|
|
|
// remove it from the list and let the process die
|
|
|
|
int index = mToastQueue.indexOf(record);
|
|
|
|
if (index >= 0) {
|
|
|
|
mToastQueue.remove(index);
|
|
|
|
}
|
|
|
|
keepProcessAliveLocked(record.pid);
|
|
|
|
if (mToastQueue.size() > 0) {
|
|
|
|
record = mToastQueue.get(0);
|
|
|
|
} else {
|
|
|
|
record = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void cancelToastLocked(int index) {
|
|
|
|
ToastRecord record = mToastQueue.get(index);
|
|
|
|
try {
|
|
|
|
record.callback.hide();
|
|
|
|
} catch (RemoteException e) {
|
2010-02-26 18:56:32 -08:00
|
|
|
Slog.w(TAG, "Object died trying to hide notification " + record.callback
|
2009-03-03 19:31:44 -08:00
|
|
|
+ " in package " + record.pkg);
|
|
|
|
// don't worry about this, we're about to remove it from
|
|
|
|
// the list anyway
|
|
|
|
}
|
|
|
|
mToastQueue.remove(index);
|
|
|
|
keepProcessAliveLocked(record.pid);
|
|
|
|
if (mToastQueue.size() > 0) {
|
|
|
|
// Show the next one. If the callback fails, this will remove
|
|
|
|
// it from the list, so don't assume that the list hasn't changed
|
|
|
|
// after this point.
|
|
|
|
showNextToastLocked();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
|
|
|
|
{
|
|
|
|
Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
|
|
|
|
long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
|
|
|
|
mHandler.removeCallbacksAndMessages(r);
|
|
|
|
mHandler.sendMessageDelayed(m, delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleTimeout(ToastRecord record)
|
|
|
|
{
|
2010-02-26 18:56:32 -08:00
|
|
|
if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
|
2009-03-03 19:31:44 -08:00
|
|
|
synchronized (mToastQueue) {
|
|
|
|
int index = indexOfToastLocked(record.pkg, record.callback);
|
|
|
|
if (index >= 0) {
|
|
|
|
cancelToastLocked(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock on mToastQueue
|
|
|
|
private int indexOfToastLocked(String pkg, ITransientNotification callback)
|
|
|
|
{
|
|
|
|
IBinder cbak = callback.asBinder();
|
|
|
|
ArrayList<ToastRecord> list = mToastQueue;
|
|
|
|
int len = list.size();
|
|
|
|
for (int i=0; i<len; i++) {
|
|
|
|
ToastRecord r = list.get(i);
|
|
|
|
if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock on mToastQueue
|
|
|
|
private void keepProcessAliveLocked(int pid)
|
|
|
|
{
|
|
|
|
int toastCount = 0; // toasts from this pid
|
|
|
|
ArrayList<ToastRecord> list = mToastQueue;
|
|
|
|
int N = list.size();
|
|
|
|
for (int i=0; i<N; i++) {
|
|
|
|
ToastRecord r = list.get(i);
|
|
|
|
if (r.pid == pid) {
|
|
|
|
toastCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
// Shouldn't happen.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private final class WorkerHandler extends Handler
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void handleMessage(Message msg)
|
|
|
|
{
|
|
|
|
switch (msg.what)
|
|
|
|
{
|
|
|
|
case MESSAGE_TIMEOUT:
|
|
|
|
handleTimeout((ToastRecord)msg.obj);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Notifications
|
|
|
|
// ============================================================================
|
2010-08-03 15:29:31 -04:00
|
|
|
public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification,
|
2012-08-31 14:05:51 -07:00
|
|
|
int[] idOut, int userId)
|
2009-03-03 19:31:44 -08:00
|
|
|
{
|
2010-08-03 15:29:31 -04:00
|
|
|
enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
|
2012-08-31 14:05:51 -07:00
|
|
|
tag, id, notification, idOut, userId);
|
2010-08-03 15:29:31 -04:00
|
|
|
}
|
2012-02-13 21:04:12 -05:00
|
|
|
|
|
|
|
private final static int clamp(int x, int low, int high) {
|
|
|
|
return (x < low) ? low : ((x > high) ? high : x);
|
2011-02-03 14:51:35 -05:00
|
|
|
}
|
|
|
|
|
2010-08-03 15:29:31 -04:00
|
|
|
// Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
|
|
|
|
// uid/pid of another application)
|
|
|
|
public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
|
2012-08-31 14:05:51 -07:00
|
|
|
String tag, int id, Notification notification, int[] idOut, int userId)
|
2010-08-03 15:29:31 -04:00
|
|
|
{
|
2012-04-11 12:33:16 -04:00
|
|
|
if (DBG) {
|
|
|
|
Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
|
|
|
|
}
|
|
|
|
checkCallerIsSystemOrSameApp(pkg);
|
|
|
|
final boolean isSystemNotification = ("android".equals(pkg));
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2012-08-31 14:05:51 -07:00
|
|
|
userId = ActivityManager.handleIncomingUser(callingPid,
|
2012-10-30 15:28:27 -07:00
|
|
|
callingUid, userId, true, false, "enqueueNotification", pkg);
|
2012-09-25 17:22:27 -07:00
|
|
|
final UserHandle user = new UserHandle(userId);
|
2012-08-31 14:05:51 -07:00
|
|
|
|
2010-06-04 11:44:54 -07:00
|
|
|
// Limit the number of notifications that any given package except the android
|
|
|
|
// package can enqueue. Prevents DOS attacks and deals with leaks.
|
2012-04-11 12:33:16 -04:00
|
|
|
if (!isSystemNotification) {
|
2010-06-04 11:44:54 -07:00
|
|
|
synchronized (mNotificationList) {
|
|
|
|
int count = 0;
|
|
|
|
final int N = mNotificationList.size();
|
|
|
|
for (int i=0; i<N; i++) {
|
|
|
|
final NotificationRecord r = mNotificationList.get(i);
|
2012-09-23 17:08:57 -07:00
|
|
|
if (r.pkg.equals(pkg) && r.userId == userId) {
|
2010-06-04 11:44:54 -07:00
|
|
|
count++;
|
|
|
|
if (count >= MAX_PACKAGE_NOTIFICATIONS) {
|
|
|
|
Slog.e(TAG, "Package has already posted " + count
|
|
|
|
+ " notifications. Not showing more. package=" + pkg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
// This conditional is a dirty hack to limit the logging done on
|
|
|
|
// behalf of the download manager without affecting other apps.
|
|
|
|
if (!pkg.equals("com.android.providers.downloads")
|
|
|
|
|| Log.isLoggable("DownloadManager", Log.VERBOSE)) {
|
2012-10-12 10:59:26 -07:00
|
|
|
EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, userId,
|
2011-11-29 23:48:29 -05:00
|
|
|
notification.toString());
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pkg == null || notification == null) {
|
|
|
|
throw new IllegalArgumentException("null not allowed: pkg=" + pkg
|
|
|
|
+ " id=" + id + " notification=" + notification);
|
|
|
|
}
|
|
|
|
if (notification.icon != 0) {
|
|
|
|
if (notification.contentView == null) {
|
|
|
|
throw new IllegalArgumentException("contentView required: pkg=" + pkg
|
|
|
|
+ " id=" + id + " notification=" + notification);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-13 21:04:12 -05:00
|
|
|
// === Scoring ===
|
2012-04-11 12:33:16 -04:00
|
|
|
|
2012-02-13 21:04:12 -05:00
|
|
|
// 0. Sanitize inputs
|
|
|
|
notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
|
|
|
|
// Migrate notification flags to scores
|
|
|
|
if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
|
|
|
|
if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
|
2012-03-28 15:46:39 -04:00
|
|
|
} else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
|
2012-02-13 21:04:12 -05:00
|
|
|
if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
|
|
|
|
}
|
2012-04-11 12:33:16 -04:00
|
|
|
|
2012-02-13 21:04:12 -05:00
|
|
|
// 1. initial score: buckets of 10, around the app
|
2012-04-11 12:33:16 -04:00
|
|
|
int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
|
2012-02-13 21:04:12 -05:00
|
|
|
|
2012-04-11 12:33:16 -04:00
|
|
|
// 2. Consult external heuristics (TBD)
|
2012-02-13 21:04:12 -05:00
|
|
|
|
2012-04-11 12:33:16 -04:00
|
|
|
// 3. Apply local rules
|
2012-02-13 21:04:12 -05:00
|
|
|
|
|
|
|
// blocked apps
|
2013-01-30 14:04:10 -05:00
|
|
|
if (ENABLE_BLOCKED_NOTIFICATIONS && !noteNotificationOp(pkg, callingUid)) {
|
|
|
|
if (!isSystemNotification) {
|
|
|
|
score = JUNK_SCORE;
|
|
|
|
Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
|
|
|
|
}
|
2012-04-11 12:33:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (DBG) {
|
|
|
|
Slog.v(TAG, "Assigned score=" + score + " to " + notification);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (score < SCORE_DISPLAY_THRESHOLD) {
|
|
|
|
// Notification will be blocked because the score is too low.
|
|
|
|
return;
|
2012-02-13 21:04:12 -05:00
|
|
|
}
|
|
|
|
|
2012-12-04 14:51:50 -05:00
|
|
|
// Should this notification make noise, vibe, or use the LED?
|
|
|
|
final boolean canInterrupt = (score >= SCORE_INTERRUPTION_THRESHOLD);
|
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
synchronized (mNotificationList) {
|
2011-02-03 14:51:35 -05:00
|
|
|
NotificationRecord r = new NotificationRecord(pkg, tag, id,
|
2012-08-31 14:05:51 -07:00
|
|
|
callingUid, callingPid, userId,
|
2012-02-13 21:04:12 -05:00
|
|
|
score,
|
2011-02-03 14:51:35 -05:00
|
|
|
notification);
|
2009-03-03 19:31:44 -08:00
|
|
|
NotificationRecord old = null;
|
|
|
|
|
2012-08-31 14:05:51 -07:00
|
|
|
int index = indexOfNotificationLocked(pkg, tag, id, userId);
|
2009-03-03 19:31:44 -08:00
|
|
|
if (index < 0) {
|
|
|
|
mNotificationList.add(r);
|
|
|
|
} else {
|
|
|
|
old = mNotificationList.remove(index);
|
|
|
|
mNotificationList.add(index, r);
|
2009-08-17 23:33:56 -07:00
|
|
|
// Make sure we don't lose the foreground service state.
|
|
|
|
if (old != null) {
|
|
|
|
notification.flags |=
|
|
|
|
old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
|
|
|
|
}
|
|
|
|
}
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2009-08-17 23:33:56 -07:00
|
|
|
// Ensure if this is a foreground service that the proper additional
|
|
|
|
// flags are set.
|
|
|
|
if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
|
|
|
|
notification.flags |= Notification.FLAG_ONGOING_EVENT
|
|
|
|
| Notification.FLAG_NO_CLEAR;
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2012-10-10 14:58:28 -07:00
|
|
|
final int currentUser;
|
|
|
|
final long token = Binder.clearCallingIdentity();
|
|
|
|
try {
|
|
|
|
currentUser = ActivityManager.getCurrentUser();
|
|
|
|
} finally {
|
|
|
|
Binder.restoreCallingIdentity(token);
|
|
|
|
}
|
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
if (notification.icon != 0) {
|
2012-09-20 16:06:08 -07:00
|
|
|
final StatusBarNotification n = new StatusBarNotification(
|
|
|
|
pkg, id, tag, r.uid, r.initialPid, score, notification, user);
|
2009-03-03 19:31:44 -08:00
|
|
|
if (old != null && old.statusBarKey != null) {
|
|
|
|
r.statusBarKey = old.statusBarKey;
|
|
|
|
long identity = Binder.clearCallingIdentity();
|
|
|
|
try {
|
2010-05-17 22:26:12 -07:00
|
|
|
mStatusBar.updateNotification(r.statusBarKey, n);
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
finally {
|
|
|
|
Binder.restoreCallingIdentity(identity);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
long identity = Binder.clearCallingIdentity();
|
|
|
|
try {
|
2010-05-17 22:26:12 -07:00
|
|
|
r.statusBarKey = mStatusBar.addNotification(n);
|
2012-12-04 14:51:50 -05:00
|
|
|
if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
|
|
|
|
&& canInterrupt) {
|
2012-02-13 20:42:19 -08:00
|
|
|
mAttentionLight.pulse();
|
|
|
|
}
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
finally {
|
|
|
|
Binder.restoreCallingIdentity(identity);
|
|
|
|
}
|
|
|
|
}
|
2012-10-10 14:58:28 -07:00
|
|
|
// Send accessibility events only for the current user.
|
|
|
|
if (currentUser == userId) {
|
|
|
|
sendAccessibilityEvent(notification, pkg);
|
|
|
|
}
|
2009-03-03 19:31:44 -08:00
|
|
|
} else {
|
2011-02-03 14:51:35 -05:00
|
|
|
Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
|
2009-03-03 19:31:44 -08:00
|
|
|
if (old != null && old.statusBarKey != null) {
|
|
|
|
long identity = Binder.clearCallingIdentity();
|
|
|
|
try {
|
2010-05-02 16:28:15 -07:00
|
|
|
mStatusBar.removeNotification(old.statusBarKey);
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
finally {
|
|
|
|
Binder.restoreCallingIdentity(identity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're not supposed to beep, vibrate, etc. then don't.
|
|
|
|
if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
|
|
|
|
&& (!(old != null
|
2009-07-08 17:09:14 -07:00
|
|
|
&& (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
|
2012-10-05 15:25:17 -07:00
|
|
|
&& (r.userId == UserHandle.USER_ALL ||
|
2012-10-05 16:03:01 -07:00
|
|
|
(r.userId == userId && r.userId == currentUser))
|
2012-12-04 14:51:50 -05:00
|
|
|
&& canInterrupt
|
2009-07-08 17:09:14 -07:00
|
|
|
&& mSystemReady) {
|
2009-11-27 05:07:55 -08:00
|
|
|
|
|
|
|
final AudioManager audioManager = (AudioManager) mContext
|
|
|
|
.getSystemService(Context.AUDIO_SERVICE);
|
2012-11-14 11:25:46 -08:00
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
// sound
|
|
|
|
final boolean useDefaultSound =
|
2009-12-04 10:31:43 -08:00
|
|
|
(notification.defaults & Notification.DEFAULT_SOUND) != 0;
|
2012-11-14 11:25:46 -08:00
|
|
|
|
|
|
|
Uri soundUri = null;
|
|
|
|
boolean hasValidSound = false;
|
|
|
|
|
|
|
|
if (useDefaultSound) {
|
|
|
|
soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
|
|
|
|
|
|
|
|
// check to see if the default notification sound is silent
|
|
|
|
ContentResolver resolver = mContext.getContentResolver();
|
|
|
|
hasValidSound = Settings.System.getString(resolver,
|
|
|
|
Settings.System.NOTIFICATION_SOUND) != null;
|
|
|
|
} else if (notification.sound != null) {
|
|
|
|
soundUri = notification.sound;
|
|
|
|
hasValidSound = (soundUri != null);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasValidSound) {
|
2009-03-03 19:31:44 -08:00
|
|
|
boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
|
|
|
|
int audioStreamType;
|
|
|
|
if (notification.audioStreamType >= 0) {
|
|
|
|
audioStreamType = notification.audioStreamType;
|
|
|
|
} else {
|
|
|
|
audioStreamType = DEFAULT_STREAM_TYPE;
|
|
|
|
}
|
|
|
|
mSoundNotification = r;
|
2009-11-27 05:07:55 -08:00
|
|
|
// do not play notifications if stream volume is 0
|
2012-10-10 12:03:41 -07:00
|
|
|
// (typically because ringer mode is silent) or if speech recognition is active.
|
|
|
|
if ((audioManager.getStreamVolume(audioStreamType) != 0)
|
|
|
|
&& !audioManager.isSpeechRecognitionActive()) {
|
2012-04-26 17:30:34 -07:00
|
|
|
final long identity = Binder.clearCallingIdentity();
|
2009-11-27 05:07:55 -08:00
|
|
|
try {
|
2012-04-26 17:30:34 -07:00
|
|
|
final IRingtonePlayer player = mAudioService.getRingtonePlayer();
|
|
|
|
if (player != null) {
|
2012-11-14 11:25:46 -08:00
|
|
|
player.playAsync(soundUri, user, looping, audioStreamType);
|
2012-04-26 17:30:34 -07:00
|
|
|
}
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
} finally {
|
2009-11-27 05:07:55 -08:00
|
|
|
Binder.restoreCallingIdentity(identity);
|
|
|
|
}
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vibrate
|
2012-11-13 20:49:47 -08:00
|
|
|
// Does the notification want to specify its own vibration?
|
|
|
|
final boolean hasCustomVibrate = notification.vibrate != null;
|
|
|
|
|
2012-11-09 23:03:26 -05:00
|
|
|
// new in 4.2: if there was supposed to be a sound and we're in vibrate mode,
|
2012-11-20 12:59:41 -05:00
|
|
|
// and no other vibration is specified, we fall back to vibration
|
2012-11-09 23:03:26 -05:00
|
|
|
final boolean convertSoundToVibration =
|
2012-11-13 20:49:47 -08:00
|
|
|
!hasCustomVibrate
|
2012-11-14 11:25:46 -08:00
|
|
|
&& hasValidSound
|
2012-11-09 23:03:26 -05:00
|
|
|
&& (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
|
|
|
|
|
2012-11-20 12:59:41 -05:00
|
|
|
// The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback.
|
2009-03-03 19:31:44 -08:00
|
|
|
final boolean useDefaultVibrate =
|
2012-11-13 20:49:47 -08:00
|
|
|
(notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
|
2012-11-09 23:03:26 -05:00
|
|
|
|
2012-11-13 20:49:47 -08:00
|
|
|
if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate)
|
2012-05-07 17:43:49 -07:00
|
|
|
&& !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
|
2009-03-03 19:31:44 -08:00
|
|
|
mVibrateNotification = r;
|
|
|
|
|
2012-11-13 20:49:47 -08:00
|
|
|
if (useDefaultVibrate || convertSoundToVibration) {
|
|
|
|
// Escalate privileges so we can use the vibrator even if the notifying app
|
|
|
|
// does not have the VIBRATE permission.
|
|
|
|
long identity = Binder.clearCallingIdentity();
|
|
|
|
try {
|
2012-11-20 12:59:41 -05:00
|
|
|
mVibrator.vibrate(useDefaultVibrate ? mDefaultVibrationPattern
|
|
|
|
: mFallbackVibrationPattern,
|
2012-11-13 20:49:47 -08:00
|
|
|
((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
|
|
|
|
} finally {
|
|
|
|
Binder.restoreCallingIdentity(identity);
|
|
|
|
}
|
|
|
|
} else if (notification.vibrate.length > 1) {
|
|
|
|
// If you want your own vibration pattern, you need the VIBRATE permission
|
|
|
|
mVibrator.vibrate(notification.vibrate,
|
|
|
|
((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
|
|
|
|
}
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// this option doesn't shut off the lights
|
|
|
|
|
|
|
|
// light
|
|
|
|
// the most recent thing gets the light
|
|
|
|
mLights.remove(old);
|
|
|
|
if (mLedNotification == old) {
|
|
|
|
mLedNotification = null;
|
|
|
|
}
|
2010-02-26 18:56:32 -08:00
|
|
|
//Slog.i(TAG, "notification.lights="
|
2009-03-03 19:31:44 -08:00
|
|
|
// + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
|
2012-12-04 14:51:50 -05:00
|
|
|
if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
|
|
|
|
&& canInterrupt) {
|
2009-03-03 19:31:44 -08:00
|
|
|
mLights.add(r);
|
|
|
|
updateLightsLocked();
|
|
|
|
} else {
|
|
|
|
if (old != null
|
|
|
|
&& ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
|
|
|
|
updateLightsLocked();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
idOut[0] = id;
|
|
|
|
}
|
|
|
|
|
2009-07-08 17:09:14 -07:00
|
|
|
private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
|
2009-05-14 22:28:01 -07:00
|
|
|
AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
|
|
|
|
if (!manager.isEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AccessibilityEvent event =
|
|
|
|
AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
|
|
|
|
event.setPackageName(packageName);
|
|
|
|
event.setClassName(Notification.class.getName());
|
|
|
|
event.setParcelableData(notification);
|
|
|
|
CharSequence tickerText = notification.tickerText;
|
|
|
|
if (!TextUtils.isEmpty(tickerText)) {
|
|
|
|
event.getText().add(tickerText);
|
|
|
|
}
|
|
|
|
|
|
|
|
manager.sendAccessibilityEvent(event);
|
|
|
|
}
|
|
|
|
|
2010-11-19 13:56:21 -08:00
|
|
|
private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
|
|
|
|
// tell the app
|
|
|
|
if (sendDelete) {
|
|
|
|
if (r.notification.deleteIntent != null) {
|
|
|
|
try {
|
|
|
|
r.notification.deleteIntent.send();
|
|
|
|
} catch (PendingIntent.CanceledException ex) {
|
|
|
|
// do nothing - there's no relevant way to recover, and
|
|
|
|
// no reason to let this propagate
|
|
|
|
Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
// status bar
|
|
|
|
if (r.notification.icon != 0) {
|
|
|
|
long identity = Binder.clearCallingIdentity();
|
|
|
|
try {
|
2010-05-02 16:28:15 -07:00
|
|
|
mStatusBar.removeNotification(r.statusBarKey);
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
finally {
|
|
|
|
Binder.restoreCallingIdentity(identity);
|
|
|
|
}
|
|
|
|
r.statusBarKey = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sound
|
|
|
|
if (mSoundNotification == r) {
|
|
|
|
mSoundNotification = null;
|
2012-04-26 17:30:34 -07:00
|
|
|
final long identity = Binder.clearCallingIdentity();
|
2009-03-03 19:31:44 -08:00
|
|
|
try {
|
2012-04-26 17:30:34 -07:00
|
|
|
final IRingtonePlayer player = mAudioService.getRingtonePlayer();
|
|
|
|
if (player != null) {
|
|
|
|
player.stopAsync();
|
|
|
|
}
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
} finally {
|
2009-03-03 19:31:44 -08:00
|
|
|
Binder.restoreCallingIdentity(identity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vibrate
|
|
|
|
if (mVibrateNotification == r) {
|
|
|
|
mVibrateNotification = null;
|
|
|
|
long identity = Binder.clearCallingIdentity();
|
|
|
|
try {
|
|
|
|
mVibrator.cancel();
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
Binder.restoreCallingIdentity(identity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// light
|
|
|
|
mLights.remove(r);
|
|
|
|
if (mLedNotification == r) {
|
|
|
|
mLedNotification = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-08-17 23:33:56 -07:00
|
|
|
* Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
|
2009-12-04 10:31:43 -08:00
|
|
|
* and none of the {@code mustNotHaveFlags}.
|
2009-03-03 19:31:44 -08:00
|
|
|
*/
|
2009-09-25 14:23:13 -07:00
|
|
|
private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
|
2012-08-31 14:05:51 -07:00
|
|
|
int mustNotHaveFlags, boolean sendDelete, int userId) {
|
2012-10-12 10:59:26 -07:00
|
|
|
EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, userId,
|
2011-11-29 23:48:29 -05:00
|
|
|
mustHaveFlags, mustNotHaveFlags);
|
2009-03-03 19:31:44 -08:00
|
|
|
|
|
|
|
synchronized (mNotificationList) {
|
2012-08-31 14:05:51 -07:00
|
|
|
int index = indexOfNotificationLocked(pkg, tag, id, userId);
|
2009-03-03 19:31:44 -08:00
|
|
|
if (index >= 0) {
|
2009-09-25 14:23:13 -07:00
|
|
|
NotificationRecord r = mNotificationList.get(index);
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
|
|
|
|
return;
|
|
|
|
}
|
2009-08-17 23:33:56 -07:00
|
|
|
if ((r.notification.flags & mustNotHaveFlags) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
mNotificationList.remove(index);
|
|
|
|
|
2010-11-19 13:56:21 -08:00
|
|
|
cancelNotificationLocked(r, sendDelete);
|
2009-03-03 19:31:44 -08:00
|
|
|
updateLightsLocked();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-12 10:59:26 -07:00
|
|
|
/**
|
|
|
|
* Determine whether the userId applies to the notification in question, either because
|
|
|
|
* they match exactly, or one of them is USER_ALL (which is treated as a wildcard).
|
|
|
|
*/
|
|
|
|
private boolean notificationMatchesUserId(NotificationRecord r, int userId) {
|
|
|
|
return
|
|
|
|
// looking for USER_ALL notifications? match everything
|
|
|
|
userId == UserHandle.USER_ALL
|
|
|
|
// a notification sent to USER_ALL matches any query
|
|
|
|
|| r.userId == UserHandle.USER_ALL
|
|
|
|
// an exact user match
|
|
|
|
|| r.userId == userId;
|
|
|
|
}
|
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
/**
|
|
|
|
* Cancels all notifications from a given package that have all of the
|
|
|
|
* {@code mustHaveFlags}.
|
|
|
|
*/
|
2010-02-19 17:02:21 -08:00
|
|
|
boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
|
2012-08-31 14:05:51 -07:00
|
|
|
int mustNotHaveFlags, boolean doit, int userId) {
|
2012-10-12 10:59:26 -07:00
|
|
|
EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, userId,
|
|
|
|
mustHaveFlags, mustNotHaveFlags);
|
2009-03-03 19:31:44 -08:00
|
|
|
|
|
|
|
synchronized (mNotificationList) {
|
|
|
|
final int N = mNotificationList.size();
|
|
|
|
boolean canceledSomething = false;
|
|
|
|
for (int i = N-1; i >= 0; --i) {
|
|
|
|
NotificationRecord r = mNotificationList.get(i);
|
2012-10-12 10:59:26 -07:00
|
|
|
if (!notificationMatchesUserId(r, userId)) {
|
2012-08-31 14:05:51 -07:00
|
|
|
continue;
|
|
|
|
}
|
2012-11-07 16:58:30 -08:00
|
|
|
// Don't remove notifications to all, if there's no package name specified
|
|
|
|
if (r.userId == UserHandle.USER_ALL && pkg == null) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-03-03 19:31:44 -08:00
|
|
|
if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-17 23:33:56 -07:00
|
|
|
if ((r.notification.flags & mustNotHaveFlags) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-10-04 12:00:53 -04:00
|
|
|
if (pkg != null && !r.pkg.equals(pkg)) {
|
2009-03-03 19:31:44 -08:00
|
|
|
continue;
|
|
|
|
}
|
2010-02-19 17:02:21 -08:00
|
|
|
canceledSomething = true;
|
|
|
|
if (!doit) {
|
|
|
|
return true;
|
|
|
|
}
|
2009-03-03 19:31:44 -08:00
|
|
|
mNotificationList.remove(i);
|
2010-11-19 13:56:21 -08:00
|
|
|
cancelNotificationLocked(r, false);
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
if (canceledSomething) {
|
|
|
|
updateLightsLocked();
|
|
|
|
}
|
2010-02-19 17:02:21 -08:00
|
|
|
return canceledSomething;
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-31 14:05:51 -07:00
|
|
|
public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) {
|
2012-04-11 12:33:16 -04:00
|
|
|
checkCallerIsSystemOrSameApp(pkg);
|
2012-08-31 14:05:51 -07:00
|
|
|
userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
|
2012-10-30 15:28:27 -07:00
|
|
|
Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg);
|
2009-08-17 23:33:56 -07:00
|
|
|
// Don't allow client applications to cancel foreground service notis.
|
2009-09-25 14:23:13 -07:00
|
|
|
cancelNotification(pkg, tag, id, 0,
|
2009-08-17 23:33:56 -07:00
|
|
|
Binder.getCallingUid() == Process.SYSTEM_UID
|
2012-08-31 14:05:51 -07:00
|
|
|
? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId);
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
|
2012-08-31 14:05:51 -07:00
|
|
|
public void cancelAllNotifications(String pkg, int userId) {
|
2012-04-11 12:33:16 -04:00
|
|
|
checkCallerIsSystemOrSameApp(pkg);
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2012-08-31 14:05:51 -07:00
|
|
|
userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
|
2012-10-30 15:28:27 -07:00
|
|
|
Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg);
|
2012-08-31 14:05:51 -07:00
|
|
|
|
2009-08-17 23:33:56 -07:00
|
|
|
// Calling from user space, don't allow the canceling of actively
|
|
|
|
// running foreground services.
|
2012-08-31 14:05:51 -07:00
|
|
|
cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId);
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
|
2012-04-11 12:33:16 -04:00
|
|
|
void checkCallerIsSystem() {
|
|
|
|
int uid = Binder.getCallingUid();
|
2012-08-20 17:23:30 -07:00
|
|
|
if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
|
2012-04-11 12:33:16 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new SecurityException("Disallowed call for uid " + uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkCallerIsSystemOrSameApp(String pkg) {
|
2009-08-17 23:33:56 -07:00
|
|
|
int uid = Binder.getCallingUid();
|
2012-08-20 17:23:30 -07:00
|
|
|
if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
|
2009-08-17 23:33:56 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2012-08-29 18:41:53 -07:00
|
|
|
ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
|
|
|
|
pkg, 0, UserHandle.getCallingUserId());
|
2012-08-16 10:48:27 -07:00
|
|
|
if (!UserHandle.isSameApp(ai.uid, uid)) {
|
2009-08-17 23:33:56 -07:00
|
|
|
throw new SecurityException("Calling uid " + uid + " gave package"
|
|
|
|
+ pkg + " which is owned by uid " + ai.uid);
|
|
|
|
}
|
2012-08-29 18:41:53 -07:00
|
|
|
} catch (RemoteException re) {
|
|
|
|
throw new SecurityException("Unknown package " + pkg + "\n" + re);
|
2009-08-17 23:33:56 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2012-08-31 14:05:51 -07:00
|
|
|
void cancelAll(int userId) {
|
2009-03-03 19:31:44 -08:00
|
|
|
synchronized (mNotificationList) {
|
|
|
|
final int N = mNotificationList.size();
|
|
|
|
for (int i=N-1; i>=0; i--) {
|
|
|
|
NotificationRecord r = mNotificationList.get(i);
|
|
|
|
|
2012-10-12 10:59:26 -07:00
|
|
|
if (!notificationMatchesUserId(r, userId)) {
|
2012-08-31 14:05:51 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
|
|
|
|
| Notification.FLAG_NO_CLEAR)) == 0) {
|
|
|
|
mNotificationList.remove(i);
|
2010-11-19 13:56:21 -08:00
|
|
|
cancelNotificationLocked(r, true);
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateLightsLocked();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock on mNotificationList
|
|
|
|
private void updateLightsLocked()
|
|
|
|
{
|
2009-03-18 17:39:46 -07:00
|
|
|
// handle notification lights
|
|
|
|
if (mLedNotification == null) {
|
|
|
|
// get next notification, if any
|
|
|
|
int n = mLights.size();
|
|
|
|
if (n > 0) {
|
|
|
|
mLedNotification = mLights.get(n-1);
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
}
|
2009-12-02 11:15:02 -05:00
|
|
|
|
2011-08-30 09:55:30 -04:00
|
|
|
// Don't flash while we are in a call or screen is on
|
|
|
|
if (mLedNotification == null || mInCall || mScreenOn) {
|
2009-11-27 14:25:58 -05:00
|
|
|
mNotificationLight.turnOff();
|
2009-03-18 17:39:46 -07:00
|
|
|
} else {
|
2010-01-20 12:13:36 -05:00
|
|
|
int ledARGB = mLedNotification.notification.ledARGB;
|
|
|
|
int ledOnMS = mLedNotification.notification.ledOnMS;
|
|
|
|
int ledOffMS = mLedNotification.notification.ledOffMS;
|
|
|
|
if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
|
|
|
|
ledARGB = mDefaultNotificationColor;
|
|
|
|
ledOnMS = mDefaultNotificationLedOn;
|
|
|
|
ledOffMS = mDefaultNotificationLedOff;
|
|
|
|
}
|
|
|
|
if (mNotificationPulseEnabled) {
|
|
|
|
// pulse repeatedly
|
|
|
|
mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
|
|
|
|
ledOnMS, ledOffMS);
|
|
|
|
}
|
2009-03-18 17:39:46 -07:00
|
|
|
}
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// lock on mNotificationList
|
2012-08-31 14:05:51 -07:00
|
|
|
private int indexOfNotificationLocked(String pkg, String tag, int id, int userId)
|
2009-03-03 19:31:44 -08:00
|
|
|
{
|
|
|
|
ArrayList<NotificationRecord> list = mNotificationList;
|
|
|
|
final int len = list.size();
|
|
|
|
for (int i=0; i<len; i++) {
|
|
|
|
NotificationRecord r = list.get(i);
|
2012-10-12 10:59:26 -07:00
|
|
|
if (!notificationMatchesUserId(r, userId) || r.id != id) {
|
2012-08-31 14:05:51 -07:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-25 14:23:13 -07:00
|
|
|
if (tag == null) {
|
|
|
|
if (r.tag != null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!tag.equals(r.tag)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-08-31 14:05:51 -07:00
|
|
|
if (r.pkg.equals(pkg)) {
|
2009-03-03 19:31:44 -08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-02 11:15:02 -05:00
|
|
|
private void updateNotificationPulse() {
|
|
|
|
synchronized (mNotificationList) {
|
|
|
|
updateLightsLocked();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
// ======================================================================
|
|
|
|
@Override
|
|
|
|
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
|
|
|
|
if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
|
|
|
|
!= PackageManager.PERMISSION_GRANTED) {
|
|
|
|
pw.println("Permission Denial: can't dump NotificationManager from from pid="
|
|
|
|
+ Binder.getCallingPid()
|
|
|
|
+ ", uid=" + Binder.getCallingUid());
|
|
|
|
return;
|
|
|
|
}
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
pw.println("Current Notification Manager state:");
|
|
|
|
|
|
|
|
int N;
|
|
|
|
|
|
|
|
synchronized (mToastQueue) {
|
|
|
|
N = mToastQueue.size();
|
|
|
|
if (N > 0) {
|
|
|
|
pw.println(" Toast Queue:");
|
|
|
|
for (int i=0; i<N; i++) {
|
|
|
|
mToastQueue.get(i).dump(pw, " ");
|
|
|
|
}
|
|
|
|
pw.println(" ");
|
|
|
|
}
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
synchronized (mNotificationList) {
|
|
|
|
N = mNotificationList.size();
|
|
|
|
if (N > 0) {
|
|
|
|
pw.println(" Notification List:");
|
|
|
|
for (int i=0; i<N; i++) {
|
|
|
|
mNotificationList.get(i).dump(pw, " ", mContext);
|
|
|
|
}
|
|
|
|
pw.println(" ");
|
|
|
|
}
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
N = mLights.size();
|
|
|
|
if (N > 0) {
|
|
|
|
pw.println(" Lights List:");
|
|
|
|
for (int i=0; i<N; i++) {
|
|
|
|
mLights.get(i).dump(pw, " ", mContext);
|
|
|
|
}
|
|
|
|
pw.println(" ");
|
|
|
|
}
|
2009-12-04 10:31:43 -08:00
|
|
|
|
2009-03-03 19:31:44 -08:00
|
|
|
pw.println(" mSoundNotification=" + mSoundNotification);
|
|
|
|
pw.println(" mVibrateNotification=" + mVibrateNotification);
|
2009-07-23 12:29:19 -04:00
|
|
|
pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
|
|
|
|
pw.println(" mSystemReady=" + mSystemReady);
|
2009-03-03 19:31:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|