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

Commit f59717dd authored by Mike Lockwood's avatar Mike Lockwood
Browse files

Combine UsbManager.setPrimaryFunction and setDefaultFunction



Due to the property trigger on persist.sys.usb.config,
setting the default function also sets the current function.
Now we combine both of these methods into setCurrentFunction, which has
a "makeDefault" option to make the new function the default.

This change should eliminate some problems with setting properties due to
multiple property triggers happening at the same time.

Change-Id: I9851299e9c2ee20475eada1a8104c0d50bf5a9e1
Signed-off-by: default avatarMike Lockwood <lockwood@android.com>
parent 7730ad56
Loading
Loading
Loading
Loading
+2 −5
Original line number Diff line number Diff line
@@ -82,11 +82,8 @@ interface IUsbManager
    /* Clears default preferences and permissions for the package */
    void clearDefaults(String packageName);

    /* Sets the current primary USB function. */
    void setPrimaryFunction(String functions);

    /* Sets the default primary USB function. */
    void setDefaultFunction(String functions);
    /* Sets the current USB function. */
    void setCurrentFunction(String function, boolean makeDefault);

    /* Sets the file path for USB mass storage backing file. */
    void setMassStorageBackingFile(String path);
+5 −19
Original line number Diff line number Diff line
@@ -408,32 +408,18 @@ public class UsbManager {
    }

    /**
     * Sets the primary USB function.
     * Sets the current USB function.
     *
     * @param function name of the USB function
     * @param makeDefault true if this should be set as the default
     *
     * {@hide}
     */
    public void setPrimaryFunction(String function) {
    public void setCurrentFunction(String function, boolean makeDefault) {
        try {
            mService.setPrimaryFunction(function);
            mService.setCurrentFunction(function, makeDefault);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setPrimaryFunction", e);
        }
    }

    /**
     * Sets the default primary USB function.
     *
     * @param function name of the USB function
     *
     * {@hide}
     */
    public void setDefaultFunction(String function) {
        try {
            mService.setDefaultFunction(function);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setDefaultFunction", e);
            Log.e(TAG, "RemoteException in setCurrentFunction", e);
        }
    }

+90 −91
Original line number Diff line number Diff line
@@ -78,14 +78,13 @@ public class UsbPreferenceActivity extends Activity implements View.OnClickListe
    public void onClick(View v) {
        if (v.equals(mMtpPtpButton)) {
            if (mPtpActive) {
                mUsbManager.setPrimaryFunction(UsbManager.USB_FUNCTION_MTP);
                mUsbManager.setDefaultFunction(UsbManager.USB_FUNCTION_MTP);
                mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MTP, true);
            } else {
                mUsbManager.setPrimaryFunction(UsbManager.USB_FUNCTION_PTP);
                mUsbManager.setDefaultFunction(UsbManager.USB_FUNCTION_PTP);
                mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_PTP, true);
            }
        } else if (v.equals(mInstallerCdButton)) {
            mUsbManager.setPrimaryFunction(UsbManager.USB_FUNCTION_MASS_STORAGE);
            // installer CD is never default
            mUsbManager.setCurrentFunction(UsbManager.USB_FUNCTION_MASS_STORAGE, false);
            mUsbManager.setMassStorageBackingFile(mInstallerImagePath);
        }

+2 −2
Original line number Diff line number Diff line
@@ -509,9 +509,9 @@ public class Tethering extends INetworkManagementEventObserver.Stub {
        }
        try {
            if (enabled) {
                usbManager.setPrimaryFunction(UsbManager.USB_FUNCTION_RNDIS);
                usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_RNDIS, false);
            } else {
                usbManager.setPrimaryFunction(null);
                usbManager.setCurrentFunction(null, false);
            }
        } catch (Exception e) {
            Log.e(TAG, "Error toggling usb RNDIS", e);
+55 −43
Original line number Diff line number Diff line
@@ -77,9 +77,8 @@ public class UsbDeviceManager {

    private static final int MSG_UPDATE_STATE = 0;
    private static final int MSG_ENABLE_ADB = 1;
    private static final int MSG_SET_PRIMARY_FUNCTION = 2;
    private static final int MSG_SET_DEFAULT_FUNCTION = 3;
    private static final int MSG_SYSTEM_READY = 4;
    private static final int MSG_SET_CURRENT_FUNCTION = 2;
    private static final int MSG_SYSTEM_READY = 3;

    // Delay for debouncing USB disconnects.
    // We often get rapid connect/disconnect events when enabling USB functions,
@@ -227,7 +226,7 @@ public class UsbDeviceManager {
                mHandler.updateState(state);
            } else if ("START".equals(accessory)) {
                Slog.d(TAG, "got accessory start");
                setPrimaryFunction(UsbManager.USB_FUNCTION_ACCESSORY);
                setCurrentFunction(UsbManager.USB_FUNCTION_ACCESSORY, false);
            }
        }
    };
