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

Commit 0f7e4a66 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Properly close USB device connection in Mtp device"

parents 40251d1d b828b779
Loading
Loading
Loading
Loading
+39 −9
Original line number Diff line number Diff line
@@ -25,7 +25,9 @@ import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;

import android.os.UserManager;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.Preconditions;
import dalvik.system.CloseGuard;

import java.io.IOException;

@@ -45,6 +47,16 @@ public final class MtpDevice {
        System.loadLibrary("media_jni");
    }

    /** Make sure that MTP device is closed properly */
    @GuardedBy("mLock")
    private CloseGuard mCloseGuard = CloseGuard.get();

    /** Current connection to the {@link #mDevice}, or null if device is not connected */
    @GuardedBy("mLock")
    private UsbDeviceConnection mConnection;

    private final Object mLock = new Object();

    /**
     * MtpClient constructor
     *
@@ -68,8 +80,11 @@ public final class MtpDevice {
        boolean result = false;

        Context context = connection.getContext();

        synchronized (mLock) {
            if (context != null) {
            UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
                UserManager userManager = (UserManager) context
                        .getSystemService(Context.USER_SERVICE);

                if (!userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
                    result = native_open(mDevice.getDeviceName(), connection.getFileDescriptor());
@@ -78,7 +93,12 @@ public final class MtpDevice {

            if (!result) {
                connection.close();
            } else {
                mConnection = connection;
                mCloseGuard.open("close");
            }
        }

        return result;
    }

@@ -88,13 +108,23 @@ public final class MtpDevice {
     * with a new {@link android.hardware.usb.UsbDeviceConnection}.
     */
    public void close() {
        synchronized (mLock) {
            if (mConnection != null) {
                mCloseGuard.close();

                native_close();

                mConnection.close();
                mConnection = null;
            }
        }
    }

    @Override
    protected void finalize() throws Throwable {
        try {
            native_close();
            mCloseGuard.warnIfOpen();
            close();
        } finally {
            super.finalize();
        }
+3 −0
Original line number Diff line number Diff line
@@ -194,6 +194,9 @@ android_mtp_MtpDevice_open(JNIEnv *env, jobject thiz, jstring deviceName, jint f
        return JNI_FALSE;
    }

    // The passed in fd is maintained by the UsbDeviceConnection
    fd = dup(fd);

    MtpDevice* device = MtpDevice::open(deviceNameStr, fd);
    env->ReleaseStringUTFChars(deviceName, deviceNameStr);