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

Commit 1a008c1c authored by Nick Kralevich's avatar Nick Kralevich
Browse files

UsbDeviceManager: fix b/21429947 regression (try #2)

As discussed in b/21429947 (commit
67401906), MTP must always be
enabled, even if access to the underlying MTP data is disabled.
Otherwise, Android will not enumerate on the USB bus, and won't
receive notifications from the kernel about USB state changes. This
effectively prevents using MTP functionality on user builds, or
on userdebug/eng builds with adb turned off.

Always ensure that MTP is the default driver mode.

Move the DISALLOW_USB_FILE_TRANSFER filtering of mUsbDataUnlocked from
setting time to the time we post the sticky broadcast.

Remove isUsbDataUnlocked(). It essentially duplicates data in the sticky
broadcast.

Bug: 22447614
Bug: 21429947
Change-Id: I9d0d94cadbf6db6281ebd77bfb7162f9d06520c2
parent 7469060e
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -97,9 +97,6 @@ interface IUsbManager
     */
    void setUsbDataUnlocked(boolean unlock);

    /* Returns true iff sensitive user data is exposed on the USB connection. */
    boolean isUsbDataUnlocked();

    /* Allow USB debugging from the attached host. If alwaysAllow is true, add the
     * the public key to list of host keys that the user has approved.
     */
+0 −15
Original line number Diff line number Diff line
@@ -519,21 +519,6 @@ public class UsbManager {
        }
    }

    /**
     * Returns {@code true} iff access to sensitive USB data is currently allowed when
     * in device mode.
     *
     * {@hide}
     */
    public boolean isUsbDataUnlocked() {
        try {
            return mService.isUsbDataUnlocked();
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in isUsbDataUnlocked", e);
        }
        return false;
    }

    /**
     * Returns a list of physical USB ports on the device.
     * <p>
+22 −21
Original line number Diff line number Diff line
@@ -49,10 +49,8 @@ import com.android.internal.util.IndentingPrintWriter;
import com.android.server.FgThread;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@@ -316,6 +314,9 @@ public class UsbDeviceManager {
                // Restore default functions.
                mCurrentFunctions = SystemProperties.get(USB_CONFIG_PROPERTY,
                        UsbManager.USB_FUNCTION_NONE);
                if (UsbManager.USB_FUNCTION_NONE.equals(mCurrentFunctions)) {
                    mCurrentFunctions = UsbManager.USB_FUNCTION_MTP;
                }
                mCurrentFunctionsApplied = mCurrentFunctions.equals(
                        SystemProperties.get(USB_STATE_PROPERTY));
                mAdbEnabled = UsbManager.containsFunction(getDefaultFunctions(),
@@ -400,6 +401,14 @@ public class UsbDeviceManager {
            return waitForState(config);
        }

        private void setUsbDataUnlocked(boolean enable) {
            if (DEBUG) Slog.d(TAG, "setUsbDataUnlocked: " + enable);
            mUsbDataUnlocked = enable;
            updateUsbNotification();
            updateUsbStateBroadcast();
            setEnabledFunctions(mCurrentFunctions, true);
        }

        private void setAdbEnabled(boolean enable) {
            if (DEBUG) Slog.d(TAG, "setAdbEnabled: " + enable);
            if (enable != mAdbEnabled) {
@@ -471,7 +480,6 @@ public class UsbDeviceManager {
            }
            functions = applyAdbFunction(functions);
            functions = applyOemOverrideFunction(functions);
            functions = applyUserRestrictions(functions);

            if (!mCurrentFunctions.equals(functions) || !mCurrentFunctionsApplied
                    || forceRestart) {
@@ -502,13 +510,9 @@ public class UsbDeviceManager {
            return functions;
        }

        private String applyUserRestrictions(String functions) {
        private boolean isUsbTransferAllowed() {
            UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
            if (userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
                functions = UsbManager.removeFunction(functions, UsbManager.USB_FUNCTION_MTP);
                functions = UsbManager.removeFunction(functions, UsbManager.USB_FUNCTION_PTP);
            }
           return functions;
            return !userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER);
        }

        private void updateCurrentAccessory() {
@@ -555,7 +559,7 @@ public class UsbDeviceManager {
                    | Intent.FLAG_RECEIVER_FOREGROUND);
            intent.putExtra(UsbManager.USB_CONNECTED, mConnected);
            intent.putExtra(UsbManager.USB_CONFIGURED, mConfigured);
            intent.putExtra(UsbManager.USB_DATA_UNLOCKED, mUsbDataUnlocked);
            intent.putExtra(UsbManager.USB_DATA_UNLOCKED, isUsbTransferAllowed() && mUsbDataUnlocked);

            if (mCurrentFunctions != null) {
                String[] functions = mCurrentFunctions.split(",");
@@ -659,10 +663,7 @@ public class UsbDeviceManager {
                    setEnabledFunctions(mCurrentFunctions, false);
                    break;
                case MSG_SET_USB_DATA_UNLOCKED:
                    mUsbDataUnlocked = (msg.arg1 == 1);
                    updateUsbNotification();
                    updateUsbStateBroadcast();
                    setEnabledFunctions(mCurrentFunctions, true);
                    setUsbDataUnlocked(msg.arg1 == 1);
                    break;
                case MSG_SYSTEM_READY:
                    updateUsbNotification();
@@ -807,8 +808,12 @@ public class UsbDeviceManager {
        }

        private String getDefaultFunctions() {
            return SystemProperties.get(USB_PERSISTENT_CONFIG_PROPERTY,
                    UsbManager.USB_FUNCTION_ADB);
            String func = SystemProperties.get(USB_PERSISTENT_CONFIG_PROPERTY,
                    UsbManager.USB_FUNCTION_NONE);
            if (UsbManager.USB_FUNCTION_NONE.equals(func)) {
                func = UsbManager.USB_FUNCTION_MTP;
            }
            return func;
        }

        public void dump(IndentingPrintWriter pw) {
@@ -817,6 +822,7 @@ public class UsbDeviceManager {
            pw.println("  mCurrentFunctionsApplied: " + mCurrentFunctionsApplied);
            pw.println("  mConnected: " + mConnected);
            pw.println("  mConfigured: " + mConfigured);
            pw.println("  mUsbDataUnlocked: " + mUsbDataUnlocked);
            pw.println("  mCurrentAccessory: " + mCurrentAccessory);
            try {
                pw.println("  Kernel state: "
@@ -864,11 +870,6 @@ public class UsbDeviceManager {
        mHandler.sendMessage(MSG_SET_USB_DATA_UNLOCKED, unlocked);
    }

    public boolean isUsbDataUnlocked() {
        if (DEBUG) Slog.d(TAG, "isUsbDataUnlocked() -> " + mHandler.mUsbDataUnlocked);
        return mHandler.mUsbDataUnlocked;
    }

    private void readOemUsbOverrideConfig() {
        String[] configList = mContext.getResources().getStringArray(
            com.android.internal.R.array.config_oemUsbModeOverride);
+0 −13
Original line number Diff line number Diff line
@@ -322,22 +322,9 @@ public class UsbService extends IUsbManager.Stub {
    @Override
    public void setUsbDataUnlocked(boolean unlocked) {
        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
        // If attempt to change USB function while file transfer is restricted, ensure that
        // usb data is always locked, and return.
        UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
        if (userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
            if (mDeviceManager != null) mDeviceManager.setUsbDataUnlocked(false);
            return;
        }
        mDeviceManager.setUsbDataUnlocked(unlocked);
    }

    @Override
    public boolean isUsbDataUnlocked() {
        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
        return mDeviceManager.isUsbDataUnlocked();
    }

    @Override
    public void allowUsbDebugging(boolean alwaysAllow, String publicKey) {
        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);