@@ -92,8 +92,8 @@ public class UsbService extends IUsbManager.Stub {
9292 private boolean mSystemReady ;
9393
9494 private UsbAccessory mCurrentAccessory ;
95- // functions to restore after exiting accessory mode
96- private final ArrayList <String > mAccessoryRestoreFunctions = new ArrayList <String >();
95+ // USB functions that are enabled by default, to restore after exiting accessory mode
96+ private final ArrayList <String > mDefaultFunctions = new ArrayList <String >();
9797
9898 private final Context mContext ;
9999 private final Object mLock = new Object ();
@@ -107,20 +107,6 @@ private final void functionEnabledLocked(String function, boolean enabled) {
107107 boolean enteringAccessoryMode =
108108 (mHasUsbAccessory && enabled && UsbManager .USB_FUNCTION_ACCESSORY .equals (function ));
109109
110- if (enteringAccessoryMode ) {
111- // keep a list of functions to reenable after exiting accessory mode
112- mAccessoryRestoreFunctions .clear ();
113- int count = mEnabledFunctions .size ();
114- for (int i = 0 ; i < count ; i ++) {
115- String f = mEnabledFunctions .get (i );
116- // RNDIS should not be restored and adb is handled automatically
117- if (!UsbManager .USB_FUNCTION_RNDIS .equals (f ) &&
118- !UsbManager .USB_FUNCTION_ADB .equals (f ) &&
119- !UsbManager .USB_FUNCTION_ACCESSORY .equals (f )) {
120- mAccessoryRestoreFunctions .add (f );
121- }
122- }
123- }
124110 if (enabled ) {
125111 if (!mEnabledFunctions .contains (function )) {
126112 mEnabledFunctions .add (function );
@@ -246,6 +232,11 @@ private final void init() {
246232 String functionName = files [i ].getName ();
247233 if (value == 1 ) {
248234 mEnabledFunctions .add (functionName );
235+ // adb is enabled/disabled automatically by the adbd daemon,
236+ // so don't treat it as a default function
237+ if (!UsbManager .USB_FUNCTION_ADB .equals (functionName )) {
238+ mDefaultFunctions .add (functionName );
239+ }
249240 } else {
250241 mDisabledFunctions .add (functionName );
251242 }
@@ -338,29 +329,24 @@ public void handleMessage(Message msg) {
338329 switch (msg .what ) {
339330 case MSG_UPDATE_STATE :
340331 if (mConnected != mLastConnected || mConfiguration != mLastConfiguration ) {
341- if (mConnected == 0 && mCurrentAccessory != null ) {
342- // turn off accessory mode when we are disconnected
332+ if (mConnected == 0 ) {
333+ // make sure accessory mode is off, and restore default functions
343334 if (UsbManager .setFunctionEnabled (
344335 UsbManager .USB_FUNCTION_ACCESSORY , false )) {
345336 Log .d (TAG , "exited USB accessory mode" );
346337
347- // restore previously enabled functions
348- for (String function : mAccessoryRestoreFunctions ) {
338+ int count = mDefaultFunctions .size ();
339+ for (int i = 0 ; i < count ; i ++) {
340+ String function = mDefaultFunctions .get (i );
349341 if (UsbManager .setFunctionEnabled (function , true )) {
350342 Log .e (TAG , "could not reenable function " + function );
351343 }
352344 }
353- mAccessoryRestoreFunctions . clear ();
345+ }
354346
347+ if (mCurrentAccessory != null ) {
355348 mDeviceManager .accessoryDetached (mCurrentAccessory );
356349 mCurrentAccessory = null ;
357-
358- // this will cause an immediate reset of the USB bus,
359- // so there is no point in sending the
360- // function disabled broadcast.
361- return ;
362- } else {
363- Log .e (TAG , "could not disable USB_FUNCTION_ACCESSORY" );
364350 }
365351 }
366352
@@ -416,8 +402,13 @@ public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
416402 pw .print (mDisabledFunctions .get (i ) + " " );
417403 }
418404 pw .println ("" );
405+ pw .print (" Default Functions: " );
406+ for (int i = 0 ; i < mDefaultFunctions .size (); i ++) {
407+ pw .print (mDefaultFunctions .get (i ) + " " );
408+ }
409+ pw .println ("" );
419410 pw .println (" mConnected: " + mConnected + ", mConfiguration: " + mConfiguration );
420- pw .println (" mCurrentAccessory: " + mCurrentAccessory );
411+ pw .println (" mCurrentAccessory: " + mCurrentAccessory );
421412
422413 mDeviceManager .dump (fd , pw );
423414 }
0 commit comments