@@ -371,6 +370,14 @@ public class UsbDeviceManager {
            sendMessage(m);
        }

        public void sendMessage(int what, Object arg0, boolean arg1) {
            removeMessages(what);
            Message m = Message.obtain(this, what);
            m.obj = arg0;
            m.arg1 = (arg1 ? 1 : 0);
            sendMessage(m);
        }

        public void updateState(String state) {
            int connected, configured;

@@ -395,24 +402,30 @@ public class UsbDeviceManager {
            sendMessageDelayed(msg, (connected == 0) ? UPDATE_DELAY : 0);
        }

        private boolean setUsbConfig(String config) {
            // set the new configuration
            SystemProperties.set("sys.usb.config", config);
        private boolean waitForState(String state) {
            // wait for the transition to complete.
            // give up after 1 second.
            for (int i = 0; i < 20; i++) {
                // State transition is done when sys.usb.conf.done is set to the new configuration
                if (config.equals(SystemProperties.get("sys.usb.state"))) return true;
                if (state.equals(SystemProperties.get("sys.usb.state"))) return true;
                try {
                    // try again in 50ms
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                }
            }
            Log.e(TAG, "waitForState(" + state + ") FAILED");
            return false;
        }

        private void setCurrentFunctions(String functions) {
        private boolean setUsbConfig(String config) {
            Log.d(TAG, "setUsbConfig(" + config + ")");
            // set the new configuration
            SystemProperties.set("sys.usb.config", config);
            return waitForState(config);
        }

        private void doSetCurrentFunctions(String functions) {
            if (!mCurrentFunctions.equals(functions)) {
                if (!setUsbConfig("none") || !setUsbConfig(functions)) {
                    Log.e(TAG, "Failed to switch USB configuration to " + functions);
@@ -428,17 +441,14 @@ public class UsbDeviceManager {
            if (enable != mAdbEnabled) {
                mAdbEnabled = enable;
                String functions;
                // Due to the persist.sys.usb.config property trigger, changing adb state requires
                // switching to default function
                if (enable) {
                    functions = addFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_ADB);
                    mDefaultFunctions = addFunction(mDefaultFunctions,
                            UsbManager.USB_FUNCTION_ADB);
                    functions = addFunction(mDefaultFunctions, UsbManager.USB_FUNCTION_ADB);
                } else {
                    functions = removeFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_ADB);
                    mDefaultFunctions = removeFunction(mDefaultFunctions,
                            UsbManager.USB_FUNCTION_ADB);
                    functions = removeFunction(mDefaultFunctions, UsbManager.USB_FUNCTION_ADB);
                }
                SystemProperties.set("persist.sys.usb.config", mDefaultFunctions);
                setCurrentFunctions(functions);
                setCurrentFunction(functions, true);
                updateAdbNotification(mAdbEnabled && mConnected);
            }
        }
@@ -449,7 +459,7 @@ public class UsbDeviceManager {
            } else {
                functionList = removeFunction(functionList, UsbManager.USB_FUNCTION_ADB);
            }
            setCurrentFunctions(functionList);
            doSetCurrentFunctions(functionList);
        }

        private void updateCurrentAccessory() {
@@ -503,8 +513,6 @@ public class UsbDeviceManager {

        @Override
        public void handleMessage(Message msg) {
            String function;

            switch (msg.what) {
                case MSG_UPDATE_STATE:
                    mConnected = (msg.arg1 == 1);
@@ -518,7 +526,7 @@ public class UsbDeviceManager {

                    if (!mConnected) {
                        // restore defaults when USB is disconnected
                        setCurrentFunctions(mDefaultFunctions);
                        doSetCurrentFunctions(mDefaultFunctions);
                    }
                    if (mSystemReady) {
                        updateUsbState();
@@ -527,20 +535,31 @@ public class UsbDeviceManager {
                case MSG_ENABLE_ADB:
                    setAdbEnabled(msg.arg1 == 1);
                    break;
                case MSG_SET_PRIMARY_FUNCTION:
                    function = (String)msg.obj;
                case MSG_SET_CURRENT_FUNCTION:
                    String function = (String)msg.obj;
                    boolean makeDefault = (msg.arg1 == 1);
                    if (makeDefault) {
                        if (function == null) {
                        function = mDefaultFunctions;
                            throw new NullPointerException();
                        }
                    setEnabledFunctions(function);
                    break;
                case MSG_SET_DEFAULT_FUNCTION:
                    function = (String)msg.obj;
                        if (mAdbEnabled) {
                            function = addFunction(function, UsbManager.USB_FUNCTION_ADB);
                        }

                        setUsbConfig("none");
                        // setting this property will change the current USB state
                        // via a property trigger
                        SystemProperties.set("persist.sys.usb.config", function);
                        if (waitForState(function)) {
                            mCurrentFunctions = function;
                            mDefaultFunctions = function;
                        }
                    } else {
                        if (function == null) {
                            function = mDefaultFunctions;
                        }
                        setEnabledFunctions(function);
                    }
                    break;
                case MSG_SYSTEM_READY:
                    updateUsbNotification(mConnected);
@@ -588,15 +607,8 @@ public class UsbDeviceManager {
            return nativeOpenAccessory();
        }

    public void setPrimaryFunction(String function) {
        mHandler.sendMessage(MSG_SET_PRIMARY_FUNCTION, function);
    }

    public void setDefaultFunction(String function) {
        if (function == null) {
            throw new NullPointerException();
        }
        mHandler.sendMessage(MSG_SET_DEFAULT_FUNCTION, function);
    public void setCurrentFunction(String function, boolean makeDefault) {
        mHandler.sendMessage(MSG_SET_CURRENT_FUNCTION, function, makeDefault);
    }

    public void setMassStorageBackingFile(String path) {
Loading