Implement issue #1943927 (need notification in status bar when adb is active)

This is a little hacky -- we just assume that if adb is enabled and power
is connected through usb, then it is active.

The icons and text are temporary until final design is provided.
This commit is contained in:
Dianne Hackborn
2009-06-26 18:16:48 -07:00
parent 9848f11fd8
commit 1dac277f93
5 changed files with 134 additions and 7 deletions

View File

@@ -25,15 +25,20 @@ import android.app.IActivityManager;
import android.app.INotificationManager;
import android.app.ITransientNotification;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.StatusBarManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentQueryMap;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.media.AsyncPlayer;
import android.media.AudioManager;
import android.net.Uri;
@@ -88,6 +93,12 @@ class NotificationManagerService extends INotificationManager.Stub
private NotificationRecord mVibrateNotification;
private Vibrator mVibrator = new Vibrator();
// adb
private int mBatteryPlugged;
private boolean mAdbEnabled = false;
private boolean mAdbNotificationShown = false;
private Notification mAdbNotification;
private ArrayList<NotificationRecord> mNotificationList;
private ArrayList<ToastRecord> mToastQueue;
@@ -297,6 +308,9 @@ class NotificationManagerService extends INotificationManager.Stub
mBatteryFull = batteryFull;
updateLights();
}
mBatteryPlugged = intent.getIntExtra("plugged", 0);
updateAdbNotification();
} else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
|| action.equals(Intent.ACTION_PACKAGE_RESTARTED)) {
Uri uri = intent.getData();
@@ -312,6 +326,31 @@ class NotificationManagerService extends INotificationManager.Stub
}
};
class SettingsObserver extends ContentObserver {
SettingsObserver(Handler handler) {
super(handler);
}
void observe() {
ContentResolver resolver = mContext.getContentResolver();
resolver.registerContentObserver(Settings.Secure.getUriFor(
Settings.Secure.ADB_ENABLED), false, this);
update();
}
@Override public void onChange(boolean selfChange) {
update();
}
public void update() {
ContentResolver resolver = mContext.getContentResolver();
mAdbEnabled = Settings.Secure.getInt(resolver,
Settings.Secure.ADB_ENABLED, 0) != 0;
updateAdbNotification();
}
}
private final SettingsObserver mSettingsObserver;
NotificationManagerService(Context context, StatusBarService statusBar,
HardwareService hardware)
{
@@ -333,6 +372,9 @@ class NotificationManagerService extends INotificationManager.Stub
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
mContext.registerReceiver(mIntentReceiver, filter);
mSettingsObserver = new SettingsObserver(mHandler);
mSettingsObserver.observe();
}
// Toasts
@@ -892,6 +934,62 @@ class NotificationManagerService extends INotificationManager.Stub
return -1;
}
// This is here instead of StatusBarPolicy because it is an important
// security feature that we don't want people customizing the platform
// to accidentally lose.
private void updateAdbNotification() {
if (mAdbEnabled && mBatteryPlugged == BatteryManager.BATTERY_PLUGGED_USB) {
if (!mAdbNotificationShown) {
NotificationManager notificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
Resources r = mContext.getResources();
CharSequence title = r.getText(
com.android.internal.R.string.adb_active_notification_title);
CharSequence message = r.getText(
com.android.internal.R.string.adb_active_notification_message);
if (mAdbNotification == null) {
mAdbNotification = new Notification();
mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_warning;
mAdbNotification.when = 0;
mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
mAdbNotification.tickerText = title;
mAdbNotification.defaults |= Notification.DEFAULT_SOUND;
}
Intent intent = new Intent(
Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
// Note: we are hard-coding the component because this is
// an important security UI that we don't want anyone
// intercepting.
intent.setComponent(new ComponentName("com.android.settings",
"com.android.settings.DevelopmentSettings"));
PendingIntent pi = PendingIntent.getActivity(mContext, 0,
intent, 0);
mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
mAdbNotificationShown = true;
notificationManager.notify(
com.android.internal.R.string.adb_active_notification_title,
mAdbNotification);
}
}
} else if (mAdbNotificationShown) {
NotificationManager notificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
mAdbNotificationShown = false;
notificationManager.cancel(
com.android.internal.R.string.adb_active_notification_title);
}
}
}
// ======================================================================
@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {