summaryrefslogtreecommitdiff
path: root/media/java/android/mtp/MtpDevice.java
diff options
context:
space:
mode:
Diffstat (limited to 'media/java/android/mtp/MtpDevice.java')
-rw-r--r--media/java/android/mtp/MtpDevice.java85
1 files changed, 84 insertions, 1 deletions
diff --git a/media/java/android/mtp/MtpDevice.java b/media/java/android/mtp/MtpDevice.java
index a68361b305e1..95cb5206b9b5 100644
--- a/media/java/android/mtp/MtpDevice.java
+++ b/media/java/android/mtp/MtpDevice.java
@@ -18,6 +18,9 @@ package android.mtp;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
+import android.os.CancellationSignal;
+import android.os.OperationCanceledException;
+import android.os.ParcelFileDescriptor;
/**
* This class represents an MTP or PTP device connected on the USB host bus. An application can
@@ -46,7 +49,7 @@ public final class MtpDevice {
/**
* Opens the MTP device. Once the device is open it takes ownership of the
- * {@link android.hardware.usb.UsbDeviceConnection}.
+ * {@link android.hardware.usb.UsbDeviceConnection}.
* The connection will be closed when you call {@link #close()}
* The connection will also be closed if this method fails.
*
@@ -235,6 +238,80 @@ public final class MtpDevice {
return native_import_file(objectHandle, destPath);
}
+ /**
+ * Copies the data for an object to a file descriptor.
+ * This call may block for an arbitrary amount of time depending on the size
+ * of the data and speed of the devices. The file descriptor is not closed
+ * on completion, and must be done by the caller.
+ *
+ * @param objectHandle handle of the object to read
+ * @param descriptor file descriptor to write the data to for the file transfer.
+ * @return true if the file transfer succeeds
+ */
+ public boolean importFile(int objectHandle, ParcelFileDescriptor descriptor) {
+ return native_import_file(objectHandle, descriptor.getFd());
+ }
+
+ /**
+ * Copies the data for an object from a file descriptor.
+ * This call may block for an arbitrary amount of time depending on the size
+ * of the data and speed of the devices. The file descriptor is not closed
+ * on completion, and must be done by the caller.
+ *
+ * @param objectHandle handle of the target file
+ * @param size size of the file in bytes
+ * @param descriptor file descriptor to read the data from.
+ * @return true if the file transfer succeeds
+ */
+ public boolean sendObject(int objectHandle, int size, ParcelFileDescriptor descriptor) {
+ return native_send_object(objectHandle, size, descriptor.getFd());
+ }
+
+ /**
+ * Uploads an object metadata for a new entry. The {@link MtpObjectInfo} can be
+ * created with the {@link MtpObjectInfo.Builder} class.
+ *
+ * The returned {@link MtpObjectInfo} has the new object handle field filled in.
+ *
+ * @param info metadata of the entry
+ * @return object info of the created entry
+ */
+ public MtpObjectInfo sendObjectInfo(MtpObjectInfo info) {
+ return native_send_object_info(info);
+ }
+
+ /**
+ * Reads an event from the device. It blocks the current thread until it gets an event.
+ * It throws OperationCanceledException if it is cancelled by signal.
+ *
+ * @param signal signal for cancellation
+ * @return obtained event
+ */
+ public MtpEvent readEvent(CancellationSignal signal) {
+ final int handle = native_submit_event_request();
+
+ if (handle < 0) {
+ throw new IllegalStateException("Other thread is reading an event.");
+ }
+
+ if (signal != null) {
+ signal.setOnCancelListener(new CancellationSignal.OnCancelListener() {
+ @Override
+ public void onCancel() {
+ native_discard_event_request(handle);
+ }
+ });
+ }
+
+ try {
+ return native_reap_event_request(handle);
+ } finally {
+ if (signal != null) {
+ signal.setOnCancelListener(null);
+ }
+ }
+ }
+
// used by the JNI code
private long mNativeContext;
@@ -251,4 +328,10 @@ public final class MtpDevice {
private native long native_get_parent(int objectHandle);
private native long native_get_storage_id(int objectHandle);
private native boolean native_import_file(int objectHandle, String destPath);
+ private native boolean native_import_file(int objectHandle, int fd);
+ private native boolean native_send_object(int objectHandle, int size, int fd);
+ private native MtpObjectInfo native_send_object_info(MtpObjectInfo info);
+ private native int native_submit_event_request();
+ private native MtpEvent native_reap_event_request(int handle);
+ private native void native_discard_event_request(int handle);
}