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

Commit 71985f5e authored by Philip P. Moltmann's avatar Philip P. Moltmann Committed by Android (Google) Code Review
Browse files

Merge "Do not access MTP devices when disabled."

parents 0676aa18 ec3cbb2a
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -16,7 +16,10 @@

package android.hardware.usb;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.content.Context;
import android.os.ParcelFileDescriptor;
import dalvik.system.CloseGuard;

@@ -33,6 +36,8 @@ public class UsbDeviceConnection {

    private final UsbDevice mDevice;

    private Context mContext;

    // used by the JNI code
    private long mNativeContext;

@@ -46,7 +51,8 @@ public class UsbDeviceConnection {
        mDevice = device;
    }

    /* package */ boolean open(String name, ParcelFileDescriptor pfd) {
    /* package */ boolean open(String name, ParcelFileDescriptor pfd, @NonNull Context context) {
        mContext = context.getApplicationContext();
        boolean wasOpened = native_open(name, pfd.getFileDescriptor());

        if (wasOpened) {
@@ -56,6 +62,15 @@ public class UsbDeviceConnection {
        return wasOpened;
    }

    /**
     * @return The application context the connection was created for.
     *
     * @hide
     */
    public @Nullable Context getContext() {
        return mContext;
    }

    /**
     * Releases all system resources related to the device.
     * Once the object is closed it cannot be used again.
+1 −1
Original line number Diff line number Diff line
@@ -332,7 +332,7 @@ public class UsbManager {
            ParcelFileDescriptor pfd = mService.openDevice(deviceName);
            if (pfd != null) {
                UsbDeviceConnection connection = new UsbDeviceConnection(device);
                boolean result = connection.open(deviceName, pfd);
                boolean result = connection.open(deviceName, pfd, mContext);
                pfd.close();
                if (result) {
                    return connection;
+13 −1
Original line number Diff line number Diff line
@@ -18,11 +18,13 @@ package android.mtp;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;

import android.os.UserManager;
import com.android.internal.util.Preconditions;

import java.io.IOException;
@@ -63,7 +65,17 @@ public final class MtpDevice {
     * @return true if the device was successfully opened.
     */
    public boolean open(@NonNull UsbDeviceConnection connection) {
        boolean result = native_open(mDevice.getDeviceName(), connection.getFileDescriptor());
        boolean result = false;

        Context context = connection.getContext();
        if (context != null) {
            UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);

            if (!userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
                result = native_open(mDevice.getDeviceName(), connection.getFileDescriptor());
            }
        }

        if (!result) {
            connection.close();
        }
+13 −4
Original line number Diff line number Diff line
@@ -1437,13 +1437,22 @@ class MountService extends IMountService.Stub
     * Decide if volume is mountable per device policies.
     */
    private boolean isMountDisallowed(VolumeInfo vol) {
        UserManager userManager = mContext.getSystemService(UserManager.class);

        boolean isUsbRestricted = false;
        if (vol.disk != null && vol.disk.isUsb()) {
            isUsbRestricted = userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER,
                    Binder.getCallingUserHandle());
        }

        boolean isTypeRestricted = false;
        if (vol.type == VolumeInfo.TYPE_PUBLIC || vol.type == VolumeInfo.TYPE_PRIVATE) {
            final UserManager userManager = mContext.getSystemService(UserManager.class);
            return userManager.hasUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
            isTypeRestricted = userManager
                    .hasUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
                    Binder.getCallingUserHandle());
        } else {
            return false;
        }

        return isUsbRestricted || isTypeRestricted;
    }

    private void enforceAdminUser() {