Skip to content

Commit 6da062a

Browse files
mikeandroidAndroid (Google) Code Review
authored andcommitted
Merge "DO NOT MERGE: Clean up USB notifications:" into gingerbread
2 parents 3333f8a + f13ec7a commit 6da062a

File tree

4 files changed

+86
-97
lines changed

4 files changed

+86
-97
lines changed

core/java/android/hardware/UsbManager.java

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,13 @@
2626
* @hide
2727
*/
2828
public class UsbManager {
29-
/**
30-
* Broadcast Action: A broadcast for USB connected events.
31-
*
32-
* The extras bundle will name/value pairs with the name of the function
33-
* and a value of either {@link #USB_FUNCTION_ENABLED} or {@link #USB_FUNCTION_DISABLED}.
34-
* Possible USB function names include {@link #USB_FUNCTION_MASS_STORAGE},
35-
* {@link #USB_FUNCTION_ADB}, {@link #USB_FUNCTION_RNDIS} and {@link #USB_FUNCTION_MTP}.
36-
*/
37-
public static final String ACTION_USB_CONNECTED =
38-
"android.hardware.action.USB_CONNECTED";
39-
40-
/**
41-
* Broadcast Action: A broadcast for USB disconnected events.
42-
*/
43-
public static final String ACTION_USB_DISCONNECTED =
44-
"android.hardware.action.USB_DISCONNECTED";
45-
4629
/**
4730
* Broadcast Action: A sticky broadcast for USB state change events.
4831
*
49-
* This is a sticky broadcast for clients that are interested in both USB connect and
50-
* disconnect events. If you are only concerned with one or the other, you can use
51-
* {@link #ACTION_USB_CONNECTED} or {@link #ACTION_USB_DISCONNECTED} to avoid receiving
52-
* unnecessary broadcasts. The boolean {@link #USB_CONNECTED} extra indicates whether
53-
* USB is connected or disconnected.
54-
* The extras bundle will also contain name/value pairs with the name of the function
55-
* and a value of either {@link #USB_FUNCTION_ENABLED} or {@link #USB_FUNCTION_DISABLED}.
32+
* This is a sticky broadcast for clients that includes USB connected/disconnected state,
33+
* the USB configuration that is currently set and a bundle containing name/value pairs
34+
* with the names of the functions and a value of either {@link #USB_FUNCTION_ENABLED}
35+
* or {@link #USB_FUNCTION_DISABLED}.
5636
* Possible USB function names include {@link #USB_FUNCTION_MASS_STORAGE},
5737
* {@link #USB_FUNCTION_ADB}, {@link #USB_FUNCTION_RNDIS} and {@link #USB_FUNCTION_MTP}.
5838
*/
@@ -65,39 +45,45 @@ public class UsbManager {
6545
*/
6646
public static final String USB_CONNECTED = "connected";
6747

48+
/**
49+
* Integer extra containing currently set USB configuration.
50+
* Used in extras for the {@link #ACTION_USB_STATE} broadcast.
51+
*/
52+
public static final String USB_CONFIGURATION = "configuration";
53+
6854
/**
6955
* Name of the USB mass storage USB function.
70-
* Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast
56+
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
7157
*/
7258
public static final String USB_FUNCTION_MASS_STORAGE = "mass_storage";
7359

7460
/**
7561
* Name of the adb USB function.
76-
* Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast
62+
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
7763
*/
7864
public static final String USB_FUNCTION_ADB = "adb";
7965

8066
/**
8167
* Name of the RNDIS ethernet USB function.
82-
* Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast
68+
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
8369
*/
8470
public static final String USB_FUNCTION_RNDIS = "rndis";
8571

8672
/**
8773
* Name of the MTP USB function.
88-
* Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast
74+
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
8975
*/
9076
public static final String USB_FUNCTION_MTP = "mtp";
9177

9278
/**
9379
* Value indicating that a USB function is enabled.
94-
* Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast
80+
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
9581
*/
9682
public static final String USB_FUNCTION_ENABLED = "enabled";
9783

9884
/**
9985
* Value indicating that a USB function is disabled.
100-
* Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast
86+
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
10187
*/
10288
public static final String USB_FUNCTION_DISABLED = "disabled";
10389

services/java/com/android/server/NotificationManagerService.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,6 @@ public void onReceive(Context context, Intent intent) {
358358
boolean adbEnabled = (UsbManager.USB_FUNCTION_ENABLED.equals(
359359
extras.getString(UsbManager.USB_FUNCTION_ADB)));
360360
updateAdbNotification(usbConnected && adbEnabled);
361-
} else if (action.equals(UsbManager.ACTION_USB_DISCONNECTED)) {
362-
updateAdbNotification(false);
363361
} else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
364362
|| action.equals(Intent.ACTION_PACKAGE_RESTARTED)
365363
|| (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))

services/java/com/android/server/UsbService.java

Lines changed: 68 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,33 @@ class UsbService {
4040
private static final String TAG = UsbService.class.getSimpleName();
4141
private static final boolean LOG = false;
4242

43-
private static final String USB_CONFIGURATION_MATCH = "DEVPATH=/devices/virtual/switch/usb_configuration";
44-
private static final String USB_FUNCTIONS_MATCH = "DEVPATH=/devices/virtual/usb_composite/";
45-
private static final String USB_CONFIGURATION_PATH = "/sys/class/switch/usb_configuration/state";
46-
private static final String USB_COMPOSITE_CLASS_PATH = "/sys/class/usb_composite";
43+
private static final String USB_CONNECTED_MATCH =
44+
"DEVPATH=/devices/virtual/switch/usb_connected";
45+
private static final String USB_CONFIGURATION_MATCH =
46+
"DEVPATH=/devices/virtual/switch/usb_configuration";
47+
private static final String USB_FUNCTIONS_MATCH =
48+
"DEVPATH=/devices/virtual/usb_composite/";
49+
private static final String USB_CONNECTED_PATH =
50+
"/sys/class/switch/usb_connected/state";
51+
private static final String USB_CONFIGURATION_PATH =
52+
"/sys/class/switch/usb_configuration/state";
53+
private static final String USB_COMPOSITE_CLASS_PATH =
54+
"/sys/class/usb_composite";
4755

4856
private static final int MSG_UPDATE = 0;
4957

50-
private int mUsbConfig = 0;
51-
private int mPreviousUsbConfig = 0;
58+
// Delay for debouncing USB disconnects.
59+
// We often get rapid connect/disconnect events when enabling USB functions,
60+
// which need debouncing.
61+
private static final int UPDATE_DELAY = 1000;
62+
63+
// current connected and configuration state
64+
private int mConnected;
65+
private int mConfiguration;
66+
67+
// last broadcasted connected and configuration state
68+
private int mLastConnected = -1;
69+
private int mLastConfiguration = -1;
5270

5371
// lists of enabled and disabled USB functions
5472
private final ArrayList<String> mEnabledFunctions = new ArrayList<String>();
@@ -58,8 +76,6 @@ class UsbService {
5876

5977
private final Context mContext;
6078

61-
private PowerManagerService mPowerManager;
62-
6379
private final UEventObserver mUEventObserver = new UEventObserver() {
6480
@Override
6581
public void onUEvent(UEventObserver.UEvent event) {
@@ -68,16 +84,23 @@ public void onUEvent(UEventObserver.UEvent event) {
6884
}
6985

7086
synchronized (this) {
71-
String switchState = event.get("SWITCH_STATE");
72-
if (switchState != null) {
87+
String name = event.get("SWITCH_NAME");
88+
String state = event.get("SWITCH_STATE");
89+
if (name != null && state != null) {
7390
try {
74-
int newConfig = Integer.parseInt(switchState);
75-
if (newConfig != mUsbConfig) {
76-
mPreviousUsbConfig = mUsbConfig;
77-
mUsbConfig = newConfig;
91+
int intState = Integer.parseInt(state);
92+
if ("usb_connected".equals(name)) {
93+
mConnected = intState;
94+
// trigger an Intent broadcast
95+
if (mSystemReady) {
96+
// debounce disconnects
97+
update(mConnected == 0);
98+
}
99+
} else if ("usb_configuration".equals(name)) {
100+
mConfiguration = intState;
78101
// trigger an Intent broadcast
79102
if (mSystemReady) {
80-
update();
103+
update(mConnected == 0);
81104
}
82105
}
83106
} catch (NumberFormatException e) {
@@ -111,6 +134,7 @@ public UsbService(Context context) {
111134
mContext = context;
112135
init(); // set initial status
113136

137+
mUEventObserver.startObserving(USB_CONNECTED_MATCH);
114138
mUEventObserver.startObserving(USB_CONFIGURATION_MATCH);
115139
mUEventObserver.startObserving(USB_FUNCTIONS_MATCH);
116140
}
@@ -119,10 +143,15 @@ private final void init() {
119143
char[] buffer = new char[1024];
120144

121145
try {
122-
FileReader file = new FileReader(USB_CONFIGURATION_PATH);
146+
FileReader file = new FileReader(USB_CONNECTED_PATH);
123147
int len = file.read(buffer, 0, 1024);
124-
mPreviousUsbConfig = mUsbConfig = Integer.valueOf((new String(buffer, 0, len)).trim());
148+
file.close();
149+
mConnected = Integer.valueOf((new String(buffer, 0, len)).trim());
125150

151+
file = new FileReader(USB_CONFIGURATION_PATH);
152+
len = file.read(buffer, 0, 1024);
153+
file.close();
154+
mConfiguration = Integer.valueOf((new String(buffer, 0, len)).trim());
126155
} catch (FileNotFoundException e) {
127156
Slog.w(TAG, "This kernel does not have USB configuration switch support");
128157
} catch (Exception e) {
@@ -135,6 +164,7 @@ private final void init() {
135164
File file = new File(files[i], "enable");
136165
FileReader reader = new FileReader(file);
137166
int len = reader.read(buffer, 0, 1024);
167+
reader.close();
138168
int value = Integer.valueOf((new String(buffer, 0, len)).trim());
139169
String functionName = files[i].getName();
140170
if (value == 1) {
@@ -152,13 +182,14 @@ private final void init() {
152182

153183
void systemReady() {
154184
synchronized (this) {
155-
update();
185+
update(false);
156186
mSystemReady = true;
157187
}
158188
}
159189

160-
private final void update() {
161-
mHandler.sendEmptyMessage(MSG_UPDATE);
190+
private final void update(boolean delayed) {
191+
mHandler.removeMessages(MSG_UPDATE);
192+
mHandler.sendEmptyMessageDelayed(MSG_UPDATE, delayed ? UPDATE_DELAY : 0);
162193
}
163194

164195
private final Handler mHandler = new Handler() {
@@ -177,31 +208,26 @@ public void handleMessage(Message msg) {
177208
switch (msg.what) {
178209
case MSG_UPDATE:
179210
synchronized (this) {
180-
final ContentResolver cr = mContext.getContentResolver();
211+
if (mConnected != mLastConnected || mConfiguration != mLastConfiguration) {
181212

182-
if (Settings.Secure.getInt(cr,
183-
Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
184-
Slog.i(TAG, "Device not provisioned, skipping USB broadcast");
185-
return;
186-
}
187-
// Send an Intent containing connected/disconnected state
188-
// and the enabled/disabled state of all USB functions
189-
Intent intent;
190-
boolean usbConnected = (mUsbConfig != 0);
191-
if (usbConnected) {
192-
intent = new Intent(UsbManager.ACTION_USB_CONNECTED);
213+
final ContentResolver cr = mContext.getContentResolver();
214+
if (Settings.Secure.getInt(cr,
215+
Settings.Secure.DEVICE_PROVISIONED, 0) == 0) {
216+
Slog.i(TAG, "Device not provisioned, skipping USB broadcast");
217+
return;
218+
}
219+
220+
mLastConnected = mConnected;
221+
mLastConfiguration = mConfiguration;
222+
223+
// send a sticky broadcast containing current USB state
224+
Intent intent = new Intent(UsbManager.ACTION_USB_STATE);
225+
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
226+
intent.putExtra(UsbManager.USB_CONNECTED, mConnected != 0);
227+
intent.putExtra(UsbManager.USB_CONFIGURATION, mConfiguration);
193228
addEnabledFunctions(intent);
194-
} else {
195-
intent = new Intent(UsbManager.ACTION_USB_DISCONNECTED);
229+
mContext.sendStickyBroadcast(intent);
196230
}
197-
mContext.sendBroadcast(intent);
198-
199-
// send a sticky broadcast for clients interested in both connect and disconnect
200-
intent = new Intent(UsbManager.ACTION_USB_STATE);
201-
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
202-
intent.putExtra(UsbManager.USB_CONNECTED, usbConnected);
203-
addEnabledFunctions(intent);
204-
mContext.sendStickyBroadcast(intent);
205231
}
206232
break;
207233
}

services/java/com/android/server/connectivity/Tethering.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,6 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
111111
private boolean mUsbMassStorageOff; // track the status of USB Mass Storage
112112
private boolean mUsbConnected; // track the status of USB connection
113113

114-
// mUsbHandler message
115-
static final int USB_STATE_CHANGE = 1;
116-
static final int USB_DISCONNECTED = 0;
117-
static final int USB_CONNECTED = 1;
118-
119-
// Time to delay before processing USB disconnect events
120-
static final long USB_DISCONNECT_DELAY = 1000;
121-
122114
public Tethering(Context context, Looper looper) {
123115
mContext = context;
124116
mLooper = looper;
@@ -429,25 +421,12 @@ private void updateUsbStatus() {
429421
}
430422
}
431423

432-
private Handler mUsbHandler = new Handler() {
433-
@Override
434-
public void handleMessage(Message msg) {
435-
mUsbConnected = (msg.arg1 == USB_CONNECTED);
436-
updateUsbStatus();
437-
}
438-
};
439-
440424
private class StateReceiver extends BroadcastReceiver {
441425
public void onReceive(Context content, Intent intent) {
442426
String action = intent.getAction();
443427
if (action.equals(UsbManager.ACTION_USB_STATE)) {
444-
// process connect events immediately, but delay handling disconnects
445-
// to debounce USB configuration changes
446-
boolean connected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
447-
Message msg = Message.obtain(mUsbHandler, USB_STATE_CHANGE,
448-
(connected ? USB_CONNECTED : USB_DISCONNECTED), 0);
449-
mUsbHandler.removeMessages(USB_STATE_CHANGE);
450-
mUsbHandler.sendMessageDelayed(msg, connected ? 0 : USB_DISCONNECT_DELAY);
428+
mUsbConnected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
429+
updateUsbStatus();
451430
} else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
452431
mUsbMassStorageOff = false;
453432
updateUsbStatus();

0 commit comments

Comments
 (0)