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

Commit 58018d01 authored by Jerry Zhang's avatar Jerry Zhang
Browse files

Fixed handling of usb state during adb changes.

When changing state to adb from mtp (charging), we want to disable
the old mtp function. Similarly when change to away from adb to
charging, enable the mtp function.

Also the mtp function should never be persisted.

Bug: 31818377
Bug: 31814300
Test: Manually verify that the correct usb configuration is displayed.
Test: Manually verify that logcat is not kicked during boot.
Change-Id: Idcb7f53be39ea38712d5de45b323d8daeb552129
parent 7a396be6
Loading
Loading
Loading
Loading
+21 −18
Original line number Original line Diff line number Diff line
@@ -339,9 +339,6 @@ public class UsbDeviceManager {
                // Restore default functions.
                // Restore default functions.
                mCurrentFunctions = SystemProperties.get(USB_CONFIG_PROPERTY,
                mCurrentFunctions = SystemProperties.get(USB_CONFIG_PROPERTY,
                        UsbManager.USB_FUNCTION_NONE);
                        UsbManager.USB_FUNCTION_NONE);
                if (UsbManager.USB_FUNCTION_NONE.equals(mCurrentFunctions)) {
                    mCurrentFunctions = UsbManager.USB_FUNCTION_MTP;
                }
                mCurrentFunctionsApplied = mCurrentFunctions.equals(
                mCurrentFunctionsApplied = mCurrentFunctions.equals(
                        SystemProperties.get(USB_STATE_PROPERTY));
                        SystemProperties.get(USB_STATE_PROPERTY));
                mAdbEnabled = UsbManager.containsFunction(getDefaultFunctions(),
                mAdbEnabled = UsbManager.containsFunction(getDefaultFunctions(),
@@ -454,17 +451,25 @@ public class UsbDeviceManager {
            if (DEBUG) Slog.d(TAG, "setAdbEnabled: " + enable);
            if (DEBUG) Slog.d(TAG, "setAdbEnabled: " + enable);
            if (enable != mAdbEnabled) {
            if (enable != mAdbEnabled) {
                mAdbEnabled = enable;
                mAdbEnabled = enable;
                String oldFunctions = mCurrentFunctions;

                // Persist the adb setting
                String newFunction = enable ? UsbManager.USB_FUNCTION_ADB
                    : UsbManager.USB_FUNCTION_NONE;
                if (!UsbManager.containsFunction(getDefaultFunctions(), newFunction))
                   SystemProperties.set(USB_PERSISTENT_CONFIG_PROPERTY, newFunction);

                // Changing the persistent config also changes the normal
                // config. Wait for this to happen before changing again.
                waitForState(newFunction);


                // Due to the persist.sys.usb.config property trigger, changing adb state requires
                // Remove mtp from the config if file transfer is not enabled
                // persisting default function
                if (oldFunctions.equals(UsbManager.USB_FUNCTION_MTP) && 
                String oldFunctions = getDefaultFunctions();
                        !mUsbDataUnlocked && enable) {
                String newFunctions = applyAdbFunction(oldFunctions);
                    oldFunctions = UsbManager.USB_FUNCTION_NONE;
                if (!oldFunctions.equals(newFunctions)) {
                    SystemProperties.set(USB_PERSISTENT_CONFIG_PROPERTY, newFunctions);
                }
                }


                // After persisting them use the lock-down aware function set
                setEnabledFunctions(oldFunctions, false, mUsbDataUnlocked);
                setEnabledFunctions(mCurrentFunctions, false, mUsbDataUnlocked);
                updateAdbNotification();
                updateAdbNotification();
            }
            }


@@ -476,7 +481,8 @@ public class UsbDeviceManager {
        /**
        /**
         * Evaluates USB function policies and applies the change accordingly.
         * Evaluates USB function policies and applies the change accordingly.
         */
         */
        private void setEnabledFunctions(String functions, boolean forceRestart, boolean usbDataUnlocked) {
        private void setEnabledFunctions(String functions, boolean forceRestart,
                boolean usbDataUnlocked) {
            if (DEBUG) Slog.d(TAG, "setEnabledFunctions functions=" + functions + ", "
            if (DEBUG) Slog.d(TAG, "setEnabledFunctions functions=" + functions + ", "
                    + "forceRestart=" + forceRestart);
                    + "forceRestart=" + forceRestart);


@@ -522,7 +528,8 @@ public class UsbDeviceManager {
        }
        }


        private boolean trySetEnabledFunctions(String functions, boolean forceRestart) {
        private boolean trySetEnabledFunctions(String functions, boolean forceRestart) {
            if (functions == null) {
            if (functions == null || applyAdbFunction(functions)
                    .equals(UsbManager.USB_FUNCTION_NONE)) {
                functions = getDefaultFunctions();
                functions = getDefaultFunctions();
            }
            }
            functions = applyAdbFunction(functions);
            functions = applyAdbFunction(functions);
@@ -602,10 +609,6 @@ public class UsbDeviceManager {
            if (mBroadcastedIntent == null) {
            if (mBroadcastedIntent == null) {
                for (String key : keySet) {
                for (String key : keySet) {
                    if (intent.getBooleanExtra(key, false)) {
                    if (intent.getBooleanExtra(key, false)) {
                        // MTP function is enabled by default.
                        if (UsbManager.USB_FUNCTION_MTP.equals(key)) {
                            continue;
                        }
                        return true;
                        return true;
                    }
                    }
                }
                }