| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.mtp; |
| 18 | |
| Daichi Hirono | 60fa361 | 2016-04-19 12:50:34 +0900 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 21 | import android.hardware.usb.UsbDevice; |
| Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 22 | import android.hardware.usb.UsbDeviceConnection; |
| Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 23 | import android.os.CancellationSignal; |
| Tomasz Mikolajewski | 74d4ff8 | 2015-08-04 18:34:03 +0900 | [diff] [blame] | 24 | import android.os.ParcelFileDescriptor; |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 25 | |
| Daichi Hirono | 2dd4825 | 2016-01-12 15:46:41 +0900 | [diff] [blame] | 26 | import com.android.internal.util.Preconditions; |
| 27 | |
| Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 28 | import java.io.IOException; |
| 29 | |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 30 | /** |
| Scott Main | 0cdd9f7 | 2011-05-05 15:53:44 -0700 | [diff] [blame] | 31 | * This class represents an MTP or PTP device connected on the USB host bus. An application can |
| 32 | * instantiate an object of this type, by referencing an attached {@link |
| 33 | * android.hardware.usb.UsbDevice} and then use methods in this class to get information about the |
| 34 | * device and objects stored on it, as well as open the connection and transfer data. |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 35 | */ |
| 36 | public final class MtpDevice { |
| 37 | |
| 38 | private static final String TAG = "MtpDevice"; |
| 39 | |
| 40 | private final UsbDevice mDevice; |
| 41 | |
| 42 | static { |
| 43 | System.loadLibrary("media_jni"); |
| 44 | } |
| 45 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 46 | /** |
| 47 | * MtpClient constructor |
| 48 | * |
| Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 49 | * @param device the {@link android.hardware.usb.UsbDevice} for the MTP or PTP device |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 50 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 51 | public MtpDevice(@NonNull UsbDevice device) { |
| 52 | Preconditions.checkNotNull(device); |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 53 | mDevice = device; |
| 54 | } |
| 55 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 56 | /** |
| Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 57 | * Opens the MTP device. Once the device is open it takes ownership of the |
| Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 58 | * {@link android.hardware.usb.UsbDeviceConnection}. |
| Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 59 | * The connection will be closed when you call {@link #close()} |
| 60 | * The connection will also be closed if this method fails. |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 61 | * |
| Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 62 | * @param connection an open {@link android.hardware.usb.UsbDeviceConnection} for the device |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 63 | * @return true if the device was successfully opened. |
| 64 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 65 | public boolean open(@NonNull UsbDeviceConnection connection) { |
| Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 66 | boolean result = native_open(mDevice.getDeviceName(), connection.getFileDescriptor()); |
| 67 | if (!result) { |
| 68 | connection.close(); |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 69 | } |
| Mike Lockwood | acc29cc | 2011-03-11 08:18:08 -0500 | [diff] [blame] | 70 | return result; |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 73 | /** |
| Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 74 | * Closes all resources related to the MtpDevice object. |
| 75 | * After this is called, the object can not be used until {@link #open} is called again |
| 76 | * with a new {@link android.hardware.usb.UsbDeviceConnection}. |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 77 | */ |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 78 | public void close() { |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 79 | native_close(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | protected void finalize() throws Throwable { |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 84 | try { |
| 85 | native_close(); |
| 86 | } finally { |
| 87 | super.finalize(); |
| 88 | } |
| 89 | } |
| 90 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 91 | /** |
| 92 | * Returns the name of the USB device |
| Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 93 | * This returns the same value as {@link android.hardware.usb.UsbDevice#getDeviceName} |
| 94 | * for the device's {@link android.hardware.usb.UsbDevice} |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 95 | * |
| 96 | * @return the device name |
| 97 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 98 | public @NonNull String getDeviceName() { |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 99 | return mDevice.getDeviceName(); |
| 100 | } |
| 101 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 102 | /** |
| Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 103 | * Returns the USB ID of the USB device. |
| 104 | * This returns the same value as {@link android.hardware.usb.UsbDevice#getDeviceId} |
| 105 | * for the device's {@link android.hardware.usb.UsbDevice} |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 106 | * |
| 107 | * @return the device ID |
| 108 | */ |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 109 | public int getDeviceId() { |
| 110 | return mDevice.getDeviceId(); |
| 111 | } |
| 112 | |
| 113 | @Override |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 114 | public @NonNull String toString() { |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 115 | return mDevice.getDeviceName(); |
| 116 | } |
| 117 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 118 | /** |
| Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 119 | * Returns the {@link MtpDeviceInfo} for this device |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 120 | * |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 121 | * @return the device info, or null if fetching device info fails |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 122 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 123 | public @Nullable MtpDeviceInfo getDeviceInfo() { |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 124 | return native_get_device_info(); |
| 125 | } |
| 126 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 127 | /** |
| 128 | * Returns the list of IDs for all storage units on this device |
| Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 129 | * Information about each storage unit can be accessed via {@link #getStorageInfo}. |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 130 | * |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 131 | * @return the list of storage IDs, or null if fetching storage IDs fails |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 132 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 133 | public @Nullable int[] getStorageIds() { |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 134 | return native_get_storage_ids(); |
| 135 | } |
| 136 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 137 | /** |
| 138 | * Returns the list of object handles for all objects on the given storage unit, |
| 139 | * with the given format and parent. |
| Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 140 | * Information about each object can be accessed via {@link #getObjectInfo}. |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 141 | * |
| 142 | * @param storageId the storage unit to query |
| 143 | * @param format the format of the object to return, or zero for all formats |
| Daichi Hirono | 660727c | 2015-06-12 10:45:08 +0900 | [diff] [blame] | 144 | * @param objectHandle the parent object to query, -1 for the storage root, |
| 145 | * or zero for all objects |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 146 | * @return the object handles, or null if fetching object handles fails |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 147 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 148 | public @Nullable int[] getObjectHandles(int storageId, int format, int objectHandle) { |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 149 | return native_get_object_handles(storageId, format, objectHandle); |
| 150 | } |
| 151 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 152 | /** |
| 153 | * Returns the data for an object as a byte array. |
| Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 154 | * This call may block for an arbitrary amount of time depending on the size |
| 155 | * of the data and speed of the devices. |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 156 | * |
| 157 | * @param objectHandle handle of the object to read |
| 158 | * @param objectSize the size of the object (this should match |
| Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 159 | * {@link MtpObjectInfo#getCompressedSize}) |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 160 | * @return the object's data, or null if reading fails |
| 161 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 162 | public @Nullable byte[] getObject(int objectHandle, int objectSize) { |
| Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 163 | Preconditions.checkArgumentNonnegative(objectSize, "objectSize should not be negative"); |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 164 | return native_get_object(objectHandle, objectSize); |
| 165 | } |
| 166 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 167 | /** |
| Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 168 | * Obtains object bytes in the specified range and writes it to an array. |
| 169 | * This call may block for an arbitrary amount of time depending on the size |
| 170 | * of the data and speed of the devices. |
| 171 | * |
| 172 | * @param objectHandle handle of the object to read |
| Daichi Hirono | 2dd4825 | 2016-01-12 15:46:41 +0900 | [diff] [blame] | 173 | * @param offset Start index of reading range. It must be a non-negative value at most |
| 174 | * 0xffffffff. |
| Daichi Hirono | 399df70 | 2016-04-13 11:07:44 +0900 | [diff] [blame] | 175 | * @param size Size of reading range. It must be a non-negative value at most Integer.MAX_VALUE |
| 176 | * or 0xffffffff. If 0xffffffff is specified, the method obtains the full bytes of object. |
| Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 177 | * @param buffer Array to write data. |
| 178 | * @return Size of bytes that are actually read. |
| 179 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 180 | public long getPartialObject(int objectHandle, long offset, long size, @NonNull byte[] buffer) |
| Daichi Hirono | 52da3ad | 2015-12-24 17:52:10 +0900 | [diff] [blame] | 181 | throws IOException { |
| 182 | return native_get_partial_object(objectHandle, offset, size, buffer); |
| 183 | } |
| 184 | |
| 185 | /** |
| Daichi Hirono | 038832b | 2016-01-22 19:34:25 +0900 | [diff] [blame] | 186 | * Obtains object bytes in the specified range and writes it to an array. |
| 187 | * This call may block for an arbitrary amount of time depending on the size |
| 188 | * of the data and speed of the devices. |
| 189 | * |
| 190 | * This is a vender-extended operation supported by Android that enables us to pass |
| 191 | * unsigned 64-bit offset. Check if the MTP device supports the operation by using |
| 192 | * {@link MtpDeviceInfo#getOperationsSupported()}. |
| 193 | * |
| 194 | * @param objectHandle handle of the object to read |
| 195 | * @param offset Start index of reading range. It must be a non-negative value. |
| Daichi Hirono | 399df70 | 2016-04-13 11:07:44 +0900 | [diff] [blame] | 196 | * @param size Size of reading range. It must be a non-negative value at most Integer.MAX_VALUE. |
| Daichi Hirono | 038832b | 2016-01-22 19:34:25 +0900 | [diff] [blame] | 197 | * @param buffer Array to write data. |
| 198 | * @return Size of bytes that are actually read. |
| 199 | * @see MtpConstants#OPERATION_GET_PARTIAL_OBJECT_64 |
| 200 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 201 | public long getPartialObject64(int objectHandle, long offset, long size, @NonNull byte[] buffer) |
| Daichi Hirono | 038832b | 2016-01-22 19:34:25 +0900 | [diff] [blame] | 202 | throws IOException { |
| 203 | return native_get_partial_object_64(objectHandle, offset, size, buffer); |
| 204 | } |
| 205 | |
| 206 | /** |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 207 | * Returns the thumbnail data for an object as a byte array. |
| Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 208 | * The size and format of the thumbnail data can be determined via |
| 209 | * {@link MtpObjectInfo#getThumbCompressedSize} and |
| 210 | * {@link MtpObjectInfo#getThumbFormat}. |
| 211 | * For typical devices the format is JPEG. |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 212 | * |
| 213 | * @param objectHandle handle of the object to read |
| 214 | * @return the object's thumbnail, or null if reading fails |
| 215 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 216 | public @Nullable byte[] getThumbnail(int objectHandle) { |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 217 | return native_get_thumbnail(objectHandle); |
| 218 | } |
| 219 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 220 | /** |
| Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 221 | * Retrieves the {@link MtpStorageInfo} for a storage unit. |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 222 | * |
| 223 | * @param storageId the ID of the storage unit |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 224 | * @return the MtpStorageInfo, or null if fetching storage info fails |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 225 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 226 | public @Nullable MtpStorageInfo getStorageInfo(int storageId) { |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 227 | return native_get_storage_info(storageId); |
| 228 | } |
| 229 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 230 | /** |
| Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 231 | * Retrieves the {@link MtpObjectInfo} for an object. |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 232 | * |
| 233 | * @param objectHandle the handle of the object |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 234 | * @return the MtpObjectInfo, or null if fetching object info fails |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 235 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 236 | public @Nullable MtpObjectInfo getObjectInfo(int objectHandle) { |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 237 | return native_get_object_info(objectHandle); |
| 238 | } |
| 239 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 240 | /** |
| Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 241 | * Deletes an object on the device. This call may block, since |
| 242 | * deleting a directory containing many files may take a long time |
| 243 | * on some devices. |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 244 | * |
| 245 | * @param objectHandle handle of the object to delete |
| 246 | * @return true if the deletion succeeds |
| 247 | */ |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 248 | public boolean deleteObject(int objectHandle) { |
| 249 | return native_delete_object(objectHandle); |
| 250 | } |
| 251 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 252 | /** |
| 253 | * Retrieves the object handle for the parent of an object on the device. |
| 254 | * |
| 255 | * @param objectHandle handle of the object to query |
| 256 | * @return the parent's handle, or zero if it is in the root of the storage |
| 257 | */ |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 258 | public long getParent(int objectHandle) { |
| 259 | return native_get_parent(objectHandle); |
| 260 | } |
| 261 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 262 | /** |
| 263 | * Retrieves the ID of the storage unit containing the given object on the device. |
| 264 | * |
| 265 | * @param objectHandle handle of the object to query |
| 266 | * @return the object's storage unit ID |
| 267 | */ |
| Mike Lockwood | 62cfeeb | 2011-03-11 18:39:03 -0500 | [diff] [blame] | 268 | public long getStorageId(int objectHandle) { |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 269 | return native_get_storage_id(objectHandle); |
| 270 | } |
| 271 | |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 272 | /** |
| 273 | * Copies the data for an object to a file in external storage. |
| Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 274 | * This call may block for an arbitrary amount of time depending on the size |
| 275 | * of the data and speed of the devices. |
| Mike Lockwood | 540380f | 2011-02-09 21:48:53 -0500 | [diff] [blame] | 276 | * |
| 277 | * @param objectHandle handle of the object to read |
| 278 | * @param destPath path to destination for the file transfer. |
| 279 | * This path should be in the external storage as defined by |
| 280 | * {@link android.os.Environment#getExternalStorageDirectory} |
| 281 | * @return true if the file transfer succeeds |
| 282 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 283 | public boolean importFile(int objectHandle, @NonNull String destPath) { |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 284 | return native_import_file(objectHandle, destPath); |
| 285 | } |
| 286 | |
| Tomasz Mikolajewski | 74d4ff8 | 2015-08-04 18:34:03 +0900 | [diff] [blame] | 287 | /** |
| 288 | * Copies the data for an object to a file descriptor. |
| 289 | * This call may block for an arbitrary amount of time depending on the size |
| 290 | * of the data and speed of the devices. The file descriptor is not closed |
| 291 | * on completion, and must be done by the caller. |
| 292 | * |
| 293 | * @param objectHandle handle of the object to read |
| 294 | * @param descriptor file descriptor to write the data to for the file transfer. |
| 295 | * @return true if the file transfer succeeds |
| 296 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 297 | public boolean importFile(int objectHandle, @NonNull ParcelFileDescriptor descriptor) { |
| Tomasz Mikolajewski | 74d4ff8 | 2015-08-04 18:34:03 +0900 | [diff] [blame] | 298 | return native_import_file(objectHandle, descriptor.getFd()); |
| 299 | } |
| 300 | |
| Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 301 | /** |
| 302 | * Copies the data for an object from a file descriptor. |
| 303 | * This call may block for an arbitrary amount of time depending on the size |
| 304 | * of the data and speed of the devices. The file descriptor is not closed |
| 305 | * on completion, and must be done by the caller. |
| 306 | * |
| 307 | * @param objectHandle handle of the target file |
| Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 308 | * @param size size of the file in bytes |
| Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 309 | * @param descriptor file descriptor to read the data from. |
| 310 | * @return true if the file transfer succeeds |
| 311 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 312 | public boolean sendObject( |
| 313 | int objectHandle, long size, @NonNull ParcelFileDescriptor descriptor) { |
| Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 314 | return native_send_object(objectHandle, size, descriptor.getFd()); |
| Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Uploads an object metadata for a new entry. The {@link MtpObjectInfo} can be |
| 319 | * created with the {@link MtpObjectInfo.Builder} class. |
| 320 | * |
| 321 | * The returned {@link MtpObjectInfo} has the new object handle field filled in. |
| 322 | * |
| 323 | * @param info metadata of the entry |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 324 | * @return object info of the created entry, or null if sending object info fails |
| Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 325 | */ |
| Daichi Hirono | 452e8fe | 2016-07-05 17:29:48 +0900 | [diff] [blame^] | 326 | public @Nullable MtpObjectInfo sendObjectInfo(@NonNull MtpObjectInfo info) { |
| Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 327 | return native_send_object_info(info); |
| 328 | } |
| 329 | |
| Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 330 | /** |
| 331 | * Reads an event from the device. It blocks the current thread until it gets an event. |
| 332 | * It throws OperationCanceledException if it is cancelled by signal. |
| 333 | * |
| 334 | * @param signal signal for cancellation |
| 335 | * @return obtained event |
| Daichi Hirono | 60fa361 | 2016-04-19 12:50:34 +0900 | [diff] [blame] | 336 | * @throws IOException |
| Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 337 | */ |
| Daichi Hirono | 60fa361 | 2016-04-19 12:50:34 +0900 | [diff] [blame] | 338 | public @NonNull MtpEvent readEvent(@Nullable CancellationSignal signal) throws IOException { |
| Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 339 | final int handle = native_submit_event_request(); |
| Daichi Hirono | 60fa361 | 2016-04-19 12:50:34 +0900 | [diff] [blame] | 340 | Preconditions.checkState(handle >= 0, "Other thread is reading an event."); |
| Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 341 | |
| 342 | if (signal != null) { |
| 343 | signal.setOnCancelListener(new CancellationSignal.OnCancelListener() { |
| 344 | @Override |
| 345 | public void onCancel() { |
| 346 | native_discard_event_request(handle); |
| 347 | } |
| 348 | }); |
| 349 | } |
| 350 | |
| 351 | try { |
| 352 | return native_reap_event_request(handle); |
| 353 | } finally { |
| 354 | if (signal != null) { |
| 355 | signal.setOnCancelListener(null); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| Daichi Hirono | 787821b | 2016-03-24 21:05:51 +0900 | [diff] [blame] | 360 | /** |
| 361 | * Returns object size in 64-bit integer. |
| 362 | * |
| Daichi Hirono | 1337deb | 2016-03-28 08:53:15 +0900 | [diff] [blame] | 363 | * Though MtpObjectInfo#getCompressedSize returns the object size in 32-bit unsigned integer, |
| 364 | * this method returns the object size in 64-bit integer from the object property. Thus it can |
| 365 | * fetch 4GB+ object size correctly. If the device does not support objectSize property, it |
| 366 | * throws IOException. |
| Daichi Hirono | 787821b | 2016-03-24 21:05:51 +0900 | [diff] [blame] | 367 | * @hide |
| 368 | */ |
| 369 | public long getObjectSizeLong(int handle, int format) throws IOException { |
| 370 | return native_get_object_size_long(handle, format); |
| 371 | } |
| 372 | |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 373 | // used by the JNI code |
| Ashok Bhat | e2e5932 | 2013-12-17 19:04:19 +0000 | [diff] [blame] | 374 | private long mNativeContext; |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 375 | |
| 376 | private native boolean native_open(String deviceName, int fd); |
| 377 | private native void native_close(); |
| 378 | private native MtpDeviceInfo native_get_device_info(); |
| 379 | private native int[] native_get_storage_ids(); |
| 380 | private native MtpStorageInfo native_get_storage_info(int storageId); |
| 381 | private native int[] native_get_object_handles(int storageId, int format, int objectHandle); |
| 382 | private native MtpObjectInfo native_get_object_info(int objectHandle); |
| Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 383 | private native byte[] native_get_object(int objectHandle, long objectSize); |
| Daichi Hirono | 2dd4825 | 2016-01-12 15:46:41 +0900 | [diff] [blame] | 384 | private native long native_get_partial_object( |
| 385 | int objectHandle, long offset, long objectSize, byte[] buffer) throws IOException; |
| Daichi Hirono | 038832b | 2016-01-22 19:34:25 +0900 | [diff] [blame] | 386 | private native int native_get_partial_object_64( |
| 387 | int objectHandle, long offset, long objectSize, byte[] buffer) throws IOException; |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 388 | private native byte[] native_get_thumbnail(int objectHandle); |
| 389 | private native boolean native_delete_object(int objectHandle); |
| Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 390 | private native int native_get_parent(int objectHandle); |
| 391 | private native int native_get_storage_id(int objectHandle); |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 392 | private native boolean native_import_file(int objectHandle, String destPath); |
| Tomasz Mikolajewski | 74d4ff8 | 2015-08-04 18:34:03 +0900 | [diff] [blame] | 393 | private native boolean native_import_file(int objectHandle, int fd); |
| Daichi Hirono | e0e6654 | 2016-01-15 14:42:53 +0900 | [diff] [blame] | 394 | private native boolean native_send_object(int objectHandle, long size, int fd); |
| Tomasz Mikolajewski | b049905 | 2015-08-06 19:13:09 +0900 | [diff] [blame] | 395 | private native MtpObjectInfo native_send_object_info(MtpObjectInfo info); |
| Daichi Hirono | 60fa361 | 2016-04-19 12:50:34 +0900 | [diff] [blame] | 396 | private native int native_submit_event_request() throws IOException; |
| 397 | private native MtpEvent native_reap_event_request(int handle) throws IOException; |
| Daichi Hirono | 0b49466 | 2015-09-10 20:38:15 +0900 | [diff] [blame] | 398 | private native void native_discard_event_request(int handle); |
| Daichi Hirono | 787821b | 2016-03-24 21:05:51 +0900 | [diff] [blame] | 399 | private native long native_get_object_size_long(int handle, int format) throws IOException; |
| Mike Lockwood | 8182e72 | 2010-12-30 15:38:45 -0500 | [diff] [blame] | 400 | } |