blob: af37c9e509cbd51a44141084d79617ea7b0340af [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 -050021import android.os.ParcelFileDescriptor;
22import android.util.Log;
23
24/**
Mike Lockwood540380f2011-02-09 21:48:53 -050025 * This class represents an MTP or PTP device connected on the USB host bus.
Mike Lockwood8182e722010-12-30 15:38:45 -050026 */
27public final class MtpDevice {
28
29 private static final String TAG = "MtpDevice";
30
31 private final UsbDevice mDevice;
32
33 static {
34 System.loadLibrary("media_jni");
35 }
36
Mike Lockwood540380f2011-02-09 21:48:53 -050037 /**
38 * MtpClient constructor
39 *
Mike Lockwoodc4308f02011-03-01 08:04:54 -080040 * @param device the {@link android.hardware.usb.UsbDevice} for the MTP or PTP device
Mike Lockwood540380f2011-02-09 21:48:53 -050041 */
Mike Lockwood8182e722010-12-30 15:38:45 -050042 public MtpDevice(UsbDevice device) {
43 mDevice = device;
44 }
45
Mike Lockwood540380f2011-02-09 21:48:53 -050046 /**
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050047 * Opens the MTP device. Once the device is open it takes ownership of the
48 * {@link android.hardware.usb.UsbDeviceConnection}.
49 * The connection will be closed when you call {@link #close()}
50 * The connection will also be closed if this method fails.
Mike Lockwood540380f2011-02-09 21:48:53 -050051 *
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050052 * @param connection an open {@link android.hardware.usb.UsbDeviceConnection} for the device
Mike Lockwood540380f2011-02-09 21:48:53 -050053 * @return true if the device was successfully opened.
54 */
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050055 public boolean open(UsbDeviceConnection connection) {
56 boolean result = native_open(mDevice.getDeviceName(), connection.getFileDescriptor());
57 if (!result) {
58 connection.close();
Mike Lockwood8182e722010-12-30 15:38:45 -050059 }
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050060 return result;
Mike Lockwood8182e722010-12-30 15:38:45 -050061 }
62
Mike Lockwood540380f2011-02-09 21:48:53 -050063 /**
64 * Closes all resources related to the MtpDevice object
65 */
Mike Lockwood8182e722010-12-30 15:38:45 -050066 public void close() {
Mike Lockwood8182e722010-12-30 15:38:45 -050067 native_close();
68 }
69
70 @Override
71 protected void finalize() throws Throwable {
Mike Lockwood8182e722010-12-30 15:38:45 -050072 try {
73 native_close();
74 } finally {
75 super.finalize();
76 }
77 }
78
Mike Lockwood540380f2011-02-09 21:48:53 -050079 /**
80 * Returns the name of the USB device
81 *
82 * @return the device name
83 */
Mike Lockwood8182e722010-12-30 15:38:45 -050084 public String getDeviceName() {
85 return mDevice.getDeviceName();
86 }
87
Mike Lockwood540380f2011-02-09 21:48:53 -050088 /**
89 * Returns the ID of the USB device
90 *
91 * @return the device ID
92 */
Mike Lockwood8182e722010-12-30 15:38:45 -050093 public int getDeviceId() {
94 return mDevice.getDeviceId();
95 }
96
97 @Override
98 public String toString() {
99 return mDevice.getDeviceName();
100 }
101
Mike Lockwood540380f2011-02-09 21:48:53 -0500102 /**
103 * Returns the {@link android.mtp.MtpDeviceInfo} for this device
104 *
105 * @return the device info
106 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500107 public MtpDeviceInfo getDeviceInfo() {
108 return native_get_device_info();
109 }
110
Mike Lockwood540380f2011-02-09 21:48:53 -0500111 /**
112 * Returns the list of IDs for all storage units on this device
113 *
114 * @return the storage IDs
115 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500116 public int[] getStorageIds() {
117 return native_get_storage_ids();
118 }
119
Mike Lockwood540380f2011-02-09 21:48:53 -0500120 /**
121 * Returns the list of object handles for all objects on the given storage unit,
122 * with the given format and parent.
123 *
124 * @param storageId the storage unit to query
125 * @param format the format of the object to return, or zero for all formats
126 * @param objectHandle the parent object to query, or zero for the storage root
127 * @return the object handles
128 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500129 public int[] getObjectHandles(int storageId, int format, int objectHandle) {
130 return native_get_object_handles(storageId, format, objectHandle);
131 }
132
Mike Lockwood540380f2011-02-09 21:48:53 -0500133 /**
134 * Returns the data for an object as a byte array.
135 *
136 * @param objectHandle handle of the object to read
137 * @param objectSize the size of the object (this should match
138 * {@link android.mtp.MtpObjectInfo#getCompressedSize}
139 * @return the object's data, or null if reading fails
140 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500141 public byte[] getObject(int objectHandle, int objectSize) {
142 return native_get_object(objectHandle, objectSize);
143 }
144
Mike Lockwood540380f2011-02-09 21:48:53 -0500145 /**
146 * Returns the thumbnail data for an object as a byte array.
147 *
148 * @param objectHandle handle of the object to read
149 * @return the object's thumbnail, or null if reading fails
150 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500151 public byte[] getThumbnail(int objectHandle) {
152 return native_get_thumbnail(objectHandle);
153 }
154
Mike Lockwood540380f2011-02-09 21:48:53 -0500155 /**
156 * Retrieves the {@link android.mtp.MtpStorageInfo} for a storage unit.
157 *
158 * @param storageId the ID of the storage unit
159 * @return the MtpStorageInfo
160 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500161 public MtpStorageInfo getStorageInfo(int storageId) {
162 return native_get_storage_info(storageId);
163 }
164
Mike Lockwood540380f2011-02-09 21:48:53 -0500165 /**
166 * Retrieves the {@link android.mtp.MtpObjectInfo} for an object.
167 *
168 * @param objectHandle the handle of the object
169 * @return the MtpObjectInfo
170 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500171 public MtpObjectInfo getObjectInfo(int objectHandle) {
172 return native_get_object_info(objectHandle);
173 }
174
Mike Lockwood540380f2011-02-09 21:48:53 -0500175 /**
176 * Deletes an object on the device.
177 *
178 * @param objectHandle handle of the object to delete
179 * @return true if the deletion succeeds
180 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500181 public boolean deleteObject(int objectHandle) {
182 return native_delete_object(objectHandle);
183 }
184
Mike Lockwood540380f2011-02-09 21:48:53 -0500185 /**
186 * Retrieves the object handle for the parent of an object on the device.
187 *
188 * @param objectHandle handle of the object to query
189 * @return the parent's handle, or zero if it is in the root of the storage
190 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500191 public long getParent(int objectHandle) {
192 return native_get_parent(objectHandle);
193 }
194
Mike Lockwood540380f2011-02-09 21:48:53 -0500195 /**
196 * Retrieves the ID of the storage unit containing the given object on the device.
197 *
198 * @param objectHandle handle of the object to query
199 * @return the object's storage unit ID
200 */
Mike Lockwood62cfeeb2011-03-11 18:39:03 -0500201 public long getStorageId(int objectHandle) {
Mike Lockwood8182e722010-12-30 15:38:45 -0500202 return native_get_storage_id(objectHandle);
203 }
204
Mike Lockwood540380f2011-02-09 21:48:53 -0500205 /**
206 * Copies the data for an object to a file in external storage.
207 *
208 * @param objectHandle handle of the object to read
209 * @param destPath path to destination for the file transfer.
210 * This path should be in the external storage as defined by
211 * {@link android.os.Environment#getExternalStorageDirectory}
212 * @return true if the file transfer succeeds
213 */
Mike Lockwood8182e722010-12-30 15:38:45 -0500214 public boolean importFile(int objectHandle, String destPath) {
215 return native_import_file(objectHandle, destPath);
216 }
217
218 // used by the JNI code
219 private int mNativeContext;
220
221 private native boolean native_open(String deviceName, int fd);
222 private native void native_close();
223 private native MtpDeviceInfo native_get_device_info();
224 private native int[] native_get_storage_ids();
225 private native MtpStorageInfo native_get_storage_info(int storageId);
226 private native int[] native_get_object_handles(int storageId, int format, int objectHandle);
227 private native MtpObjectInfo native_get_object_info(int objectHandle);
228 private native byte[] native_get_object(int objectHandle, int objectSize);
229 private native byte[] native_get_thumbnail(int objectHandle);
230 private native boolean native_delete_object(int objectHandle);
231 private native long native_get_parent(int objectHandle);
232 private native long native_get_storage_id(int objectHandle);
233 private native boolean native_import_file(int objectHandle, String destPath);
234}