Merge "DO NOT MERGE: Clean up USB notifications:" into gingerbread

This commit is contained in:
Mike Lockwood
2011-02-28 13:12:03 -08:00
committed by Android (Google) Code Review
4 changed files with 86 additions and 97 deletions

View File

@ -26,33 +26,13 @@ import java.io.IOException;
* @hide * @hide
*/ */
public class UsbManager { public class UsbManager {
/**
* Broadcast Action: A broadcast for USB connected events.
*
* The extras bundle will name/value pairs with the name of the function
* and a value of either {@link #USB_FUNCTION_ENABLED} or {@link #USB_FUNCTION_DISABLED}.
* Possible USB function names include {@link #USB_FUNCTION_MASS_STORAGE},
* {@link #USB_FUNCTION_ADB}, {@link #USB_FUNCTION_RNDIS} and {@link #USB_FUNCTION_MTP}.
*/
public static final String ACTION_USB_CONNECTED =
"android.hardware.action.USB_CONNECTED";
/**
* Broadcast Action: A broadcast for USB disconnected events.
*/
public static final String ACTION_USB_DISCONNECTED =
"android.hardware.action.USB_DISCONNECTED";
/** /**
* Broadcast Action: A sticky broadcast for USB state change events. * Broadcast Action: A sticky broadcast for USB state change events.
* *
* This is a sticky broadcast for clients that are interested in both USB connect and * This is a sticky broadcast for clients that includes USB connected/disconnected state,
* disconnect events. If you are only concerned with one or the other, you can use * the USB configuration that is currently set and a bundle containing name/value pairs
* {@link #ACTION_USB_CONNECTED} or {@link #ACTION_USB_DISCONNECTED} to avoid receiving * with the names of the functions and a value of either {@link #USB_FUNCTION_ENABLED}
* unnecessary broadcasts. The boolean {@link #USB_CONNECTED} extra indicates whether * or {@link #USB_FUNCTION_DISABLED}.
* USB is connected or disconnected.
* The extras bundle will also contain name/value pairs with the name of the function
* and a value of either {@link #USB_FUNCTION_ENABLED} or {@link #USB_FUNCTION_DISABLED}.
* Possible USB function names include {@link #USB_FUNCTION_MASS_STORAGE}, * Possible USB function names include {@link #USB_FUNCTION_MASS_STORAGE},
* {@link #USB_FUNCTION_ADB}, {@link #USB_FUNCTION_RNDIS} and {@link #USB_FUNCTION_MTP}. * {@link #USB_FUNCTION_ADB}, {@link #USB_FUNCTION_RNDIS} and {@link #USB_FUNCTION_MTP}.
*/ */
@ -65,39 +45,45 @@ public class UsbManager {
*/ */
public static final String USB_CONNECTED = "connected"; public static final String USB_CONNECTED = "connected";
/**
* Integer extra containing currently set USB configuration.
* Used in extras for the {@link #ACTION_USB_STATE} broadcast.
*/
public static final String USB_CONFIGURATION = "configuration";
/** /**
* Name of the USB mass storage USB function. * Name of the USB mass storage USB function.
* Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast * Used in extras for the {@link #ACTION_USB_STATE} broadcast
*/ */
public static final String USB_FUNCTION_MASS_STORAGE = "mass_storage"; public static final String USB_FUNCTION_MASS_STORAGE = "mass_storage";
/** /**
* Name of the adb USB function. * Name of the adb USB function.
* Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast * Used in extras for the {@link #ACTION_USB_STATE} broadcast
*/ */
public static final String USB_FUNCTION_ADB = "adb"; public static final String USB_FUNCTION_ADB = "adb";
/** /**
* Name of the RNDIS ethernet USB function. * Name of the RNDIS ethernet USB function.
* Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast * Used in extras for the {@link #ACTION_USB_STATE} broadcast
*/ */
public static final String USB_FUNCTION_RNDIS = "rndis"; public static final String USB_FUNCTION_RNDIS = "rndis";
/** /**
* Name of the MTP USB function. * Name of the MTP USB function.
* Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast * Used in extras for the {@link #ACTION_USB_STATE} broadcast
*/ */
public static final String USB_FUNCTION_MTP = "mtp"; public static final String USB_FUNCTION_MTP = "mtp";
/** /**
* Value indicating that a USB function is enabled. * Value indicating that a USB function is enabled.
* Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast * Used in extras for the {@link #ACTION_USB_STATE} broadcast
*/ */
public static final String USB_FUNCTION_ENABLED = "enabled"; public static final String USB_FUNCTION_ENABLED = "enabled";
/** /**
* Value indicating that a USB function is disabled. * Value indicating that a USB function is disabled.
* Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast * Used in extras for the {@link #ACTION_USB_STATE} broadcast
*/ */
public static final String USB_FUNCTION_DISABLED = "disabled"; public static final String USB_FUNCTION_DISABLED = "disabled";

View File

@ -358,8 +358,6 @@ public class NotificationManagerService extends INotificationManager.Stub
boolean adbEnabled = (UsbManager.USB_FUNCTION_ENABLED.equals( boolean adbEnabled = (UsbManager.USB_FUNCTION_ENABLED.equals(
extras.getString(UsbManager.USB_FUNCTION_ADB))); extras.getString(UsbManager.USB_FUNCTION_ADB)));
updateAdbNotification(usbConnected && adbEnabled); updateAdbNotification(usbConnected && adbEnabled);
} else if (action.equals(UsbManager.ACTION_USB_DISCONNECTED)) {
updateAdbNotification(false);
} else if (action.equals(Intent.ACTION_PACKAGE_REMOVED) } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
|| action.equals(Intent.ACTION_PACKAGE_RESTARTED) || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
|| (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))

View File

@ -40,15 +40,33 @@ class UsbService {
private static final String TAG = UsbService.class.getSimpleName(); private static final String TAG = UsbService.class.getSimpleName();
private static final boolean LOG = false; private static final boolean LOG = false;
private static final String USB_CONFIGURATION_MATCH = "DEVPATH=/devices/virtual/switch/usb_configuration"; private static final String USB_CONNECTED_MATCH =
private static final String USB_FUNCTIONS_MATCH = "DEVPATH=/devices/virtual/usb_composite/"; "DEVPATH=/devices/virtual/switch/usb_connected";
private static final String USB_CONFIGURATION_PATH = "/sys/class/switch/usb_configuration/state"; private static final String USB_CONFIGURATION_MATCH =
private static final String USB_COMPOSITE_CLASS_PATH = "/sys/class/usb_composite"; "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 static final int MSG_UPDATE = 0;
private int mUsbConfig = 0; // Delay for debouncing USB disconnects.
private int mPreviousUsbConfig = 0; // 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 // lists of enabled and disabled USB functions
private final ArrayList<String> mEnabledFunctions = new ArrayList<String>(); private final ArrayList<String> mEnabledFunctions = new ArrayList<String>();
@ -58,8 +76,6 @@ class UsbService {
private final Context mContext; private final Context mContext;
private PowerManagerService mPowerManager;
private final UEventObserver mUEventObserver = new UEventObserver() { private final UEventObserver mUEventObserver = new UEventObserver() {
@Override @Override
public void onUEvent(UEventObserver.UEvent event) { public void onUEvent(UEventObserver.UEvent event) {
@ -68,16 +84,23 @@ class UsbService {
} }
synchronized (this) { synchronized (this) {
String switchState = event.get("SWITCH_STATE"); String name = event.get("SWITCH_NAME");
if (switchState != null) { String state = event.get("SWITCH_STATE");
if (name != null && state != null) {
try { try {
int newConfig = Integer.parseInt(switchState); int intState = Integer.parseInt(state);
if (newConfig != mUsbConfig) { if ("usb_connected".equals(name)) {
mPreviousUsbConfig = mUsbConfig; mConnected = intState;
mUsbConfig = newConfig;
// trigger an Intent broadcast // trigger an Intent broadcast
if (mSystemReady) { if (mSystemReady) {
update(); // debounce disconnects
update(mConnected == 0);
}
} else if ("usb_configuration".equals(name)) {
mConfiguration = intState;
// trigger an Intent broadcast
if (mSystemReady) {
update(mConnected == 0);
} }
} }
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -111,6 +134,7 @@ class UsbService {
mContext = context; mContext = context;
init(); // set initial status init(); // set initial status
mUEventObserver.startObserving(USB_CONNECTED_MATCH);
mUEventObserver.startObserving(USB_CONFIGURATION_MATCH); mUEventObserver.startObserving(USB_CONFIGURATION_MATCH);
mUEventObserver.startObserving(USB_FUNCTIONS_MATCH); mUEventObserver.startObserving(USB_FUNCTIONS_MATCH);
} }
@ -119,10 +143,15 @@ class UsbService {
char[] buffer = new char[1024]; char[] buffer = new char[1024];
try { try {
FileReader file = new FileReader(USB_CONFIGURATION_PATH); FileReader file = new FileReader(USB_CONNECTED_PATH);
int len = file.read(buffer, 0, 1024); 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) { } catch (FileNotFoundException e) {
Slog.w(TAG, "This kernel does not have USB configuration switch support"); Slog.w(TAG, "This kernel does not have USB configuration switch support");
} catch (Exception e) { } catch (Exception e) {
@ -135,6 +164,7 @@ class UsbService {
File file = new File(files[i], "enable"); File file = new File(files[i], "enable");
FileReader reader = new FileReader(file); FileReader reader = new FileReader(file);
int len = reader.read(buffer, 0, 1024); int len = reader.read(buffer, 0, 1024);
reader.close();
int value = Integer.valueOf((new String(buffer, 0, len)).trim()); int value = Integer.valueOf((new String(buffer, 0, len)).trim());
String functionName = files[i].getName(); String functionName = files[i].getName();
if (value == 1) { if (value == 1) {
@ -152,13 +182,14 @@ class UsbService {
void systemReady() { void systemReady() {
synchronized (this) { synchronized (this) {
update(); update(false);
mSystemReady = true; mSystemReady = true;
} }
} }
private final void update() { private final void update(boolean delayed) {
mHandler.sendEmptyMessage(MSG_UPDATE); mHandler.removeMessages(MSG_UPDATE);
mHandler.sendEmptyMessageDelayed(MSG_UPDATE, delayed ? UPDATE_DELAY : 0);
} }
private final Handler mHandler = new Handler() { private final Handler mHandler = new Handler() {
@ -177,31 +208,26 @@ class UsbService {
switch (msg.what) { switch (msg.what) {
case MSG_UPDATE: case MSG_UPDATE:
synchronized (this) { synchronized (this) {
final ContentResolver cr = mContext.getContentResolver(); if (mConnected != mLastConnected || mConfiguration != mLastConfiguration) {
if (Settings.Secure.getInt(cr, final ContentResolver cr = mContext.getContentResolver();
Settings.Secure.DEVICE_PROVISIONED, 0) == 0) { if (Settings.Secure.getInt(cr,
Slog.i(TAG, "Device not provisioned, skipping USB broadcast"); Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
return; 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; mLastConnected = mConnected;
boolean usbConnected = (mUsbConfig != 0); mLastConfiguration = mConfiguration;
if (usbConnected) {
intent = new Intent(UsbManager.ACTION_USB_CONNECTED); // 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); addEnabledFunctions(intent);
} else { mContext.sendStickyBroadcast(intent);
intent = new Intent(UsbManager.ACTION_USB_DISCONNECTED);
} }
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; break;
} }

View File

@ -111,14 +111,6 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
private boolean mUsbMassStorageOff; // track the status of USB Mass Storage private boolean mUsbMassStorageOff; // track the status of USB Mass Storage
private boolean mUsbConnected; // track the status of USB connection 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) { public Tethering(Context context, Looper looper) {
mContext = context; mContext = context;
mLooper = looper; 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 { private class StateReceiver extends BroadcastReceiver {
public void onReceive(Context content, Intent intent) { public void onReceive(Context content, Intent intent) {
String action = intent.getAction(); String action = intent.getAction();
if (action.equals(UsbManager.ACTION_USB_STATE)) { if (action.equals(UsbManager.ACTION_USB_STATE)) {
// process connect events immediately, but delay handling disconnects mUsbConnected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
// to debounce USB configuration changes updateUsbStatus();
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);
} else if (action.equals(Intent.ACTION_MEDIA_SHARED)) { } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
mUsbMassStorageOff = false; mUsbMassStorageOff = false;
updateUsbStatus(); updateUsbStatus();