blob: ec925918d4e6a5d3db7305d5c2db99526f2f6c8a [file] [log] [blame]
Mike Lockwood8182e722010-12-30 15:38:45 -05001/*
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
17package android.mtp;
18
Daichi Hirono60fa3612016-04-19 12:50:34 +090019import android.annotation.NonNull;
20import android.annotation.Nullable;
Philip P. Moltmannec3cbb22016-09-14 13:24:52 -070021import android.content.Context;
Mike Lockwoodc4308f02011-03-01 08:04:54 -080022import android.hardware.usb.UsbDevice;
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050023import android.hardware.usb.UsbDeviceConnection;
Daichi Hirono0b494662015-09-10 20:38:15 +090024import android.os.CancellationSignal;
Tomasz Mikolajewski74d4ff82015-08-04 18:34:03 +090025import android.os.ParcelFileDescriptor;
Mike Lockwood8182e722010-12-30 15:38:45 -050026
Philip P. Moltmannec3cbb22016-09-14 13:24:52 -070027import android.os.UserManager;
Philip P. Moltmannb828b772016-09-13 16:35:45 -070028import com.android.internal.annotations.GuardedBy;
Daichi Hirono2dd48252016-01-12 15:46:41 +090029import com.android.internal.util.Preconditions;
Philip P. Moltmannb828b772016-09-13 16:35:45 -070030import dalvik.system.CloseGuard;
Daichi Hirono2dd48252016-01-12 15:46:41 +090031
Daichi Hirono52da3ad2015-12-24 17:52:10 +090032import java.io.IOException;
33
Mike Lockwood8182e722010-12-30 15:38:45 -050034/**
Scott Main0cdd9f72011-05-05 15:53:44 -070035 * 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 Lockwood8182e722010-12-30 15:38:45 -050039 */
40public 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. Moltmannb828b772016-09-13 16:35:45 -070050 /** 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 Lockwood540380f2011-02-09 21:48:53 -050060 /**
61 * MtpClient constructor
62 *
Mike Lockwoodc4308f02011-03-01 08:04:54 -080063 * @param device the {@link android.hardware.usb.UsbDevice} for the MTP or PTP device
Mike Lockwood540380f2011-02-09 21:48:53 -050064 */
Daichi Hirono452e8fe2016-07-05 17:29:48 +090065 public MtpDevice(@NonNull UsbDevice device) {
66 Preconditions.checkNotNull(device);
Mike Lockwood8182e722010-12-30 15:38:45 -050067 mDevice = device;
68 }
69
Mike Lockwood540380f2011-02-09 21:48:53 -050070 /**
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050071 * Opens the MTP device. Once the device is open it takes ownership of the
Daichi Hirono0b494662015-09-10 20:38:15 +090072 * {@link android.hardware.usb.UsbDeviceConnection}.
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050073 * The connection will be closed when you call {@link #close()}
74 * The connection will also be closed if this method fails.
Mike Lockwood540380f2011-02-09 21:48:53 -050075 *
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050076 * @param connection an open {@link android.hardware.usb.UsbDeviceConnection} for the device
Mike Lockwood540380f2011-02-09 21:48:53 -050077 * @return true if the device was successfully opened.
78 */
Daichi Hirono452e8fe2016-07-05 17:29:48 +090079 public boolean open(@NonNull UsbDeviceConnection connection) {
Philip P. Moltmannec3cbb22016-09-14 13:24:52 -070080 boolean result = false;
81
82 Context context = connection.getContext();
Philip P. Moltmannec3cbb22016-09-14 13:24:52 -070083
Philip P. Moltmannb828b772016-09-13 16:35:45 -070084 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. Moltmannec3cbb22016-09-14 13:24:52 -070099 }
100 }
101
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500102 return result;
Mike Lockwood8182e722010-12-30 15:38:45 -0500103 }
104
Mike Lockwood540380f2011-02-09 21:48:53 -0500105 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400106 * 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 Lockwood540380f2011-02-09 21:48:53 -0500109 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500110 public void close() {
Philip P. Moltmannb828b772016-09-13 16:35:45 -0700111 synchronized (mLock) {
112 if (mConnection != null) {
113 mCloseGuard.close();
114
115 native_close();
116
117 mConnection.close();
118 mConnection = null;
119 }
120 }
Mike Lockwood8182e722010-12-30 15:38:45 -0500121 }
122
123 @Override
124 protected void finalize() throws Throwable {
Mike Lockwood8182e722010-12-30 15:38:45 -0500125 try {
Narayan Kamath492e9e82017-03-22 14:28:08 +0000126 if (mCloseGuard != null) {
127 mCloseGuard.warnIfOpen();
128 }
129
Philip P. Moltmannb828b772016-09-13 16:35:45 -0700130 close();
Mike Lockwood8182e722010-12-30 15:38:45 -0500131 } finally {
132 super.finalize();
133 }
134 }
135
Mike Lockwood540380f2011-02-09 21:48:53 -0500136 /**
137 * Returns the name of the USB device
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400138 * This returns the same value as {@link android.hardware.usb.UsbDevice#getDeviceName}
139 * for the device's {@link android.hardware.usb.UsbDevice}
Mike Lockwood540380f2011-02-09 21:48:53 -0500140 *
141 * @return the device name
142 */
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900143 public @NonNull String getDeviceName() {
Mike Lockwood8182e722010-12-30 15:38:45 -0500144 return mDevice.getDeviceName();
145 }
146
Mike Lockwood540380f2011-02-09 21:48:53 -0500147 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400148 * 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 Lockwood540380f2011-02-09 21:48:53 -0500151 *
152 * @return the device ID
153 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500154 public int getDeviceId() {
155 return mDevice.getDeviceId();
156 }
157
158 @Override
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900159 public @NonNull String toString() {
Mike Lockwood8182e722010-12-30 15:38:45 -0500160 return mDevice.getDeviceName();
161 }
162
Mike Lockwood540380f2011-02-09 21:48:53 -0500163 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400164 * Returns the {@link MtpDeviceInfo} for this device
Mike Lockwood540380f2011-02-09 21:48:53 -0500165 *
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900166 * @return the device info, or null if fetching device info fails
Mike Lockwood540380f2011-02-09 21:48:53 -0500167 */
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900168 public @Nullable MtpDeviceInfo getDeviceInfo() {
Mike Lockwood8182e722010-12-30 15:38:45 -0500169 return native_get_device_info();
170 }
171
Mike Lockwood540380f2011-02-09 21:48:53 -0500172 /**
James Wei4f1a6d62021-06-19 20:58:00 +0800173 * 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 Lockwood540380f2011-02-09 21:48:53 -0500185 * Returns the list of IDs for all storage units on this device
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400186 * Information about each storage unit can be accessed via {@link #getStorageInfo}.
Mike Lockwood540380f2011-02-09 21:48:53 -0500187 *
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900188 * @return the list of storage IDs, or null if fetching storage IDs fails
Mike Lockwood540380f2011-02-09 21:48:53 -0500189 */
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900190 public @Nullable int[] getStorageIds() {
Mike Lockwood8182e722010-12-30 15:38:45 -0500191 return native_get_storage_ids();
192 }
193
Mike Lockwood540380f2011-02-09 21:48:53 -0500194 /**
195 * Returns the list of object handles for all objects on the given storage unit,
196 * with the given format and parent.
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400197 * Information about each object can be accessed via {@link #getObjectInfo}.
Mike Lockwood540380f2011-02-09 21:48:53 -0500198 *
199 * @param storageId the storage unit to query
200 * @param format the format of the object to return, or zero for all formats
Daichi Hirono660727c2015-06-12 10:45:08 +0900201 * @param objectHandle the parent object to query, -1 for the storage root,
202 * or zero for all objects
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900203 * @return the object handles, or null if fetching object handles fails
Mike Lockwood540380f2011-02-09 21:48:53 -0500204 */
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900205 public @Nullable int[] getObjectHandles(int storageId, int format, int objectHandle) {
Mike Lockwood8182e722010-12-30 15:38:45 -0500206 return native_get_object_handles(storageId, format, objectHandle);
207 }
208
Mike Lockwood540380f2011-02-09 21:48:53 -0500209 /**
210 * Returns the data for an object as a byte array.
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400211 * This call may block for an arbitrary amount of time depending on the size
212 * of the data and speed of the devices.
Mike Lockwood540380f2011-02-09 21:48:53 -0500213 *
214 * @param objectHandle handle of the object to read
215 * @param objectSize the size of the object (this should match
Daichi Hironoe0e66542016-01-15 14:42:53 +0900216 * {@link MtpObjectInfo#getCompressedSize})
Mike Lockwood540380f2011-02-09 21:48:53 -0500217 * @return the object's data, or null if reading fails
218 */
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900219 public @Nullable byte[] getObject(int objectHandle, int objectSize) {
Daichi Hironoe0e66542016-01-15 14:42:53 +0900220 Preconditions.checkArgumentNonnegative(objectSize, "objectSize should not be negative");
Mike Lockwood8182e722010-12-30 15:38:45 -0500221 return native_get_object(objectHandle, objectSize);
222 }
223
Mike Lockwood540380f2011-02-09 21:48:53 -0500224 /**
Daichi Hirono52da3ad2015-12-24 17:52:10 +0900225 * 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 Hirono2dd48252016-01-12 15:46:41 +0900230 * @param offset Start index of reading range. It must be a non-negative value at most
231 * 0xffffffff.
Daichi Hirono399df702016-04-13 11:07:44 +0900232 * @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 Hirono52da3ad2015-12-24 17:52:10 +0900234 * @param buffer Array to write data.
235 * @return Size of bytes that are actually read.
236 */
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900237 public long getPartialObject(int objectHandle, long offset, long size, @NonNull byte[] buffer)
Daichi Hirono52da3ad2015-12-24 17:52:10 +0900238 throws IOException {
239 return native_get_partial_object(objectHandle, offset, size, buffer);
240 }
241
242 /**
Daichi Hirono038832b2016-01-22 19:34:25 +0900243 * 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 Hirono399df702016-04-13 11:07:44 +0900253 * @param size Size of reading range. It must be a non-negative value at most Integer.MAX_VALUE.
Daichi Hirono038832b2016-01-22 19:34:25 +0900254 * @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 Hirono452e8fe2016-07-05 17:29:48 +0900258 public long getPartialObject64(int objectHandle, long offset, long size, @NonNull byte[] buffer)
Daichi Hirono038832b2016-01-22 19:34:25 +0900259 throws IOException {
260 return native_get_partial_object_64(objectHandle, offset, size, buffer);
261 }
262
263 /**
Mike Lockwood540380f2011-02-09 21:48:53 -0500264 * Returns the thumbnail data for an object as a byte array.
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400265 * 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 Lockwood540380f2011-02-09 21:48:53 -0500269 *
270 * @param objectHandle handle of the object to read
271 * @return the object's thumbnail, or null if reading fails
272 */
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900273 public @Nullable byte[] getThumbnail(int objectHandle) {
Mike Lockwood8182e722010-12-30 15:38:45 -0500274 return native_get_thumbnail(objectHandle);
275 }
276
Mike Lockwood540380f2011-02-09 21:48:53 -0500277 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400278 * Retrieves the {@link MtpStorageInfo} for a storage unit.
Mike Lockwood540380f2011-02-09 21:48:53 -0500279 *
280 * @param storageId the ID of the storage unit
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900281 * @return the MtpStorageInfo, or null if fetching storage info fails
Mike Lockwood540380f2011-02-09 21:48:53 -0500282 */
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900283 public @Nullable MtpStorageInfo getStorageInfo(int storageId) {
Mike Lockwood8182e722010-12-30 15:38:45 -0500284 return native_get_storage_info(storageId);
285 }
286
Mike Lockwood540380f2011-02-09 21:48:53 -0500287 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400288 * Retrieves the {@link MtpObjectInfo} for an object.
Mike Lockwood540380f2011-02-09 21:48:53 -0500289 *
290 * @param objectHandle the handle of the object
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900291 * @return the MtpObjectInfo, or null if fetching object info fails
Mike Lockwood540380f2011-02-09 21:48:53 -0500292 */
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900293 public @Nullable MtpObjectInfo getObjectInfo(int objectHandle) {
Mike Lockwood8182e722010-12-30 15:38:45 -0500294 return native_get_object_info(objectHandle);
295 }
296
Mike Lockwood540380f2011-02-09 21:48:53 -0500297 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400298 * 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 Lockwood540380f2011-02-09 21:48:53 -0500301 *
302 * @param objectHandle handle of the object to delete
303 * @return true if the deletion succeeds
304 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500305 public boolean deleteObject(int objectHandle) {
306 return native_delete_object(objectHandle);
307 }
308
Mike Lockwood540380f2011-02-09 21:48:53 -0500309 /**
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 Lockwood8182e722010-12-30 15:38:45 -0500315 public long getParent(int objectHandle) {
316 return native_get_parent(objectHandle);
317 }
318
Mike Lockwood540380f2011-02-09 21:48:53 -0500319 /**
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 Lockwood62cfeeb2011-03-11 18:39:03 -0500325 public long getStorageId(int objectHandle) {
Mike Lockwood8182e722010-12-30 15:38:45 -0500326 return native_get_storage_id(objectHandle);
327 }
328
Mike Lockwood540380f2011-02-09 21:48:53 -0500329 /**
330 * Copies the data for an object to a file in external storage.
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400331 * This call may block for an arbitrary amount of time depending on the size
332 * of the data and speed of the devices.
Mike Lockwood540380f2011-02-09 21:48:53 -0500333 *
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 Hirono452e8fe2016-07-05 17:29:48 +0900340 public boolean importFile(int objectHandle, @NonNull String destPath) {
Mike Lockwood8182e722010-12-30 15:38:45 -0500341 return native_import_file(objectHandle, destPath);
342 }
343
Tomasz Mikolajewski74d4ff82015-08-04 18:34:03 +0900344 /**
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 Hirono452e8fe2016-07-05 17:29:48 +0900354 public boolean importFile(int objectHandle, @NonNull ParcelFileDescriptor descriptor) {
Tomasz Mikolajewski74d4ff82015-08-04 18:34:03 +0900355 return native_import_file(objectHandle, descriptor.getFd());
356 }
357
Tomasz Mikolajewskib0499052015-08-06 19:13:09 +0900358 /**
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 Mikolajewskib80a3cf2015-08-24 16:10:51 +0900365 * @param size size of the file in bytes
Tomasz Mikolajewskib0499052015-08-06 19:13:09 +0900366 * @param descriptor file descriptor to read the data from.
367 * @return true if the file transfer succeeds
368 */
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900369 public boolean sendObject(
370 int objectHandle, long size, @NonNull ParcelFileDescriptor descriptor) {
Tomasz Mikolajewskib80a3cf2015-08-24 16:10:51 +0900371 return native_send_object(objectHandle, size, descriptor.getFd());
Tomasz Mikolajewskib0499052015-08-06 19:13:09 +0900372 }
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 Hirono452e8fe2016-07-05 17:29:48 +0900381 * @return object info of the created entry, or null if sending object info fails
Tomasz Mikolajewskib0499052015-08-06 19:13:09 +0900382 */
Daichi Hirono452e8fe2016-07-05 17:29:48 +0900383 public @Nullable MtpObjectInfo sendObjectInfo(@NonNull MtpObjectInfo info) {
Tomasz Mikolajewskib0499052015-08-06 19:13:09 +0900384 return native_send_object_info(info);
385 }
386
Daichi Hirono0b494662015-09-10 20:38:15 +0900387 /**
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 Hirono60fa3612016-04-19 12:50:34 +0900393 * @throws IOException
Daichi Hirono0b494662015-09-10 20:38:15 +0900394 */
Daichi Hirono60fa3612016-04-19 12:50:34 +0900395 public @NonNull MtpEvent readEvent(@Nullable CancellationSignal signal) throws IOException {
Daichi Hirono0b494662015-09-10 20:38:15 +0900396 final int handle = native_submit_event_request();
Daichi Hirono60fa3612016-04-19 12:50:34 +0900397 Preconditions.checkState(handle >= 0, "Other thread is reading an event.");
Daichi Hirono0b494662015-09-10 20:38:15 +0900398
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 Hirono787821b2016-03-24 21:05:51 +0900417 /**
418 * Returns object size in 64-bit integer.
419 *
Daichi Hirono1337deb2016-03-28 08:53:15 +0900420 * 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 Hirono787821b2016-03-24 21:05:51 +0900424 * @hide
425 */
426 public long getObjectSizeLong(int handle, int format) throws IOException {
427 return native_get_object_size_long(handle, format);
428 }
429
Mike Lockwood8182e722010-12-30 15:38:45 -0500430 // used by the JNI code
Ashok Bhate2e59322013-12-17 19:04:19 +0000431 private long mNativeContext;
Mike Lockwood8182e722010-12-30 15:38:45 -0500432
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 Wei4f1a6d62021-06-19 20:58:00 +0800436 private native int native_set_device_property_init_version(String propertyStr);
Mike Lockwood8182e722010-12-30 15:38:45 -0500437 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 Hironoe0e66542016-01-15 14:42:53 +0900441 private native byte[] native_get_object(int objectHandle, long objectSize);
Daichi Hirono2dd48252016-01-12 15:46:41 +0900442 private native long native_get_partial_object(
443 int objectHandle, long offset, long objectSize, byte[] buffer) throws IOException;
Daichi Hirono038832b2016-01-22 19:34:25 +0900444 private native int native_get_partial_object_64(
445 int objectHandle, long offset, long objectSize, byte[] buffer) throws IOException;
Mike Lockwood8182e722010-12-30 15:38:45 -0500446 private native byte[] native_get_thumbnail(int objectHandle);
447 private native boolean native_delete_object(int objectHandle);
Daichi Hironoe0e66542016-01-15 14:42:53 +0900448 private native int native_get_parent(int objectHandle);
449 private native int native_get_storage_id(int objectHandle);
Mike Lockwood8182e722010-12-30 15:38:45 -0500450 private native boolean native_import_file(int objectHandle, String destPath);
Tomasz Mikolajewski74d4ff82015-08-04 18:34:03 +0900451 private native boolean native_import_file(int objectHandle, int fd);
Daichi Hironoe0e66542016-01-15 14:42:53 +0900452 private native boolean native_send_object(int objectHandle, long size, int fd);
Tomasz Mikolajewskib0499052015-08-06 19:13:09 +0900453 private native MtpObjectInfo native_send_object_info(MtpObjectInfo info);
Daichi Hirono60fa3612016-04-19 12:50:34 +0900454 private native int native_submit_event_request() throws IOException;
455 private native MtpEvent native_reap_event_request(int handle) throws IOException;
Daichi Hirono0b494662015-09-10 20:38:15 +0900456 private native void native_discard_event_request(int handle);
Daichi Hirono787821b2016-03-24 21:05:51 +0900457 private native long native_get_object_size_long(int handle, int format) throws IOException;
Mike Lockwood8182e722010-12-30 15:38:45 -0500458}