Skip to content

Commit 8f2e386

Browse files
committed
DO NOT MERGE: UsbService: Make USB disconnect handling for accessory mode more robust
Handle the case where the kernel driver is in accessory mode but we failed to initialize it at the framework level. On disconnnect, check to see if the accessory kernel driver is enabled rather than checking mCurrentAccessory. That way we will restore the USB state in the kernel even if mCurrentAccessory is null. Change-Id: I2c4f6edb34aae2064f4b62ec0461d1fdd8770541 Signed-off-by: Mike Lockwood <lockwood@android.com>
1 parent 87805ca commit 8f2e386

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,14 @@ public void handleMessage(Message msg) {
387387
case MSG_UPDATE_STATE:
388388
if (mConnected != mLastConnected || mConfiguration != mLastConfiguration) {
389389
if (mConnected == 0) {
390-
// make sure accessory mode is off, and restore default functions
391-
if (mCurrentAccessory != null && UsbManager.setFunctionEnabled(
392-
UsbManager.USB_FUNCTION_ACCESSORY, false)) {
390+
if (UsbManager.isFunctionEnabled(
391+
UsbManager.USB_FUNCTION_ACCESSORY)) {
392+
// make sure accessory mode is off, and restore default functions
393393
Log.d(TAG, "exited USB accessory mode");
394-
394+
if (!UsbManager.setFunctionEnabled
395+
(UsbManager.USB_FUNCTION_ACCESSORY, false)) {
396+
Log.e(TAG, "could not disable accessory function");
397+
}
395398
int count = mDefaultFunctions.size();
396399
for (int i = 0; i < count; i++) {
397400
String function = mDefaultFunctions.get(i);
@@ -400,8 +403,10 @@ public void handleMessage(Message msg) {
400403
}
401404
}
402405

403-
mDeviceManager.accessoryDetached(mCurrentAccessory);
404-
mCurrentAccessory = null;
406+
if (mCurrentAccessory != null) {
407+
mDeviceManager.accessoryDetached(mCurrentAccessory);
408+
mCurrentAccessory = null;
409+
}
405410
}
406411
}
407412

0 commit comments

Comments
 (0)