summaryrefslogtreecommitdiffstats
path: root/services/java
diff options
context:
space:
mode:
authorMike Lockwood <lockwood@android.com>2010-12-10 16:19:32 -0800
committerMike Lockwood <lockwood@android.com>2011-02-27 18:39:26 -0800
commitf13ec7a3ac18ad87936ddab2e18f57c3bf91bfb6 (patch)
tree9d0ac03104b77fa965250610d5e1fa30ab7fc6a5 /services/java
parent7916432b3cd9d0396872aee6d3d585f19b4b7ef6 (diff)
downloadframeworks_base-f13ec7a3ac18ad87936ddab2e18f57c3bf91bfb6.zip
frameworks_base-f13ec7a3ac18ad87936ddab2e18f57c3bf91bfb6.tar.gz
frameworks_base-f13ec7a3ac18ad87936ddab2e18f57c3bf91bfb6.tar.bz2
DO NOT MERGE: Clean up USB notifications:
Add support for separate USB connected and configuration events Include both USB connected/disconnected and configuration state in USB_STATE Intent Remove redundant USB_CONNECTED and USB_DISCONNECTED Intents Now we just have the sticky USB_STATE broadcast Move USB disconnnect rebouncing from Tethering to UsbService Change-Id: I1dea480f4b0daf14247cf37c5f2060498882c002 Signed-off-by: Mike Lockwood <lockwood@android.com>
Diffstat (limited to 'services/java')
-rwxr-xr-xservices/java/com/android/server/NotificationManagerService.java2
-rw-r--r--services/java/com/android/server/UsbService.java110
-rw-r--r--services/java/com/android/server/connectivity/Tethering.java25
3 files changed, 70 insertions, 67 deletions
diff --git a/services/java/com/android/server/NotificationManagerService.java b/services/java/com/android/server/NotificationManagerService.java
index 6a4cb81..a5333da 100755
--- a/services/java/com/android/server/NotificationManagerService.java
+++ b/services/java/com/android/server/NotificationManagerService.java
@@ -358,8 +358,6 @@ public class NotificationManagerService extends INotificationManager.Stub
boolean adbEnabled = (UsbManager.USB_FUNCTION_ENABLED.equals(
extras.getString(UsbManager.USB_FUNCTION_ADB)));
updateAdbNotification(usbConnected && adbEnabled);
- } else if (action.equals(UsbManager.ACTION_USB_DISCONNECTED)) {
- updateAdbNotification(false);
} else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
|| action.equals(Intent.ACTION_PACKAGE_RESTARTED)
|| (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
diff --git a/services/java/com/android/server/UsbService.java b/services/java/com/android/server/UsbService.java
index ad400bf..672c95e 100644
--- a/services/java/com/android/server/UsbService.java
+++ b/services/java/com/android/server/UsbService.java
@@ -40,15 +40,33 @@ class UsbService {
private static final String TAG = UsbService.class.getSimpleName();
private static final boolean LOG = false;
- private static final String USB_CONFIGURATION_MATCH = "DEVPATH=/devices/virtual/switch/usb_configuration";
- private static final String USB_FUNCTIONS_MATCH = "DEVPATH=/devices/virtual/usb_composite/";
- private static final String USB_CONFIGURATION_PATH = "/sys/class/switch/usb_configuration/state";
- private static final String USB_COMPOSITE_CLASS_PATH = "/sys/class/usb_composite";
+ private static final String USB_CONNECTED_MATCH =
+ "DEVPATH=/devices/virtual/switch/usb_connected";
+ private static final String USB_CONFIGURATION_MATCH =
+ "DEVPATH=/devices/virtual/switch/usb_configuration";
+ private static final String USB_FUNCTIONS_MATCH =
+ "DEVPATH=/devices/virtual/usb_composite/";
+ private static final String USB_CONNECTED_PATH =
+ "/sys/class/switch/usb_connected/state";
+ private static final String USB_CONFIGURATION_PATH =
+ "/sys/class/switch/usb_configuration/state";
+ private static final String USB_COMPOSITE_CLASS_PATH =
+ "/sys/class/usb_composite";
private static final int MSG_UPDATE = 0;
- private int mUsbConfig = 0;
- private int mPreviousUsbConfig = 0;
+ // Delay for debouncing USB disconnects.
+ // We often get rapid connect/disconnect events when enabling USB functions,
+ // which need debouncing.
+ private static final int UPDATE_DELAY = 1000;
+
+ // current connected and configuration state
+ private int mConnected;
+ private int mConfiguration;
+
+ // last broadcasted connected and configuration state
+ private int mLastConnected = -1;
+ private int mLastConfiguration = -1;
// lists of enabled and disabled USB functions
private final ArrayList<String> mEnabledFunctions = new ArrayList<String>();
@@ -58,8 +76,6 @@ class UsbService {
private final Context mContext;
- private PowerManagerService mPowerManager;
-
private final UEventObserver mUEventObserver = new UEventObserver() {
@Override
public void onUEvent(UEventObserver.UEvent event) {
@@ -68,16 +84,23 @@ class UsbService {
}
synchronized (this) {
- String switchState = event.get("SWITCH_STATE");
- if (switchState != null) {
+ String name = event.get("SWITCH_NAME");
+ String state = event.get("SWITCH_STATE");
+ if (name != null && state != null) {
try {
- int newConfig = Integer.parseInt(switchState);
- if (newConfig != mUsbConfig) {
- mPreviousUsbConfig = mUsbConfig;
- mUsbConfig = newConfig;
+ int intState = Integer.parseInt(state);
+ if ("usb_connected".equals(name)) {
+ mConnected = intState;
+ // trigger an Intent broadcast
+ if (mSystemReady) {
+ // debounce disconnects
+ update(mConnected == 0);
+ }
+ } else if ("usb_configuration".equals(name)) {
+ mConfiguration = intState;
// trigger an Intent broadcast
if (mSystemReady) {
- update();
+ update(mConnected == 0);
}
}
} catch (NumberFormatException e) {
@@ -111,6 +134,7 @@ class UsbService {
mContext = context;
init(); // set initial status
+ mUEventObserver.startObserving(USB_CONNECTED_MATCH);
mUEventObserver.startObserving(USB_CONFIGURATION_MATCH);
mUEventObserver.startObserving(USB_FUNCTIONS_MATCH);
}
@@ -119,10 +143,15 @@ class UsbService {
char[] buffer = new char[1024];
try {
- FileReader file = new FileReader(USB_CONFIGURATION_PATH);
+ FileReader file = new FileReader(USB_CONNECTED_PATH);
int len = file.read(buffer, 0, 1024);
- mPreviousUsbConfig = mUsbConfig = Integer.valueOf((new String(buffer, 0, len)).trim());
+ file.close();
+ mConnected = Integer.valueOf((new String(buffer, 0, len)).trim());
+ file = new FileReader(USB_CONFIGURATION_PATH);
+ len = file.read(buffer, 0, 1024);
+ file.close();
+ mConfiguration = Integer.valueOf((new String(buffer, 0, len)).trim());
} catch (FileNotFoundException e) {
Slog.w(TAG, "This kernel does not have USB configuration switch support");
} catch (Exception e) {
@@ -135,6 +164,7 @@ class UsbService {
File file = new File(files[i], "enable");
FileReader reader = new FileReader(file);
int len = reader.read(buffer, 0, 1024);
+ reader.close();
int value = Integer.valueOf((new String(buffer, 0, len)).trim());
String functionName = files[i].getName();
if (value == 1) {
@@ -152,13 +182,14 @@ class UsbService {
void systemReady() {
synchronized (this) {
- update();
+ update(false);
mSystemReady = true;
}
}
- private final void update() {
- mHandler.sendEmptyMessage(MSG_UPDATE);
+ private final void update(boolean delayed) {
+ mHandler.removeMessages(MSG_UPDATE);
+ mHandler.sendEmptyMessageDelayed(MSG_UPDATE, delayed ? UPDATE_DELAY : 0);
}
private final Handler mHandler = new Handler() {
@@ -177,31 +208,26 @@ class UsbService {
switch (msg.what) {
case MSG_UPDATE:
synchronized (this) {
- final ContentResolver cr = mContext.getContentResolver();
+ if (mConnected != mLastConnected || mConfiguration != mLastConfiguration) {
- if (Settings.Secure.getInt(cr,
- Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
- Slog.i(TAG, "Device not provisioned, skipping USB broadcast");
- return;
- }
- // Send an Intent containing connected/disconnected state
- // and the enabled/disabled state of all USB functions
- Intent intent;
- boolean usbConnected = (mUsbConfig != 0);
- if (usbConnected) {
- intent = new Intent(UsbManager.ACTION_USB_CONNECTED);
+ final ContentResolver cr = mContext.getContentResolver();
+ if (Settings.Secure.getInt(cr,
+ Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
+ Slog.i(TAG, "Device not provisioned, skipping USB broadcast");
+ return;
+ }
+
+ mLastConnected = mConnected;
+ mLastConfiguration = mConfiguration;
+
+ // send a sticky broadcast containing current USB state
+ Intent intent = new Intent(UsbManager.ACTION_USB_STATE);
+ intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
+ intent.putExtra(UsbManager.USB_CONNECTED, mConnected != 0);
+ intent.putExtra(UsbManager.USB_CONFIGURATION, mConfiguration);
addEnabledFunctions(intent);
- } else {
- intent = new Intent(UsbManager.ACTION_USB_DISCONNECTED);
+ mContext.sendStickyBroadcast(intent);
}
- mContext.sendBroadcast(intent);
-
- // send a sticky broadcast for clients interested in both connect and disconnect
- intent = new Intent(UsbManager.ACTION_USB_STATE);
- intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
- intent.putExtra(UsbManager.USB_CONNECTED, usbConnected);
- addEnabledFunctions(intent);
- mContext.sendStickyBroadcast(intent);
}
break;
}
diff --git a/services/java/com/android/server/connectivity/Tethering.java b/services/java/com/android/server/connectivity/Tethering.java
index 5712e60..7652a26 100644
--- a/services/java/com/android/server/connectivity/Tethering.java
+++ b/services/java/com/android/server/connectivity/Tethering.java
@@ -111,14 +111,6 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
private boolean mUsbMassStorageOff; // track the status of USB Mass Storage
private boolean mUsbConnected; // track the status of USB connection
- // mUsbHandler message
- static final int USB_STATE_CHANGE = 1;
- static final int USB_DISCONNECTED = 0;
- static final int USB_CONNECTED = 1;
-
- // Time to delay before processing USB disconnect events
- static final long USB_DISCONNECT_DELAY = 1000;
-
public Tethering(Context context, Looper looper) {
mContext = context;
mLooper = looper;
@@ -429,25 +421,12 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
}
}
- private Handler mUsbHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- mUsbConnected = (msg.arg1 == USB_CONNECTED);
- updateUsbStatus();
- }
- };
-
private class StateReceiver extends BroadcastReceiver {
public void onReceive(Context content, Intent intent) {
String action = intent.getAction();
if (action.equals(UsbManager.ACTION_USB_STATE)) {
- // process connect events immediately, but delay handling disconnects
- // to debounce USB configuration changes
- boolean connected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
- Message msg = Message.obtain(mUsbHandler, USB_STATE_CHANGE,
- (connected ? USB_CONNECTED : USB_DISCONNECTED), 0);
- mUsbHandler.removeMessages(USB_STATE_CHANGE);
- mUsbHandler.sendMessageDelayed(msg, connected ? 0 : USB_DISCONNECT_DELAY);
+ mUsbConnected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
+ updateUsbStatus();
} else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
mUsbMassStorageOff = false;
updateUsbStatus();