Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 049c7186 authored by David Stevens's avatar David Stevens
Browse files

Exit accessory mode after debounce timeout when necessary

Change-Id: I1e7c1f4f31a310ff0369690a25859b33aaed14fc
Fixes: 29377436
Test: manual testing with 2011 ADK and multiple Android Auto headunits
parent 6a022fe3
Loading
Loading
Loading
Loading
+25 −20
Original line number Diff line number Diff line
@@ -119,6 +119,7 @@ public class UsbDeviceManager {
    private static final int MSG_USER_SWITCHED = 5;
    private static final int MSG_UPDATE_USER_RESTRICTIONS = 6;
    private static final int MSG_UPDATE_HOST_STATE = 7;
    private static final int MSG_ACCESSORY_MODE_ENTER_TIMEOUT = 8;

    private static final int AUDIO_MODE_SOURCE = 1;

@@ -127,9 +128,6 @@ public class UsbDeviceManager {
    // which need debouncing.
    private static final int UPDATE_DELAY = 1000;

    // Time we received a request to enter USB accessory mode
    private long mAccessoryModeRequestTime = 0;

    // Timeout for entering USB request mode.
    // Request is cancelled if host does not configure device within 10 seconds.
    private static final int ACCESSORY_REQUEST_TIMEOUT = 10 * 1000;
@@ -297,7 +295,8 @@ public class UsbDeviceManager {
        }

        if (functions != null) {
            mAccessoryModeRequestTime = SystemClock.elapsedRealtime();
            mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_ACCESSORY_MODE_ENTER_TIMEOUT),
                    ACCESSORY_REQUEST_TIMEOUT);
            setCurrentFunctions(functions, false);
        }
    }
@@ -586,14 +585,10 @@ public class UsbDeviceManager {
        private void updateCurrentAccessory() {
            // We are entering accessory mode if we have received a request from the host
            // and the request has not timed out yet.
            boolean enteringAccessoryMode =
                    mAccessoryModeRequestTime > 0 &&
                        SystemClock.elapsedRealtime() <
                            mAccessoryModeRequestTime + ACCESSORY_REQUEST_TIMEOUT;
            boolean enteringAccessoryMode = hasMessages(MSG_ACCESSORY_MODE_ENTER_TIMEOUT);

            if (mConfigured && enteringAccessoryMode) {
                // successfully entered accessory mode

                if (mAccessoryStrings != null) {
                    mCurrentAccessory = new UsbAccessory(mAccessoryStrings);
                    Slog.d(TAG, "entering USB accessory mode: " + mCurrentAccessory);
@@ -604,7 +599,12 @@ public class UsbDeviceManager {
                } else {
                    Slog.e(TAG, "nativeGetAccessoryStrings failed");
                }
            } else if (!enteringAccessoryMode) {
            } else if (!mConnected && !enteringAccessoryMode) {
                notifyAccessoryModeExit();
            }
        }

        private void notifyAccessoryModeExit() {
            // make sure accessory mode is off
            // and restore default functions
            Slog.d(TAG, "exited USB accessory mode");
@@ -618,7 +618,6 @@ public class UsbDeviceManager {
                mAccessoryStrings = null;
            }
        }
        }

        private boolean isUsbStateChanged(Intent intent) {
            final Set<String> keySet = intent.getExtras().keySet();
@@ -805,6 +804,12 @@ public class UsbDeviceManager {
                    }
                    break;
                }
                case MSG_ACCESSORY_MODE_ENTER_TIMEOUT: {
                    if (!mConnected) {
                        notifyAccessoryModeExit();
                    }
                    break;
                }
            }
        }