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