blob: 72dcaa8409a99e5b9fab045391dbbae658da6074 [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
Mike Lockwoodc4308f02011-03-01 08:04:54 -080019import android.hardware.usb.UsbDevice;
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050020import android.hardware.usb.UsbDeviceConnection;
Mike Lockwood8182e722010-12-30 15:38:45 -050021
22/**
Scott Main0cdd9f72011-05-05 15:53:44 -070023 * This class represents an MTP or PTP device connected on the USB host bus. An application can
24 * instantiate an object of this type, by referencing an attached {@link
25 * android.hardware.usb.UsbDevice} and then use methods in this class to get information about the
26 * device and objects stored on it, as well as open the connection and transfer data.
Mike Lockwood8182e722010-12-30 15:38:45 -050027 */
28public final class MtpDevice {
29
30 private static final String TAG = "MtpDevice";
31
32 private final UsbDevice mDevice;
33
34 static {
35 System.loadLibrary("media_jni");
36 }
37
Mike Lockwood540380f2011-02-09 21:48:53 -050038 /**
39 * MtpClient constructor
40 *
Mike Lockwoodc4308f02011-03-01 08:04:54 -080041 * @param device the {@link android.hardware.usb.UsbDevice} for the MTP or PTP device
Mike Lockwood540380f2011-02-09 21:48:53 -050042 */
Mike Lockwood8182e722010-12-30 15:38:45 -050043 public MtpDevice(UsbDevice device) {
44 mDevice = device;
45 }
46
Mike Lockwood540380f2011-02-09 21:48:53 -050047 /**
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050048 * Opens the MTP device. Once the device is open it takes ownership of the
49 * {@link android.hardware.usb.UsbDeviceConnection}.
50 * The connection will be closed when you call {@link #close()}
51 * The connection will also be closed if this method fails.
Mike Lockwood540380f2011-02-09 21:48:53 -050052 *
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050053 * @param connection an open {@link android.hardware.usb.UsbDeviceConnection} for the device
Mike Lockwood540380f2011-02-09 21:48:53 -050054 * @return true if the device was successfully opened.
55 */
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050056 public boolean open(UsbDeviceConnection connection) {
57 boolean result = native_open(mDevice.getDeviceName(), connection.getFileDescriptor());
58 if (!result) {
59 connection.close();
Mike Lockwood8182e722010-12-30 15:38:45 -050060 }
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050061 return result;
Mike Lockwood8182e722010-12-30 15:38:45 -050062 }
63
Mike Lockwood540380f2011-02-09 21:48:53 -050064 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -040065 * Closes all resources related to the MtpDevice object.
66 * After this is called, the object can not be used until {@link #open} is called again
67 * with a new {@link android.hardware.usb.UsbDeviceConnection}.
Mike Lockwood540380f2011-02-09 21:48:53 -050068 */
Mike Lockwood8182e722010-12-30 15:38:45 -050069 public void close() {
Mike Lockwood8182e722010-12-30 15:38:45 -050070 native_close();
71 }
72
73 @Override
74 protected void finalize() throws Throwable {
Mike Lockwood8182e722010-12-30 15:38:45 -050075 try {
76 native_close();
77 } finally {
78 super.finalize();
79 }
80 }
81
Mike Lockwood540380f2011-02-09 21:48:53 -050082 /**
83 * Returns the name of the USB device
Mike Lockwood11dd5ae2011-04-01 14:00:08 -040084 * This returns the same value as {@link android.hardware.usb.UsbDevice#getDeviceName}
85 * for the device's {@link android.hardware.usb.UsbDevice}
Mike Lockwood540380f2011-02-09 21:48:53 -050086 *
87 * @return the device name
88 */
Mike Lockwood8182e722010-12-30 15:38:45 -050089 public String getDeviceName() {
90 return mDevice.getDeviceName();
91 }
92
Mike Lockwood540380f2011-02-09 21:48:53 -050093 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -040094 * Returns the USB ID of the USB device.
95 * This returns the same value as {@link android.hardware.usb.UsbDevice#getDeviceId}
96 * for the device's {@link android.hardware.usb.UsbDevice}
Mike Lockwood540380f2011-02-09 21:48:53 -050097 *
98 * @return the device ID
99 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500100 public int getDeviceId() {
101 return mDevice.getDeviceId();
102 }
103
104 @Override
105 public String toString() {
106 return mDevice.getDeviceName();
107 }
108
Mike Lockwood540380f2011-02-09 21:48:53 -0500109 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400110 * Returns the {@link MtpDeviceInfo} for this device
Mike Lockwood540380f2011-02-09 21:48:53 -0500111 *
112 * @return the device info
113 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500114 public MtpDeviceInfo getDeviceInfo() {
115 return native_get_device_info();
116 }
117
Mike Lockwood540380f2011-02-09 21:48:53 -0500118 /**
119 * Returns the list of IDs for all storage units on this device
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400120 * Information about each storage unit can be accessed via {@link #getStorageInfo}.
Mike Lockwood540380f2011-02-09 21:48:53 -0500121 *
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400122 * @return the list of storage IDs
Mike Lockwood540380f2011-02-09 21:48:53 -0500123 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500124 public int[] getStorageIds() {
125 return native_get_storage_ids();
126 }
127
Mike Lockwood540380f2011-02-09 21:48:53 -0500128 /**
129 * Returns the list of object handles for all objects on the given storage unit,
130 * with the given format and parent.
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400131 * Information about each object can be accessed via {@link #getObjectInfo}.
Mike Lockwood540380f2011-02-09 21:48:53 -0500132 *
133 * @param storageId the storage unit to query
134 * @param format the format of the object to return, or zero for all formats
135 * @param objectHandle the parent object to query, or zero for the storage root
136 * @return the object handles
137 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500138 public int[] getObjectHandles(int storageId, int format, int objectHandle) {
139 return native_get_object_handles(storageId, format, objectHandle);
140 }
141
Mike Lockwood540380f2011-02-09 21:48:53 -0500142 /**
143 * Returns the data for an object as a byte array.
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400144 * This call may block for an arbitrary amount of time depending on the size
145 * of the data and speed of the devices.
Mike Lockwood540380f2011-02-09 21:48:53 -0500146 *
147 * @param objectHandle handle of the object to read
148 * @param objectSize the size of the object (this should match
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400149 * {@link MtpObjectInfo#getCompressedSize}
Mike Lockwood540380f2011-02-09 21:48:53 -0500150 * @return the object's data, or null if reading fails
151 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500152 public byte[] getObject(int objectHandle, int objectSize) {
153 return native_get_object(objectHandle, objectSize);
154 }
155
Mike Lockwood540380f2011-02-09 21:48:53 -0500156 /**
157 * Returns the thumbnail data for an object as a byte array.
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400158 * The size and format of the thumbnail data can be determined via
159 * {@link MtpObjectInfo#getThumbCompressedSize} and
160 * {@link MtpObjectInfo#getThumbFormat}.
161 * For typical devices the format is JPEG.
Mike Lockwood540380f2011-02-09 21:48:53 -0500162 *
163 * @param objectHandle handle of the object to read
164 * @return the object's thumbnail, or null if reading fails
165 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500166 public byte[] getThumbnail(int objectHandle) {
167 return native_get_thumbnail(objectHandle);
168 }
169
Mike Lockwood540380f2011-02-09 21:48:53 -0500170 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400171 * Retrieves the {@link MtpStorageInfo} for a storage unit.
Mike Lockwood540380f2011-02-09 21:48:53 -0500172 *
173 * @param storageId the ID of the storage unit
174 * @return the MtpStorageInfo
175 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500176 public MtpStorageInfo getStorageInfo(int storageId) {
177 return native_get_storage_info(storageId);
178 }
179
Mike Lockwood540380f2011-02-09 21:48:53 -0500180 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400181 * Retrieves the {@link MtpObjectInfo} for an object.
Mike Lockwood540380f2011-02-09 21:48:53 -0500182 *
183 * @param objectHandle the handle of the object
184 * @return the MtpObjectInfo
185 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500186 public MtpObjectInfo getObjectInfo(int objectHandle) {
187 return native_get_object_info(objectHandle);
188 }
189
Mike Lockwood540380f2011-02-09 21:48:53 -0500190 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400191 * Deletes an object on the device. This call may block, since
192 * deleting a directory containing many files may take a long time
193 * on some devices.
Mike Lockwood540380f2011-02-09 21:48:53 -0500194 *
195 * @param objectHandle handle of the object to delete
196 * @return true if the deletion succeeds
197 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500198 public boolean deleteObject(int objectHandle) {
199 return native_delete_object(objectHandle);
200 }
201
Mike Lockwood540380f2011-02-09 21:48:53 -0500202 /**
203 * Retrieves the object handle for the parent of an object on the device.
204 *
205 * @param objectHandle handle of the object to query
206 * @return the parent's handle, or zero if it is in the root of the storage
207 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500208 public long getParent(int objectHandle) {
209 return native_get_parent(objectHandle);
210 }
211
Mike Lockwood540380f2011-02-09 21:48:53 -0500212 /**
213 * Retrieves the ID of the storage unit containing the given object on the device.
214 *
215 * @param objectHandle handle of the object to query
216 * @return the object's storage unit ID
217 */
Mike Lockwood62cfeeb2011-03-11 18:39:03 -0500218 public long getStorageId(int objectHandle) {
Mike Lockwood8182e722010-12-30 15:38:45 -0500219 return native_get_storage_id(objectHandle);
220 }
221
Mike Lockwood540380f2011-02-09 21:48:53 -0500222 /**
223 * Copies the data for an object to a file in external storage.
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400224 * This call may block for an arbitrary amount of time depending on the size
225 * of the data and speed of the devices.
Mike Lockwood540380f2011-02-09 21:48:53 -0500226 *
227 * @param objectHandle handle of the object to read
228 * @param destPath path to destination for the file transfer.
229 * This path should be in the external storage as defined by
230 * {@link android.os.Environment#getExternalStorageDirectory}
231 * @return true if the file transfer succeeds
232 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500233 public boolean importFile(int objectHandle, String destPath) {
234 return native_import_file(objectHandle, destPath);
235 }
236
237 // used by the JNI code
Ashok Bhate2e59322013-12-17 19:04:19 +0000238 private long mNativeContext;
Mike Lockwood8182e722010-12-30 15:38:45 -0500239
240 private native boolean native_open(String deviceName, int fd);
241 private native void native_close();
242 private native MtpDeviceInfo native_get_device_info();
243 private native int[] native_get_storage_ids();
244 private native MtpStorageInfo native_get_storage_info(int storageId);
245 private native int[] native_get_object_handles(int storageId, int format, int objectHandle);
246 private native MtpObjectInfo native_get_object_info(int objectHandle);
247 private native byte[] native_get_object(int objectHandle, int objectSize);
248 private native byte[] native_get_thumbnail(int objectHandle);
249 private native boolean native_delete_object(int objectHandle);
250 private native long native_get_parent(int objectHandle);
251 private native long native_get_storage_id(int objectHandle);
252 private native boolean native_import_file(int objectHandle, String destPath);
253}