diff options
author | 2016-09-13 16:35:45 -0700 | |
---|---|---|
committer | 2016-10-17 11:19:58 -0700 | |
commit | b828b77985416c4d870771a02cd523397726c215 (patch) | |
tree | 5ff111299700ca511cb2a0648256c83fc20879c3 | |
parent | cf6782c9409f98404302eba22c891072291e73b3 (diff) |
Properly close USB device connection in Mtp device
Test: Built
Fixes: 32073045
Change-Id: I05179377532c1bd4dff1f4a4e0e837cb645317e3
-rw-r--r-- | media/java/android/mtp/MtpDevice.java | 48 | ||||
-rw-r--r-- | media/jni/android_mtp_MtpDevice.cpp | 3 |
2 files changed, 42 insertions, 9 deletions
diff --git a/media/java/android/mtp/MtpDevice.java b/media/java/android/mtp/MtpDevice.java index 4e7551c1e855..d6958b324ca3 100644 --- a/media/java/android/mtp/MtpDevice.java +++ b/media/java/android/mtp/MtpDevice.java @@ -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,17 +80,25 @@ public final class MtpDevice { 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()); + synchronized (mLock) { + 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(); + 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() { - native_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(); } diff --git a/media/jni/android_mtp_MtpDevice.cpp b/media/jni/android_mtp_MtpDevice.cpp index 8bcc85f7155b..768ac1d0a4cb 100644 --- a/media/jni/android_mtp_MtpDevice.cpp +++ b/media/jni/android_mtp_MtpDevice.cpp @@ -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); |